2cee82b900a12958c866b85912c4fb4b7b5b2e5e
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
);
151 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
152 return !m
->buf
? -ENOMEM
: -EAGAIN
;
156 * seq_read - ->read() method for sequential files.
157 * @file: the file to read from
158 * @buf: the buffer to read to
159 * @size: the maximum number of bytes to read
160 * @ppos: the current position in the file
162 * Ready-made ->f_op->read()
164 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
166 struct seq_file
*m
= file
->private_data
;
173 mutex_lock(&m
->lock
);
176 * seq_file->op->..m_start/m_stop/m_next may do special actions
177 * or optimisations based on the file->f_version, so we want to
178 * pass the file->f_version to those methods.
180 * seq_file->version is just copy of f_version, and seq_file
181 * methods can treat it simply as file version.
182 * It is copied in first and copied out after all operations.
183 * It is convenient to have it as part of structure to avoid the
184 * need of passing another argument to all the seq_file methods.
186 m
->version
= file
->f_version
;
188 /* Don't assume *ppos is where we left it */
189 if (unlikely(*ppos
!= m
->read_pos
)) {
190 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
193 /* With prejudice... */
204 /* grab buffer if we didn't have one */
206 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
210 /* if not empty - flush it first */
212 n
= min(m
->count
, size
);
213 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
228 /* we need at least one record in buffer */
230 p
= m
->op
->start(m
, &pos
);
235 err
= m
->op
->show(m
, p
);
240 if (unlikely(!m
->count
)) {
241 p
= m
->op
->next(m
, p
, &pos
);
245 if (m
->count
< m
->size
)
249 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
255 p
= m
->op
->start(m
, &pos
);
261 /* they want more? let's try to get some more */
262 while (m
->count
< size
) {
263 size_t offs
= m
->count
;
265 p
= m
->op
->next(m
, p
, &next
);
266 if (!p
|| IS_ERR(p
)) {
270 err
= m
->op
->show(m
, p
);
271 if (seq_overflow(m
) || err
) {
273 if (likely(err
<= 0))
279 n
= min(m
->count
, size
);
280 err
= copy_to_user(buf
, m
->buf
, n
);
295 m
->read_pos
+= copied
;
297 file
->f_version
= m
->version
;
298 mutex_unlock(&m
->lock
);
307 EXPORT_SYMBOL(seq_read
);
310 * seq_lseek - ->llseek() method for sequential files.
311 * @file: the file in question
312 * @offset: new position
313 * @whence: 0 for absolute, 1 for relative position
315 * Ready-made ->f_op->llseek()
317 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
319 struct seq_file
*m
= file
->private_data
;
320 loff_t retval
= -EINVAL
;
322 mutex_lock(&m
->lock
);
323 m
->version
= file
->f_version
;
326 offset
+= file
->f_pos
;
331 if (offset
!= m
->read_pos
) {
332 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
335 /* with extreme prejudice... */
342 m
->read_pos
= offset
;
343 retval
= file
->f_pos
= offset
;
346 file
->f_pos
= offset
;
349 file
->f_version
= m
->version
;
350 mutex_unlock(&m
->lock
);
353 EXPORT_SYMBOL(seq_lseek
);
356 * seq_release - free the structures associated with sequential file.
357 * @file: file in question
360 * Frees the structures associated with sequential file; can be used
361 * as ->f_op->release() if you don't have private data to destroy.
363 int seq_release(struct inode
*inode
, struct file
*file
)
365 struct seq_file
*m
= file
->private_data
;
370 EXPORT_SYMBOL(seq_release
);
373 * seq_escape - print string into buffer, escaping some characters
376 * @esc: set of characters that need escaping
378 * Puts string into buffer, replacing each occurrence of character from
379 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
382 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
384 char *end
= m
->buf
+ m
->size
;
388 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
389 if (!strchr(esc
, c
)) {
395 *p
++ = '0' + ((c
& 0300) >> 6);
396 *p
++ = '0' + ((c
& 070) >> 3);
397 *p
++ = '0' + (c
& 07);
403 m
->count
= p
- m
->buf
;
406 EXPORT_SYMBOL(seq_escape
);
408 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
412 if (m
->count
< m
->size
) {
413 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
414 if (m
->count
+ len
< m
->size
) {
422 EXPORT_SYMBOL(seq_vprintf
);
424 int seq_printf(struct seq_file
*m
, const char *f
, ...)
430 ret
= seq_vprintf(m
, f
, args
);
435 EXPORT_SYMBOL(seq_printf
);
438 * mangle_path - mangle and copy path to buffer beginning
440 * @p: beginning of path in above buffer
441 * @esc: set of characters that need escaping
443 * Copy the path from @p to @s, replacing each occurrence of character from
444 * @esc with usual octal escape.
445 * Returns pointer past last written character in @s, or NULL in case of
448 char *mangle_path(char *s
, const char *p
, const char *esc
)
454 } else if (!strchr(esc
, c
)) {
456 } else if (s
+ 4 > p
) {
460 *s
++ = '0' + ((c
& 0300) >> 6);
461 *s
++ = '0' + ((c
& 070) >> 3);
462 *s
++ = '0' + (c
& 07);
467 EXPORT_SYMBOL(mangle_path
);
470 * seq_path - seq_file interface to print a pathname
471 * @m: the seq_file handle
472 * @path: the struct path to print
473 * @esc: set of characters to escape in the output
475 * return the absolute path of 'path', as represented by the
476 * dentry / mnt pair in the path parameter.
478 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
481 size_t size
= seq_get_buf(m
, &buf
);
485 char *p
= d_path(path
, buf
, size
);
487 char *end
= mangle_path(buf
, p
, esc
);
496 EXPORT_SYMBOL(seq_path
);
499 * Same as seq_path, but relative to supplied root.
501 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
502 const struct path
*root
, const char *esc
)
505 size_t size
= seq_get_buf(m
, &buf
);
506 int res
= -ENAMETOOLONG
;
511 p
= __d_path(path
, root
, buf
, size
);
516 char *end
= mangle_path(buf
, p
, esc
);
525 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
529 * returns the path of the 'dentry' from the root of its filesystem.
531 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
534 size_t size
= seq_get_buf(m
, &buf
);
538 char *p
= dentry_path(dentry
, buf
, size
);
540 char *end
= mangle_path(buf
, p
, esc
);
550 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
551 unsigned int nr_bits
)
553 if (m
->count
< m
->size
) {
554 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
555 m
->size
- m
->count
, bits
, nr_bits
);
556 if (m
->count
+ len
< m
->size
) {
564 EXPORT_SYMBOL(seq_bitmap
);
566 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
567 unsigned int nr_bits
)
569 if (m
->count
< m
->size
) {
570 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
571 m
->size
- m
->count
, bits
, nr_bits
);
572 if (m
->count
+ len
< m
->size
) {
580 EXPORT_SYMBOL(seq_bitmap_list
);
582 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
584 return NULL
+ (*pos
== 0);
587 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
593 static void single_stop(struct seq_file
*p
, void *v
)
597 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
600 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
604 op
->start
= single_start
;
605 op
->next
= single_next
;
606 op
->stop
= single_stop
;
608 res
= seq_open(file
, op
);
610 ((struct seq_file
*)file
->private_data
)->private = data
;
616 EXPORT_SYMBOL(single_open
);
618 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
619 void *data
, size_t size
)
621 char *buf
= seq_buf_alloc(size
);
625 ret
= single_open(file
, show
, data
);
630 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
631 ((struct seq_file
*)file
->private_data
)->size
= size
;
634 EXPORT_SYMBOL(single_open_size
);
636 int single_release(struct inode
*inode
, struct file
*file
)
638 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
639 int res
= seq_release(inode
, file
);
643 EXPORT_SYMBOL(single_release
);
645 int seq_release_private(struct inode
*inode
, struct file
*file
)
647 struct seq_file
*seq
= file
->private_data
;
651 return seq_release(inode
, file
);
653 EXPORT_SYMBOL(seq_release_private
);
655 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
660 struct seq_file
*seq
;
662 private = kzalloc(psize
, GFP_KERNEL
);
666 rc
= seq_open(f
, ops
);
670 seq
= f
->private_data
;
671 seq
->private = private;
679 EXPORT_SYMBOL(__seq_open_private
);
681 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
684 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
686 EXPORT_SYMBOL(seq_open_private
);
688 int seq_putc(struct seq_file
*m
, char c
)
690 if (m
->count
< m
->size
) {
691 m
->buf
[m
->count
++] = c
;
696 EXPORT_SYMBOL(seq_putc
);
698 int seq_puts(struct seq_file
*m
, const char *s
)
701 if (m
->count
+ len
< m
->size
) {
702 memcpy(m
->buf
+ m
->count
, s
, len
);
709 EXPORT_SYMBOL(seq_puts
);
712 * A helper routine for putting decimal numbers without rich format of printf().
713 * only 'unsigned long long' is supported.
714 * This routine will put one byte delimiter + number into seq_file.
715 * This routine is very quick when you show lots of numbers.
716 * In usual cases, it will be better to use seq_printf(). It's easier to read.
718 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
719 unsigned long long num
)
723 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
727 m
->buf
[m
->count
++] = delimiter
;
730 m
->buf
[m
->count
++] = num
+ '0';
734 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
743 EXPORT_SYMBOL(seq_put_decimal_ull
);
745 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
749 if (m
->count
+ 3 >= m
->size
) {
754 m
->buf
[m
->count
++] = delimiter
;
758 return seq_put_decimal_ull(m
, delimiter
, num
);
761 EXPORT_SYMBOL(seq_put_decimal_ll
);
764 * seq_write - write arbitrary data to buffer
765 * @seq: seq_file identifying the buffer to which data should be written
766 * @data: data address
767 * @len: number of bytes
769 * Return 0 on success, non-zero otherwise.
771 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
773 if (seq
->count
+ len
< seq
->size
) {
774 memcpy(seq
->buf
+ seq
->count
, data
, len
);
778 seq_set_overflow(seq
);
781 EXPORT_SYMBOL(seq_write
);
783 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
785 struct list_head
*lh
;
787 list_for_each(lh
, head
)
793 EXPORT_SYMBOL(seq_list_start
);
795 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
800 return seq_list_start(head
, pos
- 1);
802 EXPORT_SYMBOL(seq_list_start_head
);
804 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
806 struct list_head
*lh
;
808 lh
= ((struct list_head
*)v
)->next
;
810 return lh
== head
? NULL
: lh
;
812 EXPORT_SYMBOL(seq_list_next
);
815 * seq_hlist_start - start an iteration of a hlist
816 * @head: the head of the hlist
817 * @pos: the start position of the sequence
819 * Called at seq_file->op->start().
821 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
823 struct hlist_node
*node
;
825 hlist_for_each(node
, head
)
830 EXPORT_SYMBOL(seq_hlist_start
);
833 * seq_hlist_start_head - start an iteration of a hlist
834 * @head: the head of the hlist
835 * @pos: the start position of the sequence
837 * Called at seq_file->op->start(). Call this function if you want to
838 * print a header at the top of the output.
840 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
843 return SEQ_START_TOKEN
;
845 return seq_hlist_start(head
, pos
- 1);
847 EXPORT_SYMBOL(seq_hlist_start_head
);
850 * seq_hlist_next - move to the next position of the hlist
851 * @v: the current iterator
852 * @head: the head of the hlist
853 * @ppos: the current position
855 * Called at seq_file->op->next().
857 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
860 struct hlist_node
*node
= v
;
863 if (v
== SEQ_START_TOKEN
)
868 EXPORT_SYMBOL(seq_hlist_next
);
871 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
872 * @head: the head of the hlist
873 * @pos: the start position of the sequence
875 * Called at seq_file->op->start().
877 * This list-traversal primitive may safely run concurrently with
878 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
879 * as long as the traversal is guarded by rcu_read_lock().
881 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
884 struct hlist_node
*node
;
886 __hlist_for_each_rcu(node
, head
)
891 EXPORT_SYMBOL(seq_hlist_start_rcu
);
894 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
895 * @head: the head of the hlist
896 * @pos: the start position of the sequence
898 * Called at seq_file->op->start(). Call this function if you want to
899 * print a header at the top of the output.
901 * This list-traversal primitive may safely run concurrently with
902 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
903 * as long as the traversal is guarded by rcu_read_lock().
905 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
909 return SEQ_START_TOKEN
;
911 return seq_hlist_start_rcu(head
, pos
- 1);
913 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
916 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
917 * @v: the current iterator
918 * @head: the head of the hlist
919 * @ppos: the current position
921 * Called at seq_file->op->next().
923 * This list-traversal primitive may safely run concurrently with
924 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
925 * as long as the traversal is guarded by rcu_read_lock().
927 struct hlist_node
*seq_hlist_next_rcu(void *v
,
928 struct hlist_head
*head
,
931 struct hlist_node
*node
= v
;
934 if (v
== SEQ_START_TOKEN
)
935 return rcu_dereference(head
->first
);
937 return rcu_dereference(node
->next
);
939 EXPORT_SYMBOL(seq_hlist_next_rcu
);