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