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 <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);
73 /* Handle old net routines */
74 n = dp->get_info(page, &start, *ppos, count);
77 } else if (dp->read_proc) {
79 * How to be a proc read function
80 * ------------------------------
82 * int f(char *buffer, char **start, off_t offset,
83 * int count, int *peof, void *dat)
85 * Assume that the buffer is "count" bytes in size.
87 * If you know you have supplied all the data you
90 * You have three ways to return data:
91 * 0) Leave *start = NULL. (This is the default.)
92 * Put the data of the requested offset at that
93 * offset within the buffer. Return the number (n)
94 * of bytes there are from the beginning of the
95 * buffer up to the last byte of data. If the
96 * number of supplied bytes (= n - offset) is
97 * greater than zero and you didn't signal eof
98 * and the reader is prepared to take more data
99 * you will be called again with the requested
100 * offset advanced by the number of bytes
101 * absorbed. This interface is useful for files
102 * no larger than the buffer.
103 * 1) Set *start = an unsigned long value less than
104 * the buffer address but greater than zero.
105 * Put the data of the requested offset at the
106 * beginning of the buffer. Return the number of
107 * bytes of data placed there. If this number 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 *start. This interface is
112 * useful when you have a large file consisting
113 * of a series of blocks which you want to count
114 * and return as wholes.
115 * (Hack by Paul.Russell@rustcorp.com.au)
116 * 2) Set *start = an address within the buffer.
117 * Put the data of the requested offset at *start.
118 * Return the number of bytes of data placed there.
119 * If this number is greater than zero and you
120 * didn't signal eof and the reader is prepared to
121 * take more data you will be called again with the
122 * requested offset advanced by the number of bytes
125 n = dp->read_proc(page, &start, *ppos,
126 count, &eof, dp->data);
130 if (n == 0) /* end of file */
132 if (n < 0) { /* error */
141 "proc_file_read: Apparent buffer overflow!\n");
149 start = page + *ppos;
150 } else if (start < page) {
153 "proc_file_read: Apparent buffer overflow!\n");
158 * Don't reduce n because doing so might
159 * cut off part of a data block.
162 "proc_file_read: Read count exceeded\n");
164 } else /* start >= page */ {
165 unsigned long startoff = (unsigned long)(start - page);
166 if (n > (PAGE_SIZE - startoff)) {
168 "proc_file_read: Apparent buffer overflow!\n");
169 n = PAGE_SIZE - startoff;
175 n -= copy_to_user(buf, start < page ? page : start, n);
182 *ppos += start < page ? (unsigned long)start : n;
187 free_page((unsigned long) page);
192 proc_file_write(struct file *file, const char __user *buffer,
193 size_t count, loff_t *ppos)
195 struct inode *inode = file->f_path.dentry->d_inode;
196 struct proc_dir_entry * dp;
203 /* FIXME: does this routine need ppos? probably... */
204 return dp->write_proc(file, buffer, count, dp->data);
209 proc_file_lseek(struct file *file, loff_t offset, int orig)
211 loff_t retval = -EINVAL;
214 offset += file->f_pos;
217 if (offset < 0 || offset > MAX_NON_LFS)
219 file->f_pos = retval = offset;
224 static const struct file_operations proc_file_operations = {
225 .llseek = proc_file_lseek,
226 .read = proc_file_read,
227 .write = proc_file_write,
230 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
232 struct inode *inode = dentry->d_inode;
233 struct proc_dir_entry *de = PDE(inode);
236 error = inode_change_ok(inode, iattr);
240 error = inode_setattr(inode, iattr);
244 de->uid = inode->i_uid;
245 de->gid = inode->i_gid;
246 de->mode = inode->i_mode;
251 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
254 struct inode *inode = dentry->d_inode;
255 struct proc_dir_entry *de = PROC_I(inode)->pde;
257 inode->i_nlink = de->nlink;
259 generic_fillattr(inode, stat);
263 static const struct inode_operations proc_file_inode_operations = {
264 .setattr = proc_notify_change,
268 * This function parses a name such as "tty/driver/serial", and
269 * returns the struct proc_dir_entry for "/proc/tty/driver", and
270 * returns "serial" in residual.
272 static int xlate_proc_name(const char *name,
273 struct proc_dir_entry **ret, const char **residual)
275 const char *cp = name, *next;
276 struct proc_dir_entry *de;
284 spin_lock(&proc_subdir_lock);
286 next = strchr(cp, '/');
291 for (de = de->subdir; de ; de = de->next) {
292 if (proc_match(len, cp, de))
304 spin_unlock(&proc_subdir_lock);
308 static DEFINE_IDR(proc_inum_idr);
309 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
311 #define PROC_DYNAMIC_FIRST 0xF0000000UL
314 * Return an inode number between PROC_DYNAMIC_FIRST and
315 * 0xffffffff, or zero on failure.
317 static unsigned int get_inode_number(void)
323 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
326 spin_lock(&proc_inum_lock);
327 error = idr_get_new(&proc_inum_idr, NULL, &i);
328 spin_unlock(&proc_inum_lock);
329 if (error == -EAGAIN)
334 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
336 /* inum will never be more than 0xf0ffffff, so no check
343 static void release_inode_number(unsigned int inum)
345 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
347 spin_lock(&proc_inum_lock);
348 idr_remove(&proc_inum_idr, id);
349 spin_unlock(&proc_inum_lock);
352 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
354 nd_set_link(nd, PDE(dentry->d_inode)->data);
358 static const struct inode_operations proc_link_inode_operations = {
359 .readlink = generic_readlink,
360 .follow_link = proc_follow_link,
364 * As some entries in /proc are volatile, we want to
365 * get rid of unused dentries. This could be made
366 * smarter: we could keep a "volatile" flag in the
367 * inode to indicate which ones to keep.
369 static int proc_delete_dentry(struct dentry * dentry)
374 static struct dentry_operations proc_dentry_operations =
376 .d_delete = proc_delete_dentry,
380 * Don't create negative dentries here, return -ENOENT by hand
383 struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
384 struct dentry *dentry)
386 struct inode *inode = NULL;
390 spin_lock(&proc_subdir_lock);
391 for (de = de->subdir; de ; de = de->next) {
392 if (de->namelen != dentry->d_name.len)
394 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
399 spin_unlock(&proc_subdir_lock);
401 inode = proc_get_inode(dir->i_sb, ino, de);
405 spin_unlock(&proc_subdir_lock);
410 dentry->d_op = &proc_dentry_operations;
411 d_add(dentry, inode);
416 return ERR_PTR(error);
419 struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
420 struct nameidata *nd)
422 return proc_lookup_de(PDE(dir), dir, dentry);
426 * This returns non-zero if at EOF, so that the /proc
427 * root directory can use this and check if it should
428 * continue with the <pid> entries..
430 * Note that the VFS-layer doesn't care about the return
431 * value of the readdir() call, as long as it's non-negative
434 int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
439 struct inode *inode = filp->f_path.dentry->d_inode;
448 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
454 if (filldir(dirent, "..", 2, i,
455 parent_ino(filp->f_path.dentry),
462 spin_lock(&proc_subdir_lock);
468 spin_unlock(&proc_subdir_lock);
478 struct proc_dir_entry *next;
480 /* filldir passes info to user space */
482 spin_unlock(&proc_subdir_lock);
483 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
484 de->low_ino, de->mode >> 12) < 0) {
488 spin_lock(&proc_subdir_lock);
494 spin_unlock(&proc_subdir_lock);
497 out: unlock_kernel();
501 int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
503 struct inode *inode = filp->f_path.dentry->d_inode;
505 return proc_readdir_de(PDE(inode), filp, dirent, filldir);
509 * These are the generic /proc directory operations. They
510 * use the in-memory "struct proc_dir_entry" tree to parse
511 * the /proc directory.
513 static const struct file_operations proc_dir_operations = {
514 .read = generic_read_dir,
515 .readdir = proc_readdir,
519 * proc directories can do almost nothing..
521 static const struct inode_operations proc_dir_inode_operations = {
522 .lookup = proc_lookup,
523 .getattr = proc_getattr,
524 .setattr = proc_notify_change,
527 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
530 struct proc_dir_entry *tmp;
532 i = get_inode_number();
537 if (S_ISDIR(dp->mode)) {
538 if (dp->proc_iops == NULL) {
539 dp->proc_fops = &proc_dir_operations;
540 dp->proc_iops = &proc_dir_inode_operations;
543 } else if (S_ISLNK(dp->mode)) {
544 if (dp->proc_iops == NULL)
545 dp->proc_iops = &proc_link_inode_operations;
546 } else if (S_ISREG(dp->mode)) {
547 if (dp->proc_fops == NULL)
548 dp->proc_fops = &proc_file_operations;
549 if (dp->proc_iops == NULL)
550 dp->proc_iops = &proc_file_inode_operations;
553 spin_lock(&proc_subdir_lock);
555 for (tmp = dir->subdir; tmp; tmp = tmp->next)
556 if (strcmp(tmp->name, dp->name) == 0) {
557 printk(KERN_WARNING "proc_dir_entry '%s' already "
558 "registered\n", dp->name);
563 dp->next = dir->subdir;
566 spin_unlock(&proc_subdir_lock);
571 static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
576 struct proc_dir_entry *ent = NULL;
577 const char *fn = name;
580 /* make sure name is valid */
581 if (!name || !strlen(name)) goto out;
583 if (xlate_proc_name(name, parent, &fn) != 0)
586 /* At this point there must not be any '/' characters beyond *fn */
592 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
595 memset(ent, 0, sizeof(struct proc_dir_entry));
596 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
597 ent->name = ((char *) ent) + sizeof(*ent);
601 atomic_set(&ent->count, 1);
603 spin_lock_init(&ent->pde_unload_lock);
604 ent->pde_unload_completion = NULL;
609 struct proc_dir_entry *proc_symlink(const char *name,
610 struct proc_dir_entry *parent, const char *dest)
612 struct proc_dir_entry *ent;
614 ent = __proc_create(&parent, name,
615 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
618 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
620 strcpy((char*)ent->data,dest);
621 if (proc_register(parent, ent) < 0) {
634 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
635 struct proc_dir_entry *parent)
637 struct proc_dir_entry *ent;
639 ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
641 if (proc_register(parent, ent) < 0) {
649 struct proc_dir_entry *proc_mkdir(const char *name,
650 struct proc_dir_entry *parent)
652 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
655 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
656 struct proc_dir_entry *parent)
658 struct proc_dir_entry *ent;
662 if ((mode & S_IALLUGO) == 0)
663 mode |= S_IRUGO | S_IXUGO;
666 if ((mode & S_IFMT) == 0)
668 if ((mode & S_IALLUGO) == 0)
673 ent = __proc_create(&parent, name, mode, nlink);
675 if (proc_register(parent, ent) < 0) {
683 struct proc_dir_entry *proc_create(const char *name, mode_t mode,
684 struct proc_dir_entry *parent,
685 const struct file_operations *proc_fops)
687 struct proc_dir_entry *pde;
691 if ((mode & S_IALLUGO) == 0)
692 mode |= S_IRUGO | S_IXUGO;
695 if ((mode & S_IFMT) == 0)
697 if ((mode & S_IALLUGO) == 0)
702 pde = __proc_create(&parent, name, mode, nlink);
705 pde->proc_fops = proc_fops;
706 if (proc_register(parent, pde) < 0)
715 void free_proc_entry(struct proc_dir_entry *de)
717 unsigned int ino = de->low_ino;
719 if (ino < PROC_DYNAMIC_FIRST)
722 release_inode_number(ino);
724 if (S_ISLNK(de->mode))
730 * Remove a /proc entry and free it if it's not currently in use.
732 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
734 struct proc_dir_entry **p;
735 struct proc_dir_entry *de = NULL;
736 const char *fn = name;
739 if (xlate_proc_name(name, &parent, &fn) != 0)
743 spin_lock(&proc_subdir_lock);
744 for (p = &parent->subdir; *p; p=&(*p)->next ) {
745 if (proc_match(len, fn, *p)) {
752 spin_unlock(&proc_subdir_lock);
756 spin_lock(&de->pde_unload_lock);
758 * Stop accepting new callers into module. If you're
759 * dynamically allocating ->proc_fops, save a pointer somewhere.
761 de->proc_fops = NULL;
762 /* Wait until all existing callers into module are done. */
763 if (de->pde_users > 0) {
764 DECLARE_COMPLETION_ONSTACK(c);
766 if (!de->pde_unload_completion)
767 de->pde_unload_completion = &c;
769 spin_unlock(&de->pde_unload_lock);
771 wait_for_completion(de->pde_unload_completion);
773 goto continue_removing;
775 spin_unlock(&de->pde_unload_lock);
778 if (S_ISDIR(de->mode))
782 printk(KERN_WARNING "%s: removing non-empty directory "
783 "'%s/%s', leaking at least '%s'\n", __func__,
784 de->parent->name, de->name, de->subdir->name);
787 if (atomic_dec_and_test(&de->count))