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
);
143 /* Don't assume *ppos is where we left it */
144 if (unlikely(*ppos
!= m
->read_pos
)) {
146 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
149 /* With prejudice... */
159 * seq_file->op->..m_start/m_stop/m_next may do special actions
160 * or optimisations based on the file->f_version, so we want to
161 * pass the file->f_version to those methods.
163 * seq_file->version is just copy of f_version, and seq_file
164 * methods can treat it simply as file version.
165 * It is copied in first and copied out after all operations.
166 * It is convenient to have it as part of structure to avoid the
167 * need of passing another argument to all the seq_file methods.
169 m
->version
= file
->f_version
;
170 /* grab buffer if we didn't have one */
172 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
176 /* if not empty - flush it first */
178 n
= min(m
->count
, size
);
179 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
192 /* we need at least one record in buffer */
194 p
= m
->op
->start(m
, &pos
);
199 err
= m
->op
->show(m
, p
);
204 if (unlikely(!m
->count
)) {
205 p
= m
->op
->next(m
, p
, &pos
);
209 if (m
->count
< m
->size
)
213 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
219 p
= m
->op
->start(m
, &pos
);
225 /* they want more? let's try to get some more */
226 while (m
->count
< size
) {
227 size_t offs
= m
->count
;
229 p
= m
->op
->next(m
, p
, &next
);
230 if (!p
|| IS_ERR(p
)) {
234 err
= m
->op
->show(m
, p
);
235 if (m
->count
== m
->size
|| err
) {
237 if (likely(err
<= 0))
243 n
= min(m
->count
, size
);
244 err
= copy_to_user(buf
, m
->buf
, n
);
259 m
->read_pos
+= copied
;
261 file
->f_version
= m
->version
;
262 mutex_unlock(&m
->lock
);
271 EXPORT_SYMBOL(seq_read
);
274 * seq_lseek - ->llseek() method for sequential files.
275 * @file: the file in question
276 * @offset: new position
277 * @origin: 0 for absolute, 1 for relative position
279 * Ready-made ->f_op->llseek()
281 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
283 struct seq_file
*m
= file
->private_data
;
284 loff_t retval
= -EINVAL
;
286 mutex_lock(&m
->lock
);
287 m
->version
= file
->f_version
;
290 offset
+= file
->f_pos
;
295 if (offset
!= m
->read_pos
) {
296 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
299 /* with extreme prejudice... */
306 m
->read_pos
= offset
;
307 retval
= file
->f_pos
= offset
;
311 file
->f_version
= m
->version
;
312 mutex_unlock(&m
->lock
);
315 EXPORT_SYMBOL(seq_lseek
);
318 * seq_release - free the structures associated with sequential file.
319 * @file: file in question
320 * @inode: file->f_path.dentry->d_inode
322 * Frees the structures associated with sequential file; can be used
323 * as ->f_op->release() if you don't have private data to destroy.
325 int seq_release(struct inode
*inode
, struct file
*file
)
327 struct seq_file
*m
= file
->private_data
;
332 EXPORT_SYMBOL(seq_release
);
335 * seq_escape - print string into buffer, escaping some characters
338 * @esc: set of characters that need escaping
340 * Puts string into buffer, replacing each occurrence of character from
341 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
344 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
346 char *end
= m
->buf
+ m
->size
;
350 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
351 if (!strchr(esc
, c
)) {
357 *p
++ = '0' + ((c
& 0300) >> 6);
358 *p
++ = '0' + ((c
& 070) >> 3);
359 *p
++ = '0' + (c
& 07);
365 m
->count
= p
- m
->buf
;
368 EXPORT_SYMBOL(seq_escape
);
370 int seq_printf(struct seq_file
*m
, const char *f
, ...)
375 if (m
->count
< m
->size
) {
377 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
379 if (m
->count
+ len
< m
->size
) {
387 EXPORT_SYMBOL(seq_printf
);
390 * mangle_path - mangle and copy path to buffer beginning
392 * @p: beginning of path in above buffer
393 * @esc: set of characters that need escaping
395 * Copy the path from @p to @s, replacing each occurrence of character from
396 * @esc with usual octal escape.
397 * Returns pointer past last written character in @s, or NULL in case of
400 char *mangle_path(char *s
, char *p
, char *esc
)
406 } else if (!strchr(esc
, c
)) {
408 } else if (s
+ 4 > p
) {
412 *s
++ = '0' + ((c
& 0300) >> 6);
413 *s
++ = '0' + ((c
& 070) >> 3);
414 *s
++ = '0' + (c
& 07);
419 EXPORT_SYMBOL(mangle_path
);
422 * seq_path - seq_file interface to print a pathname
423 * @m: the seq_file handle
424 * @path: the struct path to print
425 * @esc: set of characters to escape in the output
427 * return the absolute path of 'path', as represented by the
428 * dentry / mnt pair in the path parameter.
430 int seq_path(struct seq_file
*m
, struct path
*path
, char *esc
)
433 size_t size
= seq_get_buf(m
, &buf
);
437 char *p
= d_path(path
, buf
, size
);
439 char *end
= mangle_path(buf
, p
, esc
);
448 EXPORT_SYMBOL(seq_path
);
451 * Same as seq_path, but relative to supplied root.
453 * root may be changed, see __d_path().
455 int seq_path_root(struct seq_file
*m
, struct path
*path
, struct path
*root
,
459 size_t size
= seq_get_buf(m
, &buf
);
460 int res
= -ENAMETOOLONG
;
465 p
= __d_path(path
, root
, buf
, size
);
468 char *end
= mangle_path(buf
, p
, esc
);
477 return res
< 0 ? res
: 0;
481 * returns the path of the 'dentry' from the root of its filesystem.
483 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, char *esc
)
486 size_t size
= seq_get_buf(m
, &buf
);
490 char *p
= dentry_path(dentry
, buf
, size
);
492 char *end
= mangle_path(buf
, p
, esc
);
502 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
503 unsigned int nr_bits
)
505 if (m
->count
< m
->size
) {
506 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
507 m
->size
- m
->count
, bits
, nr_bits
);
508 if (m
->count
+ len
< m
->size
) {
516 EXPORT_SYMBOL(seq_bitmap
);
518 int seq_bitmap_list(struct seq_file
*m
, const unsigned long *bits
,
519 unsigned int nr_bits
)
521 if (m
->count
< m
->size
) {
522 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
523 m
->size
- m
->count
, bits
, nr_bits
);
524 if (m
->count
+ len
< m
->size
) {
532 EXPORT_SYMBOL(seq_bitmap_list
);
534 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
536 return NULL
+ (*pos
== 0);
539 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
545 static void single_stop(struct seq_file
*p
, void *v
)
549 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
552 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
556 op
->start
= single_start
;
557 op
->next
= single_next
;
558 op
->stop
= single_stop
;
560 res
= seq_open(file
, op
);
562 ((struct seq_file
*)file
->private_data
)->private = data
;
568 EXPORT_SYMBOL(single_open
);
570 int single_release(struct inode
*inode
, struct file
*file
)
572 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
573 int res
= seq_release(inode
, file
);
577 EXPORT_SYMBOL(single_release
);
579 int seq_release_private(struct inode
*inode
, struct file
*file
)
581 struct seq_file
*seq
= file
->private_data
;
585 return seq_release(inode
, file
);
587 EXPORT_SYMBOL(seq_release_private
);
589 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
594 struct seq_file
*seq
;
596 private = kzalloc(psize
, GFP_KERNEL
);
600 rc
= seq_open(f
, ops
);
604 seq
= f
->private_data
;
605 seq
->private = private;
613 EXPORT_SYMBOL(__seq_open_private
);
615 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
618 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
620 EXPORT_SYMBOL(seq_open_private
);
622 int seq_putc(struct seq_file
*m
, char c
)
624 if (m
->count
< m
->size
) {
625 m
->buf
[m
->count
++] = c
;
630 EXPORT_SYMBOL(seq_putc
);
632 int seq_puts(struct seq_file
*m
, const char *s
)
635 if (m
->count
+ len
< m
->size
) {
636 memcpy(m
->buf
+ m
->count
, s
, len
);
643 EXPORT_SYMBOL(seq_puts
);
646 * seq_write - write arbitrary data to buffer
647 * @seq: seq_file identifying the buffer to which data should be written
648 * @data: data address
649 * @len: number of bytes
651 * Return 0 on success, non-zero otherwise.
653 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
655 if (seq
->count
+ len
< seq
->size
) {
656 memcpy(seq
->buf
+ seq
->count
, data
, len
);
660 seq
->count
= seq
->size
;
663 EXPORT_SYMBOL(seq_write
);
665 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
667 struct list_head
*lh
;
669 list_for_each(lh
, head
)
675 EXPORT_SYMBOL(seq_list_start
);
677 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
682 return seq_list_start(head
, pos
- 1);
684 EXPORT_SYMBOL(seq_list_start_head
);
686 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
688 struct list_head
*lh
;
690 lh
= ((struct list_head
*)v
)->next
;
692 return lh
== head
? NULL
: lh
;
694 EXPORT_SYMBOL(seq_list_next
);
697 * seq_hlist_start - start an iteration of a hlist
698 * @head: the head of the hlist
699 * @pos: the start position of the sequence
701 * Called at seq_file->op->start().
703 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
705 struct hlist_node
*node
;
707 hlist_for_each(node
, head
)
712 EXPORT_SYMBOL(seq_hlist_start
);
715 * seq_hlist_start_head - start an iteration of a hlist
716 * @head: the head of the hlist
717 * @pos: the start position of the sequence
719 * Called at seq_file->op->start(). Call this function if you want to
720 * print a header at the top of the output.
722 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
725 return SEQ_START_TOKEN
;
727 return seq_hlist_start(head
, pos
- 1);
729 EXPORT_SYMBOL(seq_hlist_start_head
);
732 * seq_hlist_next - move to the next position of the hlist
733 * @v: the current iterator
734 * @head: the head of the hlist
735 * @ppos: the current position
737 * Called at seq_file->op->next().
739 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
742 struct hlist_node
*node
= v
;
745 if (v
== SEQ_START_TOKEN
)
750 EXPORT_SYMBOL(seq_hlist_next
);
753 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
754 * @head: the head of the hlist
755 * @pos: the start position of the sequence
757 * Called at seq_file->op->start().
759 * This list-traversal primitive may safely run concurrently with
760 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
761 * as long as the traversal is guarded by rcu_read_lock().
763 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
766 struct hlist_node
*node
;
768 __hlist_for_each_rcu(node
, head
)
773 EXPORT_SYMBOL(seq_hlist_start_rcu
);
776 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
777 * @head: the head of the hlist
778 * @pos: the start position of the sequence
780 * Called at seq_file->op->start(). Call this function if you want to
781 * print a header at the top of the output.
783 * This list-traversal primitive may safely run concurrently with
784 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
785 * as long as the traversal is guarded by rcu_read_lock().
787 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
791 return SEQ_START_TOKEN
;
793 return seq_hlist_start_rcu(head
, pos
- 1);
795 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
798 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
799 * @v: the current iterator
800 * @head: the head of the hlist
801 * @ppos: the current position
803 * Called at seq_file->op->next().
805 * This list-traversal primitive may safely run concurrently with
806 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
807 * as long as the traversal is guarded by rcu_read_lock().
809 struct hlist_node
*seq_hlist_next_rcu(void *v
,
810 struct hlist_head
*head
,
813 struct hlist_node
*node
= v
;
816 if (v
== SEQ_START_TOKEN
)
817 return rcu_dereference(head
->first
);
819 return rcu_dereference(node
->next
);
821 EXPORT_SYMBOL(seq_hlist_next_rcu
);
This page took 0.048228 seconds and 5 git commands to generate.