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