Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / fs / proc / generic.c
blob1585657a2d7b0621f9244be8c4446edf7f243798
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 #include <asm/bitops.h>
19 static ssize_t proc_file_read(struct file * file, char * buf,
20 size_t nbytes, loff_t *ppos);
21 static ssize_t proc_file_write(struct file * file, const char * buffer,
22 size_t count, loff_t *ppos);
23 static loff_t proc_file_lseek(struct file *, loff_t, int);
25 int proc_match(int len, const char *name,struct proc_dir_entry * de)
27 if (!de || !de->low_ino)
28 return 0;
29 if (de->namelen != len)
30 return 0;
31 return !memcmp(name, de->name, len);
34 static struct file_operations proc_file_operations = {
35 llseek: proc_file_lseek,
36 read: proc_file_read,
37 write: proc_file_write,
40 #ifndef MIN
41 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
42 #endif
44 /* 4K page size but our output routines use some slack for overruns */
45 #define PROC_BLOCK_SIZE (3*1024)
47 static ssize_t
48 proc_file_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
50 struct inode * inode = file->f_dentry->d_inode;
51 char *page;
52 ssize_t retval=0;
53 int eof=0;
54 ssize_t n, count;
55 char *start;
56 struct proc_dir_entry * dp;
58 dp = (struct proc_dir_entry *) inode->u.generic_ip;
59 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
60 return -ENOMEM;
62 while ((nbytes > 0) && !eof)
64 count = MIN(PROC_BLOCK_SIZE, nbytes);
66 start = NULL;
67 if (dp->get_info) {
69 * Handle backwards compatibility with the old net
70 * routines.
72 n = dp->get_info(page, &start, *ppos, count);
73 if (n < count)
74 eof = 1;
75 } else if (dp->read_proc) {
76 n = dp->read_proc(page, &start, *ppos,
77 count, &eof, dp->data);
78 } else
79 break;
81 if (!start) {
83 * For proc files that are less than 4k
85 start = page + *ppos;
86 n -= *ppos;
87 if (n <= 0)
88 break;
89 if (n > count)
90 n = count;
92 if (n == 0)
93 break; /* End of file */
94 if (n < 0) {
95 if (retval == 0)
96 retval = n;
97 break;
100 /* This is a hack to allow mangling of file pos independent
101 * of actual bytes read. Simply place the data at page,
102 * return the bytes, and set `start' to the desired offset
103 * as an unsigned int. - Paul.Russell@rustcorp.com.au
105 n -= copy_to_user(buf, start < page ? page : start, n);
106 if (n == 0) {
107 if (retval == 0)
108 retval = -EFAULT;
109 break;
112 *ppos += start < page ? (long)start : n; /* Move down the file */
113 nbytes -= n;
114 buf += n;
115 retval += n;
117 free_page((unsigned long) page);
118 return retval;
121 static ssize_t
122 proc_file_write(struct file * file, const char * buffer,
123 size_t count, loff_t *ppos)
125 struct inode *inode = file->f_dentry->d_inode;
126 struct proc_dir_entry * dp;
128 dp = (struct proc_dir_entry *) inode->u.generic_ip;
130 if (!dp->write_proc)
131 return -EIO;
133 /* FIXME: does this routine need ppos? probably... */
134 return dp->write_proc(file, buffer, count, dp->data);
138 static loff_t
139 proc_file_lseek(struct file * file, loff_t offset, int orig)
141 switch (orig) {
142 case 0:
143 if (offset < 0)
144 return -EINVAL;
145 file->f_pos = offset;
146 return(file->f_pos);
147 case 1:
148 if (offset + file->f_pos < 0)
149 return -EINVAL;
150 file->f_pos += offset;
151 return(file->f_pos);
152 case 2:
153 return(-EINVAL);
154 default:
155 return(-EINVAL);
160 * This function parses a name such as "tty/driver/serial", and
161 * returns the struct proc_dir_entry for "/proc/tty/driver", and
162 * returns "serial" in residual.
164 static int xlate_proc_name(const char *name,
165 struct proc_dir_entry **ret, const char **residual)
167 const char *cp = name, *next;
168 struct proc_dir_entry *de;
169 int len;
171 de = &proc_root;
172 while (1) {
173 next = strchr(cp, '/');
174 if (!next)
175 break;
177 len = next - cp;
178 for (de = de->subdir; de ; de = de->next) {
179 if (proc_match(len, cp, de))
180 break;
182 if (!de)
183 return -ENOENT;
184 cp += len + 1;
186 *residual = cp;
187 *ret = de;
188 return 0;
191 static unsigned char proc_alloc_map[PROC_NDYNAMIC / 8] = {0};
193 static int make_inode_number(void)
195 int i = find_first_zero_bit((void *) proc_alloc_map, PROC_NDYNAMIC);
196 if (i<0 || i>=PROC_NDYNAMIC)
197 return -1;
198 set_bit(i, (void *) proc_alloc_map);
199 return PROC_DYNAMIC_FIRST + i;
202 static int proc_readlink(struct dentry *dentry, char *buffer, int buflen)
204 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
205 return vfs_readlink(dentry, buffer, buflen, s);
208 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
210 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
211 return vfs_follow_link(nd, s);
214 static struct inode_operations proc_link_inode_operations = {
215 readlink: proc_readlink,
216 follow_link: proc_follow_link
220 * As some entries in /proc are volatile, we want to
221 * get rid of unused dentries. This could be made
222 * smarter: we could keep a "volatile" flag in the
223 * inode to indicate which ones to keep.
225 static int proc_delete_dentry(struct dentry * dentry)
227 return 1;
230 static struct dentry_operations proc_dentry_operations =
232 d_delete: proc_delete_dentry,
236 * Don't create negative dentries here, return -ENOENT by hand
237 * instead.
239 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
241 struct inode *inode;
242 struct proc_dir_entry * de;
243 int error;
245 error = -ENOENT;
246 inode = NULL;
247 de = (struct proc_dir_entry *) dir->u.generic_ip;
248 if (de) {
249 for (de = de->subdir; de ; de = de->next) {
250 if (!de || !de->low_ino)
251 continue;
252 if (de->namelen != dentry->d_name.len)
253 continue;
254 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
255 int ino = de->low_ino;
256 error = -EINVAL;
257 inode = proc_get_inode(dir->i_sb, ino, de);
258 break;
263 if (inode) {
264 dentry->d_op = &proc_dentry_operations;
265 d_add(dentry, inode);
266 return NULL;
268 return ERR_PTR(error);
272 * This returns non-zero if at EOF, so that the /proc
273 * root directory can use this and check if it should
274 * continue with the <pid> entries..
276 * Note that the VFS-layer doesn't care about the return
277 * value of the readdir() call, as long as it's non-negative
278 * for success..
280 int proc_readdir(struct file * filp,
281 void * dirent, filldir_t filldir)
283 struct proc_dir_entry * de;
284 unsigned int ino;
285 int i;
286 struct inode *inode = filp->f_dentry->d_inode;
288 ino = inode->i_ino;
289 de = (struct proc_dir_entry *) inode->u.generic_ip;
290 if (!de)
291 return -EINVAL;
292 i = filp->f_pos;
293 switch (i) {
294 case 0:
295 if (filldir(dirent, ".", 1, i, ino) < 0)
296 return 0;
297 i++;
298 filp->f_pos++;
299 /* fall through */
300 case 1:
301 if (filldir(dirent, "..", 2, i,
302 filp->f_dentry->d_parent->d_inode->i_ino
303 ) < 0)
304 return 0;
305 i++;
306 filp->f_pos++;
307 /* fall through */
308 default:
309 de = de->subdir;
310 i -= 2;
311 for (;;) {
312 if (!de)
313 return 1;
314 if (!i)
315 break;
316 de = de->next;
317 i--;
320 do {
321 if (filldir(dirent, de->name, de->namelen, filp->f_pos, de->low_ino) < 0)
322 return 0;
323 filp->f_pos++;
324 de = de->next;
325 } while (de);
327 return 1;
331 * These are the generic /proc directory operations. They
332 * use the in-memory "struct proc_dir_entry" tree to parse
333 * the /proc directory.
335 static struct file_operations proc_dir_operations = {
336 readdir: proc_readdir,
340 * proc directories can do almost nothing..
342 static struct inode_operations proc_dir_inode_operations = {
343 lookup: proc_lookup,
346 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
348 int i;
350 i = make_inode_number();
351 if (i < 0)
352 return -EAGAIN;
353 dp->low_ino = i;
354 dp->next = dir->subdir;
355 dp->parent = dir;
356 dir->subdir = dp;
357 if (S_ISDIR(dp->mode)) {
358 if (dp->proc_iops == NULL) {
359 dp->proc_fops = &proc_dir_operations;
360 dp->proc_iops = &proc_dir_inode_operations;
362 dir->nlink++;
363 } else if (S_ISLNK(dp->mode)) {
364 if (dp->proc_iops == NULL)
365 dp->proc_iops = &proc_link_inode_operations;
366 } else if (S_ISREG(dp->mode)) {
367 if (dp->proc_fops == NULL)
368 dp->proc_fops = &proc_file_operations;
370 return 0;
374 * Kill an inode that got unregistered..
376 static void proc_kill_inodes(struct proc_dir_entry *de)
378 struct list_head *p;
379 struct super_block *sb = proc_mnt->mnt_sb;
382 * Actually it's a partial revoke().
384 file_list_lock();
385 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
386 struct file * filp = list_entry(p, struct file, f_list);
387 struct dentry * dentry;
388 struct inode * inode;
390 dentry = filp->f_dentry;
391 if (!dentry)
392 continue;
393 if (dentry->d_op != &proc_dentry_operations)
394 continue;
395 inode = dentry->d_inode;
396 if (!inode)
397 continue;
398 if (inode->u.generic_ip != de)
399 continue;
400 filp->f_op = NULL;
402 file_list_unlock();
405 struct proc_dir_entry *proc_symlink(const char *name,
406 struct proc_dir_entry *parent, char *dest)
408 struct proc_dir_entry *ent = NULL;
409 const char *fn = name;
410 int len;
412 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
413 goto out;
414 len = strlen(fn);
416 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
417 if (!ent)
418 goto out;
419 memset(ent, 0, sizeof(struct proc_dir_entry));
420 memcpy(((char *) ent) + sizeof(*ent), fn, len + 1);
421 ent->name = ((char *) ent) + sizeof(*ent);
422 ent->namelen = len;
423 ent->nlink = 1;
424 ent->mode = S_IFLNK|S_IRUGO|S_IWUGO|S_IXUGO;
425 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
426 if (!ent->data) {
427 kfree(ent);
428 goto out;
430 strcpy((char*)ent->data,dest);
432 proc_register(parent, ent);
434 out:
435 return ent;
438 struct proc_dir_entry *proc_mknod(const char *name, mode_t mode,
439 struct proc_dir_entry *parent, kdev_t rdev)
441 struct proc_dir_entry *ent = NULL;
442 const char *fn = name;
443 int len;
445 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
446 goto out;
447 len = strlen(fn);
449 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
450 if (!ent)
451 goto out;
452 memset(ent, 0, sizeof(struct proc_dir_entry));
453 memcpy(((char *) ent) + sizeof(*ent), fn, len + 1);
454 ent->name = ((char *) ent) + sizeof(*ent);
455 ent->namelen = len;
456 ent->nlink = 1;
457 ent->mode = mode;
458 ent->rdev = rdev;
460 proc_register(parent, ent);
462 out:
463 return ent;
466 struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)
468 struct proc_dir_entry *ent = NULL;
469 const char *fn = name;
470 int len;
472 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
473 goto out;
474 len = strlen(fn);
476 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
477 if (!ent)
478 goto out;
479 memset(ent, 0, sizeof(struct proc_dir_entry));
480 memcpy(((char *) ent) + sizeof(*ent), fn, len + 1);
481 ent->name = ((char *) ent) + sizeof(*ent);
482 ent->namelen = len;
483 ent->proc_fops = &proc_dir_operations;
484 ent->proc_iops = &proc_dir_inode_operations;
485 ent->nlink = 2;
486 ent->mode = S_IFDIR | S_IRUGO | S_IXUGO;
488 proc_register(parent, ent);
490 out:
491 return ent;
494 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
495 struct proc_dir_entry *parent)
497 struct proc_dir_entry *ent = NULL;
498 const char *fn = name;
499 int len;
501 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
502 goto out;
503 len = strlen(fn);
505 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
506 if (!ent)
507 goto out;
508 memset(ent, 0, sizeof(struct proc_dir_entry));
509 memcpy(((char *) ent) + sizeof(*ent), fn, len + 1);
510 ent->name = ((char *) ent) + sizeof(*ent);
511 ent->namelen = len;
513 if (S_ISDIR(mode)) {
514 if ((mode & S_IALLUGO) == 0)
515 mode |= S_IRUGO | S_IXUGO;
516 ent->proc_fops = &proc_dir_operations;
517 ent->proc_iops = &proc_dir_inode_operations;
518 ent->nlink = 2;
519 } else {
520 if ((mode & S_IFMT) == 0)
521 mode |= S_IFREG;
522 if ((mode & S_IALLUGO) == 0)
523 mode |= S_IRUGO;
524 ent->nlink = 1;
526 ent->mode = mode;
528 proc_register(parent, ent);
530 out:
531 return ent;
534 void free_proc_entry(struct proc_dir_entry *de)
536 int ino = de->low_ino;
538 if (ino < PROC_DYNAMIC_FIRST &&
539 ino >= PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
540 return;
541 if (S_ISLNK(de->mode) && de->data)
542 kfree(de->data);
543 kfree(de);
547 * Remove a /proc entry and free it if it's not currently in use.
548 * If it is in use, we set the 'deleted' flag.
550 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
552 struct proc_dir_entry **p;
553 struct proc_dir_entry *de;
554 const char *fn = name;
555 int len;
557 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
558 goto out;
559 len = strlen(fn);
560 for (p = &parent->subdir; *p; p=&(*p)->next ) {
561 if (!proc_match(len, fn, *p))
562 continue;
563 de = *p;
564 *p = de->next;
565 de->next = NULL;
566 if (S_ISDIR(de->mode))
567 parent->nlink--;
568 clear_bit(de->low_ino-PROC_DYNAMIC_FIRST,
569 (void *) proc_alloc_map);
570 proc_kill_inodes(de);
571 de->nlink = 0;
572 de->deleted = 1;
573 if (!de->count)
574 free_proc_entry(de);
575 else {
576 printk("remove_proc_entry: %s/%s busy, count=%d\n",
577 parent->name, de->name, de->count);
579 break;
581 out:
582 return;