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