gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / fs / seq_file.c
blob4408057d1dc805311a73009cb1439eeb548b68dd
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>
19 static void seq_set_overflow(struct seq_file *m)
21 m->count = m->size;
24 static void *seq_buf_alloc(unsigned long size)
26 void *buf;
29 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
30 * it's better to fall back to vmalloc() than to kill things.
32 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
33 if (!buf && size > PAGE_SIZE)
34 buf = vmalloc(size);
35 return buf;
38 /**
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
50 * Returning SEQ_SKIP means "discard this element and move on".
52 int seq_open(struct file *file, const struct seq_operations *op)
54 struct seq_file *p = file->private_data;
56 if (!p) {
57 p = kmalloc(sizeof(*p), GFP_KERNEL);
58 if (!p)
59 return -ENOMEM;
60 file->private_data = p;
62 memset(p, 0, sizeof(*p));
63 mutex_init(&p->lock);
64 p->op = op;
65 #ifdef CONFIG_USER_NS
66 p->user_ns = file->f_cred->user_ns;
67 #endif
70 * Wrappers around seq_open(e.g. swaps_open) need to be
71 * aware of this. If they set f_version themselves, they
72 * should call seq_open first and then set f_version.
74 file->f_version = 0;
77 * seq_files support lseek() and pread(). They do not implement
78 * write() at all, but we clear FMODE_PWRITE here for historical
79 * reasons.
81 * If a client of seq_files a) implements file.write() and b) wishes to
82 * support pwrite() then that client will need to implement its own
83 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
85 file->f_mode &= ~FMODE_PWRITE;
86 return 0;
88 EXPORT_SYMBOL(seq_open);
90 static int traverse(struct seq_file *m, loff_t offset)
92 loff_t pos = 0, index;
93 int error = 0;
94 void *p;
96 m->version = 0;
97 index = 0;
98 m->count = m->from = 0;
99 if (!offset) {
100 m->index = index;
101 return 0;
103 if (!m->buf) {
104 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
105 if (!m->buf)
106 return -ENOMEM;
108 p = m->op->start(m, &index);
109 while (p) {
110 error = PTR_ERR(p);
111 if (IS_ERR(p))
112 break;
113 error = m->op->show(m, p);
114 if (error < 0)
115 break;
116 if (unlikely(error)) {
117 error = 0;
118 m->count = 0;
120 if (seq_has_overflowed(m))
121 goto Eoverflow;
122 if (pos + m->count > offset) {
123 m->from = offset - pos;
124 m->count -= m->from;
125 m->index = index;
126 break;
128 pos += m->count;
129 m->count = 0;
130 if (pos == offset) {
131 index++;
132 m->index = index;
133 break;
135 p = m->op->next(m, p, &index);
137 m->op->stop(m, p);
138 m->index = index;
139 return error;
141 Eoverflow:
142 m->op->stop(m, p);
143 kvfree(m->buf);
144 m->count = 0;
145 m->buf = seq_buf_alloc(m->size <<= 1);
146 return !m->buf ? -ENOMEM : -EAGAIN;
150 * seq_read - ->read() method for sequential files.
151 * @file: the file to read from
152 * @buf: the buffer to read to
153 * @size: the maximum number of bytes to read
154 * @ppos: the current position in the file
156 * Ready-made ->f_op->read()
158 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
160 struct seq_file *m = file->private_data;
161 size_t copied = 0;
162 loff_t pos;
163 size_t n;
164 void *p;
165 int err = 0;
167 mutex_lock(&m->lock);
170 * seq_file->op->..m_start/m_stop/m_next may do special actions
171 * or optimisations based on the file->f_version, so we want to
172 * pass the file->f_version to those methods.
174 * seq_file->version is just copy of f_version, and seq_file
175 * methods can treat it simply as file version.
176 * It is copied in first and copied out after all operations.
177 * It is convenient to have it as part of structure to avoid the
178 * need of passing another argument to all the seq_file methods.
180 m->version = file->f_version;
182 /* Don't assume *ppos is where we left it */
183 if (unlikely(*ppos != m->read_pos)) {
184 while ((err = traverse(m, *ppos)) == -EAGAIN)
186 if (err) {
187 /* With prejudice... */
188 m->read_pos = 0;
189 m->version = 0;
190 m->index = 0;
191 m->count = 0;
192 goto Done;
193 } else {
194 m->read_pos = *ppos;
198 /* grab buffer if we didn't have one */
199 if (!m->buf) {
200 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
201 if (!m->buf)
202 goto Enomem;
204 /* if not empty - flush it first */
205 if (m->count) {
206 n = min(m->count, size);
207 err = copy_to_user(buf, m->buf + m->from, n);
208 if (err)
209 goto Efault;
210 m->count -= n;
211 m->from += n;
212 size -= n;
213 buf += n;
214 copied += n;
215 if (!m->count) {
216 m->from = 0;
217 m->index++;
219 if (!size)
220 goto Done;
222 /* we need at least one record in buffer */
223 pos = m->index;
224 p = m->op->start(m, &pos);
225 while (1) {
226 err = PTR_ERR(p);
227 if (!p || IS_ERR(p))
228 break;
229 err = m->op->show(m, p);
230 if (err < 0)
231 break;
232 if (unlikely(err))
233 m->count = 0;
234 if (unlikely(!m->count)) {
235 p = m->op->next(m, p, &pos);
236 m->index = pos;
237 continue;
239 if (m->count < m->size)
240 goto Fill;
241 m->op->stop(m, p);
242 kvfree(m->buf);
243 m->count = 0;
244 m->buf = seq_buf_alloc(m->size <<= 1);
245 if (!m->buf)
246 goto Enomem;
247 m->version = 0;
248 pos = m->index;
249 p = m->op->start(m, &pos);
251 m->op->stop(m, p);
252 m->count = 0;
253 goto Done;
254 Fill:
255 /* they want more? let's try to get some more */
256 while (m->count < size) {
257 size_t offs = m->count;
258 loff_t next = pos;
259 p = m->op->next(m, p, &next);
260 if (!p || IS_ERR(p)) {
261 err = PTR_ERR(p);
262 break;
264 err = m->op->show(m, p);
265 if (seq_has_overflowed(m) || err) {
266 m->count = offs;
267 if (likely(err <= 0))
268 break;
270 pos = next;
272 m->op->stop(m, p);
273 n = min(m->count, size);
274 err = copy_to_user(buf, m->buf, n);
275 if (err)
276 goto Efault;
277 copied += n;
278 m->count -= n;
279 if (m->count)
280 m->from = n;
281 else
282 pos++;
283 m->index = pos;
284 Done:
285 if (!copied)
286 copied = err;
287 else {
288 *ppos += copied;
289 m->read_pos += copied;
291 file->f_version = m->version;
292 mutex_unlock(&m->lock);
293 return copied;
294 Enomem:
295 err = -ENOMEM;
296 goto Done;
297 Efault:
298 err = -EFAULT;
299 goto Done;
301 EXPORT_SYMBOL(seq_read);
304 * seq_lseek - ->llseek() method for sequential files.
305 * @file: the file in question
306 * @offset: new position
307 * @whence: 0 for absolute, 1 for relative position
309 * Ready-made ->f_op->llseek()
311 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
313 struct seq_file *m = file->private_data;
314 loff_t retval = -EINVAL;
316 mutex_lock(&m->lock);
317 m->version = file->f_version;
318 switch (whence) {
319 case SEEK_CUR:
320 offset += file->f_pos;
321 case SEEK_SET:
322 if (offset < 0)
323 break;
324 retval = offset;
325 if (offset != m->read_pos) {
326 while ((retval = traverse(m, offset)) == -EAGAIN)
328 if (retval) {
329 /* with extreme prejudice... */
330 file->f_pos = 0;
331 m->read_pos = 0;
332 m->version = 0;
333 m->index = 0;
334 m->count = 0;
335 } else {
336 m->read_pos = offset;
337 retval = file->f_pos = offset;
339 } else {
340 file->f_pos = offset;
343 file->f_version = m->version;
344 mutex_unlock(&m->lock);
345 return retval;
347 EXPORT_SYMBOL(seq_lseek);
350 * seq_release - free the structures associated with sequential file.
351 * @file: file in question
352 * @inode: its inode
354 * Frees the structures associated with sequential file; can be used
355 * as ->f_op->release() if you don't have private data to destroy.
357 int seq_release(struct inode *inode, struct file *file)
359 struct seq_file *m = file->private_data;
360 kvfree(m->buf);
361 kfree(m);
362 return 0;
364 EXPORT_SYMBOL(seq_release);
367 * seq_escape - print string into buffer, escaping some characters
368 * @m: target buffer
369 * @s: string
370 * @esc: set of characters that need escaping
372 * Puts string into buffer, replacing each occurrence of character from
373 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
374 * case of overflow.
376 int seq_escape(struct seq_file *m, const char *s, const char *esc)
378 char *end = m->buf + m->size;
379 char *p;
380 char c;
382 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
383 if (!strchr(esc, c)) {
384 *p++ = c;
385 continue;
387 if (p + 3 < end) {
388 *p++ = '\\';
389 *p++ = '0' + ((c & 0300) >> 6);
390 *p++ = '0' + ((c & 070) >> 3);
391 *p++ = '0' + (c & 07);
392 continue;
394 seq_set_overflow(m);
395 return -1;
397 m->count = p - m->buf;
398 return 0;
400 EXPORT_SYMBOL(seq_escape);
402 int seq_vprintf(struct seq_file *m, const char *f, va_list args)
404 int len;
406 if (m->count < m->size) {
407 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
408 if (m->count + len < m->size) {
409 m->count += len;
410 return 0;
413 seq_set_overflow(m);
414 return -1;
416 EXPORT_SYMBOL(seq_vprintf);
418 int seq_printf(struct seq_file *m, const char *f, ...)
420 int ret;
421 va_list args;
423 va_start(args, f);
424 ret = seq_vprintf(m, f, args);
425 va_end(args);
427 return ret;
429 EXPORT_SYMBOL(seq_printf);
432 * mangle_path - mangle and copy path to buffer beginning
433 * @s: buffer start
434 * @p: beginning of path in above buffer
435 * @esc: set of characters that need escaping
437 * Copy the path from @p to @s, replacing each occurrence of character from
438 * @esc with usual octal escape.
439 * Returns pointer past last written character in @s, or NULL in case of
440 * failure.
442 char *mangle_path(char *s, const char *p, const char *esc)
444 while (s <= p) {
445 char c = *p++;
446 if (!c) {
447 return s;
448 } else if (!strchr(esc, c)) {
449 *s++ = c;
450 } else if (s + 4 > p) {
451 break;
452 } else {
453 *s++ = '\\';
454 *s++ = '0' + ((c & 0300) >> 6);
455 *s++ = '0' + ((c & 070) >> 3);
456 *s++ = '0' + (c & 07);
459 return NULL;
461 EXPORT_SYMBOL(mangle_path);
464 * seq_path - seq_file interface to print a pathname
465 * @m: the seq_file handle
466 * @path: the struct path to print
467 * @esc: set of characters to escape in the output
469 * return the absolute path of 'path', as represented by the
470 * dentry / mnt pair in the path parameter.
472 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
474 char *buf;
475 size_t size = seq_get_buf(m, &buf);
476 int res = -1;
478 if (size) {
479 char *p = d_path(path, buf, size);
480 if (!IS_ERR(p)) {
481 char *end = mangle_path(buf, p, esc);
482 if (end)
483 res = end - buf;
486 seq_commit(m, res);
488 return res;
490 EXPORT_SYMBOL(seq_path);
493 * Same as seq_path, but relative to supplied root.
495 int seq_path_root(struct seq_file *m, const struct path *path,
496 const struct path *root, const char *esc)
498 char *buf;
499 size_t size = seq_get_buf(m, &buf);
500 int res = -ENAMETOOLONG;
502 if (size) {
503 char *p;
505 p = __d_path(path, root, buf, size);
506 if (!p)
507 return SEQ_SKIP;
508 res = PTR_ERR(p);
509 if (!IS_ERR(p)) {
510 char *end = mangle_path(buf, p, esc);
511 if (end)
512 res = end - buf;
513 else
514 res = -ENAMETOOLONG;
517 seq_commit(m, res);
519 return res < 0 && res != -ENAMETOOLONG ? res : 0;
523 * returns the path of the 'dentry' from the root of its filesystem.
525 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
527 char *buf;
528 size_t size = seq_get_buf(m, &buf);
529 int res = -1;
531 if (size) {
532 char *p = dentry_path(dentry, buf, size);
533 if (!IS_ERR(p)) {
534 char *end = mangle_path(buf, p, esc);
535 if (end)
536 res = end - buf;
539 seq_commit(m, res);
541 return res;
544 static void *single_start(struct seq_file *p, loff_t *pos)
546 return NULL + (*pos == 0);
549 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
551 ++*pos;
552 return NULL;
555 static void single_stop(struct seq_file *p, void *v)
559 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
560 void *data)
562 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
563 int res = -ENOMEM;
565 if (op) {
566 op->start = single_start;
567 op->next = single_next;
568 op->stop = single_stop;
569 op->show = show;
570 res = seq_open(file, op);
571 if (!res)
572 ((struct seq_file *)file->private_data)->private = data;
573 else
574 kfree(op);
576 return res;
578 EXPORT_SYMBOL(single_open);
580 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
581 void *data, size_t size)
583 char *buf = seq_buf_alloc(size);
584 int ret;
585 if (!buf)
586 return -ENOMEM;
587 ret = single_open(file, show, data);
588 if (ret) {
589 kvfree(buf);
590 return ret;
592 ((struct seq_file *)file->private_data)->buf = buf;
593 ((struct seq_file *)file->private_data)->size = size;
594 return 0;
596 EXPORT_SYMBOL(single_open_size);
598 int single_release(struct inode *inode, struct file *file)
600 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
601 int res = seq_release(inode, file);
602 kfree(op);
603 return res;
605 EXPORT_SYMBOL(single_release);
607 int seq_release_private(struct inode *inode, struct file *file)
609 struct seq_file *seq = file->private_data;
611 kfree(seq->private);
612 seq->private = NULL;
613 return seq_release(inode, file);
615 EXPORT_SYMBOL(seq_release_private);
617 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
618 int psize)
620 int rc;
621 void *private;
622 struct seq_file *seq;
624 private = kzalloc(psize, GFP_KERNEL);
625 if (private == NULL)
626 goto out;
628 rc = seq_open(f, ops);
629 if (rc < 0)
630 goto out_free;
632 seq = f->private_data;
633 seq->private = private;
634 return private;
636 out_free:
637 kfree(private);
638 out:
639 return NULL;
641 EXPORT_SYMBOL(__seq_open_private);
643 int seq_open_private(struct file *filp, const struct seq_operations *ops,
644 int psize)
646 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
648 EXPORT_SYMBOL(seq_open_private);
650 int seq_putc(struct seq_file *m, char c)
652 if (m->count < m->size) {
653 m->buf[m->count++] = c;
654 return 0;
656 return -1;
658 EXPORT_SYMBOL(seq_putc);
660 int seq_puts(struct seq_file *m, const char *s)
662 int len = strlen(s);
663 if (m->count + len < m->size) {
664 memcpy(m->buf + m->count, s, len);
665 m->count += len;
666 return 0;
668 seq_set_overflow(m);
669 return -1;
671 EXPORT_SYMBOL(seq_puts);
674 * A helper routine for putting decimal numbers without rich format of printf().
675 * only 'unsigned long long' is supported.
676 * This routine will put one byte delimiter + number into seq_file.
677 * This routine is very quick when you show lots of numbers.
678 * In usual cases, it will be better to use seq_printf(). It's easier to read.
680 int seq_put_decimal_ull(struct seq_file *m, char delimiter,
681 unsigned long long num)
683 int len;
685 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
686 goto overflow;
688 if (delimiter)
689 m->buf[m->count++] = delimiter;
691 if (num < 10) {
692 m->buf[m->count++] = num + '0';
693 return 0;
696 len = num_to_str(m->buf + m->count, m->size - m->count, num);
697 if (!len)
698 goto overflow;
699 m->count += len;
700 return 0;
701 overflow:
702 seq_set_overflow(m);
703 return -1;
705 EXPORT_SYMBOL(seq_put_decimal_ull);
707 int seq_put_decimal_ll(struct seq_file *m, char delimiter,
708 long long num)
710 if (num < 0) {
711 if (m->count + 3 >= m->size) {
712 seq_set_overflow(m);
713 return -1;
715 if (delimiter)
716 m->buf[m->count++] = delimiter;
717 num = -num;
718 delimiter = '-';
720 return seq_put_decimal_ull(m, delimiter, num);
723 EXPORT_SYMBOL(seq_put_decimal_ll);
726 * seq_write - write arbitrary data to buffer
727 * @seq: seq_file identifying the buffer to which data should be written
728 * @data: data address
729 * @len: number of bytes
731 * Return 0 on success, non-zero otherwise.
733 int seq_write(struct seq_file *seq, const void *data, size_t len)
735 if (seq->count + len < seq->size) {
736 memcpy(seq->buf + seq->count, data, len);
737 seq->count += len;
738 return 0;
740 seq_set_overflow(seq);
741 return -1;
743 EXPORT_SYMBOL(seq_write);
746 * seq_pad - write padding spaces to buffer
747 * @m: seq_file identifying the buffer to which data should be written
748 * @c: the byte to append after padding if non-zero
750 void seq_pad(struct seq_file *m, char c)
752 int size = m->pad_until - m->count;
753 if (size > 0)
754 seq_printf(m, "%*s", size, "");
755 if (c)
756 seq_putc(m, c);
758 EXPORT_SYMBOL(seq_pad);
760 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
762 struct list_head *lh;
764 list_for_each(lh, head)
765 if (pos-- == 0)
766 return lh;
768 return NULL;
770 EXPORT_SYMBOL(seq_list_start);
772 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
774 if (!pos)
775 return head;
777 return seq_list_start(head, pos - 1);
779 EXPORT_SYMBOL(seq_list_start_head);
781 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
783 struct list_head *lh;
785 lh = ((struct list_head *)v)->next;
786 ++*ppos;
787 return lh == head ? NULL : lh;
789 EXPORT_SYMBOL(seq_list_next);
792 * seq_hlist_start - start an iteration of a hlist
793 * @head: the head of the hlist
794 * @pos: the start position of the sequence
796 * Called at seq_file->op->start().
798 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
800 struct hlist_node *node;
802 hlist_for_each(node, head)
803 if (pos-- == 0)
804 return node;
805 return NULL;
807 EXPORT_SYMBOL(seq_hlist_start);
810 * seq_hlist_start_head - start an iteration of a hlist
811 * @head: the head of the hlist
812 * @pos: the start position of the sequence
814 * Called at seq_file->op->start(). Call this function if you want to
815 * print a header at the top of the output.
817 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
819 if (!pos)
820 return SEQ_START_TOKEN;
822 return seq_hlist_start(head, pos - 1);
824 EXPORT_SYMBOL(seq_hlist_start_head);
827 * seq_hlist_next - move to the next position of the hlist
828 * @v: the current iterator
829 * @head: the head of the hlist
830 * @ppos: the current position
832 * Called at seq_file->op->next().
834 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
835 loff_t *ppos)
837 struct hlist_node *node = v;
839 ++*ppos;
840 if (v == SEQ_START_TOKEN)
841 return head->first;
842 else
843 return node->next;
845 EXPORT_SYMBOL(seq_hlist_next);
848 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
849 * @head: the head of the hlist
850 * @pos: the start position of the sequence
852 * Called at seq_file->op->start().
854 * This list-traversal primitive may safely run concurrently with
855 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
856 * as long as the traversal is guarded by rcu_read_lock().
858 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
859 loff_t pos)
861 struct hlist_node *node;
863 __hlist_for_each_rcu(node, head)
864 if (pos-- == 0)
865 return node;
866 return NULL;
868 EXPORT_SYMBOL(seq_hlist_start_rcu);
871 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
872 * @head: the head of the hlist
873 * @pos: the start position of the sequence
875 * Called at seq_file->op->start(). Call this function if you want to
876 * print a header at the top of the output.
878 * This list-traversal primitive may safely run concurrently with
879 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
880 * as long as the traversal is guarded by rcu_read_lock().
882 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
883 loff_t pos)
885 if (!pos)
886 return SEQ_START_TOKEN;
888 return seq_hlist_start_rcu(head, pos - 1);
890 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
893 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
894 * @v: the current iterator
895 * @head: the head of the hlist
896 * @ppos: the current position
898 * Called at seq_file->op->next().
900 * This list-traversal primitive may safely run concurrently with
901 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
902 * as long as the traversal is guarded by rcu_read_lock().
904 struct hlist_node *seq_hlist_next_rcu(void *v,
905 struct hlist_head *head,
906 loff_t *ppos)
908 struct hlist_node *node = v;
910 ++*ppos;
911 if (v == SEQ_START_TOKEN)
912 return rcu_dereference(head->first);
913 else
914 return rcu_dereference(node->next);
916 EXPORT_SYMBOL(seq_hlist_next_rcu);
919 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
920 * @head: pointer to percpu array of struct hlist_heads
921 * @cpu: pointer to cpu "cursor"
922 * @pos: start position of sequence
924 * Called at seq_file->op->start().
926 struct hlist_node *
927 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
929 struct hlist_node *node;
931 for_each_possible_cpu(*cpu) {
932 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
933 if (pos-- == 0)
934 return node;
937 return NULL;
939 EXPORT_SYMBOL(seq_hlist_start_percpu);
942 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
943 * @v: pointer to current hlist_node
944 * @head: pointer to percpu array of struct hlist_heads
945 * @cpu: pointer to cpu "cursor"
946 * @pos: start position of sequence
948 * Called at seq_file->op->next().
950 struct hlist_node *
951 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
952 int *cpu, loff_t *pos)
954 struct hlist_node *node = v;
956 ++*pos;
958 if (node->next)
959 return node->next;
961 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
962 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
963 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
965 if (!hlist_empty(bucket))
966 return bucket->first;
968 return NULL;
970 EXPORT_SYMBOL(seq_hlist_next_percpu);