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;
345 file->f_version = m->version;
346 mutex_unlock(&m->lock);
349 EXPORT_SYMBOL(seq_lseek);
352 * seq_release - free the structures associated with sequential file.
353 * @file: file in question
356 * Frees the structures associated with sequential file; can be used
357 * as ->f_op->release() if you don't have private data to destroy.
359 int seq_release(struct inode *inode, struct file *file)
361 struct seq_file *m = file->private_data;
366 EXPORT_SYMBOL(seq_release);
369 * seq_escape - print string into buffer, escaping some characters
372 * @esc: set of characters that need escaping
374 * Puts string into buffer, replacing each occurrence of character from
375 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
378 int seq_escape(struct seq_file *m, const char *s, const char *esc)
380 char *end = m->buf + m->size;
384 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
385 if (!strchr(esc, c)) {
391 *p++ = '0' + ((c & 0300) >> 6);
392 *p++ = '0' + ((c & 070) >> 3);
393 *p++ = '0' + (c & 07);
399 m->count = p - m->buf;
402 EXPORT_SYMBOL(seq_escape);
404 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
408 if (m->count < m->size) {
409 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
410 if (m->count + len < m->size) {
418 EXPORT_SYMBOL(seq_vprintf);
420 int seq_printf(struct seq_file *m, const char *f, ...)
426 ret = seq_vprintf(m, f, args);
431 EXPORT_SYMBOL(seq_printf);
434 * mangle_path - mangle and copy path to buffer beginning
436 * @p: beginning of path in above buffer
437 * @esc: set of characters that need escaping
439 * Copy the path from @p to @s, replacing each occurrence of character from
440 * @esc with usual octal escape.
441 * Returns pointer past last written character in @s, or NULL in case of
444 char *mangle_path(char *s, const char *p, const char *esc)
450 } else if (!strchr(esc, c)) {
452 } else if (s + 4 > p) {
456 *s++ = '0' + ((c & 0300) >> 6);
457 *s++ = '0' + ((c & 070) >> 3);
458 *s++ = '0' + (c & 07);
463 EXPORT_SYMBOL(mangle_path);
466 * seq_path - seq_file interface to print a pathname
467 * @m: the seq_file handle
468 * @path: the struct path to print
469 * @esc: set of characters to escape in the output
471 * return the absolute path of 'path', as represented by the
472 * dentry / mnt pair in the path parameter.
474 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
477 size_t size = seq_get_buf(m, &buf);
481 char *p = d_path(path, buf, size);
483 char *end = mangle_path(buf, p, esc);
492 EXPORT_SYMBOL(seq_path);
495 * Same as seq_path, but relative to supplied root.
497 int seq_path_root(struct seq_file *m, const struct path *path,
498 const struct path *root, const char *esc)
501 size_t size = seq_get_buf(m, &buf);
502 int res = -ENAMETOOLONG;
507 p = __d_path(path, root, buf, size);
512 char *end = mangle_path(buf, p, esc);
521 return res < 0 && res != -ENAMETOOLONG ? res : 0;
525 * returns the path of the 'dentry' from the root of its filesystem.
527 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
530 size_t size = seq_get_buf(m, &buf);
534 char *p = dentry_path(dentry, buf, size);
536 char *end = mangle_path(buf, p, esc);
546 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
547 unsigned int nr_bits)
549 if (m->count < m->size) {
550 int len = bitmap_scnprintf(m->buf + m->count,
551 m->size - m->count, bits, nr_bits);
552 if (m->count + len < m->size) {
560 EXPORT_SYMBOL(seq_bitmap);
562 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
563 unsigned int nr_bits)
565 if (m->count < m->size) {
566 int len = bitmap_scnlistprintf(m->buf + m->count,
567 m->size - m->count, bits, nr_bits);
568 if (m->count + len < m->size) {
576 EXPORT_SYMBOL(seq_bitmap_list);
578 static void *single_start(struct seq_file *p, loff_t *pos)
580 return NULL + (*pos == 0);
583 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
589 static void single_stop(struct seq_file *p, void *v)
593 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
596 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
600 op->start = single_start;
601 op->next = single_next;
602 op->stop = single_stop;
604 res = seq_open(file, op);
606 ((struct seq_file *)file->private_data)->private = data;
612 EXPORT_SYMBOL(single_open);
614 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
615 void *data, size_t size)
617 char *buf = seq_buf_alloc(size);
621 ret = single_open(file, show, data);
626 ((struct seq_file *)file->private_data)->buf = buf;
627 ((struct seq_file *)file->private_data)->size = size;
630 EXPORT_SYMBOL(single_open_size);
632 int single_release(struct inode *inode, struct file *file)
634 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
635 int res = seq_release(inode, file);
639 EXPORT_SYMBOL(single_release);
641 int seq_release_private(struct inode *inode, struct file *file)
643 struct seq_file *seq = file->private_data;
647 return seq_release(inode, file);
649 EXPORT_SYMBOL(seq_release_private);
651 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
656 struct seq_file *seq;
658 private = kzalloc(psize, GFP_KERNEL);
662 rc = seq_open(f, ops);
666 seq = f->private_data;
667 seq->private = private;
675 EXPORT_SYMBOL(__seq_open_private);
677 int seq_open_private(struct file *filp, const struct seq_operations *ops,
680 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
682 EXPORT_SYMBOL(seq_open_private);
684 int seq_putc(struct seq_file *m, char c)
686 if (m->count < m->size) {
687 m->buf[m->count++] = c;
692 EXPORT_SYMBOL(seq_putc);
694 int seq_puts(struct seq_file *m, const char *s)
697 if (m->count + len < m->size) {
698 memcpy(m->buf + m->count, s, len);
705 EXPORT_SYMBOL(seq_puts);
708 * A helper routine for putting decimal numbers without rich format of printf().
709 * only 'unsigned long long' is supported.
710 * This routine will put one byte delimiter + number into seq_file.
711 * This routine is very quick when you show lots of numbers.
712 * In usual cases, it will be better to use seq_printf(). It's easier to read.
714 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
715 unsigned long long num)
719 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
723 m->buf[m->count++] = delimiter;
726 m->buf[m->count++] = num + '0';
730 len = num_to_str(m->buf + m->count, m->size - m->count, num);
739 EXPORT_SYMBOL(seq_put_decimal_ull);
741 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
745 if (m->count + 3 >= m->size) {
750 m->buf[m->count++] = delimiter;
754 return seq_put_decimal_ull(m, delimiter, num);
757 EXPORT_SYMBOL(seq_put_decimal_ll);
760 * seq_write - write arbitrary data to buffer
761 * @seq: seq_file identifying the buffer to which data should be written
762 * @data: data address
763 * @len: number of bytes
765 * Return 0 on success, non-zero otherwise.
767 int seq_write(struct seq_file *seq, const void *data, size_t len)
769 if (seq->count + len < seq->size) {
770 memcpy(seq->buf + seq->count, data, len);
774 seq_set_overflow(seq);
777 EXPORT_SYMBOL(seq_write);
779 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
781 struct list_head *lh;
783 list_for_each(lh, head)
789 EXPORT_SYMBOL(seq_list_start);
791 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
796 return seq_list_start(head, pos - 1);
798 EXPORT_SYMBOL(seq_list_start_head);
800 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
802 struct list_head *lh;
804 lh = ((struct list_head *)v)->next;
806 return lh == head ? NULL : lh;
808 EXPORT_SYMBOL(seq_list_next);
811 * seq_hlist_start - start an iteration of a hlist
812 * @head: the head of the hlist
813 * @pos: the start position of the sequence
815 * Called at seq_file->op->start().
817 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
819 struct hlist_node *node;
821 hlist_for_each(node, head)
826 EXPORT_SYMBOL(seq_hlist_start);
829 * seq_hlist_start_head - 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(). Call this function if you want to
834 * print a header at the top of the output.
836 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
839 return SEQ_START_TOKEN;
841 return seq_hlist_start(head, pos - 1);
843 EXPORT_SYMBOL(seq_hlist_start_head);
846 * seq_hlist_next - move to the next position of the hlist
847 * @v: the current iterator
848 * @head: the head of the hlist
849 * @ppos: the current position
851 * Called at seq_file->op->next().
853 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
856 struct hlist_node *node = v;
859 if (v == SEQ_START_TOKEN)
864 EXPORT_SYMBOL(seq_hlist_next);
867 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
868 * @head: the head of the hlist
869 * @pos: the start position of the sequence
871 * Called at seq_file->op->start().
873 * This list-traversal primitive may safely run concurrently with
874 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
875 * as long as the traversal is guarded by rcu_read_lock().
877 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
880 struct hlist_node *node;
882 __hlist_for_each_rcu(node, head)
887 EXPORT_SYMBOL(seq_hlist_start_rcu);
890 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
891 * @head: the head of the hlist
892 * @pos: the start position of the sequence
894 * Called at seq_file->op->start(). Call this function if you want to
895 * print a header at the top of the output.
897 * This list-traversal primitive may safely run concurrently with
898 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
899 * as long as the traversal is guarded by rcu_read_lock().
901 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
905 return SEQ_START_TOKEN;
907 return seq_hlist_start_rcu(head, pos - 1);
909 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
912 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
913 * @v: the current iterator
914 * @head: the head of the hlist
915 * @ppos: the current position
917 * Called at seq_file->op->next().
919 * This list-traversal primitive may safely run concurrently with
920 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
921 * as long as the traversal is guarded by rcu_read_lock().
923 struct hlist_node *seq_hlist_next_rcu(void *v,
924 struct hlist_head *head,
927 struct hlist_node *node = v;
930 if (v == SEQ_START_TOKEN)
931 return rcu_dereference(head->first);
933 return rcu_dereference(node->next);
935 EXPORT_SYMBOL(seq_hlist_next_rcu);