Linux 3.12.70
[linux/fpc-iii.git] / fs / seq_file.c
bloba1648936a42b3b946cd9912aae9842b1aeccd33d
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;
38 gfp_t gfp = GFP_KERNEL;
41 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
42 * it's better to fall back to vmalloc() than to kill things. For small
43 * allocations, just use GFP_KERNEL which will oom kill, thus no need
44 * for vmalloc fallback.
46 if (size > PAGE_SIZE)
47 gfp |= __GFP_NORETRY | __GFP_NOWARN;
48 buf = kmalloc(size, gfp);
49 if (!buf && size > PAGE_SIZE)
50 buf = vmalloc(size);
51 return buf;
54 /**
55 * seq_open - initialize sequential file
56 * @file: file we initialize
57 * @op: method table describing the sequence
59 * seq_open() sets @file, associating it with a sequence described
60 * by @op. @op->start() sets the iterator up and returns the first
61 * element of sequence. @op->stop() shuts it down. @op->next()
62 * returns the next element of sequence. @op->show() prints element
63 * into the buffer. In case of error ->start() and ->next() return
64 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
65 * returns 0 in case of success and negative number in case of error.
66 * Returning SEQ_SKIP means "discard this element and move on".
68 int seq_open(struct file *file, const struct seq_operations *op)
70 struct seq_file *p = file->private_data;
72 if (!p) {
73 p = kmalloc(sizeof(*p), GFP_KERNEL);
74 if (!p)
75 return -ENOMEM;
76 file->private_data = p;
78 memset(p, 0, sizeof(*p));
79 mutex_init(&p->lock);
80 p->op = op;
81 #ifdef CONFIG_USER_NS
82 p->user_ns = file->f_cred->user_ns;
83 #endif
86 * Wrappers around seq_open(e.g. swaps_open) need to be
87 * aware of this. If they set f_version themselves, they
88 * should call seq_open first and then set f_version.
90 file->f_version = 0;
93 * seq_files support lseek() and pread(). They do not implement
94 * write() at all, but we clear FMODE_PWRITE here for historical
95 * reasons.
97 * If a client of seq_files a) implements file.write() and b) wishes to
98 * support pwrite() then that client will need to implement its own
99 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
101 file->f_mode &= ~FMODE_PWRITE;
102 return 0;
104 EXPORT_SYMBOL(seq_open);
106 static int traverse(struct seq_file *m, loff_t offset)
108 loff_t pos = 0, index;
109 int error = 0;
110 void *p;
112 m->version = 0;
113 index = 0;
114 m->count = m->from = 0;
115 if (!offset) {
116 m->index = index;
117 return 0;
119 if (!m->buf) {
120 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
121 if (!m->buf)
122 return -ENOMEM;
124 p = m->op->start(m, &index);
125 while (p) {
126 error = PTR_ERR(p);
127 if (IS_ERR(p))
128 break;
129 error = m->op->show(m, p);
130 if (error < 0)
131 break;
132 if (unlikely(error)) {
133 error = 0;
134 m->count = 0;
136 if (seq_overflow(m))
137 goto Eoverflow;
138 if (pos + m->count > offset) {
139 m->from = offset - pos;
140 m->count -= m->from;
141 m->index = index;
142 break;
144 pos += m->count;
145 m->count = 0;
146 if (pos == offset) {
147 index++;
148 m->index = index;
149 break;
151 p = m->op->next(m, p, &index);
153 m->op->stop(m, p);
154 m->index = index;
155 return error;
157 Eoverflow:
158 m->op->stop(m, p);
159 kvfree(m->buf);
160 m->count = 0;
161 m->buf = seq_buf_alloc(m->size <<= 1);
162 return !m->buf ? -ENOMEM : -EAGAIN;
166 * seq_read - ->read() method for sequential files.
167 * @file: the file to read from
168 * @buf: the buffer to read to
169 * @size: the maximum number of bytes to read
170 * @ppos: the current position in the file
172 * Ready-made ->f_op->read()
174 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
176 struct seq_file *m = file->private_data;
177 size_t copied = 0;
178 loff_t pos;
179 size_t n;
180 void *p;
181 int err = 0;
183 mutex_lock(&m->lock);
186 * seq_file->op->..m_start/m_stop/m_next may do special actions
187 * or optimisations based on the file->f_version, so we want to
188 * pass the file->f_version to those methods.
190 * seq_file->version is just copy of f_version, and seq_file
191 * methods can treat it simply as file version.
192 * It is copied in first and copied out after all operations.
193 * It is convenient to have it as part of structure to avoid the
194 * need of passing another argument to all the seq_file methods.
196 m->version = file->f_version;
198 /* Don't assume *ppos is where we left it */
199 if (unlikely(*ppos != m->read_pos)) {
200 while ((err = traverse(m, *ppos)) == -EAGAIN)
202 if (err) {
203 /* With prejudice... */
204 m->read_pos = 0;
205 m->version = 0;
206 m->index = 0;
207 m->count = 0;
208 goto Done;
209 } else {
210 m->read_pos = *ppos;
214 /* grab buffer if we didn't have one */
215 if (!m->buf) {
216 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
217 if (!m->buf)
218 goto Enomem;
220 /* if not empty - flush it first */
221 if (m->count) {
222 n = min(m->count, size);
223 err = copy_to_user(buf, m->buf + m->from, n);
224 if (err)
225 goto Efault;
226 m->count -= n;
227 m->from += n;
228 size -= n;
229 buf += n;
230 copied += n;
231 if (!m->count) {
232 m->from = 0;
233 m->index++;
235 if (!size)
236 goto Done;
238 /* we need at least one record in buffer */
239 pos = m->index;
240 p = m->op->start(m, &pos);
241 while (1) {
242 err = PTR_ERR(p);
243 if (!p || IS_ERR(p))
244 break;
245 err = m->op->show(m, p);
246 if (err < 0)
247 break;
248 if (unlikely(err))
249 m->count = 0;
250 if (unlikely(!m->count)) {
251 p = m->op->next(m, p, &pos);
252 m->index = pos;
253 continue;
255 if (m->count < m->size)
256 goto Fill;
257 m->op->stop(m, p);
258 kvfree(m->buf);
259 m->count = 0;
260 m->buf = seq_buf_alloc(m->size <<= 1);
261 if (!m->buf)
262 goto Enomem;
263 m->version = 0;
264 pos = m->index;
265 p = m->op->start(m, &pos);
267 m->op->stop(m, p);
268 m->count = 0;
269 goto Done;
270 Fill:
271 /* they want more? let's try to get some more */
272 while (m->count < size) {
273 size_t offs = m->count;
274 loff_t next = pos;
275 p = m->op->next(m, p, &next);
276 if (!p || IS_ERR(p)) {
277 err = PTR_ERR(p);
278 break;
280 err = m->op->show(m, p);
281 if (seq_overflow(m) || err) {
282 m->count = offs;
283 if (likely(err <= 0))
284 break;
286 pos = next;
288 m->op->stop(m, p);
289 n = min(m->count, size);
290 err = copy_to_user(buf, m->buf, n);
291 if (err)
292 goto Efault;
293 copied += n;
294 m->count -= n;
295 if (m->count)
296 m->from = n;
297 else
298 pos++;
299 m->index = pos;
300 Done:
301 if (!copied)
302 copied = err;
303 else {
304 *ppos += copied;
305 m->read_pos += copied;
307 file->f_version = m->version;
308 mutex_unlock(&m->lock);
309 return copied;
310 Enomem:
311 err = -ENOMEM;
312 goto Done;
313 Efault:
314 err = -EFAULT;
315 goto Done;
317 EXPORT_SYMBOL(seq_read);
320 * seq_lseek - ->llseek() method for sequential files.
321 * @file: the file in question
322 * @offset: new position
323 * @whence: 0 for absolute, 1 for relative position
325 * Ready-made ->f_op->llseek()
327 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
329 struct seq_file *m = file->private_data;
330 loff_t retval = -EINVAL;
332 mutex_lock(&m->lock);
333 m->version = file->f_version;
334 switch (whence) {
335 case SEEK_CUR:
336 offset += file->f_pos;
337 case SEEK_SET:
338 if (offset < 0)
339 break;
340 retval = offset;
341 if (offset != m->read_pos) {
342 while ((retval = traverse(m, offset)) == -EAGAIN)
344 if (retval) {
345 /* with extreme prejudice... */
346 file->f_pos = 0;
347 m->read_pos = 0;
348 m->version = 0;
349 m->index = 0;
350 m->count = 0;
351 } else {
352 m->read_pos = offset;
353 retval = file->f_pos = offset;
355 } else {
356 file->f_pos = offset;
359 file->f_version = m->version;
360 mutex_unlock(&m->lock);
361 return retval;
363 EXPORT_SYMBOL(seq_lseek);
366 * seq_release - free the structures associated with sequential file.
367 * @file: file in question
368 * @inode: its inode
370 * Frees the structures associated with sequential file; can be used
371 * as ->f_op->release() if you don't have private data to destroy.
373 int seq_release(struct inode *inode, struct file *file)
375 struct seq_file *m = file->private_data;
376 kvfree(m->buf);
377 kfree(m);
378 return 0;
380 EXPORT_SYMBOL(seq_release);
383 * seq_escape - print string into buffer, escaping some characters
384 * @m: target buffer
385 * @s: string
386 * @esc: set of characters that need escaping
388 * Puts string into buffer, replacing each occurrence of character from
389 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
390 * case of overflow.
392 int seq_escape(struct seq_file *m, const char *s, const char *esc)
394 char *end = m->buf + m->size;
395 char *p;
396 char c;
398 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
399 if (!strchr(esc, c)) {
400 *p++ = c;
401 continue;
403 if (p + 3 < end) {
404 *p++ = '\\';
405 *p++ = '0' + ((c & 0300) >> 6);
406 *p++ = '0' + ((c & 070) >> 3);
407 *p++ = '0' + (c & 07);
408 continue;
410 seq_set_overflow(m);
411 return -1;
413 m->count = p - m->buf;
414 return 0;
416 EXPORT_SYMBOL(seq_escape);
418 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
420 int len;
422 if (m->count < m->size) {
423 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
424 if (m->count + len < m->size) {
425 m->count += len;
426 return 0;
429 seq_set_overflow(m);
430 return -1;
432 EXPORT_SYMBOL(seq_vprintf);
434 int seq_printf(struct seq_file *m, const char *f, ...)
436 int ret;
437 va_list args;
439 va_start(args, f);
440 ret = seq_vprintf(m, f, args);
441 va_end(args);
443 return ret;
445 EXPORT_SYMBOL(seq_printf);
448 * mangle_path - mangle and copy path to buffer beginning
449 * @s: buffer start
450 * @p: beginning of path in above buffer
451 * @esc: set of characters that need escaping
453 * Copy the path from @p to @s, replacing each occurrence of character from
454 * @esc with usual octal escape.
455 * Returns pointer past last written character in @s, or NULL in case of
456 * failure.
458 char *mangle_path(char *s, const char *p, const char *esc)
460 while (s <= p) {
461 char c = *p++;
462 if (!c) {
463 return s;
464 } else if (!strchr(esc, c)) {
465 *s++ = c;
466 } else if (s + 4 > p) {
467 break;
468 } else {
469 *s++ = '\\';
470 *s++ = '0' + ((c & 0300) >> 6);
471 *s++ = '0' + ((c & 070) >> 3);
472 *s++ = '0' + (c & 07);
475 return NULL;
477 EXPORT_SYMBOL(mangle_path);
480 * seq_path - seq_file interface to print a pathname
481 * @m: the seq_file handle
482 * @path: the struct path to print
483 * @esc: set of characters to escape in the output
485 * return the absolute path of 'path', as represented by the
486 * dentry / mnt pair in the path parameter.
488 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
490 char *buf;
491 size_t size = seq_get_buf(m, &buf);
492 int res = -1;
494 if (size) {
495 char *p = d_path(path, buf, size);
496 if (!IS_ERR(p)) {
497 char *end = mangle_path(buf, p, esc);
498 if (end)
499 res = end - buf;
502 seq_commit(m, res);
504 return res;
506 EXPORT_SYMBOL(seq_path);
509 * Same as seq_path, but relative to supplied root.
511 int seq_path_root(struct seq_file *m, const struct path *path,
512 const struct path *root, const char *esc)
514 char *buf;
515 size_t size = seq_get_buf(m, &buf);
516 int res = -ENAMETOOLONG;
518 if (size) {
519 char *p;
521 p = __d_path(path, root, buf, size);
522 if (!p)
523 return SEQ_SKIP;
524 res = PTR_ERR(p);
525 if (!IS_ERR(p)) {
526 char *end = mangle_path(buf, p, esc);
527 if (end)
528 res = end - buf;
529 else
530 res = -ENAMETOOLONG;
533 seq_commit(m, res);
535 return res < 0 && res != -ENAMETOOLONG ? res : 0;
539 * returns the path of the 'dentry' from the root of its filesystem.
541 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
543 char *buf;
544 size_t size = seq_get_buf(m, &buf);
545 int res = -1;
547 if (size) {
548 char *p = dentry_path(dentry, buf, size);
549 if (!IS_ERR(p)) {
550 char *end = mangle_path(buf, p, esc);
551 if (end)
552 res = end - buf;
555 seq_commit(m, res);
557 return res;
560 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
561 unsigned int nr_bits)
563 if (m->count < m->size) {
564 int len = bitmap_scnprintf(m->buf + m->count,
565 m->size - m->count, bits, nr_bits);
566 if (m->count + len < m->size) {
567 m->count += len;
568 return 0;
571 seq_set_overflow(m);
572 return -1;
574 EXPORT_SYMBOL(seq_bitmap);
576 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
577 unsigned int nr_bits)
579 if (m->count < m->size) {
580 int len = bitmap_scnlistprintf(m->buf + m->count,
581 m->size - m->count, bits, nr_bits);
582 if (m->count + len < m->size) {
583 m->count += len;
584 return 0;
587 seq_set_overflow(m);
588 return -1;
590 EXPORT_SYMBOL(seq_bitmap_list);
592 static void *single_start(struct seq_file *p, loff_t *pos)
594 return NULL + (*pos == 0);
597 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
599 ++*pos;
600 return NULL;
603 static void single_stop(struct seq_file *p, void *v)
607 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
608 void *data)
610 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
611 int res = -ENOMEM;
613 if (op) {
614 op->start = single_start;
615 op->next = single_next;
616 op->stop = single_stop;
617 op->show = show;
618 res = seq_open(file, op);
619 if (!res)
620 ((struct seq_file *)file->private_data)->private = data;
621 else
622 kfree(op);
624 return res;
626 EXPORT_SYMBOL(single_open);
628 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
629 void *data, size_t size)
631 char *buf = seq_buf_alloc(size);
632 int ret;
633 if (!buf)
634 return -ENOMEM;
635 ret = single_open(file, show, data);
636 if (ret) {
637 kvfree(buf);
638 return ret;
640 ((struct seq_file *)file->private_data)->buf = buf;
641 ((struct seq_file *)file->private_data)->size = size;
642 return 0;
644 EXPORT_SYMBOL(single_open_size);
646 int single_release(struct inode *inode, struct file *file)
648 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
649 int res = seq_release(inode, file);
650 kfree(op);
651 return res;
653 EXPORT_SYMBOL(single_release);
655 int seq_release_private(struct inode *inode, struct file *file)
657 struct seq_file *seq = file->private_data;
659 kfree(seq->private);
660 seq->private = NULL;
661 return seq_release(inode, file);
663 EXPORT_SYMBOL(seq_release_private);
665 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
666 int psize)
668 int rc;
669 void *private;
670 struct seq_file *seq;
672 private = kzalloc(psize, GFP_KERNEL);
673 if (private == NULL)
674 goto out;
676 rc = seq_open(f, ops);
677 if (rc < 0)
678 goto out_free;
680 seq = f->private_data;
681 seq->private = private;
682 return private;
684 out_free:
685 kfree(private);
686 out:
687 return NULL;
689 EXPORT_SYMBOL(__seq_open_private);
691 int seq_open_private(struct file *filp, const struct seq_operations *ops,
692 int psize)
694 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
696 EXPORT_SYMBOL(seq_open_private);
698 int seq_putc(struct seq_file *m, char c)
700 if (m->count < m->size) {
701 m->buf[m->count++] = c;
702 return 0;
704 return -1;
706 EXPORT_SYMBOL(seq_putc);
708 int seq_puts(struct seq_file *m, const char *s)
710 int len = strlen(s);
711 if (m->count + len < m->size) {
712 memcpy(m->buf + m->count, s, len);
713 m->count += len;
714 return 0;
716 seq_set_overflow(m);
717 return -1;
719 EXPORT_SYMBOL(seq_puts);
722 * A helper routine for putting decimal numbers without rich format of printf().
723 * only 'unsigned long long' is supported.
724 * This routine will put one byte delimiter + number into seq_file.
725 * This routine is very quick when you show lots of numbers.
726 * In usual cases, it will be better to use seq_printf(). It's easier to read.
728 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
729 unsigned long long num)
731 int len;
733 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
734 goto overflow;
736 if (delimiter)
737 m->buf[m->count++] = delimiter;
739 if (num < 10) {
740 m->buf[m->count++] = num + '0';
741 return 0;
744 len = num_to_str(m->buf + m->count, m->size - m->count, num);
745 if (!len)
746 goto overflow;
747 m->count += len;
748 return 0;
749 overflow:
750 seq_set_overflow(m);
751 return -1;
753 EXPORT_SYMBOL(seq_put_decimal_ull);
755 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
756 long long num)
758 if (num < 0) {
759 if (m->count + 3 >= m->size) {
760 seq_set_overflow(m);
761 return -1;
763 if (delimiter)
764 m->buf[m->count++] = delimiter;
765 num = -num;
766 delimiter = '-';
768 return seq_put_decimal_ull(m, delimiter, num);
771 EXPORT_SYMBOL(seq_put_decimal_ll);
774 * seq_write - write arbitrary data to buffer
775 * @seq: seq_file identifying the buffer to which data should be written
776 * @data: data address
777 * @len: number of bytes
779 * Return 0 on success, non-zero otherwise.
781 int seq_write(struct seq_file *seq, const void *data, size_t len)
783 if (seq->count + len < seq->size) {
784 memcpy(seq->buf + seq->count, data, len);
785 seq->count += len;
786 return 0;
788 seq_set_overflow(seq);
789 return -1;
791 EXPORT_SYMBOL(seq_write);
793 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
795 struct list_head *lh;
797 list_for_each(lh, head)
798 if (pos-- == 0)
799 return lh;
801 return NULL;
803 EXPORT_SYMBOL(seq_list_start);
805 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
807 if (!pos)
808 return head;
810 return seq_list_start(head, pos - 1);
812 EXPORT_SYMBOL(seq_list_start_head);
814 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
816 struct list_head *lh;
818 lh = ((struct list_head *)v)->next;
819 ++*ppos;
820 return lh == head ? NULL : lh;
822 EXPORT_SYMBOL(seq_list_next);
825 * seq_hlist_start - start an iteration of a hlist
826 * @head: the head of the hlist
827 * @pos: the start position of the sequence
829 * Called at seq_file->op->start().
831 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
833 struct hlist_node *node;
835 hlist_for_each(node, head)
836 if (pos-- == 0)
837 return node;
838 return NULL;
840 EXPORT_SYMBOL(seq_hlist_start);
843 * seq_hlist_start_head - start an iteration of a hlist
844 * @head: the head of the hlist
845 * @pos: the start position of the sequence
847 * Called at seq_file->op->start(). Call this function if you want to
848 * print a header at the top of the output.
850 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
852 if (!pos)
853 return SEQ_START_TOKEN;
855 return seq_hlist_start(head, pos - 1);
857 EXPORT_SYMBOL(seq_hlist_start_head);
860 * seq_hlist_next - move to the next position of the hlist
861 * @v: the current iterator
862 * @head: the head of the hlist
863 * @ppos: the current position
865 * Called at seq_file->op->next().
867 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
868 loff_t *ppos)
870 struct hlist_node *node = v;
872 ++*ppos;
873 if (v == SEQ_START_TOKEN)
874 return head->first;
875 else
876 return node->next;
878 EXPORT_SYMBOL(seq_hlist_next);
881 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
882 * @head: the head of the hlist
883 * @pos: the start position of the sequence
885 * Called at seq_file->op->start().
887 * This list-traversal primitive may safely run concurrently with
888 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
889 * as long as the traversal is guarded by rcu_read_lock().
891 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
892 loff_t pos)
894 struct hlist_node *node;
896 __hlist_for_each_rcu(node, head)
897 if (pos-- == 0)
898 return node;
899 return NULL;
901 EXPORT_SYMBOL(seq_hlist_start_rcu);
904 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
905 * @head: the head of the hlist
906 * @pos: the start position of the sequence
908 * Called at seq_file->op->start(). Call this function if you want to
909 * print a header at the top of the output.
911 * This list-traversal primitive may safely run concurrently with
912 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
913 * as long as the traversal is guarded by rcu_read_lock().
915 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
916 loff_t pos)
918 if (!pos)
919 return SEQ_START_TOKEN;
921 return seq_hlist_start_rcu(head, pos - 1);
923 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
926 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
927 * @v: the current iterator
928 * @head: the head of the hlist
929 * @ppos: the current position
931 * Called at seq_file->op->next().
933 * This list-traversal primitive may safely run concurrently with
934 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
935 * as long as the traversal is guarded by rcu_read_lock().
937 struct hlist_node *seq_hlist_next_rcu(void *v,
938 struct hlist_head *head,
939 loff_t *ppos)
941 struct hlist_node *node = v;
943 ++*ppos;
944 if (v == SEQ_START_TOKEN)
945 return rcu_dereference(head->first);
946 else
947 return rcu_dereference(node->next);
949 EXPORT_SYMBOL(seq_hlist_next_rcu);
952 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
953 * @head: pointer to percpu array of struct hlist_heads
954 * @cpu: pointer to cpu "cursor"
955 * @pos: start position of sequence
957 * Called at seq_file->op->start().
959 struct hlist_node *
960 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
962 struct hlist_node *node;
964 for_each_possible_cpu(*cpu) {
965 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
966 if (pos-- == 0)
967 return node;
970 return NULL;
972 EXPORT_SYMBOL(seq_hlist_start_percpu);
975 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
976 * @v: pointer to current hlist_node
977 * @head: pointer to percpu array of struct hlist_heads
978 * @cpu: pointer to cpu "cursor"
979 * @pos: start position of sequence
981 * Called at seq_file->op->next().
983 struct hlist_node *
984 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
985 int *cpu, loff_t *pos)
987 struct hlist_node *node = v;
989 ++*pos;
991 if (node->next)
992 return node->next;
994 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
995 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
996 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
998 if (!hlist_empty(bucket))
999 return bucket->first;
1001 return NULL;
1003 EXPORT_SYMBOL(seq_hlist_next_percpu);