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 <asm/uaccess.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 #define __NO_VERSION__
18 #include <linux/module.h>
19 #include <asm/bitops.h>
21 static ssize_t proc_file_read(struct file * file, char * buf,
22 size_t nbytes, loff_t *ppos);
23 static ssize_t proc_file_write(struct file * file, const char * buffer,
24 size_t count, loff_t *ppos);
25 static loff_t proc_file_lseek(struct file *, loff_t, int);
27 int proc_match(int len, const char *name,struct proc_dir_entry * de)
29 if (!de || !de->low_ino)
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,
39 write: proc_file_write,
43 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
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 * buf, size_t nbytes, loff_t *ppos)
52 struct inode * inode = file->f_dentry->d_inode;
58 struct proc_dir_entry * dp;
60 dp = (struct proc_dir_entry *) inode->u.generic_ip;
61 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
64 while ((nbytes > 0) && !eof)
66 count = MIN(PROC_BLOCK_SIZE, nbytes);
71 * Handle backwards compatibility with the old net
74 n = dp->get_info(page, &start, *ppos, count);
77 } else if (dp->read_proc) {
78 n = dp->read_proc(page, &start, *ppos,
79 count, &eof, dp->data);
85 * For proc files that are less than 4k
95 break; /* End of file */
102 /* This is a hack to allow mangling of file pos independent
103 * of actual bytes read. Simply place the data at page,
104 * return the bytes, and set `start' to the desired offset
105 * as an unsigned int. - Paul.Russell@rustcorp.com.au
107 n -= copy_to_user(buf, start < page ? page : start, n);
114 *ppos += start < page ? (long)start : n; /* Move down the file */
119 free_page((unsigned long) page);
124 proc_file_write(struct file * file, const char * buffer,
125 size_t count, loff_t *ppos)
127 struct inode *inode = file->f_dentry->d_inode;
128 struct proc_dir_entry * dp;
130 dp = (struct proc_dir_entry *) inode->u.generic_ip;
135 /* FIXME: does this routine need ppos? probably... */
136 return dp->write_proc(file, buffer, count, dp->data);
141 proc_file_lseek(struct file * file, loff_t offset, int origin)
147 offset += file->f_dentry->d_inode->i_size;
150 offset += file->f_pos;
153 if (offset>=0 && offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
154 if (offset != file->f_pos) {
155 file->f_pos = offset;
164 * This function parses a name such as "tty/driver/serial", and
165 * returns the struct proc_dir_entry for "/proc/tty/driver", and
166 * returns "serial" in residual.
168 static int xlate_proc_name(const char *name,
169 struct proc_dir_entry **ret, const char **residual)
171 const char *cp = name, *next;
172 struct proc_dir_entry *de;
177 next = strchr(cp, '/');
182 for (de = de->subdir; de ; de = de->next) {
183 if (proc_match(len, cp, de))
195 static unsigned long proc_alloc_map[(PROC_NDYNAMIC + BITS_PER_LONG - 1) / BITS_PER_LONG];
197 spinlock_t proc_alloc_map_lock = SPIN_LOCK_UNLOCKED;
199 static int make_inode_number(void)
202 spin_lock(&proc_alloc_map_lock);
203 i = find_first_zero_bit(proc_alloc_map, PROC_NDYNAMIC);
204 if (i < 0 || i >= PROC_NDYNAMIC) {
208 set_bit(i, proc_alloc_map);
209 i += PROC_DYNAMIC_FIRST;
211 spin_unlock(&proc_alloc_map_lock);
215 static int proc_readlink(struct dentry *dentry, char *buffer, int buflen)
217 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
218 return vfs_readlink(dentry, buffer, buflen, s);
221 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
223 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
224 return vfs_follow_link(nd, s);
227 static struct inode_operations proc_link_inode_operations = {
228 readlink: proc_readlink,
229 follow_link: proc_follow_link,
233 * As some entries in /proc are volatile, we want to
234 * get rid of unused dentries. This could be made
235 * smarter: we could keep a "volatile" flag in the
236 * inode to indicate which ones to keep.
238 static int proc_delete_dentry(struct dentry * dentry)
243 static struct dentry_operations proc_dentry_operations =
245 d_delete: proc_delete_dentry,
249 * Don't create negative dentries here, return -ENOENT by hand
252 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
255 struct proc_dir_entry * de;
260 de = (struct proc_dir_entry *) dir->u.generic_ip;
262 for (de = de->subdir; de ; de = de->next) {
263 if (!de || !de->low_ino)
265 if (de->namelen != dentry->d_name.len)
267 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
268 int ino = de->low_ino;
270 inode = proc_get_inode(dir->i_sb, ino, de);
277 dentry->d_op = &proc_dentry_operations;
278 d_add(dentry, inode);
281 return ERR_PTR(error);
285 * This returns non-zero if at EOF, so that the /proc
286 * root directory can use this and check if it should
287 * continue with the <pid> entries..
289 * Note that the VFS-layer doesn't care about the return
290 * value of the readdir() call, as long as it's non-negative
293 int proc_readdir(struct file * filp,
294 void * dirent, filldir_t filldir)
296 struct proc_dir_entry * de;
299 struct inode *inode = filp->f_dentry->d_inode;
302 de = (struct proc_dir_entry *) inode->u.generic_ip;
308 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
314 if (filldir(dirent, "..", 2, i,
315 filp->f_dentry->d_parent->d_inode->i_ino,
334 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
335 de->low_ino, de->mode >> 12) < 0)
345 * These are the generic /proc directory operations. They
346 * use the in-memory "struct proc_dir_entry" tree to parse
347 * the /proc directory.
349 static struct file_operations proc_dir_operations = {
350 read: generic_read_dir,
351 readdir: proc_readdir,
355 * proc directories can do almost nothing..
357 static struct inode_operations proc_dir_inode_operations = {
361 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
365 i = make_inode_number();
369 dp->next = dir->subdir;
372 if (S_ISDIR(dp->mode)) {
373 if (dp->proc_iops == NULL) {
374 dp->proc_fops = &proc_dir_operations;
375 dp->proc_iops = &proc_dir_inode_operations;
378 } else if (S_ISLNK(dp->mode)) {
379 if (dp->proc_iops == NULL)
380 dp->proc_iops = &proc_link_inode_operations;
381 } else if (S_ISREG(dp->mode)) {
382 if (dp->proc_fops == NULL)
383 dp->proc_fops = &proc_file_operations;
389 * Kill an inode that got unregistered..
391 static void proc_kill_inodes(struct proc_dir_entry *de)
394 struct super_block *sb = proc_mnt->mnt_sb;
397 * Actually it's a partial revoke().
400 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
401 struct file * filp = list_entry(p, struct file, f_list);
402 struct dentry * dentry = filp->f_dentry;
403 struct inode * inode;
404 struct file_operations *fops;
406 if (dentry->d_op != &proc_dentry_operations)
408 inode = dentry->d_inode;
409 if (inode->u.generic_ip != de)
418 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
423 struct proc_dir_entry *ent = NULL;
424 const char *fn = name;
427 /* make sure name is valid */
428 if (!name || !strlen(name)) goto out;
430 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
434 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
437 memset(ent, 0, sizeof(struct proc_dir_entry));
438 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
439 ent->name = ((char *) ent) + sizeof(*ent);
447 struct proc_dir_entry *proc_symlink(const char *name,
448 struct proc_dir_entry *parent, const char *dest)
450 struct proc_dir_entry *ent;
452 ent = proc_create(&parent,name,
453 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
456 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
458 strcpy((char*)ent->data,dest);
459 if (proc_register(parent, ent) < 0) {
472 struct proc_dir_entry *proc_mknod(const char *name, mode_t mode,
473 struct proc_dir_entry *parent, kdev_t rdev)
475 struct proc_dir_entry *ent;
477 ent = proc_create(&parent,name,mode,1);
480 if (proc_register(parent, ent) < 0) {
488 struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)
490 struct proc_dir_entry *ent;
492 ent = proc_create(&parent,name,
493 (S_IFDIR | S_IRUGO | S_IXUGO),2);
495 ent->proc_fops = &proc_dir_operations;
496 ent->proc_iops = &proc_dir_inode_operations;
498 if (proc_register(parent, ent) < 0) {
506 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
507 struct proc_dir_entry *parent)
509 struct proc_dir_entry *ent;
513 if ((mode & S_IALLUGO) == 0)
514 mode |= S_IRUGO | S_IXUGO;
517 if ((mode & S_IFMT) == 0)
519 if ((mode & S_IALLUGO) == 0)
524 ent = proc_create(&parent,name,mode,nlink);
527 ent->proc_fops = &proc_dir_operations;
528 ent->proc_iops = &proc_dir_inode_operations;
530 if (proc_register(parent, ent) < 0) {
538 void free_proc_entry(struct proc_dir_entry *de)
540 int ino = de->low_ino;
542 if (ino < PROC_DYNAMIC_FIRST ||
543 ino >= PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
545 if (S_ISLNK(de->mode) && de->data)
551 * Remove a /proc entry and free it if it's not currently in use.
552 * If it is in use, we set the 'deleted' flag.
554 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
556 struct proc_dir_entry **p;
557 struct proc_dir_entry *de;
558 const char *fn = name;
561 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
564 for (p = &parent->subdir; *p; p=&(*p)->next ) {
565 if (!proc_match(len, fn, *p))
570 if (S_ISDIR(de->mode))
572 clear_bit(de->low_ino - PROC_DYNAMIC_FIRST,
574 proc_kill_inodes(de);
576 if (!atomic_read(&de->count))
580 printk("remove_proc_entry: %s/%s busy, count=%d\n",
581 parent->name, de->name, atomic_read(&de->count));