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