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