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/vs_base.h>
22 #include <linux/vserver/inode.h>
23 #include <linux/bitops.h>
24 #include <linux/vserver/inode.h>
25 #include <asm/uaccess.h>
27 static ssize_t proc_file_read(struct file *file, char __user *buf,
28 size_t nbytes, loff_t *ppos);
29 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
30 size_t count, loff_t *ppos);
31 static loff_t proc_file_lseek(struct file *, loff_t, int);
33 int proc_match(int len, const char *name, struct proc_dir_entry *de)
35 if (de->namelen != len)
37 return !memcmp(name, de->name, len);
40 static struct file_operations proc_file_operations = {
41 .llseek = proc_file_lseek,
42 .read = proc_file_read,
43 .write = proc_file_write,
46 /* buffer size is one page but our output routines use some slack for overruns */
47 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
50 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
53 struct inode * inode = file->f_dentry->d_inode;
59 struct proc_dir_entry * dp;
62 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
65 while ((nbytes > 0) && !eof) {
66 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
70 /* Handle old net routines */
71 n = dp->get_info(page, &start, *ppos, count);
74 } else if (dp->read_proc) {
76 * How to be a proc read function
77 * ------------------------------
79 * int f(char *buffer, char **start, off_t offset,
80 * int count, int *peof, void *dat)
82 * Assume that the buffer is "count" bytes in size.
84 * If you know you have supplied all the data you
87 * You have three ways to return data:
88 * 0) Leave *start = NULL. (This is the default.)
89 * Put the data of the requested offset at that
90 * offset within the buffer. Return the number (n)
91 * of bytes there are from the beginning of the
92 * buffer up to the last byte of data. If the
93 * number of supplied bytes (= n - offset) is
94 * greater than zero and you didn't signal eof
95 * and the reader is prepared to take more data
96 * you will be called again with the requested
97 * offset advanced by the number of bytes
98 * absorbed. This interface is useful for files
99 * no larger than the buffer.
100 * 1) Set *start = an unsigned long value less than
101 * the buffer address but greater than zero.
102 * Put the data of the requested offset at the
103 * beginning of the buffer. Return the number of
104 * bytes of data placed there. If this number is
105 * greater than zero and you didn't signal eof
106 * and the reader is prepared to take more data
107 * you will be called again with the requested
108 * offset advanced by *start. This interface is
109 * useful when you have a large file consisting
110 * of a series of blocks which you want to count
111 * and return as wholes.
112 * (Hack by Paul.Russell@rustcorp.com.au)
113 * 2) Set *start = an address within the buffer.
114 * Put the data of the requested offset at *start.
115 * Return the number of bytes of data placed there.
116 * If this number is greater than zero and you
117 * didn't signal eof and the reader is prepared to
118 * take more data you will be called again with the
119 * requested offset advanced by the number of bytes
122 n = dp->read_proc(page, &start, *ppos,
123 count, &eof, dp->data);
127 if (n == 0) /* end of file */
129 if (n < 0) { /* error */
138 "proc_file_read: Apparent buffer overflow!\n");
146 start = page + *ppos;
147 } else if (start < page) {
150 "proc_file_read: Apparent buffer overflow!\n");
155 * Don't reduce n because doing so might
156 * cut off part of a data block.
159 "proc_file_read: Read count exceeded\n");
161 } else /* start >= page */ {
162 unsigned long startoff = (unsigned long)(start - page);
163 if (n > (PAGE_SIZE - startoff)) {
165 "proc_file_read: Apparent buffer overflow!\n");
166 n = PAGE_SIZE - startoff;
172 n -= copy_to_user(buf, start < page ? page : start, n);
179 *ppos += start < page ? (unsigned long)start : n;
184 free_page((unsigned long) page);
189 proc_file_write(struct file *file, const char __user *buffer,
190 size_t count, loff_t *ppos)
192 struct inode *inode = file->f_dentry->d_inode;
193 struct proc_dir_entry * dp;
200 /* FIXME: does this routine need ppos? probably... */
201 return dp->write_proc(file, buffer, count, dp->data);
206 proc_file_lseek(struct file *file, loff_t offset, int orig)
214 file->f_pos = offset;
218 if (offset + file->f_pos < 0)
220 file->f_pos += offset;
234 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
236 struct inode *inode = dentry->d_inode;
237 struct proc_dir_entry *de = PDE(inode);
240 error = inode_change_ok(inode, iattr);
244 error = inode_setattr(inode, iattr);
248 de->uid = inode->i_uid;
249 de->gid = inode->i_gid;
250 de->mode = inode->i_mode;
255 static struct inode_operations proc_file_inode_operations = {
256 .setattr = proc_notify_change,
260 * This function parses a name such as "tty/driver/serial", and
261 * returns the struct proc_dir_entry for "/proc/tty/driver", and
262 * returns "serial" in residual.
264 static int xlate_proc_name(const char *name,
265 struct proc_dir_entry **ret, const char **residual)
267 const char *cp = name, *next;
268 struct proc_dir_entry *de;
273 next = strchr(cp, '/');
278 for (de = de->subdir; de ; de = de->next) {
279 if (proc_match(len, cp, de))
291 static DEFINE_IDR(proc_inum_idr);
292 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
294 #define PROC_DYNAMIC_FIRST 0xF0000000UL
297 * Return an inode number between PROC_DYNAMIC_FIRST and
298 * 0xffffffff, or zero on failure.
300 static unsigned int get_inode_number(void)
306 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
309 spin_lock(&proc_inum_lock);
310 error = idr_get_new(&proc_inum_idr, NULL, &i);
311 spin_unlock(&proc_inum_lock);
312 if (error == -EAGAIN)
317 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
319 /* inum will never be more than 0xf0ffffff, so no check
326 static void release_inode_number(unsigned int inum)
328 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
330 spin_lock(&proc_inum_lock);
331 idr_remove(&proc_inum_idr, id);
332 spin_unlock(&proc_inum_lock);
335 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
337 nd_set_link(nd, PDE(dentry->d_inode)->data);
341 static struct inode_operations proc_link_inode_operations = {
342 .readlink = generic_readlink,
343 .follow_link = proc_follow_link,
347 * As some entries in /proc are volatile, we want to
348 * get rid of unused dentries. This could be made
349 * smarter: we could keep a "volatile" flag in the
350 * inode to indicate which ones to keep.
352 static int proc_delete_dentry(struct dentry * dentry)
357 static int proc_revalidate_dentry(struct dentry *de, struct nameidata *nd)
359 /* maybe add a check if it's really necessary? */
363 static struct dentry_operations proc_dentry_operations =
365 .d_revalidate = proc_revalidate_dentry,
366 .d_delete = proc_delete_dentry,
370 * Don't create negative dentries here, return -ENOENT by hand
373 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
375 struct inode *inode = NULL;
376 struct proc_dir_entry * de;
382 for (de = de->subdir; de ; de = de->next) {
383 if (de->namelen != dentry->d_name.len)
385 if (!vx_hide_check(0, de->vx_flags))
387 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
388 unsigned int ino = de->low_ino;
391 inode = proc_get_inode(dir->i_sb, ino, de);
392 /* generic proc entries belong to the host */
401 dentry->d_op = &proc_dentry_operations;
402 d_add(dentry, inode);
405 return ERR_PTR(error);
409 * This returns non-zero if at EOF, so that the /proc
410 * root directory can use this and check if it should
411 * continue with the <pid> entries..
413 * Note that the VFS-layer doesn't care about the return
414 * value of the readdir() call, as long as it's non-negative
417 int proc_readdir(struct file * filp,
418 void * dirent, filldir_t filldir)
420 struct proc_dir_entry * de;
423 struct inode *inode = filp->f_dentry->d_inode;
437 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
443 if (filldir(dirent, "..", 2, i,
444 parent_ino(filp->f_dentry),
465 if (!vx_hide_check(0, de->vx_flags))
467 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
468 de->low_ino, de->mode >> 12) < 0)
476 out: unlock_kernel();
481 * These are the generic /proc directory operations. They
482 * use the in-memory "struct proc_dir_entry" tree to parse
483 * the /proc directory.
485 static struct file_operations proc_dir_operations = {
486 .read = generic_read_dir,
487 .readdir = proc_readdir,
491 * proc directories can do almost nothing..
493 static struct inode_operations proc_dir_inode_operations = {
494 .lookup = proc_lookup,
495 .setattr = proc_notify_change,
498 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
502 i = get_inode_number();
506 dp->next = dir->subdir;
509 if (S_ISDIR(dp->mode)) {
510 if (dp->proc_iops == NULL) {
511 dp->proc_fops = &proc_dir_operations;
512 dp->proc_iops = &proc_dir_inode_operations;
515 } else if (S_ISLNK(dp->mode)) {
516 if (dp->proc_iops == NULL)
517 dp->proc_iops = &proc_link_inode_operations;
518 } else if (S_ISREG(dp->mode)) {
519 if (dp->proc_fops == NULL)
520 dp->proc_fops = &proc_file_operations;
521 if (dp->proc_iops == NULL)
522 dp->proc_iops = &proc_file_inode_operations;
528 * Kill an inode that got unregistered..
530 static void proc_kill_inodes(struct proc_dir_entry *de)
533 struct super_block *sb = proc_mnt->mnt_sb;
536 * Actually it's a partial revoke().
539 list_for_each(p, &sb->s_files) {
540 struct file * filp = list_entry(p, struct file, f_list);
541 struct dentry * dentry = filp->f_dentry;
542 struct inode * inode;
543 struct file_operations *fops;
545 if (dentry->d_op != &proc_dentry_operations)
547 inode = dentry->d_inode;
548 if (PDE(inode) != de)
557 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
562 struct proc_dir_entry *ent = NULL;
563 const char *fn = name;
566 /* make sure name is valid */
567 if (!name || !strlen(name)) goto out;
569 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
572 /* At this point there must not be any '/' characters beyond *fn */
578 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
581 memset(ent, 0, sizeof(struct proc_dir_entry));
582 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
583 ent->name = ((char *) ent) + sizeof(*ent);
587 ent->vx_flags = IATTR_PROC_DEFAULT;
592 struct proc_dir_entry *proc_symlink(const char *name,
593 struct proc_dir_entry *parent, const char *dest)
595 struct proc_dir_entry *ent;
597 ent = proc_create(&parent,name,
598 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
601 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
603 strcpy((char*)ent->data,dest);
604 if (proc_register(parent, ent) < 0) {
609 ent->vx_flags = IATTR_PROC_SYMLINK;
618 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
619 struct proc_dir_entry *parent)
621 struct proc_dir_entry *ent;
623 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
625 ent->proc_fops = &proc_dir_operations;
626 ent->proc_iops = &proc_dir_inode_operations;
628 if (proc_register(parent, ent) < 0) {
636 struct proc_dir_entry *proc_mkdir(const char *name,
637 struct proc_dir_entry *parent)
639 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
642 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
643 struct proc_dir_entry *parent)
645 struct proc_dir_entry *ent;
649 if ((mode & S_IALLUGO) == 0)
650 mode |= S_IRUGO | S_IXUGO;
653 if ((mode & S_IFMT) == 0)
655 if ((mode & S_IALLUGO) == 0)
660 ent = proc_create(&parent,name,mode,nlink);
663 ent->proc_fops = &proc_dir_operations;
664 ent->proc_iops = &proc_dir_inode_operations;
666 if (proc_register(parent, ent) < 0) {
674 void free_proc_entry(struct proc_dir_entry *de)
676 unsigned int ino = de->low_ino;
678 if (ino < PROC_DYNAMIC_FIRST)
681 release_inode_number(ino);
683 if (S_ISLNK(de->mode) && de->data)
689 * Remove a /proc entry and free it if it's not currently in use.
690 * If it is in use, we set the 'deleted' flag.
692 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
694 struct proc_dir_entry **p;
695 struct proc_dir_entry *de;
696 const char *fn = name;
699 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
702 for (p = &parent->subdir; *p; p=&(*p)->next ) {
703 if (!proc_match(len, fn, *p))
708 if (S_ISDIR(de->mode))
710 proc_kill_inodes(de);
713 if (!atomic_read(&de->count))
717 printk("remove_proc_entry: %s/%s busy, count=%d\n",
718 parent->name, de->name, atomic_read(&de->count));