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;
61 dp = (struct proc_dir_entry *) inode->u.generic_ip;
62 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
65 while ((nbytes > 0) && !eof)
67 count = MIN(PROC_BLOCK_SIZE, nbytes);
68 if ((unsigned)pos > INT_MAX)
74 * Handle backwards compatibility with the old net
77 n = dp->get_info(page, &start, pos, count);
80 } else if (dp->read_proc) {
81 n = dp->read_proc(page, &start, pos,
82 count, &eof, dp->data);
88 * For proc files that are less than 4k
98 break; /* End of file */
105 /* This is a hack to allow mangling of file pos independent
106 * of actual bytes read. Simply place the data at page,
107 * return the bytes, and set `start' to the desired offset
108 * as an unsigned int. - Paul.Russell@rustcorp.com.au
110 n -= copy_to_user(buf, start < page ? page : start, n);
117 pos += start < page ? (long)start : n; /* Move down the file */
122 free_page((unsigned long) page);
128 proc_file_write(struct file * file, const char * buffer,
129 size_t count, loff_t *ppos)
131 struct inode *inode = file->f_dentry->d_inode;
132 struct proc_dir_entry * dp;
134 dp = (struct proc_dir_entry *) inode->u.generic_ip;
139 /* FIXME: does this routine need ppos? probably... */
140 return dp->write_proc(file, buffer, count, dp->data);
145 proc_file_lseek(struct file * file, loff_t offset, int origin)
151 offset += file->f_dentry->d_inode->i_size;
154 offset += file->f_pos;
157 if (offset>=0 && (unsigned long long)offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
158 if (offset != file->f_pos) {
159 file->f_pos = offset;
164 /* RED-PEN user can fake an error here by setting offset to >=-4095 && <0 */
169 * This function parses a name such as "tty/driver/serial", and
170 * returns the struct proc_dir_entry for "/proc/tty/driver", and
171 * returns "serial" in residual.
173 static int xlate_proc_name(const char *name,
174 struct proc_dir_entry **ret, const char **residual)
176 const char *cp = name, *next;
177 struct proc_dir_entry *de;
182 next = strchr(cp, '/');
187 for (de = de->subdir; de ; de = de->next) {
188 if (proc_match(len, cp, de))
200 static unsigned long proc_alloc_map[(PROC_NDYNAMIC + BITS_PER_LONG - 1) / BITS_PER_LONG];
202 spinlock_t proc_alloc_map_lock = SPIN_LOCK_UNLOCKED;
204 static int make_inode_number(void)
207 spin_lock(&proc_alloc_map_lock);
208 i = find_first_zero_bit(proc_alloc_map, PROC_NDYNAMIC);
209 if (i < 0 || i >= PROC_NDYNAMIC) {
213 set_bit(i, proc_alloc_map);
214 i += PROC_DYNAMIC_FIRST;
216 spin_unlock(&proc_alloc_map_lock);
220 static int proc_readlink(struct dentry *dentry, char *buffer, int buflen)
222 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
223 return vfs_readlink(dentry, buffer, buflen, s);
226 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
228 char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
229 return vfs_follow_link(nd, s);
232 static struct inode_operations proc_link_inode_operations = {
233 readlink: proc_readlink,
234 follow_link: proc_follow_link,
238 * As some entries in /proc are volatile, we want to
239 * get rid of unused dentries. This could be made
240 * smarter: we could keep a "volatile" flag in the
241 * inode to indicate which ones to keep.
243 static int proc_delete_dentry(struct dentry * dentry)
248 static struct dentry_operations proc_dentry_operations =
250 d_delete: proc_delete_dentry,
254 * Don't create negative dentries here, return -ENOENT by hand
257 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
260 struct proc_dir_entry * de;
265 de = (struct proc_dir_entry *) dir->u.generic_ip;
267 for (de = de->subdir; de ; de = de->next) {
268 if (!de || !de->low_ino)
270 if (de->namelen != dentry->d_name.len)
272 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
273 int ino = de->low_ino;
275 inode = proc_get_inode(dir->i_sb, ino, de);
282 dentry->d_op = &proc_dentry_operations;
283 d_add(dentry, inode);
286 return ERR_PTR(error);
290 * This returns non-zero if at EOF, so that the /proc
291 * root directory can use this and check if it should
292 * continue with the <pid> entries..
294 * Note that the VFS-layer doesn't care about the return
295 * value of the readdir() call, as long as it's non-negative
298 int proc_readdir(struct file * filp,
299 void * dirent, filldir_t filldir)
301 struct proc_dir_entry * de;
304 struct inode *inode = filp->f_dentry->d_inode;
307 de = (struct proc_dir_entry *) inode->u.generic_ip;
313 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
319 if (filldir(dirent, "..", 2, i,
320 filp->f_dentry->d_parent->d_inode->i_ino,
339 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
340 de->low_ino, de->mode >> 12) < 0)
350 * These are the generic /proc directory operations. They
351 * use the in-memory "struct proc_dir_entry" tree to parse
352 * the /proc directory.
354 static struct file_operations proc_dir_operations = {
355 read: generic_read_dir,
356 readdir: proc_readdir,
360 * proc directories can do almost nothing..
362 static struct inode_operations proc_dir_inode_operations = {
366 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
370 i = make_inode_number();
374 dp->next = dir->subdir;
377 if (S_ISDIR(dp->mode)) {
378 if (dp->proc_iops == NULL) {
379 dp->proc_fops = &proc_dir_operations;
380 dp->proc_iops = &proc_dir_inode_operations;
383 } else if (S_ISLNK(dp->mode)) {
384 if (dp->proc_iops == NULL)
385 dp->proc_iops = &proc_link_inode_operations;
386 } else if (S_ISREG(dp->mode)) {
387 if (dp->proc_fops == NULL)
388 dp->proc_fops = &proc_file_operations;
394 * Kill an inode that got unregistered..
396 static void proc_kill_inodes(struct proc_dir_entry *de)
399 struct super_block *sb = proc_mnt->mnt_sb;
402 * Actually it's a partial revoke().
405 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
406 struct file * filp = list_entry(p, struct file, f_list);
407 struct dentry * dentry = filp->f_dentry;
408 struct inode * inode;
409 struct file_operations *fops;
411 if (dentry->d_op != &proc_dentry_operations)
413 inode = dentry->d_inode;
414 if (inode->u.generic_ip != de)
423 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
428 struct proc_dir_entry *ent = NULL;
429 const char *fn = name;
432 /* make sure name is valid */
433 if (!name || !strlen(name)) goto out;
435 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
439 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
442 memset(ent, 0, sizeof(struct proc_dir_entry));
443 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
444 ent->name = ((char *) ent) + sizeof(*ent);
452 struct proc_dir_entry *proc_symlink(const char *name,
453 struct proc_dir_entry *parent, const char *dest)
455 struct proc_dir_entry *ent;
457 ent = proc_create(&parent,name,
458 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
461 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
463 strcpy((char*)ent->data,dest);
464 if (proc_register(parent, ent) < 0) {
477 struct proc_dir_entry *proc_mknod(const char *name, mode_t mode,
478 struct proc_dir_entry *parent, kdev_t rdev)
480 struct proc_dir_entry *ent;
482 ent = proc_create(&parent,name,mode,1);
485 if (proc_register(parent, ent) < 0) {
493 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
494 struct proc_dir_entry *parent)
496 struct proc_dir_entry *ent;
498 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
500 ent->proc_fops = &proc_dir_operations;
501 ent->proc_iops = &proc_dir_inode_operations;
503 if (proc_register(parent, ent) < 0) {
511 struct proc_dir_entry *proc_mkdir(const char *name,
512 struct proc_dir_entry *parent)
514 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
517 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
518 struct proc_dir_entry *parent)
520 struct proc_dir_entry *ent;
524 if ((mode & S_IALLUGO) == 0)
525 mode |= S_IRUGO | S_IXUGO;
528 if ((mode & S_IFMT) == 0)
530 if ((mode & S_IALLUGO) == 0)
535 ent = proc_create(&parent,name,mode,nlink);
538 ent->proc_fops = &proc_dir_operations;
539 ent->proc_iops = &proc_dir_inode_operations;
541 if (proc_register(parent, ent) < 0) {
549 void free_proc_entry(struct proc_dir_entry *de)
551 int ino = de->low_ino;
553 if (ino < PROC_DYNAMIC_FIRST ||
554 ino >= PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
556 if (S_ISLNK(de->mode) && de->data)
562 * Remove a /proc entry and free it if it's not currently in use.
563 * If it is in use, we set the 'deleted' flag.
565 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
567 struct proc_dir_entry **p;
568 struct proc_dir_entry *de;
569 const char *fn = name;
572 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
575 for (p = &parent->subdir; *p; p=&(*p)->next ) {
576 if (!proc_match(len, fn, *p))
581 if (S_ISDIR(de->mode))
583 clear_bit(de->low_ino - PROC_DYNAMIC_FIRST,
585 proc_kill_inodes(de);
587 if (!atomic_read(&de->count))
591 printk("remove_proc_entry: %s/%s busy, count=%d\n",
592 parent->name, de->name, atomic_read(&de->count));