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;
327 if (offset != m->read_pos) {
328 while ((retval = traverse(m, offset)) == -EAGAIN)
331 /* with extreme prejudice... */
338 m->read_pos = offset;
339 retval = file->f_pos = offset;
342 file->f_pos = offset;
345 file->f_version = m->version;
346 mutex_unlock(&m->lock);
349 EXPORT_SYMBOL(seq_lseek);
352 * seq_release - free the structures associated with sequential file.
353 * @file: file in question
356 * Frees the structures associated with sequential file; can be used
357 * as ->f_op->release() if you don't have private data to destroy.
359 int seq_release(struct inode *inode, struct file *file)
361 struct seq_file *m = file->private_data;
363 kmem_cache_free(seq_file_cache, m);
366 EXPORT_SYMBOL(seq_release);
369 * seq_escape - print string into buffer, escaping some characters
372 * @esc: set of characters that need escaping
374 * Puts string into buffer, replacing each occurrence of character from
375 * @esc with usual octal escape.
376 * Use seq_has_overflowed() to check for errors.
378 void seq_escape(struct seq_file *m, const char *s, const char *esc)
381 size_t size = seq_get_buf(m, &buf);
384 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
385 seq_commit(m, ret < size ? ret : -1);
387 EXPORT_SYMBOL(seq_escape);
389 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
393 if (m->count < m->size) {
394 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
395 if (m->count + len < m->size) {
402 EXPORT_SYMBOL(seq_vprintf);
404 void seq_printf(struct seq_file *m, const char *f, ...)
409 seq_vprintf(m, f, args);
412 EXPORT_SYMBOL(seq_printf);
415 * mangle_path - mangle and copy path to buffer beginning
417 * @p: beginning of path in above buffer
418 * @esc: set of characters that need escaping
420 * Copy the path from @p to @s, replacing each occurrence of character from
421 * @esc with usual octal escape.
422 * Returns pointer past last written character in @s, or NULL in case of
425 char *mangle_path(char *s, const char *p, const char *esc)
431 } else if (!strchr(esc, c)) {
433 } else if (s + 4 > p) {
437 *s++ = '0' + ((c & 0300) >> 6);
438 *s++ = '0' + ((c & 070) >> 3);
439 *s++ = '0' + (c & 07);
444 EXPORT_SYMBOL(mangle_path);
447 * seq_path - seq_file interface to print a pathname
448 * @m: the seq_file handle
449 * @path: the struct path to print
450 * @esc: set of characters to escape in the output
452 * return the absolute path of 'path', as represented by the
453 * dentry / mnt pair in the path parameter.
455 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
458 size_t size = seq_get_buf(m, &buf);
462 char *p = d_path(path, buf, size);
464 char *end = mangle_path(buf, p, esc);
473 EXPORT_SYMBOL(seq_path);
476 * seq_file_path - seq_file interface to print a pathname of a file
477 * @m: the seq_file handle
478 * @file: the struct file to print
479 * @esc: set of characters to escape in the output
481 * return the absolute path to the file.
483 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
485 return seq_path(m, &file->f_path, esc);
487 EXPORT_SYMBOL(seq_file_path);
490 * Same as seq_path, but relative to supplied root.
492 int seq_path_root(struct seq_file *m, const struct path *path,
493 const struct path *root, const char *esc)
496 size_t size = seq_get_buf(m, &buf);
497 int res = -ENAMETOOLONG;
502 p = __d_path(path, root, buf, size);
507 char *end = mangle_path(buf, p, esc);
516 return res < 0 && res != -ENAMETOOLONG ? res : 0;
520 * returns the path of the 'dentry' from the root of its filesystem.
522 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
525 size_t size = seq_get_buf(m, &buf);
529 char *p = dentry_path(dentry, buf, size);
531 char *end = mangle_path(buf, p, esc);
540 EXPORT_SYMBOL(seq_dentry);
542 static void *single_start(struct seq_file *p, loff_t *pos)
544 return NULL + (*pos == 0);
547 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
553 static void single_stop(struct seq_file *p, void *v)
557 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
560 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
564 op->start = single_start;
565 op->next = single_next;
566 op->stop = single_stop;
568 res = seq_open(file, op);
570 ((struct seq_file *)file->private_data)->private = data;
576 EXPORT_SYMBOL(single_open);
578 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
579 void *data, size_t size)
581 char *buf = seq_buf_alloc(size);
585 ret = single_open(file, show, data);
590 ((struct seq_file *)file->private_data)->buf = buf;
591 ((struct seq_file *)file->private_data)->size = size;
594 EXPORT_SYMBOL(single_open_size);
596 int single_release(struct inode *inode, struct file *file)
598 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
599 int res = seq_release(inode, file);
603 EXPORT_SYMBOL(single_release);
605 int seq_release_private(struct inode *inode, struct file *file)
607 struct seq_file *seq = file->private_data;
611 return seq_release(inode, file);
613 EXPORT_SYMBOL(seq_release_private);
615 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
620 struct seq_file *seq;
622 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
626 rc = seq_open(f, ops);
630 seq = f->private_data;
631 seq->private = private;
639 EXPORT_SYMBOL(__seq_open_private);
641 int seq_open_private(struct file *filp, const struct seq_operations *ops,
644 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
646 EXPORT_SYMBOL(seq_open_private);
648 void seq_putc(struct seq_file *m, char c)
650 if (m->count >= m->size)
653 m->buf[m->count++] = c;
655 EXPORT_SYMBOL(seq_putc);
657 void seq_puts(struct seq_file *m, const char *s)
661 if (m->count + len >= m->size) {
665 memcpy(m->buf + m->count, s, len);
668 EXPORT_SYMBOL(seq_puts);
671 * A helper routine for putting decimal numbers without rich format of printf().
672 * only 'unsigned long long' is supported.
673 * @m: seq_file identifying the buffer to which data should be written
674 * @delimiter: a string which is printed before the number
676 * @width: a minimum field width
678 * This routine will put strlen(delimiter) + number into seq_filed.
679 * This routine is very quick when you show lots of numbers.
680 * In usual cases, it will be better to use seq_printf(). It's easier to read.
682 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
683 unsigned long long num, unsigned int width)
687 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
690 if (delimiter && delimiter[0]) {
691 if (delimiter[1] == 0)
692 seq_putc(m, delimiter[0]);
694 seq_puts(m, delimiter);
700 if (m->count + width >= m->size)
703 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
714 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
715 unsigned long long num)
717 return seq_put_decimal_ull_width(m, delimiter, num, 0);
719 EXPORT_SYMBOL(seq_put_decimal_ull);
722 * seq_put_hex_ll - put a number in hexadecimal notation
723 * @m: seq_file identifying the buffer to which data should be written
724 * @delimiter: a string which is printed before the number
726 * @width: a minimum field width
728 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
730 * This routine is very quick when you show lots of numbers.
731 * In usual cases, it will be better to use seq_printf(). It's easier to read.
733 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
734 unsigned long long v, unsigned int width)
739 if (delimiter && delimiter[0]) {
740 if (delimiter[1] == 0)
741 seq_putc(m, delimiter[0]);
743 seq_puts(m, delimiter);
746 /* If x is 0, the result of __builtin_clzll is undefined */
750 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
755 if (m->count + len > m->size) {
760 for (i = len - 1; i >= 0; i--) {
761 m->buf[m->count + i] = hex_asc[0xf & v];
767 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
771 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
774 if (delimiter && delimiter[0]) {
775 if (delimiter[1] == 0)
776 seq_putc(m, delimiter[0]);
778 seq_puts(m, delimiter);
781 if (m->count + 2 >= m->size)
785 m->buf[m->count++] = '-';
790 m->buf[m->count++] = num + '0';
794 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
804 EXPORT_SYMBOL(seq_put_decimal_ll);
807 * seq_write - write arbitrary data to buffer
808 * @seq: seq_file identifying the buffer to which data should be written
809 * @data: data address
810 * @len: number of bytes
812 * Return 0 on success, non-zero otherwise.
814 int seq_write(struct seq_file *seq, const void *data, size_t len)
816 if (seq->count + len < seq->size) {
817 memcpy(seq->buf + seq->count, data, len);
821 seq_set_overflow(seq);
824 EXPORT_SYMBOL(seq_write);
827 * seq_pad - write padding spaces to buffer
828 * @m: seq_file identifying the buffer to which data should be written
829 * @c: the byte to append after padding if non-zero
831 void seq_pad(struct seq_file *m, char c)
833 int size = m->pad_until - m->count;
835 if (size + m->count > m->size) {
839 memset(m->buf + m->count, ' ', size);
845 EXPORT_SYMBOL(seq_pad);
847 /* A complete analogue of print_hex_dump() */
848 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
849 int rowsize, int groupsize, const void *buf, size_t len,
853 int i, linelen, remaining = len;
858 if (rowsize != 16 && rowsize != 32)
861 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
862 linelen = min(remaining, rowsize);
863 remaining -= rowsize;
865 switch (prefix_type) {
866 case DUMP_PREFIX_ADDRESS:
867 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
869 case DUMP_PREFIX_OFFSET:
870 seq_printf(m, "%s%.8x: ", prefix_str, i);
873 seq_printf(m, "%s", prefix_str);
877 size = seq_get_buf(m, &buffer);
878 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
879 buffer, size, ascii);
880 seq_commit(m, ret < size ? ret : -1);
885 EXPORT_SYMBOL(seq_hex_dump);
887 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
889 struct list_head *lh;
891 list_for_each(lh, head)
897 EXPORT_SYMBOL(seq_list_start);
899 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
904 return seq_list_start(head, pos - 1);
906 EXPORT_SYMBOL(seq_list_start_head);
908 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
910 struct list_head *lh;
912 lh = ((struct list_head *)v)->next;
914 return lh == head ? NULL : lh;
916 EXPORT_SYMBOL(seq_list_next);
919 * seq_hlist_start - start an iteration of a hlist
920 * @head: the head of the hlist
921 * @pos: the start position of the sequence
923 * Called at seq_file->op->start().
925 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
927 struct hlist_node *node;
929 hlist_for_each(node, head)
934 EXPORT_SYMBOL(seq_hlist_start);
937 * seq_hlist_start_head - start an iteration of a hlist
938 * @head: the head of the hlist
939 * @pos: the start position of the sequence
941 * Called at seq_file->op->start(). Call this function if you want to
942 * print a header at the top of the output.
944 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
947 return SEQ_START_TOKEN;
949 return seq_hlist_start(head, pos - 1);
951 EXPORT_SYMBOL(seq_hlist_start_head);
954 * seq_hlist_next - move to the next position of the hlist
955 * @v: the current iterator
956 * @head: the head of the hlist
957 * @ppos: the current position
959 * Called at seq_file->op->next().
961 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
964 struct hlist_node *node = v;
967 if (v == SEQ_START_TOKEN)
972 EXPORT_SYMBOL(seq_hlist_next);
975 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
976 * @head: the head of the hlist
977 * @pos: the start position of the sequence
979 * Called at seq_file->op->start().
981 * This list-traversal primitive may safely run concurrently with
982 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
983 * as long as the traversal is guarded by rcu_read_lock().
985 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
988 struct hlist_node *node;
990 __hlist_for_each_rcu(node, head)
995 EXPORT_SYMBOL(seq_hlist_start_rcu);
998 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
999 * @head: the head of the hlist
1000 * @pos: the start position of the sequence
1002 * Called at seq_file->op->start(). Call this function if you want to
1003 * print a header at the top of the output.
1005 * This list-traversal primitive may safely run concurrently with
1006 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1007 * as long as the traversal is guarded by rcu_read_lock().
1009 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1013 return SEQ_START_TOKEN;
1015 return seq_hlist_start_rcu(head, pos - 1);
1017 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1020 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1021 * @v: the current iterator
1022 * @head: the head of the hlist
1023 * @ppos: the current position
1025 * Called at seq_file->op->next().
1027 * This list-traversal primitive may safely run concurrently with
1028 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1029 * as long as the traversal is guarded by rcu_read_lock().
1031 struct hlist_node *seq_hlist_next_rcu(void *v,
1032 struct hlist_head *head,
1035 struct hlist_node *node = v;
1038 if (v == SEQ_START_TOKEN)
1039 return rcu_dereference(head->first);
1041 return rcu_dereference(node->next);
1043 EXPORT_SYMBOL(seq_hlist_next_rcu);
1046 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1047 * @head: pointer to percpu array of struct hlist_heads
1048 * @cpu: pointer to cpu "cursor"
1049 * @pos: start position of sequence
1051 * Called at seq_file->op->start().
1054 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1056 struct hlist_node *node;
1058 for_each_possible_cpu(*cpu) {
1059 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1066 EXPORT_SYMBOL(seq_hlist_start_percpu);
1069 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1070 * @v: pointer to current hlist_node
1071 * @head: pointer to percpu array of struct hlist_heads
1072 * @cpu: pointer to cpu "cursor"
1073 * @pos: start position of sequence
1075 * Called at seq_file->op->next().
1078 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1079 int *cpu, loff_t *pos)
1081 struct hlist_node *node = v;
1088 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1089 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1090 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1092 if (!hlist_empty(bucket))
1093 return bucket->first;
1097 EXPORT_SYMBOL(seq_hlist_next_percpu);
1099 void __init seq_file_init(void)
1101 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);