4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
13 #include <asm/uaccess.h>
17 * seq_open - initialize sequential file
18 * @file: file we initialize
19 * @op: method table describing the sequence
21 * seq_open() sets @file, associating it with a sequence described
22 * by @op. @op->start() sets the iterator up and returns the first
23 * element of sequence. @op->stop() shuts it down. @op->next()
24 * returns the next element of sequence. @op->show() prints element
25 * into the buffer. In case of error ->start() and ->next() return
26 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
27 * returns 0 in case of success and negative number in case of error.
28 * Returning SEQ_SKIP means "discard this element and move on".
30 int seq_open(struct file
*file
, const struct seq_operations
*op
)
32 struct seq_file
*p
= file
->private_data
;
35 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
38 file
->private_data
= p
;
40 memset(p
, 0, sizeof(*p
));
45 * Wrappers around seq_open(e.g. swaps_open) need to be
46 * aware of this. If they set f_version themselves, they
47 * should call seq_open first and then set f_version.
52 * seq_files support lseek() and pread(). They do not implement
53 * write() at all, but we clear FMODE_PWRITE here for historical
56 * If a client of seq_files a) implements file.write() and b) wishes to
57 * support pwrite() then that client will need to implement its own
58 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
60 file
->f_mode
&= ~FMODE_PWRITE
;
63 EXPORT_SYMBOL(seq_open
);
65 static int traverse(struct seq_file
*m
, loff_t offset
)
67 loff_t pos
= 0, index
;
73 m
->count
= m
->from
= 0;
79 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
83 p
= m
->op
->start(m
, &index
);
88 error
= m
->op
->show(m
, p
);
91 if (unlikely(error
)) {
95 if (m
->count
== m
->size
)
97 if (pos
+ m
->count
> offset
) {
98 m
->from
= offset
- pos
;
110 p
= m
->op
->next(m
, p
, &index
);
119 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
120 return !m
->buf
? -ENOMEM
: -EAGAIN
;
124 * seq_read - ->read() method for sequential files.
125 * @file: the file to read from
126 * @buf: the buffer to read to
127 * @size: the maximum number of bytes to read
128 * @ppos: the current position in the file
130 * Ready-made ->f_op->read()
132 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
134 struct seq_file
*m
= file
->private_data
;
141 mutex_lock(&m
->lock
);
144 * seq_file->op->..m_start/m_stop/m_next may do special actions
145 * or optimisations based on the file->f_version, so we want to
146 * pass the file->f_version to those methods.
148 * seq_file->version is just copy of f_version, and seq_file
149 * methods can treat it simply as file version.
150 * It is copied in first and copied out after all operations.
151 * It is convenient to have it as part of structure to avoid the
152 * need of passing another argument to all the seq_file methods.
154 m
->version
= file
->f_version
;
156 /* Don't assume *ppos is where we left it */
157 if (unlikely(*ppos
!= m
->read_pos
)) {
158 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
161 /* With prejudice... */
172 /* grab buffer if we didn't have one */
174 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
178 /* if not empty - flush it first */
180 n
= min(m
->count
, size
);
181 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
194 /* we need at least one record in buffer */
196 p
= m
->op
->start(m
, &pos
);
201 err
= m
->op
->show(m
, p
);
206 if (unlikely(!m
->count
)) {
207 p
= m
->op
->next(m
, p
, &pos
);
211 if (m
->count
< m
->size
)
215 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
221 p
= m
->op
->start(m
, &pos
);
227 /* they want more? let's try to get some more */
228 while (m
->count
< size
) {
229 size_t offs
= m
->count
;
231 p
= m
->op
->next(m
, p
, &next
);
232 if (!p
|| IS_ERR(p
)) {
236 err
= m
->op
->show(m
, p
);
237 if (m
->count
== m
->size
|| err
) {
239 if (likely(err
<= 0))
245 n
= min(m
->count
, size
);
246 err
= copy_to_user(buf
, m
->buf
, n
);
261 m
->read_pos
+= copied
;
263 file
->f_version
= m
->version
;
264 mutex_unlock(&m
->lock
);
273 EXPORT_SYMBOL(seq_read
);
276 * seq_lseek - ->llseek() method for sequential files.
277 * @file: the file in question
278 * @offset: new position
279 * @origin: 0 for absolute, 1 for relative position
281 * Ready-made ->f_op->llseek()
283 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
285 struct seq_file
*m
= file
->private_data
;
286 loff_t retval
= -EINVAL
;
288 mutex_lock(&m
->lock
);
289 m
->version
= file
->f_version
;
292 offset
+= file
->f_pos
;
297 if (offset
!= m
->read_pos
) {
298 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
301 /* with extreme prejudice... */
308 m
->read_pos
= offset
;
309 retval
= file
->f_pos
= offset
;
313 file
->f_version
= m
->version
;
314 mutex_unlock(&m
->lock
);
317 EXPORT_SYMBOL(seq_lseek
);
320 * seq_release - free the structures associated with sequential file.
321 * @file: file in question
322 * @inode: file->f_path.dentry->d_inode
324 * Frees the structures associated with sequential file; can be used
325 * as ->f_op->release() if you don't have private data to destroy.
327 int seq_release(struct inode
*inode
, struct file
*file
)
329 struct seq_file
*m
= file
->private_data
;
334 EXPORT_SYMBOL(seq_release
);
337 * seq_escape - print string into buffer, escaping some characters
340 * @esc: set of characters that need escaping
342 * Puts string into buffer, replacing each occurrence of character from
343 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
346 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
348 char *end
= m
->buf
+ m
->size
;
352 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
353 if (!strchr(esc
, c
)) {
359 *p
++ = '0' + ((c
& 0300) >> 6);
360 *p
++ = '0' + ((c
& 070) >> 3);
361 *p
++ = '0' + (c
& 07);
367 m
->count
= p
- m
->buf
;
370 EXPORT_SYMBOL(seq_escape
);
372 int seq_printf(struct seq_file
*m
, const char *f
, ...)
377 if (m
->count
< m
->size
) {
379 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
381 if (m
->count
+ len
< m
->size
) {
389 EXPORT_SYMBOL(seq_printf
);
392 * mangle_path - mangle and copy path to buffer beginning
394 * @p: beginning of path in above buffer
395 * @esc: set of characters that need escaping
397 * Copy the path from @p to @s, replacing each occurrence of character from
398 * @esc with usual octal escape.
399 * Returns pointer past last written character in @s, or NULL in case of
402 char *mangle_path(char *s
, const char *p
, const char *esc
)
408 } else if (!strchr(esc
, c
)) {
410 } else if (s
+ 4 > p
) {
414 *s
++ = '0' + ((c
& 0300) >> 6);
415 *s
++ = '0' + ((c
& 070) >> 3);
416 *s
++ = '0' + (c
& 07);
421 EXPORT_SYMBOL(mangle_path
);
424 * seq_path - seq_file interface to print a pathname
425 * @m: the seq_file handle
426 * @path: the struct path to print
427 * @esc: set of characters to escape in the output
429 * return the absolute path of 'path', as represented by the
430 * dentry / mnt pair in the path parameter.
432 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
435 size_t size
= seq_get_buf(m
, &buf
);
439 char *p
= d_path(path
, buf
, size
);
441 char *end
= mangle_path(buf
, p
, esc
);
450 EXPORT_SYMBOL(seq_path
);
453 * Same as seq_path, but relative to supplied root.
455 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
456 const struct path
*root
, const char *esc
)
459 size_t size
= seq_get_buf(m
, &buf
);
460 int res
= -ENAMETOOLONG
;
465 p
= __d_path(path
, root
, buf
, size
);
470 char *end
= mangle_path(buf
, p
, esc
);
479 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
483 * returns the path of the 'dentry' from the root of its filesystem.
485 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
488 size_t size
= seq_get_buf(m
, &buf
);
492 char *p
= dentry_path(dentry
, buf
, size
);
494 char *end
= mangle_path(buf
, p
, esc
);
504 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
505 unsigned int nr_bits
)
507 if (m
->count
< m
->size
) {
508 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
509 m
->size
- m
->count
, bits
, nr_bits
);
510 if (m
->count
+ len
< m
->size
) {
518 EXPORT_SYMBOL(seq_bitmap
);
520 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
521 unsigned int nr_bits
)
523 if (m
->count
< m
->size
) {
524 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
525 m
->size
- m
->count
, bits
, nr_bits
);
526 if (m
->count
+ len
< m
->size
) {
534 EXPORT_SYMBOL(seq_bitmap_list
);
536 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
538 return NULL
+ (*pos
== 0);
541 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
547 static void single_stop(struct seq_file
*p
, void *v
)
551 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
554 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
558 op
->start
= single_start
;
559 op
->next
= single_next
;
560 op
->stop
= single_stop
;
562 res
= seq_open(file
, op
);
564 ((struct seq_file
*)file
->private_data
)->private = data
;
570 EXPORT_SYMBOL(single_open
);
572 int single_release(struct inode
*inode
, struct file
*file
)
574 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
575 int res
= seq_release(inode
, file
);
579 EXPORT_SYMBOL(single_release
);
581 int seq_release_private(struct inode
*inode
, struct file
*file
)
583 struct seq_file
*seq
= file
->private_data
;
587 return seq_release(inode
, file
);
589 EXPORT_SYMBOL(seq_release_private
);
591 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
596 struct seq_file
*seq
;
598 private = kzalloc(psize
, GFP_KERNEL
);
602 rc
= seq_open(f
, ops
);
606 seq
= f
->private_data
;
607 seq
->private = private;
615 EXPORT_SYMBOL(__seq_open_private
);
617 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
620 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
622 EXPORT_SYMBOL(seq_open_private
);
624 int seq_putc(struct seq_file
*m
, char c
)
626 if (m
->count
< m
->size
) {
627 m
->buf
[m
->count
++] = c
;
632 EXPORT_SYMBOL(seq_putc
);
634 int seq_puts(struct seq_file
*m
, const char *s
)
637 if (m
->count
+ len
< m
->size
) {
638 memcpy(m
->buf
+ m
->count
, s
, len
);
645 EXPORT_SYMBOL(seq_puts
);
648 * seq_write - write arbitrary data to buffer
649 * @seq: seq_file identifying the buffer to which data should be written
650 * @data: data address
651 * @len: number of bytes
653 * Return 0 on success, non-zero otherwise.
655 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
657 if (seq
->count
+ len
< seq
->size
) {
658 memcpy(seq
->buf
+ seq
->count
, data
, len
);
662 seq
->count
= seq
->size
;
665 EXPORT_SYMBOL(seq_write
);
667 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
669 struct list_head
*lh
;
671 list_for_each(lh
, head
)
677 EXPORT_SYMBOL(seq_list_start
);
679 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
684 return seq_list_start(head
, pos
- 1);
686 EXPORT_SYMBOL(seq_list_start_head
);
688 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
690 struct list_head
*lh
;
692 lh
= ((struct list_head
*)v
)->next
;
694 return lh
== head
? NULL
: lh
;
696 EXPORT_SYMBOL(seq_list_next
);
699 * seq_hlist_start - start an iteration of a hlist
700 * @head: the head of the hlist
701 * @pos: the start position of the sequence
703 * Called at seq_file->op->start().
705 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
707 struct hlist_node
*node
;
709 hlist_for_each(node
, head
)
714 EXPORT_SYMBOL(seq_hlist_start
);
717 * seq_hlist_start_head - start an iteration of a hlist
718 * @head: the head of the hlist
719 * @pos: the start position of the sequence
721 * Called at seq_file->op->start(). Call this function if you want to
722 * print a header at the top of the output.
724 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
727 return SEQ_START_TOKEN
;
729 return seq_hlist_start(head
, pos
- 1);
731 EXPORT_SYMBOL(seq_hlist_start_head
);
734 * seq_hlist_next - move to the next position of the hlist
735 * @v: the current iterator
736 * @head: the head of the hlist
737 * @ppos: the current position
739 * Called at seq_file->op->next().
741 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
744 struct hlist_node
*node
= v
;
747 if (v
== SEQ_START_TOKEN
)
752 EXPORT_SYMBOL(seq_hlist_next
);
755 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
756 * @head: the head of the hlist
757 * @pos: the start position of the sequence
759 * Called at seq_file->op->start().
761 * This list-traversal primitive may safely run concurrently with
762 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
763 * as long as the traversal is guarded by rcu_read_lock().
765 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
768 struct hlist_node
*node
;
770 __hlist_for_each_rcu(node
, head
)
775 EXPORT_SYMBOL(seq_hlist_start_rcu
);
778 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
779 * @head: the head of the hlist
780 * @pos: the start position of the sequence
782 * Called at seq_file->op->start(). Call this function if you want to
783 * print a header at the top of the output.
785 * This list-traversal primitive may safely run concurrently with
786 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
787 * as long as the traversal is guarded by rcu_read_lock().
789 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
793 return SEQ_START_TOKEN
;
795 return seq_hlist_start_rcu(head
, pos
- 1);
797 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
800 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
801 * @v: the current iterator
802 * @head: the head of the hlist
803 * @ppos: the current position
805 * Called at seq_file->op->next().
807 * This list-traversal primitive may safely run concurrently with
808 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
809 * as long as the traversal is guarded by rcu_read_lock().
811 struct hlist_node
*seq_hlist_next_rcu(void *v
,
812 struct hlist_head
*head
,
815 struct hlist_node
*node
= v
;
818 if (v
== SEQ_START_TOKEN
)
819 return rcu_dereference(head
->first
);
821 return rcu_dereference(node
->next
);
823 EXPORT_SYMBOL(seq_hlist_next_rcu
);