1 // SPDX-License-Identifier: GPL-2.0
5 * helper functions for making synthetic files from sequences of records.
6 * initial implementation -- AV, Oct 2001.
9 #include <linux/cache.h>
11 #include <linux/export.h>
12 #include <linux/seq_file.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 #include <linux/cred.h>
17 #include <linux/printk.h>
18 #include <linux/string_helpers.h>
20 #include <linux/uaccess.h>
23 static struct kmem_cache *seq_file_cache __ro_after_init;
25 static void seq_set_overflow(struct seq_file *m)
30 static void *seq_buf_alloc(unsigned long size)
32 if (unlikely(size > MAX_RW_COUNT))
35 return kvmalloc(size, GFP_KERNEL_ACCOUNT);
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
50 * Returning SEQ_SKIP means "discard this element and move on".
51 * Note: seq_open() will allocate a struct seq_file and store its
52 * pointer in @file->private_data. This pointer should not be modified.
54 int seq_open(struct file *file, const struct seq_operations *op)
58 WARN_ON(file->private_data);
60 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
64 file->private_data = p;
69 // No refcounting: the lifetime of 'p' is constrained
70 // to the lifetime of the file.
74 * Wrappers around seq_open(e.g. swaps_open) need to be
75 * aware of this. If they set f_version themselves, they
76 * should call seq_open first and then set f_version.
81 * seq_files support lseek() and pread(). They do not implement
82 * write() at all, but we clear FMODE_PWRITE here for historical
85 * If a client of seq_files a) implements file.write() and b) wishes to
86 * support pwrite() then that client will need to implement its own
87 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
89 file->f_mode &= ~FMODE_PWRITE;
92 EXPORT_SYMBOL(seq_open);
94 static int traverse(struct seq_file *m, loff_t offset)
102 m->count = m->from = 0;
107 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
111 p = m->op->start(m, &m->index);
116 error = m->op->show(m, p);
119 if (unlikely(error)) {
123 if (seq_has_overflowed(m))
125 p = m->op->next(m, p, &m->index);
126 if (pos + m->count > offset) {
127 m->from = offset - pos;
143 m->buf = seq_buf_alloc(m->size <<= 1);
144 return !m->buf ? -ENOMEM : -EAGAIN;
148 * seq_read - ->read() method for sequential files.
149 * @file: the file to read from
150 * @buf: the buffer to read to
151 * @size: the maximum number of bytes to read
152 * @ppos: the current position in the file
154 * Ready-made ->f_op->read()
156 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
158 struct seq_file *m = file->private_data;
164 mutex_lock(&m->lock);
167 * seq_file->op->..m_start/m_stop/m_next may do special actions
168 * or optimisations based on the file->f_version, so we want to
169 * pass the file->f_version to those methods.
171 * seq_file->version is just copy of f_version, and seq_file
172 * methods can treat it simply as file version.
173 * It is copied in first and copied out after all operations.
174 * It is convenient to have it as part of structure to avoid the
175 * need of passing another argument to all the seq_file methods.
177 m->version = file->f_version;
180 * if request is to read from zero offset, reset iterator to first
181 * record as it might have been already advanced by previous requests
189 /* Don't assume *ppos is where we left it */
190 if (unlikely(*ppos != m->read_pos)) {
191 while ((err = traverse(m, *ppos)) == -EAGAIN)
194 /* With prejudice... */
205 /* grab buffer if we didn't have one */
207 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
211 /* if not empty - flush it first */
213 n = min(m->count, size);
214 err = copy_to_user(buf, m->buf + m->from, n);
225 /* we need at least one record in buffer */
227 p = m->op->start(m, &m->index);
232 err = m->op->show(m, p);
237 if (unlikely(!m->count)) {
238 p = m->op->next(m, p, &m->index);
241 if (m->count < m->size)
246 m->buf = seq_buf_alloc(m->size <<= 1);
250 p = m->op->start(m, &m->index);
256 /* they want more? let's try to get some more */
258 size_t offs = m->count;
259 loff_t pos = m->index;
261 p = m->op->next(m, p, &m->index);
263 /* Buggy ->next function */
265 if (!p || IS_ERR(p)) {
269 if (m->count >= size)
271 err = m->op->show(m, p);
272 if (seq_has_overflowed(m) || err) {
274 if (likely(err <= 0))
279 n = min(m->count, size);
280 err = copy_to_user(buf, m->buf, n);
291 m->read_pos += copied;
293 file->f_version = m->version;
294 mutex_unlock(&m->lock);
303 EXPORT_SYMBOL(seq_read);
306 * seq_lseek - ->llseek() method for sequential files.
307 * @file: the file in question
308 * @offset: new position
309 * @whence: 0 for absolute, 1 for relative position
311 * Ready-made ->f_op->llseek()
313 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
315 struct seq_file *m = file->private_data;
316 loff_t retval = -EINVAL;
318 mutex_lock(&m->lock);
319 m->version = file->f_version;
322 offset += file->f_pos;
328 if (offset != m->read_pos) {
329 while ((retval = traverse(m, offset)) == -EAGAIN)
332 /* with extreme prejudice... */
339 m->read_pos = offset;
340 retval = file->f_pos = offset;
343 file->f_pos = offset;
346 file->f_version = m->version;
347 mutex_unlock(&m->lock);
350 EXPORT_SYMBOL(seq_lseek);
353 * seq_release - free the structures associated with sequential file.
354 * @file: file in question
357 * Frees the structures associated with sequential file; can be used
358 * as ->f_op->release() if you don't have private data to destroy.
360 int seq_release(struct inode *inode, struct file *file)
362 struct seq_file *m = file->private_data;
364 kmem_cache_free(seq_file_cache, m);
367 EXPORT_SYMBOL(seq_release);
370 * seq_escape - print string into buffer, escaping some characters
373 * @esc: set of characters that need escaping
375 * Puts string into buffer, replacing each occurrence of character from
376 * @esc with usual octal escape.
377 * Use seq_has_overflowed() to check for errors.
379 void seq_escape(struct seq_file *m, const char *s, const char *esc)
382 size_t size = seq_get_buf(m, &buf);
385 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
386 seq_commit(m, ret < size ? ret : -1);
388 EXPORT_SYMBOL(seq_escape);
390 void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
393 size_t size = seq_get_buf(m, &buf);
396 ret = string_escape_mem_ascii(src, isz, buf, size);
397 seq_commit(m, ret < size ? ret : -1);
399 EXPORT_SYMBOL(seq_escape_mem_ascii);
401 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
405 if (m->count < m->size) {
406 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
407 if (m->count + len < m->size) {
414 EXPORT_SYMBOL(seq_vprintf);
416 void seq_printf(struct seq_file *m, const char *f, ...)
421 seq_vprintf(m, f, args);
424 EXPORT_SYMBOL(seq_printf);
427 * mangle_path - mangle and copy path to buffer beginning
429 * @p: beginning of path in above buffer
430 * @esc: set of characters that need escaping
432 * Copy the path from @p to @s, replacing each occurrence of character from
433 * @esc with usual octal escape.
434 * Returns pointer past last written character in @s, or NULL in case of
437 char *mangle_path(char *s, const char *p, const char *esc)
443 } else if (!strchr(esc, c)) {
445 } else if (s + 4 > p) {
449 *s++ = '0' + ((c & 0300) >> 6);
450 *s++ = '0' + ((c & 070) >> 3);
451 *s++ = '0' + (c & 07);
456 EXPORT_SYMBOL(mangle_path);
459 * seq_path - seq_file interface to print a pathname
460 * @m: the seq_file handle
461 * @path: the struct path to print
462 * @esc: set of characters to escape in the output
464 * return the absolute path of 'path', as represented by the
465 * dentry / mnt pair in the path parameter.
467 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
470 size_t size = seq_get_buf(m, &buf);
474 char *p = d_path(path, buf, size);
476 char *end = mangle_path(buf, p, esc);
485 EXPORT_SYMBOL(seq_path);
488 * seq_file_path - seq_file interface to print a pathname of a file
489 * @m: the seq_file handle
490 * @file: the struct file to print
491 * @esc: set of characters to escape in the output
493 * return the absolute path to the file.
495 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
497 return seq_path(m, &file->f_path, esc);
499 EXPORT_SYMBOL(seq_file_path);
502 * Same as seq_path, but relative to supplied root.
504 int seq_path_root(struct seq_file *m, const struct path *path,
505 const struct path *root, const char *esc)
508 size_t size = seq_get_buf(m, &buf);
509 int res = -ENAMETOOLONG;
514 p = __d_path(path, root, buf, size);
519 char *end = mangle_path(buf, p, esc);
528 return res < 0 && res != -ENAMETOOLONG ? res : 0;
532 * returns the path of the 'dentry' from the root of its filesystem.
534 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
537 size_t size = seq_get_buf(m, &buf);
541 char *p = dentry_path(dentry, buf, size);
543 char *end = mangle_path(buf, p, esc);
552 EXPORT_SYMBOL(seq_dentry);
554 static void *single_start(struct seq_file *p, loff_t *pos)
556 return NULL + (*pos == 0);
559 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
565 static void single_stop(struct seq_file *p, void *v)
569 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
572 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
576 op->start = single_start;
577 op->next = single_next;
578 op->stop = single_stop;
580 res = seq_open(file, op);
582 ((struct seq_file *)file->private_data)->private = data;
588 EXPORT_SYMBOL(single_open);
590 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
591 void *data, size_t size)
593 char *buf = seq_buf_alloc(size);
597 ret = single_open(file, show, data);
602 ((struct seq_file *)file->private_data)->buf = buf;
603 ((struct seq_file *)file->private_data)->size = size;
606 EXPORT_SYMBOL(single_open_size);
608 int single_release(struct inode *inode, struct file *file)
610 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
611 int res = seq_release(inode, file);
615 EXPORT_SYMBOL(single_release);
617 int seq_release_private(struct inode *inode, struct file *file)
619 struct seq_file *seq = file->private_data;
623 return seq_release(inode, file);
625 EXPORT_SYMBOL(seq_release_private);
627 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
632 struct seq_file *seq;
634 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
638 rc = seq_open(f, ops);
642 seq = f->private_data;
643 seq->private = private;
651 EXPORT_SYMBOL(__seq_open_private);
653 int seq_open_private(struct file *filp, const struct seq_operations *ops,
656 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
658 EXPORT_SYMBOL(seq_open_private);
660 void seq_putc(struct seq_file *m, char c)
662 if (m->count >= m->size)
665 m->buf[m->count++] = c;
667 EXPORT_SYMBOL(seq_putc);
669 void seq_puts(struct seq_file *m, const char *s)
673 if (m->count + len >= m->size) {
677 memcpy(m->buf + m->count, s, len);
680 EXPORT_SYMBOL(seq_puts);
683 * A helper routine for putting decimal numbers without rich format of printf().
684 * only 'unsigned long long' is supported.
685 * @m: seq_file identifying the buffer to which data should be written
686 * @delimiter: a string which is printed before the number
688 * @width: a minimum field width
690 * This routine will put strlen(delimiter) + number into seq_filed.
691 * This routine is very quick when you show lots of numbers.
692 * In usual cases, it will be better to use seq_printf(). It's easier to read.
694 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
695 unsigned long long num, unsigned int width)
699 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
702 if (delimiter && delimiter[0]) {
703 if (delimiter[1] == 0)
704 seq_putc(m, delimiter[0]);
706 seq_puts(m, delimiter);
712 if (m->count + width >= m->size)
715 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
726 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
727 unsigned long long num)
729 return seq_put_decimal_ull_width(m, delimiter, num, 0);
731 EXPORT_SYMBOL(seq_put_decimal_ull);
734 * seq_put_hex_ll - put a number in hexadecimal notation
735 * @m: seq_file identifying the buffer to which data should be written
736 * @delimiter: a string which is printed before the number
738 * @width: a minimum field width
740 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
742 * This routine is very quick when you show lots of numbers.
743 * In usual cases, it will be better to use seq_printf(). It's easier to read.
745 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
746 unsigned long long v, unsigned int width)
751 if (delimiter && delimiter[0]) {
752 if (delimiter[1] == 0)
753 seq_putc(m, delimiter[0]);
755 seq_puts(m, delimiter);
758 /* If x is 0, the result of __builtin_clzll is undefined */
762 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
767 if (m->count + len > m->size) {
772 for (i = len - 1; i >= 0; i--) {
773 m->buf[m->count + i] = hex_asc[0xf & v];
779 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
783 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
786 if (delimiter && delimiter[0]) {
787 if (delimiter[1] == 0)
788 seq_putc(m, delimiter[0]);
790 seq_puts(m, delimiter);
793 if (m->count + 2 >= m->size)
797 m->buf[m->count++] = '-';
802 m->buf[m->count++] = num + '0';
806 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
816 EXPORT_SYMBOL(seq_put_decimal_ll);
819 * seq_write - write arbitrary data to buffer
820 * @seq: seq_file identifying the buffer to which data should be written
821 * @data: data address
822 * @len: number of bytes
824 * Return 0 on success, non-zero otherwise.
826 int seq_write(struct seq_file *seq, const void *data, size_t len)
828 if (seq->count + len < seq->size) {
829 memcpy(seq->buf + seq->count, data, len);
833 seq_set_overflow(seq);
836 EXPORT_SYMBOL(seq_write);
839 * seq_pad - write padding spaces to buffer
840 * @m: seq_file identifying the buffer to which data should be written
841 * @c: the byte to append after padding if non-zero
843 void seq_pad(struct seq_file *m, char c)
845 int size = m->pad_until - m->count;
847 if (size + m->count > m->size) {
851 memset(m->buf + m->count, ' ', size);
857 EXPORT_SYMBOL(seq_pad);
859 /* A complete analogue of print_hex_dump() */
860 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
861 int rowsize, int groupsize, const void *buf, size_t len,
865 int i, linelen, remaining = len;
870 if (rowsize != 16 && rowsize != 32)
873 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
874 linelen = min(remaining, rowsize);
875 remaining -= rowsize;
877 switch (prefix_type) {
878 case DUMP_PREFIX_ADDRESS:
879 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
881 case DUMP_PREFIX_OFFSET:
882 seq_printf(m, "%s%.8x: ", prefix_str, i);
885 seq_printf(m, "%s", prefix_str);
889 size = seq_get_buf(m, &buffer);
890 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
891 buffer, size, ascii);
892 seq_commit(m, ret < size ? ret : -1);
897 EXPORT_SYMBOL(seq_hex_dump);
899 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
901 struct list_head *lh;
903 list_for_each(lh, head)
909 EXPORT_SYMBOL(seq_list_start);
911 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
916 return seq_list_start(head, pos - 1);
918 EXPORT_SYMBOL(seq_list_start_head);
920 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
922 struct list_head *lh;
924 lh = ((struct list_head *)v)->next;
926 return lh == head ? NULL : lh;
928 EXPORT_SYMBOL(seq_list_next);
931 * seq_hlist_start - start an iteration of a hlist
932 * @head: the head of the hlist
933 * @pos: the start position of the sequence
935 * Called at seq_file->op->start().
937 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
939 struct hlist_node *node;
941 hlist_for_each(node, head)
946 EXPORT_SYMBOL(seq_hlist_start);
949 * seq_hlist_start_head - start an iteration of a hlist
950 * @head: the head of the hlist
951 * @pos: the start position of the sequence
953 * Called at seq_file->op->start(). Call this function if you want to
954 * print a header at the top of the output.
956 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
959 return SEQ_START_TOKEN;
961 return seq_hlist_start(head, pos - 1);
963 EXPORT_SYMBOL(seq_hlist_start_head);
966 * seq_hlist_next - move to the next position of the hlist
967 * @v: the current iterator
968 * @head: the head of the hlist
969 * @ppos: the current position
971 * Called at seq_file->op->next().
973 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
976 struct hlist_node *node = v;
979 if (v == SEQ_START_TOKEN)
984 EXPORT_SYMBOL(seq_hlist_next);
987 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
988 * @head: the head of the hlist
989 * @pos: the start position of the sequence
991 * Called at seq_file->op->start().
993 * This list-traversal primitive may safely run concurrently with
994 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
995 * as long as the traversal is guarded by rcu_read_lock().
997 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
1000 struct hlist_node *node;
1002 __hlist_for_each_rcu(node, head)
1007 EXPORT_SYMBOL(seq_hlist_start_rcu);
1010 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1011 * @head: the head of the hlist
1012 * @pos: the start position of the sequence
1014 * Called at seq_file->op->start(). Call this function if you want to
1015 * print a header at the top of the output.
1017 * This list-traversal primitive may safely run concurrently with
1018 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1019 * as long as the traversal is guarded by rcu_read_lock().
1021 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1025 return SEQ_START_TOKEN;
1027 return seq_hlist_start_rcu(head, pos - 1);
1029 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1032 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1033 * @v: the current iterator
1034 * @head: the head of the hlist
1035 * @ppos: the current position
1037 * Called at seq_file->op->next().
1039 * This list-traversal primitive may safely run concurrently with
1040 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1041 * as long as the traversal is guarded by rcu_read_lock().
1043 struct hlist_node *seq_hlist_next_rcu(void *v,
1044 struct hlist_head *head,
1047 struct hlist_node *node = v;
1050 if (v == SEQ_START_TOKEN)
1051 return rcu_dereference(head->first);
1053 return rcu_dereference(node->next);
1055 EXPORT_SYMBOL(seq_hlist_next_rcu);
1058 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1059 * @head: pointer to percpu array of struct hlist_heads
1060 * @cpu: pointer to cpu "cursor"
1061 * @pos: start position of sequence
1063 * Called at seq_file->op->start().
1066 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1068 struct hlist_node *node;
1070 for_each_possible_cpu(*cpu) {
1071 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1078 EXPORT_SYMBOL(seq_hlist_start_percpu);
1081 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1082 * @v: pointer to current hlist_node
1083 * @head: pointer to percpu array of struct hlist_heads
1084 * @cpu: pointer to cpu "cursor"
1085 * @pos: start position of sequence
1087 * Called at seq_file->op->next().
1090 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1091 int *cpu, loff_t *pos)
1093 struct hlist_node *node = v;
1100 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1101 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1102 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1104 if (!hlist_empty(bucket))
1105 return bucket->first;
1109 EXPORT_SYMBOL(seq_hlist_next_percpu);
1111 void __init seq_file_init(void)
1113 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);