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 return kvmalloc(size, GFP_KERNEL_ACCOUNT);
36 * seq_open - initialize sequential file
37 * @file: file we initialize
38 * @op: method table describing the sequence
40 * seq_open() sets @file, associating it with a sequence described
41 * by @op. @op->start() sets the iterator up and returns the first
42 * element of sequence. @op->stop() shuts it down. @op->next()
43 * returns the next element of sequence. @op->show() prints element
44 * into the buffer. In case of error ->start() and ->next() return
45 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
46 * returns 0 in case of success and negative number in case of error.
47 * Returning SEQ_SKIP means "discard this element and move on".
48 * Note: seq_open() will allocate a struct seq_file and store its
49 * pointer in @file->private_data. This pointer should not be modified.
51 int seq_open(struct file *file, const struct seq_operations *op)
55 WARN_ON(file->private_data);
57 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
61 file->private_data = p;
66 // No refcounting: the lifetime of 'p' is constrained
67 // to the lifetime of the file.
71 * Wrappers around seq_open(e.g. swaps_open) need to be
72 * aware of this. If they set f_version themselves, they
73 * should call seq_open first and then set f_version.
78 * seq_files support lseek() and pread(). They do not implement
79 * write() at all, but we clear FMODE_PWRITE here for historical
82 * If a client of seq_files a) implements file.write() and b) wishes to
83 * support pwrite() then that client will need to implement its own
84 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
86 file->f_mode &= ~FMODE_PWRITE;
89 EXPORT_SYMBOL(seq_open);
91 static int traverse(struct seq_file *m, loff_t offset)
99 m->count = m->from = 0;
104 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
108 p = m->op->start(m, &m->index);
113 error = m->op->show(m, p);
116 if (unlikely(error)) {
120 if (seq_has_overflowed(m))
122 p = m->op->next(m, p, &m->index);
123 if (pos + m->count > offset) {
124 m->from = offset - pos;
140 m->buf = seq_buf_alloc(m->size <<= 1);
141 return !m->buf ? -ENOMEM : -EAGAIN;
145 * seq_read - ->read() method for sequential files.
146 * @file: the file to read from
147 * @buf: the buffer to read to
148 * @size: the maximum number of bytes to read
149 * @ppos: the current position in the file
151 * Ready-made ->f_op->read()
153 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
155 struct seq_file *m = file->private_data;
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;
177 * if request is to read from zero offset, reset iterator to first
178 * record as it might have been already advanced by previous requests
186 /* Don't assume *ppos is where we left it */
187 if (unlikely(*ppos != m->read_pos)) {
188 while ((err = traverse(m, *ppos)) == -EAGAIN)
191 /* With prejudice... */
202 /* grab buffer if we didn't have one */
204 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
208 /* if not empty - flush it first */
210 n = min(m->count, size);
211 err = copy_to_user(buf, m->buf + m->from, n);
222 /* we need at least one record in buffer */
224 p = m->op->start(m, &m->index);
229 err = m->op->show(m, p);
234 if (unlikely(!m->count)) {
235 p = m->op->next(m, p, &m->index);
238 if (m->count < m->size)
243 m->buf = seq_buf_alloc(m->size <<= 1);
247 p = m->op->start(m, &m->index);
253 /* they want more? let's try to get some more */
255 size_t offs = m->count;
256 loff_t pos = m->index;
258 p = m->op->next(m, p, &m->index);
260 /* Buggy ->next function */
262 if (!p || IS_ERR(p)) {
266 if (m->count >= size)
268 err = m->op->show(m, p);
269 if (seq_has_overflowed(m) || err) {
271 if (likely(err <= 0))
276 n = min(m->count, size);
277 err = copy_to_user(buf, m->buf, n);
288 m->read_pos += copied;
290 file->f_version = m->version;
291 mutex_unlock(&m->lock);
300 EXPORT_SYMBOL(seq_read);
303 * seq_lseek - ->llseek() method for sequential files.
304 * @file: the file in question
305 * @offset: new position
306 * @whence: 0 for absolute, 1 for relative position
308 * Ready-made ->f_op->llseek()
310 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
312 struct seq_file *m = file->private_data;
313 loff_t retval = -EINVAL;
315 mutex_lock(&m->lock);
316 m->version = file->f_version;
319 offset += file->f_pos;
325 if (offset != m->read_pos) {
326 while ((retval = traverse(m, offset)) == -EAGAIN)
329 /* with extreme prejudice... */
336 m->read_pos = offset;
337 retval = file->f_pos = offset;
340 file->f_pos = offset;
343 file->f_version = m->version;
344 mutex_unlock(&m->lock);
347 EXPORT_SYMBOL(seq_lseek);
350 * seq_release - free the structures associated with sequential file.
351 * @file: file in question
354 * Frees the structures associated with sequential file; can be used
355 * as ->f_op->release() if you don't have private data to destroy.
357 int seq_release(struct inode *inode, struct file *file)
359 struct seq_file *m = file->private_data;
361 kmem_cache_free(seq_file_cache, m);
364 EXPORT_SYMBOL(seq_release);
367 * seq_escape - print string into buffer, escaping some characters
370 * @esc: set of characters that need escaping
372 * Puts string into buffer, replacing each occurrence of character from
373 * @esc with usual octal escape.
374 * Use seq_has_overflowed() to check for errors.
376 void seq_escape(struct seq_file *m, const char *s, const char *esc)
379 size_t size = seq_get_buf(m, &buf);
382 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
383 seq_commit(m, ret < size ? ret : -1);
385 EXPORT_SYMBOL(seq_escape);
387 void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
390 size_t size = seq_get_buf(m, &buf);
393 ret = string_escape_mem_ascii(src, isz, buf, size);
394 seq_commit(m, ret < size ? ret : -1);
396 EXPORT_SYMBOL(seq_escape_mem_ascii);
398 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
402 if (m->count < m->size) {
403 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
404 if (m->count + len < m->size) {
411 EXPORT_SYMBOL(seq_vprintf);
413 void seq_printf(struct seq_file *m, const char *f, ...)
418 seq_vprintf(m, f, args);
421 EXPORT_SYMBOL(seq_printf);
424 * mangle_path - mangle and copy path to buffer beginning
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
434 char *mangle_path(char *s, const char *p, const char *esc)
440 } else if (!strchr(esc, c)) {
442 } else if (s + 4 > p) {
446 *s++ = '0' + ((c & 0300) >> 6);
447 *s++ = '0' + ((c & 070) >> 3);
448 *s++ = '0' + (c & 07);
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)
467 size_t size = seq_get_buf(m, &buf);
471 char *p = d_path(path, buf, size);
473 char *end = mangle_path(buf, p, esc);
482 EXPORT_SYMBOL(seq_path);
485 * seq_file_path - seq_file interface to print a pathname of a file
486 * @m: the seq_file handle
487 * @file: the struct file to print
488 * @esc: set of characters to escape in the output
490 * return the absolute path to the file.
492 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
494 return seq_path(m, &file->f_path, esc);
496 EXPORT_SYMBOL(seq_file_path);
499 * Same as seq_path, but relative to supplied root.
501 int seq_path_root(struct seq_file *m, const struct path *path,
502 const struct path *root, const char *esc)
505 size_t size = seq_get_buf(m, &buf);
506 int res = -ENAMETOOLONG;
511 p = __d_path(path, root, buf, size);
516 char *end = mangle_path(buf, p, esc);
525 return res < 0 && res != -ENAMETOOLONG ? res : 0;
529 * returns the path of the 'dentry' from the root of its filesystem.
531 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
534 size_t size = seq_get_buf(m, &buf);
538 char *p = dentry_path(dentry, buf, size);
540 char *end = mangle_path(buf, p, esc);
549 EXPORT_SYMBOL(seq_dentry);
551 static void *single_start(struct seq_file *p, loff_t *pos)
553 return NULL + (*pos == 0);
556 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
562 static void single_stop(struct seq_file *p, void *v)
566 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
569 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
573 op->start = single_start;
574 op->next = single_next;
575 op->stop = single_stop;
577 res = seq_open(file, op);
579 ((struct seq_file *)file->private_data)->private = data;
585 EXPORT_SYMBOL(single_open);
587 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
588 void *data, size_t size)
590 char *buf = seq_buf_alloc(size);
594 ret = single_open(file, show, data);
599 ((struct seq_file *)file->private_data)->buf = buf;
600 ((struct seq_file *)file->private_data)->size = size;
603 EXPORT_SYMBOL(single_open_size);
605 int single_release(struct inode *inode, struct file *file)
607 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
608 int res = seq_release(inode, file);
612 EXPORT_SYMBOL(single_release);
614 int seq_release_private(struct inode *inode, struct file *file)
616 struct seq_file *seq = file->private_data;
620 return seq_release(inode, file);
622 EXPORT_SYMBOL(seq_release_private);
624 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
629 struct seq_file *seq;
631 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
635 rc = seq_open(f, ops);
639 seq = f->private_data;
640 seq->private = private;
648 EXPORT_SYMBOL(__seq_open_private);
650 int seq_open_private(struct file *filp, const struct seq_operations *ops,
653 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
655 EXPORT_SYMBOL(seq_open_private);
657 void seq_putc(struct seq_file *m, char c)
659 if (m->count >= m->size)
662 m->buf[m->count++] = c;
664 EXPORT_SYMBOL(seq_putc);
666 void seq_puts(struct seq_file *m, const char *s)
670 if (m->count + len >= m->size) {
674 memcpy(m->buf + m->count, s, len);
677 EXPORT_SYMBOL(seq_puts);
680 * A helper routine for putting decimal numbers without rich format of printf().
681 * only 'unsigned long long' is supported.
682 * @m: seq_file identifying the buffer to which data should be written
683 * @delimiter: a string which is printed before the number
685 * @width: a minimum field width
687 * This routine will put strlen(delimiter) + number into seq_filed.
688 * This routine is very quick when you show lots of numbers.
689 * In usual cases, it will be better to use seq_printf(). It's easier to read.
691 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
692 unsigned long long num, unsigned int width)
696 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
699 if (delimiter && delimiter[0]) {
700 if (delimiter[1] == 0)
701 seq_putc(m, delimiter[0]);
703 seq_puts(m, delimiter);
709 if (m->count + width >= m->size)
712 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
723 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
724 unsigned long long num)
726 return seq_put_decimal_ull_width(m, delimiter, num, 0);
728 EXPORT_SYMBOL(seq_put_decimal_ull);
731 * seq_put_hex_ll - put a number in hexadecimal notation
732 * @m: seq_file identifying the buffer to which data should be written
733 * @delimiter: a string which is printed before the number
735 * @width: a minimum field width
737 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
739 * This routine is very quick when you show lots of numbers.
740 * In usual cases, it will be better to use seq_printf(). It's easier to read.
742 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
743 unsigned long long v, unsigned int width)
748 if (delimiter && delimiter[0]) {
749 if (delimiter[1] == 0)
750 seq_putc(m, delimiter[0]);
752 seq_puts(m, delimiter);
755 /* If x is 0, the result of __builtin_clzll is undefined */
759 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
764 if (m->count + len > m->size) {
769 for (i = len - 1; i >= 0; i--) {
770 m->buf[m->count + i] = hex_asc[0xf & v];
776 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
780 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
783 if (delimiter && delimiter[0]) {
784 if (delimiter[1] == 0)
785 seq_putc(m, delimiter[0]);
787 seq_puts(m, delimiter);
790 if (m->count + 2 >= m->size)
794 m->buf[m->count++] = '-';
799 m->buf[m->count++] = num + '0';
803 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
813 EXPORT_SYMBOL(seq_put_decimal_ll);
816 * seq_write - write arbitrary data to buffer
817 * @seq: seq_file identifying the buffer to which data should be written
818 * @data: data address
819 * @len: number of bytes
821 * Return 0 on success, non-zero otherwise.
823 int seq_write(struct seq_file *seq, const void *data, size_t len)
825 if (seq->count + len < seq->size) {
826 memcpy(seq->buf + seq->count, data, len);
830 seq_set_overflow(seq);
833 EXPORT_SYMBOL(seq_write);
836 * seq_pad - write padding spaces to buffer
837 * @m: seq_file identifying the buffer to which data should be written
838 * @c: the byte to append after padding if non-zero
840 void seq_pad(struct seq_file *m, char c)
842 int size = m->pad_until - m->count;
844 if (size + m->count > m->size) {
848 memset(m->buf + m->count, ' ', size);
854 EXPORT_SYMBOL(seq_pad);
856 /* A complete analogue of print_hex_dump() */
857 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
858 int rowsize, int groupsize, const void *buf, size_t len,
862 int i, linelen, remaining = len;
867 if (rowsize != 16 && rowsize != 32)
870 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
871 linelen = min(remaining, rowsize);
872 remaining -= rowsize;
874 switch (prefix_type) {
875 case DUMP_PREFIX_ADDRESS:
876 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
878 case DUMP_PREFIX_OFFSET:
879 seq_printf(m, "%s%.8x: ", prefix_str, i);
882 seq_printf(m, "%s", prefix_str);
886 size = seq_get_buf(m, &buffer);
887 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
888 buffer, size, ascii);
889 seq_commit(m, ret < size ? ret : -1);
894 EXPORT_SYMBOL(seq_hex_dump);
896 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
898 struct list_head *lh;
900 list_for_each(lh, head)
906 EXPORT_SYMBOL(seq_list_start);
908 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
913 return seq_list_start(head, pos - 1);
915 EXPORT_SYMBOL(seq_list_start_head);
917 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
919 struct list_head *lh;
921 lh = ((struct list_head *)v)->next;
923 return lh == head ? NULL : lh;
925 EXPORT_SYMBOL(seq_list_next);
928 * seq_hlist_start - start an iteration of a hlist
929 * @head: the head of the hlist
930 * @pos: the start position of the sequence
932 * Called at seq_file->op->start().
934 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
936 struct hlist_node *node;
938 hlist_for_each(node, head)
943 EXPORT_SYMBOL(seq_hlist_start);
946 * seq_hlist_start_head - start an iteration of a hlist
947 * @head: the head of the hlist
948 * @pos: the start position of the sequence
950 * Called at seq_file->op->start(). Call this function if you want to
951 * print a header at the top of the output.
953 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
956 return SEQ_START_TOKEN;
958 return seq_hlist_start(head, pos - 1);
960 EXPORT_SYMBOL(seq_hlist_start_head);
963 * seq_hlist_next - move to the next position of the hlist
964 * @v: the current iterator
965 * @head: the head of the hlist
966 * @ppos: the current position
968 * Called at seq_file->op->next().
970 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
973 struct hlist_node *node = v;
976 if (v == SEQ_START_TOKEN)
981 EXPORT_SYMBOL(seq_hlist_next);
984 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
985 * @head: the head of the hlist
986 * @pos: the start position of the sequence
988 * Called at seq_file->op->start().
990 * This list-traversal primitive may safely run concurrently with
991 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
992 * as long as the traversal is guarded by rcu_read_lock().
994 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
997 struct hlist_node *node;
999 __hlist_for_each_rcu(node, head)
1004 EXPORT_SYMBOL(seq_hlist_start_rcu);
1007 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1008 * @head: the head of the hlist
1009 * @pos: the start position of the sequence
1011 * Called at seq_file->op->start(). Call this function if you want to
1012 * print a header at the top of the output.
1014 * This list-traversal primitive may safely run concurrently with
1015 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1016 * as long as the traversal is guarded by rcu_read_lock().
1018 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1022 return SEQ_START_TOKEN;
1024 return seq_hlist_start_rcu(head, pos - 1);
1026 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1029 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1030 * @v: the current iterator
1031 * @head: the head of the hlist
1032 * @ppos: the current position
1034 * Called at seq_file->op->next().
1036 * This list-traversal primitive may safely run concurrently with
1037 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1038 * as long as the traversal is guarded by rcu_read_lock().
1040 struct hlist_node *seq_hlist_next_rcu(void *v,
1041 struct hlist_head *head,
1044 struct hlist_node *node = v;
1047 if (v == SEQ_START_TOKEN)
1048 return rcu_dereference(head->first);
1050 return rcu_dereference(node->next);
1052 EXPORT_SYMBOL(seq_hlist_next_rcu);
1055 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1056 * @head: pointer to percpu array of struct hlist_heads
1057 * @cpu: pointer to cpu "cursor"
1058 * @pos: start position of sequence
1060 * Called at seq_file->op->start().
1063 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1065 struct hlist_node *node;
1067 for_each_possible_cpu(*cpu) {
1068 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1075 EXPORT_SYMBOL(seq_hlist_start_percpu);
1078 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1079 * @v: pointer to current hlist_node
1080 * @head: pointer to percpu array of struct hlist_heads
1081 * @cpu: pointer to cpu "cursor"
1082 * @pos: start position of sequence
1084 * Called at seq_file->op->next().
1087 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1088 int *cpu, loff_t *pos)
1090 struct hlist_node *node = v;
1097 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1098 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1099 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1101 if (!hlist_empty(bucket))
1102 return bucket->first;
1106 EXPORT_SYMBOL(seq_hlist_next_percpu);
1108 void __init seq_file_init(void)
1110 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);