38c85408176c6dbcf32eff87d9b5cc961369e12a
[linux-2.6.git] / fs / proc / generic.c
1 /*
2  * proc/fs/generic.c --- generic routines for the proc-fs
3  *
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  */
10
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/vserver/inode.h>
23 #include <asm/uaccess.h>
24
25 #include "internal.h"
26
27 static ssize_t proc_file_read(struct file *file, char __user *buf,
28                               size_t nbytes, loff_t *ppos);
29 static ssize_t proc_file_write(struct file *file, const char __user *buffer,
30                                size_t count, loff_t *ppos);
31 static loff_t proc_file_lseek(struct file *, loff_t, int);
32
33 int proc_match(int len, const char *name, struct proc_dir_entry *de)
34 {
35         if (de->namelen != len)
36                 return 0;
37         return !memcmp(name, de->name, len);
38 }
39
40 static struct file_operations proc_file_operations = {
41         .llseek         = proc_file_lseek,
42         .read           = proc_file_read,
43         .write          = proc_file_write,
44 };
45
46 /* buffer size is one page but our output routines use some slack for overruns */
47 #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
48
49 static ssize_t
50 proc_file_read(struct file *file, char __user *buf, size_t nbytes,
51                loff_t *ppos)
52 {
53         struct inode * inode = file->f_dentry->d_inode;
54         char    *page;
55         ssize_t retval=0;
56         int     eof=0;
57         ssize_t n, count;
58         char    *start;
59         struct proc_dir_entry * dp;
60         unsigned long long pos;
61
62         /*
63          * Gaah, please just use "seq_file" instead. The legacy /proc
64          * interfaces cut loff_t down to off_t for reads, and ignore
65          * the offset entirely for writes..
66          */
67         pos = *ppos;
68         if (pos > MAX_NON_LFS)
69                 return 0;
70         if (nbytes > MAX_NON_LFS - pos)
71                 nbytes = MAX_NON_LFS - pos;
72
73         dp = PDE(inode);
74         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
75                 return -ENOMEM;
76
77         while ((nbytes > 0) && !eof) {
78                 count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
79
80                 start = NULL;
81                 if (dp->get_info) {
82                         /* Handle old net routines */
83                         n = dp->get_info(page, &start, *ppos, count);
84                         if (n < count)
85                                 eof = 1;
86                 } else if (dp->read_proc) {
87                         /*
88                          * How to be a proc read function
89                          * ------------------------------
90                          * Prototype:
91                          *    int f(char *buffer, char **start, off_t offset,
92                          *          int count, int *peof, void *dat)
93                          *
94                          * Assume that the buffer is "count" bytes in size.
95                          *
96                          * If you know you have supplied all the data you
97                          * have, set *peof.
98                          *
99                          * You have three ways to return data:
100                          * 0) Leave *start = NULL.  (This is the default.)
101                          *    Put the data of the requested offset at that
102                          *    offset within the buffer.  Return the number (n)
103                          *    of bytes there are from the beginning of the
104                          *    buffer up to the last byte of data.  If the
105                          *    number of supplied bytes (= n - offset) is 
106                          *    greater than zero and you didn't signal eof
107                          *    and the reader is prepared to take more data
108                          *    you will be called again with the requested
109                          *    offset advanced by the number of bytes 
110                          *    absorbed.  This interface is useful for files
111                          *    no larger than the buffer.
112                          * 1) Set *start = an unsigned long value less than
113                          *    the buffer address but greater than zero.
114                          *    Put the data of the requested offset at the
115                          *    beginning of the buffer.  Return the number of
116                          *    bytes of data placed there.  If this number is
117                          *    greater than zero and you didn't signal eof
118                          *    and the reader is prepared to take more data
119                          *    you will be called again with the requested
120                          *    offset advanced by *start.  This interface is
121                          *    useful when you have a large file consisting
122                          *    of a series of blocks which you want to count
123                          *    and return as wholes.
124                          *    (Hack by Paul.Russell@rustcorp.com.au)
125                          * 2) Set *start = an address within the buffer.
126                          *    Put the data of the requested offset at *start.
127                          *    Return the number of bytes of data placed there.
128                          *    If this number is greater than zero and you
129                          *    didn't signal eof and the reader is prepared to
130                          *    take more data you will be called again with the
131                          *    requested offset advanced by the number of bytes
132                          *    absorbed.
133                          */
134                         n = dp->read_proc(page, &start, *ppos,
135                                           count, &eof, dp->data);
136                 } else
137                         break;
138
139                 if (n == 0)   /* end of file */
140                         break;
141                 if (n < 0) {  /* error */
142                         if (retval == 0)
143                                 retval = n;
144                         break;
145                 }
146
147                 if (start == NULL) {
148                         if (n > PAGE_SIZE) {
149                                 printk(KERN_ERR
150                                        "proc_file_read: Apparent buffer overflow!\n");
151                                 n = PAGE_SIZE;
152                         }
153                         n -= *ppos;
154                         if (n <= 0)
155                                 break;
156                         if (n > count)
157                                 n = count;
158                         start = page + *ppos;
159                 } else if (start < page) {
160                         if (n > PAGE_SIZE) {
161                                 printk(KERN_ERR
162                                        "proc_file_read: Apparent buffer overflow!\n");
163                                 n = PAGE_SIZE;
164                         }
165                         if (n > count) {
166                                 /*
167                                  * Don't reduce n because doing so might
168                                  * cut off part of a data block.
169                                  */
170                                 printk(KERN_WARNING
171                                        "proc_file_read: Read count exceeded\n");
172                         }
173                 } else /* start >= page */ {
174                         unsigned long startoff = (unsigned long)(start - page);
175                         if (n > (PAGE_SIZE - startoff)) {
176                                 printk(KERN_ERR
177                                        "proc_file_read: Apparent buffer overflow!\n");
178                                 n = PAGE_SIZE - startoff;
179                         }
180                         if (n > count)
181                                 n = count;
182                 }
183                 
184                 n -= copy_to_user(buf, start < page ? page : start, n);
185                 if (n == 0) {
186                         if (retval == 0)
187                                 retval = -EFAULT;
188                         break;
189                 }
190
191                 *ppos += start < page ? (unsigned long)start : n;
192                 nbytes -= n;
193                 buf += n;
194                 retval += n;
195         }
196         free_page((unsigned long) page);
197         return retval;
198 }
199
200 static ssize_t
201 proc_file_write(struct file *file, const char __user *buffer,
202                 size_t count, loff_t *ppos)
203 {
204         struct inode *inode = file->f_dentry->d_inode;
205         struct proc_dir_entry * dp;
206         
207         dp = PDE(inode);
208
209         if (!dp->write_proc)
210                 return -EIO;
211
212         /* FIXME: does this routine need ppos?  probably... */
213         return dp->write_proc(file, buffer, count, dp->data);
214 }
215
216
217 static loff_t
218 proc_file_lseek(struct file *file, loff_t offset, int orig)
219 {
220         loff_t retval = -EINVAL;
221         switch (orig) {
222         case 1:
223                 offset += file->f_pos;
224         /* fallthrough */
225         case 0:
226                 if (offset < 0 || offset > MAX_NON_LFS)
227                         break;
228                 file->f_pos = retval = offset;
229         }
230         return retval;
231 }
232
233 static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
234 {
235         struct inode *inode = dentry->d_inode;
236         struct proc_dir_entry *de = PDE(inode);
237         int error;
238
239         error = inode_change_ok(inode, iattr);
240         if (error)
241                 goto out;
242
243         error = inode_setattr(inode, iattr);
244         if (error)
245                 goto out;
246         
247         de->uid = inode->i_uid;
248         de->gid = inode->i_gid;
249         de->mode = inode->i_mode;
250 out:
251         return error;
252 }
253
254 static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
255                         struct kstat *stat)
256 {
257         struct inode *inode = dentry->d_inode;
258         struct proc_dir_entry *de = PROC_I(inode)->pde;
259         if (de && de->nlink)
260                 inode->i_nlink = de->nlink;
261
262         generic_fillattr(inode, stat);
263         return 0;
264 }
265
266 static struct inode_operations proc_file_inode_operations = {
267         .setattr        = proc_notify_change,
268 };
269
270 /*
271  * This function parses a name such as "tty/driver/serial", and
272  * returns the struct proc_dir_entry for "/proc/tty/driver", and
273  * returns "serial" in residual.
274  */
275 static int xlate_proc_name(const char *name,
276                            struct proc_dir_entry **ret, const char **residual)
277 {
278         const char              *cp = name, *next;
279         struct proc_dir_entry   *de;
280         int                     len;
281
282         de = &proc_root;
283         while (1) {
284                 next = strchr(cp, '/');
285                 if (!next)
286                         break;
287
288                 len = next - cp;
289                 for (de = de->subdir; de ; de = de->next) {
290                         if (proc_match(len, cp, de))
291                                 break;
292                 }
293                 if (!de)
294                         return -ENOENT;
295                 cp += len + 1;
296         }
297         *residual = cp;
298         *ret = de;
299         return 0;
300 }
301
302 static DEFINE_IDR(proc_inum_idr);
303 static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
304
305 #define PROC_DYNAMIC_FIRST 0xF0000000UL
306
307 /*
308  * Return an inode number between PROC_DYNAMIC_FIRST and
309  * 0xffffffff, or zero on failure.
310  */
311 static unsigned int get_inode_number(void)
312 {
313         int i, inum = 0;
314         int error;
315
316 retry:
317         if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
318                 return 0;
319
320         spin_lock(&proc_inum_lock);
321         error = idr_get_new(&proc_inum_idr, NULL, &i);
322         spin_unlock(&proc_inum_lock);
323         if (error == -EAGAIN)
324                 goto retry;
325         else if (error)
326                 return 0;
327
328         inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
329
330         /* inum will never be more than 0xf0ffffff, so no check
331          * for overflow.
332          */
333
334         return inum;
335 }
336
337 static void release_inode_number(unsigned int inum)
338 {
339         int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
340
341         spin_lock(&proc_inum_lock);
342         idr_remove(&proc_inum_idr, id);
343         spin_unlock(&proc_inum_lock);
344 }
345
346 static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
347 {
348         nd_set_link(nd, PDE(dentry->d_inode)->data);
349         return NULL;
350 }
351
352 static struct inode_operations proc_link_inode_operations = {
353         .readlink       = generic_readlink,
354         .follow_link    = proc_follow_link,
355 };
356
357 /*
358  * As some entries in /proc are volatile, we want to 
359  * get rid of unused dentries.  This could be made 
360  * smarter: we could keep a "volatile" flag in the 
361  * inode to indicate which ones to keep.
362  */
363 static int proc_delete_dentry(struct dentry * dentry)
364 {
365         return 1;
366 }
367
368 static struct dentry_operations proc_dentry_operations =
369 {
370         .d_delete       = proc_delete_dentry,
371 };
372
373 /*
374  * Don't create negative dentries here, return -ENOENT by hand
375  * instead.
376  */
377 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
378 {
379         struct inode *inode = NULL;
380         struct proc_dir_entry * de;
381         int error = -ENOENT;
382
383         lock_kernel();
384         de = PDE(dir);
385         if (de) {
386                 for (de = de->subdir; de ; de = de->next) {
387                         if (de->namelen != dentry->d_name.len)
388                                 continue;
389                         if (!vx_hide_check(0, de->vx_flags))
390                                 continue;
391                         if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
392                                 unsigned int ino = de->low_ino;
393
394                                 error = -EINVAL;
395                                 inode = proc_get_inode(dir->i_sb, ino, de);
396                                 /* generic proc entries belong to the host */
397                                 inode->i_xid = 0;
398                                 break;
399                         }
400                 }
401         }
402         unlock_kernel();
403
404         if (inode) {
405                 dentry->d_op = &proc_dentry_operations;
406                 d_add(dentry, inode);
407                 return NULL;
408         }
409         return ERR_PTR(error);
410 }
411
412 /*
413  * This returns non-zero if at EOF, so that the /proc
414  * root directory can use this and check if it should
415  * continue with the <pid> entries..
416  *
417  * Note that the VFS-layer doesn't care about the return
418  * value of the readdir() call, as long as it's non-negative
419  * for success..
420  */
421 int proc_readdir(struct file * filp,
422         void * dirent, filldir_t filldir)
423 {
424         struct proc_dir_entry * de;
425         unsigned int ino;
426         int i;
427         struct inode *inode = filp->f_dentry->d_inode;
428         int ret = 0;
429
430         lock_kernel();
431
432         ino = inode->i_ino;
433         de = PDE(inode);
434         if (!de) {
435                 ret = -EINVAL;
436                 goto out;
437         }
438         i = filp->f_pos;
439         switch (i) {
440                 case 0:
441                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
442                                 goto out;
443                         i++;
444                         filp->f_pos++;
445                         /* fall through */
446                 case 1:
447                         if (filldir(dirent, "..", 2, i,
448                                     parent_ino(filp->f_dentry),
449                                     DT_DIR) < 0)
450                                 goto out;
451                         i++;
452                         filp->f_pos++;
453                         /* fall through */
454                 default:
455                         de = de->subdir;
456                         i -= 2;
457                         for (;;) {
458                                 if (!de) {
459                                         ret = 1;
460                                         goto out;
461                                 }
462                                 if (!i)
463                                         break;
464                                 de = de->next;
465                                 i--;
466                         }
467
468                         do {
469                                 if (!vx_hide_check(0, de->vx_flags))
470                                         goto skip;
471                                 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
472                                             de->low_ino, de->mode >> 12) < 0)
473                                         goto out;
474                         skip:
475                                 filp->f_pos++;
476                                 de = de->next;
477                         } while (de);
478         }
479         ret = 1;
480 out:    unlock_kernel();
481         return ret;     
482 }
483
484 /*
485  * These are the generic /proc directory operations. They
486  * use the in-memory "struct proc_dir_entry" tree to parse
487  * the /proc directory.
488  */
489 static struct file_operations proc_dir_operations = {
490         .read                   = generic_read_dir,
491         .readdir                = proc_readdir,
492 };
493
494 /*
495  * proc directories can do almost nothing..
496  */
497 static struct inode_operations proc_dir_inode_operations = {
498         .lookup         = proc_lookup,
499         .getattr        = proc_getattr,
500         .setattr        = proc_notify_change,
501 };
502
503 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
504 {
505         unsigned int i;
506         
507         i = get_inode_number();
508         if (i == 0)
509                 return -EAGAIN;
510         dp->low_ino = i;
511         dp->next = dir->subdir;
512         dp->parent = dir;
513         dir->subdir = dp;
514         if (S_ISDIR(dp->mode)) {
515                 if (dp->proc_iops == NULL) {
516                         dp->proc_fops = &proc_dir_operations;
517                         dp->proc_iops = &proc_dir_inode_operations;
518                 }
519                 dir->nlink++;
520         } else if (S_ISLNK(dp->mode)) {
521                 if (dp->proc_iops == NULL)
522                         dp->proc_iops = &proc_link_inode_operations;
523         } else if (S_ISREG(dp->mode)) {
524                 if (dp->proc_fops == NULL)
525                         dp->proc_fops = &proc_file_operations;
526                 if (dp->proc_iops == NULL)
527                         dp->proc_iops = &proc_file_inode_operations;
528         }
529         return 0;
530 }
531
532 /*
533  * Kill an inode that got unregistered..
534  */
535 static void proc_kill_inodes(struct proc_dir_entry *de)
536 {
537         struct list_head *p;
538         struct super_block *sb = proc_mnt->mnt_sb;
539
540         /*
541          * Actually it's a partial revoke().
542          */
543         file_list_lock();
544         list_for_each(p, &sb->s_files) {
545                 struct file * filp = list_entry(p, struct file, f_u.fu_list);
546                 struct dentry * dentry = filp->f_dentry;
547                 struct inode * inode;
548                 struct file_operations *fops;
549
550                 if (dentry->d_op != &proc_dentry_operations)
551                         continue;
552                 inode = dentry->d_inode;
553                 if (PDE(inode) != de)
554                         continue;
555                 fops = filp->f_op;
556                 filp->f_op = NULL;
557                 fops_put(fops);
558         }
559         file_list_unlock();
560 }
561
562 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
563                                           const char *name,
564                                           mode_t mode,
565                                           nlink_t nlink)
566 {
567         struct proc_dir_entry *ent = NULL;
568         const char *fn = name;
569         int len;
570
571         /* make sure name is valid */
572         if (!name || !strlen(name)) goto out;
573
574         if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
575                 goto out;
576
577         /* At this point there must not be any '/' characters beyond *fn */
578         if (strchr(fn, '/'))
579                 goto out;
580
581         len = strlen(fn);
582
583         ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
584         if (!ent) goto out;
585
586         memset(ent, 0, sizeof(struct proc_dir_entry));
587         memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
588         ent->name = ((char *) ent) + sizeof(*ent);
589         ent->namelen = len;
590         ent->mode = mode;
591         ent->nlink = nlink;
592         ent->vx_flags = IATTR_PROC_DEFAULT;
593  out:
594         return ent;
595 }
596
597 struct proc_dir_entry *proc_symlink(const char *name,
598                 struct proc_dir_entry *parent, const char *dest)
599 {
600         struct proc_dir_entry *ent;
601
602         ent = proc_create(&parent,name,
603                           (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
604
605         if (ent) {
606                 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
607                 if (ent->data) {
608                         strcpy((char*)ent->data,dest);
609                         if (proc_register(parent, ent) < 0) {
610                                 kfree(ent->data);
611                                 kfree(ent);
612                                 ent = NULL;
613                         } else
614                                 ent->vx_flags = IATTR_PROC_SYMLINK;
615                 } else {
616                         kfree(ent);
617                         ent = NULL;
618                 }
619         }
620         return ent;
621 }
622
623 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
624                 struct proc_dir_entry *parent)
625 {
626         struct proc_dir_entry *ent;
627
628         ent = proc_create(&parent, name, S_IFDIR | mode, 2);
629         if (ent) {
630                 ent->proc_fops = &proc_dir_operations;
631                 ent->proc_iops = &proc_dir_inode_operations;
632
633                 if (proc_register(parent, ent) < 0) {
634                         kfree(ent);
635                         ent = NULL;
636                 }
637         }
638         return ent;
639 }
640
641 struct proc_dir_entry *proc_mkdir(const char *name,
642                 struct proc_dir_entry *parent)
643 {
644         return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
645 }
646
647 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
648                                          struct proc_dir_entry *parent)
649 {
650         struct proc_dir_entry *ent;
651         nlink_t nlink;
652
653         if (S_ISDIR(mode)) {
654                 if ((mode & S_IALLUGO) == 0)
655                         mode |= S_IRUGO | S_IXUGO;
656                 nlink = 2;
657         } else {
658                 if ((mode & S_IFMT) == 0)
659                         mode |= S_IFREG;
660                 if ((mode & S_IALLUGO) == 0)
661                         mode |= S_IRUGO;
662                 nlink = 1;
663         }
664
665         ent = proc_create(&parent,name,mode,nlink);
666         if (ent) {
667                 if (S_ISDIR(mode)) {
668                         ent->proc_fops = &proc_dir_operations;
669                         ent->proc_iops = &proc_dir_inode_operations;
670                 }
671                 if (proc_register(parent, ent) < 0) {
672                         kfree(ent);
673                         ent = NULL;
674                 }
675         }
676         return ent;
677 }
678
679 void free_proc_entry(struct proc_dir_entry *de)
680 {
681         unsigned int ino = de->low_ino;
682
683         if (ino < PROC_DYNAMIC_FIRST)
684                 return;
685
686         release_inode_number(ino);
687
688         if (S_ISLNK(de->mode) && de->data)
689                 kfree(de->data);
690         kfree(de);
691 }
692
693 /*
694  * Remove a /proc entry and free it if it's not currently in use.
695  * If it is in use, we set the 'deleted' flag.
696  */
697 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
698 {
699         struct proc_dir_entry **p;
700         struct proc_dir_entry *de;
701         const char *fn = name;
702         int len;
703
704         if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
705                 goto out;
706         len = strlen(fn);
707         for (p = &parent->subdir; *p; p=&(*p)->next ) {
708                 if (!proc_match(len, fn, *p))
709                         continue;
710                 de = *p;
711                 *p = de->next;
712                 de->next = NULL;
713                 if (S_ISDIR(de->mode))
714                         parent->nlink--;
715                 proc_kill_inodes(de);
716                 de->nlink = 0;
717                 WARN_ON(de->subdir);
718                 if (!atomic_read(&de->count))
719                         free_proc_entry(de);
720                 else {
721                         de->deleted = 1;
722                         printk("remove_proc_entry: %s/%s busy, count=%d\n",
723                                 parent->name, de->name, atomic_read(&de->count));
724                 }
725                 break;
726         }
727 out:
728         return;
729 }