ALSA: seq: Fix snd_seq_call_port_info_ioctl in compat mode
[linux/fpc-iii.git] / fs / seq_file.c
blob3dd44db1465e9a55c7e7eb9cb0c39e5a6bc429ff
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;
331 } else {
332 file->f_pos = offset;
335 file->f_version = m->version;
336 mutex_unlock(&m->lock);
337 return retval;
339 EXPORT_SYMBOL(seq_lseek);
342 * seq_release - free the structures associated with sequential file.
343 * @file: file in question
344 * @inode: its inode
346 * Frees the structures associated with sequential file; can be used
347 * as ->f_op->release() if you don't have private data to destroy.
349 int seq_release(struct inode *inode, struct file *file)
351 struct seq_file *m = file->private_data;
352 kfree(m->buf);
353 kfree(m);
354 return 0;
356 EXPORT_SYMBOL(seq_release);
359 * seq_escape - print string into buffer, escaping some characters
360 * @m: target buffer
361 * @s: string
362 * @esc: set of characters that need escaping
364 * Puts string into buffer, replacing each occurrence of character from
365 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
366 * case of overflow.
368 int seq_escape(struct seq_file *m, const char *s, const char *esc)
370 char *end = m->buf + m->size;
371 char *p;
372 char c;
374 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
375 if (!strchr(esc, c)) {
376 *p++ = c;
377 continue;
379 if (p + 3 < end) {
380 *p++ = '\\';
381 *p++ = '0' + ((c & 0300) >> 6);
382 *p++ = '0' + ((c & 070) >> 3);
383 *p++ = '0' + (c & 07);
384 continue;
386 seq_set_overflow(m);
387 return -1;
389 m->count = p - m->buf;
390 return 0;
392 EXPORT_SYMBOL(seq_escape);
394 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
396 int len;
398 if (m->count < m->size) {
399 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
400 if (m->count + len < m->size) {
401 m->count += len;
402 return 0;
405 seq_set_overflow(m);
406 return -1;
408 EXPORT_SYMBOL(seq_vprintf);
410 int seq_printf(struct seq_file *m, const char *f, ...)
412 int ret;
413 va_list args;
415 va_start(args, f);
416 ret = seq_vprintf(m, f, args);
417 va_end(args);
419 return ret;
421 EXPORT_SYMBOL(seq_printf);
424 * mangle_path - mangle and copy path to buffer beginning
425 * @s: buffer start
426 * @p: beginning of path in above buffer
427 * @esc: set of characters that need escaping
429 * Copy the path from @p to @s, replacing each occurrence of character from
430 * @esc with usual octal escape.
431 * Returns pointer past last written character in @s, or NULL in case of
432 * failure.
434 char *mangle_path(char *s, const char *p, const char *esc)
436 while (s <= p) {
437 char c = *p++;
438 if (!c) {
439 return s;
440 } else if (!strchr(esc, c)) {
441 *s++ = c;
442 } else if (s + 4 > p) {
443 break;
444 } else {
445 *s++ = '\\';
446 *s++ = '0' + ((c & 0300) >> 6);
447 *s++ = '0' + ((c & 070) >> 3);
448 *s++ = '0' + (c & 07);
451 return NULL;
453 EXPORT_SYMBOL(mangle_path);
456 * seq_path - seq_file interface to print a pathname
457 * @m: the seq_file handle
458 * @path: the struct path to print
459 * @esc: set of characters to escape in the output
461 * return the absolute path of 'path', as represented by the
462 * dentry / mnt pair in the path parameter.
464 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
466 char *buf;
467 size_t size = seq_get_buf(m, &buf);
468 int res = -1;
470 if (size) {
471 char *p = d_path(path, buf, size);
472 if (!IS_ERR(p)) {
473 char *end = mangle_path(buf, p, esc);
474 if (end)
475 res = end - buf;
478 seq_commit(m, res);
480 return res;
482 EXPORT_SYMBOL(seq_path);
485 * Same as seq_path, but relative to supplied root.
487 int seq_path_root(struct seq_file *m, const struct path *path,
488 const struct path *root, const char *esc)
490 char *buf;
491 size_t size = seq_get_buf(m, &buf);
492 int res = -ENAMETOOLONG;
494 if (size) {
495 char *p;
497 p = __d_path(path, root, buf, size);
498 if (!p)
499 return SEQ_SKIP;
500 res = PTR_ERR(p);
501 if (!IS_ERR(p)) {
502 char *end = mangle_path(buf, p, esc);
503 if (end)
504 res = end - buf;
505 else
506 res = -ENAMETOOLONG;
509 seq_commit(m, res);
511 return res < 0 && res != -ENAMETOOLONG ? res : 0;
515 * returns the path of the 'dentry' from the root of its filesystem.
517 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
519 char *buf;
520 size_t size = seq_get_buf(m, &buf);
521 int res = -1;
523 if (size) {
524 char *p = dentry_path(dentry, buf, size);
525 if (!IS_ERR(p)) {
526 char *end = mangle_path(buf, p, esc);
527 if (end)
528 res = end - buf;
531 seq_commit(m, res);
533 return res;
536 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
537 unsigned int nr_bits)
539 if (m->count < m->size) {
540 int len = bitmap_scnprintf(m->buf + m->count,
541 m->size - m->count, bits, nr_bits);
542 if (m->count + len < m->size) {
543 m->count += len;
544 return 0;
547 seq_set_overflow(m);
548 return -1;
550 EXPORT_SYMBOL(seq_bitmap);
552 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
553 unsigned int nr_bits)
555 if (m->count < m->size) {
556 int len = bitmap_scnlistprintf(m->buf + m->count,
557 m->size - m->count, bits, nr_bits);
558 if (m->count + len < m->size) {
559 m->count += len;
560 return 0;
563 seq_set_overflow(m);
564 return -1;
566 EXPORT_SYMBOL(seq_bitmap_list);
568 static void *single_start(struct seq_file *p, loff_t *pos)
570 return NULL + (*pos == 0);
573 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
575 ++*pos;
576 return NULL;
579 static void single_stop(struct seq_file *p, void *v)
583 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
584 void *data)
586 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
587 int res = -ENOMEM;
589 if (op) {
590 op->start = single_start;
591 op->next = single_next;
592 op->stop = single_stop;
593 op->show = show;
594 res = seq_open(file, op);
595 if (!res)
596 ((struct seq_file *)file->private_data)->private = data;
597 else
598 kfree(op);
600 return res;
602 EXPORT_SYMBOL(single_open);
604 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
605 void *data, size_t size)
607 char *buf = kmalloc(size, GFP_KERNEL);
608 int ret;
609 if (!buf)
610 return -ENOMEM;
611 ret = single_open(file, show, data);
612 if (ret) {
613 kfree(buf);
614 return ret;
616 ((struct seq_file *)file->private_data)->buf = buf;
617 ((struct seq_file *)file->private_data)->size = size;
618 return 0;
620 EXPORT_SYMBOL(single_open_size);
622 int single_release(struct inode *inode, struct file *file)
624 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
625 int res = seq_release(inode, file);
626 kfree(op);
627 return res;
629 EXPORT_SYMBOL(single_release);
631 int seq_release_private(struct inode *inode, struct file *file)
633 struct seq_file *seq = file->private_data;
635 kfree(seq->private);
636 seq->private = NULL;
637 return seq_release(inode, file);
639 EXPORT_SYMBOL(seq_release_private);
641 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
642 int psize)
644 int rc;
645 void *private;
646 struct seq_file *seq;
648 private = kzalloc(psize, GFP_KERNEL);
649 if (private == NULL)
650 goto out;
652 rc = seq_open(f, ops);
653 if (rc < 0)
654 goto out_free;
656 seq = f->private_data;
657 seq->private = private;
658 return private;
660 out_free:
661 kfree(private);
662 out:
663 return NULL;
665 EXPORT_SYMBOL(__seq_open_private);
667 int seq_open_private(struct file *filp, const struct seq_operations *ops,
668 int psize)
670 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
672 EXPORT_SYMBOL(seq_open_private);
674 int seq_putc(struct seq_file *m, char c)
676 if (m->count < m->size) {
677 m->buf[m->count++] = c;
678 return 0;
680 return -1;
682 EXPORT_SYMBOL(seq_putc);
684 int seq_puts(struct seq_file *m, const char *s)
686 int len = strlen(s);
687 if (m->count + len < m->size) {
688 memcpy(m->buf + m->count, s, len);
689 m->count += len;
690 return 0;
692 seq_set_overflow(m);
693 return -1;
695 EXPORT_SYMBOL(seq_puts);
698 * A helper routine for putting decimal numbers without rich format of printf().
699 * only 'unsigned long long' is supported.
700 * This routine will put one byte delimiter + number into seq_file.
701 * This routine is very quick when you show lots of numbers.
702 * In usual cases, it will be better to use seq_printf(). It's easier to read.
704 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
705 unsigned long long num)
707 int len;
709 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
710 goto overflow;
712 if (delimiter)
713 m->buf[m->count++] = delimiter;
715 if (num < 10) {
716 m->buf[m->count++] = num + '0';
717 return 0;
720 len = num_to_str(m->buf + m->count, m->size - m->count, num);
721 if (!len)
722 goto overflow;
723 m->count += len;
724 return 0;
725 overflow:
726 seq_set_overflow(m);
727 return -1;
729 EXPORT_SYMBOL(seq_put_decimal_ull);
731 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
732 long long num)
734 if (num < 0) {
735 if (m->count + 3 >= m->size) {
736 seq_set_overflow(m);
737 return -1;
739 if (delimiter)
740 m->buf[m->count++] = delimiter;
741 num = -num;
742 delimiter = '-';
744 return seq_put_decimal_ull(m, delimiter, num);
747 EXPORT_SYMBOL(seq_put_decimal_ll);
750 * seq_write - write arbitrary data to buffer
751 * @seq: seq_file identifying the buffer to which data should be written
752 * @data: data address
753 * @len: number of bytes
755 * Return 0 on success, non-zero otherwise.
757 int seq_write(struct seq_file *seq, const void *data, size_t len)
759 if (seq->count + len < seq->size) {
760 memcpy(seq->buf + seq->count, data, len);
761 seq->count += len;
762 return 0;
764 seq_set_overflow(seq);
765 return -1;
767 EXPORT_SYMBOL(seq_write);
769 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
771 struct list_head *lh;
773 list_for_each(lh, head)
774 if (pos-- == 0)
775 return lh;
777 return NULL;
779 EXPORT_SYMBOL(seq_list_start);
781 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
783 if (!pos)
784 return head;
786 return seq_list_start(head, pos - 1);
788 EXPORT_SYMBOL(seq_list_start_head);
790 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
792 struct list_head *lh;
794 lh = ((struct list_head *)v)->next;
795 ++*ppos;
796 return lh == head ? NULL : lh;
798 EXPORT_SYMBOL(seq_list_next);
801 * seq_hlist_start - start an iteration of a hlist
802 * @head: the head of the hlist
803 * @pos: the start position of the sequence
805 * Called at seq_file->op->start().
807 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
809 struct hlist_node *node;
811 hlist_for_each(node, head)
812 if (pos-- == 0)
813 return node;
814 return NULL;
816 EXPORT_SYMBOL(seq_hlist_start);
819 * seq_hlist_start_head - start an iteration of a hlist
820 * @head: the head of the hlist
821 * @pos: the start position of the sequence
823 * Called at seq_file->op->start(). Call this function if you want to
824 * print a header at the top of the output.
826 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
828 if (!pos)
829 return SEQ_START_TOKEN;
831 return seq_hlist_start(head, pos - 1);
833 EXPORT_SYMBOL(seq_hlist_start_head);
836 * seq_hlist_next - move to the next position of the hlist
837 * @v: the current iterator
838 * @head: the head of the hlist
839 * @ppos: the current position
841 * Called at seq_file->op->next().
843 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
844 loff_t *ppos)
846 struct hlist_node *node = v;
848 ++*ppos;
849 if (v == SEQ_START_TOKEN)
850 return head->first;
851 else
852 return node->next;
854 EXPORT_SYMBOL(seq_hlist_next);
857 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
858 * @head: the head of the hlist
859 * @pos: the start position of the sequence
861 * Called at seq_file->op->start().
863 * This list-traversal primitive may safely run concurrently with
864 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
865 * as long as the traversal is guarded by rcu_read_lock().
867 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
868 loff_t pos)
870 struct hlist_node *node;
872 __hlist_for_each_rcu(node, head)
873 if (pos-- == 0)
874 return node;
875 return NULL;
877 EXPORT_SYMBOL(seq_hlist_start_rcu);
880 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
881 * @head: the head of the hlist
882 * @pos: the start position of the sequence
884 * Called at seq_file->op->start(). Call this function if you want to
885 * print a header at the top of the output.
887 * This list-traversal primitive may safely run concurrently with
888 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
889 * as long as the traversal is guarded by rcu_read_lock().
891 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
892 loff_t pos)
894 if (!pos)
895 return SEQ_START_TOKEN;
897 return seq_hlist_start_rcu(head, pos - 1);
899 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
902 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
903 * @v: the current iterator
904 * @head: the head of the hlist
905 * @ppos: the current position
907 * Called at seq_file->op->next().
909 * This list-traversal primitive may safely run concurrently with
910 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
911 * as long as the traversal is guarded by rcu_read_lock().
913 struct hlist_node *seq_hlist_next_rcu(void *v,
914 struct hlist_head *head,
915 loff_t *ppos)
917 struct hlist_node *node = v;
919 ++*ppos;
920 if (v == SEQ_START_TOKEN)
921 return rcu_dereference(head->first);
922 else
923 return rcu_dereference(node->next);
925 EXPORT_SYMBOL(seq_hlist_next_rcu);