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>
16 #include <asm/uaccess.h>
21 * seq_files have a buffer which can may overflow. When this happens a larger
22 * buffer is reallocated and all the data will be printed again.
23 * The overflow state is true when m->count == m->size.
25 static bool seq_overflow(struct seq_file
*m
)
27 return m
->count
== m
->size
;
30 static void seq_set_overflow(struct seq_file
*m
)
35 static void *seq_buf_alloc(unsigned long size
)
39 buf
= kmalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
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".
59 int seq_open(struct file
*file
, const struct seq_operations
*op
)
61 struct seq_file
*p
= file
->private_data
;
64 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
67 file
->private_data
= p
;
69 memset(p
, 0, sizeof(*p
));
73 p
->user_ns
= file
->f_cred
->user_ns
;
77 * Wrappers around seq_open(e.g. swaps_open) need to be
78 * aware of this. If they set f_version themselves, they
79 * should call seq_open first and then set f_version.
84 * seq_files support lseek() and pread(). They do not implement
85 * write() at all, but we clear FMODE_PWRITE here for historical
88 * If a client of seq_files a) implements file.write() and b) wishes to
89 * support pwrite() then that client will need to implement its own
90 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
92 file
->f_mode
&= ~FMODE_PWRITE
;
95 EXPORT_SYMBOL(seq_open
);
97 static int traverse(struct seq_file
*m
, loff_t offset
)
99 loff_t pos
= 0, index
;
105 m
->count
= m
->from
= 0;
111 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
115 p
= m
->op
->start(m
, &index
);
120 error
= m
->op
->show(m
, p
);
123 if (unlikely(error
)) {
129 if (pos
+ m
->count
> offset
) {
130 m
->from
= offset
- pos
;
142 p
= m
->op
->next(m
, p
, &index
);
152 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
153 return !m
->buf
? -ENOMEM
: -EAGAIN
;
157 * seq_read - ->read() method for sequential files.
158 * @file: the file to read from
159 * @buf: the buffer to read to
160 * @size: the maximum number of bytes to read
161 * @ppos: the current position in the file
163 * Ready-made ->f_op->read()
165 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
167 struct seq_file
*m
= file
->private_data
;
174 mutex_lock(&m
->lock
);
177 * seq_file->op->..m_start/m_stop/m_next may do special actions
178 * or optimisations based on the file->f_version, so we want to
179 * pass the file->f_version to those methods.
181 * seq_file->version is just copy of f_version, and seq_file
182 * methods can treat it simply as file version.
183 * It is copied in first and copied out after all operations.
184 * It is convenient to have it as part of structure to avoid the
185 * need of passing another argument to all the seq_file methods.
187 m
->version
= file
->f_version
;
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
);
229 /* we need at least one record in buffer */
231 p
= m
->op
->start(m
, &pos
);
236 err
= m
->op
->show(m
, p
);
241 if (unlikely(!m
->count
)) {
242 p
= m
->op
->next(m
, p
, &pos
);
246 if (m
->count
< m
->size
)
251 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
256 p
= m
->op
->start(m
, &pos
);
262 /* they want more? let's try to get some more */
263 while (m
->count
< size
) {
264 size_t offs
= m
->count
;
266 p
= m
->op
->next(m
, p
, &next
);
267 if (!p
|| IS_ERR(p
)) {
271 err
= m
->op
->show(m
, p
);
272 if (seq_overflow(m
) || err
) {
274 if (likely(err
<= 0))
280 n
= min(m
->count
, size
);
281 err
= copy_to_user(buf
, m
->buf
, n
);
296 m
->read_pos
+= copied
;
298 file
->f_version
= m
->version
;
299 mutex_unlock(&m
->lock
);
308 EXPORT_SYMBOL(seq_read
);
311 * seq_lseek - ->llseek() method for sequential files.
312 * @file: the file in question
313 * @offset: new position
314 * @whence: 0 for absolute, 1 for relative position
316 * Ready-made ->f_op->llseek()
318 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
320 struct seq_file
*m
= file
->private_data
;
321 loff_t retval
= -EINVAL
;
323 mutex_lock(&m
->lock
);
324 m
->version
= file
->f_version
;
327 offset
+= file
->f_pos
;
332 if (offset
!= m
->read_pos
) {
333 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
336 /* with extreme prejudice... */
343 m
->read_pos
= offset
;
344 retval
= file
->f_pos
= offset
;
347 file
->f_pos
= offset
;
350 file
->f_version
= m
->version
;
351 mutex_unlock(&m
->lock
);
354 EXPORT_SYMBOL(seq_lseek
);
357 * seq_release - free the structures associated with sequential file.
358 * @file: file in question
361 * Frees the structures associated with sequential file; can be used
362 * as ->f_op->release() if you don't have private data to destroy.
364 int seq_release(struct inode
*inode
, struct file
*file
)
366 struct seq_file
*m
= file
->private_data
;
371 EXPORT_SYMBOL(seq_release
);
374 * seq_escape - print string into buffer, escaping some characters
377 * @esc: set of characters that need escaping
379 * Puts string into buffer, replacing each occurrence of character from
380 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
383 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
385 char *end
= m
->buf
+ m
->size
;
389 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
390 if (!strchr(esc
, c
)) {
396 *p
++ = '0' + ((c
& 0300) >> 6);
397 *p
++ = '0' + ((c
& 070) >> 3);
398 *p
++ = '0' + (c
& 07);
404 m
->count
= p
- m
->buf
;
407 EXPORT_SYMBOL(seq_escape
);
409 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
413 if (m
->count
< m
->size
) {
414 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
415 if (m
->count
+ len
< m
->size
) {
423 EXPORT_SYMBOL(seq_vprintf
);
425 int seq_printf(struct seq_file
*m
, const char *f
, ...)
431 ret
= seq_vprintf(m
, f
, args
);
436 EXPORT_SYMBOL(seq_printf
);
439 * mangle_path - mangle and copy path to buffer beginning
441 * @p: beginning of path in above buffer
442 * @esc: set of characters that need escaping
444 * Copy the path from @p to @s, replacing each occurrence of character from
445 * @esc with usual octal escape.
446 * Returns pointer past last written character in @s, or NULL in case of
449 char *mangle_path(char *s
, const char *p
, const char *esc
)
455 } else if (!strchr(esc
, c
)) {
457 } else if (s
+ 4 > p
) {
461 *s
++ = '0' + ((c
& 0300) >> 6);
462 *s
++ = '0' + ((c
& 070) >> 3);
463 *s
++ = '0' + (c
& 07);
468 EXPORT_SYMBOL(mangle_path
);
471 * seq_path - seq_file interface to print a pathname
472 * @m: the seq_file handle
473 * @path: the struct path to print
474 * @esc: set of characters to escape in the output
476 * return the absolute path of 'path', as represented by the
477 * dentry / mnt pair in the path parameter.
479 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
482 size_t size
= seq_get_buf(m
, &buf
);
486 char *p
= d_path(path
, buf
, size
);
488 char *end
= mangle_path(buf
, p
, esc
);
497 EXPORT_SYMBOL(seq_path
);
500 * Same as seq_path, but relative to supplied root.
502 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
503 const struct path
*root
, const char *esc
)
506 size_t size
= seq_get_buf(m
, &buf
);
507 int res
= -ENAMETOOLONG
;
512 p
= __d_path(path
, root
, buf
, size
);
517 char *end
= mangle_path(buf
, p
, esc
);
526 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
530 * returns the path of the 'dentry' from the root of its filesystem.
532 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
535 size_t size
= seq_get_buf(m
, &buf
);
539 char *p
= dentry_path(dentry
, buf
, size
);
541 char *end
= mangle_path(buf
, p
, esc
);
551 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
552 unsigned int nr_bits
)
554 if (m
->count
< m
->size
) {
555 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
556 m
->size
- m
->count
, bits
, nr_bits
);
557 if (m
->count
+ len
< m
->size
) {
565 EXPORT_SYMBOL(seq_bitmap
);
567 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
568 unsigned int nr_bits
)
570 if (m
->count
< m
->size
) {
571 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
572 m
->size
- m
->count
, bits
, nr_bits
);
573 if (m
->count
+ len
< m
->size
) {
581 EXPORT_SYMBOL(seq_bitmap_list
);
583 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
585 return NULL
+ (*pos
== 0);
588 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
594 static void single_stop(struct seq_file
*p
, void *v
)
598 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
601 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
605 op
->start
= single_start
;
606 op
->next
= single_next
;
607 op
->stop
= single_stop
;
609 res
= seq_open(file
, op
);
611 ((struct seq_file
*)file
->private_data
)->private = data
;
617 EXPORT_SYMBOL(single_open
);
619 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
620 void *data
, size_t size
)
622 char *buf
= seq_buf_alloc(size
);
626 ret
= single_open(file
, show
, data
);
631 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
632 ((struct seq_file
*)file
->private_data
)->size
= size
;
635 EXPORT_SYMBOL(single_open_size
);
637 int single_release(struct inode
*inode
, struct file
*file
)
639 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
640 int res
= seq_release(inode
, file
);
644 EXPORT_SYMBOL(single_release
);
646 int seq_release_private(struct inode
*inode
, struct file
*file
)
648 struct seq_file
*seq
= file
->private_data
;
652 return seq_release(inode
, file
);
654 EXPORT_SYMBOL(seq_release_private
);
656 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
661 struct seq_file
*seq
;
663 private = kzalloc(psize
, GFP_KERNEL
);
667 rc
= seq_open(f
, ops
);
671 seq
= f
->private_data
;
672 seq
->private = private;
680 EXPORT_SYMBOL(__seq_open_private
);
682 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
685 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
687 EXPORT_SYMBOL(seq_open_private
);
689 int seq_putc(struct seq_file
*m
, char c
)
691 if (m
->count
< m
->size
) {
692 m
->buf
[m
->count
++] = c
;
697 EXPORT_SYMBOL(seq_putc
);
699 int seq_puts(struct seq_file
*m
, const char *s
)
702 if (m
->count
+ len
< m
->size
) {
703 memcpy(m
->buf
+ m
->count
, s
, len
);
710 EXPORT_SYMBOL(seq_puts
);
713 * A helper routine for putting decimal numbers without rich format of printf().
714 * only 'unsigned long long' is supported.
715 * This routine will put one byte delimiter + number into seq_file.
716 * This routine is very quick when you show lots of numbers.
717 * In usual cases, it will be better to use seq_printf(). It's easier to read.
719 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
720 unsigned long long num
)
724 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
728 m
->buf
[m
->count
++] = delimiter
;
731 m
->buf
[m
->count
++] = num
+ '0';
735 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
744 EXPORT_SYMBOL(seq_put_decimal_ull
);
746 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
750 if (m
->count
+ 3 >= m
->size
) {
755 m
->buf
[m
->count
++] = delimiter
;
759 return seq_put_decimal_ull(m
, delimiter
, num
);
762 EXPORT_SYMBOL(seq_put_decimal_ll
);
765 * seq_write - write arbitrary data to buffer
766 * @seq: seq_file identifying the buffer to which data should be written
767 * @data: data address
768 * @len: number of bytes
770 * Return 0 on success, non-zero otherwise.
772 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
774 if (seq
->count
+ len
< seq
->size
) {
775 memcpy(seq
->buf
+ seq
->count
, data
, len
);
779 seq_set_overflow(seq
);
782 EXPORT_SYMBOL(seq_write
);
785 * seq_pad - write padding spaces to buffer
786 * @m: seq_file identifying the buffer to which data should be written
787 * @c: the byte to append after padding if non-zero
789 void seq_pad(struct seq_file
*m
, char c
)
791 int size
= m
->pad_until
- m
->count
;
793 seq_printf(m
, "%*s", size
, "");
797 EXPORT_SYMBOL(seq_pad
);
799 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
801 struct list_head
*lh
;
803 list_for_each(lh
, head
)
809 EXPORT_SYMBOL(seq_list_start
);
811 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
816 return seq_list_start(head
, pos
- 1);
818 EXPORT_SYMBOL(seq_list_start_head
);
820 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
822 struct list_head
*lh
;
824 lh
= ((struct list_head
*)v
)->next
;
826 return lh
== head
? NULL
: lh
;
828 EXPORT_SYMBOL(seq_list_next
);
831 * seq_hlist_start - start an iteration of a hlist
832 * @head: the head of the hlist
833 * @pos: the start position of the sequence
835 * Called at seq_file->op->start().
837 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
839 struct hlist_node
*node
;
841 hlist_for_each(node
, head
)
846 EXPORT_SYMBOL(seq_hlist_start
);
849 * seq_hlist_start_head - start an iteration of a hlist
850 * @head: the head of the hlist
851 * @pos: the start position of the sequence
853 * Called at seq_file->op->start(). Call this function if you want to
854 * print a header at the top of the output.
856 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
859 return SEQ_START_TOKEN
;
861 return seq_hlist_start(head
, pos
- 1);
863 EXPORT_SYMBOL(seq_hlist_start_head
);
866 * seq_hlist_next - move to the next position of the hlist
867 * @v: the current iterator
868 * @head: the head of the hlist
869 * @ppos: the current position
871 * Called at seq_file->op->next().
873 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
876 struct hlist_node
*node
= v
;
879 if (v
== SEQ_START_TOKEN
)
884 EXPORT_SYMBOL(seq_hlist_next
);
887 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
888 * @head: the head of the hlist
889 * @pos: the start position of the sequence
891 * Called at seq_file->op->start().
893 * This list-traversal primitive may safely run concurrently with
894 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
895 * as long as the traversal is guarded by rcu_read_lock().
897 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
900 struct hlist_node
*node
;
902 __hlist_for_each_rcu(node
, head
)
907 EXPORT_SYMBOL(seq_hlist_start_rcu
);
910 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
911 * @head: the head of the hlist
912 * @pos: the start position of the sequence
914 * Called at seq_file->op->start(). Call this function if you want to
915 * print a header at the top of the output.
917 * This list-traversal primitive may safely run concurrently with
918 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
919 * as long as the traversal is guarded by rcu_read_lock().
921 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
925 return SEQ_START_TOKEN
;
927 return seq_hlist_start_rcu(head
, pos
- 1);
929 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
932 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
933 * @v: the current iterator
934 * @head: the head of the hlist
935 * @ppos: the current position
937 * Called at seq_file->op->next().
939 * This list-traversal primitive may safely run concurrently with
940 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
941 * as long as the traversal is guarded by rcu_read_lock().
943 struct hlist_node
*seq_hlist_next_rcu(void *v
,
944 struct hlist_head
*head
,
947 struct hlist_node
*node
= v
;
950 if (v
== SEQ_START_TOKEN
)
951 return rcu_dereference(head
->first
);
953 return rcu_dereference(node
->next
);
955 EXPORT_SYMBOL(seq_hlist_next_rcu
);
958 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
959 * @head: pointer to percpu array of struct hlist_heads
960 * @cpu: pointer to cpu "cursor"
961 * @pos: start position of sequence
963 * Called at seq_file->op->start().
966 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
968 struct hlist_node
*node
;
970 for_each_possible_cpu(*cpu
) {
971 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
978 EXPORT_SYMBOL(seq_hlist_start_percpu
);
981 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
982 * @v: pointer to current hlist_node
983 * @head: pointer to percpu array of struct hlist_heads
984 * @cpu: pointer to cpu "cursor"
985 * @pos: start position of sequence
987 * Called at seq_file->op->next().
990 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
991 int *cpu
, loff_t
*pos
)
993 struct hlist_node
*node
= v
;
1000 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
1001 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
1002 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
1004 if (!hlist_empty(bucket
))
1005 return bucket
->first
;
1009 EXPORT_SYMBOL(seq_hlist_next_percpu
);