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);
151 m->buf = seq_buf_alloc(m->size <<= 1);
152 return !m->buf ? -ENOMEM : -EAGAIN;
156 * seq_read - ->read() method for sequential files.
157 * @file: the file to read from
158 * @buf: the buffer to read to
159 * @size: the maximum number of bytes to read
160 * @ppos: the current position in the file
162 * Ready-made ->f_op->read()
164 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
166 struct seq_file *m = file->private_data;
173 mutex_lock(&m->lock);
176 * seq_file->op->..m_start/m_stop/m_next may do special actions
177 * or optimisations based on the file->f_version, so we want to
178 * pass the file->f_version to those methods.
180 * seq_file->version is just copy of f_version, and seq_file
181 * methods can treat it simply as file version.
182 * It is copied in first and copied out after all operations.
183 * It is convenient to have it as part of structure to avoid the
184 * need of passing another argument to all the seq_file methods.
186 m->version = file->f_version;
188 /* Don't assume *ppos is where we left it */
189 if (unlikely(*ppos != m->read_pos)) {
190 while ((err = traverse(m, *ppos)) == -EAGAIN)
193 /* With prejudice... */
204 /* grab buffer if we didn't have one */
206 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
210 /* if not empty - flush it first */
212 n = min(m->count, size);
213 err = copy_to_user(buf, m->buf + m->from, n);
226 /* we need at least one record in buffer */
228 p = m->op->start(m, &pos);
233 err = m->op->show(m, p);
238 if (unlikely(!m->count)) {
239 p = m->op->next(m, p, &pos);
243 if (m->count < m->size)
247 m->buf = seq_buf_alloc(m->size <<= 1);
253 p = m->op->start(m, &pos);
259 /* they want more? let's try to get some more */
260 while (m->count < size) {
261 size_t offs = m->count;
263 p = m->op->next(m, p, &next);
264 if (!p || IS_ERR(p)) {
268 err = m->op->show(m, p);
269 if (seq_overflow(m) || err) {
271 if (likely(err <= 0))
277 n = min(m->count, size);
278 err = copy_to_user(buf, m->buf, n);
293 m->read_pos += copied;
295 file->f_version = m->version;
296 mutex_unlock(&m->lock);
305 EXPORT_SYMBOL(seq_read);
308 * seq_lseek - ->llseek() method for sequential files.
309 * @file: the file in question
310 * @offset: new position
311 * @whence: 0 for absolute, 1 for relative position
313 * Ready-made ->f_op->llseek()
315 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
317 struct seq_file *m = file->private_data;
318 loff_t retval = -EINVAL;
320 mutex_lock(&m->lock);
321 m->version = file->f_version;
324 offset += file->f_pos;
329 if (offset != m->read_pos) {
330 while ((retval = traverse(m, offset)) == -EAGAIN)
333 /* with extreme prejudice... */
340 m->read_pos = offset;
341 retval = file->f_pos = offset;
344 file->f_pos = offset;
347 file->f_version = m->version;
348 mutex_unlock(&m->lock);
351 EXPORT_SYMBOL(seq_lseek);
354 * seq_release - free the structures associated with sequential file.
355 * @file: file in question
358 * Frees the structures associated with sequential file; can be used
359 * as ->f_op->release() if you don't have private data to destroy.
361 int seq_release(struct inode *inode, struct file *file)
363 struct seq_file *m = file->private_data;
368 EXPORT_SYMBOL(seq_release);
371 * seq_escape - print string into buffer, escaping some characters
374 * @esc: set of characters that need escaping
376 * Puts string into buffer, replacing each occurrence of character from
377 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
380 int seq_escape(struct seq_file *m, const char *s, const char *esc)
382 char *end = m->buf + m->size;
386 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
387 if (!strchr(esc, c)) {
393 *p++ = '0' + ((c & 0300) >> 6);
394 *p++ = '0' + ((c & 070) >> 3);
395 *p++ = '0' + (c & 07);
401 m->count = p - m->buf;
404 EXPORT_SYMBOL(seq_escape);
406 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
410 if (m->count < m->size) {
411 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
412 if (m->count + len < m->size) {
420 EXPORT_SYMBOL(seq_vprintf);
422 int seq_printf(struct seq_file *m, const char *f, ...)
428 ret = seq_vprintf(m, f, args);
433 EXPORT_SYMBOL(seq_printf);
436 * mangle_path - mangle and copy path to buffer beginning
438 * @p: beginning of path in above buffer
439 * @esc: set of characters that need escaping
441 * Copy the path from @p to @s, replacing each occurrence of character from
442 * @esc with usual octal escape.
443 * Returns pointer past last written character in @s, or NULL in case of
446 char *mangle_path(char *s, const char *p, const char *esc)
452 } else if (!strchr(esc, c)) {
454 } else if (s + 4 > p) {
458 *s++ = '0' + ((c & 0300) >> 6);
459 *s++ = '0' + ((c & 070) >> 3);
460 *s++ = '0' + (c & 07);
465 EXPORT_SYMBOL(mangle_path);
468 * seq_path - seq_file interface to print a pathname
469 * @m: the seq_file handle
470 * @path: the struct path to print
471 * @esc: set of characters to escape in the output
473 * return the absolute path of 'path', as represented by the
474 * dentry / mnt pair in the path parameter.
476 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
479 size_t size = seq_get_buf(m, &buf);
483 char *p = d_path(path, buf, size);
485 char *end = mangle_path(buf, p, esc);
494 EXPORT_SYMBOL(seq_path);
497 * Same as seq_path, but relative to supplied root.
499 int seq_path_root(struct seq_file *m, const struct path *path,
500 const struct path *root, const char *esc)
503 size_t size = seq_get_buf(m, &buf);
504 int res = -ENAMETOOLONG;
509 p = __d_path(path, root, buf, size);
514 char *end = mangle_path(buf, p, esc);
523 return res < 0 && res != -ENAMETOOLONG ? res : 0;
527 * returns the path of the 'dentry' from the root of its filesystem.
529 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
532 size_t size = seq_get_buf(m, &buf);
536 char *p = dentry_path(dentry, buf, size);
538 char *end = mangle_path(buf, p, esc);
548 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
549 unsigned int nr_bits)
551 if (m->count < m->size) {
552 int len = bitmap_scnprintf(m->buf + m->count,
553 m->size - m->count, bits, nr_bits);
554 if (m->count + len < m->size) {
562 EXPORT_SYMBOL(seq_bitmap);
564 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
565 unsigned int nr_bits)
567 if (m->count < m->size) {
568 int len = bitmap_scnlistprintf(m->buf + m->count,
569 m->size - m->count, bits, nr_bits);
570 if (m->count + len < m->size) {
578 EXPORT_SYMBOL(seq_bitmap_list);
580 static void *single_start(struct seq_file *p, loff_t *pos)
582 return NULL + (*pos == 0);
585 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
591 static void single_stop(struct seq_file *p, void *v)
595 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
598 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
602 op->start = single_start;
603 op->next = single_next;
604 op->stop = single_stop;
606 res = seq_open(file, op);
608 ((struct seq_file *)file->private_data)->private = data;
614 EXPORT_SYMBOL(single_open);
616 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
617 void *data, size_t size)
619 char *buf = seq_buf_alloc(size);
623 ret = single_open(file, show, data);
628 ((struct seq_file *)file->private_data)->buf = buf;
629 ((struct seq_file *)file->private_data)->size = size;
632 EXPORT_SYMBOL(single_open_size);
634 int single_release(struct inode *inode, struct file *file)
636 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
637 int res = seq_release(inode, file);
641 EXPORT_SYMBOL(single_release);
643 int seq_release_private(struct inode *inode, struct file *file)
645 struct seq_file *seq = file->private_data;
649 return seq_release(inode, file);
651 EXPORT_SYMBOL(seq_release_private);
653 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
658 struct seq_file *seq;
660 private = kzalloc(psize, GFP_KERNEL);
664 rc = seq_open(f, ops);
668 seq = f->private_data;
669 seq->private = private;
677 EXPORT_SYMBOL(__seq_open_private);
679 int seq_open_private(struct file *filp, const struct seq_operations *ops,
682 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
684 EXPORT_SYMBOL(seq_open_private);
686 int seq_putc(struct seq_file *m, char c)
688 if (m->count < m->size) {
689 m->buf[m->count++] = c;
694 EXPORT_SYMBOL(seq_putc);
696 int seq_puts(struct seq_file *m, const char *s)
699 if (m->count + len < m->size) {
700 memcpy(m->buf + m->count, s, len);
707 EXPORT_SYMBOL(seq_puts);
710 * A helper routine for putting decimal numbers without rich format of printf().
711 * only 'unsigned long long' is supported.
712 * This routine will put one byte delimiter + number into seq_file.
713 * This routine is very quick when you show lots of numbers.
714 * In usual cases, it will be better to use seq_printf(). It's easier to read.
716 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
717 unsigned long long num)
721 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
725 m->buf[m->count++] = delimiter;
728 m->buf[m->count++] = num + '0';
732 len = num_to_str(m->buf + m->count, m->size - m->count, num);
741 EXPORT_SYMBOL(seq_put_decimal_ull);
743 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
747 if (m->count + 3 >= m->size) {
752 m->buf[m->count++] = delimiter;
756 return seq_put_decimal_ull(m, delimiter, num);
759 EXPORT_SYMBOL(seq_put_decimal_ll);
762 * seq_write - write arbitrary data to buffer
763 * @seq: seq_file identifying the buffer to which data should be written
764 * @data: data address
765 * @len: number of bytes
767 * Return 0 on success, non-zero otherwise.
769 int seq_write(struct seq_file *seq, const void *data, size_t len)
771 if (seq->count + len < seq->size) {
772 memcpy(seq->buf + seq->count, data, len);
776 seq_set_overflow(seq);
779 EXPORT_SYMBOL(seq_write);
781 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
783 struct list_head *lh;
785 list_for_each(lh, head)
791 EXPORT_SYMBOL(seq_list_start);
793 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
798 return seq_list_start(head, pos - 1);
800 EXPORT_SYMBOL(seq_list_start_head);
802 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
804 struct list_head *lh;
806 lh = ((struct list_head *)v)->next;
808 return lh == head ? NULL : lh;
810 EXPORT_SYMBOL(seq_list_next);
813 * seq_hlist_start - start an iteration of a hlist
814 * @head: the head of the hlist
815 * @pos: the start position of the sequence
817 * Called at seq_file->op->start().
819 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
821 struct hlist_node *node;
823 hlist_for_each(node, head)
828 EXPORT_SYMBOL(seq_hlist_start);
831 * seq_hlist_start_head - start an iteration of a hlist
832 * @head: the head of the hlist
833 * @pos: the start position of the sequence
835 * Called at seq_file->op->start(). Call this function if you want to
836 * print a header at the top of the output.
838 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
841 return SEQ_START_TOKEN;
843 return seq_hlist_start(head, pos - 1);
845 EXPORT_SYMBOL(seq_hlist_start_head);
848 * seq_hlist_next - move to the next position of the hlist
849 * @v: the current iterator
850 * @head: the head of the hlist
851 * @ppos: the current position
853 * Called at seq_file->op->next().
855 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
858 struct hlist_node *node = v;
861 if (v == SEQ_START_TOKEN)
866 EXPORT_SYMBOL(seq_hlist_next);
869 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
870 * @head: the head of the hlist
871 * @pos: the start position of the sequence
873 * Called at seq_file->op->start().
875 * This list-traversal primitive may safely run concurrently with
876 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
877 * as long as the traversal is guarded by rcu_read_lock().
879 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
882 struct hlist_node *node;
884 __hlist_for_each_rcu(node, head)
889 EXPORT_SYMBOL(seq_hlist_start_rcu);
892 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
893 * @head: the head of the hlist
894 * @pos: the start position of the sequence
896 * Called at seq_file->op->start(). Call this function if you want to
897 * print a header at the top of the output.
899 * This list-traversal primitive may safely run concurrently with
900 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
901 * as long as the traversal is guarded by rcu_read_lock().
903 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
907 return SEQ_START_TOKEN;
909 return seq_hlist_start_rcu(head, pos - 1);
911 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
914 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
915 * @v: the current iterator
916 * @head: the head of the hlist
917 * @ppos: the current position
919 * Called at seq_file->op->next().
921 * This list-traversal primitive may safely run concurrently with
922 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
923 * as long as the traversal is guarded by rcu_read_lock().
925 struct hlist_node *seq_hlist_next_rcu(void *v,
926 struct hlist_head *head,
929 struct hlist_node *node = v;
932 if (v == SEQ_START_TOKEN)
933 return rcu_dereference(head->first);
935 return rcu_dereference(node->next);
937 EXPORT_SYMBOL(seq_hlist_next_rcu);