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
)
38 gfp_t gfp
= GFP_KERNEL
;
41 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
42 * it's better to fall back to vmalloc() than to kill things. For small
43 * allocations, just use GFP_KERNEL which will oom kill, thus no need
44 * for vmalloc fallback.
47 gfp
|= __GFP_NORETRY
| __GFP_NOWARN
;
48 buf
= kmalloc(size
, gfp
);
49 if (!buf
&& size
> PAGE_SIZE
)
55 * seq_open - initialize sequential file
56 * @file: file we initialize
57 * @op: method table describing the sequence
59 * seq_open() sets @file, associating it with a sequence described
60 * by @op. @op->start() sets the iterator up and returns the first
61 * element of sequence. @op->stop() shuts it down. @op->next()
62 * returns the next element of sequence. @op->show() prints element
63 * into the buffer. In case of error ->start() and ->next() return
64 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
65 * returns 0 in case of success and negative number in case of error.
66 * Returning SEQ_SKIP means "discard this element and move on".
68 int seq_open(struct file
*file
, const struct seq_operations
*op
)
70 struct seq_file
*p
= file
->private_data
;
73 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
76 file
->private_data
= p
;
78 memset(p
, 0, sizeof(*p
));
82 p
->user_ns
= file
->f_cred
->user_ns
;
86 * Wrappers around seq_open(e.g. swaps_open) need to be
87 * aware of this. If they set f_version themselves, they
88 * should call seq_open first and then set f_version.
93 * seq_files support lseek() and pread(). They do not implement
94 * write() at all, but we clear FMODE_PWRITE here for historical
97 * If a client of seq_files a) implements file.write() and b) wishes to
98 * support pwrite() then that client will need to implement its own
99 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
101 file
->f_mode
&= ~FMODE_PWRITE
;
104 EXPORT_SYMBOL(seq_open
);
106 static int traverse(struct seq_file
*m
, loff_t offset
)
108 loff_t pos
= 0, index
;
114 m
->count
= m
->from
= 0;
120 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
124 p
= m
->op
->start(m
, &index
);
129 error
= m
->op
->show(m
, p
);
132 if (unlikely(error
)) {
138 if (pos
+ m
->count
> offset
) {
139 m
->from
= offset
- pos
;
151 p
= m
->op
->next(m
, p
, &index
);
161 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
162 return !m
->buf
? -ENOMEM
: -EAGAIN
;
166 * seq_read - ->read() method for sequential files.
167 * @file: the file to read from
168 * @buf: the buffer to read to
169 * @size: the maximum number of bytes to read
170 * @ppos: the current position in the file
172 * Ready-made ->f_op->read()
174 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
176 struct seq_file
*m
= file
->private_data
;
183 mutex_lock(&m
->lock
);
186 * seq_file->op->..m_start/m_stop/m_next may do special actions
187 * or optimisations based on the file->f_version, so we want to
188 * pass the file->f_version to those methods.
190 * seq_file->version is just copy of f_version, and seq_file
191 * methods can treat it simply as file version.
192 * It is copied in first and copied out after all operations.
193 * It is convenient to have it as part of structure to avoid the
194 * need of passing another argument to all the seq_file methods.
196 m
->version
= file
->f_version
;
198 /* Don't assume *ppos is where we left it */
199 if (unlikely(*ppos
!= m
->read_pos
)) {
200 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
203 /* With prejudice... */
214 /* grab buffer if we didn't have one */
216 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
220 /* if not empty - flush it first */
222 n
= min(m
->count
, size
);
223 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
238 /* we need at least one record in buffer */
240 p
= m
->op
->start(m
, &pos
);
245 err
= m
->op
->show(m
, p
);
250 if (unlikely(!m
->count
)) {
251 p
= m
->op
->next(m
, p
, &pos
);
255 if (m
->count
< m
->size
)
260 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
265 p
= m
->op
->start(m
, &pos
);
271 /* they want more? let's try to get some more */
272 while (m
->count
< size
) {
273 size_t offs
= m
->count
;
275 p
= m
->op
->next(m
, p
, &next
);
276 if (!p
|| IS_ERR(p
)) {
280 err
= m
->op
->show(m
, p
);
281 if (seq_overflow(m
) || err
) {
283 if (likely(err
<= 0))
289 n
= min(m
->count
, size
);
290 err
= copy_to_user(buf
, m
->buf
, n
);
305 m
->read_pos
+= copied
;
307 file
->f_version
= m
->version
;
308 mutex_unlock(&m
->lock
);
317 EXPORT_SYMBOL(seq_read
);
320 * seq_lseek - ->llseek() method for sequential files.
321 * @file: the file in question
322 * @offset: new position
323 * @whence: 0 for absolute, 1 for relative position
325 * Ready-made ->f_op->llseek()
327 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
329 struct seq_file
*m
= file
->private_data
;
330 loff_t retval
= -EINVAL
;
332 mutex_lock(&m
->lock
);
333 m
->version
= file
->f_version
;
336 offset
+= file
->f_pos
;
341 if (offset
!= m
->read_pos
) {
342 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
345 /* with extreme prejudice... */
352 m
->read_pos
= offset
;
353 retval
= file
->f_pos
= offset
;
356 file
->f_pos
= offset
;
359 file
->f_version
= m
->version
;
360 mutex_unlock(&m
->lock
);
363 EXPORT_SYMBOL(seq_lseek
);
366 * seq_release - free the structures associated with sequential file.
367 * @file: file in question
370 * Frees the structures associated with sequential file; can be used
371 * as ->f_op->release() if you don't have private data to destroy.
373 int seq_release(struct inode
*inode
, struct file
*file
)
375 struct seq_file
*m
= file
->private_data
;
380 EXPORT_SYMBOL(seq_release
);
383 * seq_escape - print string into buffer, escaping some characters
386 * @esc: set of characters that need escaping
388 * Puts string into buffer, replacing each occurrence of character from
389 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
392 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
394 char *end
= m
->buf
+ m
->size
;
398 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
399 if (!strchr(esc
, c
)) {
405 *p
++ = '0' + ((c
& 0300) >> 6);
406 *p
++ = '0' + ((c
& 070) >> 3);
407 *p
++ = '0' + (c
& 07);
413 m
->count
= p
- m
->buf
;
416 EXPORT_SYMBOL(seq_escape
);
418 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
422 if (m
->count
< m
->size
) {
423 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
424 if (m
->count
+ len
< m
->size
) {
432 EXPORT_SYMBOL(seq_vprintf
);
434 int seq_printf(struct seq_file
*m
, const char *f
, ...)
440 ret
= seq_vprintf(m
, f
, args
);
445 EXPORT_SYMBOL(seq_printf
);
448 * mangle_path - mangle and copy path to buffer beginning
450 * @p: beginning of path in above buffer
451 * @esc: set of characters that need escaping
453 * Copy the path from @p to @s, replacing each occurrence of character from
454 * @esc with usual octal escape.
455 * Returns pointer past last written character in @s, or NULL in case of
458 char *mangle_path(char *s
, const char *p
, const char *esc
)
464 } else if (!strchr(esc
, c
)) {
466 } else if (s
+ 4 > p
) {
470 *s
++ = '0' + ((c
& 0300) >> 6);
471 *s
++ = '0' + ((c
& 070) >> 3);
472 *s
++ = '0' + (c
& 07);
477 EXPORT_SYMBOL(mangle_path
);
480 * seq_path - seq_file interface to print a pathname
481 * @m: the seq_file handle
482 * @path: the struct path to print
483 * @esc: set of characters to escape in the output
485 * return the absolute path of 'path', as represented by the
486 * dentry / mnt pair in the path parameter.
488 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
491 size_t size
= seq_get_buf(m
, &buf
);
495 char *p
= d_path(path
, buf
, size
);
497 char *end
= mangle_path(buf
, p
, esc
);
506 EXPORT_SYMBOL(seq_path
);
509 * Same as seq_path, but relative to supplied root.
511 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
512 const struct path
*root
, const char *esc
)
515 size_t size
= seq_get_buf(m
, &buf
);
516 int res
= -ENAMETOOLONG
;
521 p
= __d_path(path
, root
, buf
, size
);
526 char *end
= mangle_path(buf
, p
, esc
);
535 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
539 * returns the path of the 'dentry' from the root of its filesystem.
541 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
544 size_t size
= seq_get_buf(m
, &buf
);
548 char *p
= dentry_path(dentry
, buf
, size
);
550 char *end
= mangle_path(buf
, p
, esc
);
560 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
561 unsigned int nr_bits
)
563 if (m
->count
< m
->size
) {
564 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
565 m
->size
- m
->count
, bits
, nr_bits
);
566 if (m
->count
+ len
< m
->size
) {
574 EXPORT_SYMBOL(seq_bitmap
);
576 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
577 unsigned int nr_bits
)
579 if (m
->count
< m
->size
) {
580 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
581 m
->size
- m
->count
, bits
, nr_bits
);
582 if (m
->count
+ len
< m
->size
) {
590 EXPORT_SYMBOL(seq_bitmap_list
);
592 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
594 return NULL
+ (*pos
== 0);
597 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
603 static void single_stop(struct seq_file
*p
, void *v
)
607 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
610 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
614 op
->start
= single_start
;
615 op
->next
= single_next
;
616 op
->stop
= single_stop
;
618 res
= seq_open(file
, op
);
620 ((struct seq_file
*)file
->private_data
)->private = data
;
626 EXPORT_SYMBOL(single_open
);
628 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
629 void *data
, size_t size
)
631 char *buf
= seq_buf_alloc(size
);
635 ret
= single_open(file
, show
, data
);
640 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
641 ((struct seq_file
*)file
->private_data
)->size
= size
;
644 EXPORT_SYMBOL(single_open_size
);
646 int single_release(struct inode
*inode
, struct file
*file
)
648 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
649 int res
= seq_release(inode
, file
);
653 EXPORT_SYMBOL(single_release
);
655 int seq_release_private(struct inode
*inode
, struct file
*file
)
657 struct seq_file
*seq
= file
->private_data
;
661 return seq_release(inode
, file
);
663 EXPORT_SYMBOL(seq_release_private
);
665 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
670 struct seq_file
*seq
;
672 private = kzalloc(psize
, GFP_KERNEL
);
676 rc
= seq_open(f
, ops
);
680 seq
= f
->private_data
;
681 seq
->private = private;
689 EXPORT_SYMBOL(__seq_open_private
);
691 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
694 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
696 EXPORT_SYMBOL(seq_open_private
);
698 int seq_putc(struct seq_file
*m
, char c
)
700 if (m
->count
< m
->size
) {
701 m
->buf
[m
->count
++] = c
;
706 EXPORT_SYMBOL(seq_putc
);
708 int seq_puts(struct seq_file
*m
, const char *s
)
711 if (m
->count
+ len
< m
->size
) {
712 memcpy(m
->buf
+ m
->count
, s
, len
);
719 EXPORT_SYMBOL(seq_puts
);
722 * A helper routine for putting decimal numbers without rich format of printf().
723 * only 'unsigned long long' is supported.
724 * This routine will put one byte delimiter + number into seq_file.
725 * This routine is very quick when you show lots of numbers.
726 * In usual cases, it will be better to use seq_printf(). It's easier to read.
728 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
729 unsigned long long num
)
733 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
737 m
->buf
[m
->count
++] = delimiter
;
740 m
->buf
[m
->count
++] = num
+ '0';
744 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
753 EXPORT_SYMBOL(seq_put_decimal_ull
);
755 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
759 if (m
->count
+ 3 >= m
->size
) {
764 m
->buf
[m
->count
++] = delimiter
;
768 return seq_put_decimal_ull(m
, delimiter
, num
);
771 EXPORT_SYMBOL(seq_put_decimal_ll
);
774 * seq_write - write arbitrary data to buffer
775 * @seq: seq_file identifying the buffer to which data should be written
776 * @data: data address
777 * @len: number of bytes
779 * Return 0 on success, non-zero otherwise.
781 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
783 if (seq
->count
+ len
< seq
->size
) {
784 memcpy(seq
->buf
+ seq
->count
, data
, len
);
788 seq_set_overflow(seq
);
791 EXPORT_SYMBOL(seq_write
);
793 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
795 struct list_head
*lh
;
797 list_for_each(lh
, head
)
803 EXPORT_SYMBOL(seq_list_start
);
805 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
810 return seq_list_start(head
, pos
- 1);
812 EXPORT_SYMBOL(seq_list_start_head
);
814 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
816 struct list_head
*lh
;
818 lh
= ((struct list_head
*)v
)->next
;
820 return lh
== head
? NULL
: lh
;
822 EXPORT_SYMBOL(seq_list_next
);
825 * seq_hlist_start - start an iteration of a hlist
826 * @head: the head of the hlist
827 * @pos: the start position of the sequence
829 * Called at seq_file->op->start().
831 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
833 struct hlist_node
*node
;
835 hlist_for_each(node
, head
)
840 EXPORT_SYMBOL(seq_hlist_start
);
843 * seq_hlist_start_head - start an iteration of a hlist
844 * @head: the head of the hlist
845 * @pos: the start position of the sequence
847 * Called at seq_file->op->start(). Call this function if you want to
848 * print a header at the top of the output.
850 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
853 return SEQ_START_TOKEN
;
855 return seq_hlist_start(head
, pos
- 1);
857 EXPORT_SYMBOL(seq_hlist_start_head
);
860 * seq_hlist_next - move to the next position of the hlist
861 * @v: the current iterator
862 * @head: the head of the hlist
863 * @ppos: the current position
865 * Called at seq_file->op->next().
867 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
870 struct hlist_node
*node
= v
;
873 if (v
== SEQ_START_TOKEN
)
878 EXPORT_SYMBOL(seq_hlist_next
);
881 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
882 * @head: the head of the hlist
883 * @pos: the start position of the sequence
885 * Called at seq_file->op->start().
887 * This list-traversal primitive may safely run concurrently with
888 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
889 * as long as the traversal is guarded by rcu_read_lock().
891 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
894 struct hlist_node
*node
;
896 __hlist_for_each_rcu(node
, head
)
901 EXPORT_SYMBOL(seq_hlist_start_rcu
);
904 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
905 * @head: the head of the hlist
906 * @pos: the start position of the sequence
908 * Called at seq_file->op->start(). Call this function if you want to
909 * print a header at the top of the output.
911 * This list-traversal primitive may safely run concurrently with
912 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
913 * as long as the traversal is guarded by rcu_read_lock().
915 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
919 return SEQ_START_TOKEN
;
921 return seq_hlist_start_rcu(head
, pos
- 1);
923 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
926 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
927 * @v: the current iterator
928 * @head: the head of the hlist
929 * @ppos: the current position
931 * Called at seq_file->op->next().
933 * This list-traversal primitive may safely run concurrently with
934 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
935 * as long as the traversal is guarded by rcu_read_lock().
937 struct hlist_node
*seq_hlist_next_rcu(void *v
,
938 struct hlist_head
*head
,
941 struct hlist_node
*node
= v
;
944 if (v
== SEQ_START_TOKEN
)
945 return rcu_dereference(head
->first
);
947 return rcu_dereference(node
->next
);
949 EXPORT_SYMBOL(seq_hlist_next_rcu
);
952 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
953 * @head: pointer to percpu array of struct hlist_heads
954 * @cpu: pointer to cpu "cursor"
955 * @pos: start position of sequence
957 * Called at seq_file->op->start().
960 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
962 struct hlist_node
*node
;
964 for_each_possible_cpu(*cpu
) {
965 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
972 EXPORT_SYMBOL(seq_hlist_start_percpu
);
975 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
976 * @v: pointer to current hlist_node
977 * @head: pointer to percpu array of struct hlist_heads
978 * @cpu: pointer to cpu "cursor"
979 * @pos: start position of sequence
981 * Called at seq_file->op->next().
984 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
985 int *cpu
, loff_t
*pos
)
987 struct hlist_node
*node
= v
;
994 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
995 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
996 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
998 if (!hlist_empty(bucket
))
999 return bucket
->first
;
1003 EXPORT_SYMBOL(seq_hlist_next_percpu
);