seq_file: properly cope with pread
[pandora-kernel.git] / fs / seq_file.c
1 /*
2  * linux/fs/seq_file.c
3  *
4  * helper functions for making synthetic files from sequences of records.
5  * initial implementation -- AV, Oct 2001.
6  */
7
8 #include <linux/fs.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12
13 #include <asm/uaccess.h>
14 #include <asm/page.h>
15
16 /**
17  *      seq_open -      initialize sequential file
18  *      @file: file we initialize
19  *      @op: method table describing the sequence
20  *
21  *      seq_open() sets @file, associating it with a sequence described
22  *      by @op.  @op->start() sets the iterator up and returns the first
23  *      element of sequence. @op->stop() shuts it down.  @op->next()
24  *      returns the next element of sequence.  @op->show() prints element
25  *      into the buffer.  In case of error ->start() and ->next() return
26  *      ERR_PTR(error).  In the end of sequence they return %NULL. ->show()
27  *      returns 0 in case of success and negative number in case of error.
28  *      Returning SEQ_SKIP means "discard this element and move on".
29  */
30 int seq_open(struct file *file, const struct seq_operations *op)
31 {
32         struct seq_file *p = file->private_data;
33
34         if (!p) {
35                 p = kmalloc(sizeof(*p), GFP_KERNEL);
36                 if (!p)
37                         return -ENOMEM;
38                 file->private_data = p;
39         }
40         memset(p, 0, sizeof(*p));
41         mutex_init(&p->lock);
42         p->op = op;
43
44         /*
45          * Wrappers around seq_open(e.g. swaps_open) need to be
46          * aware of this. If they set f_version themselves, they
47          * should call seq_open first and then set f_version.
48          */
49         file->f_version = 0;
50
51         /*
52          * seq_files support lseek() and pread().  They do not implement
53          * write() at all, but we clear FMODE_PWRITE here for historical
54          * reasons.
55          *
56          * If a client of seq_files a) implements file.write() and b) wishes to
57          * support pwrite() then that client will need to implement its own
58          * file.open() which calls seq_open() and then sets FMODE_PWRITE.
59          */
60         file->f_mode &= ~FMODE_PWRITE;
61         return 0;
62 }
63 EXPORT_SYMBOL(seq_open);
64
65 static int traverse(struct seq_file *m, loff_t offset)
66 {
67         loff_t pos = 0, index;
68         int error = 0;
69         void *p;
70
71         m->version = 0;
72         index = 0;
73         m->count = m->from = 0;
74         if (!offset) {
75                 m->index = index;
76                 return 0;
77         }
78         if (!m->buf) {
79                 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
80                 if (!m->buf)
81                         return -ENOMEM;
82         }
83         p = m->op->start(m, &index);
84         while (p) {
85                 error = PTR_ERR(p);
86                 if (IS_ERR(p))
87                         break;
88                 error = m->op->show(m, p);
89                 if (error < 0)
90                         break;
91                 if (unlikely(error)) {
92                         error = 0;
93                         m->count = 0;
94                 }
95                 if (m->count == m->size)
96                         goto Eoverflow;
97                 if (pos + m->count > offset) {
98                         m->from = offset - pos;
99                         m->count -= m->from;
100                         m->index = index;
101                         break;
102                 }
103                 pos += m->count;
104                 m->count = 0;
105                 if (pos == offset) {
106                         index++;
107                         m->index = index;
108                         break;
109                 }
110                 p = m->op->next(m, p, &index);
111         }
112         m->op->stop(m, p);
113         m->index = index;
114         return error;
115
116 Eoverflow:
117         m->op->stop(m, p);
118         kfree(m->buf);
119         m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
120         return !m->buf ? -ENOMEM : -EAGAIN;
121 }
122
123 /**
124  *      seq_read -      ->read() method for sequential files.
125  *      @file: the file to read from
126  *      @buf: the buffer to read to
127  *      @size: the maximum number of bytes to read
128  *      @ppos: the current position in the file
129  *
130  *      Ready-made ->f_op->read()
131  */
132 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
133 {
134         struct seq_file *m = (struct seq_file *)file->private_data;
135         size_t copied = 0;
136         loff_t pos;
137         size_t n;
138         void *p;
139         int err = 0;
140
141         mutex_lock(&m->lock);
142
143         /* Don't assume *ppos is where we left it */
144         if (unlikely(*ppos != m->read_pos)) {
145                 m->read_pos = *ppos;
146                 while ((err = traverse(m, *ppos)) == -EAGAIN)
147                         ;
148                 if (err) {
149                         /* With prejudice... */
150                         m->read_pos = 0;
151                         m->version = 0;
152                         m->index = 0;
153                         m->count = 0;
154                         goto Done;
155                 }
156         }
157
158         /*
159          * seq_file->op->..m_start/m_stop/m_next may do special actions
160          * or optimisations based on the file->f_version, so we want to
161          * pass the file->f_version to those methods.
162          *
163          * seq_file->version is just copy of f_version, and seq_file
164          * methods can treat it simply as file version.
165          * It is copied in first and copied out after all operations.
166          * It is convenient to have it as  part of structure to avoid the
167          * need of passing another argument to all the seq_file methods.
168          */
169         m->version = file->f_version;
170         /* grab buffer if we didn't have one */
171         if (!m->buf) {
172                 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
173                 if (!m->buf)
174                         goto Enomem;
175         }
176         /* if not empty - flush it first */
177         if (m->count) {
178                 n = min(m->count, size);
179                 err = copy_to_user(buf, m->buf + m->from, n);
180                 if (err)
181                         goto Efault;
182                 m->count -= n;
183                 m->from += n;
184                 size -= n;
185                 buf += n;
186                 copied += n;
187                 if (!m->count)
188                         m->index++;
189                 if (!size)
190                         goto Done;
191         }
192         /* we need at least one record in buffer */
193         pos = m->index;
194         p = m->op->start(m, &pos);
195         while (1) {
196                 err = PTR_ERR(p);
197                 if (!p || IS_ERR(p))
198                         break;
199                 err = m->op->show(m, p);
200                 if (err < 0)
201                         break;
202                 if (unlikely(err))
203                         m->count = 0;
204                 if (unlikely(!m->count)) {
205                         p = m->op->next(m, p, &pos);
206                         m->index = pos;
207                         continue;
208                 }
209                 if (m->count < m->size)
210                         goto Fill;
211                 m->op->stop(m, p);
212                 kfree(m->buf);
213                 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
214                 if (!m->buf)
215                         goto Enomem;
216                 m->count = 0;
217                 m->version = 0;
218                 pos = m->index;
219                 p = m->op->start(m, &pos);
220         }
221         m->op->stop(m, p);
222         m->count = 0;
223         goto Done;
224 Fill:
225         /* they want more? let's try to get some more */
226         while (m->count < size) {
227                 size_t offs = m->count;
228                 loff_t next = pos;
229                 p = m->op->next(m, p, &next);
230                 if (!p || IS_ERR(p)) {
231                         err = PTR_ERR(p);
232                         break;
233                 }
234                 err = m->op->show(m, p);
235                 if (m->count == m->size || err) {
236                         m->count = offs;
237                         if (likely(err <= 0))
238                                 break;
239                 }
240                 pos = next;
241         }
242         m->op->stop(m, p);
243         n = min(m->count, size);
244         err = copy_to_user(buf, m->buf, n);
245         if (err)
246                 goto Efault;
247         copied += n;
248         m->count -= n;
249         if (m->count)
250                 m->from = n;
251         else
252                 pos++;
253         m->index = pos;
254 Done:
255         if (!copied)
256                 copied = err;
257         else {
258                 *ppos += copied;
259                 m->read_pos += copied;
260         }
261         file->f_version = m->version;
262         mutex_unlock(&m->lock);
263         return copied;
264 Enomem:
265         err = -ENOMEM;
266         goto Done;
267 Efault:
268         err = -EFAULT;
269         goto Done;
270 }
271 EXPORT_SYMBOL(seq_read);
272
273 /**
274  *      seq_lseek -     ->llseek() method for sequential files.
275  *      @file: the file in question
276  *      @offset: new position
277  *      @origin: 0 for absolute, 1 for relative position
278  *
279  *      Ready-made ->f_op->llseek()
280  */
281 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
282 {
283         struct seq_file *m = (struct seq_file *)file->private_data;
284         loff_t retval = -EINVAL;
285
286         mutex_lock(&m->lock);
287         m->version = file->f_version;
288         switch (origin) {
289                 case 1:
290                         offset += file->f_pos;
291                 case 0:
292                         if (offset < 0)
293                                 break;
294                         retval = offset;
295                         if (offset != m->read_pos) {
296                                 while ((retval=traverse(m, offset)) == -EAGAIN)
297                                         ;
298                                 if (retval) {
299                                         /* with extreme prejudice... */
300                                         file->f_pos = 0;
301                                         m->read_pos = 0;
302                                         m->version = 0;
303                                         m->index = 0;
304                                         m->count = 0;
305                                 } else {
306                                         m->read_pos = offset;
307                                         retval = file->f_pos = offset;
308                                 }
309                         }
310         }
311         file->f_version = m->version;
312         mutex_unlock(&m->lock);
313         return retval;
314 }
315 EXPORT_SYMBOL(seq_lseek);
316
317 /**
318  *      seq_release -   free the structures associated with sequential file.
319  *      @file: file in question
320  *      @inode: file->f_path.dentry->d_inode
321  *
322  *      Frees the structures associated with sequential file; can be used
323  *      as ->f_op->release() if you don't have private data to destroy.
324  */
325 int seq_release(struct inode *inode, struct file *file)
326 {
327         struct seq_file *m = (struct seq_file *)file->private_data;
328         kfree(m->buf);
329         kfree(m);
330         return 0;
331 }
332 EXPORT_SYMBOL(seq_release);
333
334 /**
335  *      seq_escape -    print string into buffer, escaping some characters
336  *      @m:     target buffer
337  *      @s:     string
338  *      @esc:   set of characters that need escaping
339  *
340  *      Puts string into buffer, replacing each occurrence of character from
341  *      @esc with usual octal escape.  Returns 0 in case of success, -1 - in
342  *      case of overflow.
343  */
344 int seq_escape(struct seq_file *m, const char *s, const char *esc)
345 {
346         char *end = m->buf + m->size;
347         char *p;
348         char c;
349
350         for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
351                 if (!strchr(esc, c)) {
352                         *p++ = c;
353                         continue;
354                 }
355                 if (p + 3 < end) {
356                         *p++ = '\\';
357                         *p++ = '0' + ((c & 0300) >> 6);
358                         *p++ = '0' + ((c & 070) >> 3);
359                         *p++ = '0' + (c & 07);
360                         continue;
361                 }
362                 m->count = m->size;
363                 return -1;
364         }
365         m->count = p - m->buf;
366         return 0;
367 }
368 EXPORT_SYMBOL(seq_escape);
369
370 int seq_printf(struct seq_file *m, const char *f, ...)
371 {
372         va_list args;
373         int len;
374
375         if (m->count < m->size) {
376                 va_start(args, f);
377                 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
378                 va_end(args);
379                 if (m->count + len < m->size) {
380                         m->count += len;
381                         return 0;
382                 }
383         }
384         m->count = m->size;
385         return -1;
386 }
387 EXPORT_SYMBOL(seq_printf);
388
389 static char *mangle_path(char *s, char *p, char *esc)
390 {
391         while (s <= p) {
392                 char c = *p++;
393                 if (!c) {
394                         return s;
395                 } else if (!strchr(esc, c)) {
396                         *s++ = c;
397                 } else if (s + 4 > p) {
398                         break;
399                 } else {
400                         *s++ = '\\';
401                         *s++ = '0' + ((c & 0300) >> 6);
402                         *s++ = '0' + ((c & 070) >> 3);
403                         *s++ = '0' + (c & 07);
404                 }
405         }
406         return NULL;
407 }
408
409 /*
410  * return the absolute path of 'dentry' residing in mount 'mnt'.
411  */
412 int seq_path(struct seq_file *m, struct path *path, char *esc)
413 {
414         if (m->count < m->size) {
415                 char *s = m->buf + m->count;
416                 char *p = d_path(path, s, m->size - m->count);
417                 if (!IS_ERR(p)) {
418                         s = mangle_path(s, p, esc);
419                         if (s) {
420                                 p = m->buf + m->count;
421                                 m->count = s - m->buf;
422                                 return s - p;
423                         }
424                 }
425         }
426         m->count = m->size;
427         return -1;
428 }
429 EXPORT_SYMBOL(seq_path);
430
431 /*
432  * Same as seq_path, but relative to supplied root.
433  *
434  * root may be changed, see __d_path().
435  */
436 int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
437                   char *esc)
438 {
439         int err = -ENAMETOOLONG;
440         if (m->count < m->size) {
441                 char *s = m->buf + m->count;
442                 char *p;
443
444                 spin_lock(&dcache_lock);
445                 p = __d_path(path, root, s, m->size - m->count);
446                 spin_unlock(&dcache_lock);
447                 err = PTR_ERR(p);
448                 if (!IS_ERR(p)) {
449                         s = mangle_path(s, p, esc);
450                         if (s) {
451                                 p = m->buf + m->count;
452                                 m->count = s - m->buf;
453                                 return 0;
454                         }
455                 }
456         }
457         m->count = m->size;
458         return err;
459 }
460
461 /*
462  * returns the path of the 'dentry' from the root of its filesystem.
463  */
464 int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
465 {
466         if (m->count < m->size) {
467                 char *s = m->buf + m->count;
468                 char *p = dentry_path(dentry, s, m->size - m->count);
469                 if (!IS_ERR(p)) {
470                         s = mangle_path(s, p, esc);
471                         if (s) {
472                                 p = m->buf + m->count;
473                                 m->count = s - m->buf;
474                                 return s - p;
475                         }
476                 }
477         }
478         m->count = m->size;
479         return -1;
480 }
481
482 int seq_bitmap(struct seq_file *m, unsigned long *bits, unsigned int nr_bits)
483 {
484         size_t len = bitmap_scnprintf_len(nr_bits);
485
486         if (m->count + len < m->size) {
487                 bitmap_scnprintf(m->buf + m->count, m->size - m->count,
488                                  bits, nr_bits);
489                 m->count += len;
490                 return 0;
491         }
492         m->count = m->size;
493         return -1;
494 }
495
496 static void *single_start(struct seq_file *p, loff_t *pos)
497 {
498         return NULL + (*pos == 0);
499 }
500
501 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
502 {
503         ++*pos;
504         return NULL;
505 }
506
507 static void single_stop(struct seq_file *p, void *v)
508 {
509 }
510
511 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
512                 void *data)
513 {
514         struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
515         int res = -ENOMEM;
516
517         if (op) {
518                 op->start = single_start;
519                 op->next = single_next;
520                 op->stop = single_stop;
521                 op->show = show;
522                 res = seq_open(file, op);
523                 if (!res)
524                         ((struct seq_file *)file->private_data)->private = data;
525                 else
526                         kfree(op);
527         }
528         return res;
529 }
530 EXPORT_SYMBOL(single_open);
531
532 int single_release(struct inode *inode, struct file *file)
533 {
534         const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
535         int res = seq_release(inode, file);
536         kfree(op);
537         return res;
538 }
539 EXPORT_SYMBOL(single_release);
540
541 int seq_release_private(struct inode *inode, struct file *file)
542 {
543         struct seq_file *seq = file->private_data;
544
545         kfree(seq->private);
546         seq->private = NULL;
547         return seq_release(inode, file);
548 }
549 EXPORT_SYMBOL(seq_release_private);
550
551 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
552                 int psize)
553 {
554         int rc;
555         void *private;
556         struct seq_file *seq;
557
558         private = kzalloc(psize, GFP_KERNEL);
559         if (private == NULL)
560                 goto out;
561
562         rc = seq_open(f, ops);
563         if (rc < 0)
564                 goto out_free;
565
566         seq = f->private_data;
567         seq->private = private;
568         return private;
569
570 out_free:
571         kfree(private);
572 out:
573         return NULL;
574 }
575 EXPORT_SYMBOL(__seq_open_private);
576
577 int seq_open_private(struct file *filp, const struct seq_operations *ops,
578                 int psize)
579 {
580         return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
581 }
582 EXPORT_SYMBOL(seq_open_private);
583
584 int seq_putc(struct seq_file *m, char c)
585 {
586         if (m->count < m->size) {
587                 m->buf[m->count++] = c;
588                 return 0;
589         }
590         return -1;
591 }
592 EXPORT_SYMBOL(seq_putc);
593
594 int seq_puts(struct seq_file *m, const char *s)
595 {
596         int len = strlen(s);
597         if (m->count + len < m->size) {
598                 memcpy(m->buf + m->count, s, len);
599                 m->count += len;
600                 return 0;
601         }
602         m->count = m->size;
603         return -1;
604 }
605 EXPORT_SYMBOL(seq_puts);
606
607 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
608 {
609         struct list_head *lh;
610
611         list_for_each(lh, head)
612                 if (pos-- == 0)
613                         return lh;
614
615         return NULL;
616 }
617
618 EXPORT_SYMBOL(seq_list_start);
619
620 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
621 {
622         if (!pos)
623                 return head;
624
625         return seq_list_start(head, pos - 1);
626 }
627
628 EXPORT_SYMBOL(seq_list_start_head);
629
630 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
631 {
632         struct list_head *lh;
633
634         lh = ((struct list_head *)v)->next;
635         ++*ppos;
636         return lh == head ? NULL : lh;
637 }
638
639 EXPORT_SYMBOL(seq_list_next);