Linux 3.18.86
[linux/fpc-iii.git] / fs / seq_file.c
blobfbb1688bff87c081ab01c58f6bcbc0a615775670
1 /*
2 * linux/fs/seq_file.c
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
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>
16 #include <asm/uaccess.h>
17 #include <asm/page.h>
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.
25 static bool seq_overflow(struct seq_file *m)
27 return m->count == m->size;
30 static void seq_set_overflow(struct seq_file *m)
32 m->count = m->size;
35 static void *seq_buf_alloc(unsigned long size)
37 void *buf;
39 buf = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
40 if (!buf && size > PAGE_SIZE)
41 buf = vmalloc(size);
42 return buf;
45 /**
46 * seq_open - initialize sequential file
47 * @file: file we initialize
48 * @op: method table describing the sequence
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".
59 int seq_open(struct file *file, const struct seq_operations *op)
61 struct seq_file *p = file->private_data;
63 if (!p) {
64 p = kmalloc(sizeof(*p), GFP_KERNEL);
65 if (!p)
66 return -ENOMEM;
67 file->private_data = p;
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
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.
81 file->f_version = 0;
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.
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.
92 file->f_mode &= ~FMODE_PWRITE;
93 return 0;
95 EXPORT_SYMBOL(seq_open);
97 static int traverse(struct seq_file *m, loff_t offset)
99 loff_t pos = 0, index;
100 int error = 0;
101 void *p;
103 m->version = 0;
104 index = 0;
105 m->count = m->from = 0;
106 if (!offset) {
107 m->index = index;
108 return 0;
110 if (!m->buf) {
111 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
112 if (!m->buf)
113 return -ENOMEM;
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;
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;
135 pos += m->count;
136 m->count = 0;
137 if (pos == offset) {
138 index++;
139 m->index = index;
140 break;
142 p = m->op->next(m, p, &index);
144 m->op->stop(m, p);
145 m->index = index;
146 return error;
148 Eoverflow:
149 m->op->stop(m, p);
150 kvfree(m->buf);
151 m->count = 0;
152 m->buf = seq_buf_alloc(m->size <<= 1);
153 return !m->buf ? -ENOMEM : -EAGAIN;
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
163 * Ready-made ->f_op->read()
165 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
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;
174 mutex_lock(&m->lock);
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.
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.
187 m->version = file->f_version;
189 /* Don't assume *ppos is where we left it */
190 if (unlikely(*ppos != m->read_pos)) {
191 while ((err = traverse(m, *ppos)) == -EAGAIN)
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;
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;
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->from = 0;
224 m->index++;
226 if (!size)
227 goto Done;
229 /* we need at least one record in buffer */
230 pos = m->index;
231 p = m->op->start(m, &pos);
232 while (1) {
233 err = PTR_ERR(p);
234 if (!p || IS_ERR(p))
235 break;
236 err = m->op->show(m, p);
237 if (err < 0)
238 break;
239 if (unlikely(err))
240 m->count = 0;
241 if (unlikely(!m->count)) {
242 p = m->op->next(m, p, &pos);
243 m->index = pos;
244 continue;
246 if (m->count < m->size)
247 goto Fill;
248 m->op->stop(m, p);
249 kvfree(m->buf);
250 m->count = 0;
251 m->buf = seq_buf_alloc(m->size <<= 1);
252 if (!m->buf)
253 goto Enomem;
254 m->version = 0;
255 pos = m->index;
256 p = m->op->start(m, &pos);
258 m->op->stop(m, p);
259 m->count = 0;
260 goto Done;
261 Fill:
262 /* they want more? let's try to get some more */
263 while (m->count < size) {
264 size_t offs = m->count;
265 loff_t next = pos;
266 p = m->op->next(m, p, &next);
267 if (!p || IS_ERR(p)) {
268 err = PTR_ERR(p);
269 break;
271 err = m->op->show(m, p);
272 if (seq_overflow(m) || err) {
273 m->count = offs;
274 if (likely(err <= 0))
275 break;
277 pos = next;
279 m->op->stop(m, p);
280 n = min(m->count, size);
281 err = copy_to_user(buf, m->buf, n);
282 if (err)
283 goto Efault;
284 copied += n;
285 m->count -= n;
286 if (m->count)
287 m->from = n;
288 else
289 pos++;
290 m->index = pos;
291 Done:
292 if (!copied)
293 copied = err;
294 else {
295 *ppos += copied;
296 m->read_pos += copied;
298 file->f_version = m->version;
299 mutex_unlock(&m->lock);
300 return copied;
301 Enomem:
302 err = -ENOMEM;
303 goto Done;
304 Efault:
305 err = -EFAULT;
306 goto Done;
308 EXPORT_SYMBOL(seq_read);
311 * seq_lseek - ->llseek() method for sequential files.
312 * @file: the file in question
313 * @offset: new position
314 * @whence: 0 for absolute, 1 for relative position
316 * Ready-made ->f_op->llseek()
318 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
320 struct seq_file *m = file->private_data;
321 loff_t retval = -EINVAL;
323 mutex_lock(&m->lock);
324 m->version = file->f_version;
325 switch (whence) {
326 case SEEK_CUR:
327 offset += file->f_pos;
328 case SEEK_SET:
329 if (offset < 0)
330 break;
331 retval = offset;
332 if (offset != m->read_pos) {
333 while ((retval = traverse(m, offset)) == -EAGAIN)
335 if (retval) {
336 /* with extreme prejudice... */
337 file->f_pos = 0;
338 m->read_pos = 0;
339 m->version = 0;
340 m->index = 0;
341 m->count = 0;
342 } else {
343 m->read_pos = offset;
344 retval = file->f_pos = offset;
346 } else {
347 file->f_pos = offset;
350 file->f_version = m->version;
351 mutex_unlock(&m->lock);
352 return retval;
354 EXPORT_SYMBOL(seq_lseek);
357 * seq_release - free the structures associated with sequential file.
358 * @file: file in question
359 * @inode: its inode
361 * Frees the structures associated with sequential file; can be used
362 * as ->f_op->release() if you don't have private data to destroy.
364 int seq_release(struct inode *inode, struct file *file)
366 struct seq_file *m = file->private_data;
367 kvfree(m->buf);
368 kfree(m);
369 return 0;
371 EXPORT_SYMBOL(seq_release);
374 * seq_escape - print string into buffer, escaping some characters
375 * @m: target buffer
376 * @s: string
377 * @esc: set of characters that need escaping
379 * Puts string into buffer, replacing each occurrence of character from
380 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
381 * case of overflow.
383 int seq_escape(struct seq_file *m, const char *s, const char *esc)
385 char *end = m->buf + m->size;
386 char *p;
387 char c;
389 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
390 if (!strchr(esc, c)) {
391 *p++ = c;
392 continue;
394 if (p + 3 < end) {
395 *p++ = '\\';
396 *p++ = '0' + ((c & 0300) >> 6);
397 *p++ = '0' + ((c & 070) >> 3);
398 *p++ = '0' + (c & 07);
399 continue;
401 seq_set_overflow(m);
402 return -1;
404 m->count = p - m->buf;
405 return 0;
407 EXPORT_SYMBOL(seq_escape);
409 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
411 int len;
413 if (m->count < m->size) {
414 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
415 if (m->count + len < m->size) {
416 m->count += len;
417 return 0;
420 seq_set_overflow(m);
421 return -1;
423 EXPORT_SYMBOL(seq_vprintf);
425 int seq_printf(struct seq_file *m, const char *f, ...)
427 int ret;
428 va_list args;
430 va_start(args, f);
431 ret = seq_vprintf(m, f, args);
432 va_end(args);
434 return ret;
436 EXPORT_SYMBOL(seq_printf);
439 * mangle_path - mangle and copy path to buffer beginning
440 * @s: buffer start
441 * @p: beginning of path in above buffer
442 * @esc: set of characters that need escaping
444 * Copy the path from @p to @s, replacing each occurrence of character from
445 * @esc with usual octal escape.
446 * Returns pointer past last written character in @s, or NULL in case of
447 * failure.
449 char *mangle_path(char *s, const char *p, const char *esc)
451 while (s <= p) {
452 char c = *p++;
453 if (!c) {
454 return s;
455 } else if (!strchr(esc, c)) {
456 *s++ = c;
457 } else if (s + 4 > p) {
458 break;
459 } else {
460 *s++ = '\\';
461 *s++ = '0' + ((c & 0300) >> 6);
462 *s++ = '0' + ((c & 070) >> 3);
463 *s++ = '0' + (c & 07);
466 return NULL;
468 EXPORT_SYMBOL(mangle_path);
471 * seq_path - seq_file interface to print a pathname
472 * @m: the seq_file handle
473 * @path: the struct path to print
474 * @esc: set of characters to escape in the output
476 * return the absolute path of 'path', as represented by the
477 * dentry / mnt pair in the path parameter.
479 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
481 char *buf;
482 size_t size = seq_get_buf(m, &buf);
483 int res = -1;
485 if (size) {
486 char *p = d_path(path, buf, size);
487 if (!IS_ERR(p)) {
488 char *end = mangle_path(buf, p, esc);
489 if (end)
490 res = end - buf;
493 seq_commit(m, res);
495 return res;
497 EXPORT_SYMBOL(seq_path);
500 * Same as seq_path, but relative to supplied root.
502 int seq_path_root(struct seq_file *m, const struct path *path,
503 const struct path *root, const char *esc)
505 char *buf;
506 size_t size = seq_get_buf(m, &buf);
507 int res = -ENAMETOOLONG;
509 if (size) {
510 char *p;
512 p = __d_path(path, root, buf, size);
513 if (!p)
514 return SEQ_SKIP;
515 res = PTR_ERR(p);
516 if (!IS_ERR(p)) {
517 char *end = mangle_path(buf, p, esc);
518 if (end)
519 res = end - buf;
520 else
521 res = -ENAMETOOLONG;
524 seq_commit(m, res);
526 return res < 0 && res != -ENAMETOOLONG ? res : 0;
530 * returns the path of the 'dentry' from the root of its filesystem.
532 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
534 char *buf;
535 size_t size = seq_get_buf(m, &buf);
536 int res = -1;
538 if (size) {
539 char *p = dentry_path(dentry, buf, size);
540 if (!IS_ERR(p)) {
541 char *end = mangle_path(buf, p, esc);
542 if (end)
543 res = end - buf;
546 seq_commit(m, res);
548 return res;
551 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
552 unsigned int nr_bits)
554 if (m->count < m->size) {
555 int len = bitmap_scnprintf(m->buf + m->count,
556 m->size - m->count, bits, nr_bits);
557 if (m->count + len < m->size) {
558 m->count += len;
559 return 0;
562 seq_set_overflow(m);
563 return -1;
565 EXPORT_SYMBOL(seq_bitmap);
567 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
568 unsigned int nr_bits)
570 if (m->count < m->size) {
571 int len = bitmap_scnlistprintf(m->buf + m->count,
572 m->size - m->count, bits, nr_bits);
573 if (m->count + len < m->size) {
574 m->count += len;
575 return 0;
578 seq_set_overflow(m);
579 return -1;
581 EXPORT_SYMBOL(seq_bitmap_list);
583 static void *single_start(struct seq_file *p, loff_t *pos)
585 return NULL + (*pos == 0);
588 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
590 ++*pos;
591 return NULL;
594 static void single_stop(struct seq_file *p, void *v)
598 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
599 void *data)
601 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
602 int res = -ENOMEM;
604 if (op) {
605 op->start = single_start;
606 op->next = single_next;
607 op->stop = single_stop;
608 op->show = show;
609 res = seq_open(file, op);
610 if (!res)
611 ((struct seq_file *)file->private_data)->private = data;
612 else
613 kfree(op);
615 return res;
617 EXPORT_SYMBOL(single_open);
619 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
620 void *data, size_t size)
622 char *buf = seq_buf_alloc(size);
623 int ret;
624 if (!buf)
625 return -ENOMEM;
626 ret = single_open(file, show, data);
627 if (ret) {
628 kvfree(buf);
629 return ret;
631 ((struct seq_file *)file->private_data)->buf = buf;
632 ((struct seq_file *)file->private_data)->size = size;
633 return 0;
635 EXPORT_SYMBOL(single_open_size);
637 int single_release(struct inode *inode, struct file *file)
639 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
640 int res = seq_release(inode, file);
641 kfree(op);
642 return res;
644 EXPORT_SYMBOL(single_release);
646 int seq_release_private(struct inode *inode, struct file *file)
648 struct seq_file *seq = file->private_data;
650 kfree(seq->private);
651 seq->private = NULL;
652 return seq_release(inode, file);
654 EXPORT_SYMBOL(seq_release_private);
656 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
657 int psize)
659 int rc;
660 void *private;
661 struct seq_file *seq;
663 private = kzalloc(psize, GFP_KERNEL);
664 if (private == NULL)
665 goto out;
667 rc = seq_open(f, ops);
668 if (rc < 0)
669 goto out_free;
671 seq = f->private_data;
672 seq->private = private;
673 return private;
675 out_free:
676 kfree(private);
677 out:
678 return NULL;
680 EXPORT_SYMBOL(__seq_open_private);
682 int seq_open_private(struct file *filp, const struct seq_operations *ops,
683 int psize)
685 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
687 EXPORT_SYMBOL(seq_open_private);
689 int seq_putc(struct seq_file *m, char c)
691 if (m->count < m->size) {
692 m->buf[m->count++] = c;
693 return 0;
695 return -1;
697 EXPORT_SYMBOL(seq_putc);
699 int seq_puts(struct seq_file *m, const char *s)
701 int len = strlen(s);
702 if (m->count + len < m->size) {
703 memcpy(m->buf + m->count, s, len);
704 m->count += len;
705 return 0;
707 seq_set_overflow(m);
708 return -1;
710 EXPORT_SYMBOL(seq_puts);
713 * A helper routine for putting decimal numbers without rich format of printf().
714 * only 'unsigned long long' is supported.
715 * This routine will put one byte delimiter + number into seq_file.
716 * This routine is very quick when you show lots of numbers.
717 * In usual cases, it will be better to use seq_printf(). It's easier to read.
719 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
720 unsigned long long num)
722 int len;
724 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
725 goto overflow;
727 if (delimiter)
728 m->buf[m->count++] = delimiter;
730 if (num < 10) {
731 m->buf[m->count++] = num + '0';
732 return 0;
735 len = num_to_str(m->buf + m->count, m->size - m->count, num);
736 if (!len)
737 goto overflow;
738 m->count += len;
739 return 0;
740 overflow:
741 seq_set_overflow(m);
742 return -1;
744 EXPORT_SYMBOL(seq_put_decimal_ull);
746 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
747 long long num)
749 if (num < 0) {
750 if (m->count + 3 >= m->size) {
751 seq_set_overflow(m);
752 return -1;
754 if (delimiter)
755 m->buf[m->count++] = delimiter;
756 num = -num;
757 delimiter = '-';
759 return seq_put_decimal_ull(m, delimiter, num);
762 EXPORT_SYMBOL(seq_put_decimal_ll);
765 * seq_write - write arbitrary data to buffer
766 * @seq: seq_file identifying the buffer to which data should be written
767 * @data: data address
768 * @len: number of bytes
770 * Return 0 on success, non-zero otherwise.
772 int seq_write(struct seq_file *seq, const void *data, size_t len)
774 if (seq->count + len < seq->size) {
775 memcpy(seq->buf + seq->count, data, len);
776 seq->count += len;
777 return 0;
779 seq_set_overflow(seq);
780 return -1;
782 EXPORT_SYMBOL(seq_write);
785 * seq_pad - write padding spaces to buffer
786 * @m: seq_file identifying the buffer to which data should be written
787 * @c: the byte to append after padding if non-zero
789 void seq_pad(struct seq_file *m, char c)
791 int size = m->pad_until - m->count;
792 if (size > 0)
793 seq_printf(m, "%*s", size, "");
794 if (c)
795 seq_putc(m, c);
797 EXPORT_SYMBOL(seq_pad);
799 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
801 struct list_head *lh;
803 list_for_each(lh, head)
804 if (pos-- == 0)
805 return lh;
807 return NULL;
809 EXPORT_SYMBOL(seq_list_start);
811 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
813 if (!pos)
814 return head;
816 return seq_list_start(head, pos - 1);
818 EXPORT_SYMBOL(seq_list_start_head);
820 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
822 struct list_head *lh;
824 lh = ((struct list_head *)v)->next;
825 ++*ppos;
826 return lh == head ? NULL : lh;
828 EXPORT_SYMBOL(seq_list_next);
831 * seq_hlist_start - start an iteration of a hlist
832 * @head: the head of the hlist
833 * @pos: the start position of the sequence
835 * Called at seq_file->op->start().
837 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
839 struct hlist_node *node;
841 hlist_for_each(node, head)
842 if (pos-- == 0)
843 return node;
844 return NULL;
846 EXPORT_SYMBOL(seq_hlist_start);
849 * seq_hlist_start_head - start an iteration of a hlist
850 * @head: the head of the hlist
851 * @pos: the start position of the sequence
853 * Called at seq_file->op->start(). Call this function if you want to
854 * print a header at the top of the output.
856 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
858 if (!pos)
859 return SEQ_START_TOKEN;
861 return seq_hlist_start(head, pos - 1);
863 EXPORT_SYMBOL(seq_hlist_start_head);
866 * seq_hlist_next - move to the next position of the hlist
867 * @v: the current iterator
868 * @head: the head of the hlist
869 * @ppos: the current position
871 * Called at seq_file->op->next().
873 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
874 loff_t *ppos)
876 struct hlist_node *node = v;
878 ++*ppos;
879 if (v == SEQ_START_TOKEN)
880 return head->first;
881 else
882 return node->next;
884 EXPORT_SYMBOL(seq_hlist_next);
887 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
888 * @head: the head of the hlist
889 * @pos: the start position of the sequence
891 * Called at seq_file->op->start().
893 * This list-traversal primitive may safely run concurrently with
894 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
895 * as long as the traversal is guarded by rcu_read_lock().
897 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
898 loff_t pos)
900 struct hlist_node *node;
902 __hlist_for_each_rcu(node, head)
903 if (pos-- == 0)
904 return node;
905 return NULL;
907 EXPORT_SYMBOL(seq_hlist_start_rcu);
910 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
911 * @head: the head of the hlist
912 * @pos: the start position of the sequence
914 * Called at seq_file->op->start(). Call this function if you want to
915 * print a header at the top of the output.
917 * This list-traversal primitive may safely run concurrently with
918 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
919 * as long as the traversal is guarded by rcu_read_lock().
921 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
922 loff_t pos)
924 if (!pos)
925 return SEQ_START_TOKEN;
927 return seq_hlist_start_rcu(head, pos - 1);
929 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
932 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
933 * @v: the current iterator
934 * @head: the head of the hlist
935 * @ppos: the current position
937 * Called at seq_file->op->next().
939 * This list-traversal primitive may safely run concurrently with
940 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
941 * as long as the traversal is guarded by rcu_read_lock().
943 struct hlist_node *seq_hlist_next_rcu(void *v,
944 struct hlist_head *head,
945 loff_t *ppos)
947 struct hlist_node *node = v;
949 ++*ppos;
950 if (v == SEQ_START_TOKEN)
951 return rcu_dereference(head->first);
952 else
953 return rcu_dereference(node->next);
955 EXPORT_SYMBOL(seq_hlist_next_rcu);
958 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
959 * @head: pointer to percpu array of struct hlist_heads
960 * @cpu: pointer to cpu "cursor"
961 * @pos: start position of sequence
963 * Called at seq_file->op->start().
965 struct hlist_node *
966 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
968 struct hlist_node *node;
970 for_each_possible_cpu(*cpu) {
971 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
972 if (pos-- == 0)
973 return node;
976 return NULL;
978 EXPORT_SYMBOL(seq_hlist_start_percpu);
981 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
982 * @v: pointer to current hlist_node
983 * @head: pointer to percpu array of struct hlist_heads
984 * @cpu: pointer to cpu "cursor"
985 * @pos: start position of sequence
987 * Called at seq_file->op->next().
989 struct hlist_node *
990 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
991 int *cpu, loff_t *pos)
993 struct hlist_node *node = v;
995 ++*pos;
997 if (node->next)
998 return node->next;
1000 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1001 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1002 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1004 if (!hlist_empty(bucket))
1005 return bucket->first;
1007 return NULL;
1009 EXPORT_SYMBOL(seq_hlist_next_percpu);