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 <asm/uaccess.h>
21 #include <asm/bitops.h>
23 static ssize_t proc_file_read(struct file *file, char __user *buf,
24 size_t nbytes, loff_t *ppos);
25 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
26 size_t count, loff_t *ppos);
27 static loff_t proc_file_lseek(struct file *, loff_t, int);
29 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 static struct file_operations proc_file_operations = {
37 .llseek = proc_file_lseek,
38 .read = proc_file_read,
39 .write = proc_file_write,
42 /* buffer size is one page but our output routines use some slack for overruns */
43 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
46 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
49 struct inode * inode = file->f_dentry->d_inode;
55 struct proc_dir_entry * dp;
58 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
61 while ((nbytes > 0) && !eof) {
62 count = min_t(ssize_t, PROC_BLOCK_SIZE, nbytes);
66 /* Handle old net routines */
67 n = dp->get_info(page, &start, *ppos, count);
70 } else if (dp->read_proc) {
72 * How to be a proc read function
73 * ------------------------------
75 * int f(char *buffer, char **start, off_t offset,
76 * int count, int *peof, void *dat)
78 * Assume that the buffer is "count" bytes in size.
80 * If you know you have supplied all the data you
83 * You have three ways to return data:
84 * 0) Leave *start = NULL. (This is the default.)
85 * Put the data of the requested offset at that
86 * offset within the buffer. Return the number (n)
87 * of bytes there are from the beginning of the
88 * buffer up to the last byte of data. If the
89 * number of supplied bytes (= n - offset) is
90 * greater than zero and you didn't signal eof
91 * and the reader is prepared to take more data
92 * you will be called again with the requested
93 * offset advanced by the number of bytes
94 * absorbed. This interface is useful for files
95 * no larger than the buffer.
96 * 1) Set *start = an unsigned long value less than
97 * the buffer address but greater than zero.
98 * Put the data of the requested offset at the
99 * beginning of the buffer. Return the number of
100 * bytes of data placed there. If this number is
101 * greater than zero and you didn't signal eof
102 * and the reader is prepared to take more data
103 * you will be called again with the requested
104 * offset advanced by *start. This interface is
105 * useful when you have a large file consisting
106 * of a series of blocks which you want to count
107 * and return as wholes.
108 * (Hack by Paul.Russell@rustcorp.com.au)
109 * 2) Set *start = an address within the buffer.
110 * Put the data of the requested offset at *start.
111 * Return the number of bytes of data placed there.
112 * If this number is greater than zero and you
113 * didn't signal eof and the reader is prepared to
114 * take more data you will be called again with the
115 * requested offset advanced by the number of bytes
118 n = dp->read_proc(page, &start, *ppos,
119 count, &eof, dp->data);
123 if (n == 0) /* end of file */
125 if (n < 0) { /* error */
134 "proc_file_read: Apparent buffer overflow!\n");
142 start = page + *ppos;
143 } else if (start < page) {
146 "proc_file_read: Apparent buffer overflow!\n");
151 * Don't reduce n because doing so might
152 * cut off part of a data block.
155 "proc_file_read: Read count exceeded\n");
157 } else /* start >= page */ {
158 unsigned long startoff = (unsigned long)(start - page);
159 if (n > (PAGE_SIZE - startoff)) {
161 "proc_file_read: Apparent buffer overflow!\n");
162 n = PAGE_SIZE - startoff;
168 n -= copy_to_user(buf, start < page ? page : start, n);
175 *ppos += start < page ? (unsigned long)start : n;
180 free_page((unsigned long) page);
185 proc_file_write(struct file *file, const char __user *buffer,
186 size_t count, loff_t *ppos)
188 struct inode *inode = file->f_dentry->d_inode;
189 struct proc_dir_entry * dp;
196 /* FIXME: does this routine need ppos? probably... */
197 return dp->write_proc(file, buffer, count, dp->data);
202 proc_file_lseek(struct file *file, loff_t offset, int orig)
210 file->f_pos = offset;
214 if (offset + file->f_pos < 0)
216 file->f_pos += offset;
230 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
232 struct inode *inode = dentry->d_inode;
233 int error = inode_setattr(inode, iattr);
235 struct proc_dir_entry *de = PDE(inode);
236 de->uid = inode->i_uid;
237 de->gid = inode->i_gid;
238 de->mode = inode->i_mode;
244 static struct inode_operations proc_file_inode_operations = {
245 .setattr = proc_notify_change,
249 * This function parses a name such as "tty/driver/serial", and
250 * returns the struct proc_dir_entry for "/proc/tty/driver", and
251 * returns "serial" in residual.
253 static int xlate_proc_name(const char *name,
254 struct proc_dir_entry **ret, const char **residual)
256 const char *cp = name, *next;
257 struct proc_dir_entry *de;
262 next = strchr(cp, '/');
267 for (de = de->subdir; de ; de = de->next) {
268 if (proc_match(len, cp, de))
280 static DEFINE_IDR(proc_inum_idr);
281 static spinlock_t proc_inum_lock = SPIN_LOCK_UNLOCKED; /* protects the above */
283 #define PROC_DYNAMIC_FIRST 0xF0000000UL
286 * Return an inode number between PROC_DYNAMIC_FIRST and
287 * 0xffffffff, or zero on failure.
289 static unsigned int get_inode_number(void)
291 unsigned int i, inum = 0;
294 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
297 spin_lock(&proc_inum_lock);
298 i = idr_get_new(&proc_inum_idr, NULL);
299 spin_unlock(&proc_inum_lock);
304 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
306 /* inum will never be more than 0xf0ffffff, so no check
313 static void release_inode_number(unsigned int inum)
315 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
317 spin_lock(&proc_inum_lock);
318 idr_remove(&proc_inum_idr, id);
319 spin_unlock(&proc_inum_lock);
322 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
324 nd_set_link(nd, PDE(dentry->d_inode)->data);
328 static struct inode_operations proc_link_inode_operations = {
329 .readlink = generic_readlink,
330 .follow_link = proc_follow_link,
334 * As some entries in /proc are volatile, we want to
335 * get rid of unused dentries. This could be made
336 * smarter: we could keep a "volatile" flag in the
337 * inode to indicate which ones to keep.
339 static int proc_delete_dentry(struct dentry * dentry)
344 static struct dentry_operations proc_dentry_operations =
346 .d_delete = proc_delete_dentry,
350 * Don't create negative dentries here, return -ENOENT by hand
353 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
355 struct inode *inode = NULL;
356 struct proc_dir_entry * de;
362 for (de = de->subdir; de ; de = de->next) {
363 if (de->namelen != dentry->d_name.len)
365 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
366 unsigned int ino = de->low_ino;
369 inode = proc_get_inode(dir->i_sb, ino, de);
377 dentry->d_op = &proc_dentry_operations;
378 d_add(dentry, inode);
381 return ERR_PTR(error);
385 * This returns non-zero if at EOF, so that the /proc
386 * root directory can use this and check if it should
387 * continue with the <pid> entries..
389 * Note that the VFS-layer doesn't care about the return
390 * value of the readdir() call, as long as it's non-negative
393 int proc_readdir(struct file * filp,
394 void * dirent, filldir_t filldir)
396 struct proc_dir_entry * de;
399 struct inode *inode = filp->f_dentry->d_inode;
413 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
419 if (filldir(dirent, "..", 2, i,
420 parent_ino(filp->f_dentry),
441 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
442 de->low_ino, de->mode >> 12) < 0)
449 out: unlock_kernel();
454 * These are the generic /proc directory operations. They
455 * use the in-memory "struct proc_dir_entry" tree to parse
456 * the /proc directory.
458 static struct file_operations proc_dir_operations = {
459 .read = generic_read_dir,
460 .readdir = proc_readdir,
464 * proc directories can do almost nothing..
466 static struct inode_operations proc_dir_inode_operations = {
467 .lookup = proc_lookup,
468 .setattr = proc_notify_change,
471 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
475 i = get_inode_number();
479 dp->next = dir->subdir;
482 if (S_ISDIR(dp->mode)) {
483 if (dp->proc_iops == NULL) {
484 dp->proc_fops = &proc_dir_operations;
485 dp->proc_iops = &proc_dir_inode_operations;
488 } else if (S_ISLNK(dp->mode)) {
489 if (dp->proc_iops == NULL)
490 dp->proc_iops = &proc_link_inode_operations;
491 } else if (S_ISREG(dp->mode)) {
492 if (dp->proc_fops == NULL)
493 dp->proc_fops = &proc_file_operations;
494 if (dp->proc_iops == NULL)
495 dp->proc_iops = &proc_file_inode_operations;
501 * Kill an inode that got unregistered..
503 static void proc_kill_inodes(struct proc_dir_entry *de)
506 struct super_block *sb = proc_mnt->mnt_sb;
509 * Actually it's a partial revoke().
512 list_for_each(p, &sb->s_files) {
513 struct file * filp = list_entry(p, struct file, f_list);
514 struct dentry * dentry = filp->f_dentry;
515 struct inode * inode;
516 struct file_operations *fops;
518 if (dentry->d_op != &proc_dentry_operations)
520 inode = dentry->d_inode;
521 if (PDE(inode) != de)
530 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
535 struct proc_dir_entry *ent = NULL;
536 const char *fn = name;
539 /* make sure name is valid */
540 if (!name || !strlen(name)) goto out;
542 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
546 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
549 memset(ent, 0, sizeof(struct proc_dir_entry));
550 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
551 ent->name = ((char *) ent) + sizeof(*ent);
559 struct proc_dir_entry *proc_symlink(const char *name,
560 struct proc_dir_entry *parent, const char *dest)
562 struct proc_dir_entry *ent;
564 ent = proc_create(&parent,name,
565 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
568 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
570 strcpy((char*)ent->data,dest);
571 if (proc_register(parent, ent) < 0) {
584 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
585 struct proc_dir_entry *parent)
587 struct proc_dir_entry *ent;
589 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
591 ent->proc_fops = &proc_dir_operations;
592 ent->proc_iops = &proc_dir_inode_operations;
594 if (proc_register(parent, ent) < 0) {
602 struct proc_dir_entry *proc_mkdir(const char *name,
603 struct proc_dir_entry *parent)
605 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
608 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
609 struct proc_dir_entry *parent)
611 struct proc_dir_entry *ent;
615 if ((mode & S_IALLUGO) == 0)
616 mode |= S_IRUGO | S_IXUGO;
619 if ((mode & S_IFMT) == 0)
621 if ((mode & S_IALLUGO) == 0)
626 ent = proc_create(&parent,name,mode,nlink);
629 ent->proc_fops = &proc_dir_operations;
630 ent->proc_iops = &proc_dir_inode_operations;
632 if (proc_register(parent, ent) < 0) {
640 void free_proc_entry(struct proc_dir_entry *de)
642 unsigned int ino = de->low_ino;
644 if (ino < PROC_DYNAMIC_FIRST)
647 release_inode_number(ino);
649 if (S_ISLNK(de->mode) && de->data)
655 * Remove a /proc entry and free it if it's not currently in use.
656 * If it is in use, we set the 'deleted' flag.
658 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
660 struct proc_dir_entry **p;
661 struct proc_dir_entry *de;
662 const char *fn = name;
665 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
668 for (p = &parent->subdir; *p; p=&(*p)->next ) {
669 if (!proc_match(len, fn, *p))
674 if (S_ISDIR(de->mode))
676 proc_kill_inodes(de);
679 if (!atomic_read(&de->count))
683 printk("remove_proc_entry: %s/%s busy, count=%d\n",
684 parent->name, de->name, atomic_read(&de->count));