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