blob: 5d54205e486b6d9351840f768829a3e94d28bf23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/module.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 * seq_open - initialize sequential file
18 * @file: file we initialize
19 * @op: method table describing the sequence
20 *
21 * seq_open() sets @file, associating it with a sequence described
22 * by @op. @op->start() sets the iterator up and returns the first
23 * element of sequence. @op->stop() shuts it down. @op->next()
24 * returns the next element of sequence. @op->show() prints element
25 * into the buffer. In case of error ->start() and ->next() return
26 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
27 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040028 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080030int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Al Viro1abe77b2005-11-07 17:15:34 -050032 struct seq_file *p = file->private_data;
33
34 if (!p) {
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
36 if (!p)
37 return -ENOMEM;
38 file->private_data = p;
39 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080041 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 p->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /*
45 * Wrappers around seq_open(e.g. swaps_open) need to be
46 * aware of this. If they set f_version themselves, they
47 * should call seq_open first and then set f_version.
48 */
49 file->f_version = 0;
50
51 /* SEQ files support lseek, but not pread/pwrite */
52 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
53 return 0;
54}
55EXPORT_SYMBOL(seq_open);
56
57/**
58 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -070059 * @file: the file to read from
60 * @buf: the buffer to read to
61 * @size: the maximum number of bytes to read
62 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * Ready-made ->f_op->read()
65 */
66ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
67{
68 struct seq_file *m = (struct seq_file *)file->private_data;
69 size_t copied = 0;
70 loff_t pos;
71 size_t n;
72 void *p;
73 int err = 0;
74
Ingo Molnar0ac17592006-03-23 03:00:37 -080075 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /*
77 * seq_file->op->..m_start/m_stop/m_next may do special actions
78 * or optimisations based on the file->f_version, so we want to
79 * pass the file->f_version to those methods.
80 *
81 * seq_file->version is just copy of f_version, and seq_file
82 * methods can treat it simply as file version.
83 * It is copied in first and copied out after all operations.
84 * It is convenient to have it as part of structure to avoid the
85 * need of passing another argument to all the seq_file methods.
86 */
87 m->version = file->f_version;
88 /* grab buffer if we didn't have one */
89 if (!m->buf) {
90 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
91 if (!m->buf)
92 goto Enomem;
93 }
94 /* if not empty - flush it first */
95 if (m->count) {
96 n = min(m->count, size);
97 err = copy_to_user(buf, m->buf + m->from, n);
98 if (err)
99 goto Efault;
100 m->count -= n;
101 m->from += n;
102 size -= n;
103 buf += n;
104 copied += n;
105 if (!m->count)
106 m->index++;
107 if (!size)
108 goto Done;
109 }
110 /* we need at least one record in buffer */
111 while (1) {
112 pos = m->index;
113 p = m->op->start(m, &pos);
114 err = PTR_ERR(p);
115 if (!p || IS_ERR(p))
116 break;
117 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400118 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
Al Viro521b5d02008-03-28 00:46:41 -0400120 if (unlikely(err))
121 m->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (m->count < m->size)
123 goto Fill;
124 m->op->stop(m, p);
125 kfree(m->buf);
126 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
127 if (!m->buf)
128 goto Enomem;
129 m->count = 0;
130 m->version = 0;
131 }
132 m->op->stop(m, p);
133 m->count = 0;
134 goto Done;
135Fill:
136 /* they want more? let's try to get some more */
137 while (m->count < size) {
138 size_t offs = m->count;
139 loff_t next = pos;
140 p = m->op->next(m, p, &next);
141 if (!p || IS_ERR(p)) {
142 err = PTR_ERR(p);
143 break;
144 }
145 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400146 if (m->count == m->size || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400148 if (likely(err <= 0))
149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 pos = next;
152 }
153 m->op->stop(m, p);
154 n = min(m->count, size);
155 err = copy_to_user(buf, m->buf, n);
156 if (err)
157 goto Efault;
158 copied += n;
159 m->count -= n;
160 if (m->count)
161 m->from = n;
162 else
163 pos++;
164 m->index = pos;
165Done:
166 if (!copied)
167 copied = err;
168 else
169 *ppos += copied;
170 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800171 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return copied;
173Enomem:
174 err = -ENOMEM;
175 goto Done;
176Efault:
177 err = -EFAULT;
178 goto Done;
179}
180EXPORT_SYMBOL(seq_read);
181
182static int traverse(struct seq_file *m, loff_t offset)
183{
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700184 loff_t pos = 0, index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int error = 0;
186 void *p;
187
188 m->version = 0;
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700189 index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 m->count = m->from = 0;
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700191 if (!offset) {
192 m->index = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!m->buf) {
196 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
197 if (!m->buf)
198 return -ENOMEM;
199 }
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700200 p = m->op->start(m, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 while (p) {
202 error = PTR_ERR(p);
203 if (IS_ERR(p))
204 break;
205 error = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400206 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
Al Viro521b5d02008-03-28 00:46:41 -0400208 if (unlikely(error)) {
209 error = 0;
210 m->count = 0;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (m->count == m->size)
213 goto Eoverflow;
214 if (pos + m->count > offset) {
215 m->from = offset - pos;
216 m->count -= m->from;
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700217 m->index = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220 pos += m->count;
221 m->count = 0;
222 if (pos == offset) {
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700223 index++;
224 m->index = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 }
Alexey Dobriyancb510b82007-07-15 23:40:39 -0700227 p = m->op->next(m, p, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 m->op->stop(m, p);
230 return error;
231
232Eoverflow:
233 m->op->stop(m, p);
234 kfree(m->buf);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
236 return !m->buf ? -ENOMEM : -EAGAIN;
237}
238
239/**
240 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700241 * @file: the file in question
242 * @offset: new position
243 * @origin: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
245 * Ready-made ->f_op->llseek()
246 */
247loff_t seq_lseek(struct file *file, loff_t offset, int origin)
248{
249 struct seq_file *m = (struct seq_file *)file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200250 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Ingo Molnar0ac17592006-03-23 03:00:37 -0800252 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 m->version = file->f_version;
254 switch (origin) {
255 case 1:
256 offset += file->f_pos;
257 case 0:
258 if (offset < 0)
259 break;
260 retval = offset;
261 if (offset != file->f_pos) {
262 while ((retval=traverse(m, offset)) == -EAGAIN)
263 ;
264 if (retval) {
265 /* with extreme prejudice... */
266 file->f_pos = 0;
267 m->version = 0;
268 m->index = 0;
269 m->count = 0;
270 } else {
271 retval = file->f_pos = offset;
272 }
273 }
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700276 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return retval;
278}
279EXPORT_SYMBOL(seq_lseek);
280
281/**
282 * seq_release - free the structures associated with sequential file.
283 * @file: file in question
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800284 * @inode: file->f_path.dentry->d_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
286 * Frees the structures associated with sequential file; can be used
287 * as ->f_op->release() if you don't have private data to destroy.
288 */
289int seq_release(struct inode *inode, struct file *file)
290{
291 struct seq_file *m = (struct seq_file *)file->private_data;
292 kfree(m->buf);
293 kfree(m);
294 return 0;
295}
296EXPORT_SYMBOL(seq_release);
297
298/**
299 * seq_escape - print string into buffer, escaping some characters
300 * @m: target buffer
301 * @s: string
302 * @esc: set of characters that need escaping
303 *
304 * Puts string into buffer, replacing each occurrence of character from
305 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
306 * case of overflow.
307 */
308int seq_escape(struct seq_file *m, const char *s, const char *esc)
309{
310 char *end = m->buf + m->size;
311 char *p;
312 char c;
313
314 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
315 if (!strchr(esc, c)) {
316 *p++ = c;
317 continue;
318 }
319 if (p + 3 < end) {
320 *p++ = '\\';
321 *p++ = '0' + ((c & 0300) >> 6);
322 *p++ = '0' + ((c & 070) >> 3);
323 *p++ = '0' + (c & 07);
324 continue;
325 }
326 m->count = m->size;
327 return -1;
328 }
329 m->count = p - m->buf;
330 return 0;
331}
332EXPORT_SYMBOL(seq_escape);
333
334int seq_printf(struct seq_file *m, const char *f, ...)
335{
336 va_list args;
337 int len;
338
339 if (m->count < m->size) {
340 va_start(args, f);
341 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
342 va_end(args);
343 if (m->count + len < m->size) {
344 m->count += len;
345 return 0;
346 }
347 }
348 m->count = m->size;
349 return -1;
350}
351EXPORT_SYMBOL(seq_printf);
352
Ram Pai6092d042008-03-27 13:06:20 +0100353static char *mangle_path(char *s, char *p, char *esc)
354{
355 while (s <= p) {
356 char c = *p++;
357 if (!c) {
358 return s;
359 } else if (!strchr(esc, c)) {
360 *s++ = c;
361 } else if (s + 4 > p) {
362 break;
363 } else {
364 *s++ = '\\';
365 *s++ = '0' + ((c & 0300) >> 6);
366 *s++ = '0' + ((c & 070) >> 3);
367 *s++ = '0' + (c & 07);
368 }
369 }
370 return NULL;
371}
372
373/*
374 * return the absolute path of 'dentry' residing in mount 'mnt'.
375 */
Jan Blunckc32c2f62008-02-14 19:38:43 -0800376int seq_path(struct seq_file *m, struct path *path, char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 if (m->count < m->size) {
379 char *s = m->buf + m->count;
Jan Blunckcf28b482008-02-14 19:38:44 -0800380 char *p = d_path(path, s, m->size - m->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!IS_ERR(p)) {
Ram Pai6092d042008-03-27 13:06:20 +0100382 s = mangle_path(s, p, esc);
383 if (s) {
384 p = m->buf + m->count;
385 m->count = s - m->buf;
386 return s - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
389 }
390 m->count = m->size;
391 return -1;
392}
393EXPORT_SYMBOL(seq_path);
394
Ram Pai6092d042008-03-27 13:06:20 +0100395/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100396 * Same as seq_path, but relative to supplied root.
397 *
398 * root may be changed, see __d_path().
399 */
400int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
401 char *esc)
402{
403 int err = -ENAMETOOLONG;
404 if (m->count < m->size) {
405 char *s = m->buf + m->count;
406 char *p;
407
408 spin_lock(&dcache_lock);
409 p = __d_path(path, root, s, m->size - m->count);
410 spin_unlock(&dcache_lock);
411 err = PTR_ERR(p);
412 if (!IS_ERR(p)) {
413 s = mangle_path(s, p, esc);
414 if (s) {
415 p = m->buf + m->count;
416 m->count = s - m->buf;
417 return 0;
418 }
419 }
420 }
421 m->count = m->size;
422 return err;
423}
424
425/*
Ram Pai6092d042008-03-27 13:06:20 +0100426 * returns the path of the 'dentry' from the root of its filesystem.
427 */
428int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
429{
430 if (m->count < m->size) {
431 char *s = m->buf + m->count;
432 char *p = dentry_path(dentry, s, m->size - m->count);
433 if (!IS_ERR(p)) {
434 s = mangle_path(s, p, esc);
435 if (s) {
436 p = m->buf + m->count;
437 m->count = s - m->buf;
438 return s - p;
439 }
440 }
441 }
442 m->count = m->size;
443 return -1;
444}
445
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700446int seq_bitmap(struct seq_file *m, unsigned long *bits, unsigned int nr_bits)
447{
448 size_t len = bitmap_scnprintf_len(nr_bits);
449
450 if (m->count + len < m->size) {
451 bitmap_scnprintf(m->buf + m->count, m->size - m->count,
452 bits, nr_bits);
453 m->count += len;
454 return 0;
455 }
456 m->count = m->size;
457 return -1;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static void *single_start(struct seq_file *p, loff_t *pos)
461{
462 return NULL + (*pos == 0);
463}
464
465static void *single_next(struct seq_file *p, void *v, loff_t *pos)
466{
467 ++*pos;
468 return NULL;
469}
470
471static void single_stop(struct seq_file *p, void *v)
472{
473}
474
475int single_open(struct file *file, int (*show)(struct seq_file *, void *),
476 void *data)
477{
478 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
479 int res = -ENOMEM;
480
481 if (op) {
482 op->start = single_start;
483 op->next = single_next;
484 op->stop = single_stop;
485 op->show = show;
486 res = seq_open(file, op);
487 if (!res)
488 ((struct seq_file *)file->private_data)->private = data;
489 else
490 kfree(op);
491 }
492 return res;
493}
494EXPORT_SYMBOL(single_open);
495
496int single_release(struct inode *inode, struct file *file)
497{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800498 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int res = seq_release(inode, file);
500 kfree(op);
501 return res;
502}
503EXPORT_SYMBOL(single_release);
504
505int seq_release_private(struct inode *inode, struct file *file)
506{
507 struct seq_file *seq = file->private_data;
508
509 kfree(seq->private);
510 seq->private = NULL;
511 return seq_release(inode, file);
512}
513EXPORT_SYMBOL(seq_release_private);
514
Pavel Emelyanov39699032007-10-10 02:28:42 -0700515void *__seq_open_private(struct file *f, const struct seq_operations *ops,
516 int psize)
517{
518 int rc;
519 void *private;
520 struct seq_file *seq;
521
522 private = kzalloc(psize, GFP_KERNEL);
523 if (private == NULL)
524 goto out;
525
526 rc = seq_open(f, ops);
527 if (rc < 0)
528 goto out_free;
529
530 seq = f->private_data;
531 seq->private = private;
532 return private;
533
534out_free:
535 kfree(private);
536out:
537 return NULL;
538}
539EXPORT_SYMBOL(__seq_open_private);
540
541int seq_open_private(struct file *filp, const struct seq_operations *ops,
542 int psize)
543{
544 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
545}
546EXPORT_SYMBOL(seq_open_private);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548int seq_putc(struct seq_file *m, char c)
549{
550 if (m->count < m->size) {
551 m->buf[m->count++] = c;
552 return 0;
553 }
554 return -1;
555}
556EXPORT_SYMBOL(seq_putc);
557
558int seq_puts(struct seq_file *m, const char *s)
559{
560 int len = strlen(s);
561 if (m->count + len < m->size) {
562 memcpy(m->buf + m->count, s, len);
563 m->count += len;
564 return 0;
565 }
566 m->count = m->size;
567 return -1;
568}
569EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700570
571struct list_head *seq_list_start(struct list_head *head, loff_t pos)
572{
573 struct list_head *lh;
574
575 list_for_each(lh, head)
576 if (pos-- == 0)
577 return lh;
578
579 return NULL;
580}
581
582EXPORT_SYMBOL(seq_list_start);
583
584struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
585{
586 if (!pos)
587 return head;
588
589 return seq_list_start(head, pos - 1);
590}
591
592EXPORT_SYMBOL(seq_list_start_head);
593
594struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
595{
596 struct list_head *lh;
597
598 lh = ((struct list_head *)v)->next;
599 ++*ppos;
600 return lh == head ? NULL : lh;
601}
602
603EXPORT_SYMBOL(seq_list_next);