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_read(struct file *file, char __user *buf, size_t nbytes,
189 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
192 spin_lock(&pde->pde_unload_lock);
193 if (!pde->proc_fops) {
194 spin_unlock(&pde->pde_unload_lock);
198 spin_unlock(&pde->pde_unload_lock);
200 rv = __proc_file_read(file, buf, nbytes, ppos);
207 proc_file_write(struct file *file, const char __user *buffer,
208 size_t count, loff_t *ppos)
210 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
213 if (pde->write_proc) {
214 spin_lock(&pde->pde_unload_lock);
215 if (!pde->proc_fops) {
216 spin_unlock(&pde->pde_unload_lock);
220 spin_unlock(&pde->pde_unload_lock);
222 /* FIXME: does this routine need ppos? probably... */
223 rv = pde->write_proc(file, buffer, count, pde->data);
231 proc_file_lseek(struct file *file, loff_t offset, int orig)
233 loff_t retval = -EINVAL;
236 offset += file->f_pos;
239 if (offset < 0 || offset > MAX_NON_LFS)
241 file->f_pos = retval = offset;
246 static const struct file_operations proc_file_operations = {
247 .llseek = proc_file_lseek,
248 .read = proc_file_read,
249 .write = proc_file_write,
252 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
254 struct inode *inode = dentry->d_inode;
255 struct proc_dir_entry *de = PDE(inode);
258 error = inode_change_ok(inode, iattr);
262 error = inode_setattr(inode, iattr);
266 de->uid = inode->i_uid;
267 de->gid = inode->i_gid;
268 de->mode = inode->i_mode;
273 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
276 struct inode *inode = dentry->d_inode;
277 struct proc_dir_entry *de = PROC_I(inode)->pde;
279 inode->i_nlink = de->nlink;
281 generic_fillattr(inode, stat);
285 static const struct inode_operations proc_file_inode_operations = {
286 .setattr = proc_notify_change,
290 * This function parses a name such as "tty/driver/serial", and
291 * returns the struct proc_dir_entry for "/proc/tty/driver", and
292 * returns "serial" in residual.
294 static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
295 const char **residual)
297 const char *cp = name, *next;
298 struct proc_dir_entry *de;
306 next = strchr(cp, '/');
311 for (de = de->subdir; de ; de = de->next) {
312 if (proc_match(len, cp, de))
324 static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
325 const char **residual)
329 spin_lock(&proc_subdir_lock);
330 rv = __xlate_proc_name(name, ret, residual);
331 spin_unlock(&proc_subdir_lock);
335 static DEFINE_IDA(proc_inum_ida);
336 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
338 #define PROC_DYNAMIC_FIRST 0xF0000000U
341 * Return an inode number between PROC_DYNAMIC_FIRST and
342 * 0xffffffff, or zero on failure.
344 * Current inode allocations in the proc-fs (hex-numbers):
347 * 00000001-00000fff static entries (goners)
350 * 00001000-00001fff unused
351 * 0001xxxx-7fffxxxx pid-dir entries for pid 1-7fff
352 * 80000000-efffffff unused
353 * f0000000-ffffffff dynamic entries
356 * Once we split the thing into several virtual filesystems,
357 * we will get rid of magical ranges (and this comment, BTW).
359 static unsigned int get_inode_number(void)
365 if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0)
368 spin_lock(&proc_inum_lock);
369 error = ida_get_new(&proc_inum_ida, &i);
370 spin_unlock(&proc_inum_lock);
371 if (error == -EAGAIN)
376 if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
377 spin_lock(&proc_inum_lock);
378 ida_remove(&proc_inum_ida, i);
379 spin_unlock(&proc_inum_lock);
382 return PROC_DYNAMIC_FIRST + i;
385 static void release_inode_number(unsigned int inum)
387 spin_lock(&proc_inum_lock);
388 ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
389 spin_unlock(&proc_inum_lock);
392 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
394 nd_set_link(nd, PDE(dentry->d_inode)->data);
398 static const struct inode_operations proc_link_inode_operations = {
399 .readlink = generic_readlink,
400 .follow_link = proc_follow_link,
404 * As some entries in /proc are volatile, we want to
405 * get rid of unused dentries. This could be made
406 * smarter: we could keep a "volatile" flag in the
407 * inode to indicate which ones to keep.
409 static int proc_delete_dentry(struct dentry * dentry)
414 static const struct dentry_operations proc_dentry_operations =
416 .d_delete = proc_delete_dentry,
420 * Don't create negative dentries here, return -ENOENT by hand
423 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
424 struct dentry *dentry)
426 struct inode *inode = NULL;
429 spin_lock(&proc_subdir_lock);
430 for (de = de->subdir; de ; de = de->next) {
431 if (de->namelen != dentry->d_name.len)
433 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
438 spin_unlock(&proc_subdir_lock);
440 inode = proc_get_inode(dir->i_sb, ino, de);
444 spin_unlock(&proc_subdir_lock);
448 dentry->d_op = &proc_dentry_operations;
449 d_add(dentry, inode);
454 return ERR_PTR(error);
457 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
458 struct nameidata *nd)
460 return proc_lookup_de(PDE(dir), dir, dentry);
464 * This returns non-zero if at EOF, so that the /proc
465 * root directory can use this and check if it should
466 * continue with the <pid> entries..
468 * Note that the VFS-layer doesn't care about the return
469 * value of the readdir() call, as long as it's non-negative
472 int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
477 struct inode *inode = filp->f_path.dentry->d_inode;
484 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
490 if (filldir(dirent, "..", 2, i,
491 parent_ino(filp->f_path.dentry),
498 spin_lock(&proc_subdir_lock);
504 spin_unlock(&proc_subdir_lock);
514 struct proc_dir_entry *next;
516 /* filldir passes info to user space */
518 spin_unlock(&proc_subdir_lock);
519 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
520 de->low_ino, de->mode >> 12) < 0) {
524 spin_lock(&proc_subdir_lock);
530 spin_unlock(&proc_subdir_lock);
537 int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
539 struct inode *inode = filp->f_path.dentry->d_inode;
541 return proc_readdir_de(PDE(inode), filp, dirent, filldir);
545 * These are the generic /proc directory operations. They
546 * use the in-memory "struct proc_dir_entry" tree to parse
547 * the /proc directory.
549 static const struct file_operations proc_dir_operations = {
550 .llseek = generic_file_llseek,
551 .read = generic_read_dir,
552 .readdir = proc_readdir,
556 * proc directories can do almost nothing..
558 static const struct inode_operations proc_dir_inode_operations = {
559 .lookup = proc_lookup,
560 .getattr = proc_getattr,
561 .setattr = proc_notify_change,
564 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
567 struct proc_dir_entry *tmp;
569 i = get_inode_number();
574 if (S_ISDIR(dp->mode)) {
575 if (dp->proc_iops == NULL) {
576 dp->proc_fops = &proc_dir_operations;
577 dp->proc_iops = &proc_dir_inode_operations;
580 } else if (S_ISLNK(dp->mode)) {
581 if (dp->proc_iops == NULL)
582 dp->proc_iops = &proc_link_inode_operations;
583 } else if (S_ISREG(dp->mode)) {
584 if (dp->proc_fops == NULL)
585 dp->proc_fops = &proc_file_operations;
586 if (dp->proc_iops == NULL)
587 dp->proc_iops = &proc_file_inode_operations;
590 spin_lock(&proc_subdir_lock);
592 for (tmp = dir->subdir; tmp; tmp = tmp->next)
593 if (strcmp(tmp->name, dp->name) == 0) {
594 WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n",
595 dir->name, dp->name);
599 dp->next = dir->subdir;
602 spin_unlock(&proc_subdir_lock);
607 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
612 struct proc_dir_entry *ent = NULL;
613 const char *fn = name;
616 /* make sure name is valid */
617 if (!name || !strlen(name)) goto out;
619 if (xlate_proc_name(name, parent, &fn) != 0)
622 /* At this point there must not be any '/' characters beyond *fn */
628 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
631 memset(ent, 0, sizeof(struct proc_dir_entry));
632 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
633 ent->name = ((char *) ent) + sizeof(*ent);
637 atomic_set(&ent->count, 1);
639 spin_lock_init(&ent->pde_unload_lock);
640 ent->pde_unload_completion = NULL;
641 INIT_LIST_HEAD(&ent->pde_openers);
646 struct proc_dir_entry *proc_symlink(const char *name,
647 struct proc_dir_entry *parent, const char *dest)
649 struct proc_dir_entry *ent;
651 ent = __proc_create(&parent, name,
652 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
655 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
657 strcpy((char*)ent->data,dest);
658 if (proc_register(parent, ent) < 0) {
670 EXPORT_SYMBOL(proc_symlink);
672 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
673 struct proc_dir_entry *parent)
675 struct proc_dir_entry *ent;
677 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
679 if (proc_register(parent, ent) < 0) {
687 struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
688 struct proc_dir_entry *parent)
690 struct proc_dir_entry *ent;
692 ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
695 if (proc_register(parent, ent) < 0) {
702 EXPORT_SYMBOL_GPL(proc_net_mkdir);
704 struct proc_dir_entry *proc_mkdir(const char *name,
705 struct proc_dir_entry *parent)
707 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
709 EXPORT_SYMBOL(proc_mkdir);
711 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
712 struct proc_dir_entry *parent)
714 struct proc_dir_entry *ent;
718 if ((mode & S_IALLUGO) == 0)
719 mode |= S_IRUGO | S_IXUGO;
722 if ((mode & S_IFMT) == 0)
724 if ((mode & S_IALLUGO) == 0)
729 ent = __proc_create(&parent, name, mode, nlink);
731 if (proc_register(parent, ent) < 0) {
738 EXPORT_SYMBOL(create_proc_entry);
740 struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
741 struct proc_dir_entry *parent,
742 const struct file_operations *proc_fops,
745 struct proc_dir_entry *pde;
749 if ((mode & S_IALLUGO) == 0)
750 mode |= S_IRUGO | S_IXUGO;
753 if ((mode & S_IFMT) == 0)
755 if ((mode & S_IALLUGO) == 0)
760 pde = __proc_create(&parent, name, mode, nlink);
763 pde->proc_fops = proc_fops;
765 if (proc_register(parent, pde) < 0)
773 EXPORT_SYMBOL(proc_create_data);
775 static void free_proc_entry(struct proc_dir_entry *de)
777 unsigned int ino = de->low_ino;
779 if (ino < PROC_DYNAMIC_FIRST)
782 release_inode_number(ino);
784 if (S_ISLNK(de->mode))
789 void pde_put(struct proc_dir_entry *pde)
791 if (atomic_dec_and_test(&pde->count))
792 free_proc_entry(pde);
796 * Remove a /proc entry and free it if it's not currently in use.
798 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
800 struct proc_dir_entry **p;
801 struct proc_dir_entry *de = NULL;
802 const char *fn = name;
805 spin_lock(&proc_subdir_lock);
806 if (__xlate_proc_name(name, &parent, &fn) != 0) {
807 spin_unlock(&proc_subdir_lock);
812 for (p = &parent->subdir; *p; p=&(*p)->next ) {
813 if (proc_match(len, fn, *p)) {
820 spin_unlock(&proc_subdir_lock);
824 spin_lock(&de->pde_unload_lock);
826 * Stop accepting new callers into module. If you're
827 * dynamically allocating ->proc_fops, save a pointer somewhere.
829 de->proc_fops = NULL;
830 /* Wait until all existing callers into module are done. */
831 if (de->pde_users > 0) {
832 DECLARE_COMPLETION_ONSTACK(c);
834 if (!de->pde_unload_completion)
835 de->pde_unload_completion = &c;
837 spin_unlock(&de->pde_unload_lock);
839 wait_for_completion(de->pde_unload_completion);
841 goto continue_removing;
843 spin_unlock(&de->pde_unload_lock);
846 spin_lock(&de->pde_unload_lock);
847 while (!list_empty(&de->pde_openers)) {
848 struct pde_opener *pdeo;
850 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
852 spin_unlock(&de->pde_unload_lock);
853 pdeo->release(pdeo->inode, pdeo->file);
855 spin_lock(&de->pde_unload_lock);
857 spin_unlock(&de->pde_unload_lock);
859 if (S_ISDIR(de->mode))
862 WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
863 "'%s/%s', leaking at least '%s'\n", __func__,
864 de->parent->name, de->name, de->subdir->name);
867 EXPORT_SYMBOL(remove_proc_entry);