]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/seq_file.c
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
)
28 buf
= kmalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
29 if (!buf
&& size
> PAGE_SIZE
)
35 * seq_open - initialize sequential file
36 * @file: file we initialize
37 * @op: method table describing the sequence
39 * seq_open() sets @file, associating it with a sequence described
40 * by @op. @op->start() sets the iterator up and returns the first
41 * element of sequence. @op->stop() shuts it down. @op->next()
42 * returns the next element of sequence. @op->show() prints element
43 * into the buffer. In case of error ->start() and ->next() return
44 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
45 * returns 0 in case of success and negative number in case of error.
46 * Returning SEQ_SKIP means "discard this element and move on".
48 int seq_open(struct file
*file
, const struct seq_operations
*op
)
50 struct seq_file
*p
= file
->private_data
;
53 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
56 file
->private_data
= p
;
58 memset(p
, 0, sizeof(*p
));
62 p
->user_ns
= file
->f_cred
->user_ns
;
66 * Wrappers around seq_open(e.g. swaps_open) need to be
67 * aware of this. If they set f_version themselves, they
68 * should call seq_open first and then set f_version.
73 * seq_files support lseek() and pread(). They do not implement
74 * write() at all, but we clear FMODE_PWRITE here for historical
77 * If a client of seq_files a) implements file.write() and b) wishes to
78 * support pwrite() then that client will need to implement its own
79 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
81 file
->f_mode
&= ~FMODE_PWRITE
;
84 EXPORT_SYMBOL(seq_open
);
86 static int traverse(struct seq_file
*m
, loff_t offset
)
88 loff_t pos
= 0, index
;
94 m
->count
= m
->from
= 0;
100 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
104 p
= m
->op
->start(m
, &index
);
109 error
= m
->op
->show(m
, p
);
112 if (unlikely(error
)) {
116 if (seq_has_overflowed(m
))
118 if (pos
+ m
->count
> offset
) {
119 m
->from
= offset
- pos
;
131 p
= m
->op
->next(m
, p
, &index
);
141 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
142 return !m
->buf
? -ENOMEM
: -EAGAIN
;
146 * seq_read - ->read() method for sequential files.
147 * @file: the file to read from
148 * @buf: the buffer to read to
149 * @size: the maximum number of bytes to read
150 * @ppos: the current position in the file
152 * Ready-made ->f_op->read()
154 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
156 struct seq_file
*m
= file
->private_data
;
163 mutex_lock(&m
->lock
);
166 * seq_file->op->..m_start/m_stop/m_next may do special actions
167 * or optimisations based on the file->f_version, so we want to
168 * pass the file->f_version to those methods.
170 * seq_file->version is just copy of f_version, and seq_file
171 * methods can treat it simply as file version.
172 * It is copied in first and copied out after all operations.
173 * It is convenient to have it as part of structure to avoid the
174 * need of passing another argument to all the seq_file methods.
176 m
->version
= file
->f_version
;
178 /* Don't assume *ppos is where we left it */
179 if (unlikely(*ppos
!= m
->read_pos
)) {
180 while ((err
= traverse(m
, *ppos
)) == -EAGAIN
)
183 /* With prejudice... */
194 /* grab buffer if we didn't have one */
196 m
->buf
= seq_buf_alloc(m
->size
= PAGE_SIZE
);
200 /* if not empty - flush it first */
202 n
= min(m
->count
, size
);
203 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
)
238 m
->buf
= seq_buf_alloc(m
->size
<<= 1);
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_has_overflowed(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
= seq_buf_alloc(size
);
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
);
772 * seq_pad - write padding spaces to buffer
773 * @m: seq_file identifying the buffer to which data should be written
774 * @c: the byte to append after padding if non-zero
776 void seq_pad(struct seq_file
*m
, char c
)
778 int size
= m
->pad_until
- m
->count
;
780 seq_printf(m
, "%*s", size
, "");
784 EXPORT_SYMBOL(seq_pad
);
786 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
788 struct list_head
*lh
;
790 list_for_each(lh
, head
)
796 EXPORT_SYMBOL(seq_list_start
);
798 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
803 return seq_list_start(head
, pos
- 1);
805 EXPORT_SYMBOL(seq_list_start_head
);
807 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
809 struct list_head
*lh
;
811 lh
= ((struct list_head
*)v
)->next
;
813 return lh
== head
? NULL
: lh
;
815 EXPORT_SYMBOL(seq_list_next
);
818 * seq_hlist_start - start an iteration of a hlist
819 * @head: the head of the hlist
820 * @pos: the start position of the sequence
822 * Called at seq_file->op->start().
824 struct hlist_node
*seq_hlist_start(struct hlist_head
*head
, loff_t pos
)
826 struct hlist_node
*node
;
828 hlist_for_each(node
, head
)
833 EXPORT_SYMBOL(seq_hlist_start
);
836 * seq_hlist_start_head - start an iteration of a hlist
837 * @head: the head of the hlist
838 * @pos: the start position of the sequence
840 * Called at seq_file->op->start(). Call this function if you want to
841 * print a header at the top of the output.
843 struct hlist_node
*seq_hlist_start_head(struct hlist_head
*head
, loff_t pos
)
846 return SEQ_START_TOKEN
;
848 return seq_hlist_start(head
, pos
- 1);
850 EXPORT_SYMBOL(seq_hlist_start_head
);
853 * seq_hlist_next - move to the next position of the hlist
854 * @v: the current iterator
855 * @head: the head of the hlist
856 * @ppos: the current position
858 * Called at seq_file->op->next().
860 struct hlist_node
*seq_hlist_next(void *v
, struct hlist_head
*head
,
863 struct hlist_node
*node
= v
;
866 if (v
== SEQ_START_TOKEN
)
871 EXPORT_SYMBOL(seq_hlist_next
);
874 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
875 * @head: the head of the hlist
876 * @pos: the start position of the sequence
878 * Called at seq_file->op->start().
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_rcu(struct hlist_head
*head
,
887 struct hlist_node
*node
;
889 __hlist_for_each_rcu(node
, head
)
894 EXPORT_SYMBOL(seq_hlist_start_rcu
);
897 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
898 * @head: the head of the hlist
899 * @pos: the start position of the sequence
901 * Called at seq_file->op->start(). Call this function if you want to
902 * print a header at the top of the output.
904 * This list-traversal primitive may safely run concurrently with
905 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
906 * as long as the traversal is guarded by rcu_read_lock().
908 struct hlist_node
*seq_hlist_start_head_rcu(struct hlist_head
*head
,
912 return SEQ_START_TOKEN
;
914 return seq_hlist_start_rcu(head
, pos
- 1);
916 EXPORT_SYMBOL(seq_hlist_start_head_rcu
);
919 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
920 * @v: the current iterator
921 * @head: the head of the hlist
922 * @ppos: the current position
924 * Called at seq_file->op->next().
926 * This list-traversal primitive may safely run concurrently with
927 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
928 * as long as the traversal is guarded by rcu_read_lock().
930 struct hlist_node
*seq_hlist_next_rcu(void *v
,
931 struct hlist_head
*head
,
934 struct hlist_node
*node
= v
;
937 if (v
== SEQ_START_TOKEN
)
938 return rcu_dereference(head
->first
);
940 return rcu_dereference(node
->next
);
942 EXPORT_SYMBOL(seq_hlist_next_rcu
);
945 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
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->start().
953 seq_hlist_start_percpu(struct hlist_head __percpu
*head
, int *cpu
, loff_t pos
)
955 struct hlist_node
*node
;
957 for_each_possible_cpu(*cpu
) {
958 hlist_for_each(node
, per_cpu_ptr(head
, *cpu
)) {
965 EXPORT_SYMBOL(seq_hlist_start_percpu
);
968 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
969 * @v: pointer to current hlist_node
970 * @head: pointer to percpu array of struct hlist_heads
971 * @cpu: pointer to cpu "cursor"
972 * @pos: start position of sequence
974 * Called at seq_file->op->next().
977 seq_hlist_next_percpu(void *v
, struct hlist_head __percpu
*head
,
978 int *cpu
, loff_t
*pos
)
980 struct hlist_node
*node
= v
;
987 for (*cpu
= cpumask_next(*cpu
, cpu_possible_mask
); *cpu
< nr_cpu_ids
;
988 *cpu
= cpumask_next(*cpu
, cpu_possible_mask
)) {
989 struct hlist_head
*bucket
= per_cpu_ptr(head
, *cpu
);
991 if (!hlist_empty(bucket
))
992 return bucket
->first
;
996 EXPORT_SYMBOL(seq_hlist_next_percpu
);