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>
17 #include <linux/pagemap.h>
19 #include <asm/uaccess.h>
22 static void seq_set_overflow(struct seq_file *m)
27 static void *seq_buf_alloc(unsigned long size)
30 gfp_t gfp = GFP_KERNEL;
32 if (unlikely(size > MAX_RW_COUNT))
36 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
37 * it's better to fall back to vmalloc() than to kill things. For small
38 * allocations, just use GFP_KERNEL which will oom kill, thus no need
39 * for vmalloc fallback.
42 gfp |= __GFP_NORETRY | __GFP_NOWARN;
43 buf = kmalloc(size, gfp);
44 if (!buf && size > PAGE_SIZE)
50 * seq_open - initialize sequential file
51 * @file: file we initialize
52 * @op: method table describing the sequence
54 * seq_open() sets @file, associating it with a sequence described
55 * by @op. @op->start() sets the iterator up and returns the first
56 * element of sequence. @op->stop() shuts it down. @op->next()
57 * returns the next element of sequence. @op->show() prints element
58 * into the buffer. In case of error ->start() and ->next() return
59 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
60 * returns 0 in case of success and negative number in case of error.
61 * Returning SEQ_SKIP means "discard this element and move on".
62 * Note: seq_open() will allocate a struct seq_file and store its
63 * pointer in @file->private_data. This pointer should not be modified.
65 int seq_open(struct file *file, const struct seq_operations *op)
69 WARN_ON(file->private_data);
71 p = kzalloc(sizeof(*p), GFP_KERNEL);
75 file->private_data = p;
80 // No refcounting: the lifetime of 'p' is constrained
81 // to the lifetime of the file.
85 * Wrappers around seq_open(e.g. swaps_open) need to be
86 * aware of this. If they set f_version themselves, they
87 * should call seq_open first and then set f_version.
92 * seq_files support lseek() and pread(). They do not implement
93 * write() at all, but we clear FMODE_PWRITE here for historical
96 * If a client of seq_files a) implements file.write() and b) wishes to
97 * support pwrite() then that client will need to implement its own
98 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
100 file->f_mode &= ~FMODE_PWRITE;
103 EXPORT_SYMBOL(seq_open);
105 static int traverse(struct seq_file *m, loff_t offset)
107 loff_t pos = 0, index;
113 m->count = m->from = 0;
119 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
123 p = m->op->start(m, &index);
128 error = m->op->show(m, p);
131 if (unlikely(error)) {
135 if (seq_has_overflowed(m))
137 if (pos + m->count > offset) {
138 m->from = offset - pos;
150 p = m->op->next(m, p, &index);
160 m->buf = seq_buf_alloc(m->size <<= 1);
161 return !m->buf ? -ENOMEM : -EAGAIN;
165 * seq_read - ->read() method for sequential files.
166 * @file: the file to read from
167 * @buf: the buffer to read to
168 * @size: the maximum number of bytes to read
169 * @ppos: the current position in the file
171 * Ready-made ->f_op->read()
173 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
175 struct seq_file *m = file->private_data;
182 mutex_lock(&m->lock);
185 * seq_file->op->..m_start/m_stop/m_next may do special actions
186 * or optimisations based on the file->f_version, so we want to
187 * pass the file->f_version to those methods.
189 * seq_file->version is just copy of f_version, and seq_file
190 * methods can treat it simply as file version.
191 * It is copied in first and copied out after all operations.
192 * It is convenient to have it as part of structure to avoid the
193 * need of passing another argument to all the seq_file methods.
195 m->version = file->f_version;
197 /* Don't assume *ppos is where we left it */
198 if (unlikely(*ppos != m->read_pos)) {
199 while ((err = traverse(m, *ppos)) == -EAGAIN)
202 /* With prejudice... */
213 /* grab buffer if we didn't have one */
215 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
219 /* if not empty - flush it first */
221 n = min(m->count, size);
222 err = copy_to_user(buf, m->buf + m->from, n);
237 /* we need at least one record in buffer */
239 p = m->op->start(m, &pos);
244 err = m->op->show(m, p);
249 if (unlikely(!m->count)) {
250 p = m->op->next(m, p, &pos);
254 if (m->count < m->size)
259 m->buf = seq_buf_alloc(m->size <<= 1);
264 p = m->op->start(m, &pos);
270 /* they want more? let's try to get some more */
271 while (m->count < size) {
272 size_t offs = m->count;
274 p = m->op->next(m, p, &next);
275 if (!p || IS_ERR(p)) {
279 err = m->op->show(m, p);
280 if (seq_has_overflowed(m) || err) {
282 if (likely(err <= 0))
288 n = min(m->count, size);
289 err = copy_to_user(buf, m->buf, n);
304 m->read_pos += copied;
306 file->f_version = m->version;
307 mutex_unlock(&m->lock);
316 EXPORT_SYMBOL(seq_read);
319 * seq_lseek - ->llseek() method for sequential files.
320 * @file: the file in question
321 * @offset: new position
322 * @whence: 0 for absolute, 1 for relative position
324 * Ready-made ->f_op->llseek()
326 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
328 struct seq_file *m = file->private_data;
329 loff_t retval = -EINVAL;
331 mutex_lock(&m->lock);
332 m->version = file->f_version;
335 offset += file->f_pos;
340 if (offset != m->read_pos) {
341 while ((retval = traverse(m, offset)) == -EAGAIN)
344 /* with extreme prejudice... */
351 m->read_pos = offset;
352 retval = file->f_pos = offset;
355 file->f_pos = offset;
358 file->f_version = m->version;
359 mutex_unlock(&m->lock);
362 EXPORT_SYMBOL(seq_lseek);
365 * seq_release - free the structures associated with sequential file.
366 * @file: file in question
369 * Frees the structures associated with sequential file; can be used
370 * as ->f_op->release() if you don't have private data to destroy.
372 int seq_release(struct inode *inode, struct file *file)
374 struct seq_file *m = file->private_data;
379 EXPORT_SYMBOL(seq_release);
382 * seq_escape - print string into buffer, escaping some characters
385 * @esc: set of characters that need escaping
387 * Puts string into buffer, replacing each occurrence of character from
388 * @esc with usual octal escape.
389 * Use seq_has_overflowed() to check for errors.
391 void seq_escape(struct seq_file *m, const char *s, const char *esc)
394 size_t size = seq_get_buf(m, &buf);
397 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
398 seq_commit(m, ret < size ? ret : -1);
400 EXPORT_SYMBOL(seq_escape);
402 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
406 if (m->count < m->size) {
407 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
408 if (m->count + len < m->size) {
415 EXPORT_SYMBOL(seq_vprintf);
417 void seq_printf(struct seq_file *m, const char *f, ...)
422 seq_vprintf(m, f, args);
425 EXPORT_SYMBOL(seq_printf);
428 * mangle_path - mangle and copy path to buffer beginning
430 * @p: beginning of path in above buffer
431 * @esc: set of characters that need escaping
433 * Copy the path from @p to @s, replacing each occurrence of character from
434 * @esc with usual octal escape.
435 * Returns pointer past last written character in @s, or NULL in case of
438 char *mangle_path(char *s, const char *p, const char *esc)
444 } else if (!strchr(esc, c)) {
446 } else if (s + 4 > p) {
450 *s++ = '0' + ((c & 0300) >> 6);
451 *s++ = '0' + ((c & 070) >> 3);
452 *s++ = '0' + (c & 07);
457 EXPORT_SYMBOL(mangle_path);
460 * seq_path - seq_file interface to print a pathname
461 * @m: the seq_file handle
462 * @path: the struct path to print
463 * @esc: set of characters to escape in the output
465 * return the absolute path of 'path', as represented by the
466 * dentry / mnt pair in the path parameter.
468 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
471 size_t size = seq_get_buf(m, &buf);
475 char *p = d_path(path, buf, size);
477 char *end = mangle_path(buf, p, esc);
486 EXPORT_SYMBOL(seq_path);
489 * seq_file_path - seq_file interface to print a pathname of a file
490 * @m: the seq_file handle
491 * @file: the struct file to print
492 * @esc: set of characters to escape in the output
494 * return the absolute path to the file.
496 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
498 return seq_path(m, &file->f_path, esc);
500 EXPORT_SYMBOL(seq_file_path);
503 * Same as seq_path, but relative to supplied root.
505 int seq_path_root(struct seq_file *m, const struct path *path,
506 const struct path *root, const char *esc)
509 size_t size = seq_get_buf(m, &buf);
510 int res = -ENAMETOOLONG;
515 p = __d_path(path, root, buf, size);
520 char *end = mangle_path(buf, p, esc);
529 return res < 0 && res != -ENAMETOOLONG ? res : 0;
533 * returns the path of the 'dentry' from the root of its filesystem.
535 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
538 size_t size = seq_get_buf(m, &buf);
542 char *p = dentry_path(dentry, buf, size);
544 char *end = mangle_path(buf, p, esc);
553 EXPORT_SYMBOL(seq_dentry);
555 static void *single_start(struct seq_file *p, loff_t *pos)
557 return NULL + (*pos == 0);
560 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
566 static void single_stop(struct seq_file *p, void *v)
570 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
573 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
577 op->start = single_start;
578 op->next = single_next;
579 op->stop = single_stop;
581 res = seq_open(file, op);
583 ((struct seq_file *)file->private_data)->private = data;
589 EXPORT_SYMBOL(single_open);
591 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
592 void *data, size_t size)
594 char *buf = seq_buf_alloc(size);
598 ret = single_open(file, show, data);
603 ((struct seq_file *)file->private_data)->buf = buf;
604 ((struct seq_file *)file->private_data)->size = size;
607 EXPORT_SYMBOL(single_open_size);
609 int single_release(struct inode *inode, struct file *file)
611 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
612 int res = seq_release(inode, file);
616 EXPORT_SYMBOL(single_release);
618 int seq_release_private(struct inode *inode, struct file *file)
620 struct seq_file *seq = file->private_data;
624 return seq_release(inode, file);
626 EXPORT_SYMBOL(seq_release_private);
628 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
633 struct seq_file *seq;
635 private = kzalloc(psize, GFP_KERNEL);
639 rc = seq_open(f, ops);
643 seq = f->private_data;
644 seq->private = private;
652 EXPORT_SYMBOL(__seq_open_private);
654 int seq_open_private(struct file *filp, const struct seq_operations *ops,
657 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
659 EXPORT_SYMBOL(seq_open_private);
661 void seq_putc(struct seq_file *m, char c)
663 if (m->count >= m->size)
666 m->buf[m->count++] = c;
668 EXPORT_SYMBOL(seq_putc);
670 void seq_puts(struct seq_file *m, const char *s)
674 if (m->count + len >= m->size) {
678 memcpy(m->buf + m->count, s, len);
681 EXPORT_SYMBOL(seq_puts);
684 * A helper routine for putting decimal numbers without rich format of printf().
685 * only 'unsigned long long' is supported.
686 * This routine will put one byte delimiter + number into seq_file.
687 * This routine is very quick when you show lots of numbers.
688 * In usual cases, it will be better to use seq_printf(). It's easier to read.
690 void seq_put_decimal_ull(struct seq_file *m, char delimiter,
691 unsigned long long num)
695 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
699 m->buf[m->count++] = delimiter;
702 m->buf[m->count++] = num + '0';
706 len = num_to_str(m->buf + m->count, m->size - m->count, num);
715 EXPORT_SYMBOL(seq_put_decimal_ull);
717 void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num)
720 if (m->count + 3 >= m->size) {
725 m->buf[m->count++] = delimiter;
729 seq_put_decimal_ull(m, delimiter, num);
731 EXPORT_SYMBOL(seq_put_decimal_ll);
734 * seq_write - write arbitrary data to buffer
735 * @seq: seq_file identifying the buffer to which data should be written
736 * @data: data address
737 * @len: number of bytes
739 * Return 0 on success, non-zero otherwise.
741 int seq_write(struct seq_file *seq, const void *data, size_t len)
743 if (seq->count + len < seq->size) {
744 memcpy(seq->buf + seq->count, data, len);
748 seq_set_overflow(seq);
751 EXPORT_SYMBOL(seq_write);
754 * seq_pad - write padding spaces to buffer
755 * @m: seq_file identifying the buffer to which data should be written
756 * @c: the byte to append after padding if non-zero
758 void seq_pad(struct seq_file *m, char c)
760 int size = m->pad_until - m->count;
762 seq_printf(m, "%*s", size, "");
766 EXPORT_SYMBOL(seq_pad);
768 /* A complete analogue of print_hex_dump() */
769 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
770 int rowsize, int groupsize, const void *buf, size_t len,
774 int i, linelen, remaining = len;
779 if (rowsize != 16 && rowsize != 32)
782 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
783 linelen = min(remaining, rowsize);
784 remaining -= rowsize;
786 switch (prefix_type) {
787 case DUMP_PREFIX_ADDRESS:
788 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
790 case DUMP_PREFIX_OFFSET:
791 seq_printf(m, "%s%.8x: ", prefix_str, i);
794 seq_printf(m, "%s", prefix_str);
798 size = seq_get_buf(m, &buffer);
799 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
800 buffer, size, ascii);
801 seq_commit(m, ret < size ? ret : -1);
806 EXPORT_SYMBOL(seq_hex_dump);
808 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
810 struct list_head *lh;
812 list_for_each(lh, head)
818 EXPORT_SYMBOL(seq_list_start);
820 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
825 return seq_list_start(head, pos - 1);
827 EXPORT_SYMBOL(seq_list_start_head);
829 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
831 struct list_head *lh;
833 lh = ((struct list_head *)v)->next;
835 return lh == head ? NULL : lh;
837 EXPORT_SYMBOL(seq_list_next);
840 * seq_hlist_start - 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().
846 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
848 struct hlist_node *node;
850 hlist_for_each(node, head)
855 EXPORT_SYMBOL(seq_hlist_start);
858 * seq_hlist_start_head - start an iteration of a hlist
859 * @head: the head of the hlist
860 * @pos: the start position of the sequence
862 * Called at seq_file->op->start(). Call this function if you want to
863 * print a header at the top of the output.
865 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
868 return SEQ_START_TOKEN;
870 return seq_hlist_start(head, pos - 1);
872 EXPORT_SYMBOL(seq_hlist_start_head);
875 * seq_hlist_next - move to the next position of the hlist
876 * @v: the current iterator
877 * @head: the head of the hlist
878 * @ppos: the current position
880 * Called at seq_file->op->next().
882 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
885 struct hlist_node *node = v;
888 if (v == SEQ_START_TOKEN)
893 EXPORT_SYMBOL(seq_hlist_next);
896 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
897 * @head: the head of the hlist
898 * @pos: the start position of the sequence
900 * Called at seq_file->op->start().
902 * This list-traversal primitive may safely run concurrently with
903 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
904 * as long as the traversal is guarded by rcu_read_lock().
906 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
909 struct hlist_node *node;
911 __hlist_for_each_rcu(node, head)
916 EXPORT_SYMBOL(seq_hlist_start_rcu);
919 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
920 * @head: the head of the hlist
921 * @pos: the start position of the sequence
923 * Called at seq_file->op->start(). Call this function if you want to
924 * print a header at the top of the output.
926 * This list-traversal primitive may safely run concurrently with
927 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
928 * as long as the traversal is guarded by rcu_read_lock().
930 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
934 return SEQ_START_TOKEN;
936 return seq_hlist_start_rcu(head, pos - 1);
938 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
941 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
942 * @v: the current iterator
943 * @head: the head of the hlist
944 * @ppos: the current position
946 * Called at seq_file->op->next().
948 * This list-traversal primitive may safely run concurrently with
949 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
950 * as long as the traversal is guarded by rcu_read_lock().
952 struct hlist_node *seq_hlist_next_rcu(void *v,
953 struct hlist_head *head,
956 struct hlist_node *node = v;
959 if (v == SEQ_START_TOKEN)
960 return rcu_dereference(head->first);
962 return rcu_dereference(node->next);
964 EXPORT_SYMBOL(seq_hlist_next_rcu);
967 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
968 * @head: pointer to percpu array of struct hlist_heads
969 * @cpu: pointer to cpu "cursor"
970 * @pos: start position of sequence
972 * Called at seq_file->op->start().
975 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
977 struct hlist_node *node;
979 for_each_possible_cpu(*cpu) {
980 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
987 EXPORT_SYMBOL(seq_hlist_start_percpu);
990 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
991 * @v: pointer to current hlist_node
992 * @head: pointer to percpu array of struct hlist_heads
993 * @cpu: pointer to cpu "cursor"
994 * @pos: start position of sequence
996 * Called at seq_file->op->next().
999 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1000 int *cpu, loff_t *pos)
1002 struct hlist_node *node = v;
1009 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1010 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1011 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1013 if (!hlist_empty(bucket))
1014 return bucket->first;
1018 EXPORT_SYMBOL(seq_hlist_next_percpu);