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>
21 * seq_files have a buffer which can may overflow. When this happens a larger
22 * buffer is reallocated and all the data will be printed again.
23 * The overflow state is true when m->count == m->size.
25 static bool seq_overflow(struct seq_file *m)
27 return m->count == m->size;
30 static void seq_set_overflow(struct seq_file *m)
35 static void *seq_buf_alloc(unsigned long size)
39 buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
40 if (!buf && size > PAGE_SIZE)
46 * seq_open - initialize sequential file
47 * @file: file we initialize
48 * @op: method table describing the sequence
50 * seq_open() sets @file, associating it with a sequence described
51 * by @op. @op->start() sets the iterator up and returns the first
52 * element of sequence. @op->stop() shuts it down. @op->next()
53 * returns the next element of sequence. @op->show() prints element
54 * into the buffer. In case of error ->start() and ->next() return
55 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
56 * returns 0 in case of success and negative number in case of error.
57 * Returning SEQ_SKIP means "discard this element and move on".
59 int seq_open(struct file *file, const struct seq_operations *op)
61 struct seq_file *p = file->private_data;
64 p = kmalloc(sizeof(*p), GFP_KERNEL);
67 file->private_data = p;
69 memset(p, 0, sizeof(*p));
73 p->user_ns = file->f_cred->user_ns;
77 * Wrappers around seq_open(e.g. swaps_open) need to be
78 * aware of this. If they set f_version themselves, they
79 * should call seq_open first and then set f_version.
84 * seq_files support lseek() and pread(). They do not implement
85 * write() at all, but we clear FMODE_PWRITE here for historical
88 * If a client of seq_files a) implements file.write() and b) wishes to
89 * support pwrite() then that client will need to implement its own
90 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
92 file->f_mode &= ~FMODE_PWRITE;
95 EXPORT_SYMBOL(seq_open);
97 static int traverse(struct seq_file *m, loff_t offset)
99 loff_t pos = 0, index;
105 m->count = m->from = 0;
111 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
115 p = m->op->start(m, &index);
120 error = m->op->show(m, p);
123 if (unlikely(error)) {
129 if (pos + m->count > offset) {
130 m->from = offset - pos;
142 p = m->op->next(m, p, &index);
152 m->buf = seq_buf_alloc(m->size <<= 1);
153 return !m->buf ? -ENOMEM : -EAGAIN;
157 * seq_read - ->read() method for sequential files.
158 * @file: the file to read from
159 * @buf: the buffer to read to
160 * @size: the maximum number of bytes to read
161 * @ppos: the current position in the file
163 * Ready-made ->f_op->read()
165 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
167 struct seq_file *m = file->private_data;
174 mutex_lock(&m->lock);
177 * seq_file->op->..m_start/m_stop/m_next may do special actions
178 * or optimisations based on the file->f_version, so we want to
179 * pass the file->f_version to those methods.
181 * seq_file->version is just copy of f_version, and seq_file
182 * methods can treat it simply as file version.
183 * It is copied in first and copied out after all operations.
184 * It is convenient to have it as part of structure to avoid the
185 * need of passing another argument to all the seq_file methods.
187 m->version = file->f_version;
189 /* Don't assume *ppos is where we left it */
190 if (unlikely(*ppos != m->read_pos)) {
191 while ((err = traverse(m, *ppos)) == -EAGAIN)
194 /* With prejudice... */
205 /* grab buffer if we didn't have one */
207 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
211 /* if not empty - flush it first */
213 n = min(m->count, size);
214 err = copy_to_user(buf, m->buf + m->from, n);
227 /* we need at least one record in buffer */
229 p = m->op->start(m, &pos);
234 err = m->op->show(m, p);
239 if (unlikely(!m->count)) {
240 p = m->op->next(m, p, &pos);
244 if (m->count < m->size)
249 m->buf = seq_buf_alloc(m->size <<= 1);
254 p = m->op->start(m, &pos);
260 /* they want more? let's try to get some more */
261 while (m->count < size) {
262 size_t offs = m->count;
264 p = m->op->next(m, p, &next);
265 if (!p || IS_ERR(p)) {
269 err = m->op->show(m, p);
270 if (seq_overflow(m) || err) {
272 if (likely(err <= 0))
278 n = min(m->count, size);
279 err = copy_to_user(buf, m->buf, n);
294 m->read_pos += copied;
296 file->f_version = m->version;
297 mutex_unlock(&m->lock);
306 EXPORT_SYMBOL(seq_read);
309 * seq_lseek - ->llseek() method for sequential files.
310 * @file: the file in question
311 * @offset: new position
312 * @whence: 0 for absolute, 1 for relative position
314 * Ready-made ->f_op->llseek()
316 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
318 struct seq_file *m = file->private_data;
319 loff_t retval = -EINVAL;
321 mutex_lock(&m->lock);
322 m->version = file->f_version;
325 offset += file->f_pos;
330 if (offset != m->read_pos) {
331 while ((retval = traverse(m, offset)) == -EAGAIN)
334 /* with extreme prejudice... */
341 m->read_pos = offset;
342 retval = file->f_pos = offset;
345 file->f_pos = offset;
348 file->f_version = m->version;
349 mutex_unlock(&m->lock);
352 EXPORT_SYMBOL(seq_lseek);
355 * seq_release - free the structures associated with sequential file.
356 * @file: file in question
359 * Frees the structures associated with sequential file; can be used
360 * as ->f_op->release() if you don't have private data to destroy.
362 int seq_release(struct inode *inode, struct file *file)
364 struct seq_file *m = file->private_data;
369 EXPORT_SYMBOL(seq_release);
372 * seq_escape - print string into buffer, escaping some characters
375 * @esc: set of characters that need escaping
377 * Puts string into buffer, replacing each occurrence of character from
378 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
381 int seq_escape(struct seq_file *m, const char *s, const char *esc)
383 char *end = m->buf + m->size;
387 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
388 if (!strchr(esc, c)) {
394 *p++ = '0' + ((c & 0300) >> 6);
395 *p++ = '0' + ((c & 070) >> 3);
396 *p++ = '0' + (c & 07);
402 m->count = p - m->buf;
405 EXPORT_SYMBOL(seq_escape);
407 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
411 if (m->count < m->size) {
412 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
413 if (m->count + len < m->size) {
421 EXPORT_SYMBOL(seq_vprintf);
423 int seq_printf(struct seq_file *m, const char *f, ...)
429 ret = seq_vprintf(m, f, args);
434 EXPORT_SYMBOL(seq_printf);
437 * mangle_path - mangle and copy path to buffer beginning
439 * @p: beginning of path in above buffer
440 * @esc: set of characters that need escaping
442 * Copy the path from @p to @s, replacing each occurrence of character from
443 * @esc with usual octal escape.
444 * Returns pointer past last written character in @s, or NULL in case of
447 char *mangle_path(char *s, const char *p, const char *esc)
453 } else if (!strchr(esc, c)) {
455 } else if (s + 4 > p) {
459 *s++ = '0' + ((c & 0300) >> 6);
460 *s++ = '0' + ((c & 070) >> 3);
461 *s++ = '0' + (c & 07);
466 EXPORT_SYMBOL(mangle_path);
469 * seq_path - seq_file interface to print a pathname
470 * @m: the seq_file handle
471 * @path: the struct path to print
472 * @esc: set of characters to escape in the output
474 * return the absolute path of 'path', as represented by the
475 * dentry / mnt pair in the path parameter.
477 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
480 size_t size = seq_get_buf(m, &buf);
484 char *p = d_path(path, buf, size);
486 char *end = mangle_path(buf, p, esc);
495 EXPORT_SYMBOL(seq_path);
498 * Same as seq_path, but relative to supplied root.
500 int seq_path_root(struct seq_file *m, const struct path *path,
501 const struct path *root, const char *esc)
504 size_t size = seq_get_buf(m, &buf);
505 int res = -ENAMETOOLONG;
510 p = __d_path(path, root, buf, size);
515 char *end = mangle_path(buf, p, esc);
524 return res < 0 && res != -ENAMETOOLONG ? res : 0;
528 * returns the path of the 'dentry' from the root of its filesystem.
530 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
533 size_t size = seq_get_buf(m, &buf);
537 char *p = dentry_path(dentry, buf, size);
539 char *end = mangle_path(buf, p, esc);
549 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
550 unsigned int nr_bits)
552 if (m->count < m->size) {
553 int len = bitmap_scnprintf(m->buf + m->count,
554 m->size - m->count, bits, nr_bits);
555 if (m->count + len < m->size) {
563 EXPORT_SYMBOL(seq_bitmap);
565 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
566 unsigned int nr_bits)
568 if (m->count < m->size) {
569 int len = bitmap_scnlistprintf(m->buf + m->count,
570 m->size - m->count, bits, nr_bits);
571 if (m->count + len < m->size) {
579 EXPORT_SYMBOL(seq_bitmap_list);
581 static void *single_start(struct seq_file *p, loff_t *pos)
583 return NULL + (*pos == 0);
586 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
592 static void single_stop(struct seq_file *p, void *v)
596 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
599 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
603 op->start = single_start;
604 op->next = single_next;
605 op->stop = single_stop;
607 res = seq_open(file, op);
609 ((struct seq_file *)file->private_data)->private = data;
615 EXPORT_SYMBOL(single_open);
617 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
618 void *data, size_t size)
620 char *buf = seq_buf_alloc(size);
624 ret = single_open(file, show, data);
629 ((struct seq_file *)file->private_data)->buf = buf;
630 ((struct seq_file *)file->private_data)->size = size;
633 EXPORT_SYMBOL(single_open_size);
635 int single_release(struct inode *inode, struct file *file)
637 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
638 int res = seq_release(inode, file);
642 EXPORT_SYMBOL(single_release);
644 int seq_release_private(struct inode *inode, struct file *file)
646 struct seq_file *seq = file->private_data;
650 return seq_release(inode, file);
652 EXPORT_SYMBOL(seq_release_private);
654 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
659 struct seq_file *seq;
661 private = kzalloc(psize, GFP_KERNEL);
665 rc = seq_open(f, ops);
669 seq = f->private_data;
670 seq->private = private;
678 EXPORT_SYMBOL(__seq_open_private);
680 int seq_open_private(struct file *filp, const struct seq_operations *ops,
683 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
685 EXPORT_SYMBOL(seq_open_private);
687 int seq_putc(struct seq_file *m, char c)
689 if (m->count < m->size) {
690 m->buf[m->count++] = c;
695 EXPORT_SYMBOL(seq_putc);
697 int seq_puts(struct seq_file *m, const char *s)
700 if (m->count + len < m->size) {
701 memcpy(m->buf + m->count, s, len);
708 EXPORT_SYMBOL(seq_puts);
711 * A helper routine for putting decimal numbers without rich format of printf().
712 * only 'unsigned long long' is supported.
713 * This routine will put one byte delimiter + number into seq_file.
714 * This routine is very quick when you show lots of numbers.
715 * In usual cases, it will be better to use seq_printf(). It's easier to read.
717 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
718 unsigned long long num)
722 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
726 m->buf[m->count++] = delimiter;
729 m->buf[m->count++] = num + '0';
733 len = num_to_str(m->buf + m->count, m->size - m->count, num);
742 EXPORT_SYMBOL(seq_put_decimal_ull);
744 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
748 if (m->count + 3 >= m->size) {
753 m->buf[m->count++] = delimiter;
757 return seq_put_decimal_ull(m, delimiter, num);
760 EXPORT_SYMBOL(seq_put_decimal_ll);
763 * seq_write - write arbitrary data to buffer
764 * @seq: seq_file identifying the buffer to which data should be written
765 * @data: data address
766 * @len: number of bytes
768 * Return 0 on success, non-zero otherwise.
770 int seq_write(struct seq_file *seq, const void *data, size_t len)
772 if (seq->count + len < seq->size) {
773 memcpy(seq->buf + seq->count, data, len);
777 seq_set_overflow(seq);
780 EXPORT_SYMBOL(seq_write);
783 * seq_pad - write padding spaces to buffer
784 * @m: seq_file identifying the buffer to which data should be written
785 * @c: the byte to append after padding if non-zero
787 void seq_pad(struct seq_file *m, char c)
789 int size = m->pad_until - m->count;
791 seq_printf(m, "%*s", size, "");
795 EXPORT_SYMBOL(seq_pad);
797 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
799 struct list_head *lh;
801 list_for_each(lh, head)
807 EXPORT_SYMBOL(seq_list_start);
809 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
814 return seq_list_start(head, pos - 1);
816 EXPORT_SYMBOL(seq_list_start_head);
818 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
820 struct list_head *lh;
822 lh = ((struct list_head *)v)->next;
824 return lh == head ? NULL : lh;
826 EXPORT_SYMBOL(seq_list_next);
829 * seq_hlist_start - start an iteration of a hlist
830 * @head: the head of the hlist
831 * @pos: the start position of the sequence
833 * Called at seq_file->op->start().
835 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
837 struct hlist_node *node;
839 hlist_for_each(node, head)
844 EXPORT_SYMBOL(seq_hlist_start);
847 * seq_hlist_start_head - start an iteration of a hlist
848 * @head: the head of the hlist
849 * @pos: the start position of the sequence
851 * Called at seq_file->op->start(). Call this function if you want to
852 * print a header at the top of the output.
854 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
857 return SEQ_START_TOKEN;
859 return seq_hlist_start(head, pos - 1);
861 EXPORT_SYMBOL(seq_hlist_start_head);
864 * seq_hlist_next - move to the next position of the hlist
865 * @v: the current iterator
866 * @head: the head of the hlist
867 * @ppos: the current position
869 * Called at seq_file->op->next().
871 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
874 struct hlist_node *node = v;
877 if (v == SEQ_START_TOKEN)
882 EXPORT_SYMBOL(seq_hlist_next);
885 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
886 * @head: the head of the hlist
887 * @pos: the start position of the sequence
889 * Called at seq_file->op->start().
891 * This list-traversal primitive may safely run concurrently with
892 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
893 * as long as the traversal is guarded by rcu_read_lock().
895 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
898 struct hlist_node *node;
900 __hlist_for_each_rcu(node, head)
905 EXPORT_SYMBOL(seq_hlist_start_rcu);
908 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
909 * @head: the head of the hlist
910 * @pos: the start position of the sequence
912 * Called at seq_file->op->start(). Call this function if you want to
913 * print a header at the top of the output.
915 * This list-traversal primitive may safely run concurrently with
916 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
917 * as long as the traversal is guarded by rcu_read_lock().
919 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
923 return SEQ_START_TOKEN;
925 return seq_hlist_start_rcu(head, pos - 1);
927 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
930 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
931 * @v: the current iterator
932 * @head: the head of the hlist
933 * @ppos: the current position
935 * Called at seq_file->op->next().
937 * This list-traversal primitive may safely run concurrently with
938 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
939 * as long as the traversal is guarded by rcu_read_lock().
941 struct hlist_node *seq_hlist_next_rcu(void *v,
942 struct hlist_head *head,
945 struct hlist_node *node = v;
948 if (v == SEQ_START_TOKEN)
949 return rcu_dereference(head->first);
951 return rcu_dereference(node->next);
953 EXPORT_SYMBOL(seq_hlist_next_rcu);
956 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
957 * @head: pointer to percpu array of struct hlist_heads
958 * @cpu: pointer to cpu "cursor"
959 * @pos: start position of sequence
961 * Called at seq_file->op->start().
964 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
966 struct hlist_node *node;
968 for_each_possible_cpu(*cpu) {
969 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
976 EXPORT_SYMBOL(seq_hlist_start_percpu);
979 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
980 * @v: pointer to current hlist_node
981 * @head: pointer to percpu array of struct hlist_heads
982 * @cpu: pointer to cpu "cursor"
983 * @pos: start position of sequence
985 * Called at seq_file->op->next().
988 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
989 int *cpu, loff_t *pos)
991 struct hlist_node *node = v;
998 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
999 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1000 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1002 if (!hlist_empty(bucket))
1003 return bucket->first;
1007 EXPORT_SYMBOL(seq_hlist_next_percpu);