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/slab.h>
17 #include <linux/mount.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 <linux/completion.h>
24 #include <asm/uaccess.h>
28 DEFINE_SPINLOCK(proc_subdir_lock
);
30 static int proc_match(int len
, const char *name
, struct proc_dir_entry
*de
)
32 if (de
->namelen
!= len
)
34 return !memcmp(name
, de
->name
, len
);
37 /* buffer size is one page but our output routines use some slack for overruns */
38 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
41 __proc_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
44 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
50 struct proc_dir_entry
* dp
;
51 unsigned long long pos
;
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..
59 if (pos
> MAX_NON_LFS
)
61 if (nbytes
> MAX_NON_LFS
- pos
)
62 nbytes
= MAX_NON_LFS
- pos
;
65 if (!(page
= (char*) __get_free_page(GFP_TEMPORARY
)))
68 while ((nbytes
> 0) && !eof
) {
69 count
= min_t(size_t, PROC_BLOCK_SIZE
, nbytes
);
74 * How to be a proc read function
75 * ------------------------------
77 * int f(char *buffer, char **start, off_t offset,
78 * int count, int *peof, void *dat)
80 * Assume that the buffer is "count" bytes in size.
82 * If you know you have supplied all the data you
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
120 n
= dp
->read_proc(page
, &start
, *ppos
,
121 count
, &eof
, dp
->data
);
125 if (n
== 0) /* end of file */
127 if (n
< 0) { /* error */
136 "proc_file_read: Apparent buffer overflow!\n");
144 start
= page
+ *ppos
;
145 } else if (start
< page
) {
148 "proc_file_read: Apparent buffer overflow!\n");
153 * Don't reduce n because doing so might
154 * cut off part of a data block.
157 "proc_file_read: Read count exceeded\n");
159 } else /* start >= page */ {
160 unsigned long startoff
= (unsigned long)(start
- page
);
161 if (n
> (PAGE_SIZE
- startoff
)) {
163 "proc_file_read: Apparent buffer overflow!\n");
164 n
= PAGE_SIZE
- startoff
;
170 n
-= copy_to_user(buf
, start
< page
? page
: start
, n
);
177 *ppos
+= start
< page
? (unsigned long)start
: n
;
182 free_page((unsigned long) page
);
187 proc_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
190 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
193 spin_lock(&pde
->pde_unload_lock
);
194 if (!pde
->proc_fops
) {
195 spin_unlock(&pde
->pde_unload_lock
);
199 spin_unlock(&pde
->pde_unload_lock
);
201 rv
= __proc_file_read(file
, buf
, nbytes
, ppos
);
208 proc_file_write(struct file
*file
, const char __user
*buffer
,
209 size_t count
, loff_t
*ppos
)
211 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
214 if (pde
->write_proc
) {
215 spin_lock(&pde
->pde_unload_lock
);
216 if (!pde
->proc_fops
) {
217 spin_unlock(&pde
->pde_unload_lock
);
221 spin_unlock(&pde
->pde_unload_lock
);
223 /* FIXME: does this routine need ppos? probably... */
224 rv
= pde
->write_proc(file
, buffer
, count
, pde
->data
);
232 proc_file_lseek(struct file
*file
, loff_t offset
, int orig
)
234 loff_t retval
= -EINVAL
;
237 offset
+= file
->f_pos
;
240 if (offset
< 0 || offset
> MAX_NON_LFS
)
242 file
->f_pos
= retval
= offset
;
247 static const struct file_operations proc_file_operations
= {
248 .llseek
= proc_file_lseek
,
249 .read
= proc_file_read
,
250 .write
= proc_file_write
,
253 static int proc_notify_change(struct dentry
*dentry
, struct iattr
*iattr
)
255 struct inode
*inode
= dentry
->d_inode
;
256 struct proc_dir_entry
*de
= PDE(inode
);
259 error
= inode_change_ok(inode
, iattr
);
263 error
= inode_setattr(inode
, iattr
);
267 de
->uid
= inode
->i_uid
;
268 de
->gid
= inode
->i_gid
;
269 de
->mode
= inode
->i_mode
;
274 static int proc_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
277 struct inode
*inode
= dentry
->d_inode
;
278 struct proc_dir_entry
*de
= PROC_I(inode
)->pde
;
280 inode
->i_nlink
= de
->nlink
;
282 generic_fillattr(inode
, stat
);
286 static const struct inode_operations proc_file_inode_operations
= {
287 .setattr
= proc_notify_change
,
291 * This function parses a name such as "tty/driver/serial", and
292 * returns the struct proc_dir_entry for "/proc/tty/driver", and
293 * returns "serial" in residual.
295 static int __xlate_proc_name(const char *name
, struct proc_dir_entry
**ret
,
296 const char **residual
)
298 const char *cp
= name
, *next
;
299 struct proc_dir_entry
*de
;
307 next
= strchr(cp
, '/');
312 for (de
= de
->subdir
; de
; de
= de
->next
) {
313 if (proc_match(len
, cp
, de
))
317 WARN(1, "name '%s'\n", name
);
327 static int xlate_proc_name(const char *name
, struct proc_dir_entry
**ret
,
328 const char **residual
)
332 spin_lock(&proc_subdir_lock
);
333 rv
= __xlate_proc_name(name
, ret
, residual
);
334 spin_unlock(&proc_subdir_lock
);
338 static DEFINE_IDA(proc_inum_ida
);
339 static DEFINE_SPINLOCK(proc_inum_lock
); /* protects the above */
341 #define PROC_DYNAMIC_FIRST 0xF0000000U
344 * Return an inode number between PROC_DYNAMIC_FIRST and
345 * 0xffffffff, or zero on failure.
347 static unsigned int get_inode_number(void)
353 if (ida_pre_get(&proc_inum_ida
, GFP_KERNEL
) == 0)
356 spin_lock(&proc_inum_lock
);
357 error
= ida_get_new(&proc_inum_ida
, &i
);
358 spin_unlock(&proc_inum_lock
);
359 if (error
== -EAGAIN
)
364 if (i
> UINT_MAX
- PROC_DYNAMIC_FIRST
) {
365 spin_lock(&proc_inum_lock
);
366 ida_remove(&proc_inum_ida
, i
);
367 spin_unlock(&proc_inum_lock
);
370 return PROC_DYNAMIC_FIRST
+ i
;
373 static void release_inode_number(unsigned int inum
)
375 spin_lock(&proc_inum_lock
);
376 ida_remove(&proc_inum_ida
, inum
- PROC_DYNAMIC_FIRST
);
377 spin_unlock(&proc_inum_lock
);
380 static void *proc_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
382 nd_set_link(nd
, PDE(dentry
->d_inode
)->data
);
386 static const struct inode_operations proc_link_inode_operations
= {
387 .readlink
= generic_readlink
,
388 .follow_link
= proc_follow_link
,
392 * As some entries in /proc are volatile, we want to
393 * get rid of unused dentries. This could be made
394 * smarter: we could keep a "volatile" flag in the
395 * inode to indicate which ones to keep.
397 static int proc_delete_dentry(struct dentry
* dentry
)
402 static const struct dentry_operations proc_dentry_operations
=
404 .d_delete
= proc_delete_dentry
,
408 * Don't create negative dentries here, return -ENOENT by hand
411 struct dentry
*proc_lookup_de(struct proc_dir_entry
*de
, struct inode
*dir
,
412 struct dentry
*dentry
)
414 struct inode
*inode
= NULL
;
417 spin_lock(&proc_subdir_lock
);
418 for (de
= de
->subdir
; de
; de
= de
->next
) {
419 if (de
->namelen
!= dentry
->d_name
.len
)
421 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
426 spin_unlock(&proc_subdir_lock
);
428 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
432 spin_unlock(&proc_subdir_lock
);
436 dentry
->d_op
= &proc_dentry_operations
;
437 d_add(dentry
, inode
);
442 return ERR_PTR(error
);
445 struct dentry
*proc_lookup(struct inode
*dir
, struct dentry
*dentry
,
446 struct nameidata
*nd
)
448 return proc_lookup_de(PDE(dir
), dir
, dentry
);
452 * This returns non-zero if at EOF, so that the /proc
453 * root directory can use this and check if it should
454 * continue with the <pid> entries..
456 * Note that the VFS-layer doesn't care about the return
457 * value of the readdir() call, as long as it's non-negative
460 int proc_readdir_de(struct proc_dir_entry
*de
, struct file
*filp
, void *dirent
,
465 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
472 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
478 if (filldir(dirent
, "..", 2, i
,
479 parent_ino(filp
->f_path
.dentry
),
486 spin_lock(&proc_subdir_lock
);
492 spin_unlock(&proc_subdir_lock
);
502 struct proc_dir_entry
*next
;
504 /* filldir passes info to user space */
506 spin_unlock(&proc_subdir_lock
);
507 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
,
508 de
->low_ino
, de
->mode
>> 12) < 0) {
512 spin_lock(&proc_subdir_lock
);
518 spin_unlock(&proc_subdir_lock
);
525 int proc_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
527 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
529 return proc_readdir_de(PDE(inode
), filp
, dirent
, filldir
);
533 * These are the generic /proc directory operations. They
534 * use the in-memory "struct proc_dir_entry" tree to parse
535 * the /proc directory.
537 static const struct file_operations proc_dir_operations
= {
538 .llseek
= generic_file_llseek
,
539 .read
= generic_read_dir
,
540 .readdir
= proc_readdir
,
544 * proc directories can do almost nothing..
546 static const struct inode_operations proc_dir_inode_operations
= {
547 .lookup
= proc_lookup
,
548 .getattr
= proc_getattr
,
549 .setattr
= proc_notify_change
,
552 static int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
555 struct proc_dir_entry
*tmp
;
557 i
= get_inode_number();
562 if (S_ISDIR(dp
->mode
)) {
563 if (dp
->proc_iops
== NULL
) {
564 dp
->proc_fops
= &proc_dir_operations
;
565 dp
->proc_iops
= &proc_dir_inode_operations
;
568 } else if (S_ISLNK(dp
->mode
)) {
569 if (dp
->proc_iops
== NULL
)
570 dp
->proc_iops
= &proc_link_inode_operations
;
571 } else if (S_ISREG(dp
->mode
)) {
572 if (dp
->proc_fops
== NULL
)
573 dp
->proc_fops
= &proc_file_operations
;
574 if (dp
->proc_iops
== NULL
)
575 dp
->proc_iops
= &proc_file_inode_operations
;
578 spin_lock(&proc_subdir_lock
);
580 for (tmp
= dir
->subdir
; tmp
; tmp
= tmp
->next
)
581 if (strcmp(tmp
->name
, dp
->name
) == 0) {
582 WARN(1, KERN_WARNING
"proc_dir_entry '%s/%s' already registered\n",
583 dir
->name
, dp
->name
);
587 dp
->next
= dir
->subdir
;
590 spin_unlock(&proc_subdir_lock
);
595 static struct proc_dir_entry
*__proc_create(struct proc_dir_entry
**parent
,
600 struct proc_dir_entry
*ent
= NULL
;
601 const char *fn
= name
;
604 /* make sure name is valid */
605 if (!name
|| !strlen(name
)) goto out
;
607 if (xlate_proc_name(name
, parent
, &fn
) != 0)
610 /* At this point there must not be any '/' characters beyond *fn */
616 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
619 memset(ent
, 0, sizeof(struct proc_dir_entry
));
620 memcpy(((char *) ent
) + sizeof(struct proc_dir_entry
), fn
, len
+ 1);
621 ent
->name
= ((char *) ent
) + sizeof(*ent
);
625 atomic_set(&ent
->count
, 1);
627 spin_lock_init(&ent
->pde_unload_lock
);
628 ent
->pde_unload_completion
= NULL
;
629 INIT_LIST_HEAD(&ent
->pde_openers
);
634 struct proc_dir_entry
*proc_symlink(const char *name
,
635 struct proc_dir_entry
*parent
, const char *dest
)
637 struct proc_dir_entry
*ent
;
639 ent
= __proc_create(&parent
, name
,
640 (S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
),1);
643 ent
->data
= kmalloc((ent
->size
=strlen(dest
))+1, GFP_KERNEL
);
645 strcpy((char*)ent
->data
,dest
);
646 if (proc_register(parent
, ent
) < 0) {
658 EXPORT_SYMBOL(proc_symlink
);
660 struct proc_dir_entry
*proc_mkdir_mode(const char *name
, mode_t mode
,
661 struct proc_dir_entry
*parent
)
663 struct proc_dir_entry
*ent
;
665 ent
= __proc_create(&parent
, name
, S_IFDIR
| mode
, 2);
667 if (proc_register(parent
, ent
) < 0) {
675 struct proc_dir_entry
*proc_net_mkdir(struct net
*net
, const char *name
,
676 struct proc_dir_entry
*parent
)
678 struct proc_dir_entry
*ent
;
680 ent
= __proc_create(&parent
, name
, S_IFDIR
| S_IRUGO
| S_IXUGO
, 2);
683 if (proc_register(parent
, ent
) < 0) {
690 EXPORT_SYMBOL_GPL(proc_net_mkdir
);
692 struct proc_dir_entry
*proc_mkdir(const char *name
,
693 struct proc_dir_entry
*parent
)
695 return proc_mkdir_mode(name
, S_IRUGO
| S_IXUGO
, parent
);
697 EXPORT_SYMBOL(proc_mkdir
);
699 struct proc_dir_entry
*create_proc_entry(const char *name
, mode_t mode
,
700 struct proc_dir_entry
*parent
)
702 struct proc_dir_entry
*ent
;
706 if ((mode
& S_IALLUGO
) == 0)
707 mode
|= S_IRUGO
| S_IXUGO
;
710 if ((mode
& S_IFMT
) == 0)
712 if ((mode
& S_IALLUGO
) == 0)
717 ent
= __proc_create(&parent
, name
, mode
, nlink
);
719 if (proc_register(parent
, ent
) < 0) {
726 EXPORT_SYMBOL(create_proc_entry
);
728 struct proc_dir_entry
*proc_create_data(const char *name
, mode_t mode
,
729 struct proc_dir_entry
*parent
,
730 const struct file_operations
*proc_fops
,
733 struct proc_dir_entry
*pde
;
737 if ((mode
& S_IALLUGO
) == 0)
738 mode
|= S_IRUGO
| S_IXUGO
;
741 if ((mode
& S_IFMT
) == 0)
743 if ((mode
& S_IALLUGO
) == 0)
748 pde
= __proc_create(&parent
, name
, mode
, nlink
);
751 pde
->proc_fops
= proc_fops
;
753 if (proc_register(parent
, pde
) < 0)
761 EXPORT_SYMBOL(proc_create_data
);
763 static void free_proc_entry(struct proc_dir_entry
*de
)
765 unsigned int ino
= de
->low_ino
;
767 if (ino
< PROC_DYNAMIC_FIRST
)
770 release_inode_number(ino
);
772 if (S_ISLNK(de
->mode
))
777 void pde_put(struct proc_dir_entry
*pde
)
779 if (atomic_dec_and_test(&pde
->count
))
780 free_proc_entry(pde
);
784 * Remove a /proc entry and free it if it's not currently in use.
786 void remove_proc_entry(const char *name
, struct proc_dir_entry
*parent
)
788 struct proc_dir_entry
**p
;
789 struct proc_dir_entry
*de
= NULL
;
790 const char *fn
= name
;
793 spin_lock(&proc_subdir_lock
);
794 if (__xlate_proc_name(name
, &parent
, &fn
) != 0) {
795 spin_unlock(&proc_subdir_lock
);
800 for (p
= &parent
->subdir
; *p
; p
=&(*p
)->next
) {
801 if (proc_match(len
, fn
, *p
)) {
808 spin_unlock(&proc_subdir_lock
);
810 WARN(1, "name '%s'\n", name
);
814 spin_lock(&de
->pde_unload_lock
);
816 * Stop accepting new callers into module. If you're
817 * dynamically allocating ->proc_fops, save a pointer somewhere.
819 de
->proc_fops
= NULL
;
820 /* Wait until all existing callers into module are done. */
821 if (de
->pde_users
> 0) {
822 DECLARE_COMPLETION_ONSTACK(c
);
824 if (!de
->pde_unload_completion
)
825 de
->pde_unload_completion
= &c
;
827 spin_unlock(&de
->pde_unload_lock
);
829 wait_for_completion(de
->pde_unload_completion
);
831 goto continue_removing
;
833 spin_unlock(&de
->pde_unload_lock
);
836 spin_lock(&de
->pde_unload_lock
);
837 while (!list_empty(&de
->pde_openers
)) {
838 struct pde_opener
*pdeo
;
840 pdeo
= list_first_entry(&de
->pde_openers
, struct pde_opener
, lh
);
842 spin_unlock(&de
->pde_unload_lock
);
843 pdeo
->release(pdeo
->inode
, pdeo
->file
);
845 spin_lock(&de
->pde_unload_lock
);
847 spin_unlock(&de
->pde_unload_lock
);
849 if (S_ISDIR(de
->mode
))
852 WARN(de
->subdir
, KERN_WARNING
"%s: removing non-empty directory "
853 "'%s/%s', leaking at least '%s'\n", __func__
,
854 de
->parent
->name
, de
->name
, de
->subdir
->name
);
857 EXPORT_SYMBOL(remove_proc_entry
);