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