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