1
2
3
4
5
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
18#include <linux/uaccess.h>
19#include <asm/page.h>
20
21static void seq_set_overflow(struct seq_file *m)
22{
23 m->count = m->size;
24}
25
26static void *seq_buf_alloc(unsigned long size)
27{
28 return kvmalloc(size, GFP_KERNEL);
29}
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47int seq_open(struct file *file, const struct seq_operations *op)
48{
49 struct seq_file *p;
50
51 WARN_ON(file->private_data);
52
53 p = kzalloc(sizeof(*p), GFP_KERNEL);
54 if (!p)
55 return -ENOMEM;
56
57 file->private_data = p;
58
59 mutex_init(&p->lock);
60 p->op = op;
61
62
63
64 p->file = file;
65
66
67
68
69
70
71 file->f_version = 0;
72
73
74
75
76
77
78
79
80
81
82 file->f_mode &= ~FMODE_PWRITE;
83 return 0;
84}
85EXPORT_SYMBOL(seq_open);
86
87static int traverse(struct seq_file *m, loff_t offset)
88{
89 loff_t pos = 0, index;
90 int error = 0;
91 void *p;
92
93 m->version = 0;
94 index = 0;
95 m->count = m->from = 0;
96 if (!offset) {
97 m->index = index;
98 return 0;
99 }
100 if (!m->buf) {
101 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
102 if (!m->buf)
103 return -ENOMEM;
104 }
105 p = m->op->start(m, &index);
106 while (p) {
107 error = PTR_ERR(p);
108 if (IS_ERR(p))
109 break;
110 error = m->op->show(m, p);
111 if (error < 0)
112 break;
113 if (unlikely(error)) {
114 error = 0;
115 m->count = 0;
116 }
117 if (seq_has_overflowed(m))
118 goto Eoverflow;
119 if (pos + m->count > offset) {
120 m->from = offset - pos;
121 m->count -= m->from;
122 m->index = index;
123 break;
124 }
125 pos += m->count;
126 m->count = 0;
127 if (pos == offset) {
128 index++;
129 m->index = index;
130 break;
131 }
132 p = m->op->next(m, p, &index);
133 }
134 m->op->stop(m, p);
135 m->index = index;
136 return error;
137
138Eoverflow:
139 m->op->stop(m, p);
140 kvfree(m->buf);
141 m->count = 0;
142 m->buf = seq_buf_alloc(m->size <<= 1);
143 return !m->buf ? -ENOMEM : -EAGAIN;
144}
145
146
147
148
149
150
151
152
153
154
155ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
156{
157 struct seq_file *m = file->private_data;
158 size_t copied = 0;
159 loff_t pos;
160 size_t n;
161 void *p;
162 int err = 0;
163
164 mutex_lock(&m->lock);
165
166
167
168
169
170
171
172
173
174
175
176
177 m->version = file->f_version;
178
179
180
181
182
183 if (*ppos == 0)
184 m->index = 0;
185
186
187 if (unlikely(*ppos != m->read_pos)) {
188 while ((err = traverse(m, *ppos)) == -EAGAIN)
189 ;
190 if (err) {
191
192 m->read_pos = 0;
193 m->version = 0;
194 m->index = 0;
195 m->count = 0;
196 goto Done;
197 } else {
198 m->read_pos = *ppos;
199 }
200 }
201
202
203 if (!m->buf) {
204 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
205 if (!m->buf)
206 goto Enomem;
207 }
208
209 if (m->count) {
210 n = min(m->count, size);
211 err = copy_to_user(buf, m->buf + m->from, n);
212 if (err)
213 goto Efault;
214 m->count -= n;
215 m->from += n;
216 size -= n;
217 buf += n;
218 copied += n;
219 if (!m->count) {
220 m->from = 0;
221 m->index++;
222 }
223 if (!size)
224 goto Done;
225 }
226
227 pos = m->index;
228 p = m->op->start(m, &pos);
229 while (1) {
230 err = PTR_ERR(p);
231 if (!p || IS_ERR(p))
232 break;
233 err = m->op->show(m, p);
234 if (err < 0)
235 break;
236 if (unlikely(err))
237 m->count = 0;
238 if (unlikely(!m->count)) {
239 p = m->op->next(m, p, &pos);
240 m->index = pos;
241 continue;
242 }
243 if (m->count < m->size)
244 goto Fill;
245 m->op->stop(m, p);
246 kvfree(m->buf);
247 m->count = 0;
248 m->buf = seq_buf_alloc(m->size <<= 1);
249 if (!m->buf)
250 goto Enomem;
251 m->version = 0;
252 pos = m->index;
253 p = m->op->start(m, &pos);
254 }
255 m->op->stop(m, p);
256 m->count = 0;
257 goto Done;
258Fill:
259
260 while (m->count < size) {
261 size_t offs = m->count;
262 loff_t next = pos;
263 p = m->op->next(m, p, &next);
264 if (!p || IS_ERR(p)) {
265 err = PTR_ERR(p);
266 break;
267 }
268 err = m->op->show(m, p);
269 if (seq_has_overflowed(m) || err) {
270 m->count = offs;
271 if (likely(err <= 0))
272 break;
273 }
274 pos = next;
275 }
276 m->op->stop(m, p);
277 n = min(m->count, size);
278 err = copy_to_user(buf, m->buf, n);
279 if (err)
280 goto Efault;
281 copied += n;
282 m->count -= n;
283 if (m->count)
284 m->from = n;
285 else
286 pos++;
287 m->index = pos;
288Done:
289 if (!copied)
290 copied = err;
291 else {
292 *ppos += copied;
293 m->read_pos += copied;
294 }
295 file->f_version = m->version;
296 mutex_unlock(&m->lock);
297 return copied;
298Enomem:
299 err = -ENOMEM;
300 goto Done;
301Efault:
302 err = -EFAULT;
303 goto Done;
304}
305EXPORT_SYMBOL(seq_read);
306
307
308
309
310
311
312
313
314
315loff_t seq_lseek(struct file *file, loff_t offset, int whence)
316{
317 struct seq_file *m = file->private_data;
318 loff_t retval = -EINVAL;
319
320 mutex_lock(&m->lock);
321 m->version = file->f_version;
322 switch (whence) {
323 case SEEK_CUR:
324 offset += file->f_pos;
325 case SEEK_SET:
326 if (offset < 0)
327 break;
328 retval = offset;
329 if (offset != m->read_pos) {
330 while ((retval = traverse(m, offset)) == -EAGAIN)
331 ;
332 if (retval) {
333
334 file->f_pos = 0;
335 m->read_pos = 0;
336 m->version = 0;
337 m->index = 0;
338 m->count = 0;
339 } else {
340 m->read_pos = offset;
341 retval = file->f_pos = offset;
342 }
343 } else {
344 file->f_pos = offset;
345 }
346 }
347 file->f_version = m->version;
348 mutex_unlock(&m->lock);
349 return retval;
350}
351EXPORT_SYMBOL(seq_lseek);
352
353
354
355
356
357
358
359
360
361int seq_release(struct inode *inode, struct file *file)
362{
363 struct seq_file *m = file->private_data;
364 kvfree(m->buf);
365 kfree(m);
366 return 0;
367}
368EXPORT_SYMBOL(seq_release);
369
370
371
372
373
374
375
376
377
378
379
380void seq_escape(struct seq_file *m, const char *s, const char *esc)
381{
382 char *buf;
383 size_t size = seq_get_buf(m, &buf);
384 int ret;
385
386 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
387 seq_commit(m, ret < size ? ret : -1);
388}
389EXPORT_SYMBOL(seq_escape);
390
391void seq_vprintf(struct seq_file *m, const char *f, va_list args)
392{
393 int len;
394
395 if (m->count < m->size) {
396 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
397 if (m->count + len < m->size) {
398 m->count += len;
399 return;
400 }
401 }
402 seq_set_overflow(m);
403}
404EXPORT_SYMBOL(seq_vprintf);
405
406void seq_printf(struct seq_file *m, const char *f, ...)
407{
408 va_list args;
409
410 va_start(args, f);
411 seq_vprintf(m, f, args);
412 va_end(args);
413}
414EXPORT_SYMBOL(seq_printf);
415
416
417
418
419
420
421
422
423
424
425
426
427char *mangle_path(char *s, const char *p, const char *esc)
428{
429 while (s <= p) {
430 char c = *p++;
431 if (!c) {
432 return s;
433 } else if (!strchr(esc, c)) {
434 *s++ = c;
435 } else if (s + 4 > p) {
436 break;
437 } else {
438 *s++ = '\\';
439 *s++ = '0' + ((c & 0300) >> 6);
440 *s++ = '0' + ((c & 070) >> 3);
441 *s++ = '0' + (c & 07);
442 }
443 }
444 return NULL;
445}
446EXPORT_SYMBOL(mangle_path);
447
448
449
450
451
452
453
454
455
456
457int seq_path(struct seq_file *m, const struct path *path, const char *esc)
458{
459 char *buf;
460 size_t size = seq_get_buf(m, &buf);
461 int res = -1;
462
463 if (size) {
464 char *p = d_path(path, buf, size);
465 if (!IS_ERR(p)) {
466 char *end = mangle_path(buf, p, esc);
467 if (end)
468 res = end - buf;
469 }
470 }
471 seq_commit(m, res);
472
473 return res;
474}
475EXPORT_SYMBOL(seq_path);
476
477
478
479
480
481
482
483
484
485int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
486{
487 return seq_path(m, &file->f_path, esc);
488}
489EXPORT_SYMBOL(seq_file_path);
490
491
492
493
494int seq_path_root(struct seq_file *m, const struct path *path,
495 const struct path *root, const char *esc)
496{
497 char *buf;
498 size_t size = seq_get_buf(m, &buf);
499 int res = -ENAMETOOLONG;
500
501 if (size) {
502 char *p;
503
504 p = __d_path(path, root, buf, size);
505 if (!p)
506 return SEQ_SKIP;
507 res = PTR_ERR(p);
508 if (!IS_ERR(p)) {
509 char *end = mangle_path(buf, p, esc);
510 if (end)
511 res = end - buf;
512 else
513 res = -ENAMETOOLONG;
514 }
515 }
516 seq_commit(m, res);
517
518 return res < 0 && res != -ENAMETOOLONG ? res : 0;
519}
520
521
522
523
524int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
525{
526 char *buf;
527 size_t size = seq_get_buf(m, &buf);
528 int res = -1;
529
530 if (size) {
531 char *p = dentry_path(dentry, buf, size);
532 if (!IS_ERR(p)) {
533 char *end = mangle_path(buf, p, esc);
534 if (end)
535 res = end - buf;
536 }
537 }
538 seq_commit(m, res);
539
540 return res;
541}
542EXPORT_SYMBOL(seq_dentry);
543
544static void *single_start(struct seq_file *p, loff_t *pos)
545{
546 return NULL + (*pos == 0);
547}
548
549static void *single_next(struct seq_file *p, void *v, loff_t *pos)
550{
551 ++*pos;
552 return NULL;
553}
554
555static void single_stop(struct seq_file *p, void *v)
556{
557}
558
559int single_open(struct file *file, int (*show)(struct seq_file *, void *),
560 void *data)
561{
562 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
563 int res = -ENOMEM;
564
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);
575 }
576 return res;
577}
578EXPORT_SYMBOL(single_open);
579
580int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
581 void *data, size_t size)
582{
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;
591 }
592 ((struct seq_file *)file->private_data)->buf = buf;
593 ((struct seq_file *)file->private_data)->size = size;
594 return 0;
595}
596EXPORT_SYMBOL(single_open_size);
597
598int single_release(struct inode *inode, struct file *file)
599{
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;
604}
605EXPORT_SYMBOL(single_release);
606
607int seq_release_private(struct inode *inode, struct file *file)
608{
609 struct seq_file *seq = file->private_data;
610
611 kfree(seq->private);
612 seq->private = NULL;
613 return seq_release(inode, file);
614}
615EXPORT_SYMBOL(seq_release_private);
616
617void *__seq_open_private(struct file *f, const struct seq_operations *ops,
618 int psize)
619{
620 int rc;
621 void *private;
622 struct seq_file *seq;
623
624 private = kzalloc(psize, GFP_KERNEL);
625 if (private == NULL)
626 goto out;
627
628 rc = seq_open(f, ops);
629 if (rc < 0)
630 goto out_free;
631
632 seq = f->private_data;
633 seq->private = private;
634 return private;
635
636out_free:
637 kfree(private);
638out:
639 return NULL;
640}
641EXPORT_SYMBOL(__seq_open_private);
642
643int seq_open_private(struct file *filp, const struct seq_operations *ops,
644 int psize)
645{
646 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
647}
648EXPORT_SYMBOL(seq_open_private);
649
650void seq_putc(struct seq_file *m, char c)
651{
652 if (m->count >= m->size)
653 return;
654
655 m->buf[m->count++] = c;
656}
657EXPORT_SYMBOL(seq_putc);
658
659void seq_puts(struct seq_file *m, const char *s)
660{
661 int len = strlen(s);
662
663 if (m->count + len >= m->size) {
664 seq_set_overflow(m);
665 return;
666 }
667 memcpy(m->buf + m->count, s, len);
668 m->count += len;
669}
670EXPORT_SYMBOL(seq_puts);
671
672
673
674
675
676
677
678
679void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
680 unsigned long long num)
681{
682 int len;
683
684 if (m->count + 2 >= m->size)
685 goto overflow;
686
687 len = strlen(delimiter);
688 if (m->count + len >= m->size)
689 goto overflow;
690
691 memcpy(m->buf + m->count, delimiter, len);
692 m->count += len;
693
694 if (m->count + 1 >= m->size)
695 goto overflow;
696
697 if (num < 10) {
698 m->buf[m->count++] = num + '0';
699 return;
700 }
701
702 len = num_to_str(m->buf + m->count, m->size - m->count, num);
703 if (!len)
704 goto overflow;
705
706 m->count += len;
707 return;
708
709overflow:
710 seq_set_overflow(m);
711}
712EXPORT_SYMBOL(seq_put_decimal_ull);
713
714void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
715{
716 int len;
717
718 if (m->count + 3 >= m->size)
719 goto overflow;
720
721 len = strlen(delimiter);
722 if (m->count + len >= m->size)
723 goto overflow;
724
725 memcpy(m->buf + m->count, delimiter, len);
726 m->count += len;
727
728 if (m->count + 2 >= m->size)
729 goto overflow;
730
731 if (num < 0) {
732 m->buf[m->count++] = '-';
733 num = -num;
734 }
735
736 if (num < 10) {
737 m->buf[m->count++] = num + '0';
738 return;
739 }
740
741 len = num_to_str(m->buf + m->count, m->size - m->count, num);
742 if (!len)
743 goto overflow;
744
745 m->count += len;
746 return;
747
748overflow:
749 seq_set_overflow(m);
750}
751EXPORT_SYMBOL(seq_put_decimal_ll);
752
753
754
755
756
757
758
759
760
761int seq_write(struct seq_file *seq, const void *data, size_t len)
762{
763 if (seq->count + len < seq->size) {
764 memcpy(seq->buf + seq->count, data, len);
765 seq->count += len;
766 return 0;
767 }
768 seq_set_overflow(seq);
769 return -1;
770}
771EXPORT_SYMBOL(seq_write);
772
773
774
775
776
777
778void seq_pad(struct seq_file *m, char c)
779{
780 int size = m->pad_until - m->count;
781 if (size > 0)
782 seq_printf(m, "%*s", size, "");
783 if (c)
784 seq_putc(m, c);
785}
786EXPORT_SYMBOL(seq_pad);
787
788
789void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
790 int rowsize, int groupsize, const void *buf, size_t len,
791 bool ascii)
792{
793 const u8 *ptr = buf;
794 int i, linelen, remaining = len;
795 char *buffer;
796 size_t size;
797 int ret;
798
799 if (rowsize != 16 && rowsize != 32)
800 rowsize = 16;
801
802 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
803 linelen = min(remaining, rowsize);
804 remaining -= rowsize;
805
806 switch (prefix_type) {
807 case DUMP_PREFIX_ADDRESS:
808 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
809 break;
810 case DUMP_PREFIX_OFFSET:
811 seq_printf(m, "%s%.8x: ", prefix_str, i);
812 break;
813 default:
814 seq_printf(m, "%s", prefix_str);
815 break;
816 }
817
818 size = seq_get_buf(m, &buffer);
819 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
820 buffer, size, ascii);
821 seq_commit(m, ret < size ? ret : -1);
822
823 seq_putc(m, '\n');
824 }
825}
826EXPORT_SYMBOL(seq_hex_dump);
827
828struct list_head *seq_list_start(struct list_head *head, loff_t pos)
829{
830 struct list_head *lh;
831
832 list_for_each(lh, head)
833 if (pos-- == 0)
834 return lh;
835
836 return NULL;
837}
838EXPORT_SYMBOL(seq_list_start);
839
840struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
841{
842 if (!pos)
843 return head;
844
845 return seq_list_start(head, pos - 1);
846}
847EXPORT_SYMBOL(seq_list_start_head);
848
849struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
850{
851 struct list_head *lh;
852
853 lh = ((struct list_head *)v)->next;
854 ++*ppos;
855 return lh == head ? NULL : lh;
856}
857EXPORT_SYMBOL(seq_list_next);
858
859
860
861
862
863
864
865
866struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
867{
868 struct hlist_node *node;
869
870 hlist_for_each(node, head)
871 if (pos-- == 0)
872 return node;
873 return NULL;
874}
875EXPORT_SYMBOL(seq_hlist_start);
876
877
878
879
880
881
882
883
884
885struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
886{
887 if (!pos)
888 return SEQ_START_TOKEN;
889
890 return seq_hlist_start(head, pos - 1);
891}
892EXPORT_SYMBOL(seq_hlist_start_head);
893
894
895
896
897
898
899
900
901
902struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
903 loff_t *ppos)
904{
905 struct hlist_node *node = v;
906
907 ++*ppos;
908 if (v == SEQ_START_TOKEN)
909 return head->first;
910 else
911 return node->next;
912}
913EXPORT_SYMBOL(seq_hlist_next);
914
915
916
917
918
919
920
921
922
923
924
925
926struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
927 loff_t pos)
928{
929 struct hlist_node *node;
930
931 __hlist_for_each_rcu(node, head)
932 if (pos-- == 0)
933 return node;
934 return NULL;
935}
936EXPORT_SYMBOL(seq_hlist_start_rcu);
937
938
939
940
941
942
943
944
945
946
947
948
949
950struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
951 loff_t pos)
952{
953 if (!pos)
954 return SEQ_START_TOKEN;
955
956 return seq_hlist_start_rcu(head, pos - 1);
957}
958EXPORT_SYMBOL(seq_hlist_start_head_rcu);
959
960
961
962
963
964
965
966
967
968
969
970
971
972struct hlist_node *seq_hlist_next_rcu(void *v,
973 struct hlist_head *head,
974 loff_t *ppos)
975{
976 struct hlist_node *node = v;
977
978 ++*ppos;
979 if (v == SEQ_START_TOKEN)
980 return rcu_dereference(head->first);
981 else
982 return rcu_dereference(node->next);
983}
984EXPORT_SYMBOL(seq_hlist_next_rcu);
985
986
987
988
989
990
991
992
993
994struct hlist_node *
995seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
996{
997 struct hlist_node *node;
998
999 for_each_possible_cpu(*cpu) {
1000 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1001 if (pos-- == 0)
1002 return node;
1003 }
1004 }
1005 return NULL;
1006}
1007EXPORT_SYMBOL(seq_hlist_start_percpu);
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018struct hlist_node *
1019seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1020 int *cpu, loff_t *pos)
1021{
1022 struct hlist_node *node = v;
1023
1024 ++*pos;
1025
1026 if (node->next)
1027 return node->next;
1028
1029 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1030 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1031 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1032
1033 if (!hlist_empty(bucket))
1034 return bucket->first;
1035 }
1036 return NULL;
1037}
1038EXPORT_SYMBOL(seq_hlist_next_percpu);
1039