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/vinline.h>
21 #include <linux/vserver/inode.h>
22 #include <asm/uaccess.h>
23 #include <asm/bitops.h>
25 static ssize_t proc_file_read(struct file *file, char __user *buf,
26 size_t nbytes, loff_t *ppos);
27 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
28 size_t count, loff_t *ppos);
29 static loff_t proc_file_lseek(struct file *, loff_t, int);
31 int proc_match(int len, const char *name, struct proc_dir_entry *de)
33 if (de->namelen != len)
35 return !memcmp(name, de->name, len);
38 static struct file_operations proc_file_operations = {
39 .llseek = proc_file_lseek,
40 .read = proc_file_read,
41 .write = proc_file_write,
44 /* buffer size is one page but our output routines use some slack for overruns */
45 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
48 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
51 struct inode * inode = file->f_dentry->d_inode;
57 struct proc_dir_entry * dp;
60 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
63 while ((nbytes > 0) && !eof) {
64 count = min_t(ssize_t, PROC_BLOCK_SIZE, nbytes);
68 /* Handle old net routines */
69 n = dp->get_info(page, &start, *ppos, count);
72 } else if (dp->read_proc) {
74 * How to be a proc read function
75 * ------------------------------
77 * int f(char *buffer, char **start, off_t offset,
78 * int count, int *peof, void *dat)
80 * Assume that the buffer is "count" bytes in size.
82 * If you know you have supplied all the data you
85 * You have three ways to return data:
86 * 0) Leave *start = NULL. (This is the default.)
87 * Put the data of the requested offset at that
88 * offset within the buffer. Return the number (n)
89 * of bytes there are from the beginning of the
90 * buffer up to the last byte of data. If the
91 * number of supplied bytes (= n - offset) is
92 * greater than zero and you didn't signal eof
93 * and the reader is prepared to take more data
94 * you will be called again with the requested
95 * offset advanced by the number of bytes
96 * absorbed. This interface is useful for files
97 * no larger than the buffer.
98 * 1) Set *start = an unsigned long value less than
99 * the buffer address but greater than zero.
100 * Put the data of the requested offset at the
101 * beginning of the buffer. Return the number of
102 * bytes of data placed there. If this number is
103 * greater than zero and you didn't signal eof
104 * and the reader is prepared to take more data
105 * you will be called again with the requested
106 * offset advanced by *start. This interface is
107 * useful when you have a large file consisting
108 * of a series of blocks which you want to count
109 * and return as wholes.
110 * (Hack by Paul.Russell@rustcorp.com.au)
111 * 2) Set *start = an address within the buffer.
112 * Put the data of the requested offset at *start.
113 * Return the number of bytes of data placed there.
114 * If this number is greater than zero and you
115 * didn't signal eof and the reader is prepared to
116 * take more data you will be called again with the
117 * requested offset advanced by the number of bytes
120 n = dp->read_proc(page, &start, *ppos,
121 count, &eof, dp->data);
125 if (n == 0) /* end of file */
127 if (n < 0) { /* error */
136 "proc_file_read: Apparent buffer overflow!\n");
144 start = page + *ppos;
145 } else if (start < page) {
148 "proc_file_read: Apparent buffer overflow!\n");
153 * Don't reduce n because doing so might
154 * cut off part of a data block.
157 "proc_file_read: Read count exceeded\n");
159 } else /* start >= page */ {
160 unsigned long startoff = (unsigned long)(start - page);
161 if (n > (PAGE_SIZE - startoff)) {
163 "proc_file_read: Apparent buffer overflow!\n");
164 n = PAGE_SIZE - startoff;
170 n -= copy_to_user(buf, start < page ? page : start, n);
177 *ppos += start < page ? (unsigned long)start : n;
182 free_page((unsigned long) page);
187 proc_file_write(struct file *file, const char __user *buffer,
188 size_t count, loff_t *ppos)
190 struct inode *inode = file->f_dentry->d_inode;
191 struct proc_dir_entry * dp;
198 /* FIXME: does this routine need ppos? probably... */
199 return dp->write_proc(file, buffer, count, dp->data);
204 proc_file_lseek(struct file *file, loff_t offset, int orig)
212 file->f_pos = offset;
216 if (offset + file->f_pos < 0)
218 file->f_pos += offset;
232 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
234 struct inode *inode = dentry->d_inode;
235 int error = inode_setattr(inode, iattr);
237 struct proc_dir_entry *de = PDE(inode);
238 de->uid = inode->i_uid;
239 de->gid = inode->i_gid;
240 de->mode = inode->i_mode;
246 static struct inode_operations proc_file_inode_operations = {
247 .setattr = proc_notify_change,
251 * This function parses a name such as "tty/driver/serial", and
252 * returns the struct proc_dir_entry for "/proc/tty/driver", and
253 * returns "serial" in residual.
255 static int xlate_proc_name(const char *name,
256 struct proc_dir_entry **ret, const char **residual)
258 const char *cp = name, *next;
259 struct proc_dir_entry *de;
264 next = strchr(cp, '/');
269 for (de = de->subdir; de ; de = de->next) {
270 if (proc_match(len, cp, de))
282 static DEFINE_IDR(proc_inum_idr);
283 static spinlock_t proc_inum_lock = SPIN_LOCK_UNLOCKED; /* protects the above */
285 #define PROC_DYNAMIC_FIRST 0xF0000000UL
288 * Return an inode number between PROC_DYNAMIC_FIRST and
289 * 0xffffffff, or zero on failure.
291 static unsigned int get_inode_number(void)
293 unsigned int i, inum = 0;
296 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
299 spin_lock(&proc_inum_lock);
300 i = idr_get_new(&proc_inum_idr, NULL);
301 spin_unlock(&proc_inum_lock);
306 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
308 /* inum will never be more than 0xf0ffffff, so no check
315 static void release_inode_number(unsigned int inum)
317 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
319 spin_lock(&proc_inum_lock);
320 idr_remove(&proc_inum_idr, id);
321 spin_unlock(&proc_inum_lock);
324 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
326 nd_set_link(nd, PDE(dentry->d_inode)->data);
330 static struct inode_operations proc_link_inode_operations = {
331 .readlink = generic_readlink,
332 .follow_link = proc_follow_link,
336 * As some entries in /proc are volatile, we want to
337 * get rid of unused dentries. This could be made
338 * smarter: we could keep a "volatile" flag in the
339 * inode to indicate which ones to keep.
341 static int proc_delete_dentry(struct dentry * dentry)
346 static int proc_revalidate_dentry(struct dentry *de, struct nameidata *nd)
348 /* maybe add a check if it's really necessary? */
352 static struct dentry_operations proc_dentry_operations =
354 .d_revalidate = proc_revalidate_dentry,
355 .d_delete = proc_delete_dentry,
359 * Don't create negative dentries here, return -ENOENT by hand
362 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
364 struct inode *inode = NULL;
365 struct proc_dir_entry * de;
371 for (de = de->subdir; de ; de = de->next) {
372 if (de->namelen != dentry->d_name.len)
374 if (!vx_hide_check(0, de->vx_flags))
376 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
377 unsigned int ino = de->low_ino;
380 inode = proc_get_inode(dir->i_sb, ino, de);
388 dentry->d_op = &proc_dentry_operations;
389 d_add(dentry, inode);
392 return ERR_PTR(error);
396 * This returns non-zero if at EOF, so that the /proc
397 * root directory can use this and check if it should
398 * continue with the <pid> entries..
400 * Note that the VFS-layer doesn't care about the return
401 * value of the readdir() call, as long as it's non-negative
404 int proc_readdir(struct file * filp,
405 void * dirent, filldir_t filldir)
407 struct proc_dir_entry * de;
410 struct inode *inode = filp->f_dentry->d_inode;
424 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
430 if (filldir(dirent, "..", 2, i,
431 parent_ino(filp->f_dentry),
452 if (!vx_hide_check(0, de->vx_flags))
454 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
455 de->low_ino, de->mode >> 12) < 0)
463 out: unlock_kernel();
468 * These are the generic /proc directory operations. They
469 * use the in-memory "struct proc_dir_entry" tree to parse
470 * the /proc directory.
472 static struct file_operations proc_dir_operations = {
473 .read = generic_read_dir,
474 .readdir = proc_readdir,
478 * proc directories can do almost nothing..
480 static struct inode_operations proc_dir_inode_operations = {
481 .lookup = proc_lookup,
482 .setattr = proc_notify_change,
485 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
489 i = get_inode_number();
493 dp->next = dir->subdir;
496 if (S_ISDIR(dp->mode)) {
497 if (dp->proc_iops == NULL) {
498 dp->proc_fops = &proc_dir_operations;
499 dp->proc_iops = &proc_dir_inode_operations;
502 } else if (S_ISLNK(dp->mode)) {
503 if (dp->proc_iops == NULL)
504 dp->proc_iops = &proc_link_inode_operations;
505 } else if (S_ISREG(dp->mode)) {
506 if (dp->proc_fops == NULL)
507 dp->proc_fops = &proc_file_operations;
508 if (dp->proc_iops == NULL)
509 dp->proc_iops = &proc_file_inode_operations;
515 * Kill an inode that got unregistered..
517 static void proc_kill_inodes(struct proc_dir_entry *de)
520 struct super_block *sb = proc_mnt->mnt_sb;
523 * Actually it's a partial revoke().
526 list_for_each(p, &sb->s_files) {
527 struct file * filp = list_entry(p, struct file, f_list);
528 struct dentry * dentry = filp->f_dentry;
529 struct inode * inode;
530 struct file_operations *fops;
532 if (dentry->d_op != &proc_dentry_operations)
534 inode = dentry->d_inode;
535 if (PDE(inode) != de)
544 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
549 struct proc_dir_entry *ent = NULL;
550 const char *fn = name;
553 /* make sure name is valid */
554 if (!name || !strlen(name)) goto out;
556 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
560 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
563 memset(ent, 0, sizeof(struct proc_dir_entry));
564 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
565 ent->name = ((char *) ent) + sizeof(*ent);
569 ent->vx_flags = IATTR_PROC_DEFAULT;
574 struct proc_dir_entry *proc_symlink(const char *name,
575 struct proc_dir_entry *parent, const char *dest)
577 struct proc_dir_entry *ent;
579 ent = proc_create(&parent,name,
580 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
583 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
585 strcpy((char*)ent->data,dest);
586 if (proc_register(parent, ent) < 0) {
591 ent->vx_flags = IATTR_PROC_SYMLINK;
600 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
601 struct proc_dir_entry *parent)
603 struct proc_dir_entry *ent;
605 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
607 ent->proc_fops = &proc_dir_operations;
608 ent->proc_iops = &proc_dir_inode_operations;
610 if (proc_register(parent, ent) < 0) {
618 struct proc_dir_entry *proc_mkdir(const char *name,
619 struct proc_dir_entry *parent)
621 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
624 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
625 struct proc_dir_entry *parent)
627 struct proc_dir_entry *ent;
631 if ((mode & S_IALLUGO) == 0)
632 mode |= S_IRUGO | S_IXUGO;
635 if ((mode & S_IFMT) == 0)
637 if ((mode & S_IALLUGO) == 0)
642 ent = proc_create(&parent,name,mode,nlink);
645 ent->proc_fops = &proc_dir_operations;
646 ent->proc_iops = &proc_dir_inode_operations;
648 if (proc_register(parent, ent) < 0) {
656 void free_proc_entry(struct proc_dir_entry *de)
658 unsigned int ino = de->low_ino;
660 if (ino < PROC_DYNAMIC_FIRST)
663 release_inode_number(ino);
665 if (S_ISLNK(de->mode) && de->data)
671 * Remove a /proc entry and free it if it's not currently in use.
672 * If it is in use, we set the 'deleted' flag.
674 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
676 struct proc_dir_entry **p;
677 struct proc_dir_entry *de;
678 const char *fn = name;
681 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
684 for (p = &parent->subdir; *p; p=&(*p)->next ) {
685 if (!proc_match(len, fn, *p))
690 if (S_ISDIR(de->mode))
692 proc_kill_inodes(de);
695 if (!atomic_read(&de->count))
699 printk("remove_proc_entry: %s/%s busy, count=%d\n",
700 parent->name, de->name, atomic_read(&de->count));