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>
13 #include <asm/uaccess.h>
18 * seq_files have a buffer which can may overflow. When this happens a larger
19 * buffer is reallocated and all the data will be printed again.
20 * The overflow state is true when m->count == m->size.
22 static bool seq_overflow(struct seq_file
*m
)
24 return m
->count
== m
->size
;
27 static void seq_set_overflow(struct seq_file
*m
)
33 * seq_open - initialize sequential file
34 * @file: file we initialize
35 * @op: method table describing the sequence
37 * seq_open() sets @file, associating it with a sequence described
38 * by @op. @op->start() sets the iterator up and returns the first
39 * element of sequence. @op->stop() shuts it down. @op->next()
40 * returns the next element of sequence. @op->show() prints element
41 * into the buffer. In case of error ->start() and ->next() return
42 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
43 * returns 0 in case of success and negative number in case of error.
44 * Returning SEQ_SKIP means "discard this element and move on".
46 int seq_open(struct file
*file
, const struct seq_operations
*op
)
48 struct seq_file
*p
= file
->private_data
;
51 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
54 file
->private_data
= p
;
56 memset(p
, 0, sizeof(*p
));
61 * Wrappers around seq_open(e.g. swaps_open) need to be
62 * aware of this. If they set f_version themselves, they
63 * should call seq_open first and then set f_version.
68 * seq_files support lseek() and pread(). They do not implement
69 * write() at all, but we clear FMODE_PWRITE here for historical
72 * If a client of seq_files a) implements file.write() and b) wishes to
73 * support pwrite() then that client will need to implement its own
74 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
76 file
->f_mode
&= ~FMODE_PWRITE
;
79 EXPORT_SYMBOL(seq_open
);
81 static int traverse(struct seq_file
*m
, loff_t offset
)
83 loff_t pos
= 0, index
;
89 m
->count
= m
->from
= 0;
95 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
99 p
= m
->op
->start(m
, &index
);
104 error
= m
->op
->show(m
, p
);
107 if (unlikely(error
)) {
113 if (pos
+ m
->count
> offset
) {
114 m
->from
= offset
- pos
;
126 p
= m
->op
->next(m
, p
, &index
);
135 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
136 return !m
->buf
? -ENOMEM
: -EAGAIN
;
140 * seq_read - ->read() method for sequential files.
141 * @file: the file to read from
142 * @buf: the buffer to read to
143 * @size: the maximum number of bytes to read
144 * @ppos: the current position in the file
146 * Ready-made ->f_op->read()
148 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
150 struct seq_file
*m
= file
->private_data
;
157 mutex_lock(&m
->lock
);
160 * seq_file->op->..m_start/m_stop/m_next may do special actions
161 * or optimisations based on the file->f_version, so we want to
162 * pass the file->f_version to those methods.
164 * seq_file->version is just copy of f_version, and seq_file
165 * methods can treat it simply as file version.
166 * It is copied in first and copied out after all operations.
167 * It is convenient to have it as part of structure to avoid the
168 * need of passing another argument to all the seq_file methods.
170 m
->version
= file
->f_version
;
172 /* Don't assume *ppos is where we left it */
173 if (unlikely(*ppos
!= m
->read_pos
)) {
174 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
177 /* With prejudice... */
188 /* grab buffer if we didn't have one */
190 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
194 /* if not empty - flush it first */
196 n
= min(m
->count
, size
);
197 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
210 /* we need at least one record in buffer */
212 p
= m
->op
->start(m
, &pos
);
217 err
= m
->op
->show(m
, p
);
222 if (unlikely(!m
->count
)) {
223 p
= m
->op
->next(m
, p
, &pos
);
227 if (m
->count
< m
->size
)
231 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
237 p
= m
->op
->start(m
, &pos
);
243 /* they want more? let's try to get some more */
244 while (m
->count
< size
) {
245 size_t offs
= m
->count
;
247 p
= m
->op
->next(m
, p
, &next
);
248 if (!p
|| IS_ERR(p
)) {
252 err
= m
->op
->show(m
, p
);
253 if (seq_overflow(m
) || err
) {
255 if (likely(err
<= 0))
261 n
= min(m
->count
, size
);
262 err
= copy_to_user(buf
, m
->buf
, n
);
277 m
->read_pos
+= copied
;
279 file
->f_version
= m
->version
;
280 mutex_unlock(&m
->lock
);
289 EXPORT_SYMBOL(seq_read
);
292 * seq_lseek - ->llseek() method for sequential files.
293 * @file: the file in question
294 * @offset: new position
295 * @origin: 0 for absolute, 1 for relative position
297 * Ready-made ->f_op->llseek()
299 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
301 struct seq_file
*m
= file
->private_data
;
302 loff_t retval
= -EINVAL
;
304 mutex_lock(&m
->lock
);
305 m
->version
= file
->f_version
;
308 offset
+= file
->f_pos
;
313 if (offset
!= m
->read_pos
) {
314 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
317 /* with extreme prejudice... */
324 m
->read_pos
= offset
;
325 retval
= file
->f_pos
= offset
;
329 file
->f_version
= m
->version
;
330 mutex_unlock(&m
->lock
);
333 EXPORT_SYMBOL(seq_lseek
);
336 * seq_release - free the structures associated with sequential file.
337 * @file: file in question
338 * @inode: file->f_path.dentry->d_inode
340 * Frees the structures associated with sequential file; can be used
341 * as ->f_op->release() if you don't have private data to destroy.
343 int seq_release(struct inode
*inode
, struct file
*file
)
345 struct seq_file
*m
= file
->private_data
;
350 EXPORT_SYMBOL(seq_release
);
353 * seq_escape - print string into buffer, escaping some characters
356 * @esc: set of characters that need escaping
358 * Puts string into buffer, replacing each occurrence of character from
359 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
362 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
364 char *end
= m
->buf
+ m
->size
;
368 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
369 if (!strchr(esc
, c
)) {
375 *p
++ = '0' + ((c
& 0300) >> 6);
376 *p
++ = '0' + ((c
& 070) >> 3);
377 *p
++ = '0' + (c
& 07);
383 m
->count
= p
- m
->buf
;
386 EXPORT_SYMBOL(seq_escape
);
388 int seq_printf(struct seq_file
*m
, const char *f
, ...)
393 if (m
->count
< m
->size
) {
395 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
397 if (m
->count
+ len
< m
->size
) {
405 EXPORT_SYMBOL(seq_printf
);
408 * mangle_path - mangle and copy path to buffer beginning
410 * @p: beginning of path in above buffer
411 * @esc: set of characters that need escaping
413 * Copy the path from @p to @s, replacing each occurrence of character from
414 * @esc with usual octal escape.
415 * Returns pointer past last written character in @s, or NULL in case of
418 char *mangle_path(char *s
, const char *p
, const char *esc
)
424 } else if (!strchr(esc
, c
)) {
426 } else if (s
+ 4 > p
) {
430 *s
++ = '0' + ((c
& 0300) >> 6);
431 *s
++ = '0' + ((c
& 070) >> 3);
432 *s
++ = '0' + (c
& 07);
437 EXPORT_SYMBOL(mangle_path
);
440 * seq_path - seq_file interface to print a pathname
441 * @m: the seq_file handle
442 * @path: the struct path to print
443 * @esc: set of characters to escape in the output
445 * return the absolute path of 'path', as represented by the
446 * dentry / mnt pair in the path parameter.
448 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
451 size_t size
= seq_get_buf(m
, &buf
);
455 char *p
= d_path(path
, buf
, size
);
457 char *end
= mangle_path(buf
, p
, esc
);
466 EXPORT_SYMBOL(seq_path
);
469 * Same as seq_path, but relative to supplied root.
471 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
472 const struct path
*root
, const char *esc
)
475 size_t size
= seq_get_buf(m
, &buf
);
476 int res
= -ENAMETOOLONG
;
481 p
= __d_path(path
, root
, buf
, size
);
486 char *end
= mangle_path(buf
, p
, esc
);
495 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
499 * returns the path of the 'dentry' from the root of its filesystem.
501 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
504 size_t size
= seq_get_buf(m
, &buf
);
508 char *p
= dentry_path(dentry
, buf
, size
);
510 char *end
= mangle_path(buf
, p
, esc
);
520 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
521 unsigned int nr_bits
)
523 if (m
->count
< m
->size
) {
524 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
525 m
->size
- m
->count
, bits
, nr_bits
);
526 if (m
->count
+ len
< m
->size
) {
534 EXPORT_SYMBOL(seq_bitmap
);
536 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
537 unsigned int nr_bits
)
539 if (m
->count
< m
->size
) {
540 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
541 m
->size
- m
->count
, bits
, nr_bits
);
542 if (m
->count
+ len
< m
->size
) {
550 EXPORT_SYMBOL(seq_bitmap_list
);
552 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
554 return NULL
+ (*pos
== 0);
557 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
563 static void single_stop(struct seq_file
*p
, void *v
)
567 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
570 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
574 op
->start
= single_start
;
575 op
->next
= single_next
;
576 op
->stop
= single_stop
;
578 res
= seq_open(file
, op
);
580 ((struct seq_file
*)file
->private_data
)->private = data
;
586 EXPORT_SYMBOL(single_open
);
588 int single_release(struct inode
*inode
, struct file
*file
)
590 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
591 int res
= seq_release(inode
, file
);
595 EXPORT_SYMBOL(single_release
);
597 int seq_release_private(struct inode
*inode
, struct file
*file
)
599 struct seq_file
*seq
= file
->private_data
;
603 return seq_release(inode
, file
);
605 EXPORT_SYMBOL(seq_release_private
);
607 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
612 struct seq_file
*seq
;
614 private = kzalloc(psize
, GFP_KERNEL
);
618 rc
= seq_open(f
, ops
);
622 seq
= f
->private_data
;
623 seq
->private = private;
631 EXPORT_SYMBOL(__seq_open_private
);
633 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
636 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
638 EXPORT_SYMBOL(seq_open_private
);
640 int seq_putc(struct seq_file
*m
, char c
)
642 if (m
->count
< m
->size
) {
643 m
->buf
[m
->count
++] = c
;
648 EXPORT_SYMBOL(seq_putc
);
650 int seq_puts(struct seq_file
*m
, const char *s
)
653 if (m
->count
+ len
< m
->size
) {
654 memcpy(m
->buf
+ m
->count
, s
, len
);
661 EXPORT_SYMBOL(seq_puts
);
664 * A helper routine for putting decimal numbers without rich format of printf().
665 * only 'unsigned long long' is supported.
666 * This routine will put one byte delimiter + number into seq_file.
667 * This routine is very quick when you show lots of numbers.
668 * In usual cases, it will be better to use seq_printf(). It's easier to read.
670 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
671 unsigned long long num
)
675 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
679 m
->buf
[m
->count
++] = delimiter
;
682 m
->buf
[m
->count
++] = num
+ '0';
686 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
695 EXPORT_SYMBOL(seq_put_decimal_ull
);
697 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
701 if (m
->count
+ 3 >= m
->size
) {
706 m
->buf
[m
->count
++] = delimiter
;
710 return seq_put_decimal_ull(m
, delimiter
, num
);
713 EXPORT_SYMBOL(seq_put_decimal_ll
);
716 * seq_write - write arbitrary data to buffer
717 * @seq: seq_file identifying the buffer to which data should be written
718 * @data: data address
719 * @len: number of bytes
721 * Return 0 on success, non-zero otherwise.
723 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
725 if (seq
->count
+ len
< seq
->size
) {
726 memcpy(seq
->buf
+ seq
->count
, data
, len
);
730 seq_set_overflow(seq
);
733 EXPORT_SYMBOL(seq_write
);
735 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
737 struct list_head
*lh
;
739 list_for_each(lh
, head
)
745 EXPORT_SYMBOL(seq_list_start
);
747 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
752 return seq_list_start(head
, pos
- 1);
754 EXPORT_SYMBOL(seq_list_start_head
);
756 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
758 struct list_head
*lh
;
760 lh
= ((struct list_head
*)v
)->next
;
762 return lh
== head
? NULL
: lh
;
764 EXPORT_SYMBOL(seq_list_next
);
767 * seq_hlist_start - start an iteration of a hlist
768 * @head: the head of the hlist
769 * @pos: the start position of the sequence
771 * Called at seq_file->op->start().
773 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
775 struct hlist_node
*node
;
777 hlist_for_each(node
, head
)
782 EXPORT_SYMBOL(seq_hlist_start
);
785 * seq_hlist_start_head - start an iteration of a hlist
786 * @head: the head of the hlist
787 * @pos: the start position of the sequence
789 * Called at seq_file->op->start(). Call this function if you want to
790 * print a header at the top of the output.
792 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
795 return SEQ_START_TOKEN
;
797 return seq_hlist_start(head
, pos
- 1);
799 EXPORT_SYMBOL(seq_hlist_start_head
);
802 * seq_hlist_next - move to the next position of the hlist
803 * @v: the current iterator
804 * @head: the head of the hlist
805 * @ppos: the current position
807 * Called at seq_file->op->next().
809 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
812 struct hlist_node
*node
= v
;
815 if (v
== SEQ_START_TOKEN
)
820 EXPORT_SYMBOL(seq_hlist_next
);
823 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
824 * @head: the head of the hlist
825 * @pos: the start position of the sequence
827 * Called at seq_file->op->start().
829 * This list-traversal primitive may safely run concurrently with
830 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
831 * as long as the traversal is guarded by rcu_read_lock().
833 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
836 struct hlist_node
*node
;
838 __hlist_for_each_rcu(node
, head
)
843 EXPORT_SYMBOL(seq_hlist_start_rcu
);
846 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
847 * @head: the head of the hlist
848 * @pos: the start position of the sequence
850 * Called at seq_file->op->start(). Call this function if you want to
851 * print a header at the top of the output.
853 * This list-traversal primitive may safely run concurrently with
854 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
855 * as long as the traversal is guarded by rcu_read_lock().
857 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
861 return SEQ_START_TOKEN
;
863 return seq_hlist_start_rcu(head
, pos
- 1);
865 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
868 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
869 * @v: the current iterator
870 * @head: the head of the hlist
871 * @ppos: the current position
873 * Called at seq_file->op->next().
875 * This list-traversal primitive may safely run concurrently with
876 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
877 * as long as the traversal is guarded by rcu_read_lock().
879 struct hlist_node
*seq_hlist_next_rcu(void *v
,
880 struct hlist_head
*head
,
883 struct hlist_node
*node
= v
;
886 if (v
== SEQ_START_TOKEN
)
887 return rcu_dereference(head
->first
);
889 return rcu_dereference(node
->next
);
891 EXPORT_SYMBOL(seq_hlist_next_rcu
);