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