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