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