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 
seq_set_overflow(struct seq_file * m)25 static void seq_set_overflow(struct seq_file *m)
26 {
27 	m->count = m->size;
28 }
29 
seq_buf_alloc(unsigned long size)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  */
seq_open(struct file * file,const struct seq_operations * op)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 
traverse(struct seq_file * m,loff_t offset)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  */
seq_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)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  */
seq_lseek(struct file * file,loff_t offset,int whence)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 	case SEEK_SET:
324 		if (offset < 0)
325 			break;
326 		retval = offset;
327 		if (offset != m->read_pos) {
328 			while ((retval = traverse(m, offset)) == -EAGAIN)
329 				;
330 			if (retval) {
331 				/* with extreme prejudice... */
332 				file->f_pos = 0;
333 				m->read_pos = 0;
334 				m->version = 0;
335 				m->index = 0;
336 				m->count = 0;
337 			} else {
338 				m->read_pos = offset;
339 				retval = file->f_pos = offset;
340 			}
341 		} else {
342 			file->f_pos = offset;
343 		}
344 	}
345 	file->f_version = m->version;
346 	mutex_unlock(&m->lock);
347 	return retval;
348 }
349 EXPORT_SYMBOL(seq_lseek);
350 
351 /**
352  *	seq_release -	free the structures associated with sequential file.
353  *	@file: file in question
354  *	@inode: its inode
355  *
356  *	Frees the structures associated with sequential file; can be used
357  *	as ->f_op->release() if you don't have private data to destroy.
358  */
seq_release(struct inode * inode,struct file * file)359 int seq_release(struct inode *inode, struct file *file)
360 {
361 	struct seq_file *m = file->private_data;
362 	kvfree(m->buf);
363 	kmem_cache_free(seq_file_cache, m);
364 	return 0;
365 }
366 EXPORT_SYMBOL(seq_release);
367 
368 /**
369  *	seq_escape -	print string into buffer, escaping some characters
370  *	@m:	target buffer
371  *	@s:	string
372  *	@esc:	set of characters that need escaping
373  *
374  *	Puts string into buffer, replacing each occurrence of character from
375  *	@esc with usual octal escape.
376  *	Use seq_has_overflowed() to check for errors.
377  */
seq_escape(struct seq_file * m,const char * s,const char * esc)378 void seq_escape(struct seq_file *m, const char *s, const char *esc)
379 {
380 	char *buf;
381 	size_t size = seq_get_buf(m, &buf);
382 	int ret;
383 
384 	ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
385 	seq_commit(m, ret < size ? ret : -1);
386 }
387 EXPORT_SYMBOL(seq_escape);
388 
seq_vprintf(struct seq_file * m,const char * f,va_list args)389 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
390 {
391 	int len;
392 
393 	if (m->count < m->size) {
394 		len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
395 		if (m->count + len < m->size) {
396 			m->count += len;
397 			return;
398 		}
399 	}
400 	seq_set_overflow(m);
401 }
402 EXPORT_SYMBOL(seq_vprintf);
403 
seq_printf(struct seq_file * m,const char * f,...)404 void seq_printf(struct seq_file *m, const char *f, ...)
405 {
406 	va_list args;
407 
408 	va_start(args, f);
409 	seq_vprintf(m, f, args);
410 	va_end(args);
411 }
412 EXPORT_SYMBOL(seq_printf);
413 
414 /**
415  *	mangle_path -	mangle and copy path to buffer beginning
416  *	@s: buffer start
417  *	@p: beginning of path in above buffer
418  *	@esc: set of characters that need escaping
419  *
420  *      Copy the path from @p to @s, replacing each occurrence of character from
421  *      @esc with usual octal escape.
422  *      Returns pointer past last written character in @s, or NULL in case of
423  *      failure.
424  */
mangle_path(char * s,const char * p,const char * esc)425 char *mangle_path(char *s, const char *p, const char *esc)
426 {
427 	while (s <= p) {
428 		char c = *p++;
429 		if (!c) {
430 			return s;
431 		} else if (!strchr(esc, c)) {
432 			*s++ = c;
433 		} else if (s + 4 > p) {
434 			break;
435 		} else {
436 			*s++ = '\\';
437 			*s++ = '0' + ((c & 0300) >> 6);
438 			*s++ = '0' + ((c & 070) >> 3);
439 			*s++ = '0' + (c & 07);
440 		}
441 	}
442 	return NULL;
443 }
444 EXPORT_SYMBOL(mangle_path);
445 
446 /**
447  * seq_path - seq_file interface to print a pathname
448  * @m: the seq_file handle
449  * @path: the struct path to print
450  * @esc: set of characters to escape in the output
451  *
452  * return the absolute path of 'path', as represented by the
453  * dentry / mnt pair in the path parameter.
454  */
seq_path(struct seq_file * m,const struct path * path,const char * esc)455 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
456 {
457 	char *buf;
458 	size_t size = seq_get_buf(m, &buf);
459 	int res = -1;
460 
461 	if (size) {
462 		char *p = d_path(path, buf, size);
463 		if (!IS_ERR(p)) {
464 			char *end = mangle_path(buf, p, esc);
465 			if (end)
466 				res = end - buf;
467 		}
468 	}
469 	seq_commit(m, res);
470 
471 	return res;
472 }
473 EXPORT_SYMBOL(seq_path);
474 
475 /**
476  * seq_file_path - seq_file interface to print a pathname of a file
477  * @m: the seq_file handle
478  * @file: the struct file to print
479  * @esc: set of characters to escape in the output
480  *
481  * return the absolute path to the file.
482  */
seq_file_path(struct seq_file * m,struct file * file,const char * esc)483 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
484 {
485 	return seq_path(m, &file->f_path, esc);
486 }
487 EXPORT_SYMBOL(seq_file_path);
488 
489 /*
490  * Same as seq_path, but relative to supplied root.
491  */
seq_path_root(struct seq_file * m,const struct path * path,const struct path * root,const char * esc)492 int seq_path_root(struct seq_file *m, const struct path *path,
493 		  const struct path *root, const char *esc)
494 {
495 	char *buf;
496 	size_t size = seq_get_buf(m, &buf);
497 	int res = -ENAMETOOLONG;
498 
499 	if (size) {
500 		char *p;
501 
502 		p = __d_path(path, root, buf, size);
503 		if (!p)
504 			return SEQ_SKIP;
505 		res = PTR_ERR(p);
506 		if (!IS_ERR(p)) {
507 			char *end = mangle_path(buf, p, esc);
508 			if (end)
509 				res = end - buf;
510 			else
511 				res = -ENAMETOOLONG;
512 		}
513 	}
514 	seq_commit(m, res);
515 
516 	return res < 0 && res != -ENAMETOOLONG ? res : 0;
517 }
518 
519 /*
520  * returns the path of the 'dentry' from the root of its filesystem.
521  */
seq_dentry(struct seq_file * m,struct dentry * dentry,const char * esc)522 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
523 {
524 	char *buf;
525 	size_t size = seq_get_buf(m, &buf);
526 	int res = -1;
527 
528 	if (size) {
529 		char *p = dentry_path(dentry, buf, size);
530 		if (!IS_ERR(p)) {
531 			char *end = mangle_path(buf, p, esc);
532 			if (end)
533 				res = end - buf;
534 		}
535 	}
536 	seq_commit(m, res);
537 
538 	return res;
539 }
540 EXPORT_SYMBOL(seq_dentry);
541 
single_start(struct seq_file * p,loff_t * pos)542 static void *single_start(struct seq_file *p, loff_t *pos)
543 {
544 	return NULL + (*pos == 0);
545 }
546 
single_next(struct seq_file * p,void * v,loff_t * pos)547 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
548 {
549 	++*pos;
550 	return NULL;
551 }
552 
single_stop(struct seq_file * p,void * v)553 static void single_stop(struct seq_file *p, void *v)
554 {
555 }
556 
single_open(struct file * file,int (* show)(struct seq_file *,void *),void * data)557 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
558 		void *data)
559 {
560 	struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
561 	int res = -ENOMEM;
562 
563 	if (op) {
564 		op->start = single_start;
565 		op->next = single_next;
566 		op->stop = single_stop;
567 		op->show = show;
568 		res = seq_open(file, op);
569 		if (!res)
570 			((struct seq_file *)file->private_data)->private = data;
571 		else
572 			kfree(op);
573 	}
574 	return res;
575 }
576 EXPORT_SYMBOL(single_open);
577 
single_open_size(struct file * file,int (* show)(struct seq_file *,void *),void * data,size_t size)578 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
579 		void *data, size_t size)
580 {
581 	char *buf = seq_buf_alloc(size);
582 	int ret;
583 	if (!buf)
584 		return -ENOMEM;
585 	ret = single_open(file, show, data);
586 	if (ret) {
587 		kvfree(buf);
588 		return ret;
589 	}
590 	((struct seq_file *)file->private_data)->buf = buf;
591 	((struct seq_file *)file->private_data)->size = size;
592 	return 0;
593 }
594 EXPORT_SYMBOL(single_open_size);
595 
single_release(struct inode * inode,struct file * file)596 int single_release(struct inode *inode, struct file *file)
597 {
598 	const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
599 	int res = seq_release(inode, file);
600 	kfree(op);
601 	return res;
602 }
603 EXPORT_SYMBOL(single_release);
604 
seq_release_private(struct inode * inode,struct file * file)605 int seq_release_private(struct inode *inode, struct file *file)
606 {
607 	struct seq_file *seq = file->private_data;
608 
609 	kfree(seq->private);
610 	seq->private = NULL;
611 	return seq_release(inode, file);
612 }
613 EXPORT_SYMBOL(seq_release_private);
614 
__seq_open_private(struct file * f,const struct seq_operations * ops,int psize)615 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
616 		int psize)
617 {
618 	int rc;
619 	void *private;
620 	struct seq_file *seq;
621 
622 	private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
623 	if (private == NULL)
624 		goto out;
625 
626 	rc = seq_open(f, ops);
627 	if (rc < 0)
628 		goto out_free;
629 
630 	seq = f->private_data;
631 	seq->private = private;
632 	return private;
633 
634 out_free:
635 	kfree(private);
636 out:
637 	return NULL;
638 }
639 EXPORT_SYMBOL(__seq_open_private);
640 
seq_open_private(struct file * filp,const struct seq_operations * ops,int psize)641 int seq_open_private(struct file *filp, const struct seq_operations *ops,
642 		int psize)
643 {
644 	return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
645 }
646 EXPORT_SYMBOL(seq_open_private);
647 
seq_putc(struct seq_file * m,char c)648 void seq_putc(struct seq_file *m, char c)
649 {
650 	if (m->count >= m->size)
651 		return;
652 
653 	m->buf[m->count++] = c;
654 }
655 EXPORT_SYMBOL(seq_putc);
656 
seq_puts(struct seq_file * m,const char * s)657 void seq_puts(struct seq_file *m, const char *s)
658 {
659 	int len = strlen(s);
660 
661 	if (m->count + len >= m->size) {
662 		seq_set_overflow(m);
663 		return;
664 	}
665 	memcpy(m->buf + m->count, s, len);
666 	m->count += len;
667 }
668 EXPORT_SYMBOL(seq_puts);
669 
670 /**
671  * A helper routine for putting decimal numbers without rich format of printf().
672  * only 'unsigned long long' is supported.
673  * @m: seq_file identifying the buffer to which data should be written
674  * @delimiter: a string which is printed before the number
675  * @num: the number
676  * @width: a minimum field width
677  *
678  * This routine will put strlen(delimiter) + number into seq_filed.
679  * This routine is very quick when you show lots of numbers.
680  * In usual cases, it will be better to use seq_printf(). It's easier to read.
681  */
seq_put_decimal_ull_width(struct seq_file * m,const char * delimiter,unsigned long long num,unsigned int width)682 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
683 			 unsigned long long num, unsigned int width)
684 {
685 	int len;
686 
687 	if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
688 		goto overflow;
689 
690 	if (delimiter && delimiter[0]) {
691 		if (delimiter[1] == 0)
692 			seq_putc(m, delimiter[0]);
693 		else
694 			seq_puts(m, delimiter);
695 	}
696 
697 	if (!width)
698 		width = 1;
699 
700 	if (m->count + width >= m->size)
701 		goto overflow;
702 
703 	len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
704 	if (!len)
705 		goto overflow;
706 
707 	m->count += len;
708 	return;
709 
710 overflow:
711 	seq_set_overflow(m);
712 }
713 
seq_put_decimal_ull(struct seq_file * m,const char * delimiter,unsigned long long num)714 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
715 			 unsigned long long num)
716 {
717 	return seq_put_decimal_ull_width(m, delimiter, num, 0);
718 }
719 EXPORT_SYMBOL(seq_put_decimal_ull);
720 
721 /**
722  * seq_put_hex_ll - put a number in hexadecimal notation
723  * @m: seq_file identifying the buffer to which data should be written
724  * @delimiter: a string which is printed before the number
725  * @v: the number
726  * @width: a minimum field width
727  *
728  * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
729  *
730  * This routine is very quick when you show lots of numbers.
731  * In usual cases, it will be better to use seq_printf(). It's easier to read.
732  */
seq_put_hex_ll(struct seq_file * m,const char * delimiter,unsigned long long v,unsigned int width)733 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
734 				unsigned long long v, unsigned int width)
735 {
736 	unsigned int len;
737 	int i;
738 
739 	if (delimiter && delimiter[0]) {
740 		if (delimiter[1] == 0)
741 			seq_putc(m, delimiter[0]);
742 		else
743 			seq_puts(m, delimiter);
744 	}
745 
746 	/* If x is 0, the result of __builtin_clzll is undefined */
747 	if (v == 0)
748 		len = 1;
749 	else
750 		len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
751 
752 	if (len < width)
753 		len = width;
754 
755 	if (m->count + len > m->size) {
756 		seq_set_overflow(m);
757 		return;
758 	}
759 
760 	for (i = len - 1; i >= 0; i--) {
761 		m->buf[m->count + i] = hex_asc[0xf & v];
762 		v = v >> 4;
763 	}
764 	m->count += len;
765 }
766 
seq_put_decimal_ll(struct seq_file * m,const char * delimiter,long long num)767 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
768 {
769 	int len;
770 
771 	if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
772 		goto overflow;
773 
774 	if (delimiter && delimiter[0]) {
775 		if (delimiter[1] == 0)
776 			seq_putc(m, delimiter[0]);
777 		else
778 			seq_puts(m, delimiter);
779 	}
780 
781 	if (m->count + 2 >= m->size)
782 		goto overflow;
783 
784 	if (num < 0) {
785 		m->buf[m->count++] = '-';
786 		num = -num;
787 	}
788 
789 	if (num < 10) {
790 		m->buf[m->count++] = num + '0';
791 		return;
792 	}
793 
794 	len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
795 	if (!len)
796 		goto overflow;
797 
798 	m->count += len;
799 	return;
800 
801 overflow:
802 	seq_set_overflow(m);
803 }
804 EXPORT_SYMBOL(seq_put_decimal_ll);
805 
806 /**
807  * seq_write - write arbitrary data to buffer
808  * @seq: seq_file identifying the buffer to which data should be written
809  * @data: data address
810  * @len: number of bytes
811  *
812  * Return 0 on success, non-zero otherwise.
813  */
seq_write(struct seq_file * seq,const void * data,size_t len)814 int seq_write(struct seq_file *seq, const void *data, size_t len)
815 {
816 	if (seq->count + len < seq->size) {
817 		memcpy(seq->buf + seq->count, data, len);
818 		seq->count += len;
819 		return 0;
820 	}
821 	seq_set_overflow(seq);
822 	return -1;
823 }
824 EXPORT_SYMBOL(seq_write);
825 
826 /**
827  * seq_pad - write padding spaces to buffer
828  * @m: seq_file identifying the buffer to which data should be written
829  * @c: the byte to append after padding if non-zero
830  */
seq_pad(struct seq_file * m,char c)831 void seq_pad(struct seq_file *m, char c)
832 {
833 	int size = m->pad_until - m->count;
834 	if (size > 0) {
835 		if (size + m->count > m->size) {
836 			seq_set_overflow(m);
837 			return;
838 		}
839 		memset(m->buf + m->count, ' ', size);
840 		m->count += size;
841 	}
842 	if (c)
843 		seq_putc(m, c);
844 }
845 EXPORT_SYMBOL(seq_pad);
846 
847 /* A complete analogue of print_hex_dump() */
seq_hex_dump(struct seq_file * m,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)848 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
849 		  int rowsize, int groupsize, const void *buf, size_t len,
850 		  bool ascii)
851 {
852 	const u8 *ptr = buf;
853 	int i, linelen, remaining = len;
854 	char *buffer;
855 	size_t size;
856 	int ret;
857 
858 	if (rowsize != 16 && rowsize != 32)
859 		rowsize = 16;
860 
861 	for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
862 		linelen = min(remaining, rowsize);
863 		remaining -= rowsize;
864 
865 		switch (prefix_type) {
866 		case DUMP_PREFIX_ADDRESS:
867 			seq_printf(m, "%s%p: ", prefix_str, ptr + i);
868 			break;
869 		case DUMP_PREFIX_OFFSET:
870 			seq_printf(m, "%s%.8x: ", prefix_str, i);
871 			break;
872 		default:
873 			seq_printf(m, "%s", prefix_str);
874 			break;
875 		}
876 
877 		size = seq_get_buf(m, &buffer);
878 		ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
879 					 buffer, size, ascii);
880 		seq_commit(m, ret < size ? ret : -1);
881 
882 		seq_putc(m, '\n');
883 	}
884 }
885 EXPORT_SYMBOL(seq_hex_dump);
886 
seq_list_start(struct list_head * head,loff_t pos)887 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
888 {
889 	struct list_head *lh;
890 
891 	list_for_each(lh, head)
892 		if (pos-- == 0)
893 			return lh;
894 
895 	return NULL;
896 }
897 EXPORT_SYMBOL(seq_list_start);
898 
seq_list_start_head(struct list_head * head,loff_t pos)899 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
900 {
901 	if (!pos)
902 		return head;
903 
904 	return seq_list_start(head, pos - 1);
905 }
906 EXPORT_SYMBOL(seq_list_start_head);
907 
seq_list_next(void * v,struct list_head * head,loff_t * ppos)908 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
909 {
910 	struct list_head *lh;
911 
912 	lh = ((struct list_head *)v)->next;
913 	++*ppos;
914 	return lh == head ? NULL : lh;
915 }
916 EXPORT_SYMBOL(seq_list_next);
917 
918 /**
919  * seq_hlist_start - start an iteration of a hlist
920  * @head: the head of the hlist
921  * @pos:  the start position of the sequence
922  *
923  * Called at seq_file->op->start().
924  */
seq_hlist_start(struct hlist_head * head,loff_t pos)925 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
926 {
927 	struct hlist_node *node;
928 
929 	hlist_for_each(node, head)
930 		if (pos-- == 0)
931 			return node;
932 	return NULL;
933 }
934 EXPORT_SYMBOL(seq_hlist_start);
935 
936 /**
937  * seq_hlist_start_head - start an iteration of a hlist
938  * @head: the head of the hlist
939  * @pos:  the start position of the sequence
940  *
941  * Called at seq_file->op->start(). Call this function if you want to
942  * print a header at the top of the output.
943  */
seq_hlist_start_head(struct hlist_head * head,loff_t pos)944 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
945 {
946 	if (!pos)
947 		return SEQ_START_TOKEN;
948 
949 	return seq_hlist_start(head, pos - 1);
950 }
951 EXPORT_SYMBOL(seq_hlist_start_head);
952 
953 /**
954  * seq_hlist_next - move to the next position of the hlist
955  * @v:    the current iterator
956  * @head: the head of the hlist
957  * @ppos: the current position
958  *
959  * Called at seq_file->op->next().
960  */
seq_hlist_next(void * v,struct hlist_head * head,loff_t * ppos)961 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
962 				  loff_t *ppos)
963 {
964 	struct hlist_node *node = v;
965 
966 	++*ppos;
967 	if (v == SEQ_START_TOKEN)
968 		return head->first;
969 	else
970 		return node->next;
971 }
972 EXPORT_SYMBOL(seq_hlist_next);
973 
974 /**
975  * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
976  * @head: the head of the hlist
977  * @pos:  the start position of the sequence
978  *
979  * Called at seq_file->op->start().
980  *
981  * This list-traversal primitive may safely run concurrently with
982  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
983  * as long as the traversal is guarded by rcu_read_lock().
984  */
seq_hlist_start_rcu(struct hlist_head * head,loff_t pos)985 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
986 				       loff_t pos)
987 {
988 	struct hlist_node *node;
989 
990 	__hlist_for_each_rcu(node, head)
991 		if (pos-- == 0)
992 			return node;
993 	return NULL;
994 }
995 EXPORT_SYMBOL(seq_hlist_start_rcu);
996 
997 /**
998  * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
999  * @head: the head of the hlist
1000  * @pos:  the start position of the sequence
1001  *
1002  * Called at seq_file->op->start(). Call this function if you want to
1003  * print a header at the top of the output.
1004  *
1005  * This list-traversal primitive may safely run concurrently with
1006  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1007  * as long as the traversal is guarded by rcu_read_lock().
1008  */
seq_hlist_start_head_rcu(struct hlist_head * head,loff_t pos)1009 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1010 					    loff_t pos)
1011 {
1012 	if (!pos)
1013 		return SEQ_START_TOKEN;
1014 
1015 	return seq_hlist_start_rcu(head, pos - 1);
1016 }
1017 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1018 
1019 /**
1020  * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1021  * @v:    the current iterator
1022  * @head: the head of the hlist
1023  * @ppos: the current position
1024  *
1025  * Called at seq_file->op->next().
1026  *
1027  * This list-traversal primitive may safely run concurrently with
1028  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1029  * as long as the traversal is guarded by rcu_read_lock().
1030  */
seq_hlist_next_rcu(void * v,struct hlist_head * head,loff_t * ppos)1031 struct hlist_node *seq_hlist_next_rcu(void *v,
1032 				      struct hlist_head *head,
1033 				      loff_t *ppos)
1034 {
1035 	struct hlist_node *node = v;
1036 
1037 	++*ppos;
1038 	if (v == SEQ_START_TOKEN)
1039 		return rcu_dereference(head->first);
1040 	else
1041 		return rcu_dereference(node->next);
1042 }
1043 EXPORT_SYMBOL(seq_hlist_next_rcu);
1044 
1045 /**
1046  * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1047  * @head: pointer to percpu array of struct hlist_heads
1048  * @cpu:  pointer to cpu "cursor"
1049  * @pos:  start position of sequence
1050  *
1051  * Called at seq_file->op->start().
1052  */
1053 struct hlist_node *
seq_hlist_start_percpu(struct hlist_head __percpu * head,int * cpu,loff_t pos)1054 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1055 {
1056 	struct hlist_node *node;
1057 
1058 	for_each_possible_cpu(*cpu) {
1059 		hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1060 			if (pos-- == 0)
1061 				return node;
1062 		}
1063 	}
1064 	return NULL;
1065 }
1066 EXPORT_SYMBOL(seq_hlist_start_percpu);
1067 
1068 /**
1069  * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1070  * @v:    pointer to current hlist_node
1071  * @head: pointer to percpu array of struct hlist_heads
1072  * @cpu:  pointer to cpu "cursor"
1073  * @pos:  start position of sequence
1074  *
1075  * Called at seq_file->op->next().
1076  */
1077 struct hlist_node *
seq_hlist_next_percpu(void * v,struct hlist_head __percpu * head,int * cpu,loff_t * pos)1078 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1079 			int *cpu, loff_t *pos)
1080 {
1081 	struct hlist_node *node = v;
1082 
1083 	++*pos;
1084 
1085 	if (node->next)
1086 		return node->next;
1087 
1088 	for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1089 	     *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1090 		struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1091 
1092 		if (!hlist_empty(bucket))
1093 			return bucket->first;
1094 	}
1095 	return NULL;
1096 }
1097 EXPORT_SYMBOL(seq_hlist_next_percpu);
1098 
seq_file_init(void)1099 void __init seq_file_init(void)
1100 {
1101 	seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
1102 }
1103