wlcore: Add RX_BA_WIN_SIZE_CHANGE_EVENT event
[linux/fpc-iii.git] / fs / seq_file.c
blobd672e2fec459116cfe67688fd679e30187e80f70
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>
15 #include <linux/printk.h>
16 #include <linux/string_helpers.h>
18 #include <asm/uaccess.h>
19 #include <asm/page.h>
21 static void seq_set_overflow(struct seq_file *m)
23 m->count = m->size;
26 static void *seq_buf_alloc(unsigned long size)
28 void *buf;
29 gfp_t gfp = GFP_KERNEL;
32 * For high order allocations, use __GFP_NORETRY to avoid oom-killing -
33 * it's better to fall back to vmalloc() than to kill things. For small
34 * allocations, just use GFP_KERNEL which will oom kill, thus no need
35 * for vmalloc fallback.
37 if (size > PAGE_SIZE)
38 gfp |= __GFP_NORETRY | __GFP_NOWARN;
39 buf = kmalloc(size, gfp);
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".
58 * Note: seq_open() will allocate a struct seq_file and store its
59 * pointer in @file->private_data. This pointer should not be modified.
61 int seq_open(struct file *file, const struct seq_operations *op)
63 struct seq_file *p;
65 WARN_ON(file->private_data);
67 p = kzalloc(sizeof(*p), GFP_KERNEL);
68 if (!p)
69 return -ENOMEM;
71 file->private_data = p;
73 mutex_init(&p->lock);
74 p->op = op;
75 #ifdef CONFIG_USER_NS
76 p->user_ns = file->f_cred->user_ns;
77 #endif
80 * Wrappers around seq_open(e.g. swaps_open) need to be
81 * aware of this. If they set f_version themselves, they
82 * should call seq_open first and then set f_version.
84 file->f_version = 0;
87 * seq_files support lseek() and pread(). They do not implement
88 * write() at all, but we clear FMODE_PWRITE here for historical
89 * reasons.
91 * If a client of seq_files a) implements file.write() and b) wishes to
92 * support pwrite() then that client will need to implement its own
93 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
95 file->f_mode &= ~FMODE_PWRITE;
96 return 0;
98 EXPORT_SYMBOL(seq_open);
100 static int traverse(struct seq_file *m, loff_t offset)
102 loff_t pos = 0, index;
103 int error = 0;
104 void *p;
106 m->version = 0;
107 index = 0;
108 m->count = m->from = 0;
109 if (!offset) {
110 m->index = index;
111 return 0;
113 if (!m->buf) {
114 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
115 if (!m->buf)
116 return -ENOMEM;
118 p = m->op->start(m, &index);
119 while (p) {
120 error = PTR_ERR(p);
121 if (IS_ERR(p))
122 break;
123 error = m->op->show(m, p);
124 if (error < 0)
125 break;
126 if (unlikely(error)) {
127 error = 0;
128 m->count = 0;
130 if (seq_has_overflowed(m))
131 goto Eoverflow;
132 if (pos + m->count > offset) {
133 m->from = offset - pos;
134 m->count -= m->from;
135 m->index = index;
136 break;
138 pos += m->count;
139 m->count = 0;
140 if (pos == offset) {
141 index++;
142 m->index = index;
143 break;
145 p = m->op->next(m, p, &index);
147 m->op->stop(m, p);
148 m->index = index;
149 return error;
151 Eoverflow:
152 m->op->stop(m, p);
153 kvfree(m->buf);
154 m->count = 0;
155 m->buf = seq_buf_alloc(m->size <<= 1);
156 return !m->buf ? -ENOMEM : -EAGAIN;
160 * seq_read - ->read() method for sequential files.
161 * @file: the file to read from
162 * @buf: the buffer to read to
163 * @size: the maximum number of bytes to read
164 * @ppos: the current position in the file
166 * Ready-made ->f_op->read()
168 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
170 struct seq_file *m = file->private_data;
171 size_t copied = 0;
172 loff_t pos;
173 size_t n;
174 void *p;
175 int err = 0;
177 mutex_lock(&m->lock);
180 * seq_file->op->..m_start/m_stop/m_next may do special actions
181 * or optimisations based on the file->f_version, so we want to
182 * pass the file->f_version to those methods.
184 * seq_file->version is just copy of f_version, and seq_file
185 * methods can treat it simply as file version.
186 * It is copied in first and copied out after all operations.
187 * It is convenient to have it as part of structure to avoid the
188 * need of passing another argument to all the seq_file methods.
190 m->version = file->f_version;
192 /* Don't assume *ppos is where we left it */
193 if (unlikely(*ppos != m->read_pos)) {
194 while ((err = traverse(m, *ppos)) == -EAGAIN)
196 if (err) {
197 /* With prejudice... */
198 m->read_pos = 0;
199 m->version = 0;
200 m->index = 0;
201 m->count = 0;
202 goto Done;
203 } else {
204 m->read_pos = *ppos;
208 /* grab buffer if we didn't have one */
209 if (!m->buf) {
210 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
211 if (!m->buf)
212 goto Enomem;
214 /* if not empty - flush it first */
215 if (m->count) {
216 n = min(m->count, size);
217 err = copy_to_user(buf, m->buf + m->from, n);
218 if (err)
219 goto Efault;
220 m->count -= n;
221 m->from += n;
222 size -= n;
223 buf += n;
224 copied += n;
225 if (!m->count) {
226 m->from = 0;
227 m->index++;
229 if (!size)
230 goto Done;
232 /* we need at least one record in buffer */
233 pos = m->index;
234 p = m->op->start(m, &pos);
235 while (1) {
236 err = PTR_ERR(p);
237 if (!p || IS_ERR(p))
238 break;
239 err = m->op->show(m, p);
240 if (err < 0)
241 break;
242 if (unlikely(err))
243 m->count = 0;
244 if (unlikely(!m->count)) {
245 p = m->op->next(m, p, &pos);
246 m->index = pos;
247 continue;
249 if (m->count < m->size)
250 goto Fill;
251 m->op->stop(m, p);
252 kvfree(m->buf);
253 m->count = 0;
254 m->buf = seq_buf_alloc(m->size <<= 1);
255 if (!m->buf)
256 goto Enomem;
257 m->version = 0;
258 pos = m->index;
259 p = m->op->start(m, &pos);
261 m->op->stop(m, p);
262 m->count = 0;
263 goto Done;
264 Fill:
265 /* they want more? let's try to get some more */
266 while (m->count < size) {
267 size_t offs = m->count;
268 loff_t next = pos;
269 p = m->op->next(m, p, &next);
270 if (!p || IS_ERR(p)) {
271 err = PTR_ERR(p);
272 break;
274 err = m->op->show(m, p);
275 if (seq_has_overflowed(m) || err) {
276 m->count = offs;
277 if (likely(err <= 0))
278 break;
280 pos = next;
282 m->op->stop(m, p);
283 n = min(m->count, size);
284 err = copy_to_user(buf, m->buf, n);
285 if (err)
286 goto Efault;
287 copied += n;
288 m->count -= n;
289 if (m->count)
290 m->from = n;
291 else
292 pos++;
293 m->index = pos;
294 Done:
295 if (!copied)
296 copied = err;
297 else {
298 *ppos += copied;
299 m->read_pos += copied;
301 file->f_version = m->version;
302 mutex_unlock(&m->lock);
303 return copied;
304 Enomem:
305 err = -ENOMEM;
306 goto Done;
307 Efault:
308 err = -EFAULT;
309 goto Done;
311 EXPORT_SYMBOL(seq_read);
314 * seq_lseek - ->llseek() method for sequential files.
315 * @file: the file in question
316 * @offset: new position
317 * @whence: 0 for absolute, 1 for relative position
319 * Ready-made ->f_op->llseek()
321 loff_t seq_lseek(struct file *file, loff_t offset, int whence)
323 struct seq_file *m = file->private_data;
324 loff_t retval = -EINVAL;
326 mutex_lock(&m->lock);
327 m->version = file->f_version;
328 switch (whence) {
329 case SEEK_CUR:
330 offset += file->f_pos;
331 case SEEK_SET:
332 if (offset < 0)
333 break;
334 retval = offset;
335 if (offset != m->read_pos) {
336 while ((retval = traverse(m, offset)) == -EAGAIN)
338 if (retval) {
339 /* with extreme prejudice... */
340 file->f_pos = 0;
341 m->read_pos = 0;
342 m->version = 0;
343 m->index = 0;
344 m->count = 0;
345 } else {
346 m->read_pos = offset;
347 retval = file->f_pos = offset;
349 } else {
350 file->f_pos = offset;
353 file->f_version = m->version;
354 mutex_unlock(&m->lock);
355 return retval;
357 EXPORT_SYMBOL(seq_lseek);
360 * seq_release - free the structures associated with sequential file.
361 * @file: file in question
362 * @inode: its inode
364 * Frees the structures associated with sequential file; can be used
365 * as ->f_op->release() if you don't have private data to destroy.
367 int seq_release(struct inode *inode, struct file *file)
369 struct seq_file *m = file->private_data;
370 kvfree(m->buf);
371 kfree(m);
372 return 0;
374 EXPORT_SYMBOL(seq_release);
377 * seq_escape - print string into buffer, escaping some characters
378 * @m: target buffer
379 * @s: string
380 * @esc: set of characters that need escaping
382 * Puts string into buffer, replacing each occurrence of character from
383 * @esc with usual octal escape.
384 * Use seq_has_overflowed() to check for errors.
386 void seq_escape(struct seq_file *m, const char *s, const char *esc)
388 char *buf;
389 size_t size = seq_get_buf(m, &buf);
390 int ret;
392 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
393 seq_commit(m, ret < size ? ret : -1);
395 EXPORT_SYMBOL(seq_escape);
397 void seq_vprintf(struct seq_file *m, const char *f, va_list args)
399 int len;
401 if (m->count < m->size) {
402 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
403 if (m->count + len < m->size) {
404 m->count += len;
405 return;
408 seq_set_overflow(m);
410 EXPORT_SYMBOL(seq_vprintf);
412 void seq_printf(struct seq_file *m, const char *f, ...)
414 va_list args;
416 va_start(args, f);
417 seq_vprintf(m, f, args);
418 va_end(args);
420 EXPORT_SYMBOL(seq_printf);
423 * mangle_path - mangle and copy path to buffer beginning
424 * @s: buffer start
425 * @p: beginning of path in above buffer
426 * @esc: set of characters that need escaping
428 * Copy the path from @p to @s, replacing each occurrence of character from
429 * @esc with usual octal escape.
430 * Returns pointer past last written character in @s, or NULL in case of
431 * failure.
433 char *mangle_path(char *s, const char *p, const char *esc)
435 while (s <= p) {
436 char c = *p++;
437 if (!c) {
438 return s;
439 } else if (!strchr(esc, c)) {
440 *s++ = c;
441 } else if (s + 4 > p) {
442 break;
443 } else {
444 *s++ = '\\';
445 *s++ = '0' + ((c & 0300) >> 6);
446 *s++ = '0' + ((c & 070) >> 3);
447 *s++ = '0' + (c & 07);
450 return NULL;
452 EXPORT_SYMBOL(mangle_path);
455 * seq_path - seq_file interface to print a pathname
456 * @m: the seq_file handle
457 * @path: the struct path to print
458 * @esc: set of characters to escape in the output
460 * return the absolute path of 'path', as represented by the
461 * dentry / mnt pair in the path parameter.
463 int seq_path(struct seq_file *m, const struct path *path, const char *esc)
465 char *buf;
466 size_t size = seq_get_buf(m, &buf);
467 int res = -1;
469 if (size) {
470 char *p = d_path(path, buf, size);
471 if (!IS_ERR(p)) {
472 char *end = mangle_path(buf, p, esc);
473 if (end)
474 res = end - buf;
477 seq_commit(m, res);
479 return res;
481 EXPORT_SYMBOL(seq_path);
484 * seq_file_path - seq_file interface to print a pathname of a file
485 * @m: the seq_file handle
486 * @file: the struct file to print
487 * @esc: set of characters to escape in the output
489 * return the absolute path to the file.
491 int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
493 return seq_path(m, &file->f_path, esc);
495 EXPORT_SYMBOL(seq_file_path);
498 * Same as seq_path, but relative to supplied root.
500 int seq_path_root(struct seq_file *m, const struct path *path,
501 const struct path *root, const char *esc)
503 char *buf;
504 size_t size = seq_get_buf(m, &buf);
505 int res = -ENAMETOOLONG;
507 if (size) {
508 char *p;
510 p = __d_path(path, root, buf, size);
511 if (!p)
512 return SEQ_SKIP;
513 res = PTR_ERR(p);
514 if (!IS_ERR(p)) {
515 char *end = mangle_path(buf, p, esc);
516 if (end)
517 res = end - buf;
518 else
519 res = -ENAMETOOLONG;
522 seq_commit(m, res);
524 return res < 0 && res != -ENAMETOOLONG ? res : 0;
528 * returns the path of the 'dentry' from the root of its filesystem.
530 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
532 char *buf;
533 size_t size = seq_get_buf(m, &buf);
534 int res = -1;
536 if (size) {
537 char *p = dentry_path(dentry, buf, size);
538 if (!IS_ERR(p)) {
539 char *end = mangle_path(buf, p, esc);
540 if (end)
541 res = end - buf;
544 seq_commit(m, res);
546 return res;
548 EXPORT_SYMBOL(seq_dentry);
550 static void *single_start(struct seq_file *p, loff_t *pos)
552 return NULL + (*pos == 0);
555 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
557 ++*pos;
558 return NULL;
561 static void single_stop(struct seq_file *p, void *v)
565 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
566 void *data)
568 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
569 int res = -ENOMEM;
571 if (op) {
572 op->start = single_start;
573 op->next = single_next;
574 op->stop = single_stop;
575 op->show = show;
576 res = seq_open(file, op);
577 if (!res)
578 ((struct seq_file *)file->private_data)->private = data;
579 else
580 kfree(op);
582 return res;
584 EXPORT_SYMBOL(single_open);
586 int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
587 void *data, size_t size)
589 char *buf = seq_buf_alloc(size);
590 int ret;
591 if (!buf)
592 return -ENOMEM;
593 ret = single_open(file, show, data);
594 if (ret) {
595 kvfree(buf);
596 return ret;
598 ((struct seq_file *)file->private_data)->buf = buf;
599 ((struct seq_file *)file->private_data)->size = size;
600 return 0;
602 EXPORT_SYMBOL(single_open_size);
604 int single_release(struct inode *inode, struct file *file)
606 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
607 int res = seq_release(inode, file);
608 kfree(op);
609 return res;
611 EXPORT_SYMBOL(single_release);
613 int seq_release_private(struct inode *inode, struct file *file)
615 struct seq_file *seq = file->private_data;
617 kfree(seq->private);
618 seq->private = NULL;
619 return seq_release(inode, file);
621 EXPORT_SYMBOL(seq_release_private);
623 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
624 int psize)
626 int rc;
627 void *private;
628 struct seq_file *seq;
630 private = kzalloc(psize, GFP_KERNEL);
631 if (private == NULL)
632 goto out;
634 rc = seq_open(f, ops);
635 if (rc < 0)
636 goto out_free;
638 seq = f->private_data;
639 seq->private = private;
640 return private;
642 out_free:
643 kfree(private);
644 out:
645 return NULL;
647 EXPORT_SYMBOL(__seq_open_private);
649 int seq_open_private(struct file *filp, const struct seq_operations *ops,
650 int psize)
652 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
654 EXPORT_SYMBOL(seq_open_private);
656 void seq_putc(struct seq_file *m, char c)
658 if (m->count >= m->size)
659 return;
661 m->buf[m->count++] = c;
663 EXPORT_SYMBOL(seq_putc);
665 void seq_puts(struct seq_file *m, const char *s)
667 int len = strlen(s);
669 if (m->count + len >= m->size) {
670 seq_set_overflow(m);
671 return;
673 memcpy(m->buf + m->count, s, len);
674 m->count += len;
676 EXPORT_SYMBOL(seq_puts);
679 * A helper routine for putting decimal numbers without rich format of printf().
680 * only 'unsigned long long' is supported.
681 * This routine will put one byte delimiter + number into seq_file.
682 * This routine is very quick when you show lots of numbers.
683 * In usual cases, it will be better to use seq_printf(). It's easier to read.
685 void seq_put_decimal_ull(struct seq_file *m, char delimiter,
686 unsigned long long num)
688 int len;
690 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
691 goto overflow;
693 if (delimiter)
694 m->buf[m->count++] = delimiter;
696 if (num < 10) {
697 m->buf[m->count++] = num + '0';
698 return;
701 len = num_to_str(m->buf + m->count, m->size - m->count, num);
702 if (!len)
703 goto overflow;
704 m->count += len;
705 return;
707 overflow:
708 seq_set_overflow(m);
710 EXPORT_SYMBOL(seq_put_decimal_ull);
712 void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num)
714 if (num < 0) {
715 if (m->count + 3 >= m->size) {
716 seq_set_overflow(m);
717 return;
719 if (delimiter)
720 m->buf[m->count++] = delimiter;
721 num = -num;
722 delimiter = '-';
724 seq_put_decimal_ull(m, delimiter, num);
726 EXPORT_SYMBOL(seq_put_decimal_ll);
729 * seq_write - write arbitrary data to buffer
730 * @seq: seq_file identifying the buffer to which data should be written
731 * @data: data address
732 * @len: number of bytes
734 * Return 0 on success, non-zero otherwise.
736 int seq_write(struct seq_file *seq, const void *data, size_t len)
738 if (seq->count + len < seq->size) {
739 memcpy(seq->buf + seq->count, data, len);
740 seq->count += len;
741 return 0;
743 seq_set_overflow(seq);
744 return -1;
746 EXPORT_SYMBOL(seq_write);
749 * seq_pad - write padding spaces to buffer
750 * @m: seq_file identifying the buffer to which data should be written
751 * @c: the byte to append after padding if non-zero
753 void seq_pad(struct seq_file *m, char c)
755 int size = m->pad_until - m->count;
756 if (size > 0)
757 seq_printf(m, "%*s", size, "");
758 if (c)
759 seq_putc(m, c);
761 EXPORT_SYMBOL(seq_pad);
763 /* A complete analogue of print_hex_dump() */
764 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
765 int rowsize, int groupsize, const void *buf, size_t len,
766 bool ascii)
768 const u8 *ptr = buf;
769 int i, linelen, remaining = len;
770 char *buffer;
771 size_t size;
772 int ret;
774 if (rowsize != 16 && rowsize != 32)
775 rowsize = 16;
777 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
778 linelen = min(remaining, rowsize);
779 remaining -= rowsize;
781 switch (prefix_type) {
782 case DUMP_PREFIX_ADDRESS:
783 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
784 break;
785 case DUMP_PREFIX_OFFSET:
786 seq_printf(m, "%s%.8x: ", prefix_str, i);
787 break;
788 default:
789 seq_printf(m, "%s", prefix_str);
790 break;
793 size = seq_get_buf(m, &buffer);
794 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
795 buffer, size, ascii);
796 seq_commit(m, ret < size ? ret : -1);
798 seq_putc(m, '\n');
801 EXPORT_SYMBOL(seq_hex_dump);
803 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
805 struct list_head *lh;
807 list_for_each(lh, head)
808 if (pos-- == 0)
809 return lh;
811 return NULL;
813 EXPORT_SYMBOL(seq_list_start);
815 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
817 if (!pos)
818 return head;
820 return seq_list_start(head, pos - 1);
822 EXPORT_SYMBOL(seq_list_start_head);
824 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
826 struct list_head *lh;
828 lh = ((struct list_head *)v)->next;
829 ++*ppos;
830 return lh == head ? NULL : lh;
832 EXPORT_SYMBOL(seq_list_next);
835 * seq_hlist_start - start an iteration of a hlist
836 * @head: the head of the hlist
837 * @pos: the start position of the sequence
839 * Called at seq_file->op->start().
841 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
843 struct hlist_node *node;
845 hlist_for_each(node, head)
846 if (pos-- == 0)
847 return node;
848 return NULL;
850 EXPORT_SYMBOL(seq_hlist_start);
853 * seq_hlist_start_head - start an iteration of a hlist
854 * @head: the head of the hlist
855 * @pos: the start position of the sequence
857 * Called at seq_file->op->start(). Call this function if you want to
858 * print a header at the top of the output.
860 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
862 if (!pos)
863 return SEQ_START_TOKEN;
865 return seq_hlist_start(head, pos - 1);
867 EXPORT_SYMBOL(seq_hlist_start_head);
870 * seq_hlist_next - move to the next position of the hlist
871 * @v: the current iterator
872 * @head: the head of the hlist
873 * @ppos: the current position
875 * Called at seq_file->op->next().
877 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
878 loff_t *ppos)
880 struct hlist_node *node = v;
882 ++*ppos;
883 if (v == SEQ_START_TOKEN)
884 return head->first;
885 else
886 return node->next;
888 EXPORT_SYMBOL(seq_hlist_next);
891 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
892 * @head: the head of the hlist
893 * @pos: the start position of the sequence
895 * Called at seq_file->op->start().
897 * This list-traversal primitive may safely run concurrently with
898 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
899 * as long as the traversal is guarded by rcu_read_lock().
901 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
902 loff_t pos)
904 struct hlist_node *node;
906 __hlist_for_each_rcu(node, head)
907 if (pos-- == 0)
908 return node;
909 return NULL;
911 EXPORT_SYMBOL(seq_hlist_start_rcu);
914 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
915 * @head: the head of the hlist
916 * @pos: the start position of the sequence
918 * Called at seq_file->op->start(). Call this function if you want to
919 * print a header at the top of the output.
921 * This list-traversal primitive may safely run concurrently with
922 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
923 * as long as the traversal is guarded by rcu_read_lock().
925 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
926 loff_t pos)
928 if (!pos)
929 return SEQ_START_TOKEN;
931 return seq_hlist_start_rcu(head, pos - 1);
933 EXPORT_SYMBOL(seq_hlist_start_head_rcu);
936 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
937 * @v: the current iterator
938 * @head: the head of the hlist
939 * @ppos: the current position
941 * Called at seq_file->op->next().
943 * This list-traversal primitive may safely run concurrently with
944 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
945 * as long as the traversal is guarded by rcu_read_lock().
947 struct hlist_node *seq_hlist_next_rcu(void *v,
948 struct hlist_head *head,
949 loff_t *ppos)
951 struct hlist_node *node = v;
953 ++*ppos;
954 if (v == SEQ_START_TOKEN)
955 return rcu_dereference(head->first);
956 else
957 return rcu_dereference(node->next);
959 EXPORT_SYMBOL(seq_hlist_next_rcu);
962 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
963 * @head: pointer to percpu array of struct hlist_heads
964 * @cpu: pointer to cpu "cursor"
965 * @pos: start position of sequence
967 * Called at seq_file->op->start().
969 struct hlist_node *
970 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
972 struct hlist_node *node;
974 for_each_possible_cpu(*cpu) {
975 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
976 if (pos-- == 0)
977 return node;
980 return NULL;
982 EXPORT_SYMBOL(seq_hlist_start_percpu);
985 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
986 * @v: pointer to current hlist_node
987 * @head: pointer to percpu array of struct hlist_heads
988 * @cpu: pointer to cpu "cursor"
989 * @pos: start position of sequence
991 * Called at seq_file->op->next().
993 struct hlist_node *
994 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
995 int *cpu, loff_t *pos)
997 struct hlist_node *node = v;
999 ++*pos;
1001 if (node->next)
1002 return node->next;
1004 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1005 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1006 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1008 if (!hlist_empty(bucket))
1009 return bucket->first;
1011 return NULL;
1013 EXPORT_SYMBOL(seq_hlist_next_percpu);