]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/seq_file.c
tools build: Use $(shell ) instead of `` to get embedded libperl's ccopts
[mirror_ubuntu-focal-kernel.git] / fs / seq_file.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/seq_file.c
4 *
5 * helper functions for making synthetic files from sequences of records.
6 * initial implementation -- AV, Oct 2001.
7 */
8
9 #include <linux/cache.h>
10 #include <linux/fs.h>
11 #include <linux/export.h>
12 #include <linux/seq_file.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 #include <linux/cred.h>
16 #include <linux/mm.h>
17 #include <linux/printk.h>
18 #include <linux/string_helpers.h>
19
20 #include <linux/uaccess.h>
21 #include <asm/page.h>
22
23 static struct kmem_cache *seq_file_cache __ro_after_init;
24
25 static void seq_set_overflow(struct seq_file *m)
26 {
27 m->count = m->size;
28 }
29
30 static void *seq_buf_alloc(unsigned long size)
31 {
32 if (unlikely(size > MAX_RW_COUNT))
33 return NULL;
34
35 return kvmalloc(size, GFP_KERNEL_ACCOUNT);
36 }
37
38 /**
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
42 *
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".
51 * Note: seq_open() will allocate a struct seq_file and store its
52 * pointer in @file->private_data. This pointer should not be modified.
53 */
54 int seq_open(struct file *file, const struct seq_operations *op)
55 {
56 struct seq_file *p;
57
58 WARN_ON(file->private_data);
59
60 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
61 if (!p)
62 return -ENOMEM;
63
64 file->private_data = p;
65
66 mutex_init(&p->lock);
67 p->op = op;
68
69 // No refcounting: the lifetime of 'p' is constrained
70 // to the lifetime of the file.
71 p->file = file;
72
73 /*
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.
77 */
78 file->f_version = 0;
79
80 /*
81 * seq_files support lseek() and pread(). They do not implement
82 * write() at all, but we clear FMODE_PWRITE here for historical
83 * reasons.
84 *
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.
88 */
89 file->f_mode &= ~FMODE_PWRITE;
90 return 0;
91 }
92 EXPORT_SYMBOL(seq_open);
93
94 static int traverse(struct seq_file *m, loff_t offset)
95 {
96 loff_t pos = 0;
97 int error = 0;
98 void *p;
99
100 m->version = 0;
101 m->index = 0;
102 m->count = m->from = 0;
103 if (!offset)
104 return 0;
105
106 if (!m->buf) {
107 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
108 if (!m->buf)
109 return -ENOMEM;
110 }
111 p = m->op->start(m, &m->index);
112 while (p) {
113 error = PTR_ERR(p);
114 if (IS_ERR(p))
115 break;
116 error = m->op->show(m, p);
117 if (error < 0)
118 break;
119 if (unlikely(error)) {
120 error = 0;
121 m->count = 0;
122 }
123 if (seq_has_overflowed(m))
124 goto Eoverflow;
125 p = m->op->next(m, p, &m->index);
126 if (pos + m->count > offset) {
127 m->from = offset - pos;
128 m->count -= m->from;
129 break;
130 }
131 pos += m->count;
132 m->count = 0;
133 if (pos == offset)
134 break;
135 }
136 m->op->stop(m, p);
137 return error;
138
139 Eoverflow:
140 m->op->stop(m, p);
141 kvfree(m->buf);
142 m->count = 0;
143 m->buf = seq_buf_alloc(m->size <<= 1);
144 return !m->buf ? -ENOMEM : -EAGAIN;
145 }
146
147 /**
148 * seq_read - ->read() method for sequential files.
149 * @file: the file to read from
150 * @buf: the buffer to read to
151 * @size: the maximum number of bytes to read
152 * @ppos: the current position in the file
153 *
154 * Ready-made ->f_op->read()
155 */
156 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
157 {
158 struct seq_file *m = file->private_data;
159 size_t copied = 0;
160 size_t n;
161 void *p;
162 int err = 0;
163
164 mutex_lock(&m->lock);
165
166 /*
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.
170 *
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.
176 */
177 m->version = file->f_version;
178
179 /*
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
182 */
183 if (*ppos == 0) {
184 m->index = 0;
185 m->version = 0;
186 m->count = 0;
187 }
188
189 /* Don't assume *ppos is where we left it */
190 if (unlikely(*ppos != m->read_pos)) {
191 while ((err = traverse(m, *ppos)) == -EAGAIN)
192 ;
193 if (err) {
194 /* With prejudice... */
195 m->read_pos = 0;
196 m->version = 0;
197 m->index = 0;
198 m->count = 0;
199 goto Done;
200 } else {
201 m->read_pos = *ppos;
202 }
203 }
204
205 /* grab buffer if we didn't have one */
206 if (!m->buf) {
207 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
208 if (!m->buf)
209 goto Enomem;
210 }
211 /* if not empty - flush it first */
212 if (m->count) {
213 n = min(m->count, size);
214 err = copy_to_user(buf, m->buf + m->from, n);
215 if (err)
216 goto Efault;
217 m->count -= n;
218 m->from += n;
219 size -= n;
220 buf += n;
221 copied += n;
222 if (!size)
223 goto Done;
224 }
225 /* we need at least one record in buffer */
226 m->from = 0;
227 p = m->op->start(m, &m->index);
228 while (1) {
229 err = PTR_ERR(p);
230 if (!p || IS_ERR(p))
231 break;
232 err = m->op->show(m, p);
233 if (err < 0)
234 break;
235 if (unlikely(err))
236 m->count = 0;
237 if (unlikely(!m->count)) {
238 p = m->op->next(m, p, &m->index);
239 continue;
240 }
241 if (m->count < m->size)
242 goto Fill;
243 m->op->stop(m, p);
244 kvfree(m->buf);
245 m->count = 0;
246 m->buf = seq_buf_alloc(m->size <<= 1);
247 if (!m->buf)
248 goto Enomem;
249 m->version = 0;
250 p = m->op->start(m, &m->index);
251 }
252 m->op->stop(m, p);
253 m->count = 0;
254 goto Done;
255 Fill:
256 /* they want more? let's try to get some more */
257 while (1) {
258 size_t offs = m->count;
259 loff_t pos = m->index;
260
261 p = m->op->next(m, p, &m->index);
262 if (pos == m->index)
263 /* Buggy ->next function */
264 m->index++;
265 if (!p || IS_ERR(p)) {
266 err = PTR_ERR(p);
267 break;
268 }
269 if (m->count >= size)
270 break;
271 err = m->op->show(m, p);
272 if (seq_has_overflowed(m) || err) {
273 m->count = offs;
274 if (likely(err <= 0))
275 break;
276 }
277 }
278 m->op->stop(m, p);
279 n = min(m->count, size);
280 err = copy_to_user(buf, m->buf, n);
281 if (err)
282 goto Efault;
283 copied += n;
284 m->count -= n;
285 m->from = n;
286 Done:
287 if (!copied)
288 copied = err;
289 else {
290 *ppos += copied;
291 m->read_pos += copied;
292 }
293 file->f_version = m->version;
294 mutex_unlock(&m->lock);
295 return copied;
296 Enomem:
297 err = -ENOMEM;
298 goto Done;
299 Efault:
300 err = -EFAULT;
301 goto Done;
302 }
303 EXPORT_SYMBOL(seq_read);
304
305 /**
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
310 *
311 * Ready-made ->f_op->llseek()
312 */
313 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
314 {
315 struct seq_file *m = file->private_data;
316 loff_t retval = -EINVAL;
317
318 mutex_lock(&m->lock);
319 m->version = file->f_version;
320 switch (whence) {
321 case SEEK_CUR:
322 offset += file->f_pos;
323 /* fall through */
324 case SEEK_SET:
325 if (offset < 0)
326 break;
327 retval = offset;
328 if (offset != m->read_pos) {
329 while ((retval = traverse(m, offset)) == -EAGAIN)
330 ;
331 if (retval) {
332 /* with extreme prejudice... */
333 file->f_pos = 0;
334 m->read_pos = 0;
335 m->version = 0;
336 m->index = 0;
337 m->count = 0;
338 } else {
339 m->read_pos = offset;
340 retval = file->f_pos = offset;
341 }
342 } else {
343 file->f_pos = offset;
344 }
345 }
346 file->f_version = m->version;
347 mutex_unlock(&m->lock);
348 return retval;
349 }
350 EXPORT_SYMBOL(seq_lseek);
351
352 /**
353 * seq_release - free the structures associated with sequential file.
354 * @file: file in question
355 * @inode: its inode
356 *
357 * Frees the structures associated with sequential file; can be used
358 * as ->f_op->release() if you don't have private data to destroy.
359 */
360 int seq_release(struct inode *inode, struct file *file)
361 {
362 struct seq_file *m = file->private_data;
363 kvfree(m->buf);
364 kmem_cache_free(seq_file_cache, m);
365 return 0;
366 }
367 EXPORT_SYMBOL(seq_release);
368
369 /**
370 * seq_escape - print string into buffer, escaping some characters
371 * @m: target buffer
372 * @s: string
373 * @esc: set of characters that need escaping
374 *
375 * Puts string into buffer, replacing each occurrence of character from
376 * @esc with usual octal escape.
377 * Use seq_has_overflowed() to check for errors.
378 */
379 void seq_escape(struct seq_file *m, const char *s, const char *esc)
380 {
381 char *buf;
382 size_t size = seq_get_buf(m, &buf);
383 int ret;
384
385 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
386 seq_commit(m, ret < size ? ret : -1);
387 }
388 EXPORT_SYMBOL(seq_escape);
389
390 void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
391 {
392 char *buf;
393 size_t size = seq_get_buf(m, &buf);
394 int ret;
395
396 ret = string_escape_mem_ascii(src, isz, buf, size);
397 seq_commit(m, ret < size ? ret : -1);
398 }
399 EXPORT_SYMBOL(seq_escape_mem_ascii);
400
401 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
402 {
403 int len;
404
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) {
408 m->count += len;
409 return;
410 }
411 }
412 seq_set_overflow(m);
413 }
414 EXPORT_SYMBOL(seq_vprintf);
415
416 void seq_printf(struct seq_file *m, const char *f, ...)
417 {
418 va_list args;
419
420 va_start(args, f);
421 seq_vprintf(m, f, args);
422 va_end(args);
423 }
424 EXPORT_SYMBOL(seq_printf);
425
426 /**
427 * mangle_path - mangle and copy path to buffer beginning
428 * @s: buffer start
429 * @p: beginning of path in above buffer
430 * @esc: set of characters that need escaping
431 *
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
435 * failure.
436 */
437 char *mangle_path(char *s, const char *p, const char *esc)
438 {
439 while (s <= p) {
440 char c = *p++;
441 if (!c) {
442 return s;
443 } else if (!strchr(esc, c)) {
444 *s++ = c;
445 } else if (s + 4 > p) {
446 break;
447 } else {
448 *s++ = '\\';
449 *s++ = '0' + ((c & 0300) >> 6);
450 *s++ = '0' + ((c & 070) >> 3);
451 *s++ = '0' + (c & 07);
452 }
453 }
454 return NULL;
455 }
456 EXPORT_SYMBOL(mangle_path);
457
458 /**
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
463 *
464 * return the absolute path of 'path', as represented by the
465 * dentry / mnt pair in the path parameter.
466 */
467 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
468 {
469 char *buf;
470 size_t size = seq_get_buf(m, &buf);
471 int res = -1;
472
473 if (size) {
474 char *p = d_path(path, buf, size);
475 if (!IS_ERR(p)) {
476 char *end = mangle_path(buf, p, esc);
477 if (end)
478 res = end - buf;
479 }
480 }
481 seq_commit(m, res);
482
483 return res;
484 }
485 EXPORT_SYMBOL(seq_path);
486
487 /**
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
492 *
493 * return the absolute path to the file.
494 */
495 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
496 {
497 return seq_path(m, &file->f_path, esc);
498 }
499 EXPORT_SYMBOL(seq_file_path);
500
501 /*
502 * Same as seq_path, but relative to supplied root.
503 */
504 int seq_path_root(struct seq_file *m, const struct path *path,
505 const struct path *root, const char *esc)
506 {
507 char *buf;
508 size_t size = seq_get_buf(m, &buf);
509 int res = -ENAMETOOLONG;
510
511 if (size) {
512 char *p;
513
514 p = __d_path(path, root, buf, size);
515 if (!p)
516 return SEQ_SKIP;
517 res = PTR_ERR(p);
518 if (!IS_ERR(p)) {
519 char *end = mangle_path(buf, p, esc);
520 if (end)
521 res = end - buf;
522 else
523 res = -ENAMETOOLONG;
524 }
525 }
526 seq_commit(m, res);
527
528 return res < 0 && res != -ENAMETOOLONG ? res : 0;
529 }
530
531 /*
532 * returns the path of the 'dentry' from the root of its filesystem.
533 */
534 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
535 {
536 char *buf;
537 size_t size = seq_get_buf(m, &buf);
538 int res = -1;
539
540 if (size) {
541 char *p = dentry_path(dentry, buf, size);
542 if (!IS_ERR(p)) {
543 char *end = mangle_path(buf, p, esc);
544 if (end)
545 res = end - buf;
546 }
547 }
548 seq_commit(m, res);
549
550 return res;
551 }
552 EXPORT_SYMBOL(seq_dentry);
553
554 static void *single_start(struct seq_file *p, loff_t *pos)
555 {
556 return NULL + (*pos == 0);
557 }
558
559 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
560 {
561 ++*pos;
562 return NULL;
563 }
564
565 static void single_stop(struct seq_file *p, void *v)
566 {
567 }
568
569 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
570 void *data)
571 {
572 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
573 int res = -ENOMEM;
574
575 if (op) {
576 op->start = single_start;
577 op->next = single_next;
578 op->stop = single_stop;
579 op->show = show;
580 res = seq_open(file, op);
581 if (!res)
582 ((struct seq_file *)file->private_data)->private = data;
583 else
584 kfree(op);
585 }
586 return res;
587 }
588 EXPORT_SYMBOL(single_open);
589
590 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
591 void *data, size_t size)
592 {
593 char *buf = seq_buf_alloc(size);
594 int ret;
595 if (!buf)
596 return -ENOMEM;
597 ret = single_open(file, show, data);
598 if (ret) {
599 kvfree(buf);
600 return ret;
601 }
602 ((struct seq_file *)file->private_data)->buf = buf;
603 ((struct seq_file *)file->private_data)->size = size;
604 return 0;
605 }
606 EXPORT_SYMBOL(single_open_size);
607
608 int single_release(struct inode *inode, struct file *file)
609 {
610 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
611 int res = seq_release(inode, file);
612 kfree(op);
613 return res;
614 }
615 EXPORT_SYMBOL(single_release);
616
617 int seq_release_private(struct inode *inode, struct file *file)
618 {
619 struct seq_file *seq = file->private_data;
620
621 kfree(seq->private);
622 seq->private = NULL;
623 return seq_release(inode, file);
624 }
625 EXPORT_SYMBOL(seq_release_private);
626
627 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
628 int psize)
629 {
630 int rc;
631 void *private;
632 struct seq_file *seq;
633
634 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
635 if (private == NULL)
636 goto out;
637
638 rc = seq_open(f, ops);
639 if (rc < 0)
640 goto out_free;
641
642 seq = f->private_data;
643 seq->private = private;
644 return private;
645
646 out_free:
647 kfree(private);
648 out:
649 return NULL;
650 }
651 EXPORT_SYMBOL(__seq_open_private);
652
653 int seq_open_private(struct file *filp, const struct seq_operations *ops,
654 int psize)
655 {
656 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
657 }
658 EXPORT_SYMBOL(seq_open_private);
659
660 void seq_putc(struct seq_file *m, char c)
661 {
662 if (m->count >= m->size)
663 return;
664
665 m->buf[m->count++] = c;
666 }
667 EXPORT_SYMBOL(seq_putc);
668
669 void seq_puts(struct seq_file *m, const char *s)
670 {
671 int len = strlen(s);
672
673 if (m->count + len >= m->size) {
674 seq_set_overflow(m);
675 return;
676 }
677 memcpy(m->buf + m->count, s, len);
678 m->count += len;
679 }
680 EXPORT_SYMBOL(seq_puts);
681
682 /**
683 * A helper routine for putting decimal numbers without rich format of printf().
684 * only 'unsigned long long' is supported.
685 * @m: seq_file identifying the buffer to which data should be written
686 * @delimiter: a string which is printed before the number
687 * @num: the number
688 * @width: a minimum field width
689 *
690 * This routine will put strlen(delimiter) + number into seq_filed.
691 * This routine is very quick when you show lots of numbers.
692 * In usual cases, it will be better to use seq_printf(). It's easier to read.
693 */
694 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
695 unsigned long long num, unsigned int width)
696 {
697 int len;
698
699 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
700 goto overflow;
701
702 if (delimiter && delimiter[0]) {
703 if (delimiter[1] == 0)
704 seq_putc(m, delimiter[0]);
705 else
706 seq_puts(m, delimiter);
707 }
708
709 if (!width)
710 width = 1;
711
712 if (m->count + width >= m->size)
713 goto overflow;
714
715 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
716 if (!len)
717 goto overflow;
718
719 m->count += len;
720 return;
721
722 overflow:
723 seq_set_overflow(m);
724 }
725
726 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
727 unsigned long long num)
728 {
729 return seq_put_decimal_ull_width(m, delimiter, num, 0);
730 }
731 EXPORT_SYMBOL(seq_put_decimal_ull);
732
733 /**
734 * seq_put_hex_ll - put a number in hexadecimal notation
735 * @m: seq_file identifying the buffer to which data should be written
736 * @delimiter: a string which is printed before the number
737 * @v: the number
738 * @width: a minimum field width
739 *
740 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
741 *
742 * This routine is very quick when you show lots of numbers.
743 * In usual cases, it will be better to use seq_printf(). It's easier to read.
744 */
745 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
746 unsigned long long v, unsigned int width)
747 {
748 unsigned int len;
749 int i;
750
751 if (delimiter && delimiter[0]) {
752 if (delimiter[1] == 0)
753 seq_putc(m, delimiter[0]);
754 else
755 seq_puts(m, delimiter);
756 }
757
758 /* If x is 0, the result of __builtin_clzll is undefined */
759 if (v == 0)
760 len = 1;
761 else
762 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
763
764 if (len < width)
765 len = width;
766
767 if (m->count + len > m->size) {
768 seq_set_overflow(m);
769 return;
770 }
771
772 for (i = len - 1; i >= 0; i--) {
773 m->buf[m->count + i] = hex_asc[0xf & v];
774 v = v >> 4;
775 }
776 m->count += len;
777 }
778
779 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
780 {
781 int len;
782
783 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
784 goto overflow;
785
786 if (delimiter && delimiter[0]) {
787 if (delimiter[1] == 0)
788 seq_putc(m, delimiter[0]);
789 else
790 seq_puts(m, delimiter);
791 }
792
793 if (m->count + 2 >= m->size)
794 goto overflow;
795
796 if (num < 0) {
797 m->buf[m->count++] = '-';
798 num = -num;
799 }
800
801 if (num < 10) {
802 m->buf[m->count++] = num + '0';
803 return;
804 }
805
806 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
807 if (!len)
808 goto overflow;
809
810 m->count += len;
811 return;
812
813 overflow:
814 seq_set_overflow(m);
815 }
816 EXPORT_SYMBOL(seq_put_decimal_ll);
817
818 /**
819 * seq_write - write arbitrary data to buffer
820 * @seq: seq_file identifying the buffer to which data should be written
821 * @data: data address
822 * @len: number of bytes
823 *
824 * Return 0 on success, non-zero otherwise.
825 */
826 int seq_write(struct seq_file *seq, const void *data, size_t len)
827 {
828 if (seq->count + len < seq->size) {
829 memcpy(seq->buf + seq->count, data, len);
830 seq->count += len;
831 return 0;
832 }
833 seq_set_overflow(seq);
834 return -1;
835 }
836 EXPORT_SYMBOL(seq_write);
837
838 /**
839 * seq_pad - write padding spaces to buffer
840 * @m: seq_file identifying the buffer to which data should be written
841 * @c: the byte to append after padding if non-zero
842 */
843 void seq_pad(struct seq_file *m, char c)
844 {
845 int size = m->pad_until - m->count;
846 if (size > 0) {
847 if (size + m->count > m->size) {
848 seq_set_overflow(m);
849 return;
850 }
851 memset(m->buf + m->count, ' ', size);
852 m->count += size;
853 }
854 if (c)
855 seq_putc(m, c);
856 }
857 EXPORT_SYMBOL(seq_pad);
858
859 /* A complete analogue of print_hex_dump() */
860 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
861 int rowsize, int groupsize, const void *buf, size_t len,
862 bool ascii)
863 {
864 const u8 *ptr = buf;
865 int i, linelen, remaining = len;
866 char *buffer;
867 size_t size;
868 int ret;
869
870 if (rowsize != 16 && rowsize != 32)
871 rowsize = 16;
872
873 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
874 linelen = min(remaining, rowsize);
875 remaining -= rowsize;
876
877 switch (prefix_type) {
878 case DUMP_PREFIX_ADDRESS:
879 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
880 break;
881 case DUMP_PREFIX_OFFSET:
882 seq_printf(m, "%s%.8x: ", prefix_str, i);
883 break;
884 default:
885 seq_printf(m, "%s", prefix_str);
886 break;
887 }
888
889 size = seq_get_buf(m, &buffer);
890 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
891 buffer, size, ascii);
892 seq_commit(m, ret < size ? ret : -1);
893
894 seq_putc(m, '\n');
895 }
896 }
897 EXPORT_SYMBOL(seq_hex_dump);
898
899 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
900 {
901 struct list_head *lh;
902
903 list_for_each(lh, head)
904 if (pos-- == 0)
905 return lh;
906
907 return NULL;
908 }
909 EXPORT_SYMBOL(seq_list_start);
910
911 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
912 {
913 if (!pos)
914 return head;
915
916 return seq_list_start(head, pos - 1);
917 }
918 EXPORT_SYMBOL(seq_list_start_head);
919
920 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
921 {
922 struct list_head *lh;
923
924 lh = ((struct list_head *)v)->next;
925 ++*ppos;
926 return lh == head ? NULL : lh;
927 }
928 EXPORT_SYMBOL(seq_list_next);
929
930 /**
931 * seq_hlist_start - start an iteration of a hlist
932 * @head: the head of the hlist
933 * @pos: the start position of the sequence
934 *
935 * Called at seq_file->op->start().
936 */
937 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
938 {
939 struct hlist_node *node;
940
941 hlist_for_each(node, head)
942 if (pos-- == 0)
943 return node;
944 return NULL;
945 }
946 EXPORT_SYMBOL(seq_hlist_start);
947
948 /**
949 * seq_hlist_start_head - start an iteration of a hlist
950 * @head: the head of the hlist
951 * @pos: the start position of the sequence
952 *
953 * Called at seq_file->op->start(). Call this function if you want to
954 * print a header at the top of the output.
955 */
956 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
957 {
958 if (!pos)
959 return SEQ_START_TOKEN;
960
961 return seq_hlist_start(head, pos - 1);
962 }
963 EXPORT_SYMBOL(seq_hlist_start_head);
964
965 /**
966 * seq_hlist_next - move to the next position of the hlist
967 * @v: the current iterator
968 * @head: the head of the hlist
969 * @ppos: the current position
970 *
971 * Called at seq_file->op->next().
972 */
973 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
974 loff_t *ppos)
975 {
976 struct hlist_node *node = v;
977
978 ++*ppos;
979 if (v == SEQ_START_TOKEN)
980 return head->first;
981 else
982 return node->next;
983 }
984 EXPORT_SYMBOL(seq_hlist_next);
985
986 /**
987 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
988 * @head: the head of the hlist
989 * @pos: the start position of the sequence
990 *
991 * Called at seq_file->op->start().
992 *
993 * This list-traversal primitive may safely run concurrently with
994 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
995 * as long as the traversal is guarded by rcu_read_lock().
996 */
997 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
998 loff_t pos)
999 {
1000 struct hlist_node *node;
1001
1002 __hlist_for_each_rcu(node, head)
1003 if (pos-- == 0)
1004 return node;
1005 return NULL;
1006 }
1007 EXPORT_SYMBOL(seq_hlist_start_rcu);
1008
1009 /**
1010 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1011 * @head: the head of the hlist
1012 * @pos: the start position of the sequence
1013 *
1014 * Called at seq_file->op->start(). Call this function if you want to
1015 * print a header at the top of the output.
1016 *
1017 * This list-traversal primitive may safely run concurrently with
1018 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1019 * as long as the traversal is guarded by rcu_read_lock().
1020 */
1021 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1022 loff_t pos)
1023 {
1024 if (!pos)
1025 return SEQ_START_TOKEN;
1026
1027 return seq_hlist_start_rcu(head, pos - 1);
1028 }
1029 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1030
1031 /**
1032 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1033 * @v: the current iterator
1034 * @head: the head of the hlist
1035 * @ppos: the current position
1036 *
1037 * Called at seq_file->op->next().
1038 *
1039 * This list-traversal primitive may safely run concurrently with
1040 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1041 * as long as the traversal is guarded by rcu_read_lock().
1042 */
1043 struct hlist_node *seq_hlist_next_rcu(void *v,
1044 struct hlist_head *head,
1045 loff_t *ppos)
1046 {
1047 struct hlist_node *node = v;
1048
1049 ++*ppos;
1050 if (v == SEQ_START_TOKEN)
1051 return rcu_dereference(head->first);
1052 else
1053 return rcu_dereference(node->next);
1054 }
1055 EXPORT_SYMBOL(seq_hlist_next_rcu);
1056
1057 /**
1058 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1059 * @head: pointer to percpu array of struct hlist_heads
1060 * @cpu: pointer to cpu "cursor"
1061 * @pos: start position of sequence
1062 *
1063 * Called at seq_file->op->start().
1064 */
1065 struct hlist_node *
1066 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1067 {
1068 struct hlist_node *node;
1069
1070 for_each_possible_cpu(*cpu) {
1071 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1072 if (pos-- == 0)
1073 return node;
1074 }
1075 }
1076 return NULL;
1077 }
1078 EXPORT_SYMBOL(seq_hlist_start_percpu);
1079
1080 /**
1081 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1082 * @v: pointer to current hlist_node
1083 * @head: pointer to percpu array of struct hlist_heads
1084 * @cpu: pointer to cpu "cursor"
1085 * @pos: start position of sequence
1086 *
1087 * Called at seq_file->op->next().
1088 */
1089 struct hlist_node *
1090 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1091 int *cpu, loff_t *pos)
1092 {
1093 struct hlist_node *node = v;
1094
1095 ++*pos;
1096
1097 if (node->next)
1098 return node->next;
1099
1100 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1101 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1102 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1103
1104 if (!hlist_empty(bucket))
1105 return bucket->first;
1106 }
1107 return NULL;
1108 }
1109 EXPORT_SYMBOL(seq_hlist_next_percpu);
1110
1111 void __init seq_file_init(void)
1112 {
1113 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
1114 }