4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
9 #include <linux/export.h>
10 #include <linux/seq_file.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
15 #include <linux/printk.h>
16 #include <linux/string_helpers.h>
18 #include <asm/uaccess.h>
21 static void seq_set_overflow(struct seq_file
*m
)
26 static void *seq_buf_alloc(unsigned long size
)
29 gfp_t gfp
= GFP_KERNEL
;
32 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
33 * it's better to fall back to vmalloc() than to kill things. For small
34 * allocations, just use GFP_KERNEL which will oom kill, thus no need
35 * for vmalloc fallback.
38 gfp
|= __GFP_NORETRY
| __GFP_NOWARN
;
39 buf
= kmalloc(size
, gfp
);
40 if (!buf
&& size
> PAGE_SIZE
)
46 * seq_open - initialize sequential file
47 * @file: file we initialize
48 * @op: method table describing the sequence
50 * seq_open() sets @file, associating it with a sequence described
51 * by @op. @op->start() sets the iterator up and returns the first
52 * element of sequence. @op->stop() shuts it down. @op->next()
53 * returns the next element of sequence. @op->show() prints element
54 * into the buffer. In case of error ->start() and ->next() return
55 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
56 * returns 0 in case of success and negative number in case of error.
57 * Returning SEQ_SKIP means "discard this element and move on".
58 * Note: seq_open() will allocate a struct seq_file and store its
59 * pointer in @file->private_data. This pointer should not be modified.
61 int seq_open(struct file
*file
, const struct seq_operations
*op
)
65 WARN_ON(file
->private_data
);
67 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
71 file
->private_data
= p
;
76 p
->user_ns
= file
->f_cred
->user_ns
;
80 * Wrappers around seq_open(e.g. swaps_open) need to be
81 * aware of this. If they set f_version themselves, they
82 * should call seq_open first and then set f_version.
87 * seq_files support lseek() and pread(). They do not implement
88 * write() at all, but we clear FMODE_PWRITE here for historical
91 * If a client of seq_files a) implements file.write() and b) wishes to
92 * support pwrite() then that client will need to implement its own
93 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
95 file
->f_mode
&= ~FMODE_PWRITE
;
98 EXPORT_SYMBOL(seq_open
);
100 static int traverse(struct seq_file
*m
, loff_t offset
)
102 loff_t pos
= 0, index
;
108 m
->count
= m
->from
= 0;
114 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
118 p
= m
->op
->start(m
, &index
);
123 error
= m
->op
->show(m
, p
);
126 if (unlikely(error
)) {
130 if (seq_has_overflowed(m
))
132 if (pos
+ m
->count
> offset
) {
133 m
->from
= offset
- pos
;
145 p
= m
->op
->next(m
, p
, &index
);
155 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
156 return !m
->buf
? -ENOMEM
: -EAGAIN
;
160 * seq_read - ->read() method for sequential files.
161 * @file: the file to read from
162 * @buf: the buffer to read to
163 * @size: the maximum number of bytes to read
164 * @ppos: the current position in the file
166 * Ready-made ->f_op->read()
168 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
170 struct seq_file
*m
= file
->private_data
;
177 mutex_lock(&m
->lock
);
180 * seq_file->op->..m_start/m_stop/m_next may do special actions
181 * or optimisations based on the file->f_version, so we want to
182 * pass the file->f_version to those methods.
184 * seq_file->version is just copy of f_version, and seq_file
185 * methods can treat it simply as file version.
186 * It is copied in first and copied out after all operations.
187 * It is convenient to have it as part of structure to avoid the
188 * need of passing another argument to all the seq_file methods.
190 m
->version
= file
->f_version
;
192 /* Don't assume *ppos is where we left it */
193 if (unlikely(*ppos
!= m
->read_pos
)) {
194 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
197 /* With prejudice... */
208 /* grab buffer if we didn't have one */
210 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
214 /* if not empty - flush it first */
216 n
= min(m
->count
, size
);
217 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
232 /* we need at least one record in buffer */
234 p
= m
->op
->start(m
, &pos
);
239 err
= m
->op
->show(m
, p
);
244 if (unlikely(!m
->count
)) {
245 p
= m
->op
->next(m
, p
, &pos
);
249 if (m
->count
< m
->size
)
254 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
259 p
= m
->op
->start(m
, &pos
);
265 /* they want more? let's try to get some more */
266 while (m
->count
< size
) {
267 size_t offs
= m
->count
;
269 p
= m
->op
->next(m
, p
, &next
);
270 if (!p
|| IS_ERR(p
)) {
274 err
= m
->op
->show(m
, p
);
275 if (seq_has_overflowed(m
) || err
) {
277 if (likely(err
<= 0))
283 n
= min(m
->count
, size
);
284 err
= copy_to_user(buf
, m
->buf
, n
);
299 m
->read_pos
+= copied
;
301 file
->f_version
= m
->version
;
302 mutex_unlock(&m
->lock
);
311 EXPORT_SYMBOL(seq_read
);
314 * seq_lseek - ->llseek() method for sequential files.
315 * @file: the file in question
316 * @offset: new position
317 * @whence: 0 for absolute, 1 for relative position
319 * Ready-made ->f_op->llseek()
321 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
323 struct seq_file
*m
= file
->private_data
;
324 loff_t retval
= -EINVAL
;
326 mutex_lock(&m
->lock
);
327 m
->version
= file
->f_version
;
330 offset
+= file
->f_pos
;
335 if (offset
!= m
->read_pos
) {
336 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
339 /* with extreme prejudice... */
346 m
->read_pos
= offset
;
347 retval
= file
->f_pos
= offset
;
350 file
->f_pos
= offset
;
353 file
->f_version
= m
->version
;
354 mutex_unlock(&m
->lock
);
357 EXPORT_SYMBOL(seq_lseek
);
360 * seq_release - free the structures associated with sequential file.
361 * @file: file in question
364 * Frees the structures associated with sequential file; can be used
365 * as ->f_op->release() if you don't have private data to destroy.
367 int seq_release(struct inode
*inode
, struct file
*file
)
369 struct seq_file
*m
= file
->private_data
;
374 EXPORT_SYMBOL(seq_release
);
377 * seq_escape - print string into buffer, escaping some characters
380 * @esc: set of characters that need escaping
382 * Puts string into buffer, replacing each occurrence of character from
383 * @esc with usual octal escape.
384 * Use seq_has_overflowed() to check for errors.
386 void seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
389 size_t size
= seq_get_buf(m
, &buf
);
392 ret
= string_escape_str(s
, buf
, size
, ESCAPE_OCTAL
, esc
);
393 seq_commit(m
, ret
< size
? ret
: -1);
395 EXPORT_SYMBOL(seq_escape
);
397 void seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
401 if (m
->count
< m
->size
) {
402 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
403 if (m
->count
+ len
< m
->size
) {
410 EXPORT_SYMBOL(seq_vprintf
);
412 void seq_printf(struct seq_file
*m
, const char *f
, ...)
417 seq_vprintf(m
, f
, args
);
420 EXPORT_SYMBOL(seq_printf
);
423 * mangle_path - mangle and copy path to buffer beginning
425 * @p: beginning of path in above buffer
426 * @esc: set of characters that need escaping
428 * Copy the path from @p to @s, replacing each occurrence of character from
429 * @esc with usual octal escape.
430 * Returns pointer past last written character in @s, or NULL in case of
433 char *mangle_path(char *s
, const char *p
, const char *esc
)
439 } else if (!strchr(esc
, c
)) {
441 } else if (s
+ 4 > p
) {
445 *s
++ = '0' + ((c
& 0300) >> 6);
446 *s
++ = '0' + ((c
& 070) >> 3);
447 *s
++ = '0' + (c
& 07);
452 EXPORT_SYMBOL(mangle_path
);
455 * seq_path - seq_file interface to print a pathname
456 * @m: the seq_file handle
457 * @path: the struct path to print
458 * @esc: set of characters to escape in the output
460 * return the absolute path of 'path', as represented by the
461 * dentry / mnt pair in the path parameter.
463 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
466 size_t size
= seq_get_buf(m
, &buf
);
470 char *p
= d_path(path
, buf
, size
);
472 char *end
= mangle_path(buf
, p
, esc
);
481 EXPORT_SYMBOL(seq_path
);
484 * seq_file_path - seq_file interface to print a pathname of a file
485 * @m: the seq_file handle
486 * @file: the struct file to print
487 * @esc: set of characters to escape in the output
489 * return the absolute path to the file.
491 int seq_file_path(struct seq_file
*m
, struct file
*file
, const char *esc
)
493 return seq_path(m
, &file
->f_path
, esc
);
495 EXPORT_SYMBOL(seq_file_path
);
498 * Same as seq_path, but relative to supplied root.
500 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
501 const struct path
*root
, const char *esc
)
504 size_t size
= seq_get_buf(m
, &buf
);
505 int res
= -ENAMETOOLONG
;
510 p
= __d_path(path
, root
, buf
, size
);
515 char *end
= mangle_path(buf
, p
, esc
);
524 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
528 * returns the path of the 'dentry' from the root of its filesystem.
530 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
533 size_t size
= seq_get_buf(m
, &buf
);
537 char *p
= dentry_path(dentry
, buf
, size
);
539 char *end
= mangle_path(buf
, p
, esc
);
548 EXPORT_SYMBOL(seq_dentry
);
550 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
552 return NULL
+ (*pos
== 0);
555 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
561 static void single_stop(struct seq_file
*p
, void *v
)
565 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
568 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
572 op
->start
= single_start
;
573 op
->next
= single_next
;
574 op
->stop
= single_stop
;
576 res
= seq_open(file
, op
);
578 ((struct seq_file
*)file
->private_data
)->private = data
;
584 EXPORT_SYMBOL(single_open
);
586 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
587 void *data
, size_t size
)
589 char *buf
= seq_buf_alloc(size
);
593 ret
= single_open(file
, show
, data
);
598 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
599 ((struct seq_file
*)file
->private_data
)->size
= size
;
602 EXPORT_SYMBOL(single_open_size
);
604 int single_release(struct inode
*inode
, struct file
*file
)
606 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
607 int res
= seq_release(inode
, file
);
611 EXPORT_SYMBOL(single_release
);
613 int seq_release_private(struct inode
*inode
, struct file
*file
)
615 struct seq_file
*seq
= file
->private_data
;
619 return seq_release(inode
, file
);
621 EXPORT_SYMBOL(seq_release_private
);
623 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
628 struct seq_file
*seq
;
630 private = kzalloc(psize
, GFP_KERNEL
);
634 rc
= seq_open(f
, ops
);
638 seq
= f
->private_data
;
639 seq
->private = private;
647 EXPORT_SYMBOL(__seq_open_private
);
649 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
652 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
654 EXPORT_SYMBOL(seq_open_private
);
656 void seq_putc(struct seq_file
*m
, char c
)
658 if (m
->count
>= m
->size
)
661 m
->buf
[m
->count
++] = c
;
663 EXPORT_SYMBOL(seq_putc
);
665 void seq_puts(struct seq_file
*m
, const char *s
)
669 if (m
->count
+ len
>= m
->size
) {
673 memcpy(m
->buf
+ m
->count
, s
, len
);
676 EXPORT_SYMBOL(seq_puts
);
679 * A helper routine for putting decimal numbers without rich format of printf().
680 * only 'unsigned long long' is supported.
681 * This routine will put one byte delimiter + number into seq_file.
682 * This routine is very quick when you show lots of numbers.
683 * In usual cases, it will be better to use seq_printf(). It's easier to read.
685 void seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
686 unsigned long long num
)
690 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
694 m
->buf
[m
->count
++] = delimiter
;
697 m
->buf
[m
->count
++] = num
+ '0';
701 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
710 EXPORT_SYMBOL(seq_put_decimal_ull
);
712 void seq_put_decimal_ll(struct seq_file
*m
, char delimiter
, long long num
)
715 if (m
->count
+ 3 >= m
->size
) {
720 m
->buf
[m
->count
++] = delimiter
;
724 seq_put_decimal_ull(m
, delimiter
, num
);
726 EXPORT_SYMBOL(seq_put_decimal_ll
);
729 * seq_write - write arbitrary data to buffer
730 * @seq: seq_file identifying the buffer to which data should be written
731 * @data: data address
732 * @len: number of bytes
734 * Return 0 on success, non-zero otherwise.
736 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
738 if (seq
->count
+ len
< seq
->size
) {
739 memcpy(seq
->buf
+ seq
->count
, data
, len
);
743 seq_set_overflow(seq
);
746 EXPORT_SYMBOL(seq_write
);
749 * seq_pad - write padding spaces to buffer
750 * @m: seq_file identifying the buffer to which data should be written
751 * @c: the byte to append after padding if non-zero
753 void seq_pad(struct seq_file
*m
, char c
)
755 int size
= m
->pad_until
- m
->count
;
757 seq_printf(m
, "%*s", size
, "");
761 EXPORT_SYMBOL(seq_pad
);
763 /* A complete analogue of print_hex_dump() */
764 void seq_hex_dump(struct seq_file
*m
, const char *prefix_str
, int prefix_type
,
765 int rowsize
, int groupsize
, const void *buf
, size_t len
,
769 int i
, linelen
, remaining
= len
;
774 if (rowsize
!= 16 && rowsize
!= 32)
777 for (i
= 0; i
< len
&& !seq_has_overflowed(m
); i
+= rowsize
) {
778 linelen
= min(remaining
, rowsize
);
779 remaining
-= rowsize
;
781 switch (prefix_type
) {
782 case DUMP_PREFIX_ADDRESS
:
783 seq_printf(m
, "%s%p: ", prefix_str
, ptr
+ i
);
785 case DUMP_PREFIX_OFFSET
:
786 seq_printf(m
, "%s%.8x: ", prefix_str
, i
);
789 seq_printf(m
, "%s", prefix_str
);
793 size
= seq_get_buf(m
, &buffer
);
794 ret
= hex_dump_to_buffer(ptr
+ i
, linelen
, rowsize
, groupsize
,
795 buffer
, size
, ascii
);
796 seq_commit(m
, ret
< size
? ret
: -1);
801 EXPORT_SYMBOL(seq_hex_dump
);
803 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
805 struct list_head
*lh
;
807 list_for_each(lh
, head
)
813 EXPORT_SYMBOL(seq_list_start
);
815 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
820 return seq_list_start(head
, pos
- 1);
822 EXPORT_SYMBOL(seq_list_start_head
);
824 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
826 struct list_head
*lh
;
828 lh
= ((struct list_head
*)v
)->next
;
830 return lh
== head
? NULL
: lh
;
832 EXPORT_SYMBOL(seq_list_next
);
835 * seq_hlist_start - start an iteration of a hlist
836 * @head: the head of the hlist
837 * @pos: the start position of the sequence
839 * Called at seq_file->op->start().
841 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
843 struct hlist_node
*node
;
845 hlist_for_each(node
, head
)
850 EXPORT_SYMBOL(seq_hlist_start
);
853 * seq_hlist_start_head - start an iteration of a hlist
854 * @head: the head of the hlist
855 * @pos: the start position of the sequence
857 * Called at seq_file->op->start(). Call this function if you want to
858 * print a header at the top of the output.
860 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
863 return SEQ_START_TOKEN
;
865 return seq_hlist_start(head
, pos
- 1);
867 EXPORT_SYMBOL(seq_hlist_start_head
);
870 * seq_hlist_next - move to the next position of the hlist
871 * @v: the current iterator
872 * @head: the head of the hlist
873 * @ppos: the current position
875 * Called at seq_file->op->next().
877 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
880 struct hlist_node
*node
= v
;
883 if (v
== SEQ_START_TOKEN
)
888 EXPORT_SYMBOL(seq_hlist_next
);
891 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
892 * @head: the head of the hlist
893 * @pos: the start position of the sequence
895 * Called at seq_file->op->start().
897 * This list-traversal primitive may safely run concurrently with
898 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
899 * as long as the traversal is guarded by rcu_read_lock().
901 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
904 struct hlist_node
*node
;
906 __hlist_for_each_rcu(node
, head
)
911 EXPORT_SYMBOL(seq_hlist_start_rcu
);
914 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
915 * @head: the head of the hlist
916 * @pos: the start position of the sequence
918 * Called at seq_file->op->start(). Call this function if you want to
919 * print a header at the top of the output.
921 * This list-traversal primitive may safely run concurrently with
922 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
923 * as long as the traversal is guarded by rcu_read_lock().
925 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
929 return SEQ_START_TOKEN
;
931 return seq_hlist_start_rcu(head
, pos
- 1);
933 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
936 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
937 * @v: the current iterator
938 * @head: the head of the hlist
939 * @ppos: the current position
941 * Called at seq_file->op->next().
943 * This list-traversal primitive may safely run concurrently with
944 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
945 * as long as the traversal is guarded by rcu_read_lock().
947 struct hlist_node
*seq_hlist_next_rcu(void *v
,
948 struct hlist_head
*head
,
951 struct hlist_node
*node
= v
;
954 if (v
== SEQ_START_TOKEN
)
955 return rcu_dereference(head
->first
);
957 return rcu_dereference(node
->next
);
959 EXPORT_SYMBOL(seq_hlist_next_rcu
);
962 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
963 * @head: pointer to percpu array of struct hlist_heads
964 * @cpu: pointer to cpu "cursor"
965 * @pos: start position of sequence
967 * Called at seq_file->op->start().
970 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
972 struct hlist_node
*node
;
974 for_each_possible_cpu(*cpu
) {
975 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
982 EXPORT_SYMBOL(seq_hlist_start_percpu
);
985 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
986 * @v: pointer to current hlist_node
987 * @head: pointer to percpu array of struct hlist_heads
988 * @cpu: pointer to cpu "cursor"
989 * @pos: start position of sequence
991 * Called at seq_file->op->next().
994 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
995 int *cpu
, loff_t
*pos
)
997 struct hlist_node
*node
= v
;
1004 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
1005 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
1006 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
1008 if (!hlist_empty(bucket
))
1009 return bucket
->first
;
1013 EXPORT_SYMBOL(seq_hlist_next_percpu
);