ALSA: ml403-ac97cr: Remove redundant platform_set_drvdata()
[linux-2.6.git] / fs / seq_file.c
blob774c1eb7f1c926c73a7018ae47d9cfb03f75da76
1 /*
2 * linux/fs/seq_file.c
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
8 #include <linux/fs.h>
9 #include <linux/export.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
12 #include <linux/cred.h>
14 #include <asm/uaccess.h>
15 #include <asm/page.h>
19 * seq_files have a buffer which can may overflow. When this happens a larger
20 * buffer is reallocated and all the data will be printed again.
21 * The overflow state is true when m->count == m->size.
23 static bool seq_overflow(struct seq_file *m)
25 return m->count == m->size;
28 static void seq_set_overflow(struct seq_file *m)
30 m->count = m->size;
33 /**
34 * seq_open - initialize sequential file
35 * @file: file we initialize
36 * @op: method table describing the sequence
38 * seq_open() sets @file, associating it with a sequence described
39 * by @op. @op->start() sets the iterator up and returns the first
40 * element of sequence. @op->stop() shuts it down. @op->next()
41 * returns the next element of sequence. @op->show() prints element
42 * into the buffer. In case of error ->start() and ->next() return
43 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
44 * returns 0 in case of success and negative number in case of error.
45 * Returning SEQ_SKIP means "discard this element and move on".
47 int seq_open(struct file *file, const struct seq_operations *op)
49 struct seq_file *p = file->private_data;
51 if (!p) {
52 p = kmalloc(sizeof(*p), GFP_KERNEL);
53 if (!p)
54 return -ENOMEM;
55 file->private_data = p;
57 memset(p, 0, sizeof(*p));
58 mutex_init(&p->lock);
59 p->op = op;
60 #ifdef CONFIG_USER_NS
61 p->user_ns = file->f_cred->user_ns;
62 #endif
65 * Wrappers around seq_open(e.g. swaps_open) need to be
66 * aware of this. If they set f_version themselves, they
67 * should call seq_open first and then set f_version.
69 file->f_version = 0;
72 * seq_files support lseek() and pread(). They do not implement
73 * write() at all, but we clear FMODE_PWRITE here for historical
74 * reasons.
76 * If a client of seq_files a) implements file.write() and b) wishes to
77 * support pwrite() then that client will need to implement its own
78 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
80 file->f_mode &= ~FMODE_PWRITE;
81 return 0;
83 EXPORT_SYMBOL(seq_open);
85 static int traverse(struct seq_file *m, loff_t offset)
87 loff_t pos = 0, index;
88 int error = 0;
89 void *p;
91 m->version = 0;
92 index = 0;
93 m->count = m->from = 0;
94 if (!offset) {
95 m->index = index;
96 return 0;
98 if (!m->buf) {
99 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
100 if (!m->buf)
101 return -ENOMEM;
103 p = m->op->start(m, &index);
104 while (p) {
105 error = PTR_ERR(p);
106 if (IS_ERR(p))
107 break;
108 error = m->op->show(m, p);
109 if (error < 0)
110 break;
111 if (unlikely(error)) {
112 error = 0;
113 m->count = 0;
115 if (seq_overflow(m))
116 goto Eoverflow;
117 if (pos + m->count > offset) {
118 m->from = offset - pos;
119 m->count -= m->from;
120 m->index = index;
121 break;
123 pos += m->count;
124 m->count = 0;
125 if (pos == offset) {
126 index++;
127 m->index = index;
128 break;
130 p = m->op->next(m, p, &index);
132 m->op->stop(m, p);
133 m->index = index;
134 return error;
136 Eoverflow:
137 m->op->stop(m, p);
138 kfree(m->buf);
139 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
140 return !m->buf ? -ENOMEM : -EAGAIN;
144 * seq_read - ->read() method for sequential files.
145 * @file: the file to read from
146 * @buf: the buffer to read to
147 * @size: the maximum number of bytes to read
148 * @ppos: the current position in the file
150 * Ready-made ->f_op->read()
152 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
154 struct seq_file *m = file->private_data;
155 size_t copied = 0;
156 loff_t pos;
157 size_t n;
158 void *p;
159 int err = 0;
161 mutex_lock(&m->lock);
164 * seq_file->op->..m_start/m_stop/m_next may do special actions
165 * or optimisations based on the file->f_version, so we want to
166 * pass the file->f_version to those methods.
168 * seq_file->version is just copy of f_version, and seq_file
169 * methods can treat it simply as file version.
170 * It is copied in first and copied out after all operations.
171 * It is convenient to have it as part of structure to avoid the
172 * need of passing another argument to all the seq_file methods.
174 m->version = file->f_version;
176 /* Don't assume *ppos is where we left it */
177 if (unlikely(*ppos != m->read_pos)) {
178 while ((err = traverse(m, *ppos)) == -EAGAIN)
180 if (err) {
181 /* With prejudice... */
182 m->read_pos = 0;
183 m->version = 0;
184 m->index = 0;
185 m->count = 0;
186 goto Done;
187 } else {
188 m->read_pos = *ppos;
192 /* grab buffer if we didn't have one */
193 if (!m->buf) {
194 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
195 if (!m->buf)
196 goto Enomem;
198 /* if not empty - flush it first */
199 if (m->count) {
200 n = min(m->count, size);
201 err = copy_to_user(buf, m->buf + m->from, n);
202 if (err)
203 goto Efault;
204 m->count -= n;
205 m->from += n;
206 size -= n;
207 buf += n;
208 copied += n;
209 if (!m->count)
210 m->index++;
211 if (!size)
212 goto Done;
214 /* we need at least one record in buffer */
215 pos = m->index;
216 p = m->op->start(m, &pos);
217 while (1) {
218 err = PTR_ERR(p);
219 if (!p || IS_ERR(p))
220 break;
221 err = m->op->show(m, p);
222 if (err < 0)
223 break;
224 if (unlikely(err))
225 m->count = 0;
226 if (unlikely(!m->count)) {
227 p = m->op->next(m, p, &pos);
228 m->index = pos;
229 continue;
231 if (m->count < m->size)
232 goto Fill;
233 m->op->stop(m, p);
234 kfree(m->buf);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
236 if (!m->buf)
237 goto Enomem;
238 m->count = 0;
239 m->version = 0;
240 pos = m->index;
241 p = m->op->start(m, &pos);
243 m->op->stop(m, p);
244 m->count = 0;
245 goto Done;
246 Fill:
247 /* they want more? let's try to get some more */
248 while (m->count < size) {
249 size_t offs = m->count;
250 loff_t next = pos;
251 p = m->op->next(m, p, &next);
252 if (!p || IS_ERR(p)) {
253 err = PTR_ERR(p);
254 break;
256 err = m->op->show(m, p);
257 if (seq_overflow(m) || err) {
258 m->count = offs;
259 if (likely(err <= 0))
260 break;
262 pos = next;
264 m->op->stop(m, p);
265 n = min(m->count, size);
266 err = copy_to_user(buf, m->buf, n);
267 if (err)
268 goto Efault;
269 copied += n;
270 m->count -= n;
271 if (m->count)
272 m->from = n;
273 else
274 pos++;
275 m->index = pos;
276 Done:
277 if (!copied)
278 copied = err;
279 else {
280 *ppos += copied;
281 m->read_pos += copied;
283 file->f_version = m->version;
284 mutex_unlock(&m->lock);
285 return copied;
286 Enomem:
287 err = -ENOMEM;
288 goto Done;
289 Efault:
290 err = -EFAULT;
291 goto Done;
293 EXPORT_SYMBOL(seq_read);
296 * seq_lseek - ->llseek() method for sequential files.
297 * @file: the file in question
298 * @offset: new position
299 * @whence: 0 for absolute, 1 for relative position
301 * Ready-made ->f_op->llseek()
303 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
305 struct seq_file *m = file->private_data;
306 loff_t retval = -EINVAL;
308 mutex_lock(&m->lock);
309 m->version = file->f_version;
310 switch (whence) {
311 case SEEK_CUR:
312 offset += file->f_pos;
313 case SEEK_SET:
314 if (offset < 0)
315 break;
316 retval = offset;
317 if (offset != m->read_pos) {
318 while ((retval = traverse(m, offset)) == -EAGAIN)
320 if (retval) {
321 /* with extreme prejudice... */
322 file->f_pos = 0;
323 m->read_pos = 0;
324 m->version = 0;
325 m->index = 0;
326 m->count = 0;
327 } else {
328 m->read_pos = offset;
329 retval = file->f_pos = offset;
333 file->f_version = m->version;
334 mutex_unlock(&m->lock);
335 return retval;
337 EXPORT_SYMBOL(seq_lseek);
340 * seq_release - free the structures associated with sequential file.
341 * @file: file in question
342 * @inode: its inode
344 * Frees the structures associated with sequential file; can be used
345 * as ->f_op->release() if you don't have private data to destroy.
347 int seq_release(struct inode *inode, struct file *file)
349 struct seq_file *m = file->private_data;
350 kfree(m->buf);
351 kfree(m);
352 return 0;
354 EXPORT_SYMBOL(seq_release);
357 * seq_escape - print string into buffer, escaping some characters
358 * @m: target buffer
359 * @s: string
360 * @esc: set of characters that need escaping
362 * Puts string into buffer, replacing each occurrence of character from
363 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
364 * case of overflow.
366 int seq_escape(struct seq_file *m, const char *s, const char *esc)
368 char *end = m->buf + m->size;
369 char *p;
370 char c;
372 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
373 if (!strchr(esc, c)) {
374 *p++ = c;
375 continue;
377 if (p + 3 < end) {
378 *p++ = '\\';
379 *p++ = '0' + ((c & 0300) >> 6);
380 *p++ = '0' + ((c & 070) >> 3);
381 *p++ = '0' + (c & 07);
382 continue;
384 seq_set_overflow(m);
385 return -1;
387 m->count = p - m->buf;
388 return 0;
390 EXPORT_SYMBOL(seq_escape);
392 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
394 int len;
396 if (m->count < m->size) {
397 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
398 if (m->count + len < m->size) {
399 m->count += len;
400 return 0;
403 seq_set_overflow(m);
404 return -1;
406 EXPORT_SYMBOL(seq_vprintf);
408 int seq_printf(struct seq_file *m, const char *f, ...)
410 int ret;
411 va_list args;
413 va_start(args, f);
414 ret = seq_vprintf(m, f, args);
415 va_end(args);
417 return ret;
419 EXPORT_SYMBOL(seq_printf);
422 * mangle_path - mangle and copy path to buffer beginning
423 * @s: buffer start
424 * @p: beginning of path in above buffer
425 * @esc: set of characters that need escaping
427 * Copy the path from @p to @s, replacing each occurrence of character from
428 * @esc with usual octal escape.
429 * Returns pointer past last written character in @s, or NULL in case of
430 * failure.
432 char *mangle_path(char *s, const char *p, const char *esc)
434 while (s <= p) {
435 char c = *p++;
436 if (!c) {
437 return s;
438 } else if (!strchr(esc, c)) {
439 *s++ = c;
440 } else if (s + 4 > p) {
441 break;
442 } else {
443 *s++ = '\\';
444 *s++ = '0' + ((c & 0300) >> 6);
445 *s++ = '0' + ((c & 070) >> 3);
446 *s++ = '0' + (c & 07);
449 return NULL;
451 EXPORT_SYMBOL(mangle_path);
454 * seq_path - seq_file interface to print a pathname
455 * @m: the seq_file handle
456 * @path: the struct path to print
457 * @esc: set of characters to escape in the output
459 * return the absolute path of 'path', as represented by the
460 * dentry / mnt pair in the path parameter.
462 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
464 char *buf;
465 size_t size = seq_get_buf(m, &buf);
466 int res = -1;
468 if (size) {
469 char *p = d_path(path, buf, size);
470 if (!IS_ERR(p)) {
471 char *end = mangle_path(buf, p, esc);
472 if (end)
473 res = end - buf;
476 seq_commit(m, res);
478 return res;
480 EXPORT_SYMBOL(seq_path);
483 * Same as seq_path, but relative to supplied root.
485 int seq_path_root(struct seq_file *m, const struct path *path,
486 const struct path *root, const char *esc)
488 char *buf;
489 size_t size = seq_get_buf(m, &buf);
490 int res = -ENAMETOOLONG;
492 if (size) {
493 char *p;
495 p = __d_path(path, root, buf, size);
496 if (!p)
497 return SEQ_SKIP;
498 res = PTR_ERR(p);
499 if (!IS_ERR(p)) {
500 char *end = mangle_path(buf, p, esc);
501 if (end)
502 res = end - buf;
503 else
504 res = -ENAMETOOLONG;
507 seq_commit(m, res);
509 return res < 0 && res != -ENAMETOOLONG ? res : 0;
513 * returns the path of the 'dentry' from the root of its filesystem.
515 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
517 char *buf;
518 size_t size = seq_get_buf(m, &buf);
519 int res = -1;
521 if (size) {
522 char *p = dentry_path(dentry, buf, size);
523 if (!IS_ERR(p)) {
524 char *end = mangle_path(buf, p, esc);
525 if (end)
526 res = end - buf;
529 seq_commit(m, res);
531 return res;
534 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
535 unsigned int nr_bits)
537 if (m->count < m->size) {
538 int len = bitmap_scnprintf(m->buf + m->count,
539 m->size - m->count, bits, nr_bits);
540 if (m->count + len < m->size) {
541 m->count += len;
542 return 0;
545 seq_set_overflow(m);
546 return -1;
548 EXPORT_SYMBOL(seq_bitmap);
550 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
551 unsigned int nr_bits)
553 if (m->count < m->size) {
554 int len = bitmap_scnlistprintf(m->buf + m->count,
555 m->size - m->count, bits, nr_bits);
556 if (m->count + len < m->size) {
557 m->count += len;
558 return 0;
561 seq_set_overflow(m);
562 return -1;
564 EXPORT_SYMBOL(seq_bitmap_list);
566 static void *single_start(struct seq_file *p, loff_t *pos)
568 return NULL + (*pos == 0);
571 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
573 ++*pos;
574 return NULL;
577 static void single_stop(struct seq_file *p, void *v)
581 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
582 void *data)
584 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
585 int res = -ENOMEM;
587 if (op) {
588 op->start = single_start;
589 op->next = single_next;
590 op->stop = single_stop;
591 op->show = show;
592 res = seq_open(file, op);
593 if (!res)
594 ((struct seq_file *)file->private_data)->private = data;
595 else
596 kfree(op);
598 return res;
600 EXPORT_SYMBOL(single_open);
602 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
603 void *data, size_t size)
605 char *buf = kmalloc(size, GFP_KERNEL);
606 int ret;
607 if (!buf)
608 return -ENOMEM;
609 ret = single_open(file, show, data);
610 if (ret) {
611 kfree(buf);
612 return ret;
614 ((struct seq_file *)file->private_data)->buf = buf;
615 ((struct seq_file *)file->private_data)->size = size;
616 return 0;
618 EXPORT_SYMBOL(single_open_size);
620 int single_release(struct inode *inode, struct file *file)
622 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
623 int res = seq_release(inode, file);
624 kfree(op);
625 return res;
627 EXPORT_SYMBOL(single_release);
629 int seq_release_private(struct inode *inode, struct file *file)
631 struct seq_file *seq = file->private_data;
633 kfree(seq->private);
634 seq->private = NULL;
635 return seq_release(inode, file);
637 EXPORT_SYMBOL(seq_release_private);
639 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
640 int psize)
642 int rc;
643 void *private;
644 struct seq_file *seq;
646 private = kzalloc(psize, GFP_KERNEL);
647 if (private == NULL)
648 goto out;
650 rc = seq_open(f, ops);
651 if (rc < 0)
652 goto out_free;
654 seq = f->private_data;
655 seq->private = private;
656 return private;
658 out_free:
659 kfree(private);
660 out:
661 return NULL;
663 EXPORT_SYMBOL(__seq_open_private);
665 int seq_open_private(struct file *filp, const struct seq_operations *ops,
666 int psize)
668 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
670 EXPORT_SYMBOL(seq_open_private);
672 int seq_putc(struct seq_file *m, char c)
674 if (m->count < m->size) {
675 m->buf[m->count++] = c;
676 return 0;
678 return -1;
680 EXPORT_SYMBOL(seq_putc);
682 int seq_puts(struct seq_file *m, const char *s)
684 int len = strlen(s);
685 if (m->count + len < m->size) {
686 memcpy(m->buf + m->count, s, len);
687 m->count += len;
688 return 0;
690 seq_set_overflow(m);
691 return -1;
693 EXPORT_SYMBOL(seq_puts);
696 * A helper routine for putting decimal numbers without rich format of printf().
697 * only 'unsigned long long' is supported.
698 * This routine will put one byte delimiter + number into seq_file.
699 * This routine is very quick when you show lots of numbers.
700 * In usual cases, it will be better to use seq_printf(). It's easier to read.
702 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
703 unsigned long long num)
705 int len;
707 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
708 goto overflow;
710 if (delimiter)
711 m->buf[m->count++] = delimiter;
713 if (num < 10) {
714 m->buf[m->count++] = num + '0';
715 return 0;
718 len = num_to_str(m->buf + m->count, m->size - m->count, num);
719 if (!len)
720 goto overflow;
721 m->count += len;
722 return 0;
723 overflow:
724 seq_set_overflow(m);
725 return -1;
727 EXPORT_SYMBOL(seq_put_decimal_ull);
729 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
730 long long num)
732 if (num < 0) {
733 if (m->count + 3 >= m->size) {
734 seq_set_overflow(m);
735 return -1;
737 if (delimiter)
738 m->buf[m->count++] = delimiter;
739 num = -num;
740 delimiter = '-';
742 return seq_put_decimal_ull(m, delimiter, num);
745 EXPORT_SYMBOL(seq_put_decimal_ll);
748 * seq_write - write arbitrary data to buffer
749 * @seq: seq_file identifying the buffer to which data should be written
750 * @data: data address
751 * @len: number of bytes
753 * Return 0 on success, non-zero otherwise.
755 int seq_write(struct seq_file *seq, const void *data, size_t len)
757 if (seq->count + len < seq->size) {
758 memcpy(seq->buf + seq->count, data, len);
759 seq->count += len;
760 return 0;
762 seq_set_overflow(seq);
763 return -1;
765 EXPORT_SYMBOL(seq_write);
767 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
769 struct list_head *lh;
771 list_for_each(lh, head)
772 if (pos-- == 0)
773 return lh;
775 return NULL;
777 EXPORT_SYMBOL(seq_list_start);
779 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
781 if (!pos)
782 return head;
784 return seq_list_start(head, pos - 1);
786 EXPORT_SYMBOL(seq_list_start_head);
788 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
790 struct list_head *lh;
792 lh = ((struct list_head *)v)->next;
793 ++*ppos;
794 return lh == head ? NULL : lh;
796 EXPORT_SYMBOL(seq_list_next);
799 * seq_hlist_start - start an iteration of a hlist
800 * @head: the head of the hlist
801 * @pos: the start position of the sequence
803 * Called at seq_file->op->start().
805 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
807 struct hlist_node *node;
809 hlist_for_each(node, head)
810 if (pos-- == 0)
811 return node;
812 return NULL;
814 EXPORT_SYMBOL(seq_hlist_start);
817 * seq_hlist_start_head - start an iteration of a hlist
818 * @head: the head of the hlist
819 * @pos: the start position of the sequence
821 * Called at seq_file->op->start(). Call this function if you want to
822 * print a header at the top of the output.
824 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
826 if (!pos)
827 return SEQ_START_TOKEN;
829 return seq_hlist_start(head, pos - 1);
831 EXPORT_SYMBOL(seq_hlist_start_head);
834 * seq_hlist_next - move to the next position of the hlist
835 * @v: the current iterator
836 * @head: the head of the hlist
837 * @ppos: the current position
839 * Called at seq_file->op->next().
841 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
842 loff_t *ppos)
844 struct hlist_node *node = v;
846 ++*ppos;
847 if (v == SEQ_START_TOKEN)
848 return head->first;
849 else
850 return node->next;
852 EXPORT_SYMBOL(seq_hlist_next);
855 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
856 * @head: the head of the hlist
857 * @pos: the start position of the sequence
859 * Called at seq_file->op->start().
861 * This list-traversal primitive may safely run concurrently with
862 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
863 * as long as the traversal is guarded by rcu_read_lock().
865 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
866 loff_t pos)
868 struct hlist_node *node;
870 __hlist_for_each_rcu(node, head)
871 if (pos-- == 0)
872 return node;
873 return NULL;
875 EXPORT_SYMBOL(seq_hlist_start_rcu);
878 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
879 * @head: the head of the hlist
880 * @pos: the start position of the sequence
882 * Called at seq_file->op->start(). Call this function if you want to
883 * print a header at the top of the output.
885 * This list-traversal primitive may safely run concurrently with
886 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
887 * as long as the traversal is guarded by rcu_read_lock().
889 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
890 loff_t pos)
892 if (!pos)
893 return SEQ_START_TOKEN;
895 return seq_hlist_start_rcu(head, pos - 1);
897 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
900 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
901 * @v: the current iterator
902 * @head: the head of the hlist
903 * @ppos: the current position
905 * Called at seq_file->op->next().
907 * This list-traversal primitive may safely run concurrently with
908 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
909 * as long as the traversal is guarded by rcu_read_lock().
911 struct hlist_node *seq_hlist_next_rcu(void *v,
912 struct hlist_head *head,
913 loff_t *ppos)
915 struct hlist_node *node = v;
917 ++*ppos;
918 if (v == SEQ_START_TOKEN)
919 return rcu_dereference(head->first);
920 else
921 return rcu_dereference(node->next);
923 EXPORT_SYMBOL(seq_hlist_next_rcu);