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 <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>
22 #include <linux/spinlock.h>
23 #include <asm/uaccess.h>
27 static ssize_t
proc_file_read(struct file
*file
, char __user
*buf
,
28 size_t nbytes
, loff_t
*ppos
);
29 static ssize_t
proc_file_write(struct file
*file
, const char __user
*buffer
,
30 size_t count
, loff_t
*ppos
);
31 static loff_t
proc_file_lseek(struct file
*, loff_t
, int);
33 DEFINE_SPINLOCK(proc_subdir_lock
);
35 static int proc_match(int len
, const char *name
, struct proc_dir_entry
*de
)
37 if (de
->namelen
!= len
)
39 return !memcmp(name
, de
->name
, len
);
42 static const struct file_operations proc_file_operations
= {
43 .llseek
= proc_file_lseek
,
44 .read
= proc_file_read
,
45 .write
= proc_file_write
,
48 /* buffer size is one page but our output routines use some slack for overruns */
49 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
52 proc_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
55 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
61 struct proc_dir_entry
* dp
;
62 unsigned long long pos
;
65 * Gaah, please just use "seq_file" instead. The legacy /proc
66 * interfaces cut loff_t down to off_t for reads, and ignore
67 * the offset entirely for writes..
70 if (pos
> MAX_NON_LFS
)
72 if (nbytes
> MAX_NON_LFS
- pos
)
73 nbytes
= MAX_NON_LFS
- pos
;
76 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
79 while ((nbytes
> 0) && !eof
) {
80 count
= min_t(size_t, PROC_BLOCK_SIZE
, nbytes
);
84 /* Handle old net routines */
85 n
= dp
->get_info(page
, &start
, *ppos
, count
);
88 } else if (dp
->read_proc
) {
90 * How to be a proc read function
91 * ------------------------------
93 * int f(char *buffer, char **start, off_t offset,
94 * int count, int *peof, void *dat)
96 * Assume that the buffer is "count" bytes in size.
98 * If you know you have supplied all the data you
101 * You have three ways to return data:
102 * 0) Leave *start = NULL. (This is the default.)
103 * Put the data of the requested offset at that
104 * offset within the buffer. Return the number (n)
105 * of bytes there are from the beginning of the
106 * buffer up to the last byte of data. If the
107 * number of supplied bytes (= n - offset) 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 the number of bytes
112 * absorbed. This interface is useful for files
113 * no larger than the buffer.
114 * 1) Set *start = an unsigned long value less than
115 * the buffer address but greater than zero.
116 * Put the data of the requested offset at the
117 * beginning of the buffer. Return the number of
118 * bytes of data placed there. If this number is
119 * greater than zero and you didn't signal eof
120 * and the reader is prepared to take more data
121 * you will be called again with the requested
122 * offset advanced by *start. This interface is
123 * useful when you have a large file consisting
124 * of a series of blocks which you want to count
125 * and return as wholes.
126 * (Hack by Paul.Russell@rustcorp.com.au)
127 * 2) Set *start = an address within the buffer.
128 * Put the data of the requested offset at *start.
129 * Return the number of bytes of data placed there.
130 * If this number is greater than zero and you
131 * didn't signal eof and the reader is prepared to
132 * take more data you will be called again with the
133 * requested offset advanced by the number of bytes
136 n
= dp
->read_proc(page
, &start
, *ppos
,
137 count
, &eof
, dp
->data
);
141 if (n
== 0) /* end of file */
143 if (n
< 0) { /* error */
152 "proc_file_read: Apparent buffer overflow!\n");
160 start
= page
+ *ppos
;
161 } else if (start
< page
) {
164 "proc_file_read: Apparent buffer overflow!\n");
169 * Don't reduce n because doing so might
170 * cut off part of a data block.
173 "proc_file_read: Read count exceeded\n");
175 } else /* start >= page */ {
176 unsigned long startoff
= (unsigned long)(start
- page
);
177 if (n
> (PAGE_SIZE
- startoff
)) {
179 "proc_file_read: Apparent buffer overflow!\n");
180 n
= PAGE_SIZE
- startoff
;
186 n
-= copy_to_user(buf
, start
< page
? page
: start
, n
);
193 *ppos
+= start
< page
? (unsigned long)start
: n
;
198 free_page((unsigned long) page
);
203 proc_file_write(struct file
*file
, const char __user
*buffer
,
204 size_t count
, loff_t
*ppos
)
206 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
207 struct proc_dir_entry
* dp
;
214 /* FIXME: does this routine need ppos? probably... */
215 return dp
->write_proc(file
, buffer
, count
, dp
->data
);
220 proc_file_lseek(struct file
*file
, loff_t offset
, int orig
)
222 loff_t retval
= -EINVAL
;
225 offset
+= file
->f_pos
;
228 if (offset
< 0 || offset
> MAX_NON_LFS
)
230 file
->f_pos
= retval
= offset
;
235 static int proc_notify_change(struct dentry
*dentry
, struct iattr
*iattr
)
237 struct inode
*inode
= dentry
->d_inode
;
238 struct proc_dir_entry
*de
= PDE(inode
);
241 error
= inode_change_ok(inode
, iattr
);
245 error
= inode_setattr(inode
, iattr
);
249 de
->uid
= inode
->i_uid
;
250 de
->gid
= inode
->i_gid
;
251 de
->mode
= inode
->i_mode
;
256 static int proc_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
259 struct inode
*inode
= dentry
->d_inode
;
260 struct proc_dir_entry
*de
= PROC_I(inode
)->pde
;
262 inode
->i_nlink
= de
->nlink
;
264 generic_fillattr(inode
, stat
);
268 static const struct inode_operations proc_file_inode_operations
= {
269 .setattr
= proc_notify_change
,
273 * This function parses a name such as "tty/driver/serial", and
274 * returns the struct proc_dir_entry for "/proc/tty/driver", and
275 * returns "serial" in residual.
277 static int xlate_proc_name(const char *name
,
278 struct proc_dir_entry
**ret
, const char **residual
)
280 const char *cp
= name
, *next
;
281 struct proc_dir_entry
*de
;
285 spin_lock(&proc_subdir_lock
);
288 next
= strchr(cp
, '/');
293 for (de
= de
->subdir
; de
; de
= de
->next
) {
294 if (proc_match(len
, cp
, de
))
306 spin_unlock(&proc_subdir_lock
);
310 static DEFINE_IDR(proc_inum_idr
);
311 static DEFINE_SPINLOCK(proc_inum_lock
); /* protects the above */
313 #define PROC_DYNAMIC_FIRST 0xF0000000UL
316 * Return an inode number between PROC_DYNAMIC_FIRST and
317 * 0xffffffff, or zero on failure.
319 static unsigned int get_inode_number(void)
325 if (idr_pre_get(&proc_inum_idr
, GFP_KERNEL
) == 0)
328 spin_lock(&proc_inum_lock
);
329 error
= idr_get_new(&proc_inum_idr
, NULL
, &i
);
330 spin_unlock(&proc_inum_lock
);
331 if (error
== -EAGAIN
)
336 inum
= (i
& MAX_ID_MASK
) + PROC_DYNAMIC_FIRST
;
338 /* inum will never be more than 0xf0ffffff, so no check
345 static void release_inode_number(unsigned int inum
)
347 int id
= (inum
- PROC_DYNAMIC_FIRST
) | ~MAX_ID_MASK
;
349 spin_lock(&proc_inum_lock
);
350 idr_remove(&proc_inum_idr
, id
);
351 spin_unlock(&proc_inum_lock
);
354 static void *proc_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
356 nd_set_link(nd
, PDE(dentry
->d_inode
)->data
);
360 static const struct inode_operations proc_link_inode_operations
= {
361 .readlink
= generic_readlink
,
362 .follow_link
= proc_follow_link
,
366 * As some entries in /proc are volatile, we want to
367 * get rid of unused dentries. This could be made
368 * smarter: we could keep a "volatile" flag in the
369 * inode to indicate which ones to keep.
371 static int proc_delete_dentry(struct dentry
* dentry
)
376 static struct dentry_operations proc_dentry_operations
=
378 .d_delete
= proc_delete_dentry
,
382 * Don't create negative dentries here, return -ENOENT by hand
385 struct dentry
*proc_lookup(struct inode
* dir
, struct dentry
*dentry
, struct nameidata
*nd
)
387 struct inode
*inode
= NULL
;
388 struct proc_dir_entry
* de
;
392 spin_lock(&proc_subdir_lock
);
395 for (de
= de
->subdir
; de
; de
= de
->next
) {
396 if (de
->namelen
!= dentry
->d_name
.len
)
398 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
399 unsigned int ino
= de
->low_ino
;
402 spin_unlock(&proc_subdir_lock
);
404 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
405 spin_lock(&proc_subdir_lock
);
410 spin_unlock(&proc_subdir_lock
);
414 dentry
->d_op
= &proc_dentry_operations
;
415 d_add(dentry
, inode
);
419 return ERR_PTR(error
);
423 * This returns non-zero if at EOF, so that the /proc
424 * root directory can use this and check if it should
425 * continue with the <pid> entries..
427 * Note that the VFS-layer doesn't care about the return
428 * value of the readdir() call, as long as it's non-negative
431 int proc_readdir(struct file
* filp
,
432 void * dirent
, filldir_t filldir
)
434 struct proc_dir_entry
* de
;
437 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
451 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
457 if (filldir(dirent
, "..", 2, i
,
458 parent_ino(filp
->f_path
.dentry
),
465 spin_lock(&proc_subdir_lock
);
471 spin_unlock(&proc_subdir_lock
);
481 struct proc_dir_entry
*next
;
483 /* filldir passes info to user space */
485 spin_unlock(&proc_subdir_lock
);
486 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
,
487 de
->low_ino
, de
->mode
>> 12) < 0) {
491 spin_lock(&proc_subdir_lock
);
497 spin_unlock(&proc_subdir_lock
);
500 out
: unlock_kernel();
505 * These are the generic /proc directory operations. They
506 * use the in-memory "struct proc_dir_entry" tree to parse
507 * the /proc directory.
509 static const struct file_operations proc_dir_operations
= {
510 .read
= generic_read_dir
,
511 .readdir
= proc_readdir
,
515 * proc directories can do almost nothing..
517 static const struct inode_operations proc_dir_inode_operations
= {
518 .lookup
= proc_lookup
,
519 .getattr
= proc_getattr
,
520 .setattr
= proc_notify_change
,
523 static int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
527 i
= get_inode_number();
532 spin_lock(&proc_subdir_lock
);
533 dp
->next
= dir
->subdir
;
536 spin_unlock(&proc_subdir_lock
);
538 if (S_ISDIR(dp
->mode
)) {
539 if (dp
->proc_iops
== NULL
) {
540 dp
->proc_fops
= &proc_dir_operations
;
541 dp
->proc_iops
= &proc_dir_inode_operations
;
544 } else if (S_ISLNK(dp
->mode
)) {
545 if (dp
->proc_iops
== NULL
)
546 dp
->proc_iops
= &proc_link_inode_operations
;
547 } else if (S_ISREG(dp
->mode
)) {
548 if (dp
->proc_fops
== NULL
)
549 dp
->proc_fops
= &proc_file_operations
;
550 if (dp
->proc_iops
== NULL
)
551 dp
->proc_iops
= &proc_file_inode_operations
;
557 * Kill an inode that got unregistered..
559 static void proc_kill_inodes(struct proc_dir_entry
*de
)
562 struct super_block
*sb
= proc_mnt
->mnt_sb
;
565 * Actually it's a partial revoke().
568 list_for_each(p
, &sb
->s_files
) {
569 struct file
* filp
= list_entry(p
, struct file
, f_u
.fu_list
);
570 struct dentry
* dentry
= filp
->f_path
.dentry
;
571 struct inode
* inode
;
572 const struct file_operations
*fops
;
574 if (dentry
->d_op
!= &proc_dentry_operations
)
576 inode
= dentry
->d_inode
;
577 if (PDE(inode
) != de
)
586 static struct proc_dir_entry
*proc_create(struct proc_dir_entry
**parent
,
591 struct proc_dir_entry
*ent
= NULL
;
592 const char *fn
= name
;
595 /* make sure name is valid */
596 if (!name
|| !strlen(name
)) goto out
;
598 if (!(*parent
) && xlate_proc_name(name
, parent
, &fn
) != 0)
601 /* At this point there must not be any '/' characters beyond *fn */
607 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
610 memset(ent
, 0, sizeof(struct proc_dir_entry
));
611 memcpy(((char *) ent
) + sizeof(struct proc_dir_entry
), fn
, len
+ 1);
612 ent
->name
= ((char *) ent
) + sizeof(*ent
);
620 struct proc_dir_entry
*proc_symlink(const char *name
,
621 struct proc_dir_entry
*parent
, const char *dest
)
623 struct proc_dir_entry
*ent
;
625 ent
= proc_create(&parent
,name
,
626 (S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
),1);
629 ent
->data
= kmalloc((ent
->size
=strlen(dest
))+1, GFP_KERNEL
);
631 strcpy((char*)ent
->data
,dest
);
632 if (proc_register(parent
, ent
) < 0) {
645 struct proc_dir_entry
*proc_mkdir_mode(const char *name
, mode_t mode
,
646 struct proc_dir_entry
*parent
)
648 struct proc_dir_entry
*ent
;
650 ent
= proc_create(&parent
, name
, S_IFDIR
| mode
, 2);
652 ent
->proc_fops
= &proc_dir_operations
;
653 ent
->proc_iops
= &proc_dir_inode_operations
;
655 if (proc_register(parent
, ent
) < 0) {
663 struct proc_dir_entry
*proc_mkdir(const char *name
,
664 struct proc_dir_entry
*parent
)
666 return proc_mkdir_mode(name
, S_IRUGO
| S_IXUGO
, parent
);
669 struct proc_dir_entry
*create_proc_entry(const char *name
, mode_t mode
,
670 struct proc_dir_entry
*parent
)
672 struct proc_dir_entry
*ent
;
676 if ((mode
& S_IALLUGO
) == 0)
677 mode
|= S_IRUGO
| S_IXUGO
;
680 if ((mode
& S_IFMT
) == 0)
682 if ((mode
& S_IALLUGO
) == 0)
687 ent
= proc_create(&parent
,name
,mode
,nlink
);
690 ent
->proc_fops
= &proc_dir_operations
;
691 ent
->proc_iops
= &proc_dir_inode_operations
;
693 if (proc_register(parent
, ent
) < 0) {
701 void free_proc_entry(struct proc_dir_entry
*de
)
703 unsigned int ino
= de
->low_ino
;
705 if (ino
< PROC_DYNAMIC_FIRST
)
708 release_inode_number(ino
);
710 if (S_ISLNK(de
->mode
) && de
->data
)
716 * Remove a /proc entry and free it if it's not currently in use.
717 * If it is in use, we set the 'deleted' flag.
719 void remove_proc_entry(const char *name
, struct proc_dir_entry
*parent
)
721 struct proc_dir_entry
**p
;
722 struct proc_dir_entry
*de
;
723 const char *fn
= name
;
726 if (!parent
&& xlate_proc_name(name
, &parent
, &fn
) != 0)
730 spin_lock(&proc_subdir_lock
);
731 for (p
= &parent
->subdir
; *p
; p
=&(*p
)->next
) {
732 if (!proc_match(len
, fn
, *p
))
737 if (S_ISDIR(de
->mode
))
739 proc_kill_inodes(de
);
742 if (!atomic_read(&de
->count
))
746 printk("remove_proc_entry: %s/%s busy, count=%d\n",
747 parent
->name
, de
->name
, atomic_read(&de
->count
));
751 spin_unlock(&proc_subdir_lock
);