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.
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
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
)
29 if (de
->namelen
!= len
)
31 return !memcmp(name
, de
->name
, len
);
34 static struct file_operations proc_file_operations
= {
35 llseek
: proc_file_lseek
,
37 write
: proc_file_write
,
41 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
44 /* 4K page size but our output routines use some slack for overruns */
45 #define PROC_BLOCK_SIZE (3*1024)
48 proc_file_read(struct file
* file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
50 struct inode
* inode
= file
->f_dentry
->d_inode
;
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
)))
62 while ((nbytes
> 0) && !eof
)
64 count
= MIN(PROC_BLOCK_SIZE
, nbytes
);
69 * Handle backwards compatibility with the old net
72 n
= dp
->get_info(page
, &start
, *ppos
, count
);
75 } else if (dp
->read_proc
) {
76 n
= dp
->read_proc(page
, &start
, *ppos
,
77 count
, &eof
, dp
->data
);
83 * For proc files that are less than 4k
93 break; /* End of file */
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
);
112 *ppos
+= start
< page
? (long)start
: n
; /* Move down the file */
117 free_page((unsigned long) page
);
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
;
133 /* FIXME: does this routine need ppos? probably... */
134 return dp
->write_proc(file
, buffer
, count
, dp
->data
);
139 proc_file_lseek(struct file
* file
, loff_t offset
, int orig
)
145 file
->f_pos
= offset
;
148 if (offset
+ file
->f_pos
< 0)
150 file
->f_pos
+= offset
;
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
;
173 next
= strchr(cp
, '/');
178 for (de
= de
->subdir
; de
; de
= de
->next
) {
179 if (proc_match(len
, cp
, de
))
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
)
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
)
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
239 struct dentry
*proc_lookup(struct inode
* dir
, struct dentry
*dentry
)
242 struct proc_dir_entry
* de
;
247 de
= (struct proc_dir_entry
*) dir
->u
.generic_ip
;
249 for (de
= de
->subdir
; de
; de
= de
->next
) {
250 if (!de
|| !de
->low_ino
)
252 if (de
->namelen
!= dentry
->d_name
.len
)
254 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
255 int ino
= de
->low_ino
;
257 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
264 dentry
->d_op
= &proc_dentry_operations
;
265 d_add(dentry
, inode
);
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
280 int proc_readdir(struct file
* filp
,
281 void * dirent
, filldir_t filldir
)
283 struct proc_dir_entry
* de
;
286 struct inode
*inode
= filp
->f_dentry
->d_inode
;
289 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
295 if (filldir(dirent
, ".", 1, i
, ino
) < 0)
301 if (filldir(dirent
, "..", 2, i
,
302 filp
->f_dentry
->d_parent
->d_inode
->i_ino
321 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
, de
->low_ino
) < 0)
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
= {
346 static int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
350 i
= make_inode_number();
354 dp
->next
= dir
->subdir
;
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
;
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
;
374 * Kill an inode that got unregistered..
376 static void proc_kill_inodes(struct proc_dir_entry
*de
)
379 struct super_block
*sb
= proc_mnt
->mnt_sb
;
382 * Actually it's a partial revoke().
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
;
393 if (dentry
->d_op
!= &proc_dentry_operations
)
395 inode
= dentry
->d_inode
;
398 if (inode
->u
.generic_ip
!= de
)
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
;
412 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
416 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
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
);
424 ent
->mode
= S_IFLNK
|S_IRUGO
|S_IWUGO
|S_IXUGO
;
425 ent
->data
= kmalloc((ent
->size
=strlen(dest
))+1, GFP_KERNEL
);
430 strcpy((char*)ent
->data
,dest
);
432 proc_register(parent
, 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
;
445 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
449 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
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
);
460 proc_register(parent
, 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
;
472 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
476 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
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
);
483 ent
->proc_fops
= &proc_dir_operations
;
484 ent
->proc_iops
= &proc_dir_inode_operations
;
486 ent
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
488 proc_register(parent
, 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
;
501 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
505 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
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
);
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
;
520 if ((mode
& S_IFMT
) == 0)
522 if ((mode
& S_IALLUGO
) == 0)
528 proc_register(parent
, 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
)
541 if (S_ISLNK(de
->mode
) && de
->data
)
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
;
557 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
560 for (p
= &parent
->subdir
; *p
; p
=&(*p
)->next
) {
561 if (!proc_match(len
, fn
, *p
))
566 if (S_ISDIR(de
->mode
))
568 clear_bit(de
->low_ino
-PROC_DYNAMIC_FIRST
,
569 (void *) proc_alloc_map
);
570 proc_kill_inodes(de
);
576 printk("remove_proc_entry: %s/%s busy, count=%d\n",
577 parent
->name
, de
->name
, de
->count
);