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 <asm/uaccess.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;
59 unsigned long long pos;
62 * Gaah, please just use "seq_file" instead. The legacy /proc
63 * interfaces cut loff_t down to off_t for reads, and ignore
64 * the offset entirely for writes..
67 if (pos > MAX_NON_LFS)
69 if (nbytes > MAX_NON_LFS - pos)
70 nbytes = MAX_NON_LFS - pos;
73 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
76 while ((nbytes > 0) && !eof) {
77 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
81 /* Handle old net routines */
82 n = dp->get_info(page, &start, *ppos, count);
85 } else if (dp->read_proc) {
87 * How to be a proc read function
88 * ------------------------------
90 * int f(char *buffer, char **start, off_t offset,
91 * int count, int *peof, void *dat)
93 * Assume that the buffer is "count" bytes in size.
95 * If you know you have supplied all the data you
98 * You have three ways to return data:
99 * 0) Leave *start = NULL. (This is the default.)
100 * Put the data of the requested offset at that
101 * offset within the buffer. Return the number (n)
102 * of bytes there are from the beginning of the
103 * buffer up to the last byte of data. If the
104 * number of supplied bytes (= n - offset) 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 the number of bytes
109 * absorbed. This interface is useful for files
110 * no larger than the buffer.
111 * 1) Set *start = an unsigned long value less than
112 * the buffer address but greater than zero.
113 * Put the data of the requested offset at the
114 * beginning of the buffer. Return the number of
115 * bytes of data placed there. If this number is
116 * greater than zero and you didn't signal eof
117 * and the reader is prepared to take more data
118 * you will be called again with the requested
119 * offset advanced by *start. This interface is
120 * useful when you have a large file consisting
121 * of a series of blocks which you want to count
122 * and return as wholes.
123 * (Hack by Paul.Russell@rustcorp.com.au)
124 * 2) Set *start = an address within the buffer.
125 * Put the data of the requested offset at *start.
126 * Return the number of bytes of data placed there.
127 * If this number is greater than zero and you
128 * didn't signal eof and the reader is prepared to
129 * take more data you will be called again with the
130 * requested offset advanced by the number of bytes
133 n = dp->read_proc(page, &start, *ppos,
134 count, &eof, dp->data);
138 if (n == 0) /* end of file */
140 if (n < 0) { /* error */
149 "proc_file_read: Apparent buffer overflow!\n");
157 start = page + *ppos;
158 } else if (start < page) {
161 "proc_file_read: Apparent buffer overflow!\n");
166 * Don't reduce n because doing so might
167 * cut off part of a data block.
170 "proc_file_read: Read count exceeded\n");
172 } else /* start >= page */ {
173 unsigned long startoff = (unsigned long)(start - page);
174 if (n > (PAGE_SIZE - startoff)) {
176 "proc_file_read: Apparent buffer overflow!\n");
177 n = PAGE_SIZE - startoff;
183 n -= copy_to_user(buf, start < page ? page : start, n);
190 *ppos += start < page ? (unsigned long)start : n;
195 free_page((unsigned long) page);
200 proc_file_write(struct file *file, const char __user *buffer,
201 size_t count, loff_t *ppos)
203 struct inode *inode = file->f_dentry->d_inode;
204 struct proc_dir_entry * dp;
211 /* FIXME: does this routine need ppos? probably... */
212 return dp->write_proc(file, buffer, count, dp->data);
217 proc_file_lseek(struct file *file, loff_t offset, int orig)
219 loff_t retval = -EINVAL;
222 offset += file->f_pos;
225 if (offset < 0 || offset > MAX_NON_LFS)
227 file->f_pos = retval = offset;
232 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
234 struct inode *inode = dentry->d_inode;
235 struct proc_dir_entry *de = PDE(inode);
238 error = inode_change_ok(inode, iattr);
242 error = inode_setattr(inode, iattr);
246 de->uid = inode->i_uid;
247 de->gid = inode->i_gid;
248 de->mode = inode->i_mode;
253 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
256 struct inode *inode = dentry->d_inode;
257 struct proc_dir_entry *de = PROC_I(inode)->pde;
259 inode->i_nlink = de->nlink;
261 generic_fillattr(inode, stat);
265 static struct inode_operations proc_file_inode_operations = {
266 .setattr = proc_notify_change,
270 * This function parses a name such as "tty/driver/serial", and
271 * returns the struct proc_dir_entry for "/proc/tty/driver", and
272 * returns "serial" in residual.
274 static int xlate_proc_name(const char *name,
275 struct proc_dir_entry **ret, const char **residual)
277 const char *cp = name, *next;
278 struct proc_dir_entry *de;
283 next = strchr(cp, '/');
288 for (de = de->subdir; de ; de = de->next) {
289 if (proc_match(len, cp, de))
301 static DEFINE_IDR(proc_inum_idr);
302 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
304 #define PROC_DYNAMIC_FIRST 0xF0000000UL
307 * Return an inode number between PROC_DYNAMIC_FIRST and
308 * 0xffffffff, or zero on failure.
310 static unsigned int get_inode_number(void)
316 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
319 spin_lock(&proc_inum_lock);
320 error = idr_get_new(&proc_inum_idr, NULL, &i);
321 spin_unlock(&proc_inum_lock);
322 if (error == -EAGAIN)
327 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
329 /* inum will never be more than 0xf0ffffff, so no check
336 static void release_inode_number(unsigned int inum)
338 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
340 spin_lock(&proc_inum_lock);
341 idr_remove(&proc_inum_idr, id);
342 spin_unlock(&proc_inum_lock);
345 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
347 nd_set_link(nd, PDE(dentry->d_inode)->data);
351 static struct inode_operations proc_link_inode_operations = {
352 .readlink = generic_readlink,
353 .follow_link = proc_follow_link,
357 * As some entries in /proc are volatile, we want to
358 * get rid of unused dentries. This could be made
359 * smarter: we could keep a "volatile" flag in the
360 * inode to indicate which ones to keep.
362 static int proc_delete_dentry(struct dentry * dentry)
367 static struct dentry_operations proc_dentry_operations =
369 .d_delete = proc_delete_dentry,
373 * Don't create negative dentries here, return -ENOENT by hand
376 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
378 struct inode *inode = NULL;
379 struct proc_dir_entry * de;
385 for (de = de->subdir; de ; de = de->next) {
386 if (de->namelen != dentry->d_name.len)
388 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
389 unsigned int ino = de->low_ino;
392 inode = proc_get_inode(dir->i_sb, ino, de);
400 dentry->d_op = &proc_dentry_operations;
401 d_add(dentry, inode);
404 return ERR_PTR(error);
408 * This returns non-zero if at EOF, so that the /proc
409 * root directory can use this and check if it should
410 * continue with the <pid> entries..
412 * Note that the VFS-layer doesn't care about the return
413 * value of the readdir() call, as long as it's non-negative
416 int proc_readdir(struct file * filp,
417 void * dirent, filldir_t filldir)
419 struct proc_dir_entry * de;
422 struct inode *inode = filp->f_dentry->d_inode;
436 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
442 if (filldir(dirent, "..", 2, i,
443 parent_ino(filp->f_dentry),
464 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
465 de->low_ino, de->mode >> 12) < 0)
472 out: unlock_kernel();
477 * These are the generic /proc directory operations. They
478 * use the in-memory "struct proc_dir_entry" tree to parse
479 * the /proc directory.
481 static struct file_operations proc_dir_operations = {
482 .read = generic_read_dir,
483 .readdir = proc_readdir,
487 * proc directories can do almost nothing..
489 static struct inode_operations proc_dir_inode_operations = {
490 .lookup = proc_lookup,
491 .getattr = proc_getattr,
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_u.fu_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)
569 /* At this point there must not be any '/' characters beyond *fn */
575 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
578 memset(ent, 0, sizeof(struct proc_dir_entry));
579 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
580 ent->name = ((char *) ent) + sizeof(*ent);
588 struct proc_dir_entry *proc_symlink(const char *name,
589 struct proc_dir_entry *parent, const char *dest)
591 struct proc_dir_entry *ent;
593 ent = proc_create(&parent,name,
594 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
597 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
599 strcpy((char*)ent->data,dest);
600 if (proc_register(parent, ent) < 0) {
613 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
614 struct proc_dir_entry *parent)
616 struct proc_dir_entry *ent;
618 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
620 ent->proc_fops = &proc_dir_operations;
621 ent->proc_iops = &proc_dir_inode_operations;
623 if (proc_register(parent, ent) < 0) {
631 struct proc_dir_entry *proc_mkdir(const char *name,
632 struct proc_dir_entry *parent)
634 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
637 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
638 struct proc_dir_entry *parent)
640 struct proc_dir_entry *ent;
644 if ((mode & S_IALLUGO) == 0)
645 mode |= S_IRUGO | S_IXUGO;
648 if ((mode & S_IFMT) == 0)
650 if ((mode & S_IALLUGO) == 0)
655 ent = proc_create(&parent,name,mode,nlink);
658 ent->proc_fops = &proc_dir_operations;
659 ent->proc_iops = &proc_dir_inode_operations;
661 if (proc_register(parent, ent) < 0) {
669 void free_proc_entry(struct proc_dir_entry *de)
671 unsigned int ino = de->low_ino;
673 if (ino < PROC_DYNAMIC_FIRST)
676 release_inode_number(ino);
678 if (S_ISLNK(de->mode) && de->data)
684 * Remove a /proc entry and free it if it's not currently in use.
685 * If it is in use, we set the 'deleted' flag.
687 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
689 struct proc_dir_entry **p;
690 struct proc_dir_entry *de;
691 const char *fn = name;
694 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
697 for (p = &parent->subdir; *p; p=&(*p)->next ) {
698 if (!proc_match(len, fn, *p))
703 if (S_ISDIR(de->mode))
705 proc_kill_inodes(de);
708 if (!atomic_read(&de->count))
712 printk("remove_proc_entry: %s/%s busy, count=%d\n",
713 parent->name, de->name, atomic_read(&de->count));