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".
52 int seq_open(struct file *file, const struct seq_operations *op)
54 struct seq_file *p = file->private_data;
57 p = kmalloc(sizeof(*p), GFP_KERNEL);
60 file->private_data = p;
62 memset(p, 0, sizeof(*p));
66 p->user_ns = file->f_cred->user_ns;
70 * Wrappers around seq_open(e.g. swaps_open) need to be
71 * aware of this. If they set f_version themselves, they
72 * should call seq_open first and then set f_version.
77 * seq_files support lseek() and pread(). They do not implement
78 * write() at all, but we clear FMODE_PWRITE here for historical
81 * If a client of seq_files a) implements file.write() and b) wishes to
82 * support pwrite() then that client will need to implement its own
83 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
85 file->f_mode &= ~FMODE_PWRITE;
88 EXPORT_SYMBOL(seq_open);
90 static int traverse(struct seq_file *m, loff_t offset)
92 loff_t pos = 0, index;
98 m->count = m->from = 0;
104 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
108 p = m->op->start(m, &index);
113 error = m->op->show(m, p);
116 if (unlikely(error)) {
120 if (seq_has_overflowed(m))
122 if (pos + m->count > offset) {
123 m->from = offset - pos;
135 p = m->op->next(m, p, &index);
145 m->buf = seq_buf_alloc(m->size <<= 1);
146 return !m->buf ? -ENOMEM : -EAGAIN;
150 * seq_read - ->read() method for sequential files.
151 * @file: the file to read from
152 * @buf: the buffer to read to
153 * @size: the maximum number of bytes to read
154 * @ppos: the current position in the file
156 * Ready-made ->f_op->read()
158 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
160 struct seq_file *m = file->private_data;
167 mutex_lock(&m->lock);
170 * seq_file->op->..m_start/m_stop/m_next may do special actions
171 * or optimisations based on the file->f_version, so we want to
172 * pass the file->f_version to those methods.
174 * seq_file->version is just copy of f_version, and seq_file
175 * methods can treat it simply as file version.
176 * It is copied in first and copied out after all operations.
177 * It is convenient to have it as part of structure to avoid the
178 * need of passing another argument to all the seq_file methods.
180 m->version = file->f_version;
182 /* Don't assume *ppos is where we left it */
183 if (unlikely(*ppos != m->read_pos)) {
184 while ((err = traverse(m, *ppos)) == -EAGAIN)
187 /* With prejudice... */
198 /* grab buffer if we didn't have one */
200 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
204 /* if not empty - flush it first */
206 n = min(m->count, size);
207 err = copy_to_user(buf, m->buf + m->from, n);
220 /* we need at least one record in buffer */
222 p = m->op->start(m, &pos);
227 err = m->op->show(m, p);
232 if (unlikely(!m->count)) {
233 p = m->op->next(m, p, &pos);
237 if (m->count < m->size)
242 m->buf = seq_buf_alloc(m->size <<= 1);
247 p = m->op->start(m, &pos);
253 /* they want more? let's try to get some more */
254 while (m->count < size) {
255 size_t offs = m->count;
257 p = m->op->next(m, p, &next);
258 if (!p || IS_ERR(p)) {
262 err = m->op->show(m, p);
263 if (seq_has_overflowed(m) || err) {
265 if (likely(err <= 0))
271 n = min(m->count, size);
272 err = copy_to_user(buf, m->buf, n);
287 m->read_pos += copied;
289 file->f_version = m->version;
290 mutex_unlock(&m->lock);
299 EXPORT_SYMBOL(seq_read);
302 * seq_lseek - ->llseek() method for sequential files.
303 * @file: the file in question
304 * @offset: new position
305 * @whence: 0 for absolute, 1 for relative position
307 * Ready-made ->f_op->llseek()
309 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
311 struct seq_file *m = file->private_data;
312 loff_t retval = -EINVAL;
314 mutex_lock(&m->lock);
315 m->version = file->f_version;
318 offset += file->f_pos;
323 if (offset != m->read_pos) {
324 while ((retval = traverse(m, offset)) == -EAGAIN)
327 /* with extreme prejudice... */
334 m->read_pos = offset;
335 retval = file->f_pos = offset;
338 file->f_pos = offset;
341 file->f_version = m->version;
342 mutex_unlock(&m->lock);
345 EXPORT_SYMBOL(seq_lseek);
348 * seq_release - free the structures associated with sequential file.
349 * @file: file in question
352 * Frees the structures associated with sequential file; can be used
353 * as ->f_op->release() if you don't have private data to destroy.
355 int seq_release(struct inode *inode, struct file *file)
357 struct seq_file *m = file->private_data;
362 EXPORT_SYMBOL(seq_release);
365 * seq_escape - print string into buffer, escaping some characters
368 * @esc: set of characters that need escaping
370 * Puts string into buffer, replacing each occurrence of character from
371 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
374 int seq_escape(struct seq_file *m, const char *s, const char *esc)
376 char *end = m->buf + m->size;
380 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
381 if (!strchr(esc, c)) {
387 *p++ = '0' + ((c & 0300) >> 6);
388 *p++ = '0' + ((c & 070) >> 3);
389 *p++ = '0' + (c & 07);
395 m->count = p - m->buf;
398 EXPORT_SYMBOL(seq_escape);
400 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
404 if (m->count < m->size) {
405 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
406 if (m->count + len < m->size) {
414 EXPORT_SYMBOL(seq_vprintf);
416 int seq_printf(struct seq_file *m, const char *f, ...)
422 ret = seq_vprintf(m, f, args);
427 EXPORT_SYMBOL(seq_printf);
430 * mangle_path - mangle and copy path to buffer beginning
432 * @p: beginning of path in above buffer
433 * @esc: set of characters that need escaping
435 * Copy the path from @p to @s, replacing each occurrence of character from
436 * @esc with usual octal escape.
437 * Returns pointer past last written character in @s, or NULL in case of
440 char *mangle_path(char *s, const char *p, const char *esc)
446 } else if (!strchr(esc, c)) {
448 } else if (s + 4 > p) {
452 *s++ = '0' + ((c & 0300) >> 6);
453 *s++ = '0' + ((c & 070) >> 3);
454 *s++ = '0' + (c & 07);
459 EXPORT_SYMBOL(mangle_path);
462 * seq_path - seq_file interface to print a pathname
463 * @m: the seq_file handle
464 * @path: the struct path to print
465 * @esc: set of characters to escape in the output
467 * return the absolute path of 'path', as represented by the
468 * dentry / mnt pair in the path parameter.
470 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
473 size_t size = seq_get_buf(m, &buf);
477 char *p = d_path(path, buf, size);
479 char *end = mangle_path(buf, p, esc);
488 EXPORT_SYMBOL(seq_path);
491 * Same as seq_path, but relative to supplied root.
493 int seq_path_root(struct seq_file *m, const struct path *path,
494 const struct path *root, const char *esc)
497 size_t size = seq_get_buf(m, &buf);
498 int res = -ENAMETOOLONG;
503 p = __d_path(path, root, buf, size);
508 char *end = mangle_path(buf, p, esc);
517 return res < 0 && res != -ENAMETOOLONG ? res : 0;
521 * returns the path of the 'dentry' from the root of its filesystem.
523 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
526 size_t size = seq_get_buf(m, &buf);
530 char *p = dentry_path(dentry, buf, size);
532 char *end = mangle_path(buf, p, esc);
542 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
543 unsigned int nr_bits)
545 if (m->count < m->size) {
546 int len = bitmap_scnprintf(m->buf + m->count,
547 m->size - m->count, bits, nr_bits);
548 if (m->count + len < m->size) {
556 EXPORT_SYMBOL(seq_bitmap);
558 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
559 unsigned int nr_bits)
561 if (m->count < m->size) {
562 int len = bitmap_scnlistprintf(m->buf + m->count,
563 m->size - m->count, bits, nr_bits);
564 if (m->count + len < m->size) {
572 EXPORT_SYMBOL(seq_bitmap_list);
574 static void *single_start(struct seq_file *p, loff_t *pos)
576 return NULL + (*pos == 0);
579 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
585 static void single_stop(struct seq_file *p, void *v)
589 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
592 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
596 op->start = single_start;
597 op->next = single_next;
598 op->stop = single_stop;
600 res = seq_open(file, op);
602 ((struct seq_file *)file->private_data)->private = data;
608 EXPORT_SYMBOL(single_open);
610 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
611 void *data, size_t size)
613 char *buf = seq_buf_alloc(size);
617 ret = single_open(file, show, data);
622 ((struct seq_file *)file->private_data)->buf = buf;
623 ((struct seq_file *)file->private_data)->size = size;
626 EXPORT_SYMBOL(single_open_size);
628 int single_release(struct inode *inode, struct file *file)
630 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
631 int res = seq_release(inode, file);
635 EXPORT_SYMBOL(single_release);
637 int seq_release_private(struct inode *inode, struct file *file)
639 struct seq_file *seq = file->private_data;
643 return seq_release(inode, file);
645 EXPORT_SYMBOL(seq_release_private);
647 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
652 struct seq_file *seq;
654 private = kzalloc(psize, GFP_KERNEL);
658 rc = seq_open(f, ops);
662 seq = f->private_data;
663 seq->private = private;
671 EXPORT_SYMBOL(__seq_open_private);
673 int seq_open_private(struct file *filp, const struct seq_operations *ops,
676 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
678 EXPORT_SYMBOL(seq_open_private);
680 int seq_putc(struct seq_file *m, char c)
682 if (m->count < m->size) {
683 m->buf[m->count++] = c;
688 EXPORT_SYMBOL(seq_putc);
690 int seq_puts(struct seq_file *m, const char *s)
693 if (m->count + len < m->size) {
694 memcpy(m->buf + m->count, s, len);
701 EXPORT_SYMBOL(seq_puts);
704 * A helper routine for putting decimal numbers without rich format of printf().
705 * only 'unsigned long long' is supported.
706 * This routine will put one byte delimiter + number into seq_file.
707 * This routine is very quick when you show lots of numbers.
708 * In usual cases, it will be better to use seq_printf(). It's easier to read.
710 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
711 unsigned long long num)
715 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
719 m->buf[m->count++] = delimiter;
722 m->buf[m->count++] = num + '0';
726 len = num_to_str(m->buf + m->count, m->size - m->count, num);
735 EXPORT_SYMBOL(seq_put_decimal_ull);
737 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
741 if (m->count + 3 >= m->size) {
746 m->buf[m->count++] = delimiter;
750 return seq_put_decimal_ull(m, delimiter, num);
753 EXPORT_SYMBOL(seq_put_decimal_ll);
756 * seq_write - write arbitrary data to buffer
757 * @seq: seq_file identifying the buffer to which data should be written
758 * @data: data address
759 * @len: number of bytes
761 * Return 0 on success, non-zero otherwise.
763 int seq_write(struct seq_file *seq, const void *data, size_t len)
765 if (seq->count + len < seq->size) {
766 memcpy(seq->buf + seq->count, data, len);
770 seq_set_overflow(seq);
773 EXPORT_SYMBOL(seq_write);
776 * seq_pad - write padding spaces to buffer
777 * @m: seq_file identifying the buffer to which data should be written
778 * @c: the byte to append after padding if non-zero
780 void seq_pad(struct seq_file *m, char c)
782 int size = m->pad_until - m->count;
784 seq_printf(m, "%*s", size, "");
788 EXPORT_SYMBOL(seq_pad);
790 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
792 struct list_head *lh;
794 list_for_each(lh, head)
800 EXPORT_SYMBOL(seq_list_start);
802 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
807 return seq_list_start(head, pos - 1);
809 EXPORT_SYMBOL(seq_list_start_head);
811 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
813 struct list_head *lh;
815 lh = ((struct list_head *)v)->next;
817 return lh == head ? NULL : lh;
819 EXPORT_SYMBOL(seq_list_next);
822 * seq_hlist_start - start an iteration of a hlist
823 * @head: the head of the hlist
824 * @pos: the start position of the sequence
826 * Called at seq_file->op->start().
828 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
830 struct hlist_node *node;
832 hlist_for_each(node, head)
837 EXPORT_SYMBOL(seq_hlist_start);
840 * seq_hlist_start_head - start an iteration of a hlist
841 * @head: the head of the hlist
842 * @pos: the start position of the sequence
844 * Called at seq_file->op->start(). Call this function if you want to
845 * print a header at the top of the output.
847 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
850 return SEQ_START_TOKEN;
852 return seq_hlist_start(head, pos - 1);
854 EXPORT_SYMBOL(seq_hlist_start_head);
857 * seq_hlist_next - move to the next position of the hlist
858 * @v: the current iterator
859 * @head: the head of the hlist
860 * @ppos: the current position
862 * Called at seq_file->op->next().
864 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
867 struct hlist_node *node = v;
870 if (v == SEQ_START_TOKEN)
875 EXPORT_SYMBOL(seq_hlist_next);
878 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
879 * @head: the head of the hlist
880 * @pos: the start position of the sequence
882 * Called at seq_file->op->start().
884 * This list-traversal primitive may safely run concurrently with
885 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
886 * as long as the traversal is guarded by rcu_read_lock().
888 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
891 struct hlist_node *node;
893 __hlist_for_each_rcu(node, head)
898 EXPORT_SYMBOL(seq_hlist_start_rcu);
901 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
902 * @head: the head of the hlist
903 * @pos: the start position of the sequence
905 * Called at seq_file->op->start(). Call this function if you want to
906 * print a header at the top of the output.
908 * This list-traversal primitive may safely run concurrently with
909 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
910 * as long as the traversal is guarded by rcu_read_lock().
912 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
916 return SEQ_START_TOKEN;
918 return seq_hlist_start_rcu(head, pos - 1);
920 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
923 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
924 * @v: the current iterator
925 * @head: the head of the hlist
926 * @ppos: the current position
928 * Called at seq_file->op->next().
930 * This list-traversal primitive may safely run concurrently with
931 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
932 * as long as the traversal is guarded by rcu_read_lock().
934 struct hlist_node *seq_hlist_next_rcu(void *v,
935 struct hlist_head *head,
938 struct hlist_node *node = v;
941 if (v == SEQ_START_TOKEN)
942 return rcu_dereference(head->first);
944 return rcu_dereference(node->next);
946 EXPORT_SYMBOL(seq_hlist_next_rcu);
949 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
950 * @head: pointer to percpu array of struct hlist_heads
951 * @cpu: pointer to cpu "cursor"
952 * @pos: start position of sequence
954 * Called at seq_file->op->start().
957 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
959 struct hlist_node *node;
961 for_each_possible_cpu(*cpu) {
962 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
969 EXPORT_SYMBOL(seq_hlist_start_percpu);
972 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
973 * @v: pointer to current hlist_node
974 * @head: pointer to percpu array of struct hlist_heads
975 * @cpu: pointer to cpu "cursor"
976 * @pos: start position of sequence
978 * Called at seq_file->op->next().
981 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
982 int *cpu, loff_t *pos)
984 struct hlist_node *node = v;
991 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
992 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
993 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
995 if (!hlist_empty(bucket))
996 return bucket->first;
1000 EXPORT_SYMBOL(seq_hlist_next_percpu);