blob: ae59f5a6c5c1c71d704b39e42abeafdaba2d3616 [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/*
396 * returns the path of the 'dentry' from the root of its filesystem.
397 */
398int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
399{
400 if (m->count < m->size) {
401 char *s = m->buf + m->count;
402 char *p = dentry_path(dentry, s, m->size - m->count);
403 if (!IS_ERR(p)) {
404 s = mangle_path(s, p, esc);
405 if (s) {
406 p = m->buf + m->count;
407 m->count = s - m->buf;
408 return s - p;
409 }
410 }
411 }
412 m->count = m->size;
413 return -1;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static void *single_start(struct seq_file *p, loff_t *pos)
417{
418 return NULL + (*pos == 0);
419}
420
421static void *single_next(struct seq_file *p, void *v, loff_t *pos)
422{
423 ++*pos;
424 return NULL;
425}
426
427static void single_stop(struct seq_file *p, void *v)
428{
429}
430
431int single_open(struct file *file, int (*show)(struct seq_file *, void *),
432 void *data)
433{
434 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
435 int res = -ENOMEM;
436
437 if (op) {
438 op->start = single_start;
439 op->next = single_next;
440 op->stop = single_stop;
441 op->show = show;
442 res = seq_open(file, op);
443 if (!res)
444 ((struct seq_file *)file->private_data)->private = data;
445 else
446 kfree(op);
447 }
448 return res;
449}
450EXPORT_SYMBOL(single_open);
451
452int single_release(struct inode *inode, struct file *file)
453{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800454 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int res = seq_release(inode, file);
456 kfree(op);
457 return res;
458}
459EXPORT_SYMBOL(single_release);
460
461int seq_release_private(struct inode *inode, struct file *file)
462{
463 struct seq_file *seq = file->private_data;
464
465 kfree(seq->private);
466 seq->private = NULL;
467 return seq_release(inode, file);
468}
469EXPORT_SYMBOL(seq_release_private);
470
Pavel Emelyanov39699032007-10-10 02:28:42 -0700471void *__seq_open_private(struct file *f, const struct seq_operations *ops,
472 int psize)
473{
474 int rc;
475 void *private;
476 struct seq_file *seq;
477
478 private = kzalloc(psize, GFP_KERNEL);
479 if (private == NULL)
480 goto out;
481
482 rc = seq_open(f, ops);
483 if (rc < 0)
484 goto out_free;
485
486 seq = f->private_data;
487 seq->private = private;
488 return private;
489
490out_free:
491 kfree(private);
492out:
493 return NULL;
494}
495EXPORT_SYMBOL(__seq_open_private);
496
497int seq_open_private(struct file *filp, const struct seq_operations *ops,
498 int psize)
499{
500 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
501}
502EXPORT_SYMBOL(seq_open_private);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504int seq_putc(struct seq_file *m, char c)
505{
506 if (m->count < m->size) {
507 m->buf[m->count++] = c;
508 return 0;
509 }
510 return -1;
511}
512EXPORT_SYMBOL(seq_putc);
513
514int seq_puts(struct seq_file *m, const char *s)
515{
516 int len = strlen(s);
517 if (m->count + len < m->size) {
518 memcpy(m->buf + m->count, s, len);
519 m->count += len;
520 return 0;
521 }
522 m->count = m->size;
523 return -1;
524}
525EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700526
527struct list_head *seq_list_start(struct list_head *head, loff_t pos)
528{
529 struct list_head *lh;
530
531 list_for_each(lh, head)
532 if (pos-- == 0)
533 return lh;
534
535 return NULL;
536}
537
538EXPORT_SYMBOL(seq_list_start);
539
540struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
541{
542 if (!pos)
543 return head;
544
545 return seq_list_start(head, pos - 1);
546}
547
548EXPORT_SYMBOL(seq_list_start_head);
549
550struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
551{
552 struct list_head *lh;
553
554 lh = ((struct list_head *)v)->next;
555 ++*ppos;
556 return lh == head ? NULL : lh;
557}
558
559EXPORT_SYMBOL(seq_list_next);