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>
16 #include <asm/uaccess.h>
19 static void seq_set_overflow(struct seq_file *m)
24 static void *seq_buf_alloc(unsigned long size)
29 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
30 * it's better to fall back to vmalloc() than to kill things.
32 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
33 if (!buf && size > PAGE_SIZE)
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
50 * Returning SEQ_SKIP means "discard this element and move on".
52 int seq_open(struct file *file, const struct seq_operations *op)
56 WARN_ON(file->private_data);
58 p = kzalloc(sizeof(*p), GFP_KERNEL);
62 file->private_data = p;
67 p->user_ns = file->f_cred->user_ns;
71 * Wrappers around seq_open(e.g. swaps_open) need to be
72 * aware of this. If they set f_version themselves, they
73 * should call seq_open first and then set f_version.
78 * seq_files support lseek() and pread(). They do not implement
79 * write() at all, but we clear FMODE_PWRITE here for historical
82 * If a client of seq_files a) implements file.write() and b) wishes to
83 * support pwrite() then that client will need to implement its own
84 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
86 file->f_mode &= ~FMODE_PWRITE;
89 EXPORT_SYMBOL(seq_open);
91 static int traverse(struct seq_file *m, loff_t offset)
93 loff_t pos = 0, index;
99 m->count = m->from = 0;
105 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
109 p = m->op->start(m, &index);
114 error = m->op->show(m, p);
117 if (unlikely(error)) {
121 if (seq_has_overflowed(m))
123 if (pos + m->count > offset) {
124 m->from = offset - pos;
136 p = m->op->next(m, p, &index);
146 m->buf = seq_buf_alloc(m->size <<= 1);
147 return !m->buf ? -ENOMEM : -EAGAIN;
151 * seq_read - ->read() method for sequential files.
152 * @file: the file to read from
153 * @buf: the buffer to read to
154 * @size: the maximum number of bytes to read
155 * @ppos: the current position in the file
157 * Ready-made ->f_op->read()
159 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
161 struct seq_file *m = file->private_data;
168 mutex_lock(&m->lock);
171 * seq_file->op->..m_start/m_stop/m_next may do special actions
172 * or optimisations based on the file->f_version, so we want to
173 * pass the file->f_version to those methods.
175 * seq_file->version is just copy of f_version, and seq_file
176 * methods can treat it simply as file version.
177 * It is copied in first and copied out after all operations.
178 * It is convenient to have it as part of structure to avoid the
179 * need of passing another argument to all the seq_file methods.
181 m->version = file->f_version;
183 /* Don't assume *ppos is where we left it */
184 if (unlikely(*ppos != m->read_pos)) {
185 while ((err = traverse(m, *ppos)) == -EAGAIN)
188 /* With prejudice... */
199 /* grab buffer if we didn't have one */
201 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
205 /* if not empty - flush it first */
207 n = min(m->count, size);
208 err = copy_to_user(buf, m->buf + m->from, n);
221 /* we need at least one record in buffer */
223 p = m->op->start(m, &pos);
228 err = m->op->show(m, p);
233 if (unlikely(!m->count)) {
234 p = m->op->next(m, p, &pos);
238 if (m->count < m->size)
243 m->buf = seq_buf_alloc(m->size <<= 1);
248 p = m->op->start(m, &pos);
254 /* they want more? let's try to get some more */
255 while (m->count < size) {
256 size_t offs = m->count;
258 p = m->op->next(m, p, &next);
259 if (!p || IS_ERR(p)) {
263 err = m->op->show(m, p);
264 if (seq_has_overflowed(m) || err) {
266 if (likely(err <= 0))
272 n = min(m->count, size);
273 err = copy_to_user(buf, m->buf, n);
288 m->read_pos += copied;
290 file->f_version = m->version;
291 mutex_unlock(&m->lock);
300 EXPORT_SYMBOL(seq_read);
303 * seq_lseek - ->llseek() method for sequential files.
304 * @file: the file in question
305 * @offset: new position
306 * @whence: 0 for absolute, 1 for relative position
308 * Ready-made ->f_op->llseek()
310 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
312 struct seq_file *m = file->private_data;
313 loff_t retval = -EINVAL;
315 mutex_lock(&m->lock);
316 m->version = file->f_version;
319 offset += file->f_pos;
324 if (offset != m->read_pos) {
325 while ((retval = traverse(m, offset)) == -EAGAIN)
328 /* with extreme prejudice... */
335 m->read_pos = offset;
336 retval = file->f_pos = offset;
339 file->f_pos = offset;
342 file->f_version = m->version;
343 mutex_unlock(&m->lock);
346 EXPORT_SYMBOL(seq_lseek);
349 * seq_release - free the structures associated with sequential file.
350 * @file: file in question
353 * Frees the structures associated with sequential file; can be used
354 * as ->f_op->release() if you don't have private data to destroy.
356 int seq_release(struct inode *inode, struct file *file)
358 struct seq_file *m = file->private_data;
363 EXPORT_SYMBOL(seq_release);
366 * seq_escape - print string into buffer, escaping some characters
369 * @esc: set of characters that need escaping
371 * Puts string into buffer, replacing each occurrence of character from
372 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
375 int seq_escape(struct seq_file *m, const char *s, const char *esc)
377 char *end = m->buf + m->size;
381 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
382 if (!strchr(esc, c)) {
388 *p++ = '0' + ((c & 0300) >> 6);
389 *p++ = '0' + ((c & 070) >> 3);
390 *p++ = '0' + (c & 07);
396 m->count = p - m->buf;
399 EXPORT_SYMBOL(seq_escape);
401 int 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) {
415 EXPORT_SYMBOL(seq_vprintf);
417 int seq_printf(struct seq_file *m, const char *f, ...)
423 ret = seq_vprintf(m, f, args);
428 EXPORT_SYMBOL(seq_printf);
431 * mangle_path - mangle and copy path to buffer beginning
433 * @p: beginning of path in above buffer
434 * @esc: set of characters that need escaping
436 * Copy the path from @p to @s, replacing each occurrence of character from
437 * @esc with usual octal escape.
438 * Returns pointer past last written character in @s, or NULL in case of
441 char *mangle_path(char *s, const char *p, const char *esc)
447 } else if (!strchr(esc, c)) {
449 } else if (s + 4 > p) {
453 *s++ = '0' + ((c & 0300) >> 6);
454 *s++ = '0' + ((c & 070) >> 3);
455 *s++ = '0' + (c & 07);
460 EXPORT_SYMBOL(mangle_path);
463 * seq_path - seq_file interface to print a pathname
464 * @m: the seq_file handle
465 * @path: the struct path to print
466 * @esc: set of characters to escape in the output
468 * return the absolute path of 'path', as represented by the
469 * dentry / mnt pair in the path parameter.
471 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
474 size_t size = seq_get_buf(m, &buf);
478 char *p = d_path(path, buf, size);
480 char *end = mangle_path(buf, p, esc);
489 EXPORT_SYMBOL(seq_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);
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 int seq_putc(struct seq_file *m, char c)
651 if (m->count < m->size) {
652 m->buf[m->count++] = c;
657 EXPORT_SYMBOL(seq_putc);
659 int seq_puts(struct seq_file *m, const char *s)
662 if (m->count + len < m->size) {
663 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 one byte 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 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
680 unsigned long long num)
684 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
688 m->buf[m->count++] = delimiter;
691 m->buf[m->count++] = num + '0';
695 len = num_to_str(m->buf + m->count, m->size - m->count, num);
704 EXPORT_SYMBOL(seq_put_decimal_ull);
706 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
710 if (m->count + 3 >= m->size) {
715 m->buf[m->count++] = delimiter;
719 return seq_put_decimal_ull(m, delimiter, num);
722 EXPORT_SYMBOL(seq_put_decimal_ll);
725 * seq_write - write arbitrary data to buffer
726 * @seq: seq_file identifying the buffer to which data should be written
727 * @data: data address
728 * @len: number of bytes
730 * Return 0 on success, non-zero otherwise.
732 int seq_write(struct seq_file *seq, const void *data, size_t len)
734 if (seq->count + len < seq->size) {
735 memcpy(seq->buf + seq->count, data, len);
739 seq_set_overflow(seq);
742 EXPORT_SYMBOL(seq_write);
745 * seq_pad - write padding spaces to buffer
746 * @m: seq_file identifying the buffer to which data should be written
747 * @c: the byte to append after padding if non-zero
749 void seq_pad(struct seq_file *m, char c)
751 int size = m->pad_until - m->count;
753 seq_printf(m, "%*s", size, "");
757 EXPORT_SYMBOL(seq_pad);
759 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
761 struct list_head *lh;
763 list_for_each(lh, head)
769 EXPORT_SYMBOL(seq_list_start);
771 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
776 return seq_list_start(head, pos - 1);
778 EXPORT_SYMBOL(seq_list_start_head);
780 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
782 struct list_head *lh;
784 lh = ((struct list_head *)v)->next;
786 return lh == head ? NULL : lh;
788 EXPORT_SYMBOL(seq_list_next);
791 * seq_hlist_start - start an iteration of a hlist
792 * @head: the head of the hlist
793 * @pos: the start position of the sequence
795 * Called at seq_file->op->start().
797 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
799 struct hlist_node *node;
801 hlist_for_each(node, head)
806 EXPORT_SYMBOL(seq_hlist_start);
809 * seq_hlist_start_head - start an iteration of a hlist
810 * @head: the head of the hlist
811 * @pos: the start position of the sequence
813 * Called at seq_file->op->start(). Call this function if you want to
814 * print a header at the top of the output.
816 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
819 return SEQ_START_TOKEN;
821 return seq_hlist_start(head, pos - 1);
823 EXPORT_SYMBOL(seq_hlist_start_head);
826 * seq_hlist_next - move to the next position of the hlist
827 * @v: the current iterator
828 * @head: the head of the hlist
829 * @ppos: the current position
831 * Called at seq_file->op->next().
833 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
836 struct hlist_node *node = v;
839 if (v == SEQ_START_TOKEN)
844 EXPORT_SYMBOL(seq_hlist_next);
847 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
848 * @head: the head of the hlist
849 * @pos: the start position of the sequence
851 * Called at seq_file->op->start().
853 * This list-traversal primitive may safely run concurrently with
854 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
855 * as long as the traversal is guarded by rcu_read_lock().
857 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
860 struct hlist_node *node;
862 __hlist_for_each_rcu(node, head)
867 EXPORT_SYMBOL(seq_hlist_start_rcu);
870 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
871 * @head: the head of the hlist
872 * @pos: the start position of the sequence
874 * Called at seq_file->op->start(). Call this function if you want to
875 * print a header at the top of the output.
877 * This list-traversal primitive may safely run concurrently with
878 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
879 * as long as the traversal is guarded by rcu_read_lock().
881 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
885 return SEQ_START_TOKEN;
887 return seq_hlist_start_rcu(head, pos - 1);
889 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
892 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
893 * @v: the current iterator
894 * @head: the head of the hlist
895 * @ppos: the current position
897 * Called at seq_file->op->next().
899 * This list-traversal primitive may safely run concurrently with
900 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
901 * as long as the traversal is guarded by rcu_read_lock().
903 struct hlist_node *seq_hlist_next_rcu(void *v,
904 struct hlist_head *head,
907 struct hlist_node *node = v;
910 if (v == SEQ_START_TOKEN)
911 return rcu_dereference(head->first);
913 return rcu_dereference(node->next);
915 EXPORT_SYMBOL(seq_hlist_next_rcu);
918 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
919 * @head: pointer to percpu array of struct hlist_heads
920 * @cpu: pointer to cpu "cursor"
921 * @pos: start position of sequence
923 * Called at seq_file->op->start().
926 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
928 struct hlist_node *node;
930 for_each_possible_cpu(*cpu) {
931 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
938 EXPORT_SYMBOL(seq_hlist_start_percpu);
941 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
942 * @v: pointer to current hlist_node
943 * @head: pointer to percpu array of struct hlist_heads
944 * @cpu: pointer to cpu "cursor"
945 * @pos: start position of sequence
947 * Called at seq_file->op->next().
950 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
951 int *cpu, loff_t *pos)
953 struct hlist_node *node = v;
960 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
961 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
962 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
964 if (!hlist_empty(bucket))
965 return bucket->first;
969 EXPORT_SYMBOL(seq_hlist_next_percpu);