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>
15 #include <linux/printk.h>
16 #include <linux/string_helpers.h>
18 #include <asm/uaccess.h>
21 static void seq_set_overflow(struct seq_file *m)
26 static void *seq_buf_alloc(unsigned long size)
31 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
32 * it's better to fall back to vmalloc() than to kill things.
34 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
35 if (!buf && size > PAGE_SIZE)
41 * seq_open - initialize sequential file
42 * @file: file we initialize
43 * @op: method table describing the sequence
45 * seq_open() sets @file, associating it with a sequence described
46 * by @op. @op->start() sets the iterator up and returns the first
47 * element of sequence. @op->stop() shuts it down. @op->next()
48 * returns the next element of sequence. @op->show() prints element
49 * into the buffer. In case of error ->start() and ->next() return
50 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
51 * returns 0 in case of success and negative number in case of error.
52 * Returning SEQ_SKIP means "discard this element and move on".
53 * Note: seq_open() will allocate a struct seq_file and store its
54 * pointer in @file->private_data. This pointer should not be modified.
56 int seq_open(struct file *file, const struct seq_operations *op)
60 WARN_ON(file->private_data);
62 p = kzalloc(sizeof(*p), GFP_KERNEL);
66 file->private_data = p;
71 p->user_ns = file->f_cred->user_ns;
75 * Wrappers around seq_open(e.g. swaps_open) need to be
76 * aware of this. If they set f_version themselves, they
77 * should call seq_open first and then set f_version.
82 * seq_files support lseek() and pread(). They do not implement
83 * write() at all, but we clear FMODE_PWRITE here for historical
86 * If a client of seq_files a) implements file.write() and b) wishes to
87 * support pwrite() then that client will need to implement its own
88 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
90 file->f_mode &= ~FMODE_PWRITE;
93 EXPORT_SYMBOL(seq_open);
95 static int traverse(struct seq_file *m, loff_t offset)
97 loff_t pos = 0, index;
103 m->count = m->from = 0;
109 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
113 p = m->op->start(m, &index);
118 error = m->op->show(m, p);
121 if (unlikely(error)) {
125 if (seq_has_overflowed(m))
127 if (pos + m->count > offset) {
128 m->from = offset - pos;
140 p = m->op->next(m, p, &index);
150 m->buf = seq_buf_alloc(m->size <<= 1);
151 return !m->buf ? -ENOMEM : -EAGAIN;
155 * seq_read - ->read() method for sequential files.
156 * @file: the file to read from
157 * @buf: the buffer to read to
158 * @size: the maximum number of bytes to read
159 * @ppos: the current position in the file
161 * Ready-made ->f_op->read()
163 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
165 struct seq_file *m = file->private_data;
172 mutex_lock(&m->lock);
175 * seq_file->op->..m_start/m_stop/m_next may do special actions
176 * or optimisations based on the file->f_version, so we want to
177 * pass the file->f_version to those methods.
179 * seq_file->version is just copy of f_version, and seq_file
180 * methods can treat it simply as file version.
181 * It is copied in first and copied out after all operations.
182 * It is convenient to have it as part of structure to avoid the
183 * need of passing another argument to all the seq_file methods.
185 m->version = file->f_version;
187 /* Don't assume *ppos is where we left it */
188 if (unlikely(*ppos != m->read_pos)) {
189 while ((err = traverse(m, *ppos)) == -EAGAIN)
192 /* With prejudice... */
203 /* grab buffer if we didn't have one */
205 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
209 /* if not empty - flush it first */
211 n = min(m->count, size);
212 err = copy_to_user(buf, m->buf + m->from, n);
225 /* we need at least one record in buffer */
227 p = m->op->start(m, &pos);
232 err = m->op->show(m, p);
237 if (unlikely(!m->count)) {
238 p = m->op->next(m, p, &pos);
242 if (m->count < m->size)
247 m->buf = seq_buf_alloc(m->size <<= 1);
252 p = m->op->start(m, &pos);
258 /* they want more? let's try to get some more */
259 while (m->count < size) {
260 size_t offs = m->count;
262 p = m->op->next(m, p, &next);
263 if (!p || IS_ERR(p)) {
267 err = m->op->show(m, p);
268 if (seq_has_overflowed(m) || err) {
270 if (likely(err <= 0))
276 n = min(m->count, size);
277 err = copy_to_user(buf, m->buf, n);
292 m->read_pos += copied;
294 file->f_version = m->version;
295 mutex_unlock(&m->lock);
304 EXPORT_SYMBOL(seq_read);
307 * seq_lseek - ->llseek() method for sequential files.
308 * @file: the file in question
309 * @offset: new position
310 * @whence: 0 for absolute, 1 for relative position
312 * Ready-made ->f_op->llseek()
314 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
316 struct seq_file *m = file->private_data;
317 loff_t retval = -EINVAL;
319 mutex_lock(&m->lock);
320 m->version = file->f_version;
323 offset += file->f_pos;
328 if (offset != m->read_pos) {
329 while ((retval = traverse(m, offset)) == -EAGAIN)
332 /* with extreme prejudice... */
339 m->read_pos = offset;
340 retval = file->f_pos = offset;
343 file->f_pos = offset;
346 file->f_version = m->version;
347 mutex_unlock(&m->lock);
350 EXPORT_SYMBOL(seq_lseek);
353 * seq_release - free the structures associated with sequential file.
354 * @file: file in question
357 * Frees the structures associated with sequential file; can be used
358 * as ->f_op->release() if you don't have private data to destroy.
360 int seq_release(struct inode *inode, struct file *file)
362 struct seq_file *m = file->private_data;
367 EXPORT_SYMBOL(seq_release);
370 * seq_escape - print string into buffer, escaping some characters
373 * @esc: set of characters that need escaping
375 * Puts string into buffer, replacing each occurrence of character from
376 * @esc with usual octal escape.
377 * Use seq_has_overflowed() to check for errors.
379 void seq_escape(struct seq_file *m, const char *s, const char *esc)
382 size_t size = seq_get_buf(m, &buf);
385 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
386 seq_commit(m, ret < size ? ret : -1);
388 EXPORT_SYMBOL(seq_escape);
390 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
394 if (m->count < m->size) {
395 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
396 if (m->count + len < m->size) {
403 EXPORT_SYMBOL(seq_vprintf);
405 void seq_printf(struct seq_file *m, const char *f, ...)
410 seq_vprintf(m, f, args);
413 EXPORT_SYMBOL(seq_printf);
416 * mangle_path - mangle and copy path to buffer beginning
418 * @p: beginning of path in above buffer
419 * @esc: set of characters that need escaping
421 * Copy the path from @p to @s, replacing each occurrence of character from
422 * @esc with usual octal escape.
423 * Returns pointer past last written character in @s, or NULL in case of
426 char *mangle_path(char *s, const char *p, const char *esc)
432 } else if (!strchr(esc, c)) {
434 } else if (s + 4 > p) {
438 *s++ = '0' + ((c & 0300) >> 6);
439 *s++ = '0' + ((c & 070) >> 3);
440 *s++ = '0' + (c & 07);
445 EXPORT_SYMBOL(mangle_path);
448 * seq_path - seq_file interface to print a pathname
449 * @m: the seq_file handle
450 * @path: the struct path to print
451 * @esc: set of characters to escape in the output
453 * return the absolute path of 'path', as represented by the
454 * dentry / mnt pair in the path parameter.
456 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
459 size_t size = seq_get_buf(m, &buf);
463 char *p = d_path(path, buf, size);
465 char *end = mangle_path(buf, p, esc);
474 EXPORT_SYMBOL(seq_path);
477 * seq_file_path - seq_file interface to print a pathname of a file
478 * @m: the seq_file handle
479 * @file: the struct file to print
480 * @esc: set of characters to escape in the output
482 * return the absolute path to the file.
484 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
486 return seq_path(m, &file->f_path, esc);
488 EXPORT_SYMBOL(seq_file_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);
541 EXPORT_SYMBOL(seq_dentry);
543 static void *single_start(struct seq_file *p, loff_t *pos)
545 return NULL + (*pos == 0);
548 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
554 static void single_stop(struct seq_file *p, void *v)
558 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
561 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
565 op->start = single_start;
566 op->next = single_next;
567 op->stop = single_stop;
569 res = seq_open(file, op);
571 ((struct seq_file *)file->private_data)->private = data;
577 EXPORT_SYMBOL(single_open);
579 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
580 void *data, size_t size)
582 char *buf = seq_buf_alloc(size);
586 ret = single_open(file, show, data);
591 ((struct seq_file *)file->private_data)->buf = buf;
592 ((struct seq_file *)file->private_data)->size = size;
595 EXPORT_SYMBOL(single_open_size);
597 int single_release(struct inode *inode, struct file *file)
599 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
600 int res = seq_release(inode, file);
604 EXPORT_SYMBOL(single_release);
606 int seq_release_private(struct inode *inode, struct file *file)
608 struct seq_file *seq = file->private_data;
612 return seq_release(inode, file);
614 EXPORT_SYMBOL(seq_release_private);
616 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
621 struct seq_file *seq;
623 private = kzalloc(psize, GFP_KERNEL);
627 rc = seq_open(f, ops);
631 seq = f->private_data;
632 seq->private = private;
640 EXPORT_SYMBOL(__seq_open_private);
642 int seq_open_private(struct file *filp, const struct seq_operations *ops,
645 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
647 EXPORT_SYMBOL(seq_open_private);
649 void seq_putc(struct seq_file *m, char c)
651 if (m->count >= m->size)
654 m->buf[m->count++] = c;
656 EXPORT_SYMBOL(seq_putc);
658 void seq_puts(struct seq_file *m, const char *s)
662 if (m->count + len >= m->size) {
666 memcpy(m->buf + m->count, s, len);
669 EXPORT_SYMBOL(seq_puts);
672 * A helper routine for putting decimal numbers without rich format of printf().
673 * only 'unsigned long long' is supported.
674 * This routine will put one byte delimiter + number into seq_file.
675 * This routine is very quick when you show lots of numbers.
676 * In usual cases, it will be better to use seq_printf(). It's easier to read.
678 void seq_put_decimal_ull(struct seq_file *m, char delimiter,
679 unsigned long long num)
683 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
687 m->buf[m->count++] = delimiter;
690 m->buf[m->count++] = num + '0';
694 len = num_to_str(m->buf + m->count, m->size - m->count, num);
703 EXPORT_SYMBOL(seq_put_decimal_ull);
705 void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num)
708 if (m->count + 3 >= m->size) {
713 m->buf[m->count++] = delimiter;
717 seq_put_decimal_ull(m, delimiter, num);
719 EXPORT_SYMBOL(seq_put_decimal_ll);
722 * seq_write - write arbitrary data to buffer
723 * @seq: seq_file identifying the buffer to which data should be written
724 * @data: data address
725 * @len: number of bytes
727 * Return 0 on success, non-zero otherwise.
729 int seq_write(struct seq_file *seq, const void *data, size_t len)
731 if (seq->count + len < seq->size) {
732 memcpy(seq->buf + seq->count, data, len);
736 seq_set_overflow(seq);
739 EXPORT_SYMBOL(seq_write);
742 * seq_pad - write padding spaces to buffer
743 * @m: seq_file identifying the buffer to which data should be written
744 * @c: the byte to append after padding if non-zero
746 void seq_pad(struct seq_file *m, char c)
748 int size = m->pad_until - m->count;
750 seq_printf(m, "%*s", size, "");
754 EXPORT_SYMBOL(seq_pad);
756 /* A complete analogue of print_hex_dump() */
757 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
758 int rowsize, int groupsize, const void *buf, size_t len,
762 int i, linelen, remaining = len;
767 if (rowsize != 16 && rowsize != 32)
770 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
771 linelen = min(remaining, rowsize);
772 remaining -= rowsize;
774 switch (prefix_type) {
775 case DUMP_PREFIX_ADDRESS:
776 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
778 case DUMP_PREFIX_OFFSET:
779 seq_printf(m, "%s%.8x: ", prefix_str, i);
782 seq_printf(m, "%s", prefix_str);
786 size = seq_get_buf(m, &buffer);
787 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
788 buffer, size, ascii);
789 seq_commit(m, ret < size ? ret : -1);
794 EXPORT_SYMBOL(seq_hex_dump);
796 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
798 struct list_head *lh;
800 list_for_each(lh, head)
806 EXPORT_SYMBOL(seq_list_start);
808 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
813 return seq_list_start(head, pos - 1);
815 EXPORT_SYMBOL(seq_list_start_head);
817 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
819 struct list_head *lh;
821 lh = ((struct list_head *)v)->next;
823 return lh == head ? NULL : lh;
825 EXPORT_SYMBOL(seq_list_next);
828 * seq_hlist_start - start an iteration of a hlist
829 * @head: the head of the hlist
830 * @pos: the start position of the sequence
832 * Called at seq_file->op->start().
834 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
836 struct hlist_node *node;
838 hlist_for_each(node, head)
843 EXPORT_SYMBOL(seq_hlist_start);
846 * seq_hlist_start_head - start an iteration of a hlist
847 * @head: the head of the hlist
848 * @pos: the start position of the sequence
850 * Called at seq_file->op->start(). Call this function if you want to
851 * print a header at the top of the output.
853 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
856 return SEQ_START_TOKEN;
858 return seq_hlist_start(head, pos - 1);
860 EXPORT_SYMBOL(seq_hlist_start_head);
863 * seq_hlist_next - move to the next position of the hlist
864 * @v: the current iterator
865 * @head: the head of the hlist
866 * @ppos: the current position
868 * Called at seq_file->op->next().
870 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
873 struct hlist_node *node = v;
876 if (v == SEQ_START_TOKEN)
881 EXPORT_SYMBOL(seq_hlist_next);
884 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
885 * @head: the head of the hlist
886 * @pos: the start position of the sequence
888 * Called at seq_file->op->start().
890 * This list-traversal primitive may safely run concurrently with
891 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
892 * as long as the traversal is guarded by rcu_read_lock().
894 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
897 struct hlist_node *node;
899 __hlist_for_each_rcu(node, head)
904 EXPORT_SYMBOL(seq_hlist_start_rcu);
907 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
908 * @head: the head of the hlist
909 * @pos: the start position of the sequence
911 * Called at seq_file->op->start(). Call this function if you want to
912 * print a header at the top of the output.
914 * This list-traversal primitive may safely run concurrently with
915 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
916 * as long as the traversal is guarded by rcu_read_lock().
918 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
922 return SEQ_START_TOKEN;
924 return seq_hlist_start_rcu(head, pos - 1);
926 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
929 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
930 * @v: the current iterator
931 * @head: the head of the hlist
932 * @ppos: the current position
934 * Called at seq_file->op->next().
936 * This list-traversal primitive may safely run concurrently with
937 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
938 * as long as the traversal is guarded by rcu_read_lock().
940 struct hlist_node *seq_hlist_next_rcu(void *v,
941 struct hlist_head *head,
944 struct hlist_node *node = v;
947 if (v == SEQ_START_TOKEN)
948 return rcu_dereference(head->first);
950 return rcu_dereference(node->next);
952 EXPORT_SYMBOL(seq_hlist_next_rcu);
955 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
956 * @head: pointer to percpu array of struct hlist_heads
957 * @cpu: pointer to cpu "cursor"
958 * @pos: start position of sequence
960 * Called at seq_file->op->start().
963 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
965 struct hlist_node *node;
967 for_each_possible_cpu(*cpu) {
968 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
975 EXPORT_SYMBOL(seq_hlist_start_percpu);
978 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
979 * @v: pointer to current hlist_node
980 * @head: pointer to percpu array of struct hlist_heads
981 * @cpu: pointer to cpu "cursor"
982 * @pos: start position of sequence
984 * Called at seq_file->op->next().
987 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
988 int *cpu, loff_t *pos)
990 struct hlist_node *node = v;
997 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
998 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
999 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1001 if (!hlist_empty(bucket))
1002 return bucket->first;
1006 EXPORT_SYMBOL(seq_hlist_next_percpu);