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/vs_base.h>
21 #include <linux/vserver/inode.h>
22 #include <linux/namei.h>
23 #include <asm/uaccess.h>
24 #include <asm/bitops.h>
26 static ssize_t proc_file_read(struct file *file, char __user *buf,
27 size_t nbytes, loff_t *ppos);
28 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
29 size_t count, loff_t *ppos);
30 static loff_t proc_file_lseek(struct file *, loff_t, int);
32 int proc_match(int len, const char *name, struct proc_dir_entry *de)
34 if (de->namelen != len)
36 return !memcmp(name, de->name, len);
39 static struct file_operations proc_file_operations = {
40 .llseek = proc_file_lseek,
41 .read = proc_file_read,
42 .write = proc_file_write,
45 /* buffer size is one page but our output routines use some slack for overruns */
46 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
49 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
52 struct inode * inode = file->f_dentry->d_inode;
58 struct proc_dir_entry * dp;
61 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
64 while ((nbytes > 0) && !eof) {
65 count = min_t(ssize_t, PROC_BLOCK_SIZE, nbytes);
69 /* Handle old net routines */
70 n = dp->get_info(page, &start, *ppos, count);
73 } else if (dp->read_proc) {
75 * How to be a proc read function
76 * ------------------------------
78 * int f(char *buffer, char **start, off_t offset,
79 * int count, int *peof, void *dat)
81 * Assume that the buffer is "count" bytes in size.
83 * If you know you have supplied all the data you
86 * You have three ways to return data:
87 * 0) Leave *start = NULL. (This is the default.)
88 * Put the data of the requested offset at that
89 * offset within the buffer. Return the number (n)
90 * of bytes there are from the beginning of the
91 * buffer up to the last byte of data. If the
92 * number of supplied bytes (= n - offset) is
93 * greater than zero and you didn't signal eof
94 * and the reader is prepared to take more data
95 * you will be called again with the requested
96 * offset advanced by the number of bytes
97 * absorbed. This interface is useful for files
98 * no larger than the buffer.
99 * 1) Set *start = an unsigned long value less than
100 * the buffer address but greater than zero.
101 * Put the data of the requested offset at the
102 * beginning of the buffer. Return the number of
103 * bytes of data placed there. If this number is
104 * greater than zero and you didn't signal eof
105 * and the reader is prepared to take more data
106 * you will be called again with the requested
107 * offset advanced by *start. This interface is
108 * useful when you have a large file consisting
109 * of a series of blocks which you want to count
110 * and return as wholes.
111 * (Hack by Paul.Russell@rustcorp.com.au)
112 * 2) Set *start = an address within the buffer.
113 * Put the data of the requested offset at *start.
114 * Return the number of bytes of data placed there.
115 * If this number is greater than zero and you
116 * didn't signal eof and the reader is prepared to
117 * take more data you will be called again with the
118 * requested offset advanced by the number of bytes
121 n = dp->read_proc(page, &start, *ppos,
122 count, &eof, dp->data);
126 if (n == 0) /* end of file */
128 if (n < 0) { /* error */
137 "proc_file_read: Apparent buffer overflow!\n");
145 start = page + *ppos;
146 } else if (start < page) {
149 "proc_file_read: Apparent buffer overflow!\n");
154 * Don't reduce n because doing so might
155 * cut off part of a data block.
158 "proc_file_read: Read count exceeded\n");
160 } else /* start >= page */ {
161 unsigned long startoff = (unsigned long)(start - page);
162 if (n > (PAGE_SIZE - startoff)) {
164 "proc_file_read: Apparent buffer overflow!\n");
165 n = PAGE_SIZE - startoff;
171 n -= copy_to_user(buf, start < page ? page : start, n);
178 *ppos += start < page ? (unsigned long)start : n;
183 free_page((unsigned long) page);
188 proc_file_write(struct file *file, const char __user *buffer,
189 size_t count, loff_t *ppos)
191 struct inode *inode = file->f_dentry->d_inode;
192 struct proc_dir_entry * dp;
199 /* FIXME: does this routine need ppos? probably... */
200 return dp->write_proc(file, buffer, count, dp->data);
205 proc_file_lseek(struct file *file, loff_t offset, int orig)
213 file->f_pos = offset;
217 if (offset + file->f_pos < 0)
219 file->f_pos += offset;
233 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
235 struct inode *inode = dentry->d_inode;
236 struct proc_dir_entry *de = PDE(inode);
239 error = inode_change_ok(inode, iattr);
243 error = inode_setattr(inode, iattr);
247 de->uid = inode->i_uid;
248 de->gid = inode->i_gid;
249 de->mode = inode->i_mode;
254 static struct inode_operations proc_file_inode_operations = {
255 .setattr = proc_notify_change,
259 * This function parses a name such as "tty/driver/serial", and
260 * returns the struct proc_dir_entry for "/proc/tty/driver", and
261 * returns "serial" in residual.
263 static int xlate_proc_name(const char *name,
264 struct proc_dir_entry **ret, const char **residual)
266 const char *cp = name, *next;
267 struct proc_dir_entry *de;
272 next = strchr(cp, '/');
277 for (de = de->subdir; de ; de = de->next) {
278 if (proc_match(len, cp, de))
290 static DEFINE_IDR(proc_inum_idr);
291 static spinlock_t proc_inum_lock = SPIN_LOCK_UNLOCKED; /* protects the above */
293 #define PROC_DYNAMIC_FIRST 0xF0000000UL
296 * Return an inode number between PROC_DYNAMIC_FIRST and
297 * 0xffffffff, or zero on failure.
299 static unsigned int get_inode_number(void)
305 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
308 spin_lock(&proc_inum_lock);
309 error = idr_get_new(&proc_inum_idr, NULL, &i);
310 spin_unlock(&proc_inum_lock);
311 if (error == -EAGAIN)
316 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
318 /* inum will never be more than 0xf0ffffff, so no check
325 static void release_inode_number(unsigned int inum)
327 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
329 spin_lock(&proc_inum_lock);
330 idr_remove(&proc_inum_idr, id);
331 spin_unlock(&proc_inum_lock);
334 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
336 nd_set_link(nd, PDE(dentry->d_inode)->data);
340 static struct inode_operations proc_link_inode_operations = {
341 .readlink = generic_readlink,
342 .follow_link = proc_follow_link,
346 * As some entries in /proc are volatile, we want to
347 * get rid of unused dentries. This could be made
348 * smarter: we could keep a "volatile" flag in the
349 * inode to indicate which ones to keep.
351 static int proc_delete_dentry(struct dentry * dentry)
356 static int proc_revalidate_dentry(struct dentry *de, struct nameidata *nd)
358 /* maybe add a check if it's really necessary? */
362 static struct dentry_operations proc_dentry_operations =
364 .d_revalidate = proc_revalidate_dentry,
365 .d_delete = proc_delete_dentry,
369 * Don't create negative dentries here, return -ENOENT by hand
372 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
374 struct inode *inode = NULL;
375 struct proc_dir_entry * de;
381 for (de = de->subdir; de ; de = de->next) {
382 if (de->namelen != dentry->d_name.len)
384 if (!vx_hide_check(0, de->vx_flags))
386 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
387 unsigned int ino = de->low_ino;
390 inode = proc_get_inode(dir->i_sb, ino, de);
398 dentry->d_op = &proc_dentry_operations;
399 d_add(dentry, inode);
402 return ERR_PTR(error);
406 * This returns non-zero if at EOF, so that the /proc
407 * root directory can use this and check if it should
408 * continue with the <pid> entries..
410 * Note that the VFS-layer doesn't care about the return
411 * value of the readdir() call, as long as it's non-negative
414 int proc_readdir(struct file * filp,
415 void * dirent, filldir_t filldir)
417 struct proc_dir_entry * de;
420 struct inode *inode = filp->f_dentry->d_inode;
434 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
440 if (filldir(dirent, "..", 2, i,
441 parent_ino(filp->f_dentry),
462 if (!vx_hide_check(0, de->vx_flags))
464 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
465 de->low_ino, de->mode >> 12) < 0)
473 out: unlock_kernel();
478 * These are the generic /proc directory operations. They
479 * use the in-memory "struct proc_dir_entry" tree to parse
480 * the /proc directory.
482 static struct file_operations proc_dir_operations = {
483 .read = generic_read_dir,
484 .readdir = proc_readdir,
488 * proc directories can do almost nothing..
490 static struct inode_operations proc_dir_inode_operations = {
491 .lookup = proc_lookup,
492 .setattr = proc_notify_change,
495 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
499 i = get_inode_number();
503 dp->next = dir->subdir;
506 if (S_ISDIR(dp->mode)) {
507 if (dp->proc_iops == NULL) {
508 dp->proc_fops = &proc_dir_operations;
509 dp->proc_iops = &proc_dir_inode_operations;
512 } else if (S_ISLNK(dp->mode)) {
513 if (dp->proc_iops == NULL)
514 dp->proc_iops = &proc_link_inode_operations;
515 } else if (S_ISREG(dp->mode)) {
516 if (dp->proc_fops == NULL)
517 dp->proc_fops = &proc_file_operations;
518 if (dp->proc_iops == NULL)
519 dp->proc_iops = &proc_file_inode_operations;
525 * Kill an inode that got unregistered..
527 static void proc_kill_inodes(struct proc_dir_entry *de)
530 struct super_block *sb = proc_mnt->mnt_sb;
533 * Actually it's a partial revoke().
536 list_for_each(p, &sb->s_files) {
537 struct file * filp = list_entry(p, struct file, f_list);
538 struct dentry * dentry = filp->f_dentry;
539 struct inode * inode;
540 struct file_operations *fops;
542 if (dentry->d_op != &proc_dentry_operations)
544 inode = dentry->d_inode;
545 if (PDE(inode) != de)
554 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
559 struct proc_dir_entry *ent = NULL;
560 const char *fn = name;
563 /* make sure name is valid */
564 if (!name || !strlen(name)) goto out;
566 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
570 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
573 memset(ent, 0, sizeof(struct proc_dir_entry));
574 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
575 ent->name = ((char *) ent) + sizeof(*ent);
579 ent->vx_flags = IATTR_PROC_DEFAULT;
584 struct proc_dir_entry *proc_symlink(const char *name,
585 struct proc_dir_entry *parent, const char *dest)
587 struct proc_dir_entry *ent;
589 ent = proc_create(&parent,name,
590 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
593 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
595 strcpy((char*)ent->data,dest);
596 if (proc_register(parent, ent) < 0) {
601 ent->vx_flags = IATTR_PROC_SYMLINK;
610 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
611 struct proc_dir_entry *parent)
613 struct proc_dir_entry *ent;
615 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
617 ent->proc_fops = &proc_dir_operations;
618 ent->proc_iops = &proc_dir_inode_operations;
620 if (proc_register(parent, ent) < 0) {
628 struct proc_dir_entry *proc_mkdir(const char *name,
629 struct proc_dir_entry *parent)
631 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
634 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
635 struct proc_dir_entry *parent)
637 struct proc_dir_entry *ent;
641 if ((mode & S_IALLUGO) == 0)
642 mode |= S_IRUGO | S_IXUGO;
645 if ((mode & S_IFMT) == 0)
647 if ((mode & S_IALLUGO) == 0)
652 ent = proc_create(&parent,name,mode,nlink);
655 ent->proc_fops = &proc_dir_operations;
656 ent->proc_iops = &proc_dir_inode_operations;
658 if (proc_register(parent, ent) < 0) {
666 void free_proc_entry(struct proc_dir_entry *de)
668 unsigned int ino = de->low_ino;
670 if (ino < PROC_DYNAMIC_FIRST)
673 release_inode_number(ino);
675 if (S_ISLNK(de->mode) && de->data)
681 * Remove a /proc entry and free it if it's not currently in use.
682 * If it is in use, we set the 'deleted' flag.
684 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
686 struct proc_dir_entry **p;
687 struct proc_dir_entry *de;
688 const char *fn = name;
691 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
694 for (p = &parent->subdir; *p; p=&(*p)->next ) {
695 if (!proc_match(len, fn, *p))
700 if (S_ISDIR(de->mode))
702 proc_kill_inodes(de);
705 if (!atomic_read(&de->count))
709 printk("remove_proc_entry: %s/%s busy, count=%d\n",
710 parent->name, de->name, atomic_read(&de->count));