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