c009e605c7c949505d571829e00b9aba4c8da057
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
);
139 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
140 return !m
->buf
? -ENOMEM
: -EAGAIN
;
144 * seq_read - ->read() method for sequential files.
145 * @file: the file to read from
146 * @buf: the buffer to read to
147 * @size: the maximum number of bytes to read
148 * @ppos: the current position in the file
150 * Ready-made ->f_op->read()
152 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
154 struct seq_file
*m
= file
->private_data
;
161 mutex_lock(&m
->lock
);
164 * seq_file->op->..m_start/m_stop/m_next may do special actions
165 * or optimisations based on the file->f_version, so we want to
166 * pass the file->f_version to those methods.
168 * seq_file->version is just copy of f_version, and seq_file
169 * methods can treat it simply as file version.
170 * It is copied in first and copied out after all operations.
171 * It is convenient to have it as part of structure to avoid the
172 * need of passing another argument to all the seq_file methods.
174 m
->version
= file
->f_version
;
176 /* Don't assume *ppos is where we left it */
177 if (unlikely(*ppos
!= m
->read_pos
)) {
178 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
181 /* With prejudice... */
192 /* grab buffer if we didn't have one */
194 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
198 /* if not empty - flush it first */
200 n
= min(m
->count
, size
);
201 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
216 /* we need at least one record in buffer */
218 p
= m
->op
->start(m
, &pos
);
223 err
= m
->op
->show(m
, p
);
228 if (unlikely(!m
->count
)) {
229 p
= m
->op
->next(m
, p
, &pos
);
233 if (m
->count
< m
->size
)
237 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
243 p
= m
->op
->start(m
, &pos
);
249 /* they want more? let's try to get some more */
250 while (m
->count
< size
) {
251 size_t offs
= m
->count
;
253 p
= m
->op
->next(m
, p
, &next
);
254 if (!p
|| IS_ERR(p
)) {
258 err
= m
->op
->show(m
, p
);
259 if (seq_overflow(m
) || err
) {
261 if (likely(err
<= 0))
267 n
= min(m
->count
, size
);
268 err
= copy_to_user(buf
, m
->buf
, n
);
283 m
->read_pos
+= copied
;
285 file
->f_version
= m
->version
;
286 mutex_unlock(&m
->lock
);
295 EXPORT_SYMBOL(seq_read
);
298 * seq_lseek - ->llseek() method for sequential files.
299 * @file: the file in question
300 * @offset: new position
301 * @whence: 0 for absolute, 1 for relative position
303 * Ready-made ->f_op->llseek()
305 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
307 struct seq_file
*m
= file
->private_data
;
308 loff_t retval
= -EINVAL
;
310 mutex_lock(&m
->lock
);
311 m
->version
= file
->f_version
;
314 offset
+= file
->f_pos
;
319 if (offset
!= m
->read_pos
) {
320 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
323 /* with extreme prejudice... */
330 m
->read_pos
= offset
;
331 retval
= file
->f_pos
= offset
;
334 file
->f_pos
= offset
;
337 file
->f_version
= m
->version
;
338 mutex_unlock(&m
->lock
);
341 EXPORT_SYMBOL(seq_lseek
);
344 * seq_release - free the structures associated with sequential file.
345 * @file: file in question
348 * Frees the structures associated with sequential file; can be used
349 * as ->f_op->release() if you don't have private data to destroy.
351 int seq_release(struct inode
*inode
, struct file
*file
)
353 struct seq_file
*m
= file
->private_data
;
358 EXPORT_SYMBOL(seq_release
);
361 * seq_escape - print string into buffer, escaping some characters
364 * @esc: set of characters that need escaping
366 * Puts string into buffer, replacing each occurrence of character from
367 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
370 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
372 char *end
= m
->buf
+ m
->size
;
376 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
377 if (!strchr(esc
, c
)) {
383 *p
++ = '0' + ((c
& 0300) >> 6);
384 *p
++ = '0' + ((c
& 070) >> 3);
385 *p
++ = '0' + (c
& 07);
391 m
->count
= p
- m
->buf
;
394 EXPORT_SYMBOL(seq_escape
);
396 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
400 if (m
->count
< m
->size
) {
401 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
402 if (m
->count
+ len
< m
->size
) {
410 EXPORT_SYMBOL(seq_vprintf
);
412 int seq_printf(struct seq_file
*m
, const char *f
, ...)
418 ret
= seq_vprintf(m
, f
, args
);
423 EXPORT_SYMBOL(seq_printf
);
426 * mangle_path - mangle and copy path to buffer beginning
428 * @p: beginning of path in above buffer
429 * @esc: set of characters that need escaping
431 * Copy the path from @p to @s, replacing each occurrence of character from
432 * @esc with usual octal escape.
433 * Returns pointer past last written character in @s, or NULL in case of
436 char *mangle_path(char *s
, const char *p
, const char *esc
)
442 } else if (!strchr(esc
, c
)) {
444 } else if (s
+ 4 > p
) {
448 *s
++ = '0' + ((c
& 0300) >> 6);
449 *s
++ = '0' + ((c
& 070) >> 3);
450 *s
++ = '0' + (c
& 07);
455 EXPORT_SYMBOL(mangle_path
);
458 * seq_path - seq_file interface to print a pathname
459 * @m: the seq_file handle
460 * @path: the struct path to print
461 * @esc: set of characters to escape in the output
463 * return the absolute path of 'path', as represented by the
464 * dentry / mnt pair in the path parameter.
466 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
469 size_t size
= seq_get_buf(m
, &buf
);
473 char *p
= d_path(path
, buf
, size
);
475 char *end
= mangle_path(buf
, p
, esc
);
484 EXPORT_SYMBOL(seq_path
);
487 * Same as seq_path, but relative to supplied root.
489 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
490 const struct path
*root
, const char *esc
)
493 size_t size
= seq_get_buf(m
, &buf
);
494 int res
= -ENAMETOOLONG
;
499 p
= __d_path(path
, root
, buf
, size
);
504 char *end
= mangle_path(buf
, p
, esc
);
513 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
517 * returns the path of the 'dentry' from the root of its filesystem.
519 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
522 size_t size
= seq_get_buf(m
, &buf
);
526 char *p
= dentry_path(dentry
, buf
, size
);
528 char *end
= mangle_path(buf
, p
, esc
);
538 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
539 unsigned int nr_bits
)
541 if (m
->count
< m
->size
) {
542 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
543 m
->size
- m
->count
, bits
, nr_bits
);
544 if (m
->count
+ len
< m
->size
) {
552 EXPORT_SYMBOL(seq_bitmap
);
554 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
555 unsigned int nr_bits
)
557 if (m
->count
< m
->size
) {
558 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
559 m
->size
- m
->count
, bits
, nr_bits
);
560 if (m
->count
+ len
< m
->size
) {
568 EXPORT_SYMBOL(seq_bitmap_list
);
570 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
572 return NULL
+ (*pos
== 0);
575 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
581 static void single_stop(struct seq_file
*p
, void *v
)
585 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
588 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
592 op
->start
= single_start
;
593 op
->next
= single_next
;
594 op
->stop
= single_stop
;
596 res
= seq_open(file
, op
);
598 ((struct seq_file
*)file
->private_data
)->private = data
;
604 EXPORT_SYMBOL(single_open
);
606 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
607 void *data
, size_t size
)
609 char *buf
= kmalloc(size
, GFP_KERNEL
);
613 ret
= single_open(file
, show
, data
);
618 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
619 ((struct seq_file
*)file
->private_data
)->size
= size
;
622 EXPORT_SYMBOL(single_open_size
);
624 int single_release(struct inode
*inode
, struct file
*file
)
626 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
627 int res
= seq_release(inode
, file
);
631 EXPORT_SYMBOL(single_release
);
633 int seq_release_private(struct inode
*inode
, struct file
*file
)
635 struct seq_file
*seq
= file
->private_data
;
639 return seq_release(inode
, file
);
641 EXPORT_SYMBOL(seq_release_private
);
643 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
648 struct seq_file
*seq
;
650 private = kzalloc(psize
, GFP_KERNEL
);
654 rc
= seq_open(f
, ops
);
658 seq
= f
->private_data
;
659 seq
->private = private;
667 EXPORT_SYMBOL(__seq_open_private
);
669 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
672 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
674 EXPORT_SYMBOL(seq_open_private
);
676 int seq_putc(struct seq_file
*m
, char c
)
678 if (m
->count
< m
->size
) {
679 m
->buf
[m
->count
++] = c
;
684 EXPORT_SYMBOL(seq_putc
);
686 int seq_puts(struct seq_file
*m
, const char *s
)
689 if (m
->count
+ len
< m
->size
) {
690 memcpy(m
->buf
+ m
->count
, s
, len
);
697 EXPORT_SYMBOL(seq_puts
);
700 * A helper routine for putting decimal numbers without rich format of printf().
701 * only 'unsigned long long' is supported.
702 * This routine will put one byte delimiter + number into seq_file.
703 * This routine is very quick when you show lots of numbers.
704 * In usual cases, it will be better to use seq_printf(). It's easier to read.
706 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
707 unsigned long long num
)
711 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
715 m
->buf
[m
->count
++] = delimiter
;
718 m
->buf
[m
->count
++] = num
+ '0';
722 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
731 EXPORT_SYMBOL(seq_put_decimal_ull
);
733 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
737 if (m
->count
+ 3 >= m
->size
) {
742 m
->buf
[m
->count
++] = delimiter
;
746 return seq_put_decimal_ull(m
, delimiter
, num
);
749 EXPORT_SYMBOL(seq_put_decimal_ll
);
752 * seq_write - write arbitrary data to buffer
753 * @seq: seq_file identifying the buffer to which data should be written
754 * @data: data address
755 * @len: number of bytes
757 * Return 0 on success, non-zero otherwise.
759 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
761 if (seq
->count
+ len
< seq
->size
) {
762 memcpy(seq
->buf
+ seq
->count
, data
, len
);
766 seq_set_overflow(seq
);
769 EXPORT_SYMBOL(seq_write
);
771 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
773 struct list_head
*lh
;
775 list_for_each(lh
, head
)
781 EXPORT_SYMBOL(seq_list_start
);
783 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
788 return seq_list_start(head
, pos
- 1);
790 EXPORT_SYMBOL(seq_list_start_head
);
792 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
794 struct list_head
*lh
;
796 lh
= ((struct list_head
*)v
)->next
;
798 return lh
== head
? NULL
: lh
;
800 EXPORT_SYMBOL(seq_list_next
);
803 * seq_hlist_start - start an iteration of a hlist
804 * @head: the head of the hlist
805 * @pos: the start position of the sequence
807 * Called at seq_file->op->start().
809 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
811 struct hlist_node
*node
;
813 hlist_for_each(node
, head
)
818 EXPORT_SYMBOL(seq_hlist_start
);
821 * seq_hlist_start_head - start an iteration of a hlist
822 * @head: the head of the hlist
823 * @pos: the start position of the sequence
825 * Called at seq_file->op->start(). Call this function if you want to
826 * print a header at the top of the output.
828 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
831 return SEQ_START_TOKEN
;
833 return seq_hlist_start(head
, pos
- 1);
835 EXPORT_SYMBOL(seq_hlist_start_head
);
838 * seq_hlist_next - move to the next position of the hlist
839 * @v: the current iterator
840 * @head: the head of the hlist
841 * @ppos: the current position
843 * Called at seq_file->op->next().
845 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
848 struct hlist_node
*node
= v
;
851 if (v
== SEQ_START_TOKEN
)
856 EXPORT_SYMBOL(seq_hlist_next
);
859 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
860 * @head: the head of the hlist
861 * @pos: the start position of the sequence
863 * Called at seq_file->op->start().
865 * This list-traversal primitive may safely run concurrently with
866 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
867 * as long as the traversal is guarded by rcu_read_lock().
869 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
872 struct hlist_node
*node
;
874 __hlist_for_each_rcu(node
, head
)
879 EXPORT_SYMBOL(seq_hlist_start_rcu
);
882 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
883 * @head: the head of the hlist
884 * @pos: the start position of the sequence
886 * Called at seq_file->op->start(). Call this function if you want to
887 * print a header at the top of the output.
889 * This list-traversal primitive may safely run concurrently with
890 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
891 * as long as the traversal is guarded by rcu_read_lock().
893 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
897 return SEQ_START_TOKEN
;
899 return seq_hlist_start_rcu(head
, pos
- 1);
901 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
904 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
905 * @v: the current iterator
906 * @head: the head of the hlist
907 * @ppos: the current position
909 * Called at seq_file->op->next().
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_next_rcu(void *v
,
916 struct hlist_head
*head
,
919 struct hlist_node
*node
= v
;
922 if (v
== SEQ_START_TOKEN
)
923 return rcu_dereference(head
->first
);
925 return rcu_dereference(node
->next
);
927 EXPORT_SYMBOL(seq_hlist_next_rcu
);