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/slab.h>
12 #include <linux/cred.h>
14 #include <asm/uaccess.h>
19 * seq_files have a buffer which can may overflow. When this happens a larger
20 * buffer is reallocated and all the data will be printed again.
21 * The overflow state is true when m->count == m->size.
23 static bool seq_overflow(struct seq_file
*m
)
25 return m
->count
== m
->size
;
28 static void seq_set_overflow(struct seq_file
*m
)
34 * seq_open - initialize sequential file
35 * @file: file we initialize
36 * @op: method table describing the sequence
38 * seq_open() sets @file, associating it with a sequence described
39 * by @op. @op->start() sets the iterator up and returns the first
40 * element of sequence. @op->stop() shuts it down. @op->next()
41 * returns the next element of sequence. @op->show() prints element
42 * into the buffer. In case of error ->start() and ->next() return
43 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
44 * returns 0 in case of success and negative number in case of error.
45 * Returning SEQ_SKIP means "discard this element and move on".
47 int seq_open(struct file
*file
, const struct seq_operations
*op
)
49 struct seq_file
*p
= file
->private_data
;
52 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
55 file
->private_data
= p
;
57 memset(p
, 0, sizeof(*p
));
61 p
->user_ns
= file
->f_cred
->user_ns
;
65 * Wrappers around seq_open(e.g. swaps_open) need to be
66 * aware of this. If they set f_version themselves, they
67 * should call seq_open first and then set f_version.
72 * seq_files support lseek() and pread(). They do not implement
73 * write() at all, but we clear FMODE_PWRITE here for historical
76 * If a client of seq_files a) implements file.write() and b) wishes to
77 * support pwrite() then that client will need to implement its own
78 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
80 file
->f_mode
&= ~FMODE_PWRITE
;
83 EXPORT_SYMBOL(seq_open
);
85 static int traverse(struct seq_file
*m
, loff_t offset
)
87 loff_t pos
= 0, index
;
93 m
->count
= m
->from
= 0;
99 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
103 p
= m
->op
->start(m
, &index
);
108 error
= m
->op
->show(m
, p
);
111 if (unlikely(error
)) {
117 if (pos
+ m
->count
> offset
) {
118 m
->from
= offset
- pos
;
130 p
= m
->op
->next(m
, p
, &index
);
140 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
141 return !m
->buf
? -ENOMEM
: -EAGAIN
;
145 * seq_read - ->read() method for sequential files.
146 * @file: the file to read from
147 * @buf: the buffer to read to
148 * @size: the maximum number of bytes to read
149 * @ppos: the current position in the file
151 * Ready-made ->f_op->read()
153 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
155 struct seq_file
*m
= file
->private_data
;
162 mutex_lock(&m
->lock
);
165 * seq_file->op->..m_start/m_stop/m_next may do special actions
166 * or optimisations based on the file->f_version, so we want to
167 * pass the file->f_version to those methods.
169 * seq_file->version is just copy of f_version, and seq_file
170 * methods can treat it simply as file version.
171 * It is copied in first and copied out after all operations.
172 * It is convenient to have it as part of structure to avoid the
173 * need of passing another argument to all the seq_file methods.
175 m
->version
= file
->f_version
;
177 /* Don't assume *ppos is where we left it */
178 if (unlikely(*ppos
!= m
->read_pos
)) {
179 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
182 /* With prejudice... */
193 /* grab buffer if we didn't have one */
195 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
199 /* if not empty - flush it first */
201 n
= min(m
->count
, size
);
202 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
215 /* we need at least one record in buffer */
217 p
= m
->op
->start(m
, &pos
);
222 err
= m
->op
->show(m
, p
);
227 if (unlikely(!m
->count
)) {
228 p
= m
->op
->next(m
, p
, &pos
);
232 if (m
->count
< m
->size
)
237 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
242 p
= m
->op
->start(m
, &pos
);
248 /* they want more? let's try to get some more */
249 while (m
->count
< size
) {
250 size_t offs
= m
->count
;
252 p
= m
->op
->next(m
, p
, &next
);
253 if (!p
|| IS_ERR(p
)) {
257 err
= m
->op
->show(m
, p
);
258 if (seq_overflow(m
) || err
) {
260 if (likely(err
<= 0))
266 n
= min(m
->count
, size
);
267 err
= copy_to_user(buf
, m
->buf
, n
);
282 m
->read_pos
+= copied
;
284 file
->f_version
= m
->version
;
285 mutex_unlock(&m
->lock
);
294 EXPORT_SYMBOL(seq_read
);
297 * seq_lseek - ->llseek() method for sequential files.
298 * @file: the file in question
299 * @offset: new position
300 * @whence: 0 for absolute, 1 for relative position
302 * Ready-made ->f_op->llseek()
304 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
306 struct seq_file
*m
= file
->private_data
;
307 loff_t retval
= -EINVAL
;
309 mutex_lock(&m
->lock
);
310 m
->version
= file
->f_version
;
313 offset
+= file
->f_pos
;
318 if (offset
!= m
->read_pos
) {
319 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
322 /* with extreme prejudice... */
329 m
->read_pos
= offset
;
330 retval
= file
->f_pos
= offset
;
333 file
->f_pos
= offset
;
336 file
->f_version
= m
->version
;
337 mutex_unlock(&m
->lock
);
340 EXPORT_SYMBOL(seq_lseek
);
343 * seq_release - free the structures associated with sequential file.
344 * @file: file in question
347 * Frees the structures associated with sequential file; can be used
348 * as ->f_op->release() if you don't have private data to destroy.
350 int seq_release(struct inode
*inode
, struct file
*file
)
352 struct seq_file
*m
= file
->private_data
;
357 EXPORT_SYMBOL(seq_release
);
360 * seq_escape - print string into buffer, escaping some characters
363 * @esc: set of characters that need escaping
365 * Puts string into buffer, replacing each occurrence of character from
366 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
369 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
371 char *end
= m
->buf
+ m
->size
;
375 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
376 if (!strchr(esc
, c
)) {
382 *p
++ = '0' + ((c
& 0300) >> 6);
383 *p
++ = '0' + ((c
& 070) >> 3);
384 *p
++ = '0' + (c
& 07);
390 m
->count
= p
- m
->buf
;
393 EXPORT_SYMBOL(seq_escape
);
395 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
399 if (m
->count
< m
->size
) {
400 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
401 if (m
->count
+ len
< m
->size
) {
409 EXPORT_SYMBOL(seq_vprintf
);
411 int seq_printf(struct seq_file
*m
, const char *f
, ...)
417 ret
= seq_vprintf(m
, f
, args
);
422 EXPORT_SYMBOL(seq_printf
);
425 * mangle_path - mangle and copy path to buffer beginning
427 * @p: beginning of path in above buffer
428 * @esc: set of characters that need escaping
430 * Copy the path from @p to @s, replacing each occurrence of character from
431 * @esc with usual octal escape.
432 * Returns pointer past last written character in @s, or NULL in case of
435 char *mangle_path(char *s
, const char *p
, const char *esc
)
441 } else if (!strchr(esc
, c
)) {
443 } else if (s
+ 4 > p
) {
447 *s
++ = '0' + ((c
& 0300) >> 6);
448 *s
++ = '0' + ((c
& 070) >> 3);
449 *s
++ = '0' + (c
& 07);
454 EXPORT_SYMBOL(mangle_path
);
457 * seq_path - seq_file interface to print a pathname
458 * @m: the seq_file handle
459 * @path: the struct path to print
460 * @esc: set of characters to escape in the output
462 * return the absolute path of 'path', as represented by the
463 * dentry / mnt pair in the path parameter.
465 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
468 size_t size
= seq_get_buf(m
, &buf
);
472 char *p
= d_path(path
, buf
, size
);
474 char *end
= mangle_path(buf
, p
, esc
);
483 EXPORT_SYMBOL(seq_path
);
486 * Same as seq_path, but relative to supplied root.
488 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
489 const struct path
*root
, const char *esc
)
492 size_t size
= seq_get_buf(m
, &buf
);
493 int res
= -ENAMETOOLONG
;
498 p
= __d_path(path
, root
, buf
, size
);
503 char *end
= mangle_path(buf
, p
, esc
);
512 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
516 * returns the path of the 'dentry' from the root of its filesystem.
518 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
521 size_t size
= seq_get_buf(m
, &buf
);
525 char *p
= dentry_path(dentry
, buf
, size
);
527 char *end
= mangle_path(buf
, p
, esc
);
537 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
538 unsigned int nr_bits
)
540 if (m
->count
< m
->size
) {
541 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
542 m
->size
- m
->count
, bits
, nr_bits
);
543 if (m
->count
+ len
< m
->size
) {
551 EXPORT_SYMBOL(seq_bitmap
);
553 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
554 unsigned int nr_bits
)
556 if (m
->count
< m
->size
) {
557 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
558 m
->size
- m
->count
, bits
, nr_bits
);
559 if (m
->count
+ len
< m
->size
) {
567 EXPORT_SYMBOL(seq_bitmap_list
);
569 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
571 return NULL
+ (*pos
== 0);
574 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
580 static void single_stop(struct seq_file
*p
, void *v
)
584 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
587 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
591 op
->start
= single_start
;
592 op
->next
= single_next
;
593 op
->stop
= single_stop
;
595 res
= seq_open(file
, op
);
597 ((struct seq_file
*)file
->private_data
)->private = data
;
603 EXPORT_SYMBOL(single_open
);
605 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
606 void *data
, size_t size
)
608 char *buf
= kmalloc(size
, GFP_KERNEL
);
612 ret
= single_open(file
, show
, data
);
617 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
618 ((struct seq_file
*)file
->private_data
)->size
= size
;
621 EXPORT_SYMBOL(single_open_size
);
623 int single_release(struct inode
*inode
, struct file
*file
)
625 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
626 int res
= seq_release(inode
, file
);
630 EXPORT_SYMBOL(single_release
);
632 int seq_release_private(struct inode
*inode
, struct file
*file
)
634 struct seq_file
*seq
= file
->private_data
;
638 return seq_release(inode
, file
);
640 EXPORT_SYMBOL(seq_release_private
);
642 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
647 struct seq_file
*seq
;
649 private = kzalloc(psize
, GFP_KERNEL
);
653 rc
= seq_open(f
, ops
);
657 seq
= f
->private_data
;
658 seq
->private = private;
666 EXPORT_SYMBOL(__seq_open_private
);
668 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
671 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
673 EXPORT_SYMBOL(seq_open_private
);
675 int seq_putc(struct seq_file
*m
, char c
)
677 if (m
->count
< m
->size
) {
678 m
->buf
[m
->count
++] = c
;
683 EXPORT_SYMBOL(seq_putc
);
685 int seq_puts(struct seq_file
*m
, const char *s
)
688 if (m
->count
+ len
< m
->size
) {
689 memcpy(m
->buf
+ m
->count
, s
, len
);
696 EXPORT_SYMBOL(seq_puts
);
699 * A helper routine for putting decimal numbers without rich format of printf().
700 * only 'unsigned long long' is supported.
701 * This routine will put one byte delimiter + number into seq_file.
702 * This routine is very quick when you show lots of numbers.
703 * In usual cases, it will be better to use seq_printf(). It's easier to read.
705 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
706 unsigned long long num
)
710 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
714 m
->buf
[m
->count
++] = delimiter
;
717 m
->buf
[m
->count
++] = num
+ '0';
721 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
730 EXPORT_SYMBOL(seq_put_decimal_ull
);
732 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
736 if (m
->count
+ 3 >= m
->size
) {
741 m
->buf
[m
->count
++] = delimiter
;
745 return seq_put_decimal_ull(m
, delimiter
, num
);
748 EXPORT_SYMBOL(seq_put_decimal_ll
);
751 * seq_write - write arbitrary data to buffer
752 * @seq: seq_file identifying the buffer to which data should be written
753 * @data: data address
754 * @len: number of bytes
756 * Return 0 on success, non-zero otherwise.
758 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
760 if (seq
->count
+ len
< seq
->size
) {
761 memcpy(seq
->buf
+ seq
->count
, data
, len
);
765 seq_set_overflow(seq
);
768 EXPORT_SYMBOL(seq_write
);
771 * seq_pad - write padding spaces to buffer
772 * @m: seq_file identifying the buffer to which data should be written
773 * @c: the byte to append after padding if non-zero
775 void seq_pad(struct seq_file
*m
, char c
)
777 int size
= m
->pad_until
- m
->count
;
779 seq_printf(m
, "%*s", size
, "");
783 EXPORT_SYMBOL(seq_pad
);
785 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
787 struct list_head
*lh
;
789 list_for_each(lh
, head
)
795 EXPORT_SYMBOL(seq_list_start
);
797 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
802 return seq_list_start(head
, pos
- 1);
804 EXPORT_SYMBOL(seq_list_start_head
);
806 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
808 struct list_head
*lh
;
810 lh
= ((struct list_head
*)v
)->next
;
812 return lh
== head
? NULL
: lh
;
814 EXPORT_SYMBOL(seq_list_next
);
817 * seq_hlist_start - start an iteration of a hlist
818 * @head: the head of the hlist
819 * @pos: the start position of the sequence
821 * Called at seq_file->op->start().
823 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
825 struct hlist_node
*node
;
827 hlist_for_each(node
, head
)
832 EXPORT_SYMBOL(seq_hlist_start
);
835 * seq_hlist_start_head - 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(). Call this function if you want to
840 * print a header at the top of the output.
842 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
845 return SEQ_START_TOKEN
;
847 return seq_hlist_start(head
, pos
- 1);
849 EXPORT_SYMBOL(seq_hlist_start_head
);
852 * seq_hlist_next - move to the next position of the hlist
853 * @v: the current iterator
854 * @head: the head of the hlist
855 * @ppos: the current position
857 * Called at seq_file->op->next().
859 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
862 struct hlist_node
*node
= v
;
865 if (v
== SEQ_START_TOKEN
)
870 EXPORT_SYMBOL(seq_hlist_next
);
873 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
874 * @head: the head of the hlist
875 * @pos: the start position of the sequence
877 * Called at seq_file->op->start().
879 * This list-traversal primitive may safely run concurrently with
880 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
881 * as long as the traversal is guarded by rcu_read_lock().
883 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
886 struct hlist_node
*node
;
888 __hlist_for_each_rcu(node
, head
)
893 EXPORT_SYMBOL(seq_hlist_start_rcu
);
896 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
897 * @head: the head of the hlist
898 * @pos: the start position of the sequence
900 * Called at seq_file->op->start(). Call this function if you want to
901 * print a header at the top of the output.
903 * This list-traversal primitive may safely run concurrently with
904 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
905 * as long as the traversal is guarded by rcu_read_lock().
907 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
911 return SEQ_START_TOKEN
;
913 return seq_hlist_start_rcu(head
, pos
- 1);
915 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
918 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
919 * @v: the current iterator
920 * @head: the head of the hlist
921 * @ppos: the current position
923 * Called at seq_file->op->next().
925 * This list-traversal primitive may safely run concurrently with
926 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
927 * as long as the traversal is guarded by rcu_read_lock().
929 struct hlist_node
*seq_hlist_next_rcu(void *v
,
930 struct hlist_head
*head
,
933 struct hlist_node
*node
= v
;
936 if (v
== SEQ_START_TOKEN
)
937 return rcu_dereference(head
->first
);
939 return rcu_dereference(node
->next
);
941 EXPORT_SYMBOL(seq_hlist_next_rcu
);
944 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
945 * @head: pointer to percpu array of struct hlist_heads
946 * @cpu: pointer to cpu "cursor"
947 * @pos: start position of sequence
949 * Called at seq_file->op->start().
952 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
954 struct hlist_node
*node
;
956 for_each_possible_cpu(*cpu
) {
957 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
964 EXPORT_SYMBOL(seq_hlist_start_percpu
);
967 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
968 * @v: pointer to current hlist_node
969 * @head: pointer to percpu array of struct hlist_heads
970 * @cpu: pointer to cpu "cursor"
971 * @pos: start position of sequence
973 * Called at seq_file->op->next().
976 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
977 int *cpu
, loff_t
*pos
)
979 struct hlist_node
*node
= v
;
986 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
987 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
988 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
990 if (!hlist_empty(bucket
))
991 return bucket
->first
;
995 EXPORT_SYMBOL(seq_hlist_next_percpu
);