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 <linux/spinlock.h>
23 #include <linux/completion.h>
24 #include <asm/uaccess.h>
28 static ssize_t proc_file_read(struct file *file, char __user *buf,
29 size_t nbytes, loff_t *ppos);
30 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
31 size_t count, loff_t *ppos);
32 static loff_t proc_file_lseek(struct file *, loff_t, int);
34 DEFINE_SPINLOCK(proc_subdir_lock);
36 static int proc_match(int len, const char *name, struct proc_dir_entry *de)
38 if (de->namelen != len)
40 return !memcmp(name, de->name, len);
43 static const struct file_operations proc_file_operations = {
44 .llseek = proc_file_lseek,
45 .read = proc_file_read,
46 .write = proc_file_write,
49 /* buffer size is one page but our output routines use some slack for overruns */
50 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
53 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
56 struct inode * inode = file->f_path.dentry->d_inode;
62 struct proc_dir_entry * dp;
63 unsigned long long pos;
66 * Gaah, please just use "seq_file" instead. The legacy /proc
67 * interfaces cut loff_t down to off_t for reads, and ignore
68 * the offset entirely for writes..
71 if (pos > MAX_NON_LFS)
73 if (nbytes > MAX_NON_LFS - pos)
74 nbytes = MAX_NON_LFS - pos;
77 if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
80 while ((nbytes > 0) && !eof) {
81 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
85 /* Handle old net routines */
86 n = dp->get_info(page, &start, *ppos, count);
89 } else if (dp->read_proc) {
91 * How to be a proc read function
92 * ------------------------------
94 * int f(char *buffer, char **start, off_t offset,
95 * int count, int *peof, void *dat)
97 * Assume that the buffer is "count" bytes in size.
99 * If you know you have supplied all the data you
102 * You have three ways to return data:
103 * 0) Leave *start = NULL. (This is the default.)
104 * Put the data of the requested offset at that
105 * offset within the buffer. Return the number (n)
106 * of bytes there are from the beginning of the
107 * buffer up to the last byte of data. If the
108 * number of supplied bytes (= n - offset) is
109 * greater than zero and you didn't signal eof
110 * and the reader is prepared to take more data
111 * you will be called again with the requested
112 * offset advanced by the number of bytes
113 * absorbed. This interface is useful for files
114 * no larger than the buffer.
115 * 1) Set *start = an unsigned long value less than
116 * the buffer address but greater than zero.
117 * Put the data of the requested offset at the
118 * beginning of the buffer. Return the number of
119 * bytes of data placed there. If this number is
120 * greater than zero and you didn't signal eof
121 * and the reader is prepared to take more data
122 * you will be called again with the requested
123 * offset advanced by *start. This interface is
124 * useful when you have a large file consisting
125 * of a series of blocks which you want to count
126 * and return as wholes.
127 * (Hack by Paul.Russell@rustcorp.com.au)
128 * 2) Set *start = an address within the buffer.
129 * Put the data of the requested offset at *start.
130 * Return the number of bytes of data placed there.
131 * If this number is greater than zero and you
132 * didn't signal eof and the reader is prepared to
133 * take more data you will be called again with the
134 * requested offset advanced by the number of bytes
137 n = dp->read_proc(page, &start, *ppos,
138 count, &eof, dp->data);
142 if (n == 0) /* end of file */
144 if (n < 0) { /* error */
153 "proc_file_read: Apparent buffer overflow!\n");
161 start = page + *ppos;
162 } else if (start < page) {
165 "proc_file_read: Apparent buffer overflow!\n");
170 * Don't reduce n because doing so might
171 * cut off part of a data block.
174 "proc_file_read: Read count exceeded\n");
176 } else /* start >= page */ {
177 unsigned long startoff = (unsigned long)(start - page);
178 if (n > (PAGE_SIZE - startoff)) {
180 "proc_file_read: Apparent buffer overflow!\n");
181 n = PAGE_SIZE - startoff;
187 n -= copy_to_user(buf, start < page ? page : start, n);
194 *ppos += start < page ? (unsigned long)start : n;
199 free_page((unsigned long) page);
204 proc_file_write(struct file *file, const char __user *buffer,
205 size_t count, loff_t *ppos)
207 struct inode *inode = file->f_path.dentry->d_inode;
208 struct proc_dir_entry * dp;
215 /* FIXME: does this routine need ppos? probably... */
216 return dp->write_proc(file, buffer, count, dp->data);
221 proc_file_lseek(struct file *file, loff_t offset, int orig)
223 loff_t retval = -EINVAL;
226 offset += file->f_pos;
229 if (offset < 0 || offset > MAX_NON_LFS)
231 file->f_pos = retval = offset;
236 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
238 struct inode *inode = dentry->d_inode;
239 struct proc_dir_entry *de = PDE(inode);
242 error = inode_change_ok(inode, iattr);
246 error = inode_setattr(inode, iattr);
250 de->uid = inode->i_uid;
251 de->gid = inode->i_gid;
252 de->mode = inode->i_mode;
257 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
260 struct inode *inode = dentry->d_inode;
261 struct proc_dir_entry *de = PROC_I(inode)->pde;
263 inode->i_nlink = de->nlink;
265 generic_fillattr(inode, stat);
269 static const struct inode_operations proc_file_inode_operations = {
270 .setattr = proc_notify_change,
274 * This function parses a name such as "tty/driver/serial", and
275 * returns the struct proc_dir_entry for "/proc/tty/driver", and
276 * returns "serial" in residual.
278 static int xlate_proc_name(const char *name,
279 struct proc_dir_entry **ret, const char **residual)
281 const char *cp = name, *next;
282 struct proc_dir_entry *de;
286 spin_lock(&proc_subdir_lock);
289 next = strchr(cp, '/');
294 for (de = de->subdir; de ; de = de->next) {
295 if (proc_match(len, cp, de))
307 spin_unlock(&proc_subdir_lock);
311 static DEFINE_IDR(proc_inum_idr);
312 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
314 #define PROC_DYNAMIC_FIRST 0xF0000000UL
317 * Return an inode number between PROC_DYNAMIC_FIRST and
318 * 0xffffffff, or zero on failure.
320 static unsigned int get_inode_number(void)
326 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
329 spin_lock(&proc_inum_lock);
330 error = idr_get_new(&proc_inum_idr, NULL, &i);
331 spin_unlock(&proc_inum_lock);
332 if (error == -EAGAIN)
337 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
339 /* inum will never be more than 0xf0ffffff, so no check
346 static void release_inode_number(unsigned int inum)
348 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
350 spin_lock(&proc_inum_lock);
351 idr_remove(&proc_inum_idr, id);
352 spin_unlock(&proc_inum_lock);
355 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
357 nd_set_link(nd, PDE(dentry->d_inode)->data);
361 static const struct inode_operations proc_link_inode_operations = {
362 .readlink = generic_readlink,
363 .follow_link = proc_follow_link,
367 * As some entries in /proc are volatile, we want to
368 * get rid of unused dentries. This could be made
369 * smarter: we could keep a "volatile" flag in the
370 * inode to indicate which ones to keep.
372 static int proc_delete_dentry(struct dentry * dentry)
377 static struct dentry_operations proc_dentry_operations =
379 .d_delete = proc_delete_dentry,
383 * Don't create negative dentries here, return -ENOENT by hand
386 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
388 struct inode *inode = NULL;
389 struct proc_dir_entry * de;
393 spin_lock(&proc_subdir_lock);
396 for (de = de->subdir; de ; de = de->next) {
397 if (de->namelen != dentry->d_name.len)
399 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
403 de = de->shadow_proc(current, de);
406 spin_unlock(&proc_subdir_lock);
408 inode = proc_get_inode(dir->i_sb, ino, de);
409 spin_lock(&proc_subdir_lock);
414 spin_unlock(&proc_subdir_lock);
418 dentry->d_op = &proc_dentry_operations;
419 d_add(dentry, inode);
423 return ERR_PTR(error);
427 * This returns non-zero if at EOF, so that the /proc
428 * root directory can use this and check if it should
429 * continue with the <pid> entries..
431 * Note that the VFS-layer doesn't care about the return
432 * value of the readdir() call, as long as it's non-negative
435 int proc_readdir(struct file * filp,
436 void * dirent, filldir_t filldir)
438 struct proc_dir_entry * de;
441 struct inode *inode = filp->f_path.dentry->d_inode;
455 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
461 if (filldir(dirent, "..", 2, i,
462 parent_ino(filp->f_path.dentry),
469 spin_lock(&proc_subdir_lock);
475 spin_unlock(&proc_subdir_lock);
485 struct proc_dir_entry *next;
487 /* filldir passes info to user space */
489 spin_unlock(&proc_subdir_lock);
490 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
491 de->low_ino, de->mode >> 12) < 0) {
495 spin_lock(&proc_subdir_lock);
501 spin_unlock(&proc_subdir_lock);
504 out: unlock_kernel();
509 * These are the generic /proc directory operations. They
510 * use the in-memory "struct proc_dir_entry" tree to parse
511 * the /proc directory.
513 static const struct file_operations proc_dir_operations = {
514 .read = generic_read_dir,
515 .readdir = proc_readdir,
519 * proc directories can do almost nothing..
521 static const struct inode_operations proc_dir_inode_operations = {
522 .lookup = proc_lookup,
523 .getattr = proc_getattr,
524 .setattr = proc_notify_change,
527 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
531 i = get_inode_number();
536 if (S_ISDIR(dp->mode)) {
537 if (dp->proc_iops == NULL) {
538 dp->proc_fops = &proc_dir_operations;
539 dp->proc_iops = &proc_dir_inode_operations;
542 } else if (S_ISLNK(dp->mode)) {
543 if (dp->proc_iops == NULL)
544 dp->proc_iops = &proc_link_inode_operations;
545 } else if (S_ISREG(dp->mode)) {
546 if (dp->proc_fops == NULL)
547 dp->proc_fops = &proc_file_operations;
548 if (dp->proc_iops == NULL)
549 dp->proc_iops = &proc_file_inode_operations;
552 spin_lock(&proc_subdir_lock);
553 dp->next = dir->subdir;
556 spin_unlock(&proc_subdir_lock);
561 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
566 struct proc_dir_entry *ent = NULL;
567 const char *fn = name;
570 /* make sure name is valid */
571 if (!name || !strlen(name)) goto out;
573 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
576 /* At this point there must not be any '/' characters beyond *fn */
582 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
585 memset(ent, 0, sizeof(struct proc_dir_entry));
586 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
587 ent->name = ((char *) ent) + sizeof(*ent);
591 atomic_set(&ent->count, 1);
593 spin_lock_init(&ent->pde_unload_lock);
594 ent->pde_unload_completion = NULL;
599 struct proc_dir_entry *proc_symlink(const char *name,
600 struct proc_dir_entry *parent, const char *dest)
602 struct proc_dir_entry *ent;
604 ent = proc_create(&parent,name,
605 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
608 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
610 strcpy((char*)ent->data,dest);
611 if (proc_register(parent, ent) < 0) {
624 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
625 struct proc_dir_entry *parent)
627 struct proc_dir_entry *ent;
629 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
631 if (proc_register(parent, ent) < 0) {
639 struct proc_dir_entry *proc_mkdir(const char *name,
640 struct proc_dir_entry *parent)
642 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
645 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
646 struct proc_dir_entry *parent)
648 struct proc_dir_entry *ent;
652 if ((mode & S_IALLUGO) == 0)
653 mode |= S_IRUGO | S_IXUGO;
656 if ((mode & S_IFMT) == 0)
658 if ((mode & S_IALLUGO) == 0)
663 ent = proc_create(&parent,name,mode,nlink);
665 if (proc_register(parent, ent) < 0) {
673 void free_proc_entry(struct proc_dir_entry *de)
675 unsigned int ino = de->low_ino;
677 if (ino < PROC_DYNAMIC_FIRST)
680 release_inode_number(ino);
682 if (S_ISLNK(de->mode) && de->data)
688 * Remove a /proc entry and free it if it's not currently in use.
690 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
692 struct proc_dir_entry **p;
693 struct proc_dir_entry *de;
694 const char *fn = name;
697 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
701 spin_lock(&proc_subdir_lock);
702 for (p = &parent->subdir; *p; p=&(*p)->next ) {
703 if (!proc_match(len, fn, *p))
709 spin_lock(&de->pde_unload_lock);
711 * Stop accepting new callers into module. If you're
712 * dynamically allocating ->proc_fops, save a pointer somewhere.
714 de->proc_fops = NULL;
715 /* Wait until all existing callers into module are done. */
716 if (de->pde_users > 0) {
717 DECLARE_COMPLETION_ONSTACK(c);
719 if (!de->pde_unload_completion)
720 de->pde_unload_completion = &c;
722 spin_unlock(&de->pde_unload_lock);
723 spin_unlock(&proc_subdir_lock);
725 wait_for_completion(de->pde_unload_completion);
727 spin_lock(&proc_subdir_lock);
728 goto continue_removing;
730 spin_unlock(&de->pde_unload_lock);
733 if (S_ISDIR(de->mode))
737 if (atomic_dec_and_test(&de->count))
741 spin_unlock(&proc_subdir_lock);