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 <linux/uaccess.h>
21 static void seq_set_overflow(struct seq_file *m)
26 static void *seq_buf_alloc(unsigned long size)
28 return kvmalloc(size, GFP_KERNEL);
32 * seq_open - initialize sequential file
33 * @file: file we initialize
34 * @op: method table describing the sequence
36 * seq_open() sets @file, associating it with a sequence described
37 * by @op. @op->start() sets the iterator up and returns the first
38 * element of sequence. @op->stop() shuts it down. @op->next()
39 * returns the next element of sequence. @op->show() prints element
40 * into the buffer. In case of error ->start() and ->next() return
41 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
42 * returns 0 in case of success and negative number in case of error.
43 * Returning SEQ_SKIP means "discard this element and move on".
44 * Note: seq_open() will allocate a struct seq_file and store its
45 * pointer in @file->private_data. This pointer should not be modified.
47 int seq_open(struct file *file, const struct seq_operations *op)
51 WARN_ON(file->private_data);
53 p = kzalloc(sizeof(*p), GFP_KERNEL);
57 file->private_data = p;
62 // No refcounting: the lifetime of 'p' is constrained
63 // to the lifetime of the file.
67 * Wrappers around seq_open(e.g. swaps_open) need to be
68 * aware of this. If they set f_version themselves, they
69 * should call seq_open first and then set f_version.
74 * seq_files support lseek() and pread(). They do not implement
75 * write() at all, but we clear FMODE_PWRITE here for historical
78 * If a client of seq_files a) implements file.write() and b) wishes to
79 * support pwrite() then that client will need to implement its own
80 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
82 file->f_mode &= ~FMODE_PWRITE;
85 EXPORT_SYMBOL(seq_open);
87 static int traverse(struct seq_file *m, loff_t offset)
89 loff_t pos = 0, index;
95 m->count = m->from = 0;
101 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
105 p = m->op->start(m, &index);
110 error = m->op->show(m, p);
113 if (unlikely(error)) {
117 if (seq_has_overflowed(m))
119 if (pos + m->count > offset) {
120 m->from = offset - pos;
132 p = m->op->next(m, p, &index);
142 m->buf = seq_buf_alloc(m->size <<= 1);
143 return !m->buf ? -ENOMEM : -EAGAIN;
147 * seq_read - ->read() method for sequential files.
148 * @file: the file to read from
149 * @buf: the buffer to read to
150 * @size: the maximum number of bytes to read
151 * @ppos: the current position in the file
153 * Ready-made ->f_op->read()
155 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
157 struct seq_file *m = file->private_data;
164 mutex_lock(&m->lock);
167 * seq_file->op->..m_start/m_stop/m_next may do special actions
168 * or optimisations based on the file->f_version, so we want to
169 * pass the file->f_version to those methods.
171 * seq_file->version is just copy of f_version, and seq_file
172 * methods can treat it simply as file version.
173 * It is copied in first and copied out after all operations.
174 * It is convenient to have it as part of structure to avoid the
175 * need of passing another argument to all the seq_file methods.
177 m->version = file->f_version;
180 * if request is to read from zero offset, reset iterator to first
181 * record as it might have been already advanced by previous requests
186 /* Don't assume *ppos is where we left it */
187 if (unlikely(*ppos != m->read_pos)) {
188 while ((err = traverse(m, *ppos)) == -EAGAIN)
191 /* With prejudice... */
202 /* grab buffer if we didn't have one */
204 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
208 /* if not empty - flush it first */
210 n = min(m->count, size);
211 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)
248 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_has_overflowed(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.
378 * Use seq_has_overflowed() to check for errors.
380 void seq_escape(struct seq_file *m, const char *s, const char *esc)
383 size_t size = seq_get_buf(m, &buf);
386 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
387 seq_commit(m, ret < size ? ret : -1);
389 EXPORT_SYMBOL(seq_escape);
391 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
395 if (m->count < m->size) {
396 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
397 if (m->count + len < m->size) {
404 EXPORT_SYMBOL(seq_vprintf);
406 void seq_printf(struct seq_file *m, const char *f, ...)
411 seq_vprintf(m, f, args);
414 EXPORT_SYMBOL(seq_printf);
417 * mangle_path - mangle and copy path to buffer beginning
419 * @p: beginning of path in above buffer
420 * @esc: set of characters that need escaping
422 * Copy the path from @p to @s, replacing each occurrence of character from
423 * @esc with usual octal escape.
424 * Returns pointer past last written character in @s, or NULL in case of
427 char *mangle_path(char *s, const char *p, const char *esc)
433 } else if (!strchr(esc, c)) {
435 } else if (s + 4 > p) {
439 *s++ = '0' + ((c & 0300) >> 6);
440 *s++ = '0' + ((c & 070) >> 3);
441 *s++ = '0' + (c & 07);
446 EXPORT_SYMBOL(mangle_path);
449 * seq_path - seq_file interface to print a pathname
450 * @m: the seq_file handle
451 * @path: the struct path to print
452 * @esc: set of characters to escape in the output
454 * return the absolute path of 'path', as represented by the
455 * dentry / mnt pair in the path parameter.
457 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
460 size_t size = seq_get_buf(m, &buf);
464 char *p = d_path(path, buf, size);
466 char *end = mangle_path(buf, p, esc);
475 EXPORT_SYMBOL(seq_path);
478 * seq_file_path - seq_file interface to print a pathname of a file
479 * @m: the seq_file handle
480 * @file: the struct file to print
481 * @esc: set of characters to escape in the output
483 * return the absolute path to the file.
485 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
487 return seq_path(m, &file->f_path, esc);
489 EXPORT_SYMBOL(seq_file_path);
492 * Same as seq_path, but relative to supplied root.
494 int seq_path_root(struct seq_file *m, const struct path *path,
495 const struct path *root, const char *esc)
498 size_t size = seq_get_buf(m, &buf);
499 int res = -ENAMETOOLONG;
504 p = __d_path(path, root, buf, size);
509 char *end = mangle_path(buf, p, esc);
518 return res < 0 && res != -ENAMETOOLONG ? res : 0;
522 * returns the path of the 'dentry' from the root of its filesystem.
524 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
527 size_t size = seq_get_buf(m, &buf);
531 char *p = dentry_path(dentry, buf, size);
533 char *end = mangle_path(buf, p, esc);
542 EXPORT_SYMBOL(seq_dentry);
544 static void *single_start(struct seq_file *p, loff_t *pos)
546 return NULL + (*pos == 0);
549 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
555 static void single_stop(struct seq_file *p, void *v)
559 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
562 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
566 op->start = single_start;
567 op->next = single_next;
568 op->stop = single_stop;
570 res = seq_open(file, op);
572 ((struct seq_file *)file->private_data)->private = data;
578 EXPORT_SYMBOL(single_open);
580 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
581 void *data, size_t size)
583 char *buf = seq_buf_alloc(size);
587 ret = single_open(file, show, data);
592 ((struct seq_file *)file->private_data)->buf = buf;
593 ((struct seq_file *)file->private_data)->size = size;
596 EXPORT_SYMBOL(single_open_size);
598 int single_release(struct inode *inode, struct file *file)
600 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
601 int res = seq_release(inode, file);
605 EXPORT_SYMBOL(single_release);
607 int seq_release_private(struct inode *inode, struct file *file)
609 struct seq_file *seq = file->private_data;
613 return seq_release(inode, file);
615 EXPORT_SYMBOL(seq_release_private);
617 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
622 struct seq_file *seq;
624 private = kzalloc(psize, GFP_KERNEL);
628 rc = seq_open(f, ops);
632 seq = f->private_data;
633 seq->private = private;
641 EXPORT_SYMBOL(__seq_open_private);
643 int seq_open_private(struct file *filp, const struct seq_operations *ops,
646 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
648 EXPORT_SYMBOL(seq_open_private);
650 void seq_putc(struct seq_file *m, char c)
652 if (m->count >= m->size)
655 m->buf[m->count++] = c;
657 EXPORT_SYMBOL(seq_putc);
659 void seq_puts(struct seq_file *m, const char *s)
663 if (m->count + len >= m->size) {
667 memcpy(m->buf + m->count, s, len);
670 EXPORT_SYMBOL(seq_puts);
673 * A helper routine for putting decimal numbers without rich format of printf().
674 * only 'unsigned long long' is supported.
675 * This routine will put strlen(delimiter) + number into seq_file.
676 * This routine is very quick when you show lots of numbers.
677 * In usual cases, it will be better to use seq_printf(). It's easier to read.
679 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
680 unsigned long long num)
684 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
687 len = strlen(delimiter);
688 if (m->count + len >= m->size)
691 memcpy(m->buf + m->count, delimiter, len);
694 if (m->count + 1 >= m->size)
698 m->buf[m->count++] = num + '0';
702 len = num_to_str(m->buf + m->count, m->size - m->count, num);
712 EXPORT_SYMBOL(seq_put_decimal_ull);
714 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
718 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
721 len = strlen(delimiter);
722 if (m->count + len >= m->size)
725 memcpy(m->buf + m->count, delimiter, len);
728 if (m->count + 2 >= m->size)
732 m->buf[m->count++] = '-';
737 m->buf[m->count++] = num + '0';
741 len = num_to_str(m->buf + m->count, m->size - m->count, num);
751 EXPORT_SYMBOL(seq_put_decimal_ll);
754 * seq_write - write arbitrary data to buffer
755 * @seq: seq_file identifying the buffer to which data should be written
756 * @data: data address
757 * @len: number of bytes
759 * Return 0 on success, non-zero otherwise.
761 int seq_write(struct seq_file *seq, const void *data, size_t len)
763 if (seq->count + len < seq->size) {
764 memcpy(seq->buf + seq->count, data, len);
768 seq_set_overflow(seq);
771 EXPORT_SYMBOL(seq_write);
774 * seq_pad - write padding spaces to buffer
775 * @m: seq_file identifying the buffer to which data should be written
776 * @c: the byte to append after padding if non-zero
778 void seq_pad(struct seq_file *m, char c)
780 int size = m->pad_until - m->count;
782 seq_printf(m, "%*s", size, "");
786 EXPORT_SYMBOL(seq_pad);
788 /* A complete analogue of print_hex_dump() */
789 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
790 int rowsize, int groupsize, const void *buf, size_t len,
794 int i, linelen, remaining = len;
799 if (rowsize != 16 && rowsize != 32)
802 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
803 linelen = min(remaining, rowsize);
804 remaining -= rowsize;
806 switch (prefix_type) {
807 case DUMP_PREFIX_ADDRESS:
808 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
810 case DUMP_PREFIX_OFFSET:
811 seq_printf(m, "%s%.8x: ", prefix_str, i);
814 seq_printf(m, "%s", prefix_str);
818 size = seq_get_buf(m, &buffer);
819 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
820 buffer, size, ascii);
821 seq_commit(m, ret < size ? ret : -1);
826 EXPORT_SYMBOL(seq_hex_dump);
828 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
830 struct list_head *lh;
832 list_for_each(lh, head)
838 EXPORT_SYMBOL(seq_list_start);
840 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
845 return seq_list_start(head, pos - 1);
847 EXPORT_SYMBOL(seq_list_start_head);
849 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
851 struct list_head *lh;
853 lh = ((struct list_head *)v)->next;
855 return lh == head ? NULL : lh;
857 EXPORT_SYMBOL(seq_list_next);
860 * seq_hlist_start - start an iteration of a hlist
861 * @head: the head of the hlist
862 * @pos: the start position of the sequence
864 * Called at seq_file->op->start().
866 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
868 struct hlist_node *node;
870 hlist_for_each(node, head)
875 EXPORT_SYMBOL(seq_hlist_start);
878 * seq_hlist_start_head - start an iteration of a hlist
879 * @head: the head of the hlist
880 * @pos: the start position of the sequence
882 * Called at seq_file->op->start(). Call this function if you want to
883 * print a header at the top of the output.
885 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
888 return SEQ_START_TOKEN;
890 return seq_hlist_start(head, pos - 1);
892 EXPORT_SYMBOL(seq_hlist_start_head);
895 * seq_hlist_next - move to the next position of the hlist
896 * @v: the current iterator
897 * @head: the head of the hlist
898 * @ppos: the current position
900 * Called at seq_file->op->next().
902 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
905 struct hlist_node *node = v;
908 if (v == SEQ_START_TOKEN)
913 EXPORT_SYMBOL(seq_hlist_next);
916 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
917 * @head: the head of the hlist
918 * @pos: the start position of the sequence
920 * Called at seq_file->op->start().
922 * This list-traversal primitive may safely run concurrently with
923 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
924 * as long as the traversal is guarded by rcu_read_lock().
926 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
929 struct hlist_node *node;
931 __hlist_for_each_rcu(node, head)
936 EXPORT_SYMBOL(seq_hlist_start_rcu);
939 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
940 * @head: the head of the hlist
941 * @pos: the start position of the sequence
943 * Called at seq_file->op->start(). Call this function if you want to
944 * print a header at the top of the output.
946 * This list-traversal primitive may safely run concurrently with
947 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
948 * as long as the traversal is guarded by rcu_read_lock().
950 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
954 return SEQ_START_TOKEN;
956 return seq_hlist_start_rcu(head, pos - 1);
958 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
961 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
962 * @v: the current iterator
963 * @head: the head of the hlist
964 * @ppos: the current position
966 * Called at seq_file->op->next().
968 * This list-traversal primitive may safely run concurrently with
969 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
970 * as long as the traversal is guarded by rcu_read_lock().
972 struct hlist_node *seq_hlist_next_rcu(void *v,
973 struct hlist_head *head,
976 struct hlist_node *node = v;
979 if (v == SEQ_START_TOKEN)
980 return rcu_dereference(head->first);
982 return rcu_dereference(node->next);
984 EXPORT_SYMBOL(seq_hlist_next_rcu);
987 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
988 * @head: pointer to percpu array of struct hlist_heads
989 * @cpu: pointer to cpu "cursor"
990 * @pos: start position of sequence
992 * Called at seq_file->op->start().
995 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
997 struct hlist_node *node;
999 for_each_possible_cpu(*cpu) {
1000 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1007 EXPORT_SYMBOL(seq_hlist_start_percpu);
1010 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1011 * @v: pointer to current hlist_node
1012 * @head: pointer to percpu array of struct hlist_heads
1013 * @cpu: pointer to cpu "cursor"
1014 * @pos: start position of sequence
1016 * Called at seq_file->op->next().
1019 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1020 int *cpu, loff_t *pos)
1022 struct hlist_node *node = v;
1029 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1030 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1031 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1033 if (!hlist_empty(bucket))
1034 return bucket->first;
1038 EXPORT_SYMBOL(seq_hlist_next_percpu);