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