4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
9 #include <linux/export.h>
10 #include <linux/seq_file.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
16 #include <asm/uaccess.h>
21 * seq_files have a buffer which can may overflow. When this happens a larger
22 * buffer is reallocated and all the data will be printed again.
23 * The overflow state is true when m->count == m->size.
25 static bool seq_overflow(struct seq_file *m)
27 return m->count == m->size;
30 static void seq_set_overflow(struct seq_file *m)
35 static void *seq_buf_alloc(unsigned long size)
39 buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
40 if (!buf && size > PAGE_SIZE)
46 * seq_open - initialize sequential file
47 * @file: file we initialize
48 * @op: method table describing the sequence
50 * seq_open() sets @file, associating it with a sequence described
51 * by @op. @op->start() sets the iterator up and returns the first
52 * element of sequence. @op->stop() shuts it down. @op->next()
53 * returns the next element of sequence. @op->show() prints element
54 * into the buffer. In case of error ->start() and ->next() return
55 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
56 * returns 0 in case of success and negative number in case of error.
57 * Returning SEQ_SKIP means "discard this element and move on".
59 int seq_open(struct file *file, const struct seq_operations *op)
61 struct seq_file *p = file->private_data;
64 p = kmalloc(sizeof(*p), GFP_KERNEL);
67 file->private_data = p;
69 memset(p, 0, sizeof(*p));
73 // No refcounting: the lifetime of 'p' is constrained
74 // to the lifetime of the file.
78 * Wrappers around seq_open(e.g. swaps_open) need to be
79 * aware of this. If they set f_version themselves, they
80 * should call seq_open first and then set f_version.
85 * seq_files support lseek() and pread(). They do not implement
86 * write() at all, but we clear FMODE_PWRITE here for historical
89 * If a client of seq_files a) implements file.write() and b) wishes to
90 * support pwrite() then that client will need to implement its own
91 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
93 file->f_mode &= ~FMODE_PWRITE;
96 EXPORT_SYMBOL(seq_open);
98 static int traverse(struct seq_file *m, loff_t offset)
100 loff_t pos = 0, index;
106 m->count = m->from = 0;
112 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
116 p = m->op->start(m, &index);
121 error = m->op->show(m, p);
124 if (unlikely(error)) {
130 if (pos + m->count > offset) {
131 m->from = offset - pos;
143 p = m->op->next(m, p, &index);
153 m->buf = seq_buf_alloc(m->size <<= 1);
154 return !m->buf ? -ENOMEM : -EAGAIN;
158 * seq_read - ->read() method for sequential files.
159 * @file: the file to read from
160 * @buf: the buffer to read to
161 * @size: the maximum number of bytes to read
162 * @ppos: the current position in the file
164 * Ready-made ->f_op->read()
166 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
168 struct seq_file *m = file->private_data;
175 mutex_lock(&m->lock);
178 * seq_file->op->..m_start/m_stop/m_next may do special actions
179 * or optimisations based on the file->f_version, so we want to
180 * pass the file->f_version to those methods.
182 * seq_file->version is just copy of f_version, and seq_file
183 * methods can treat it simply as file version.
184 * It is copied in first and copied out after all operations.
185 * It is convenient to have it as part of structure to avoid the
186 * need of passing another argument to all the seq_file methods.
188 m->version = file->f_version;
190 /* Don't assume *ppos is where we left it */
191 if (unlikely(*ppos != m->read_pos)) {
192 while ((err = traverse(m, *ppos)) == -EAGAIN)
195 /* With prejudice... */
206 /* grab buffer if we didn't have one */
208 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
212 /* if not empty - flush it first */
214 n = min(m->count, size);
215 err = copy_to_user(buf, m->buf + m->from, n);
230 /* we need at least one record in buffer */
232 p = m->op->start(m, &pos);
237 err = m->op->show(m, p);
242 if (unlikely(!m->count)) {
243 p = m->op->next(m, p, &pos);
247 if (m->count < m->size)
252 m->buf = seq_buf_alloc(m->size <<= 1);
257 p = m->op->start(m, &pos);
263 /* they want more? let's try to get some more */
264 while (m->count < size) {
265 size_t offs = m->count;
267 p = m->op->next(m, p, &next);
268 if (!p || IS_ERR(p)) {
272 err = m->op->show(m, p);
273 if (seq_overflow(m) || err) {
275 if (likely(err <= 0))
281 n = min(m->count, size);
282 err = copy_to_user(buf, m->buf, n);
297 m->read_pos += copied;
299 file->f_version = m->version;
300 mutex_unlock(&m->lock);
309 EXPORT_SYMBOL(seq_read);
312 * seq_lseek - ->llseek() method for sequential files.
313 * @file: the file in question
314 * @offset: new position
315 * @whence: 0 for absolute, 1 for relative position
317 * Ready-made ->f_op->llseek()
319 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
321 struct seq_file *m = file->private_data;
322 loff_t retval = -EINVAL;
324 mutex_lock(&m->lock);
325 m->version = file->f_version;
328 offset += file->f_pos;
333 if (offset != m->read_pos) {
334 while ((retval = traverse(m, offset)) == -EAGAIN)
337 /* with extreme prejudice... */
344 m->read_pos = offset;
345 retval = file->f_pos = offset;
348 file->f_pos = offset;
351 file->f_version = m->version;
352 mutex_unlock(&m->lock);
355 EXPORT_SYMBOL(seq_lseek);
358 * seq_release - free the structures associated with sequential file.
359 * @file: file in question
362 * Frees the structures associated with sequential file; can be used
363 * as ->f_op->release() if you don't have private data to destroy.
365 int seq_release(struct inode *inode, struct file *file)
367 struct seq_file *m = file->private_data;
372 EXPORT_SYMBOL(seq_release);
375 * seq_escape - print string into buffer, escaping some characters
378 * @esc: set of characters that need escaping
380 * Puts string into buffer, replacing each occurrence of character from
381 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
384 int seq_escape(struct seq_file *m, const char *s, const char *esc)
386 char *end = m->buf + m->size;
390 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
391 if (!strchr(esc, c)) {
397 *p++ = '0' + ((c & 0300) >> 6);
398 *p++ = '0' + ((c & 070) >> 3);
399 *p++ = '0' + (c & 07);
405 m->count = p - m->buf;
408 EXPORT_SYMBOL(seq_escape);
410 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
414 if (m->count < m->size) {
415 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
416 if (m->count + len < m->size) {
424 EXPORT_SYMBOL(seq_vprintf);
426 int seq_printf(struct seq_file *m, const char *f, ...)
432 ret = seq_vprintf(m, f, args);
437 EXPORT_SYMBOL(seq_printf);
440 * mangle_path - mangle and copy path to buffer beginning
442 * @p: beginning of path in above buffer
443 * @esc: set of characters that need escaping
445 * Copy the path from @p to @s, replacing each occurrence of character from
446 * @esc with usual octal escape.
447 * Returns pointer past last written character in @s, or NULL in case of
450 char *mangle_path(char *s, const char *p, const char *esc)
456 } else if (!strchr(esc, c)) {
458 } else if (s + 4 > p) {
462 *s++ = '0' + ((c & 0300) >> 6);
463 *s++ = '0' + ((c & 070) >> 3);
464 *s++ = '0' + (c & 07);
469 EXPORT_SYMBOL(mangle_path);
472 * seq_path - seq_file interface to print a pathname
473 * @m: the seq_file handle
474 * @path: the struct path to print
475 * @esc: set of characters to escape in the output
477 * return the absolute path of 'path', as represented by the
478 * dentry / mnt pair in the path parameter.
480 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
483 size_t size = seq_get_buf(m, &buf);
487 char *p = d_path(path, buf, size);
489 char *end = mangle_path(buf, p, esc);
498 EXPORT_SYMBOL(seq_path);
501 * Same as seq_path, but relative to supplied root.
503 int seq_path_root(struct seq_file *m, const struct path *path,
504 const struct path *root, const char *esc)
507 size_t size = seq_get_buf(m, &buf);
508 int res = -ENAMETOOLONG;
513 p = __d_path(path, root, buf, size);
518 char *end = mangle_path(buf, p, esc);
527 return res < 0 && res != -ENAMETOOLONG ? res : 0;
531 * returns the path of the 'dentry' from the root of its filesystem.
533 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
536 size_t size = seq_get_buf(m, &buf);
540 char *p = dentry_path(dentry, buf, size);
542 char *end = mangle_path(buf, p, esc);
552 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
553 unsigned int nr_bits)
555 if (m->count < m->size) {
556 int len = bitmap_scnprintf(m->buf + m->count,
557 m->size - m->count, bits, nr_bits);
558 if (m->count + len < m->size) {
566 EXPORT_SYMBOL(seq_bitmap);
568 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
569 unsigned int nr_bits)
571 if (m->count < m->size) {
572 int len = bitmap_scnlistprintf(m->buf + m->count,
573 m->size - m->count, bits, nr_bits);
574 if (m->count + len < m->size) {
582 EXPORT_SYMBOL(seq_bitmap_list);
584 static void *single_start(struct seq_file *p, loff_t *pos)
586 return NULL + (*pos == 0);
589 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
595 static void single_stop(struct seq_file *p, void *v)
599 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
602 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
606 op->start = single_start;
607 op->next = single_next;
608 op->stop = single_stop;
610 res = seq_open(file, op);
612 ((struct seq_file *)file->private_data)->private = data;
618 EXPORT_SYMBOL(single_open);
620 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
621 void *data, size_t size)
623 char *buf = seq_buf_alloc(size);
627 ret = single_open(file, show, data);
632 ((struct seq_file *)file->private_data)->buf = buf;
633 ((struct seq_file *)file->private_data)->size = size;
636 EXPORT_SYMBOL(single_open_size);
638 int single_release(struct inode *inode, struct file *file)
640 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
641 int res = seq_release(inode, file);
645 EXPORT_SYMBOL(single_release);
647 int seq_release_private(struct inode *inode, struct file *file)
649 struct seq_file *seq = file->private_data;
653 return seq_release(inode, file);
655 EXPORT_SYMBOL(seq_release_private);
657 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
662 struct seq_file *seq;
664 private = kzalloc(psize, GFP_KERNEL);
668 rc = seq_open(f, ops);
672 seq = f->private_data;
673 seq->private = private;
681 EXPORT_SYMBOL(__seq_open_private);
683 int seq_open_private(struct file *filp, const struct seq_operations *ops,
686 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
688 EXPORT_SYMBOL(seq_open_private);
690 int seq_putc(struct seq_file *m, char c)
692 if (m->count < m->size) {
693 m->buf[m->count++] = c;
698 EXPORT_SYMBOL(seq_putc);
700 int seq_puts(struct seq_file *m, const char *s)
703 if (m->count + len < m->size) {
704 memcpy(m->buf + m->count, s, len);
711 EXPORT_SYMBOL(seq_puts);
714 * A helper routine for putting decimal numbers without rich format of printf().
715 * only 'unsigned long long' is supported.
716 * This routine will put one byte delimiter + number into seq_file.
717 * This routine is very quick when you show lots of numbers.
718 * In usual cases, it will be better to use seq_printf(). It's easier to read.
720 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
721 unsigned long long num)
725 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
729 m->buf[m->count++] = delimiter;
732 m->buf[m->count++] = num + '0';
736 len = num_to_str(m->buf + m->count, m->size - m->count, num);
745 EXPORT_SYMBOL(seq_put_decimal_ull);
747 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
751 if (m->count + 3 >= m->size) {
756 m->buf[m->count++] = delimiter;
760 return seq_put_decimal_ull(m, delimiter, num);
763 EXPORT_SYMBOL(seq_put_decimal_ll);
766 * seq_write - write arbitrary data to buffer
767 * @seq: seq_file identifying the buffer to which data should be written
768 * @data: data address
769 * @len: number of bytes
771 * Return 0 on success, non-zero otherwise.
773 int seq_write(struct seq_file *seq, const void *data, size_t len)
775 if (seq->count + len < seq->size) {
776 memcpy(seq->buf + seq->count, data, len);
780 seq_set_overflow(seq);
783 EXPORT_SYMBOL(seq_write);
786 * seq_pad - write padding spaces to buffer
787 * @m: seq_file identifying the buffer to which data should be written
788 * @c: the byte to append after padding if non-zero
790 void seq_pad(struct seq_file *m, char c)
792 int size = m->pad_until - m->count;
794 seq_printf(m, "%*s", size, "");
798 EXPORT_SYMBOL(seq_pad);
800 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
802 struct list_head *lh;
804 list_for_each(lh, head)
810 EXPORT_SYMBOL(seq_list_start);
812 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
817 return seq_list_start(head, pos - 1);
819 EXPORT_SYMBOL(seq_list_start_head);
821 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
823 struct list_head *lh;
825 lh = ((struct list_head *)v)->next;
827 return lh == head ? NULL : lh;
829 EXPORT_SYMBOL(seq_list_next);
832 * seq_hlist_start - start an iteration of a hlist
833 * @head: the head of the hlist
834 * @pos: the start position of the sequence
836 * Called at seq_file->op->start().
838 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
840 struct hlist_node *node;
842 hlist_for_each(node, head)
847 EXPORT_SYMBOL(seq_hlist_start);
850 * seq_hlist_start_head - start an iteration of a hlist
851 * @head: the head of the hlist
852 * @pos: the start position of the sequence
854 * Called at seq_file->op->start(). Call this function if you want to
855 * print a header at the top of the output.
857 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
860 return SEQ_START_TOKEN;
862 return seq_hlist_start(head, pos - 1);
864 EXPORT_SYMBOL(seq_hlist_start_head);
867 * seq_hlist_next - move to the next position of the hlist
868 * @v: the current iterator
869 * @head: the head of the hlist
870 * @ppos: the current position
872 * Called at seq_file->op->next().
874 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
877 struct hlist_node *node = v;
880 if (v == SEQ_START_TOKEN)
885 EXPORT_SYMBOL(seq_hlist_next);
888 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
889 * @head: the head of the hlist
890 * @pos: the start position of the sequence
892 * Called at seq_file->op->start().
894 * This list-traversal primitive may safely run concurrently with
895 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
896 * as long as the traversal is guarded by rcu_read_lock().
898 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
901 struct hlist_node *node;
903 __hlist_for_each_rcu(node, head)
908 EXPORT_SYMBOL(seq_hlist_start_rcu);
911 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
912 * @head: the head of the hlist
913 * @pos: the start position of the sequence
915 * Called at seq_file->op->start(). Call this function if you want to
916 * print a header at the top of the output.
918 * This list-traversal primitive may safely run concurrently with
919 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
920 * as long as the traversal is guarded by rcu_read_lock().
922 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
926 return SEQ_START_TOKEN;
928 return seq_hlist_start_rcu(head, pos - 1);
930 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
933 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
934 * @v: the current iterator
935 * @head: the head of the hlist
936 * @ppos: the current position
938 * Called at seq_file->op->next().
940 * This list-traversal primitive may safely run concurrently with
941 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
942 * as long as the traversal is guarded by rcu_read_lock().
944 struct hlist_node *seq_hlist_next_rcu(void *v,
945 struct hlist_head *head,
948 struct hlist_node *node = v;
951 if (v == SEQ_START_TOKEN)
952 return rcu_dereference(head->first);
954 return rcu_dereference(node->next);
956 EXPORT_SYMBOL(seq_hlist_next_rcu);
959 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
960 * @head: pointer to percpu array of struct hlist_heads
961 * @cpu: pointer to cpu "cursor"
962 * @pos: start position of sequence
964 * Called at seq_file->op->start().
967 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
969 struct hlist_node *node;
971 for_each_possible_cpu(*cpu) {
972 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
979 EXPORT_SYMBOL(seq_hlist_start_percpu);
982 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
983 * @v: pointer to current hlist_node
984 * @head: pointer to percpu array of struct hlist_heads
985 * @cpu: pointer to cpu "cursor"
986 * @pos: start position of sequence
988 * Called at seq_file->op->next().
991 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
992 int *cpu, loff_t *pos)
994 struct hlist_node *node = v;
1001 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1002 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1003 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1005 if (!hlist_empty(bucket))
1006 return bucket->first;
1010 EXPORT_SYMBOL(seq_hlist_next_percpu);