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