Original kernel 2.4.37.5
[tomato.git] / release / src / linux / linux / fs / proc / generic.c
blob896e79a460fca28f93a318755461228d9821b879
1 /*
2 * proc/fs/generic.c --- generic routines for the proc-fs
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 */
11 #include <asm/uaccess.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 #define __NO_VERSION__
18 #include <linux/module.h>
19 #include <asm/bitops.h>
21 static ssize_t proc_file_read(struct file * file, char * buf,
22 size_t nbytes, loff_t *ppos);
23 static ssize_t proc_file_write(struct file * file, const char * buffer,
24 size_t count, loff_t *ppos);
25 static loff_t proc_file_lseek(struct file *, loff_t, int);
27 int proc_match(int len, const char *name,struct proc_dir_entry * de)
29 if (!de || !de->low_ino)
30 return 0;
31 if (de->namelen != len)
32 return 0;
33 return !memcmp(name, de->name, len);
36 static struct file_operations proc_file_operations = {
37 llseek: proc_file_lseek,
38 read: proc_file_read,
39 write: proc_file_write,
42 #ifndef MIN
43 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
44 #endif
46 /* buffer size is one page but our output routines use some slack for overruns */
47 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
49 static ssize_t
50 proc_file_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
52 struct inode * inode = file->f_dentry->d_inode;
53 char *page;
54 ssize_t retval=0;
55 int eof=0;
56 ssize_t n, count;
57 char *start;
58 struct proc_dir_entry * dp;
59 loff_t pos = *ppos;
61 dp = (struct proc_dir_entry *) inode->u.generic_ip;
62 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
63 return -ENOMEM;
65 while ((nbytes > 0) && !eof)
67 count = MIN(PROC_BLOCK_SIZE, nbytes);
68 if ((unsigned)pos > INT_MAX)
69 break;
71 start = NULL;
72 if (dp->get_info) {
74 * Handle backwards compatibility with the old net
75 * routines.
77 n = dp->get_info(page, &start, pos, count);
78 if (n < count)
79 eof = 1;
80 } else if (dp->read_proc) {
81 n = dp->read_proc(page, &start, pos,
82 count, &eof, dp->data);
83 } else
84 break;
86 if (!start) {
88 * For proc files that are less than 4k
90 start = page + pos;
91 n -= pos;
92 if (n <= 0)
93 break;
94 if (n > count)
95 n = count;
97 if (n == 0)
98 break; /* End of file */
99 if (n < 0) {
100 if (retval == 0)
101 retval = n;
102 break;
105 /* This is a hack to allow mangling of file pos independent
106 * of actual bytes read. Simply place the data at page,
107 * return the bytes, and set `start' to the desired offset
108 * as an unsigned int. - Paul.Russell@rustcorp.com.au
110 n -= copy_to_user(buf, start < page ? page : start, n);
111 if (n == 0) {
112 if (retval == 0)
113 retval = -EFAULT;
114 break;
117 pos += start < page ? (long)start : n; /* Move down the file */
118 nbytes -= n;
119 buf += n;
120 retval += n;
122 free_page((unsigned long) page);
123 *ppos = pos;
124 return retval;
127 static ssize_t
128 proc_file_write(struct file * file, const char * buffer,
129 size_t count, loff_t *ppos)
131 struct inode *inode = file->f_dentry->d_inode;
132 struct proc_dir_entry * dp;
134 dp = (struct proc_dir_entry *) inode->u.generic_ip;
136 if (!dp->write_proc)
137 return -EIO;
139 /* FIXME: does this routine need ppos? probably... */
140 return dp->write_proc(file, buffer, count, dp->data);
144 static loff_t
145 proc_file_lseek(struct file * file, loff_t offset, int origin)
147 long long retval;
149 switch (origin) {
150 case 2:
151 offset += file->f_dentry->d_inode->i_size;
152 break;
153 case 1:
154 offset += file->f_pos;
156 retval = -EINVAL;
157 if (offset>=0 && (unsigned long long)offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
158 if (offset != file->f_pos) {
159 file->f_pos = offset;
160 file->f_reada = 0;
162 retval = offset;
164 /* RED-PEN user can fake an error here by setting offset to >=-4095 && <0 */
165 return retval;
169 * This function parses a name such as "tty/driver/serial", and
170 * returns the struct proc_dir_entry for "/proc/tty/driver", and
171 * returns "serial" in residual.
173 static int xlate_proc_name(const char *name,
174 struct proc_dir_entry **ret, const char **residual)
176 const char *cp = name, *next;
177 struct proc_dir_entry *de;
178 int len;
180 de = &proc_root;
181 while (1) {
182 next = strchr(cp, '/');
183 if (!next)
184 break;
186 len = next - cp;
187 for (de = de->subdir; de ; de = de->next) {
188 if (proc_match(len, cp, de))
189 break;
191 if (!de)
192 return -ENOENT;
193 cp += len + 1;
195 *residual = cp;
196 *ret = de;
197 return 0;
200 static unsigned long proc_alloc_map[(PROC_NDYNAMIC + BITS_PER_LONG - 1) / BITS_PER_LONG];
202 spinlock_t proc_alloc_map_lock = SPIN_LOCK_UNLOCKED;
204 static int make_inode_number(void)
206 int i;
207 spin_lock(&proc_alloc_map_lock);
208 i = find_first_zero_bit(proc_alloc_map, PROC_NDYNAMIC);
209 if (i < 0 || i >= PROC_NDYNAMIC) {
210 i = -1;
211 goto out;
213 set_bit(i, proc_alloc_map);
214 i += PROC_DYNAMIC_FIRST;
215 out:
216 spin_unlock(&proc_alloc_map_lock);
217 return i;
220 static int proc_readlink(struct dentry *dentry, char *buffer, int buflen)
222 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
223 return vfs_readlink(dentry, buffer, buflen, s);
226 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
228 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
229 return vfs_follow_link(nd, s);
232 static struct inode_operations proc_link_inode_operations = {
233 readlink: proc_readlink,
234 follow_link: proc_follow_link,
238 * As some entries in /proc are volatile, we want to
239 * get rid of unused dentries. This could be made
240 * smarter: we could keep a "volatile" flag in the
241 * inode to indicate which ones to keep.
243 static int proc_delete_dentry(struct dentry * dentry)
245 return 1;
248 static struct dentry_operations proc_dentry_operations =
250 d_delete: proc_delete_dentry,
254 * Don't create negative dentries here, return -ENOENT by hand
255 * instead.
257 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
259 struct inode *inode;
260 struct proc_dir_entry * de;
261 int error;
263 error = -ENOENT;
264 inode = NULL;
265 de = (struct proc_dir_entry *) dir->u.generic_ip;
266 if (de) {
267 for (de = de->subdir; de ; de = de->next) {
268 if (!de || !de->low_ino)
269 continue;
270 if (de->namelen != dentry->d_name.len)
271 continue;
272 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
273 int ino = de->low_ino;
274 error = -EINVAL;
275 inode = proc_get_inode(dir->i_sb, ino, de);
276 break;
281 if (inode) {
282 dentry->d_op = &proc_dentry_operations;
283 d_add(dentry, inode);
284 return NULL;
286 return ERR_PTR(error);
290 * This returns non-zero if at EOF, so that the /proc
291 * root directory can use this and check if it should
292 * continue with the <pid> entries..
294 * Note that the VFS-layer doesn't care about the return
295 * value of the readdir() call, as long as it's non-negative
296 * for success..
298 int proc_readdir(struct file * filp,
299 void * dirent, filldir_t filldir)
301 struct proc_dir_entry * de;
302 unsigned int ino;
303 int i;
304 struct inode *inode = filp->f_dentry->d_inode;
306 ino = inode->i_ino;
307 de = (struct proc_dir_entry *) inode->u.generic_ip;
308 if (!de)
309 return -EINVAL;
310 i = filp->f_pos;
311 switch (i) {
312 case 0:
313 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
314 return 0;
315 i++;
316 filp->f_pos++;
317 /* fall through */
318 case 1:
319 if (filldir(dirent, "..", 2, i,
320 filp->f_dentry->d_parent->d_inode->i_ino,
321 DT_DIR) < 0)
322 return 0;
323 i++;
324 filp->f_pos++;
325 /* fall through */
326 default:
327 de = de->subdir;
328 i -= 2;
329 for (;;) {
330 if (!de)
331 return 1;
332 if (!i)
333 break;
334 de = de->next;
335 i--;
338 do {
339 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
340 de->low_ino, de->mode >> 12) < 0)
341 return 0;
342 filp->f_pos++;
343 de = de->next;
344 } while (de);
346 return 1;
350 * These are the generic /proc directory operations. They
351 * use the in-memory "struct proc_dir_entry" tree to parse
352 * the /proc directory.
354 static struct file_operations proc_dir_operations = {
355 read: generic_read_dir,
356 readdir: proc_readdir,
360 * proc directories can do almost nothing..
362 static struct inode_operations proc_dir_inode_operations = {
363 lookup: proc_lookup,
366 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
368 int i;
370 i = make_inode_number();
371 if (i < 0)
372 return -EAGAIN;
373 dp->low_ino = i;
374 dp->next = dir->subdir;
375 dp->parent = dir;
376 dir->subdir = dp;
377 if (S_ISDIR(dp->mode)) {
378 if (dp->proc_iops == NULL) {
379 dp->proc_fops = &proc_dir_operations;
380 dp->proc_iops = &proc_dir_inode_operations;
382 dir->nlink++;
383 } else if (S_ISLNK(dp->mode)) {
384 if (dp->proc_iops == NULL)
385 dp->proc_iops = &proc_link_inode_operations;
386 } else if (S_ISREG(dp->mode)) {
387 if (dp->proc_fops == NULL)
388 dp->proc_fops = &proc_file_operations;
390 return 0;
394 * Kill an inode that got unregistered..
396 static void proc_kill_inodes(struct proc_dir_entry *de)
398 struct list_head *p;
399 struct super_block *sb = proc_mnt->mnt_sb;
402 * Actually it's a partial revoke().
404 file_list_lock();
405 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
406 struct file * filp = list_entry(p, struct file, f_list);
407 struct dentry * dentry = filp->f_dentry;
408 struct inode * inode;
409 struct file_operations *fops;
411 if (dentry->d_op != &proc_dentry_operations)
412 continue;
413 inode = dentry->d_inode;
414 if (inode->u.generic_ip != de)
415 continue;
416 fops = filp->f_op;
417 filp->f_op = NULL;
418 fops_put(fops);
420 file_list_unlock();
423 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
424 const char *name,
425 mode_t mode,
426 nlink_t nlink)
428 struct proc_dir_entry *ent = NULL;
429 const char *fn = name;
430 int len;
432 /* make sure name is valid */
433 if (!name || !strlen(name)) goto out;
435 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
436 goto out;
437 len = strlen(fn);
439 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
440 if (!ent) goto out;
442 memset(ent, 0, sizeof(struct proc_dir_entry));
443 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
444 ent->name = ((char *) ent) + sizeof(*ent);
445 ent->namelen = len;
446 ent->mode = mode;
447 ent->nlink = nlink;
448 out:
449 return ent;
452 struct proc_dir_entry *proc_symlink(const char *name,
453 struct proc_dir_entry *parent, const char *dest)
455 struct proc_dir_entry *ent;
457 ent = proc_create(&parent,name,
458 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
460 if (ent) {
461 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
462 if (ent->data) {
463 strcpy((char*)ent->data,dest);
464 if (proc_register(parent, ent) < 0) {
465 kfree(ent->data);
466 kfree(ent);
467 ent = NULL;
469 } else {
470 kfree(ent);
471 ent = NULL;
474 return ent;
477 struct proc_dir_entry *proc_mknod(const char *name, mode_t mode,
478 struct proc_dir_entry *parent, kdev_t rdev)
480 struct proc_dir_entry *ent;
482 ent = proc_create(&parent,name,mode,1);
483 if (ent) {
484 ent->rdev = rdev;
485 if (proc_register(parent, ent) < 0) {
486 kfree(ent);
487 ent = NULL;
490 return ent;
493 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
494 struct proc_dir_entry *parent)
496 struct proc_dir_entry *ent;
498 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
499 if (ent) {
500 ent->proc_fops = &proc_dir_operations;
501 ent->proc_iops = &proc_dir_inode_operations;
503 if (proc_register(parent, ent) < 0) {
504 kfree(ent);
505 ent = NULL;
508 return ent;
511 struct proc_dir_entry *proc_mkdir(const char *name,
512 struct proc_dir_entry *parent)
514 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
517 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
518 struct proc_dir_entry *parent)
520 struct proc_dir_entry *ent;
521 nlink_t nlink;
523 if (S_ISDIR(mode)) {
524 if ((mode & S_IALLUGO) == 0)
525 mode |= S_IRUGO | S_IXUGO;
526 nlink = 2;
527 } else {
528 if ((mode & S_IFMT) == 0)
529 mode |= S_IFREG;
530 if ((mode & S_IALLUGO) == 0)
531 mode |= S_IRUGO;
532 nlink = 1;
535 ent = proc_create(&parent,name,mode,nlink);
536 if (ent) {
537 if (S_ISDIR(mode)) {
538 ent->proc_fops = &proc_dir_operations;
539 ent->proc_iops = &proc_dir_inode_operations;
541 if (proc_register(parent, ent) < 0) {
542 kfree(ent);
543 ent = NULL;
546 return ent;
549 void free_proc_entry(struct proc_dir_entry *de)
551 int ino = de->low_ino;
553 if (ino < PROC_DYNAMIC_FIRST ||
554 ino >= PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
555 return;
556 if (S_ISLNK(de->mode) && de->data)
557 kfree(de->data);
558 kfree(de);
562 * Remove a /proc entry and free it if it's not currently in use.
563 * If it is in use, we set the 'deleted' flag.
565 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
567 struct proc_dir_entry **p;
568 struct proc_dir_entry *de;
569 const char *fn = name;
570 int len;
572 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
573 goto out;
574 len = strlen(fn);
575 for (p = &parent->subdir; *p; p=&(*p)->next ) {
576 if (!proc_match(len, fn, *p))
577 continue;
578 de = *p;
579 *p = de->next;
580 de->next = NULL;
581 if (S_ISDIR(de->mode))
582 parent->nlink--;
583 clear_bit(de->low_ino - PROC_DYNAMIC_FIRST,
584 proc_alloc_map);
585 proc_kill_inodes(de);
586 de->nlink = 0;
587 if (!atomic_read(&de->count))
588 free_proc_entry(de);
589 else {
590 de->deleted = 1;
591 printk("remove_proc_entry: %s/%s busy, count=%d\n",
592 parent->name, de->name, atomic_read(&de->count));
594 break;
596 out:
597 return;