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 * seq_files support lseek() and pread(). They do not implement
72 * write() at all, but we clear FMODE_PWRITE here for historical
75 * If a client of seq_files a) implements file.write() and b) wishes to
76 * support pwrite() then that client will need to implement its own
77 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
79 file->f_mode &= ~FMODE_PWRITE;
82 EXPORT_SYMBOL(seq_open);
84 static int traverse(struct seq_file *m, loff_t offset)
91 m->count = m->from = 0;
96 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
100 p = m->op->start(m, &m->index);
105 error = m->op->show(m, p);
108 if (unlikely(error)) {
112 if (seq_has_overflowed(m))
114 p = m->op->next(m, p, &m->index);
115 if (pos + m->count > offset) {
116 m->from = offset - pos;
132 m->buf = seq_buf_alloc(m->size <<= 1);
133 return !m->buf ? -ENOMEM : -EAGAIN;
137 * seq_read - ->read() method for sequential files.
138 * @file: the file to read from
139 * @buf: the buffer to read to
140 * @size: the maximum number of bytes to read
141 * @ppos: the current position in the file
143 * Ready-made ->f_op->read()
145 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
147 struct seq_file *m = file->private_data;
153 mutex_lock(&m->lock);
156 * if request is to read from zero offset, reset iterator to first
157 * record as it might have been already advanced by previous requests
164 /* Don't assume *ppos is where we left it */
165 if (unlikely(*ppos != m->read_pos)) {
166 while ((err = traverse(m, *ppos)) == -EAGAIN)
169 /* With prejudice... */
179 /* grab buffer if we didn't have one */
181 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
185 /* if not empty - flush it first */
187 n = min(m->count, size);
188 err = copy_to_user(buf, m->buf + m->from, n);
199 /* we need at least one record in buffer */
201 p = m->op->start(m, &m->index);
206 err = m->op->show(m, p);
211 if (unlikely(!m->count)) {
212 p = m->op->next(m, p, &m->index);
215 if (m->count < m->size)
220 m->buf = seq_buf_alloc(m->size <<= 1);
223 p = m->op->start(m, &m->index);
229 /* they want more? let's try to get some more */
231 size_t offs = m->count;
232 loff_t pos = m->index;
234 p = m->op->next(m, p, &m->index);
235 if (pos == m->index) {
236 pr_info_ratelimited("buggy seq_file .next function %ps "
237 "did not updated position index\n",
241 if (!p || IS_ERR(p)) {
245 if (m->count >= size)
247 err = m->op->show(m, p);
248 if (seq_has_overflowed(m) || err) {
250 if (likely(err <= 0))
255 n = min(m->count, size);
256 err = copy_to_user(buf, m->buf, n);
267 m->read_pos += copied;
269 mutex_unlock(&m->lock);
278 EXPORT_SYMBOL(seq_read);
281 * seq_lseek - ->llseek() method for sequential files.
282 * @file: the file in question
283 * @offset: new position
284 * @whence: 0 for absolute, 1 for relative position
286 * Ready-made ->f_op->llseek()
288 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
290 struct seq_file *m = file->private_data;
291 loff_t retval = -EINVAL;
293 mutex_lock(&m->lock);
296 offset += file->f_pos;
302 if (offset != m->read_pos) {
303 while ((retval = traverse(m, offset)) == -EAGAIN)
306 /* with extreme prejudice... */
312 m->read_pos = offset;
313 retval = file->f_pos = offset;
316 file->f_pos = offset;
319 mutex_unlock(&m->lock);
322 EXPORT_SYMBOL(seq_lseek);
325 * seq_release - free the structures associated with sequential file.
326 * @file: file in question
329 * Frees the structures associated with sequential file; can be used
330 * as ->f_op->release() if you don't have private data to destroy.
332 int seq_release(struct inode *inode, struct file *file)
334 struct seq_file *m = file->private_data;
336 kmem_cache_free(seq_file_cache, m);
339 EXPORT_SYMBOL(seq_release);
342 * seq_escape - print string into buffer, escaping some characters
345 * @esc: set of characters that need escaping
347 * Puts string into buffer, replacing each occurrence of character from
348 * @esc with usual octal escape.
349 * Use seq_has_overflowed() to check for errors.
351 void seq_escape(struct seq_file *m, const char *s, const char *esc)
354 size_t size = seq_get_buf(m, &buf);
357 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
358 seq_commit(m, ret < size ? ret : -1);
360 EXPORT_SYMBOL(seq_escape);
362 void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
365 size_t size = seq_get_buf(m, &buf);
368 ret = string_escape_mem_ascii(src, isz, buf, size);
369 seq_commit(m, ret < size ? ret : -1);
371 EXPORT_SYMBOL(seq_escape_mem_ascii);
373 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
377 if (m->count < m->size) {
378 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
379 if (m->count + len < m->size) {
386 EXPORT_SYMBOL(seq_vprintf);
388 void seq_printf(struct seq_file *m, const char *f, ...)
393 seq_vprintf(m, f, args);
396 EXPORT_SYMBOL(seq_printf);
399 * mangle_path - mangle and copy path to buffer beginning
401 * @p: beginning of path in above buffer
402 * @esc: set of characters that need escaping
404 * Copy the path from @p to @s, replacing each occurrence of character from
405 * @esc with usual octal escape.
406 * Returns pointer past last written character in @s, or NULL in case of
409 char *mangle_path(char *s, const char *p, const char *esc)
415 } else if (!strchr(esc, c)) {
417 } else if (s + 4 > p) {
421 *s++ = '0' + ((c & 0300) >> 6);
422 *s++ = '0' + ((c & 070) >> 3);
423 *s++ = '0' + (c & 07);
428 EXPORT_SYMBOL(mangle_path);
431 * seq_path - seq_file interface to print a pathname
432 * @m: the seq_file handle
433 * @path: the struct path to print
434 * @esc: set of characters to escape in the output
436 * return the absolute path of 'path', as represented by the
437 * dentry / mnt pair in the path parameter.
439 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
442 size_t size = seq_get_buf(m, &buf);
446 char *p = d_path(path, buf, size);
448 char *end = mangle_path(buf, p, esc);
457 EXPORT_SYMBOL(seq_path);
460 * seq_file_path - seq_file interface to print a pathname of a file
461 * @m: the seq_file handle
462 * @file: the struct file to print
463 * @esc: set of characters to escape in the output
465 * return the absolute path to the file.
467 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
469 return seq_path(m, &file->f_path, esc);
471 EXPORT_SYMBOL(seq_file_path);
474 * Same as seq_path, but relative to supplied root.
476 int seq_path_root(struct seq_file *m, const struct path *path,
477 const struct path *root, const char *esc)
480 size_t size = seq_get_buf(m, &buf);
481 int res = -ENAMETOOLONG;
486 p = __d_path(path, root, buf, size);
491 char *end = mangle_path(buf, p, esc);
500 return res < 0 && res != -ENAMETOOLONG ? res : 0;
504 * returns the path of the 'dentry' from the root of its filesystem.
506 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
509 size_t size = seq_get_buf(m, &buf);
513 char *p = dentry_path(dentry, buf, size);
515 char *end = mangle_path(buf, p, esc);
524 EXPORT_SYMBOL(seq_dentry);
526 static void *single_start(struct seq_file *p, loff_t *pos)
528 return NULL + (*pos == 0);
531 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
537 static void single_stop(struct seq_file *p, void *v)
541 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
544 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
548 op->start = single_start;
549 op->next = single_next;
550 op->stop = single_stop;
552 res = seq_open(file, op);
554 ((struct seq_file *)file->private_data)->private = data;
560 EXPORT_SYMBOL(single_open);
562 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
563 void *data, size_t size)
565 char *buf = seq_buf_alloc(size);
569 ret = single_open(file, show, data);
574 ((struct seq_file *)file->private_data)->buf = buf;
575 ((struct seq_file *)file->private_data)->size = size;
578 EXPORT_SYMBOL(single_open_size);
580 int single_release(struct inode *inode, struct file *file)
582 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
583 int res = seq_release(inode, file);
587 EXPORT_SYMBOL(single_release);
589 int seq_release_private(struct inode *inode, struct file *file)
591 struct seq_file *seq = file->private_data;
595 return seq_release(inode, file);
597 EXPORT_SYMBOL(seq_release_private);
599 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
604 struct seq_file *seq;
606 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
610 rc = seq_open(f, ops);
614 seq = f->private_data;
615 seq->private = private;
623 EXPORT_SYMBOL(__seq_open_private);
625 int seq_open_private(struct file *filp, const struct seq_operations *ops,
628 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
630 EXPORT_SYMBOL(seq_open_private);
632 void seq_putc(struct seq_file *m, char c)
634 if (m->count >= m->size)
637 m->buf[m->count++] = c;
639 EXPORT_SYMBOL(seq_putc);
641 void seq_puts(struct seq_file *m, const char *s)
645 if (m->count + len >= m->size) {
649 memcpy(m->buf + m->count, s, len);
652 EXPORT_SYMBOL(seq_puts);
655 * A helper routine for putting decimal numbers without rich format of printf().
656 * only 'unsigned long long' is supported.
657 * @m: seq_file identifying the buffer to which data should be written
658 * @delimiter: a string which is printed before the number
660 * @width: a minimum field width
662 * This routine will put strlen(delimiter) + number into seq_filed.
663 * This routine is very quick when you show lots of numbers.
664 * In usual cases, it will be better to use seq_printf(). It's easier to read.
666 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
667 unsigned long long num, unsigned int width)
671 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
674 if (delimiter && delimiter[0]) {
675 if (delimiter[1] == 0)
676 seq_putc(m, delimiter[0]);
678 seq_puts(m, delimiter);
684 if (m->count + width >= m->size)
687 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
698 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
699 unsigned long long num)
701 return seq_put_decimal_ull_width(m, delimiter, num, 0);
703 EXPORT_SYMBOL(seq_put_decimal_ull);
706 * seq_put_hex_ll - put a number in hexadecimal notation
707 * @m: seq_file identifying the buffer to which data should be written
708 * @delimiter: a string which is printed before the number
710 * @width: a minimum field width
712 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
714 * This routine is very quick when you show lots of numbers.
715 * In usual cases, it will be better to use seq_printf(). It's easier to read.
717 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
718 unsigned long long v, unsigned int width)
723 if (delimiter && delimiter[0]) {
724 if (delimiter[1] == 0)
725 seq_putc(m, delimiter[0]);
727 seq_puts(m, delimiter);
730 /* If x is 0, the result of __builtin_clzll is undefined */
734 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
739 if (m->count + len > m->size) {
744 for (i = len - 1; i >= 0; i--) {
745 m->buf[m->count + i] = hex_asc[0xf & v];
751 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
755 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
758 if (delimiter && delimiter[0]) {
759 if (delimiter[1] == 0)
760 seq_putc(m, delimiter[0]);
762 seq_puts(m, delimiter);
765 if (m->count + 2 >= m->size)
769 m->buf[m->count++] = '-';
774 m->buf[m->count++] = num + '0';
778 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
788 EXPORT_SYMBOL(seq_put_decimal_ll);
791 * seq_write - write arbitrary data to buffer
792 * @seq: seq_file identifying the buffer to which data should be written
793 * @data: data address
794 * @len: number of bytes
796 * Return 0 on success, non-zero otherwise.
798 int seq_write(struct seq_file *seq, const void *data, size_t len)
800 if (seq->count + len < seq->size) {
801 memcpy(seq->buf + seq->count, data, len);
805 seq_set_overflow(seq);
808 EXPORT_SYMBOL(seq_write);
811 * seq_pad - write padding spaces to buffer
812 * @m: seq_file identifying the buffer to which data should be written
813 * @c: the byte to append after padding if non-zero
815 void seq_pad(struct seq_file *m, char c)
817 int size = m->pad_until - m->count;
819 if (size + m->count > m->size) {
823 memset(m->buf + m->count, ' ', size);
829 EXPORT_SYMBOL(seq_pad);
831 /* A complete analogue of print_hex_dump() */
832 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
833 int rowsize, int groupsize, const void *buf, size_t len,
837 int i, linelen, remaining = len;
842 if (rowsize != 16 && rowsize != 32)
845 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
846 linelen = min(remaining, rowsize);
847 remaining -= rowsize;
849 switch (prefix_type) {
850 case DUMP_PREFIX_ADDRESS:
851 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
853 case DUMP_PREFIX_OFFSET:
854 seq_printf(m, "%s%.8x: ", prefix_str, i);
857 seq_printf(m, "%s", prefix_str);
861 size = seq_get_buf(m, &buffer);
862 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
863 buffer, size, ascii);
864 seq_commit(m, ret < size ? ret : -1);
869 EXPORT_SYMBOL(seq_hex_dump);
871 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
873 struct list_head *lh;
875 list_for_each(lh, head)
881 EXPORT_SYMBOL(seq_list_start);
883 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
888 return seq_list_start(head, pos - 1);
890 EXPORT_SYMBOL(seq_list_start_head);
892 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
894 struct list_head *lh;
896 lh = ((struct list_head *)v)->next;
898 return lh == head ? NULL : lh;
900 EXPORT_SYMBOL(seq_list_next);
903 * seq_hlist_start - start an iteration of a hlist
904 * @head: the head of the hlist
905 * @pos: the start position of the sequence
907 * Called at seq_file->op->start().
909 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
911 struct hlist_node *node;
913 hlist_for_each(node, head)
918 EXPORT_SYMBOL(seq_hlist_start);
921 * seq_hlist_start_head - start an iteration of a hlist
922 * @head: the head of the hlist
923 * @pos: the start position of the sequence
925 * Called at seq_file->op->start(). Call this function if you want to
926 * print a header at the top of the output.
928 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
931 return SEQ_START_TOKEN;
933 return seq_hlist_start(head, pos - 1);
935 EXPORT_SYMBOL(seq_hlist_start_head);
938 * seq_hlist_next - move to the next position of the hlist
939 * @v: the current iterator
940 * @head: the head of the hlist
941 * @ppos: the current position
943 * Called at seq_file->op->next().
945 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
948 struct hlist_node *node = v;
951 if (v == SEQ_START_TOKEN)
956 EXPORT_SYMBOL(seq_hlist_next);
959 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
960 * @head: the head of the hlist
961 * @pos: the start position of the sequence
963 * Called at seq_file->op->start().
965 * This list-traversal primitive may safely run concurrently with
966 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
967 * as long as the traversal is guarded by rcu_read_lock().
969 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
972 struct hlist_node *node;
974 __hlist_for_each_rcu(node, head)
979 EXPORT_SYMBOL(seq_hlist_start_rcu);
982 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
983 * @head: the head of the hlist
984 * @pos: the start position of the sequence
986 * Called at seq_file->op->start(). Call this function if you want to
987 * print a header at the top of the output.
989 * This list-traversal primitive may safely run concurrently with
990 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
991 * as long as the traversal is guarded by rcu_read_lock().
993 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
997 return SEQ_START_TOKEN;
999 return seq_hlist_start_rcu(head, pos - 1);
1001 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1004 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1005 * @v: the current iterator
1006 * @head: the head of the hlist
1007 * @ppos: the current position
1009 * Called at seq_file->op->next().
1011 * This list-traversal primitive may safely run concurrently with
1012 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1013 * as long as the traversal is guarded by rcu_read_lock().
1015 struct hlist_node *seq_hlist_next_rcu(void *v,
1016 struct hlist_head *head,
1019 struct hlist_node *node = v;
1022 if (v == SEQ_START_TOKEN)
1023 return rcu_dereference(head->first);
1025 return rcu_dereference(node->next);
1027 EXPORT_SYMBOL(seq_hlist_next_rcu);
1030 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1031 * @head: pointer to percpu array of struct hlist_heads
1032 * @cpu: pointer to cpu "cursor"
1033 * @pos: start position of sequence
1035 * Called at seq_file->op->start().
1038 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1040 struct hlist_node *node;
1042 for_each_possible_cpu(*cpu) {
1043 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1050 EXPORT_SYMBOL(seq_hlist_start_percpu);
1053 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1054 * @v: pointer to current hlist_node
1055 * @head: pointer to percpu array of struct hlist_heads
1056 * @cpu: pointer to cpu "cursor"
1057 * @pos: start position of sequence
1059 * Called at seq_file->op->next().
1062 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1063 int *cpu, loff_t *pos)
1065 struct hlist_node *node = v;
1072 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1073 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1074 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1076 if (!hlist_empty(bucket))
1077 return bucket->first;
1081 EXPORT_SYMBOL(seq_hlist_next_percpu);
1083 void __init seq_file_init(void)
1085 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);