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
)
42 buf
= kmalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
47 * seq_open - initialize sequential file
48 * @file: file we initialize
49 * @op: method table describing the sequence
51 * seq_open() sets @file, associating it with a sequence described
52 * by @op. @op->start() sets the iterator up and returns the first
53 * element of sequence. @op->stop() shuts it down. @op->next()
54 * returns the next element of sequence. @op->show() prints element
55 * into the buffer. In case of error ->start() and ->next() return
56 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
57 * returns 0 in case of success and negative number in case of error.
58 * Returning SEQ_SKIP means "discard this element and move on".
60 int seq_open(struct file
*file
, const struct seq_operations
*op
)
62 struct seq_file
*p
= file
->private_data
;
65 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
68 file
->private_data
= p
;
70 memset(p
, 0, sizeof(*p
));
74 p
->user_ns
= file
->f_cred
->user_ns
;
78 * Wrappers around seq_open(e.g. swaps_open) need to be
79 * aware of this. If they set f_version themselves, they
80 * should call seq_open first and then set f_version.
85 * seq_files support lseek() and pread(). They do not implement
86 * write() at all, but we clear FMODE_PWRITE here for historical
89 * If a client of seq_files a) implements file.write() and b) wishes to
90 * support pwrite() then that client will need to implement its own
91 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
93 file
->f_mode
&= ~FMODE_PWRITE
;
96 EXPORT_SYMBOL(seq_open
);
98 static int traverse(struct seq_file
*m
, loff_t offset
)
100 loff_t pos
= 0, index
;
106 m
->count
= m
->from
= 0;
112 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
116 p
= m
->op
->start(m
, &index
);
121 error
= m
->op
->show(m
, p
);
124 if (unlikely(error
)) {
130 if (pos
+ m
->count
> offset
) {
131 m
->from
= offset
- pos
;
143 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
);
227 /* we need at least one record in buffer */
229 p
= m
->op
->start(m
, &pos
);
234 err
= m
->op
->show(m
, p
);
239 if (unlikely(!m
->count
)) {
240 p
= m
->op
->next(m
, p
, &pos
);
244 if (m
->count
< m
->size
)
248 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
254 p
= m
->op
->start(m
, &pos
);
260 /* they want more? let's try to get some more */
261 while (m
->count
< size
) {
262 size_t offs
= m
->count
;
264 p
= m
->op
->next(m
, p
, &next
);
265 if (!p
|| IS_ERR(p
)) {
269 err
= m
->op
->show(m
, p
);
270 if (seq_overflow(m
) || err
) {
272 if (likely(err
<= 0))
278 n
= min(m
->count
, size
);
279 err
= copy_to_user(buf
, m
->buf
, n
);
294 m
->read_pos
+= copied
;
296 file
->f_version
= m
->version
;
297 mutex_unlock(&m
->lock
);
306 EXPORT_SYMBOL(seq_read
);
309 * seq_lseek - ->llseek() method for sequential files.
310 * @file: the file in question
311 * @offset: new position
312 * @whence: 0 for absolute, 1 for relative position
314 * Ready-made ->f_op->llseek()
316 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
318 struct seq_file
*m
= file
->private_data
;
319 loff_t retval
= -EINVAL
;
321 mutex_lock(&m
->lock
);
322 m
->version
= file
->f_version
;
325 offset
+= file
->f_pos
;
330 if (offset
!= m
->read_pos
) {
331 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
334 /* with extreme prejudice... */
341 m
->read_pos
= offset
;
342 retval
= file
->f_pos
= offset
;
346 file
->f_version
= m
->version
;
347 mutex_unlock(&m
->lock
);
350 EXPORT_SYMBOL(seq_lseek
);
353 * seq_release - free the structures associated with sequential file.
354 * @file: file in question
357 * Frees the structures associated with sequential file; can be used
358 * as ->f_op->release() if you don't have private data to destroy.
360 int seq_release(struct inode
*inode
, struct file
*file
)
362 struct seq_file
*m
= file
->private_data
;
367 EXPORT_SYMBOL(seq_release
);
370 * seq_escape - print string into buffer, escaping some characters
373 * @esc: set of characters that need escaping
375 * Puts string into buffer, replacing each occurrence of character from
376 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
379 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
381 char *end
= m
->buf
+ m
->size
;
385 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
386 if (!strchr(esc
, c
)) {
392 *p
++ = '0' + ((c
& 0300) >> 6);
393 *p
++ = '0' + ((c
& 070) >> 3);
394 *p
++ = '0' + (c
& 07);
400 m
->count
= p
- m
->buf
;
403 EXPORT_SYMBOL(seq_escape
);
405 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
409 if (m
->count
< m
->size
) {
410 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
411 if (m
->count
+ len
< m
->size
) {
419 EXPORT_SYMBOL(seq_vprintf
);
421 int seq_printf(struct seq_file
*m
, const char *f
, ...)
427 ret
= seq_vprintf(m
, f
, args
);
432 EXPORT_SYMBOL(seq_printf
);
435 * mangle_path - mangle and copy path to buffer beginning
437 * @p: beginning of path in above buffer
438 * @esc: set of characters that need escaping
440 * Copy the path from @p to @s, replacing each occurrence of character from
441 * @esc with usual octal escape.
442 * Returns pointer past last written character in @s, or NULL in case of
445 char *mangle_path(char *s
, const char *p
, const char *esc
)
451 } else if (!strchr(esc
, c
)) {
453 } else if (s
+ 4 > p
) {
457 *s
++ = '0' + ((c
& 0300) >> 6);
458 *s
++ = '0' + ((c
& 070) >> 3);
459 *s
++ = '0' + (c
& 07);
464 EXPORT_SYMBOL(mangle_path
);
467 * seq_path - seq_file interface to print a pathname
468 * @m: the seq_file handle
469 * @path: the struct path to print
470 * @esc: set of characters to escape in the output
472 * return the absolute path of 'path', as represented by the
473 * dentry / mnt pair in the path parameter.
475 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
478 size_t size
= seq_get_buf(m
, &buf
);
482 char *p
= d_path(path
, buf
, size
);
484 char *end
= mangle_path(buf
, p
, esc
);
493 EXPORT_SYMBOL(seq_path
);
496 * Same as seq_path, but relative to supplied root.
498 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
499 const struct path
*root
, const char *esc
)
502 size_t size
= seq_get_buf(m
, &buf
);
503 int res
= -ENAMETOOLONG
;
508 p
= __d_path(path
, root
, buf
, size
);
513 char *end
= mangle_path(buf
, p
, esc
);
522 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
526 * returns the path of the 'dentry' from the root of its filesystem.
528 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
531 size_t size
= seq_get_buf(m
, &buf
);
535 char *p
= dentry_path(dentry
, buf
, size
);
537 char *end
= mangle_path(buf
, p
, esc
);
547 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
548 unsigned int nr_bits
)
550 if (m
->count
< m
->size
) {
551 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
552 m
->size
- m
->count
, bits
, nr_bits
);
553 if (m
->count
+ len
< m
->size
) {
561 EXPORT_SYMBOL(seq_bitmap
);
563 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
564 unsigned int nr_bits
)
566 if (m
->count
< m
->size
) {
567 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
568 m
->size
- m
->count
, bits
, nr_bits
);
569 if (m
->count
+ len
< m
->size
) {
577 EXPORT_SYMBOL(seq_bitmap_list
);
579 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
581 return NULL
+ (*pos
== 0);
584 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
590 static void single_stop(struct seq_file
*p
, void *v
)
594 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
597 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
601 op
->start
= single_start
;
602 op
->next
= single_next
;
603 op
->stop
= single_stop
;
605 res
= seq_open(file
, op
);
607 ((struct seq_file
*)file
->private_data
)->private = data
;
613 EXPORT_SYMBOL(single_open
);
615 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
616 void *data
, size_t size
)
618 char *buf
= seq_buf_alloc(size
);
622 ret
= single_open(file
, show
, data
);
627 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
628 ((struct seq_file
*)file
->private_data
)->size
= size
;
631 EXPORT_SYMBOL(single_open_size
);
633 int single_release(struct inode
*inode
, struct file
*file
)
635 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
636 int res
= seq_release(inode
, file
);
640 EXPORT_SYMBOL(single_release
);
642 int seq_release_private(struct inode
*inode
, struct file
*file
)
644 struct seq_file
*seq
= file
->private_data
;
648 return seq_release(inode
, file
);
650 EXPORT_SYMBOL(seq_release_private
);
652 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
657 struct seq_file
*seq
;
659 private = kzalloc(psize
, GFP_KERNEL
);
663 rc
= seq_open(f
, ops
);
667 seq
= f
->private_data
;
668 seq
->private = private;
676 EXPORT_SYMBOL(__seq_open_private
);
678 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
681 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
683 EXPORT_SYMBOL(seq_open_private
);
685 int seq_putc(struct seq_file
*m
, char c
)
687 if (m
->count
< m
->size
) {
688 m
->buf
[m
->count
++] = c
;
693 EXPORT_SYMBOL(seq_putc
);
695 int seq_puts(struct seq_file
*m
, const char *s
)
698 if (m
->count
+ len
< m
->size
) {
699 memcpy(m
->buf
+ m
->count
, s
, len
);
706 EXPORT_SYMBOL(seq_puts
);
709 * A helper routine for putting decimal numbers without rich format of printf().
710 * only 'unsigned long long' is supported.
711 * This routine will put one byte delimiter + number into seq_file.
712 * This routine is very quick when you show lots of numbers.
713 * In usual cases, it will be better to use seq_printf(). It's easier to read.
715 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
716 unsigned long long num
)
720 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
724 m
->buf
[m
->count
++] = delimiter
;
727 m
->buf
[m
->count
++] = num
+ '0';
731 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
740 EXPORT_SYMBOL(seq_put_decimal_ull
);
742 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
746 if (m
->count
+ 3 >= m
->size
) {
751 m
->buf
[m
->count
++] = delimiter
;
755 return seq_put_decimal_ull(m
, delimiter
, num
);
758 EXPORT_SYMBOL(seq_put_decimal_ll
);
761 * seq_write - write arbitrary data to buffer
762 * @seq: seq_file identifying the buffer to which data should be written
763 * @data: data address
764 * @len: number of bytes
766 * Return 0 on success, non-zero otherwise.
768 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
770 if (seq
->count
+ len
< seq
->size
) {
771 memcpy(seq
->buf
+ seq
->count
, data
, len
);
775 seq_set_overflow(seq
);
778 EXPORT_SYMBOL(seq_write
);
780 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
782 struct list_head
*lh
;
784 list_for_each(lh
, head
)
790 EXPORT_SYMBOL(seq_list_start
);
792 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
797 return seq_list_start(head
, pos
- 1);
799 EXPORT_SYMBOL(seq_list_start_head
);
801 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
803 struct list_head
*lh
;
805 lh
= ((struct list_head
*)v
)->next
;
807 return lh
== head
? NULL
: lh
;
809 EXPORT_SYMBOL(seq_list_next
);
812 * seq_hlist_start - start an iteration of a hlist
813 * @head: the head of the hlist
814 * @pos: the start position of the sequence
816 * Called at seq_file->op->start().
818 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
820 struct hlist_node
*node
;
822 hlist_for_each(node
, head
)
827 EXPORT_SYMBOL(seq_hlist_start
);
830 * seq_hlist_start_head - start an iteration of a hlist
831 * @head: the head of the hlist
832 * @pos: the start position of the sequence
834 * Called at seq_file->op->start(). Call this function if you want to
835 * print a header at the top of the output.
837 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
840 return SEQ_START_TOKEN
;
842 return seq_hlist_start(head
, pos
- 1);
844 EXPORT_SYMBOL(seq_hlist_start_head
);
847 * seq_hlist_next - move to the next position of the hlist
848 * @v: the current iterator
849 * @head: the head of the hlist
850 * @ppos: the current position
852 * Called at seq_file->op->next().
854 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
857 struct hlist_node
*node
= v
;
860 if (v
== SEQ_START_TOKEN
)
865 EXPORT_SYMBOL(seq_hlist_next
);
868 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
869 * @head: the head of the hlist
870 * @pos: the start position of the sequence
872 * Called at seq_file->op->start().
874 * This list-traversal primitive may safely run concurrently with
875 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
876 * as long as the traversal is guarded by rcu_read_lock().
878 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
881 struct hlist_node
*node
;
883 __hlist_for_each_rcu(node
, head
)
888 EXPORT_SYMBOL(seq_hlist_start_rcu
);
891 * seq_hlist_start_head_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(). Call this function if you want to
896 * print a header at the top of the output.
898 * This list-traversal primitive may safely run concurrently with
899 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
900 * as long as the traversal is guarded by rcu_read_lock().
902 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
906 return SEQ_START_TOKEN
;
908 return seq_hlist_start_rcu(head
, pos
- 1);
910 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
913 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
914 * @v: the current iterator
915 * @head: the head of the hlist
916 * @ppos: the current position
918 * Called at seq_file->op->next().
920 * This list-traversal primitive may safely run concurrently with
921 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
922 * as long as the traversal is guarded by rcu_read_lock().
924 struct hlist_node
*seq_hlist_next_rcu(void *v
,
925 struct hlist_head
*head
,
928 struct hlist_node
*node
= v
;
931 if (v
== SEQ_START_TOKEN
)
932 return rcu_dereference(head
->first
);
934 return rcu_dereference(node
->next
);
936 EXPORT_SYMBOL(seq_hlist_next_rcu
);