[PATCH] mm: pfn_to_pgdat not used in common code
[wrt350n-kernel.git] / fs / proc / generic.c
blob72b431d0a0a4124d0dbbc52fae842d622bffc3fc
1 /*
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.
6 *
7 * Copyright (C) 1991, 1992 Linus Torvalds.
8 * Copyright (C) 1997 Theodore Ts'o
9 */
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>
24 static ssize_t proc_file_read(struct file *file, char __user *buf,
25 size_t nbytes, loff_t *ppos);
26 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
27 size_t count, loff_t *ppos);
28 static loff_t proc_file_lseek(struct file *, loff_t, int);
30 int proc_match(int len, const char *name, struct proc_dir_entry *de)
32 if (de->namelen != len)
33 return 0;
34 return !memcmp(name, de->name, len);
37 static struct file_operations proc_file_operations = {
38 .llseek = proc_file_lseek,
39 .read = proc_file_read,
40 .write = proc_file_write,
43 /* buffer size is one page but our output routines use some slack for overruns */
44 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
46 static ssize_t
47 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
48 loff_t *ppos)
50 struct inode * inode = file->f_dentry->d_inode;
51 char *page;
52 ssize_t retval=0;
53 int eof=0;
54 ssize_t n, count;
55 char *start;
56 struct proc_dir_entry * dp;
57 unsigned long long pos;
60 * Gaah, please just use "seq_file" instead. The legacy /proc
61 * interfaces cut loff_t down to off_t for reads, and ignore
62 * the offset entirely for writes..
64 pos = *ppos;
65 if (pos > MAX_NON_LFS)
66 return 0;
67 if (nbytes > MAX_NON_LFS - pos)
68 nbytes = MAX_NON_LFS - pos;
70 dp = PDE(inode);
71 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
72 return -ENOMEM;
74 while ((nbytes > 0) && !eof) {
75 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
77 start = NULL;
78 if (dp->get_info) {
79 /* Handle old net routines */
80 n = dp->get_info(page, &start, *ppos, count);
81 if (n < count)
82 eof = 1;
83 } else if (dp->read_proc) {
85 * How to be a proc read function
86 * ------------------------------
87 * Prototype:
88 * int f(char *buffer, char **start, off_t offset,
89 * int count, int *peof, void *dat)
91 * Assume that the buffer is "count" bytes in size.
93 * If you know you have supplied all the data you
94 * have, set *peof.
96 * You have three ways to return data:
97 * 0) Leave *start = NULL. (This is the default.)
98 * Put the data of the requested offset at that
99 * offset within the buffer. Return the number (n)
100 * of bytes there are from the beginning of the
101 * buffer up to the last byte of data. If the
102 * number of supplied bytes (= n - offset) 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 the number of bytes
107 * absorbed. This interface is useful for files
108 * no larger than the buffer.
109 * 1) Set *start = an unsigned long value less than
110 * the buffer address but greater than zero.
111 * Put the data of the requested offset at the
112 * beginning of the buffer. Return the number of
113 * bytes of data placed there. If this number is
114 * greater than zero and you didn't signal eof
115 * and the reader is prepared to take more data
116 * you will be called again with the requested
117 * offset advanced by *start. This interface is
118 * useful when you have a large file consisting
119 * of a series of blocks which you want to count
120 * and return as wholes.
121 * (Hack by Paul.Russell@rustcorp.com.au)
122 * 2) Set *start = an address within the buffer.
123 * Put the data of the requested offset at *start.
124 * Return the number of bytes of data placed there.
125 * If this number is greater than zero and you
126 * didn't signal eof and the reader is prepared to
127 * take more data you will be called again with the
128 * requested offset advanced by the number of bytes
129 * absorbed.
131 n = dp->read_proc(page, &start, *ppos,
132 count, &eof, dp->data);
133 } else
134 break;
136 if (n == 0) /* end of file */
137 break;
138 if (n < 0) { /* error */
139 if (retval == 0)
140 retval = n;
141 break;
144 if (start == NULL) {
145 if (n > PAGE_SIZE) {
146 printk(KERN_ERR
147 "proc_file_read: Apparent buffer overflow!\n");
148 n = PAGE_SIZE;
150 n -= *ppos;
151 if (n <= 0)
152 break;
153 if (n > count)
154 n = count;
155 start = page + *ppos;
156 } else if (start < page) {
157 if (n > PAGE_SIZE) {
158 printk(KERN_ERR
159 "proc_file_read: Apparent buffer overflow!\n");
160 n = PAGE_SIZE;
162 if (n > count) {
164 * Don't reduce n because doing so might
165 * cut off part of a data block.
167 printk(KERN_WARNING
168 "proc_file_read: Read count exceeded\n");
170 } else /* start >= page */ {
171 unsigned long startoff = (unsigned long)(start - page);
172 if (n > (PAGE_SIZE - startoff)) {
173 printk(KERN_ERR
174 "proc_file_read: Apparent buffer overflow!\n");
175 n = PAGE_SIZE - startoff;
177 if (n > count)
178 n = count;
181 n -= copy_to_user(buf, start < page ? page : start, n);
182 if (n == 0) {
183 if (retval == 0)
184 retval = -EFAULT;
185 break;
188 *ppos += start < page ? (unsigned long)start : n;
189 nbytes -= n;
190 buf += n;
191 retval += n;
193 free_page((unsigned long) page);
194 return retval;
197 static ssize_t
198 proc_file_write(struct file *file, const char __user *buffer,
199 size_t count, loff_t *ppos)
201 struct inode *inode = file->f_dentry->d_inode;
202 struct proc_dir_entry * dp;
204 dp = PDE(inode);
206 if (!dp->write_proc)
207 return -EIO;
209 /* FIXME: does this routine need ppos? probably... */
210 return dp->write_proc(file, buffer, count, dp->data);
214 static loff_t
215 proc_file_lseek(struct file *file, loff_t offset, int orig)
217 loff_t retval = -EINVAL;
218 switch (orig) {
219 case 1:
220 offset += file->f_pos;
221 /* fallthrough */
222 case 0:
223 if (offset < 0 || offset > MAX_NON_LFS)
224 break;
225 file->f_pos = retval = offset;
227 return retval;
230 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
232 struct inode *inode = dentry->d_inode;
233 struct proc_dir_entry *de = PDE(inode);
234 int error;
236 error = inode_change_ok(inode, iattr);
237 if (error)
238 goto out;
240 error = inode_setattr(inode, iattr);
241 if (error)
242 goto out;
244 de->uid = inode->i_uid;
245 de->gid = inode->i_gid;
246 de->mode = inode->i_mode;
247 out:
248 return error;
251 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
252 struct kstat *stat)
254 struct inode *inode = dentry->d_inode;
255 struct proc_dir_entry *de = PROC_I(inode)->pde;
256 if (de && de->nlink)
257 inode->i_nlink = de->nlink;
259 generic_fillattr(inode, stat);
260 return 0;
263 static struct inode_operations proc_file_inode_operations = {
264 .setattr = proc_notify_change,
268 * This function parses a name such as "tty/driver/serial", and
269 * returns the struct proc_dir_entry for "/proc/tty/driver", and
270 * returns "serial" in residual.
272 static int xlate_proc_name(const char *name,
273 struct proc_dir_entry **ret, const char **residual)
275 const char *cp = name, *next;
276 struct proc_dir_entry *de;
277 int len;
279 de = &proc_root;
280 while (1) {
281 next = strchr(cp, '/');
282 if (!next)
283 break;
285 len = next - cp;
286 for (de = de->subdir; de ; de = de->next) {
287 if (proc_match(len, cp, de))
288 break;
290 if (!de)
291 return -ENOENT;
292 cp += len + 1;
294 *residual = cp;
295 *ret = de;
296 return 0;
299 static DEFINE_IDR(proc_inum_idr);
300 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
302 #define PROC_DYNAMIC_FIRST 0xF0000000UL
305 * Return an inode number between PROC_DYNAMIC_FIRST and
306 * 0xffffffff, or zero on failure.
308 static unsigned int get_inode_number(void)
310 int i, inum = 0;
311 int error;
313 retry:
314 if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
315 return 0;
317 spin_lock(&proc_inum_lock);
318 error = idr_get_new(&proc_inum_idr, NULL, &i);
319 spin_unlock(&proc_inum_lock);
320 if (error == -EAGAIN)
321 goto retry;
322 else if (error)
323 return 0;
325 inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
327 /* inum will never be more than 0xf0ffffff, so no check
328 * for overflow.
331 return inum;
334 static void release_inode_number(unsigned int inum)
336 int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
338 spin_lock(&proc_inum_lock);
339 idr_remove(&proc_inum_idr, id);
340 spin_unlock(&proc_inum_lock);
343 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
345 nd_set_link(nd, PDE(dentry->d_inode)->data);
346 return NULL;
349 static struct inode_operations proc_link_inode_operations = {
350 .readlink = generic_readlink,
351 .follow_link = proc_follow_link,
355 * As some entries in /proc are volatile, we want to
356 * get rid of unused dentries. This could be made
357 * smarter: we could keep a "volatile" flag in the
358 * inode to indicate which ones to keep.
360 static int proc_delete_dentry(struct dentry * dentry)
362 return 1;
365 static struct dentry_operations proc_dentry_operations =
367 .d_delete = proc_delete_dentry,
371 * Don't create negative dentries here, return -ENOENT by hand
372 * instead.
374 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
376 struct inode *inode = NULL;
377 struct proc_dir_entry * de;
378 int error = -ENOENT;
380 lock_kernel();
381 de = PDE(dir);
382 if (de) {
383 for (de = de->subdir; de ; de = de->next) {
384 if (de->namelen != dentry->d_name.len)
385 continue;
386 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
387 unsigned int ino = de->low_ino;
389 error = -EINVAL;
390 inode = proc_get_inode(dir->i_sb, ino, de);
391 break;
395 unlock_kernel();
397 if (inode) {
398 dentry->d_op = &proc_dentry_operations;
399 d_add(dentry, inode);
400 return NULL;
402 return ERR_PTR(error);
406 * This returns non-zero if at EOF, so that the /proc
407 * root directory can use this and check if it should
408 * continue with the <pid> entries..
410 * Note that the VFS-layer doesn't care about the return
411 * value of the readdir() call, as long as it's non-negative
412 * for success..
414 int proc_readdir(struct file * filp,
415 void * dirent, filldir_t filldir)
417 struct proc_dir_entry * de;
418 unsigned int ino;
419 int i;
420 struct inode *inode = filp->f_dentry->d_inode;
421 int ret = 0;
423 lock_kernel();
425 ino = inode->i_ino;
426 de = PDE(inode);
427 if (!de) {
428 ret = -EINVAL;
429 goto out;
431 i = filp->f_pos;
432 switch (i) {
433 case 0:
434 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
435 goto out;
436 i++;
437 filp->f_pos++;
438 /* fall through */
439 case 1:
440 if (filldir(dirent, "..", 2, i,
441 parent_ino(filp->f_dentry),
442 DT_DIR) < 0)
443 goto out;
444 i++;
445 filp->f_pos++;
446 /* fall through */
447 default:
448 de = de->subdir;
449 i -= 2;
450 for (;;) {
451 if (!de) {
452 ret = 1;
453 goto out;
455 if (!i)
456 break;
457 de = de->next;
458 i--;
461 do {
462 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
463 de->low_ino, de->mode >> 12) < 0)
464 goto out;
465 filp->f_pos++;
466 de = de->next;
467 } while (de);
469 ret = 1;
470 out: unlock_kernel();
471 return ret;
475 * These are the generic /proc directory operations. They
476 * use the in-memory "struct proc_dir_entry" tree to parse
477 * the /proc directory.
479 static struct file_operations proc_dir_operations = {
480 .read = generic_read_dir,
481 .readdir = proc_readdir,
485 * proc directories can do almost nothing..
487 static struct inode_operations proc_dir_inode_operations = {
488 .lookup = proc_lookup,
489 .getattr = proc_getattr,
490 .setattr = proc_notify_change,
493 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
495 unsigned int i;
497 i = get_inode_number();
498 if (i == 0)
499 return -EAGAIN;
500 dp->low_ino = i;
501 dp->next = dir->subdir;
502 dp->parent = dir;
503 dir->subdir = dp;
504 if (S_ISDIR(dp->mode)) {
505 if (dp->proc_iops == NULL) {
506 dp->proc_fops = &proc_dir_operations;
507 dp->proc_iops = &proc_dir_inode_operations;
509 dir->nlink++;
510 } else if (S_ISLNK(dp->mode)) {
511 if (dp->proc_iops == NULL)
512 dp->proc_iops = &proc_link_inode_operations;
513 } else if (S_ISREG(dp->mode)) {
514 if (dp->proc_fops == NULL)
515 dp->proc_fops = &proc_file_operations;
516 if (dp->proc_iops == NULL)
517 dp->proc_iops = &proc_file_inode_operations;
519 return 0;
523 * Kill an inode that got unregistered..
525 static void proc_kill_inodes(struct proc_dir_entry *de)
527 struct list_head *p;
528 struct super_block *sb = proc_mnt->mnt_sb;
531 * Actually it's a partial revoke().
533 file_list_lock();
534 list_for_each(p, &sb->s_files) {
535 struct file * filp = list_entry(p, struct file, f_u.fu_list);
536 struct dentry * dentry = filp->f_dentry;
537 struct inode * inode;
538 struct file_operations *fops;
540 if (dentry->d_op != &proc_dentry_operations)
541 continue;
542 inode = dentry->d_inode;
543 if (PDE(inode) != de)
544 continue;
545 fops = filp->f_op;
546 filp->f_op = NULL;
547 fops_put(fops);
549 file_list_unlock();
552 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
553 const char *name,
554 mode_t mode,
555 nlink_t nlink)
557 struct proc_dir_entry *ent = NULL;
558 const char *fn = name;
559 int len;
561 /* make sure name is valid */
562 if (!name || !strlen(name)) goto out;
564 if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
565 goto out;
567 /* At this point there must not be any '/' characters beyond *fn */
568 if (strchr(fn, '/'))
569 goto out;
571 len = strlen(fn);
573 ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
574 if (!ent) goto out;
576 memset(ent, 0, sizeof(struct proc_dir_entry));
577 memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
578 ent->name = ((char *) ent) + sizeof(*ent);
579 ent->namelen = len;
580 ent->mode = mode;
581 ent->nlink = nlink;
582 out:
583 return ent;
586 struct proc_dir_entry *proc_symlink(const char *name,
587 struct proc_dir_entry *parent, const char *dest)
589 struct proc_dir_entry *ent;
591 ent = proc_create(&parent,name,
592 (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
594 if (ent) {
595 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
596 if (ent->data) {
597 strcpy((char*)ent->data,dest);
598 if (proc_register(parent, ent) < 0) {
599 kfree(ent->data);
600 kfree(ent);
601 ent = NULL;
603 } else {
604 kfree(ent);
605 ent = NULL;
608 return ent;
611 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
612 struct proc_dir_entry *parent)
614 struct proc_dir_entry *ent;
616 ent = proc_create(&parent, name, S_IFDIR | mode, 2);
617 if (ent) {
618 ent->proc_fops = &proc_dir_operations;
619 ent->proc_iops = &proc_dir_inode_operations;
621 if (proc_register(parent, ent) < 0) {
622 kfree(ent);
623 ent = NULL;
626 return ent;
629 struct proc_dir_entry *proc_mkdir(const char *name,
630 struct proc_dir_entry *parent)
632 return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
635 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
636 struct proc_dir_entry *parent)
638 struct proc_dir_entry *ent;
639 nlink_t nlink;
641 if (S_ISDIR(mode)) {
642 if ((mode & S_IALLUGO) == 0)
643 mode |= S_IRUGO | S_IXUGO;
644 nlink = 2;
645 } else {
646 if ((mode & S_IFMT) == 0)
647 mode |= S_IFREG;
648 if ((mode & S_IALLUGO) == 0)
649 mode |= S_IRUGO;
650 nlink = 1;
653 ent = proc_create(&parent,name,mode,nlink);
654 if (ent) {
655 if (S_ISDIR(mode)) {
656 ent->proc_fops = &proc_dir_operations;
657 ent->proc_iops = &proc_dir_inode_operations;
659 if (proc_register(parent, ent) < 0) {
660 kfree(ent);
661 ent = NULL;
664 return ent;
667 void free_proc_entry(struct proc_dir_entry *de)
669 unsigned int ino = de->low_ino;
671 if (ino < PROC_DYNAMIC_FIRST)
672 return;
674 release_inode_number(ino);
676 if (S_ISLNK(de->mode) && de->data)
677 kfree(de->data);
678 kfree(de);
682 * Remove a /proc entry and free it if it's not currently in use.
683 * If it is in use, we set the 'deleted' flag.
685 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
687 struct proc_dir_entry **p;
688 struct proc_dir_entry *de;
689 const char *fn = name;
690 int len;
692 if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
693 goto out;
694 len = strlen(fn);
695 for (p = &parent->subdir; *p; p=&(*p)->next ) {
696 if (!proc_match(len, fn, *p))
697 continue;
698 de = *p;
699 *p = de->next;
700 de->next = NULL;
701 if (S_ISDIR(de->mode))
702 parent->nlink--;
703 proc_kill_inodes(de);
704 de->nlink = 0;
705 WARN_ON(de->subdir);
706 if (!atomic_read(&de->count))
707 free_proc_entry(de);
708 else {
709 de->deleted = 1;
710 printk("remove_proc_entry: %s/%s busy, count=%d\n",
711 parent->name, de->name, atomic_read(&de->count));
713 break;
715 out:
716 return;