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