Merge to Fedora kernel-2.6.7-1.441
[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         int i, inum = 0;
294         int error;
295
296 retry:
297         if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
298                 return 0;
299
300         spin_lock(&proc_inum_lock);
301         error = idr_get_new(&proc_inum_idr, NULL, &i);
302         spin_unlock(&proc_inum_lock);
303         if (error == -EAGAIN)
304                 goto retry;
305         else if (error)
306                 return 0;
307
308         inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
309
310         /* inum will never be more than 0xf0ffffff, so no check
311          * for overflow.
312          */
313
314         return inum;
315 }
316
317 static void release_inode_number(unsigned int inum)
318 {
319         int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
320
321         spin_lock(&proc_inum_lock);
322         idr_remove(&proc_inum_idr, id);
323         spin_unlock(&proc_inum_lock);
324 }
325
326 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
327 {
328         nd_set_link(nd, PDE(dentry->d_inode)->data);
329         return 0;
330 }
331
332 static struct inode_operations proc_link_inode_operations = {
333         .readlink       = generic_readlink,
334         .follow_link    = proc_follow_link,
335 };
336
337 /*
338  * As some entries in /proc are volatile, we want to 
339  * get rid of unused dentries.  This could be made 
340  * smarter: we could keep a "volatile" flag in the 
341  * inode to indicate which ones to keep.
342  */
343 static int proc_delete_dentry(struct dentry * dentry)
344 {
345         return 1;
346 }
347
348 static int proc_revalidate_dentry(struct dentry *de, struct nameidata *nd)
349 {
350         /* maybe add a check if it's really necessary? */
351         return 0;
352 }
353
354 static struct dentry_operations proc_dentry_operations =
355 {
356         .d_revalidate   = proc_revalidate_dentry,
357         .d_delete       = proc_delete_dentry,
358 };
359
360 /*
361  * Don't create negative dentries here, return -ENOENT by hand
362  * instead.
363  */
364 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
365 {
366         struct inode *inode = NULL;
367         struct proc_dir_entry * de;
368         int error = -ENOENT;
369
370         lock_kernel();
371         de = PDE(dir);
372         if (de) {
373                 for (de = de->subdir; de ; de = de->next) {
374                         if (de->namelen != dentry->d_name.len)
375                                 continue;
376                         if (!vx_hide_check(0, de->vx_flags))
377                                 continue;
378                         if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
379                                 unsigned int ino = de->low_ino;
380
381                                 error = -EINVAL;
382                                 inode = proc_get_inode(dir->i_sb, ino, de);
383                                 break;
384                         }
385                 }
386         }
387         unlock_kernel();
388
389         if (inode) {
390                 dentry->d_op = &proc_dentry_operations;
391                 d_add(dentry, inode);
392                 return NULL;
393         }
394         return ERR_PTR(error);
395 }
396
397 /*
398  * This returns non-zero if at EOF, so that the /proc
399  * root directory can use this and check if it should
400  * continue with the <pid> entries..
401  *
402  * Note that the VFS-layer doesn't care about the return
403  * value of the readdir() call, as long as it's non-negative
404  * for success..
405  */
406 int proc_readdir(struct file * filp,
407         void * dirent, filldir_t filldir)
408 {
409         struct proc_dir_entry * de;
410         unsigned int ino;
411         int i;
412         struct inode *inode = filp->f_dentry->d_inode;
413         int ret = 0;
414
415         lock_kernel();
416
417         ino = inode->i_ino;
418         de = PDE(inode);
419         if (!de) {
420                 ret = -EINVAL;
421                 goto out;
422         }
423         i = filp->f_pos;
424         switch (i) {
425                 case 0:
426                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
427                                 goto out;
428                         i++;
429                         filp->f_pos++;
430                         /* fall through */
431                 case 1:
432                         if (filldir(dirent, "..", 2, i,
433                                     parent_ino(filp->f_dentry),
434                                     DT_DIR) < 0)
435                                 goto out;
436                         i++;
437                         filp->f_pos++;
438                         /* fall through */
439                 default:
440                         de = de->subdir;
441                         i -= 2;
442                         for (;;) {
443                                 if (!de) {
444                                         ret = 1;
445                                         goto out;
446                                 }
447                                 if (!i)
448                                         break;
449                                 de = de->next;
450                                 i--;
451                         }
452
453                         do {
454                                 if (!vx_hide_check(0, de->vx_flags))
455                                         goto skip;
456                                 if (filldir(dirent, de->name, de->namelen, filp->f_pos,
457                                             de->low_ino, de->mode >> 12) < 0)
458                                         goto out;
459                         skip:
460                                 filp->f_pos++;
461                                 de = de->next;
462                         } while (de);
463         }
464         ret = 1;
465 out:    unlock_kernel();
466         return ret;     
467 }
468
469 /*
470  * These are the generic /proc directory operations. They
471  * use the in-memory "struct proc_dir_entry" tree to parse
472  * the /proc directory.
473  */
474 static struct file_operations proc_dir_operations = {
475         .read                   = generic_read_dir,
476         .readdir                = proc_readdir,
477 };
478
479 /*
480  * proc directories can do almost nothing..
481  */
482 static struct inode_operations proc_dir_inode_operations = {
483         .lookup         = proc_lookup,
484         .setattr        = proc_notify_change,
485 };
486
487 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
488 {
489         unsigned int i;
490         
491         i = get_inode_number();
492         if (i == 0)
493                 return -EAGAIN;
494         dp->low_ino = i;
495         dp->next = dir->subdir;
496         dp->parent = dir;
497         dir->subdir = dp;
498         if (S_ISDIR(dp->mode)) {
499                 if (dp->proc_iops == NULL) {
500                         dp->proc_fops = &proc_dir_operations;
501                         dp->proc_iops = &proc_dir_inode_operations;
502                 }
503                 dir->nlink++;
504         } else if (S_ISLNK(dp->mode)) {
505                 if (dp->proc_iops == NULL)
506                         dp->proc_iops = &proc_link_inode_operations;
507         } else if (S_ISREG(dp->mode)) {
508                 if (dp->proc_fops == NULL)
509                         dp->proc_fops = &proc_file_operations;
510                 if (dp->proc_iops == NULL)
511                         dp->proc_iops = &proc_file_inode_operations;
512         }
513         return 0;
514 }
515
516 /*
517  * Kill an inode that got unregistered..
518  */
519 static void proc_kill_inodes(struct proc_dir_entry *de)
520 {
521         struct list_head *p;
522         struct super_block *sb = proc_mnt->mnt_sb;
523
524         /*
525          * Actually it's a partial revoke().
526          */
527         file_list_lock();
528         list_for_each(p, &sb->s_files) {
529                 struct file * filp = list_entry(p, struct file, f_list);
530                 struct dentry * dentry = filp->f_dentry;
531                 struct inode * inode;
532                 struct file_operations *fops;
533
534                 if (dentry->d_op != &proc_dentry_operations)
535                         continue;
536                 inode = dentry->d_inode;
537                 if (PDE(inode) != de)
538                         continue;
539                 fops = filp->f_op;
540                 filp->f_op = NULL;
541                 fops_put(fops);
542         }
543         file_list_unlock();
544 }
545
546 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
547                                           const char *name,
548                                           mode_t mode,
549                                           nlink_t nlink)
550 {
551         struct proc_dir_entry *ent = NULL;
552         const char *fn = name;
553         int len;
554
555         /* make sure name is valid */
556         if (!name || !strlen(name)) goto out;
557
558         if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
559                 goto out;
560         len = strlen(fn);
561
562         ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
563         if (!ent) goto out;
564
565         memset(ent, 0, sizeof(struct proc_dir_entry));
566         memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
567         ent->name = ((char *) ent) + sizeof(*ent);
568         ent->namelen = len;
569         ent->mode = mode;
570         ent->nlink = nlink;
571         ent->vx_flags = IATTR_PROC_DEFAULT;
572  out:
573         return ent;
574 }
575
576 struct proc_dir_entry *proc_symlink(const char *name,
577                 struct proc_dir_entry *parent, const char *dest)
578 {
579         struct proc_dir_entry *ent;
580
581         ent = proc_create(&parent,name,
582                           (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
583
584         if (ent) {
585                 ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
586                 if (ent->data) {
587                         strcpy((char*)ent->data,dest);
588                         if (proc_register(parent, ent) < 0) {
589                                 kfree(ent->data);
590                                 kfree(ent);
591                                 ent = NULL;
592                         } else
593                                 ent->vx_flags = IATTR_PROC_SYMLINK;
594                 } else {
595                         kfree(ent);
596                         ent = NULL;
597                 }
598         }
599         return ent;
600 }
601
602 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
603                 struct proc_dir_entry *parent)
604 {
605         struct proc_dir_entry *ent;
606
607         ent = proc_create(&parent, name, S_IFDIR | mode, 2);
608         if (ent) {
609                 ent->proc_fops = &proc_dir_operations;
610                 ent->proc_iops = &proc_dir_inode_operations;
611
612                 if (proc_register(parent, ent) < 0) {
613                         kfree(ent);
614                         ent = NULL;
615                 }
616         }
617         return ent;
618 }
619
620 struct proc_dir_entry *proc_mkdir(const char *name,
621                 struct proc_dir_entry *parent)
622 {
623         return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
624 }
625
626 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
627                                          struct proc_dir_entry *parent)
628 {
629         struct proc_dir_entry *ent;
630         nlink_t nlink;
631
632         if (S_ISDIR(mode)) {
633                 if ((mode & S_IALLUGO) == 0)
634                         mode |= S_IRUGO | S_IXUGO;
635                 nlink = 2;
636         } else {
637                 if ((mode & S_IFMT) == 0)
638                         mode |= S_IFREG;
639                 if ((mode & S_IALLUGO) == 0)
640                         mode |= S_IRUGO;
641                 nlink = 1;
642         }
643
644         ent = proc_create(&parent,name,mode,nlink);
645         if (ent) {
646                 if (S_ISDIR(mode)) {
647                         ent->proc_fops = &proc_dir_operations;
648                         ent->proc_iops = &proc_dir_inode_operations;
649                 }
650                 if (proc_register(parent, ent) < 0) {
651                         kfree(ent);
652                         ent = NULL;
653                 }
654         }
655         return ent;
656 }
657
658 void free_proc_entry(struct proc_dir_entry *de)
659 {
660         unsigned int ino = de->low_ino;
661
662         if (ino < PROC_DYNAMIC_FIRST)
663                 return;
664
665         release_inode_number(ino);
666
667         if (S_ISLNK(de->mode) && de->data)
668                 kfree(de->data);
669         kfree(de);
670 }
671
672 /*
673  * Remove a /proc entry and free it if it's not currently in use.
674  * If it is in use, we set the 'deleted' flag.
675  */
676 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
677 {
678         struct proc_dir_entry **p;
679         struct proc_dir_entry *de;
680         const char *fn = name;
681         int len;
682
683         if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
684                 goto out;
685         len = strlen(fn);
686         for (p = &parent->subdir; *p; p=&(*p)->next ) {
687                 if (!proc_match(len, fn, *p))
688                         continue;
689                 de = *p;
690                 *p = de->next;
691                 de->next = NULL;
692                 if (S_ISDIR(de->mode))
693                         parent->nlink--;
694                 proc_kill_inodes(de);
695                 de->nlink = 0;
696                 BUG_ON(de->subdir);
697                 if (!atomic_read(&de->count))
698                         free_proc_entry(de);
699                 else {
700                         de->deleted = 1;
701                         printk("remove_proc_entry: %s/%s busy, count=%d\n",
702                                 parent->name, de->name, atomic_read(&de->count));
703                 }
704                 break;
705         }
706 out:
707         return;
708 }