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