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)
29 gfp_t gfp = GFP_KERNEL;
31 if (unlikely(size > MAX_RW_COUNT))
35 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
36 * it's better to fall back to vmalloc() than to kill things. For small
37 * allocations, just use GFP_KERNEL which will oom kill, thus no need
38 * for vmalloc fallback.
41 gfp |= __GFP_NORETRY | __GFP_NOWARN;
42 buf = kmalloc(size, gfp);
43 if (!buf && size > PAGE_SIZE)
49 * seq_open - initialize sequential file
50 * @file: file we initialize
51 * @op: method table describing the sequence
53 * seq_open() sets @file, associating it with a sequence described
54 * by @op. @op->start() sets the iterator up and returns the first
55 * element of sequence. @op->stop() shuts it down. @op->next()
56 * returns the next element of sequence. @op->show() prints element
57 * into the buffer. In case of error ->start() and ->next() return
58 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
59 * returns 0 in case of success and negative number in case of error.
60 * Returning SEQ_SKIP means "discard this element and move on".
61 * Note: seq_open() will allocate a struct seq_file and store its
62 * pointer in @file->private_data. This pointer should not be modified.
64 int seq_open(struct file *file, const struct seq_operations *op)
68 WARN_ON(file->private_data);
70 p = kzalloc(sizeof(*p), GFP_KERNEL);
74 file->private_data = p;
79 // No refcounting: the lifetime of 'p' is constrained
80 // to the lifetime of the file.
84 * Wrappers around seq_open(e.g. swaps_open) need to be
85 * aware of this. If they set f_version themselves, they
86 * should call seq_open first and then set f_version.
91 * seq_files support lseek() and pread(). They do not implement
92 * write() at all, but we clear FMODE_PWRITE here for historical
95 * If a client of seq_files a) implements file.write() and b) wishes to
96 * support pwrite() then that client will need to implement its own
97 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
99 file->f_mode &= ~FMODE_PWRITE;
102 EXPORT_SYMBOL(seq_open);
104 static int traverse(struct seq_file *m, loff_t offset)
106 loff_t pos = 0, index;
112 m->count = m->from = 0;
118 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
122 p = m->op->start(m, &index);
127 error = m->op->show(m, p);
130 if (unlikely(error)) {
134 if (seq_has_overflowed(m))
136 if (pos + m->count > offset) {
137 m->from = offset - pos;
149 p = m->op->next(m, p, &index);
159 m->buf = seq_buf_alloc(m->size <<= 1);
160 return !m->buf ? -ENOMEM : -EAGAIN;
164 * seq_read - ->read() method for sequential files.
165 * @file: the file to read from
166 * @buf: the buffer to read to
167 * @size: the maximum number of bytes to read
168 * @ppos: the current position in the file
170 * Ready-made ->f_op->read()
172 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
174 struct seq_file *m = file->private_data;
181 mutex_lock(&m->lock);
184 * seq_file->op->..m_start/m_stop/m_next may do special actions
185 * or optimisations based on the file->f_version, so we want to
186 * pass the file->f_version to those methods.
188 * seq_file->version is just copy of f_version, and seq_file
189 * methods can treat it simply as file version.
190 * It is copied in first and copied out after all operations.
191 * It is convenient to have it as part of structure to avoid the
192 * need of passing another argument to all the seq_file methods.
194 m->version = file->f_version;
196 /* Don't assume *ppos is where we left it */
197 if (unlikely(*ppos != m->read_pos)) {
198 while ((err = traverse(m, *ppos)) == -EAGAIN)
201 /* With prejudice... */
212 /* grab buffer if we didn't have one */
214 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
218 /* if not empty - flush it first */
220 n = min(m->count, size);
221 err = copy_to_user(buf, m->buf + m->from, n);
236 /* we need at least one record in buffer */
238 p = m->op->start(m, &pos);
243 err = m->op->show(m, p);
248 if (unlikely(!m->count)) {
249 p = m->op->next(m, p, &pos);
253 if (m->count < m->size)
258 m->buf = seq_buf_alloc(m->size <<= 1);
263 p = m->op->start(m, &pos);
269 /* they want more? let's try to get some more */
270 while (m->count < size) {
271 size_t offs = m->count;
273 p = m->op->next(m, p, &next);
274 if (!p || IS_ERR(p)) {
278 err = m->op->show(m, p);
279 if (seq_has_overflowed(m) || err) {
281 if (likely(err <= 0))
287 n = min(m->count, size);
288 err = copy_to_user(buf, m->buf, n);
303 m->read_pos += copied;
305 file->f_version = m->version;
306 mutex_unlock(&m->lock);
315 EXPORT_SYMBOL(seq_read);
318 * seq_lseek - ->llseek() method for sequential files.
319 * @file: the file in question
320 * @offset: new position
321 * @whence: 0 for absolute, 1 for relative position
323 * Ready-made ->f_op->llseek()
325 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
327 struct seq_file *m = file->private_data;
328 loff_t retval = -EINVAL;
330 mutex_lock(&m->lock);
331 m->version = file->f_version;
334 offset += file->f_pos;
339 if (offset != m->read_pos) {
340 while ((retval = traverse(m, offset)) == -EAGAIN)
343 /* with extreme prejudice... */
350 m->read_pos = offset;
351 retval = file->f_pos = offset;
354 file->f_pos = offset;
357 file->f_version = m->version;
358 mutex_unlock(&m->lock);
361 EXPORT_SYMBOL(seq_lseek);
364 * seq_release - free the structures associated with sequential file.
365 * @file: file in question
368 * Frees the structures associated with sequential file; can be used
369 * as ->f_op->release() if you don't have private data to destroy.
371 int seq_release(struct inode *inode, struct file *file)
373 struct seq_file *m = file->private_data;
378 EXPORT_SYMBOL(seq_release);
381 * seq_escape - print string into buffer, escaping some characters
384 * @esc: set of characters that need escaping
386 * Puts string into buffer, replacing each occurrence of character from
387 * @esc with usual octal escape.
388 * Use seq_has_overflowed() to check for errors.
390 void seq_escape(struct seq_file *m, const char *s, const char *esc)
393 size_t size = seq_get_buf(m, &buf);
396 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
397 seq_commit(m, ret < size ? ret : -1);
399 EXPORT_SYMBOL(seq_escape);
401 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
405 if (m->count < m->size) {
406 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
407 if (m->count + len < m->size) {
414 EXPORT_SYMBOL(seq_vprintf);
416 void seq_printf(struct seq_file *m, const char *f, ...)
421 seq_vprintf(m, f, args);
424 EXPORT_SYMBOL(seq_printf);
427 * mangle_path - mangle and copy path to buffer beginning
429 * @p: beginning of path in above buffer
430 * @esc: set of characters that need escaping
432 * Copy the path from @p to @s, replacing each occurrence of character from
433 * @esc with usual octal escape.
434 * Returns pointer past last written character in @s, or NULL in case of
437 char *mangle_path(char *s, const char *p, const char *esc)
443 } else if (!strchr(esc, c)) {
445 } else if (s + 4 > p) {
449 *s++ = '0' + ((c & 0300) >> 6);
450 *s++ = '0' + ((c & 070) >> 3);
451 *s++ = '0' + (c & 07);
456 EXPORT_SYMBOL(mangle_path);
459 * seq_path - seq_file interface to print a pathname
460 * @m: the seq_file handle
461 * @path: the struct path to print
462 * @esc: set of characters to escape in the output
464 * return the absolute path of 'path', as represented by the
465 * dentry / mnt pair in the path parameter.
467 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
470 size_t size = seq_get_buf(m, &buf);
474 char *p = d_path(path, buf, size);
476 char *end = mangle_path(buf, p, esc);
485 EXPORT_SYMBOL(seq_path);
488 * seq_file_path - seq_file interface to print a pathname of a file
489 * @m: the seq_file handle
490 * @file: the struct file to print
491 * @esc: set of characters to escape in the output
493 * return the absolute path to the file.
495 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
497 return seq_path(m, &file->f_path, esc);
499 EXPORT_SYMBOL(seq_file_path);
502 * Same as seq_path, but relative to supplied root.
504 int seq_path_root(struct seq_file *m, const struct path *path,
505 const struct path *root, const char *esc)
508 size_t size = seq_get_buf(m, &buf);
509 int res = -ENAMETOOLONG;
514 p = __d_path(path, root, buf, size);
519 char *end = mangle_path(buf, p, esc);
528 return res < 0 && res != -ENAMETOOLONG ? res : 0;
532 * returns the path of the 'dentry' from the root of its filesystem.
534 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
537 size_t size = seq_get_buf(m, &buf);
541 char *p = dentry_path(dentry, buf, size);
543 char *end = mangle_path(buf, p, esc);
552 EXPORT_SYMBOL(seq_dentry);
554 static void *single_start(struct seq_file *p, loff_t *pos)
556 return NULL + (*pos == 0);
559 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
565 static void single_stop(struct seq_file *p, void *v)
569 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
572 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
576 op->start = single_start;
577 op->next = single_next;
578 op->stop = single_stop;
580 res = seq_open(file, op);
582 ((struct seq_file *)file->private_data)->private = data;
588 EXPORT_SYMBOL(single_open);
590 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
591 void *data, size_t size)
593 char *buf = seq_buf_alloc(size);
597 ret = single_open(file, show, data);
602 ((struct seq_file *)file->private_data)->buf = buf;
603 ((struct seq_file *)file->private_data)->size = size;
606 EXPORT_SYMBOL(single_open_size);
608 int single_release(struct inode *inode, struct file *file)
610 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
611 int res = seq_release(inode, file);
615 EXPORT_SYMBOL(single_release);
617 int seq_release_private(struct inode *inode, struct file *file)
619 struct seq_file *seq = file->private_data;
623 return seq_release(inode, file);
625 EXPORT_SYMBOL(seq_release_private);
627 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
632 struct seq_file *seq;
634 private = kzalloc(psize, GFP_KERNEL);
638 rc = seq_open(f, ops);
642 seq = f->private_data;
643 seq->private = private;
651 EXPORT_SYMBOL(__seq_open_private);
653 int seq_open_private(struct file *filp, const struct seq_operations *ops,
656 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
658 EXPORT_SYMBOL(seq_open_private);
660 void seq_putc(struct seq_file *m, char c)
662 if (m->count >= m->size)
665 m->buf[m->count++] = c;
667 EXPORT_SYMBOL(seq_putc);
669 void seq_puts(struct seq_file *m, const char *s)
673 if (m->count + len >= m->size) {
677 memcpy(m->buf + m->count, s, len);
680 EXPORT_SYMBOL(seq_puts);
683 * A helper routine for putting decimal numbers without rich format of printf().
684 * only 'unsigned long long' is supported.
685 * This routine will put strlen(delimiter) + number into seq_file.
686 * This routine is very quick when you show lots of numbers.
687 * In usual cases, it will be better to use seq_printf(). It's easier to read.
689 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
690 unsigned long long num)
694 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
697 len = strlen(delimiter);
698 if (m->count + len >= m->size)
701 memcpy(m->buf + m->count, delimiter, len);
704 if (m->count + 1 >= m->size)
708 m->buf[m->count++] = num + '0';
712 len = num_to_str(m->buf + m->count, m->size - m->count, num);
722 EXPORT_SYMBOL(seq_put_decimal_ull);
724 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
728 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
731 len = strlen(delimiter);
732 if (m->count + len >= m->size)
735 memcpy(m->buf + m->count, delimiter, len);
738 if (m->count + 2 >= m->size)
742 m->buf[m->count++] = '-';
747 m->buf[m->count++] = num + '0';
751 len = num_to_str(m->buf + m->count, m->size - m->count, num);
761 EXPORT_SYMBOL(seq_put_decimal_ll);
764 * seq_write - write arbitrary data to buffer
765 * @seq: seq_file identifying the buffer to which data should be written
766 * @data: data address
767 * @len: number of bytes
769 * Return 0 on success, non-zero otherwise.
771 int seq_write(struct seq_file *seq, const void *data, size_t len)
773 if (seq->count + len < seq->size) {
774 memcpy(seq->buf + seq->count, data, len);
778 seq_set_overflow(seq);
781 EXPORT_SYMBOL(seq_write);
784 * seq_pad - write padding spaces to buffer
785 * @m: seq_file identifying the buffer to which data should be written
786 * @c: the byte to append after padding if non-zero
788 void seq_pad(struct seq_file *m, char c)
790 int size = m->pad_until - m->count;
792 seq_printf(m, "%*s", size, "");
796 EXPORT_SYMBOL(seq_pad);
798 /* A complete analogue of print_hex_dump() */
799 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
800 int rowsize, int groupsize, const void *buf, size_t len,
804 int i, linelen, remaining = len;
809 if (rowsize != 16 && rowsize != 32)
812 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
813 linelen = min(remaining, rowsize);
814 remaining -= rowsize;
816 switch (prefix_type) {
817 case DUMP_PREFIX_ADDRESS:
818 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
820 case DUMP_PREFIX_OFFSET:
821 seq_printf(m, "%s%.8x: ", prefix_str, i);
824 seq_printf(m, "%s", prefix_str);
828 size = seq_get_buf(m, &buffer);
829 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
830 buffer, size, ascii);
831 seq_commit(m, ret < size ? ret : -1);
836 EXPORT_SYMBOL(seq_hex_dump);
838 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
840 struct list_head *lh;
842 list_for_each(lh, head)
848 EXPORT_SYMBOL(seq_list_start);
850 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
855 return seq_list_start(head, pos - 1);
857 EXPORT_SYMBOL(seq_list_start_head);
859 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
861 struct list_head *lh;
863 lh = ((struct list_head *)v)->next;
865 return lh == head ? NULL : lh;
867 EXPORT_SYMBOL(seq_list_next);
870 * seq_hlist_start - start an iteration of a hlist
871 * @head: the head of the hlist
872 * @pos: the start position of the sequence
874 * Called at seq_file->op->start().
876 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
878 struct hlist_node *node;
880 hlist_for_each(node, head)
885 EXPORT_SYMBOL(seq_hlist_start);
888 * seq_hlist_start_head - start an iteration of a hlist
889 * @head: the head of the hlist
890 * @pos: the start position of the sequence
892 * Called at seq_file->op->start(). Call this function if you want to
893 * print a header at the top of the output.
895 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
898 return SEQ_START_TOKEN;
900 return seq_hlist_start(head, pos - 1);
902 EXPORT_SYMBOL(seq_hlist_start_head);
905 * seq_hlist_next - move to the next position of the hlist
906 * @v: the current iterator
907 * @head: the head of the hlist
908 * @ppos: the current position
910 * Called at seq_file->op->next().
912 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
915 struct hlist_node *node = v;
918 if (v == SEQ_START_TOKEN)
923 EXPORT_SYMBOL(seq_hlist_next);
926 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
927 * @head: the head of the hlist
928 * @pos: the start position of the sequence
930 * Called at seq_file->op->start().
932 * This list-traversal primitive may safely run concurrently with
933 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
934 * as long as the traversal is guarded by rcu_read_lock().
936 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
939 struct hlist_node *node;
941 __hlist_for_each_rcu(node, head)
946 EXPORT_SYMBOL(seq_hlist_start_rcu);
949 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
950 * @head: the head of the hlist
951 * @pos: the start position of the sequence
953 * Called at seq_file->op->start(). Call this function if you want to
954 * print a header at the top of the output.
956 * This list-traversal primitive may safely run concurrently with
957 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
958 * as long as the traversal is guarded by rcu_read_lock().
960 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
964 return SEQ_START_TOKEN;
966 return seq_hlist_start_rcu(head, pos - 1);
968 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
971 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
972 * @v: the current iterator
973 * @head: the head of the hlist
974 * @ppos: the current position
976 * Called at seq_file->op->next().
978 * This list-traversal primitive may safely run concurrently with
979 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
980 * as long as the traversal is guarded by rcu_read_lock().
982 struct hlist_node *seq_hlist_next_rcu(void *v,
983 struct hlist_head *head,
986 struct hlist_node *node = v;
989 if (v == SEQ_START_TOKEN)
990 return rcu_dereference(head->first);
992 return rcu_dereference(node->next);
994 EXPORT_SYMBOL(seq_hlist_next_rcu);
997 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
998 * @head: pointer to percpu array of struct hlist_heads
999 * @cpu: pointer to cpu "cursor"
1000 * @pos: start position of sequence
1002 * Called at seq_file->op->start().
1005 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1007 struct hlist_node *node;
1009 for_each_possible_cpu(*cpu) {
1010 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1017 EXPORT_SYMBOL(seq_hlist_start_percpu);
1020 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1021 * @v: pointer to current hlist_node
1022 * @head: pointer to percpu array of struct hlist_heads
1023 * @cpu: pointer to cpu "cursor"
1024 * @pos: start position of sequence
1026 * Called at seq_file->op->next().
1029 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1030 int *cpu, loff_t *pos)
1032 struct hlist_node *node = v;
1039 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1040 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1041 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1043 if (!hlist_empty(bucket))
1044 return bucket->first;
1048 EXPORT_SYMBOL(seq_hlist_next_percpu);