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