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