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