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>
19 static void seq_set_overflow(struct seq_file
*m
)
24 static void *seq_buf_alloc(unsigned long size
)
29 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
30 * it's better to fall back to vmalloc() than to kill things.
32 buf
= kmalloc(size
, GFP_KERNEL
| __GFP_NORETRY
| __GFP_NOWARN
);
33 if (!buf
&& size
> PAGE_SIZE
)
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
50 * Returning SEQ_SKIP means "discard this element and move on".
51 * Note: seq_open() will allocate a struct seq_file and store its
52 * pointer in @file->private_data. This pointer should not be modified.
54 int seq_open(struct file
*file
, const struct seq_operations
*op
)
58 WARN_ON(file
->private_data
);
60 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
64 file
->private_data
= p
;
69 p
->user_ns
= file
->f_cred
->user_ns
;
73 * Wrappers around seq_open(e.g. swaps_open) need to be
74 * aware of this. If they set f_version themselves, they
75 * should call seq_open first and then set f_version.
80 * seq_files support lseek() and pread(). They do not implement
81 * write() at all, but we clear FMODE_PWRITE here for historical
84 * If a client of seq_files a) implements file.write() and b) wishes to
85 * support pwrite() then that client will need to implement its own
86 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
88 file
->f_mode
&= ~FMODE_PWRITE
;
91 EXPORT_SYMBOL(seq_open
);
93 static int traverse(struct seq_file
*m
, loff_t offset
)
95 loff_t pos
= 0, index
;
101 m
->count
= m
->from
= 0;
107 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
111 p
= m
->op
->start(m
, &index
);
116 error
= m
->op
->show(m
, p
);
119 if (unlikely(error
)) {
123 if (seq_has_overflowed(m
))
125 if (pos
+ m
->count
> offset
) {
126 m
->from
= offset
- pos
;
138 p
= m
->op
->next(m
, p
, &index
);
148 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
149 return !m
->buf
? -ENOMEM
: -EAGAIN
;
153 * seq_read - ->read() method for sequential files.
154 * @file: the file to read from
155 * @buf: the buffer to read to
156 * @size: the maximum number of bytes to read
157 * @ppos: the current position in the file
159 * Ready-made ->f_op->read()
161 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
163 struct seq_file
*m
= file
->private_data
;
170 mutex_lock(&m
->lock
);
173 * seq_file->op->..m_start/m_stop/m_next may do special actions
174 * or optimisations based on the file->f_version, so we want to
175 * pass the file->f_version to those methods.
177 * seq_file->version is just copy of f_version, and seq_file
178 * methods can treat it simply as file version.
179 * It is copied in first and copied out after all operations.
180 * It is convenient to have it as part of structure to avoid the
181 * need of passing another argument to all the seq_file methods.
183 m
->version
= file
->f_version
;
185 /* Don't assume *ppos is where we left it */
186 if (unlikely(*ppos
!= m
->read_pos
)) {
187 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
190 /* With prejudice... */
201 /* grab buffer if we didn't have one */
203 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
207 /* if not empty - flush it first */
209 n
= min(m
->count
, size
);
210 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
223 /* we need at least one record in buffer */
225 p
= m
->op
->start(m
, &pos
);
230 err
= m
->op
->show(m
, p
);
235 if (unlikely(!m
->count
)) {
236 p
= m
->op
->next(m
, p
, &pos
);
240 if (m
->count
< m
->size
)
245 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
250 p
= m
->op
->start(m
, &pos
);
256 /* they want more? let's try to get some more */
257 while (m
->count
< size
) {
258 size_t offs
= m
->count
;
260 p
= m
->op
->next(m
, p
, &next
);
261 if (!p
|| IS_ERR(p
)) {
265 err
= m
->op
->show(m
, p
);
266 if (seq_has_overflowed(m
) || err
) {
268 if (likely(err
<= 0))
274 n
= min(m
->count
, size
);
275 err
= copy_to_user(buf
, m
->buf
, n
);
290 m
->read_pos
+= copied
;
292 file
->f_version
= m
->version
;
293 mutex_unlock(&m
->lock
);
302 EXPORT_SYMBOL(seq_read
);
305 * seq_lseek - ->llseek() method for sequential files.
306 * @file: the file in question
307 * @offset: new position
308 * @whence: 0 for absolute, 1 for relative position
310 * Ready-made ->f_op->llseek()
312 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int whence
)
314 struct seq_file
*m
= file
->private_data
;
315 loff_t retval
= -EINVAL
;
317 mutex_lock(&m
->lock
);
318 m
->version
= file
->f_version
;
321 offset
+= file
->f_pos
;
326 if (offset
!= m
->read_pos
) {
327 while ((retval
= traverse(m
, offset
)) == -EAGAIN
)
330 /* with extreme prejudice... */
337 m
->read_pos
= offset
;
338 retval
= file
->f_pos
= offset
;
341 file
->f_pos
= offset
;
344 file
->f_version
= m
->version
;
345 mutex_unlock(&m
->lock
);
348 EXPORT_SYMBOL(seq_lseek
);
351 * seq_release - free the structures associated with sequential file.
352 * @file: file in question
355 * Frees the structures associated with sequential file; can be used
356 * as ->f_op->release() if you don't have private data to destroy.
358 int seq_release(struct inode
*inode
, struct file
*file
)
360 struct seq_file
*m
= file
->private_data
;
365 EXPORT_SYMBOL(seq_release
);
368 * seq_escape - print string into buffer, escaping some characters
371 * @esc: set of characters that need escaping
373 * Puts string into buffer, replacing each occurrence of character from
374 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
377 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
379 char *end
= m
->buf
+ m
->size
;
383 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
384 if (!strchr(esc
, c
)) {
390 *p
++ = '0' + ((c
& 0300) >> 6);
391 *p
++ = '0' + ((c
& 070) >> 3);
392 *p
++ = '0' + (c
& 07);
398 m
->count
= p
- m
->buf
;
401 EXPORT_SYMBOL(seq_escape
);
403 int seq_vprintf(struct seq_file
*m
, const char *f
, va_list args
)
407 if (m
->count
< m
->size
) {
408 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
409 if (m
->count
+ len
< m
->size
) {
417 EXPORT_SYMBOL(seq_vprintf
);
419 int seq_printf(struct seq_file
*m
, const char *f
, ...)
425 ret
= seq_vprintf(m
, f
, args
);
430 EXPORT_SYMBOL(seq_printf
);
433 * mangle_path - mangle and copy path to buffer beginning
435 * @p: beginning of path in above buffer
436 * @esc: set of characters that need escaping
438 * Copy the path from @p to @s, replacing each occurrence of character from
439 * @esc with usual octal escape.
440 * Returns pointer past last written character in @s, or NULL in case of
443 char *mangle_path(char *s
, const char *p
, const char *esc
)
449 } else if (!strchr(esc
, c
)) {
451 } else if (s
+ 4 > p
) {
455 *s
++ = '0' + ((c
& 0300) >> 6);
456 *s
++ = '0' + ((c
& 070) >> 3);
457 *s
++ = '0' + (c
& 07);
462 EXPORT_SYMBOL(mangle_path
);
465 * seq_path - seq_file interface to print a pathname
466 * @m: the seq_file handle
467 * @path: the struct path to print
468 * @esc: set of characters to escape in the output
470 * return the absolute path of 'path', as represented by the
471 * dentry / mnt pair in the path parameter.
473 int seq_path(struct seq_file
*m
, const struct path
*path
, const char *esc
)
476 size_t size
= seq_get_buf(m
, &buf
);
480 char *p
= d_path(path
, buf
, size
);
482 char *end
= mangle_path(buf
, p
, esc
);
491 EXPORT_SYMBOL(seq_path
);
494 * Same as seq_path, but relative to supplied root.
496 int seq_path_root(struct seq_file
*m
, const struct path
*path
,
497 const struct path
*root
, const char *esc
)
500 size_t size
= seq_get_buf(m
, &buf
);
501 int res
= -ENAMETOOLONG
;
506 p
= __d_path(path
, root
, buf
, size
);
511 char *end
= mangle_path(buf
, p
, esc
);
520 return res
< 0 && res
!= -ENAMETOOLONG
? res
: 0;
524 * returns the path of the 'dentry' from the root of its filesystem.
526 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, const char *esc
)
529 size_t size
= seq_get_buf(m
, &buf
);
533 char *p
= dentry_path(dentry
, buf
, size
);
535 char *end
= mangle_path(buf
, p
, esc
);
544 EXPORT_SYMBOL(seq_dentry
);
546 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
548 return NULL
+ (*pos
== 0);
551 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
557 static void single_stop(struct seq_file
*p
, void *v
)
561 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
564 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
568 op
->start
= single_start
;
569 op
->next
= single_next
;
570 op
->stop
= single_stop
;
572 res
= seq_open(file
, op
);
574 ((struct seq_file
*)file
->private_data
)->private = data
;
580 EXPORT_SYMBOL(single_open
);
582 int single_open_size(struct file
*file
, int (*show
)(struct seq_file
*, void *),
583 void *data
, size_t size
)
585 char *buf
= seq_buf_alloc(size
);
589 ret
= single_open(file
, show
, data
);
594 ((struct seq_file
*)file
->private_data
)->buf
= buf
;
595 ((struct seq_file
*)file
->private_data
)->size
= size
;
598 EXPORT_SYMBOL(single_open_size
);
600 int single_release(struct inode
*inode
, struct file
*file
)
602 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
603 int res
= seq_release(inode
, file
);
607 EXPORT_SYMBOL(single_release
);
609 int seq_release_private(struct inode
*inode
, struct file
*file
)
611 struct seq_file
*seq
= file
->private_data
;
615 return seq_release(inode
, file
);
617 EXPORT_SYMBOL(seq_release_private
);
619 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
624 struct seq_file
*seq
;
626 private = kzalloc(psize
, GFP_KERNEL
);
630 rc
= seq_open(f
, ops
);
634 seq
= f
->private_data
;
635 seq
->private = private;
643 EXPORT_SYMBOL(__seq_open_private
);
645 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
648 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
650 EXPORT_SYMBOL(seq_open_private
);
652 int seq_putc(struct seq_file
*m
, char c
)
654 if (m
->count
< m
->size
) {
655 m
->buf
[m
->count
++] = c
;
660 EXPORT_SYMBOL(seq_putc
);
662 int seq_puts(struct seq_file
*m
, const char *s
)
665 if (m
->count
+ len
< m
->size
) {
666 memcpy(m
->buf
+ m
->count
, s
, len
);
673 EXPORT_SYMBOL(seq_puts
);
676 * A helper routine for putting decimal numbers without rich format of printf().
677 * only 'unsigned long long' is supported.
678 * This routine will put one byte delimiter + number into seq_file.
679 * This routine is very quick when you show lots of numbers.
680 * In usual cases, it will be better to use seq_printf(). It's easier to read.
682 int seq_put_decimal_ull(struct seq_file
*m
, char delimiter
,
683 unsigned long long num
)
687 if (m
->count
+ 2 >= m
->size
) /* we'll write 2 bytes at least */
691 m
->buf
[m
->count
++] = delimiter
;
694 m
->buf
[m
->count
++] = num
+ '0';
698 len
= num_to_str(m
->buf
+ m
->count
, m
->size
- m
->count
, num
);
707 EXPORT_SYMBOL(seq_put_decimal_ull
);
709 int seq_put_decimal_ll(struct seq_file
*m
, char delimiter
,
713 if (m
->count
+ 3 >= m
->size
) {
718 m
->buf
[m
->count
++] = delimiter
;
722 return seq_put_decimal_ull(m
, delimiter
, num
);
725 EXPORT_SYMBOL(seq_put_decimal_ll
);
728 * seq_write - write arbitrary data to buffer
729 * @seq: seq_file identifying the buffer to which data should be written
730 * @data: data address
731 * @len: number of bytes
733 * Return 0 on success, non-zero otherwise.
735 int seq_write(struct seq_file
*seq
, const void *data
, size_t len
)
737 if (seq
->count
+ len
< seq
->size
) {
738 memcpy(seq
->buf
+ seq
->count
, data
, len
);
742 seq_set_overflow(seq
);
745 EXPORT_SYMBOL(seq_write
);
748 * seq_pad - write padding spaces to buffer
749 * @m: seq_file identifying the buffer to which data should be written
750 * @c: the byte to append after padding if non-zero
752 void seq_pad(struct seq_file
*m
, char c
)
754 int size
= m
->pad_until
- m
->count
;
756 seq_printf(m
, "%*s", size
, "");
760 EXPORT_SYMBOL(seq_pad
);
762 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
764 struct list_head
*lh
;
766 list_for_each(lh
, head
)
772 EXPORT_SYMBOL(seq_list_start
);
774 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
779 return seq_list_start(head
, pos
- 1);
781 EXPORT_SYMBOL(seq_list_start_head
);
783 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
785 struct list_head
*lh
;
787 lh
= ((struct list_head
*)v
)->next
;
789 return lh
== head
? NULL
: lh
;
791 EXPORT_SYMBOL(seq_list_next
);
794 * seq_hlist_start - start an iteration of a hlist
795 * @head: the head of the hlist
796 * @pos: the start position of the sequence
798 * Called at seq_file->op->start().
800 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
802 struct hlist_node
*node
;
804 hlist_for_each(node
, head
)
809 EXPORT_SYMBOL(seq_hlist_start
);
812 * seq_hlist_start_head - start an iteration of a hlist
813 * @head: the head of the hlist
814 * @pos: the start position of the sequence
816 * Called at seq_file->op->start(). Call this function if you want to
817 * print a header at the top of the output.
819 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
822 return SEQ_START_TOKEN
;
824 return seq_hlist_start(head
, pos
- 1);
826 EXPORT_SYMBOL(seq_hlist_start_head
);
829 * seq_hlist_next - move to the next position of the hlist
830 * @v: the current iterator
831 * @head: the head of the hlist
832 * @ppos: the current position
834 * Called at seq_file->op->next().
836 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
839 struct hlist_node
*node
= v
;
842 if (v
== SEQ_START_TOKEN
)
847 EXPORT_SYMBOL(seq_hlist_next
);
850 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
851 * @head: the head of the hlist
852 * @pos: the start position of the sequence
854 * Called at seq_file->op->start().
856 * This list-traversal primitive may safely run concurrently with
857 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
858 * as long as the traversal is guarded by rcu_read_lock().
860 struct hlist_node
*seq_hlist_start_rcu(struct hlist_head
*head
,
863 struct hlist_node
*node
;
865 __hlist_for_each_rcu(node
, head
)
870 EXPORT_SYMBOL(seq_hlist_start_rcu
);
873 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
874 * @head: the head of the hlist
875 * @pos: the start position of the sequence
877 * Called at seq_file->op->start(). Call this function if you want to
878 * print a header at the top of the output.
880 * This list-traversal primitive may safely run concurrently with
881 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
882 * as long as the traversal is guarded by rcu_read_lock().
884 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
888 return SEQ_START_TOKEN
;
890 return seq_hlist_start_rcu(head
, pos
- 1);
892 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
895 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
896 * @v: the current iterator
897 * @head: the head of the hlist
898 * @ppos: the current position
900 * Called at seq_file->op->next().
902 * This list-traversal primitive may safely run concurrently with
903 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
904 * as long as the traversal is guarded by rcu_read_lock().
906 struct hlist_node
*seq_hlist_next_rcu(void *v
,
907 struct hlist_head
*head
,
910 struct hlist_node
*node
= v
;
913 if (v
== SEQ_START_TOKEN
)
914 return rcu_dereference(head
->first
);
916 return rcu_dereference(node
->next
);
918 EXPORT_SYMBOL(seq_hlist_next_rcu
);
921 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
922 * @head: pointer to percpu array of struct hlist_heads
923 * @cpu: pointer to cpu "cursor"
924 * @pos: start position of sequence
926 * Called at seq_file->op->start().
929 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
931 struct hlist_node
*node
;
933 for_each_possible_cpu(*cpu
) {
934 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
941 EXPORT_SYMBOL(seq_hlist_start_percpu
);
944 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
945 * @v: pointer to current hlist_node
946 * @head: pointer to percpu array of struct hlist_heads
947 * @cpu: pointer to cpu "cursor"
948 * @pos: start position of sequence
950 * Called at seq_file->op->next().
953 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
954 int *cpu
, loff_t
*pos
)
956 struct hlist_node
*node
= v
;
963 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
964 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
965 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
967 if (!hlist_empty(bucket
))
968 return bucket
->first
;
972 EXPORT_SYMBOL(seq_hlist_next_percpu
);
This page took 0.05197 seconds and 6 git commands to generate.