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