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
);
236 /* Buggy ->next function */
238 if (!p
|| IS_ERR(p
)) {
242 if (m
->count
>= size
)
244 err
= m
->op
->show(m
, p
);
245 if (seq_has_overflowed(m
) || err
) {
247 if (likely(err
<= 0))
252 n
= min(m
->count
, size
);
253 err
= copy_to_user(buf
, m
->buf
, n
);
264 m
->read_pos
+= copied
;
266 mutex_unlock(&m
->lock
);
275 EXPORT_SYMBOL(seq_read
);
278 * seq_lseek - ->llseek() method for sequential files.
279 * @file: the file in question
280 * @offset: new position
281 * @whence: 0 for absolute, 1 for relative position
283 * Ready-made ->f_op->llseek()
285 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
287 struct seq_file
*m
= file
->private_data
;
288 loff_t retval
= -EINVAL
;
290 mutex_lock(&m
->lock
);
293 offset
+= file
->f_pos
;
299 if (offset
!= m
->read_pos
) {
300 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
303 /* with extreme prejudice... */
309 m
->read_pos
= offset
;
310 retval
= file
->f_pos
= offset
;
313 file
->f_pos
= offset
;
316 mutex_unlock(&m
->lock
);
319 EXPORT_SYMBOL(seq_lseek
);
322 * seq_release - free the structures associated with sequential file.
323 * @file: file in question
326 * Frees the structures associated with sequential file; can be used
327 * as ->f_op->release() if you don't have private data to destroy.
329 int seq_release(struct inode
*inode
, struct file
*file
)
331 struct seq_file
*m
= file
->private_data
;
333 kmem_cache_free(seq_file_cache
, m
);
336 EXPORT_SYMBOL(seq_release
);
339 * seq_escape - print string into buffer, escaping some characters
342 * @esc: set of characters that need escaping
344 * Puts string into buffer, replacing each occurrence of character from
345 * @esc with usual octal escape.
346 * Use seq_has_overflowed() to check for errors.
348 void seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
351 size_t size
= seq_get_buf(m
, &buf
);
354 ret
= string_escape_str(s
, buf
, size
, ESCAPE_OCTAL
, esc
);
355 seq_commit(m
, ret
< size
? ret
: -1);
357 EXPORT_SYMBOL(seq_escape
);
359 void seq_escape_mem_ascii(struct seq_file
*m
, const char *src
, size_t isz
)
362 size_t size
= seq_get_buf(m
, &buf
);
365 ret
= string_escape_mem_ascii(src
, isz
, buf
, size
);
366 seq_commit(m
, ret
< size
? ret
: -1);
368 EXPORT_SYMBOL(seq_escape_mem_ascii
);
370 void seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
374 if (m
->count
< m
->size
) {
375 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
376 if (m
->count
+ len
< m
->size
) {
383 EXPORT_SYMBOL(seq_vprintf
);
385 void seq_printf(struct seq_file
*m
, const char *f
, ...)
390 seq_vprintf(m
, f
, args
);
393 EXPORT_SYMBOL(seq_printf
);
396 * mangle_path - mangle and copy path to buffer beginning
398 * @p: beginning of path in above buffer
399 * @esc: set of characters that need escaping
401 * Copy the path from @p to @s, replacing each occurrence of character from
402 * @esc with usual octal escape.
403 * Returns pointer past last written character in @s, or NULL in case of
406 char *mangle_path(char *s
, const char *p
, const char *esc
)
412 } else if (!strchr(esc
, c
)) {
414 } else if (s
+ 4 > p
) {
418 *s
++ = '0' + ((c
& 0300) >> 6);
419 *s
++ = '0' + ((c
& 070) >> 3);
420 *s
++ = '0' + (c
& 07);
425 EXPORT_SYMBOL(mangle_path
);
428 * seq_path - seq_file interface to print a pathname
429 * @m: the seq_file handle
430 * @path: the struct path to print
431 * @esc: set of characters to escape in the output
433 * return the absolute path of 'path', as represented by the
434 * dentry / mnt pair in the path parameter.
436 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
439 size_t size
= seq_get_buf(m
, &buf
);
443 char *p
= d_path(path
, buf
, size
);
445 char *end
= mangle_path(buf
, p
, esc
);
454 EXPORT_SYMBOL(seq_path
);
457 * seq_file_path - seq_file interface to print a pathname of a file
458 * @m: the seq_file handle
459 * @file: the struct file to print
460 * @esc: set of characters to escape in the output
462 * return the absolute path to the file.
464 int seq_file_path(struct seq_file
*m
, struct file
*file
, const char *esc
)
466 return seq_path(m
, &file
->f_path
, esc
);
468 EXPORT_SYMBOL(seq_file_path
);
471 * Same as seq_path, but relative to supplied root.
473 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
474 const struct path
*root
, const char *esc
)
477 size_t size
= seq_get_buf(m
, &buf
);
478 int res
= -ENAMETOOLONG
;
483 p
= __d_path(path
, root
, buf
, size
);
488 char *end
= mangle_path(buf
, p
, esc
);
497 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
501 * returns the path of the 'dentry' from the root of its filesystem.
503 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
506 size_t size
= seq_get_buf(m
, &buf
);
510 char *p
= dentry_path(dentry
, buf
, size
);
512 char *end
= mangle_path(buf
, p
, esc
);
521 EXPORT_SYMBOL(seq_dentry
);
523 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
525 return NULL
+ (*pos
== 0);
528 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
534 static void single_stop(struct seq_file
*p
, void *v
)
538 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
541 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL_ACCOUNT
);
545 op
->start
= single_start
;
546 op
->next
= single_next
;
547 op
->stop
= single_stop
;
549 res
= seq_open(file
, op
);
551 ((struct seq_file
*)file
->private_data
)->private = data
;
557 EXPORT_SYMBOL(single_open
);
559 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
560 void *data
, size_t size
)
562 char *buf
= seq_buf_alloc(size
);
566 ret
= single_open(file
, show
, data
);
571 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
572 ((struct seq_file
*)file
->private_data
)->size
= size
;
575 EXPORT_SYMBOL(single_open_size
);
577 int single_release(struct inode
*inode
, struct file
*file
)
579 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
580 int res
= seq_release(inode
, file
);
584 EXPORT_SYMBOL(single_release
);
586 int seq_release_private(struct inode
*inode
, struct file
*file
)
588 struct seq_file
*seq
= file
->private_data
;
592 return seq_release(inode
, file
);
594 EXPORT_SYMBOL(seq_release_private
);
596 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
601 struct seq_file
*seq
;
603 private = kzalloc(psize
, GFP_KERNEL_ACCOUNT
);
607 rc
= seq_open(f
, ops
);
611 seq
= f
->private_data
;
612 seq
->private = private;
620 EXPORT_SYMBOL(__seq_open_private
);
622 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
625 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
627 EXPORT_SYMBOL(seq_open_private
);
629 void seq_putc(struct seq_file
*m
, char c
)
631 if (m
->count
>= m
->size
)
634 m
->buf
[m
->count
++] = c
;
636 EXPORT_SYMBOL(seq_putc
);
638 void seq_puts(struct seq_file
*m
, const char *s
)
642 if (m
->count
+ len
>= m
->size
) {
646 memcpy(m
->buf
+ m
->count
, s
, len
);
649 EXPORT_SYMBOL(seq_puts
);
652 * A helper routine for putting decimal numbers without rich format of printf().
653 * only 'unsigned long long' is supported.
654 * @m: seq_file identifying the buffer to which data should be written
655 * @delimiter: a string which is printed before the number
657 * @width: a minimum field width
659 * This routine will put strlen(delimiter) + number into seq_filed.
660 * This routine is very quick when you show lots of numbers.
661 * In usual cases, it will be better to use seq_printf(). It's easier to read.
663 void seq_put_decimal_ull_width(struct seq_file
*m
, const char *delimiter
,
664 unsigned long long num
, unsigned int width
)
668 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
671 if (delimiter
&& delimiter
[0]) {
672 if (delimiter
[1] == 0)
673 seq_putc(m
, delimiter
[0]);
675 seq_puts(m
, delimiter
);
681 if (m
->count
+ width
>= m
->size
)
684 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
, width
);
695 void seq_put_decimal_ull(struct seq_file
*m
, const char *delimiter
,
696 unsigned long long num
)
698 return seq_put_decimal_ull_width(m
, delimiter
, num
, 0);
700 EXPORT_SYMBOL(seq_put_decimal_ull
);
703 * seq_put_hex_ll - put a number in hexadecimal notation
704 * @m: seq_file identifying the buffer to which data should be written
705 * @delimiter: a string which is printed before the number
707 * @width: a minimum field width
709 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
711 * This routine is very quick when you show lots of numbers.
712 * In usual cases, it will be better to use seq_printf(). It's easier to read.
714 void seq_put_hex_ll(struct seq_file
*m
, const char *delimiter
,
715 unsigned long long v
, unsigned int width
)
720 if (delimiter
&& delimiter
[0]) {
721 if (delimiter
[1] == 0)
722 seq_putc(m
, delimiter
[0]);
724 seq_puts(m
, delimiter
);
727 /* If x is 0, the result of __builtin_clzll is undefined */
731 len
= (sizeof(v
) * 8 - __builtin_clzll(v
) + 3) / 4;
736 if (m
->count
+ len
> m
->size
) {
741 for (i
= len
- 1; i
>= 0; i
--) {
742 m
->buf
[m
->count
+ i
] = hex_asc
[0xf & v
];
748 void seq_put_decimal_ll(struct seq_file
*m
, const char *delimiter
, long long num
)
752 if (m
->count
+ 3 >= m
->size
) /* we'll write 2 bytes at least */
755 if (delimiter
&& delimiter
[0]) {
756 if (delimiter
[1] == 0)
757 seq_putc(m
, delimiter
[0]);
759 seq_puts(m
, delimiter
);
762 if (m
->count
+ 2 >= m
->size
)
766 m
->buf
[m
->count
++] = '-';
771 m
->buf
[m
->count
++] = num
+ '0';
775 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
, 0);
785 EXPORT_SYMBOL(seq_put_decimal_ll
);
788 * seq_write - write arbitrary data to buffer
789 * @seq: seq_file identifying the buffer to which data should be written
790 * @data: data address
791 * @len: number of bytes
793 * Return 0 on success, non-zero otherwise.
795 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
797 if (seq
->count
+ len
< seq
->size
) {
798 memcpy(seq
->buf
+ seq
->count
, data
, len
);
802 seq_set_overflow(seq
);
805 EXPORT_SYMBOL(seq_write
);
808 * seq_pad - write padding spaces to buffer
809 * @m: seq_file identifying the buffer to which data should be written
810 * @c: the byte to append after padding if non-zero
812 void seq_pad(struct seq_file
*m
, char c
)
814 int size
= m
->pad_until
- m
->count
;
816 if (size
+ m
->count
> m
->size
) {
820 memset(m
->buf
+ m
->count
, ' ', size
);
826 EXPORT_SYMBOL(seq_pad
);
828 /* A complete analogue of print_hex_dump() */
829 void seq_hex_dump(struct seq_file
*m
, const char *prefix_str
, int prefix_type
,
830 int rowsize
, int groupsize
, const void *buf
, size_t len
,
834 int i
, linelen
, remaining
= len
;
839 if (rowsize
!= 16 && rowsize
!= 32)
842 for (i
= 0; i
< len
&& !seq_has_overflowed(m
); i
+= rowsize
) {
843 linelen
= min(remaining
, rowsize
);
844 remaining
-= rowsize
;
846 switch (prefix_type
) {
847 case DUMP_PREFIX_ADDRESS
:
848 seq_printf(m
, "%s%p: ", prefix_str
, ptr
+ i
);
850 case DUMP_PREFIX_OFFSET
:
851 seq_printf(m
, "%s%.8x: ", prefix_str
, i
);
854 seq_printf(m
, "%s", prefix_str
);
858 size
= seq_get_buf(m
, &buffer
);
859 ret
= hex_dump_to_buffer(ptr
+ i
, linelen
, rowsize
, groupsize
,
860 buffer
, size
, ascii
);
861 seq_commit(m
, ret
< size
? ret
: -1);
866 EXPORT_SYMBOL(seq_hex_dump
);
868 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
870 struct list_head
*lh
;
872 list_for_each(lh
, head
)
878 EXPORT_SYMBOL(seq_list_start
);
880 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
885 return seq_list_start(head
, pos
- 1);
887 EXPORT_SYMBOL(seq_list_start_head
);
889 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
891 struct list_head
*lh
;
893 lh
= ((struct list_head
*)v
)->next
;
895 return lh
== head
? NULL
: lh
;
897 EXPORT_SYMBOL(seq_list_next
);
900 * seq_hlist_start - start an iteration of a hlist
901 * @head: the head of the hlist
902 * @pos: the start position of the sequence
904 * Called at seq_file->op->start().
906 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
908 struct hlist_node
*node
;
910 hlist_for_each(node
, head
)
915 EXPORT_SYMBOL(seq_hlist_start
);
918 * seq_hlist_start_head - start an iteration of a hlist
919 * @head: the head of the hlist
920 * @pos: the start position of the sequence
922 * Called at seq_file->op->start(). Call this function if you want to
923 * print a header at the top of the output.
925 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
928 return SEQ_START_TOKEN
;
930 return seq_hlist_start(head
, pos
- 1);
932 EXPORT_SYMBOL(seq_hlist_start_head
);
935 * seq_hlist_next - move to the next position of the hlist
936 * @v: the current iterator
937 * @head: the head of the hlist
938 * @ppos: the current position
940 * Called at seq_file->op->next().
942 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
945 struct hlist_node
*node
= v
;
948 if (v
== SEQ_START_TOKEN
)
953 EXPORT_SYMBOL(seq_hlist_next
);
956 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
957 * @head: the head of the hlist
958 * @pos: the start position of the sequence
960 * Called at seq_file->op->start().
962 * This list-traversal primitive may safely run concurrently with
963 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
964 * as long as the traversal is guarded by rcu_read_lock().
966 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
969 struct hlist_node
*node
;
971 __hlist_for_each_rcu(node
, head
)
976 EXPORT_SYMBOL(seq_hlist_start_rcu
);
979 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
980 * @head: the head of the hlist
981 * @pos: the start position of the sequence
983 * Called at seq_file->op->start(). Call this function if you want to
984 * print a header at the top of the output.
986 * This list-traversal primitive may safely run concurrently with
987 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
988 * as long as the traversal is guarded by rcu_read_lock().
990 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
994 return SEQ_START_TOKEN
;
996 return seq_hlist_start_rcu(head
, pos
- 1);
998 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
1001 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1002 * @v: the current iterator
1003 * @head: the head of the hlist
1004 * @ppos: the current position
1006 * Called at seq_file->op->next().
1008 * This list-traversal primitive may safely run concurrently with
1009 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1010 * as long as the traversal is guarded by rcu_read_lock().
1012 struct hlist_node
*seq_hlist_next_rcu(void *v
,
1013 struct hlist_head
*head
,
1016 struct hlist_node
*node
= v
;
1019 if (v
== SEQ_START_TOKEN
)
1020 return rcu_dereference(head
->first
);
1022 return rcu_dereference(node
->next
);
1024 EXPORT_SYMBOL(seq_hlist_next_rcu
);
1027 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1028 * @head: pointer to percpu array of struct hlist_heads
1029 * @cpu: pointer to cpu "cursor"
1030 * @pos: start position of sequence
1032 * Called at seq_file->op->start().
1035 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
1037 struct hlist_node
*node
;
1039 for_each_possible_cpu(*cpu
) {
1040 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
1047 EXPORT_SYMBOL(seq_hlist_start_percpu
);
1050 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1051 * @v: pointer to current hlist_node
1052 * @head: pointer to percpu array of struct hlist_heads
1053 * @cpu: pointer to cpu "cursor"
1054 * @pos: start position of sequence
1056 * Called at seq_file->op->next().
1059 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
1060 int *cpu
, loff_t
*pos
)
1062 struct hlist_node
*node
= v
;
1069 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
1070 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
1071 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
1073 if (!hlist_empty(bucket
))
1074 return bucket
->first
;
1078 EXPORT_SYMBOL(seq_hlist_next_percpu
);
1080 void __init
seq_file_init(void)
1082 seq_file_cache
= KMEM_CACHE(seq_file
, SLAB_ACCOUNT
|SLAB_PANIC
);