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>
17 #include <asm/uaccess.h>
20 static void seq_set_overflow(struct seq_file *m)
25 static void *seq_buf_alloc(unsigned long size)
30 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
31 * it's better to fall back to vmalloc() than to kill things.
33 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
34 if (!buf && size > PAGE_SIZE)
40 * seq_open - initialize sequential file
41 * @file: file we initialize
42 * @op: method table describing the sequence
44 * seq_open() sets @file, associating it with a sequence described
45 * by @op. @op->start() sets the iterator up and returns the first
46 * element of sequence. @op->stop() shuts it down. @op->next()
47 * returns the next element of sequence. @op->show() prints element
48 * into the buffer. In case of error ->start() and ->next() return
49 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
50 * returns 0 in case of success and negative number in case of error.
51 * Returning SEQ_SKIP means "discard this element and move on".
52 * Note: seq_open() will allocate a struct seq_file and store its
53 * pointer in @file->private_data. This pointer should not be modified.
55 int seq_open(struct file *file, const struct seq_operations *op)
59 WARN_ON(file->private_data);
61 p = kzalloc(sizeof(*p), GFP_KERNEL);
65 file->private_data = p;
70 p->user_ns = file->f_cred->user_ns;
74 * Wrappers around seq_open(e.g. swaps_open) need to be
75 * aware of this. If they set f_version themselves, they
76 * should call seq_open first and then set f_version.
81 * seq_files support lseek() and pread(). They do not implement
82 * write() at all, but we clear FMODE_PWRITE here for historical
85 * If a client of seq_files a) implements file.write() and b) wishes to
86 * support pwrite() then that client will need to implement its own
87 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
89 file->f_mode &= ~FMODE_PWRITE;
92 EXPORT_SYMBOL(seq_open);
94 static int traverse(struct seq_file *m, loff_t offset)
96 loff_t pos = 0, index;
102 m->count = m->from = 0;
108 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
112 p = m->op->start(m, &index);
117 error = m->op->show(m, p);
120 if (unlikely(error)) {
124 if (seq_has_overflowed(m))
126 if (pos + m->count > offset) {
127 m->from = offset - pos;
139 p = m->op->next(m, p, &index);
149 m->buf = seq_buf_alloc(m->size <<= 1);
150 return !m->buf ? -ENOMEM : -EAGAIN;
154 * seq_read - ->read() method for sequential files.
155 * @file: the file to read from
156 * @buf: the buffer to read to
157 * @size: the maximum number of bytes to read
158 * @ppos: the current position in the file
160 * Ready-made ->f_op->read()
162 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
164 struct seq_file *m = file->private_data;
171 mutex_lock(&m->lock);
174 * seq_file->op->..m_start/m_stop/m_next may do special actions
175 * or optimisations based on the file->f_version, so we want to
176 * pass the file->f_version to those methods.
178 * seq_file->version is just copy of f_version, and seq_file
179 * methods can treat it simply as file version.
180 * It is copied in first and copied out after all operations.
181 * It is convenient to have it as part of structure to avoid the
182 * need of passing another argument to all the seq_file methods.
184 m->version = file->f_version;
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);
224 /* we need at least one record in buffer */
226 p = m->op->start(m, &pos);
231 err = m->op->show(m, p);
236 if (unlikely(!m->count)) {
237 p = m->op->next(m, p, &pos);
241 if (m->count < m->size)
246 m->buf = seq_buf_alloc(m->size <<= 1);
251 p = m->op->start(m, &pos);
257 /* they want more? let's try to get some more */
258 while (m->count < size) {
259 size_t offs = m->count;
261 p = m->op->next(m, p, &next);
262 if (!p || IS_ERR(p)) {
266 err = m->op->show(m, p);
267 if (seq_has_overflowed(m) || err) {
269 if (likely(err <= 0))
275 n = min(m->count, size);
276 err = copy_to_user(buf, m->buf, n);
291 m->read_pos += copied;
293 file->f_version = m->version;
294 mutex_unlock(&m->lock);
303 EXPORT_SYMBOL(seq_read);
306 * seq_lseek - ->llseek() method for sequential files.
307 * @file: the file in question
308 * @offset: new position
309 * @whence: 0 for absolute, 1 for relative position
311 * Ready-made ->f_op->llseek()
313 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
315 struct seq_file *m = file->private_data;
316 loff_t retval = -EINVAL;
318 mutex_lock(&m->lock);
319 m->version = file->f_version;
322 offset += file->f_pos;
327 if (offset != m->read_pos) {
328 while ((retval = traverse(m, offset)) == -EAGAIN)
331 /* with extreme prejudice... */
338 m->read_pos = offset;
339 retval = file->f_pos = offset;
342 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 * seq_file_path - seq_file interface to print a pathname of a file
496 * @m: the seq_file handle
497 * @file: the struct file to print
498 * @esc: set of characters to escape in the output
500 * return the absolute path to the file.
502 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
504 return seq_path(m, &file->f_path, esc);
506 EXPORT_SYMBOL(seq_file_path);
509 * Same as seq_path, but relative to supplied root.
511 int seq_path_root(struct seq_file *m, const struct path *path,
512 const struct path *root, const char *esc)
515 size_t size = seq_get_buf(m, &buf);
516 int res = -ENAMETOOLONG;
521 p = __d_path(path, root, buf, size);
526 char *end = mangle_path(buf, p, esc);
535 return res < 0 && res != -ENAMETOOLONG ? res : 0;
539 * returns the path of the 'dentry' from the root of its filesystem.
541 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
544 size_t size = seq_get_buf(m, &buf);
548 char *p = dentry_path(dentry, buf, size);
550 char *end = mangle_path(buf, p, esc);
559 EXPORT_SYMBOL(seq_dentry);
561 static void *single_start(struct seq_file *p, loff_t *pos)
563 return NULL + (*pos == 0);
566 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
572 static void single_stop(struct seq_file *p, void *v)
576 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
579 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
583 op->start = single_start;
584 op->next = single_next;
585 op->stop = single_stop;
587 res = seq_open(file, op);
589 ((struct seq_file *)file->private_data)->private = data;
595 EXPORT_SYMBOL(single_open);
597 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
598 void *data, size_t size)
600 char *buf = seq_buf_alloc(size);
604 ret = single_open(file, show, data);
609 ((struct seq_file *)file->private_data)->buf = buf;
610 ((struct seq_file *)file->private_data)->size = size;
613 EXPORT_SYMBOL(single_open_size);
615 int single_release(struct inode *inode, struct file *file)
617 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
618 int res = seq_release(inode, file);
622 EXPORT_SYMBOL(single_release);
624 int seq_release_private(struct inode *inode, struct file *file)
626 struct seq_file *seq = file->private_data;
630 return seq_release(inode, file);
632 EXPORT_SYMBOL(seq_release_private);
634 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
639 struct seq_file *seq;
641 private = kzalloc(psize, GFP_KERNEL);
645 rc = seq_open(f, ops);
649 seq = f->private_data;
650 seq->private = private;
658 EXPORT_SYMBOL(__seq_open_private);
660 int seq_open_private(struct file *filp, const struct seq_operations *ops,
663 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
665 EXPORT_SYMBOL(seq_open_private);
667 int seq_putc(struct seq_file *m, char c)
669 if (m->count < m->size) {
670 m->buf[m->count++] = c;
675 EXPORT_SYMBOL(seq_putc);
677 int seq_puts(struct seq_file *m, const char *s)
680 if (m->count + len < m->size) {
681 memcpy(m->buf + m->count, s, len);
688 EXPORT_SYMBOL(seq_puts);
691 * A helper routine for putting decimal numbers without rich format of printf().
692 * only 'unsigned long long' is supported.
693 * This routine will put one byte delimiter + number into seq_file.
694 * This routine is very quick when you show lots of numbers.
695 * In usual cases, it will be better to use seq_printf(). It's easier to read.
697 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
698 unsigned long long num)
702 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
706 m->buf[m->count++] = delimiter;
709 m->buf[m->count++] = num + '0';
713 len = num_to_str(m->buf + m->count, m->size - m->count, num);
722 EXPORT_SYMBOL(seq_put_decimal_ull);
724 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
728 if (m->count + 3 >= m->size) {
733 m->buf[m->count++] = delimiter;
737 return seq_put_decimal_ull(m, delimiter, num);
740 EXPORT_SYMBOL(seq_put_decimal_ll);
743 * seq_write - write arbitrary data to buffer
744 * @seq: seq_file identifying the buffer to which data should be written
745 * @data: data address
746 * @len: number of bytes
748 * Return 0 on success, non-zero otherwise.
750 int seq_write(struct seq_file *seq, const void *data, size_t len)
752 if (seq->count + len < seq->size) {
753 memcpy(seq->buf + seq->count, data, len);
757 seq_set_overflow(seq);
760 EXPORT_SYMBOL(seq_write);
763 * seq_pad - write padding spaces to buffer
764 * @m: seq_file identifying the buffer to which data should be written
765 * @c: the byte to append after padding if non-zero
767 void seq_pad(struct seq_file *m, char c)
769 int size = m->pad_until - m->count;
771 seq_printf(m, "%*s", size, "");
775 EXPORT_SYMBOL(seq_pad);
777 /* A complete analogue of print_hex_dump() */
778 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
779 int rowsize, int groupsize, const void *buf, size_t len,
783 int i, linelen, remaining = len;
786 if (rowsize != 16 && rowsize != 32)
789 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
790 linelen = min(remaining, rowsize);
791 remaining -= rowsize;
793 switch (prefix_type) {
794 case DUMP_PREFIX_ADDRESS:
795 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
797 case DUMP_PREFIX_OFFSET:
798 seq_printf(m, "%s%.8x: ", prefix_str, i);
801 seq_printf(m, "%s", prefix_str);
805 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
806 m->buf + m->count, m->size - m->count,
808 if (ret >= m->size - m->count) {
816 EXPORT_SYMBOL(seq_hex_dump);
818 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
820 struct list_head *lh;
822 list_for_each(lh, head)
828 EXPORT_SYMBOL(seq_list_start);
830 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
835 return seq_list_start(head, pos - 1);
837 EXPORT_SYMBOL(seq_list_start_head);
839 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
841 struct list_head *lh;
843 lh = ((struct list_head *)v)->next;
845 return lh == head ? NULL : lh;
847 EXPORT_SYMBOL(seq_list_next);
850 * seq_hlist_start - start an iteration of a hlist
851 * @head: the head of the hlist
852 * @pos: the start position of the sequence
854 * Called at seq_file->op->start().
856 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
858 struct hlist_node *node;
860 hlist_for_each(node, head)
865 EXPORT_SYMBOL(seq_hlist_start);
868 * seq_hlist_start_head - start an iteration of a hlist
869 * @head: the head of the hlist
870 * @pos: the start position of the sequence
872 * Called at seq_file->op->start(). Call this function if you want to
873 * print a header at the top of the output.
875 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
878 return SEQ_START_TOKEN;
880 return seq_hlist_start(head, pos - 1);
882 EXPORT_SYMBOL(seq_hlist_start_head);
885 * seq_hlist_next - move to the next position of the hlist
886 * @v: the current iterator
887 * @head: the head of the hlist
888 * @ppos: the current position
890 * Called at seq_file->op->next().
892 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
895 struct hlist_node *node = v;
898 if (v == SEQ_START_TOKEN)
903 EXPORT_SYMBOL(seq_hlist_next);
906 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
907 * @head: the head of the hlist
908 * @pos: the start position of the sequence
910 * Called at seq_file->op->start().
912 * This list-traversal primitive may safely run concurrently with
913 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
914 * as long as the traversal is guarded by rcu_read_lock().
916 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
919 struct hlist_node *node;
921 __hlist_for_each_rcu(node, head)
926 EXPORT_SYMBOL(seq_hlist_start_rcu);
929 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
930 * @head: the head of the hlist
931 * @pos: the start position of the sequence
933 * Called at seq_file->op->start(). Call this function if you want to
934 * print a header at the top of the output.
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_start_head_rcu(struct hlist_head *head,
944 return SEQ_START_TOKEN;
946 return seq_hlist_start_rcu(head, pos - 1);
948 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
951 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
952 * @v: the current iterator
953 * @head: the head of the hlist
954 * @ppos: the current position
956 * Called at seq_file->op->next().
958 * This list-traversal primitive may safely run concurrently with
959 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
960 * as long as the traversal is guarded by rcu_read_lock().
962 struct hlist_node *seq_hlist_next_rcu(void *v,
963 struct hlist_head *head,
966 struct hlist_node *node = v;
969 if (v == SEQ_START_TOKEN)
970 return rcu_dereference(head->first);
972 return rcu_dereference(node->next);
974 EXPORT_SYMBOL(seq_hlist_next_rcu);
977 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
978 * @head: pointer to percpu array of struct hlist_heads
979 * @cpu: pointer to cpu "cursor"
980 * @pos: start position of sequence
982 * Called at seq_file->op->start().
985 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
987 struct hlist_node *node;
989 for_each_possible_cpu(*cpu) {
990 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
997 EXPORT_SYMBOL(seq_hlist_start_percpu);
1000 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1001 * @v: pointer to current hlist_node
1002 * @head: pointer to percpu array of struct hlist_heads
1003 * @cpu: pointer to cpu "cursor"
1004 * @pos: start position of sequence
1006 * Called at seq_file->op->next().
1009 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1010 int *cpu, loff_t *pos)
1012 struct hlist_node *node = v;
1019 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1020 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1021 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1023 if (!hlist_empty(bucket))
1024 return bucket->first;
1028 EXPORT_SYMBOL(seq_hlist_next_percpu);