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/init.h>
18 #include <linux/idr.h>
19 #include <linux/namei.h>
20 #include <linux/bitops.h>
21 #include <linux/spinlock.h>
22 #include <linux/completion.h>
23 #include <asm/uaccess.h>
27 DEFINE_SPINLOCK(proc_subdir_lock
);
29 static int proc_match(int len
, const char *name
, struct proc_dir_entry
*de
)
31 if (de
->namelen
!= len
)
33 return !memcmp(name
, de
->name
, len
);
36 /* buffer size is one page but our output routines use some slack for overruns */
37 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
40 proc_file_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
43 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
49 struct proc_dir_entry
* dp
;
50 unsigned long long pos
;
53 * Gaah, please just use "seq_file" instead. The legacy /proc
54 * interfaces cut loff_t down to off_t for reads, and ignore
55 * the offset entirely for writes..
58 if (pos
> MAX_NON_LFS
)
60 if (nbytes
> MAX_NON_LFS
- pos
)
61 nbytes
= MAX_NON_LFS
- pos
;
64 if (!(page
= (char*) __get_free_page(GFP_TEMPORARY
)))
67 while ((nbytes
> 0) && !eof
) {
68 count
= min_t(size_t, PROC_BLOCK_SIZE
, nbytes
);
73 * How to be a proc read function
74 * ------------------------------
76 * int f(char *buffer, char **start, off_t offset,
77 * int count, int *peof, void *dat)
79 * Assume that the buffer is "count" bytes in size.
81 * If you know you have supplied all the data you
84 * You have three ways to return data:
85 * 0) Leave *start = NULL. (This is the default.)
86 * Put the data of the requested offset at that
87 * offset within the buffer. Return the number (n)
88 * of bytes there are from the beginning of the
89 * buffer up to the last byte of data. If the
90 * number of supplied bytes (= n - offset) is
91 * greater than zero and you didn't signal eof
92 * and the reader is prepared to take more data
93 * you will be called again with the requested
94 * offset advanced by the number of bytes
95 * absorbed. This interface is useful for files
96 * no larger than the buffer.
97 * 1) Set *start = an unsigned long value less than
98 * the buffer address but greater than zero.
99 * Put the data of the requested offset at the
100 * beginning of the buffer. Return the number of
101 * bytes of data placed there. If this number is
102 * greater than zero and you didn't signal eof
103 * and the reader is prepared to take more data
104 * you will be called again with the requested
105 * offset advanced by *start. This interface is
106 * useful when you have a large file consisting
107 * of a series of blocks which you want to count
108 * and return as wholes.
109 * (Hack by Paul.Russell@rustcorp.com.au)
110 * 2) Set *start = an address within the buffer.
111 * Put the data of the requested offset at *start.
112 * Return the number of bytes of data placed there.
113 * If this number is greater than zero and you
114 * didn't signal eof and the reader is prepared to
115 * take more data you will be called again with the
116 * requested offset advanced by the number of bytes
119 n
= dp
->read_proc(page
, &start
, *ppos
,
120 count
, &eof
, dp
->data
);
124 if (n
== 0) /* end of file */
126 if (n
< 0) { /* error */
135 "proc_file_read: Apparent buffer overflow!\n");
143 start
= page
+ *ppos
;
144 } else if (start
< page
) {
147 "proc_file_read: Apparent buffer overflow!\n");
152 * Don't reduce n because doing so might
153 * cut off part of a data block.
156 "proc_file_read: Read count exceeded\n");
158 } else /* start >= page */ {
159 unsigned long startoff
= (unsigned long)(start
- page
);
160 if (n
> (PAGE_SIZE
- startoff
)) {
162 "proc_file_read: Apparent buffer overflow!\n");
163 n
= PAGE_SIZE
- startoff
;
169 n
-= copy_to_user(buf
, start
< page
? page
: start
, n
);
176 *ppos
+= start
< page
? (unsigned long)start
: n
;
181 free_page((unsigned long) page
);
186 proc_file_write(struct file
*file
, const char __user
*buffer
,
187 size_t count
, loff_t
*ppos
)
189 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
190 struct proc_dir_entry
* dp
;
197 /* FIXME: does this routine need ppos? probably... */
198 return dp
->write_proc(file
, buffer
, count
, dp
->data
);
203 proc_file_lseek(struct file
*file
, loff_t offset
, int orig
)
205 loff_t retval
= -EINVAL
;
208 offset
+= file
->f_pos
;
211 if (offset
< 0 || offset
> MAX_NON_LFS
)
213 file
->f_pos
= retval
= offset
;
218 static const struct file_operations proc_file_operations
= {
219 .llseek
= proc_file_lseek
,
220 .read
= proc_file_read
,
221 .write
= proc_file_write
,
224 static int proc_notify_change(struct dentry
*dentry
, struct iattr
*iattr
)
226 struct inode
*inode
= dentry
->d_inode
;
227 struct proc_dir_entry
*de
= PDE(inode
);
230 error
= inode_change_ok(inode
, iattr
);
234 error
= inode_setattr(inode
, iattr
);
238 de
->uid
= inode
->i_uid
;
239 de
->gid
= inode
->i_gid
;
240 de
->mode
= inode
->i_mode
;
245 static int proc_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
248 struct inode
*inode
= dentry
->d_inode
;
249 struct proc_dir_entry
*de
= PROC_I(inode
)->pde
;
251 inode
->i_nlink
= de
->nlink
;
253 generic_fillattr(inode
, stat
);
257 static const struct inode_operations proc_file_inode_operations
= {
258 .setattr
= proc_notify_change
,
262 * This function parses a name such as "tty/driver/serial", and
263 * returns the struct proc_dir_entry for "/proc/tty/driver", and
264 * returns "serial" in residual.
266 static int xlate_proc_name(const char *name
,
267 struct proc_dir_entry
**ret
, const char **residual
)
269 const char *cp
= name
, *next
;
270 struct proc_dir_entry
*de
;
278 spin_lock(&proc_subdir_lock
);
280 next
= strchr(cp
, '/');
285 for (de
= de
->subdir
; de
; de
= de
->next
) {
286 if (proc_match(len
, cp
, de
))
298 spin_unlock(&proc_subdir_lock
);
302 static DEFINE_IDA(proc_inum_ida
);
303 static DEFINE_SPINLOCK(proc_inum_lock
); /* protects the above */
305 #define PROC_DYNAMIC_FIRST 0xF0000000U
308 * Return an inode number between PROC_DYNAMIC_FIRST and
309 * 0xffffffff, or zero on failure.
311 * Current inode allocations in the proc-fs (hex-numbers):
314 * 00000001-00000fff static entries (goners)
317 * 00001000-00001fff unused
318 * 0001xxxx-7fffxxxx pid-dir entries for pid 1-7fff
319 * 80000000-efffffff unused
320 * f0000000-ffffffff dynamic entries
323 * Once we split the thing into several virtual filesystems,
324 * we will get rid of magical ranges (and this comment, BTW).
326 static unsigned int get_inode_number(void)
332 if (ida_pre_get(&proc_inum_ida
, GFP_KERNEL
) == 0)
335 spin_lock(&proc_inum_lock
);
336 error
= ida_get_new(&proc_inum_ida
, &i
);
337 spin_unlock(&proc_inum_lock
);
338 if (error
== -EAGAIN
)
343 if (i
> UINT_MAX
- PROC_DYNAMIC_FIRST
) {
344 spin_lock(&proc_inum_lock
);
345 ida_remove(&proc_inum_ida
, i
);
346 spin_unlock(&proc_inum_lock
);
349 return PROC_DYNAMIC_FIRST
+ i
;
352 static void release_inode_number(unsigned int inum
)
354 spin_lock(&proc_inum_lock
);
355 ida_remove(&proc_inum_ida
, inum
- PROC_DYNAMIC_FIRST
);
356 spin_unlock(&proc_inum_lock
);
359 static void *proc_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
361 nd_set_link(nd
, PDE(dentry
->d_inode
)->data
);
365 static const struct inode_operations proc_link_inode_operations
= {
366 .readlink
= generic_readlink
,
367 .follow_link
= proc_follow_link
,
371 * As some entries in /proc are volatile, we want to
372 * get rid of unused dentries. This could be made
373 * smarter: we could keep a "volatile" flag in the
374 * inode to indicate which ones to keep.
376 static int proc_delete_dentry(struct dentry
* dentry
)
381 static const struct dentry_operations proc_dentry_operations
=
383 .d_delete
= proc_delete_dentry
,
387 * Don't create negative dentries here, return -ENOENT by hand
390 struct dentry
*proc_lookup_de(struct proc_dir_entry
*de
, struct inode
*dir
,
391 struct dentry
*dentry
)
393 struct inode
*inode
= NULL
;
396 spin_lock(&proc_subdir_lock
);
397 for (de
= de
->subdir
; de
; de
= de
->next
) {
398 if (de
->namelen
!= dentry
->d_name
.len
)
400 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
405 spin_unlock(&proc_subdir_lock
);
407 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
411 spin_unlock(&proc_subdir_lock
);
415 dentry
->d_op
= &proc_dentry_operations
;
416 d_add(dentry
, inode
);
421 return ERR_PTR(error
);
424 struct dentry
*proc_lookup(struct inode
*dir
, struct dentry
*dentry
,
425 struct nameidata
*nd
)
427 return proc_lookup_de(PDE(dir
), dir
, dentry
);
431 * This returns non-zero if at EOF, so that the /proc
432 * root directory can use this and check if it should
433 * continue with the <pid> entries..
435 * Note that the VFS-layer doesn't care about the return
436 * value of the readdir() call, as long as it's non-negative
439 int proc_readdir_de(struct proc_dir_entry
*de
, struct file
*filp
, void *dirent
,
444 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
);
504 int proc_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
506 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
508 return proc_readdir_de(PDE(inode
), filp
, dirent
, filldir
);
512 * These are the generic /proc directory operations. They
513 * use the in-memory "struct proc_dir_entry" tree to parse
514 * the /proc directory.
516 static const struct file_operations proc_dir_operations
= {
517 .llseek
= generic_file_llseek
,
518 .read
= generic_read_dir
,
519 .readdir
= proc_readdir
,
523 * proc directories can do almost nothing..
525 static const struct inode_operations proc_dir_inode_operations
= {
526 .lookup
= proc_lookup
,
527 .getattr
= proc_getattr
,
528 .setattr
= proc_notify_change
,
531 static int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
534 struct proc_dir_entry
*tmp
;
536 i
= get_inode_number();
541 if (S_ISDIR(dp
->mode
)) {
542 if (dp
->proc_iops
== NULL
) {
543 dp
->proc_fops
= &proc_dir_operations
;
544 dp
->proc_iops
= &proc_dir_inode_operations
;
547 } else if (S_ISLNK(dp
->mode
)) {
548 if (dp
->proc_iops
== NULL
)
549 dp
->proc_iops
= &proc_link_inode_operations
;
550 } else if (S_ISREG(dp
->mode
)) {
551 if (dp
->proc_fops
== NULL
)
552 dp
->proc_fops
= &proc_file_operations
;
553 if (dp
->proc_iops
== NULL
)
554 dp
->proc_iops
= &proc_file_inode_operations
;
557 spin_lock(&proc_subdir_lock
);
559 for (tmp
= dir
->subdir
; tmp
; tmp
= tmp
->next
)
560 if (strcmp(tmp
->name
, dp
->name
) == 0) {
561 WARN(1, KERN_WARNING
"proc_dir_entry '%s/%s' already registered\n",
562 dir
->name
, dp
->name
);
566 dp
->next
= dir
->subdir
;
569 spin_unlock(&proc_subdir_lock
);
574 static struct proc_dir_entry
*__proc_create(struct proc_dir_entry
**parent
,
579 struct proc_dir_entry
*ent
= NULL
;
580 const char *fn
= name
;
583 /* make sure name is valid */
584 if (!name
|| !strlen(name
)) goto out
;
586 if (xlate_proc_name(name
, parent
, &fn
) != 0)
589 /* At this point there must not be any '/' characters beyond *fn */
595 ent
= kmalloc(sizeof(struct proc_dir_entry
) + len
+ 1, GFP_KERNEL
);
598 memset(ent
, 0, sizeof(struct proc_dir_entry
));
599 memcpy(((char *) ent
) + sizeof(struct proc_dir_entry
), fn
, len
+ 1);
600 ent
->name
= ((char *) ent
) + sizeof(*ent
);
604 atomic_set(&ent
->count
, 1);
606 spin_lock_init(&ent
->pde_unload_lock
);
607 ent
->pde_unload_completion
= NULL
;
608 INIT_LIST_HEAD(&ent
->pde_openers
);
613 struct proc_dir_entry
*proc_symlink(const char *name
,
614 struct proc_dir_entry
*parent
, const char *dest
)
616 struct proc_dir_entry
*ent
;
618 ent
= __proc_create(&parent
, name
,
619 (S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
),1);
622 ent
->data
= kmalloc((ent
->size
=strlen(dest
))+1, GFP_KERNEL
);
624 strcpy((char*)ent
->data
,dest
);
625 if (proc_register(parent
, ent
) < 0) {
638 struct proc_dir_entry
*proc_mkdir_mode(const char *name
, mode_t mode
,
639 struct proc_dir_entry
*parent
)
641 struct proc_dir_entry
*ent
;
643 ent
= __proc_create(&parent
, name
, S_IFDIR
| mode
, 2);
645 if (proc_register(parent
, ent
) < 0) {
653 struct proc_dir_entry
*proc_net_mkdir(struct net
*net
, const char *name
,
654 struct proc_dir_entry
*parent
)
656 struct proc_dir_entry
*ent
;
658 ent
= __proc_create(&parent
, name
, S_IFDIR
| S_IRUGO
| S_IXUGO
, 2);
661 if (proc_register(parent
, ent
) < 0) {
668 EXPORT_SYMBOL_GPL(proc_net_mkdir
);
670 struct proc_dir_entry
*proc_mkdir(const char *name
,
671 struct proc_dir_entry
*parent
)
673 return proc_mkdir_mode(name
, S_IRUGO
| S_IXUGO
, parent
);
676 struct proc_dir_entry
*create_proc_entry(const char *name
, mode_t mode
,
677 struct proc_dir_entry
*parent
)
679 struct proc_dir_entry
*ent
;
683 if ((mode
& S_IALLUGO
) == 0)
684 mode
|= S_IRUGO
| S_IXUGO
;
687 if ((mode
& S_IFMT
) == 0)
689 if ((mode
& S_IALLUGO
) == 0)
694 ent
= __proc_create(&parent
, name
, mode
, nlink
);
696 if (proc_register(parent
, ent
) < 0) {
704 struct proc_dir_entry
*proc_create_data(const char *name
, mode_t mode
,
705 struct proc_dir_entry
*parent
,
706 const struct file_operations
*proc_fops
,
709 struct proc_dir_entry
*pde
;
713 if ((mode
& S_IALLUGO
) == 0)
714 mode
|= S_IRUGO
| S_IXUGO
;
717 if ((mode
& S_IFMT
) == 0)
719 if ((mode
& S_IALLUGO
) == 0)
724 pde
= __proc_create(&parent
, name
, mode
, nlink
);
727 pde
->proc_fops
= proc_fops
;
729 if (proc_register(parent
, pde
) < 0)
738 void free_proc_entry(struct proc_dir_entry
*de
)
740 unsigned int ino
= de
->low_ino
;
742 if (ino
< PROC_DYNAMIC_FIRST
)
745 release_inode_number(ino
);
747 if (S_ISLNK(de
->mode
))
753 * Remove a /proc entry and free it if it's not currently in use.
755 void remove_proc_entry(const char *name
, struct proc_dir_entry
*parent
)
757 struct proc_dir_entry
**p
;
758 struct proc_dir_entry
*de
= NULL
;
759 const char *fn
= name
;
762 if (xlate_proc_name(name
, &parent
, &fn
) != 0)
766 spin_lock(&proc_subdir_lock
);
767 for (p
= &parent
->subdir
; *p
; p
=&(*p
)->next
) {
768 if (proc_match(len
, fn
, *p
)) {
775 spin_unlock(&proc_subdir_lock
);
779 spin_lock(&de
->pde_unload_lock
);
781 * Stop accepting new callers into module. If you're
782 * dynamically allocating ->proc_fops, save a pointer somewhere.
784 de
->proc_fops
= NULL
;
785 /* Wait until all existing callers into module are done. */
786 if (de
->pde_users
> 0) {
787 DECLARE_COMPLETION_ONSTACK(c
);
789 if (!de
->pde_unload_completion
)
790 de
->pde_unload_completion
= &c
;
792 spin_unlock(&de
->pde_unload_lock
);
794 wait_for_completion(de
->pde_unload_completion
);
796 goto continue_removing
;
798 spin_unlock(&de
->pde_unload_lock
);
801 spin_lock(&de
->pde_unload_lock
);
802 while (!list_empty(&de
->pde_openers
)) {
803 struct pde_opener
*pdeo
;
805 pdeo
= list_first_entry(&de
->pde_openers
, struct pde_opener
, lh
);
807 spin_unlock(&de
->pde_unload_lock
);
808 pdeo
->release(pdeo
->inode
, pdeo
->file
);
810 spin_lock(&de
->pde_unload_lock
);
812 spin_unlock(&de
->pde_unload_lock
);
814 if (S_ISDIR(de
->mode
))
817 WARN(de
->subdir
, KERN_WARNING
"%s: removing non-empty directory "
818 "'%s/%s', leaking at least '%s'\n", __func__
,
819 de
->parent
->name
, de
->name
, de
->subdir
->name
);
820 if (atomic_dec_and_test(&de
->count
))