fed up with those stupid warnings
[mmotm.git] / fs / seq_file.c
blobb3bd0dd4547f7efb39618fe689289b6f436aebe3
1 /*
2 * linux/fs/seq_file.c
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
8 #include <linux/fs.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
13 #include <asm/uaccess.h>
14 #include <asm/page.h>
16 /**
17 * seq_open - initialize sequential file
18 * @file: file we initialize
19 * @op: method table describing the sequence
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.
28 * Returning SEQ_SKIP means "discard this element and move on".
30 int seq_open(struct file *file, const struct seq_operations *op)
32 struct seq_file *p = file->private_data;
34 if (!p) {
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
36 if (!p)
37 return -ENOMEM;
38 file->private_data = p;
40 memset(p, 0, sizeof(*p));
41 mutex_init(&p->lock);
42 p->op = op;
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.
49 file->f_version = 0;
52 * seq_files support lseek() and pread(). They do not implement
53 * write() at all, but we clear FMODE_PWRITE here for historical
54 * reasons.
56 * If a client of seq_files a) implements file.write() and b) wishes to
57 * support pwrite() then that client will need to implement its own
58 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
60 file->f_mode &= ~FMODE_PWRITE;
61 return 0;
63 EXPORT_SYMBOL(seq_open);
65 static int traverse(struct seq_file *m, loff_t offset)
67 loff_t pos = 0, index;
68 int error = 0;
69 void *p;
71 m->version = 0;
72 index = 0;
73 m->count = m->from = 0;
74 if (!offset) {
75 m->index = index;
76 return 0;
78 if (!m->buf) {
79 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
80 if (!m->buf)
81 return -ENOMEM;
83 p = m->op->start(m, &index);
84 while (p) {
85 error = PTR_ERR(p);
86 if (IS_ERR(p))
87 break;
88 error = m->op->show(m, p);
89 if (error < 0)
90 break;
91 if (unlikely(error)) {
92 error = 0;
93 m->count = 0;
95 if (m->count == m->size)
96 goto Eoverflow;
97 if (pos + m->count > offset) {
98 m->from = offset - pos;
99 m->count -= m->from;
100 m->index = index;
101 break;
103 pos += m->count;
104 m->count = 0;
105 if (pos == offset) {
106 index++;
107 m->index = index;
108 break;
110 p = m->op->next(m, p, &index);
112 m->op->stop(m, p);
113 m->index = index;
114 return error;
116 Eoverflow:
117 m->op->stop(m, p);
118 kfree(m->buf);
119 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
120 return !m->buf ? -ENOMEM : -EAGAIN;
124 * seq_read - ->read() method for sequential files.
125 * @file: the file to read from
126 * @buf: the buffer to read to
127 * @size: the maximum number of bytes to read
128 * @ppos: the current position in the file
130 * Ready-made ->f_op->read()
132 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
134 struct seq_file *m = (struct seq_file *)file->private_data;
135 size_t copied = 0;
136 loff_t pos;
137 size_t n;
138 void *p;
139 int err = 0;
141 mutex_lock(&m->lock);
143 /* Don't assume *ppos is where we left it */
144 if (unlikely(*ppos != m->read_pos)) {
145 m->read_pos = *ppos;
146 while ((err = traverse(m, *ppos)) == -EAGAIN)
148 if (err) {
149 /* With prejudice... */
150 m->read_pos = 0;
151 m->version = 0;
152 m->index = 0;
153 m->count = 0;
154 goto Done;
159 * seq_file->op->..m_start/m_stop/m_next may do special actions
160 * or optimisations based on the file->f_version, so we want to
161 * pass the file->f_version to those methods.
163 * seq_file->version is just copy of f_version, and seq_file
164 * methods can treat it simply as file version.
165 * It is copied in first and copied out after all operations.
166 * It is convenient to have it as part of structure to avoid the
167 * need of passing another argument to all the seq_file methods.
169 m->version = file->f_version;
170 /* grab buffer if we didn't have one */
171 if (!m->buf) {
172 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
173 if (!m->buf)
174 goto Enomem;
176 /* if not empty - flush it first */
177 if (m->count) {
178 n = min(m->count, size);
179 err = copy_to_user(buf, m->buf + m->from, n);
180 if (err)
181 goto Efault;
182 m->count -= n;
183 m->from += n;
184 size -= n;
185 buf += n;
186 copied += n;
187 if (!m->count)
188 m->index++;
189 if (!size)
190 goto Done;
192 /* we need at least one record in buffer */
193 pos = m->index;
194 p = m->op->start(m, &pos);
195 while (1) {
196 err = PTR_ERR(p);
197 if (!p || IS_ERR(p))
198 break;
199 err = m->op->show(m, p);
200 if (err < 0)
201 break;
202 if (unlikely(err))
203 m->count = 0;
204 if (unlikely(!m->count)) {
205 p = m->op->next(m, p, &pos);
206 m->index = pos;
207 continue;
209 if (m->count < m->size)
210 goto Fill;
211 m->op->stop(m, p);
212 kfree(m->buf);
213 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
214 if (!m->buf)
215 goto Enomem;
216 m->count = 0;
217 m->version = 0;
218 pos = m->index;
219 p = m->op->start(m, &pos);
221 m->op->stop(m, p);
222 m->count = 0;
223 goto Done;
224 Fill:
225 /* they want more? let's try to get some more */
226 while (m->count < size) {
227 size_t offs = m->count;
228 loff_t next = pos;
229 p = m->op->next(m, p, &next);
230 if (!p || IS_ERR(p)) {
231 err = PTR_ERR(p);
232 break;
234 err = m->op->show(m, p);
235 if (m->count == m->size || err) {
236 m->count = offs;
237 if (likely(err <= 0))
238 break;
240 pos = next;
242 m->op->stop(m, p);
243 n = min(m->count, size);
244 err = copy_to_user(buf, m->buf, n);
245 if (err)
246 goto Efault;
247 copied += n;
248 m->count -= n;
249 if (m->count)
250 m->from = n;
251 else
252 pos++;
253 m->index = pos;
254 Done:
255 if (!copied)
256 copied = err;
257 else {
258 *ppos += copied;
259 m->read_pos += copied;
261 file->f_version = m->version;
262 mutex_unlock(&m->lock);
263 return copied;
264 Enomem:
265 err = -ENOMEM;
266 goto Done;
267 Efault:
268 err = -EFAULT;
269 goto Done;
271 EXPORT_SYMBOL(seq_read);
274 * seq_lseek - ->llseek() method for sequential files.
275 * @file: the file in question
276 * @offset: new position
277 * @origin: 0 for absolute, 1 for relative position
279 * Ready-made ->f_op->llseek()
281 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
283 struct seq_file *m = (struct seq_file *)file->private_data;
284 loff_t retval = -EINVAL;
286 mutex_lock(&m->lock);
287 m->version = file->f_version;
288 switch (origin) {
289 case 1:
290 offset += file->f_pos;
291 case 0:
292 if (offset < 0)
293 break;
294 retval = offset;
295 if (offset != m->read_pos) {
296 while ((retval=traverse(m, offset)) == -EAGAIN)
298 if (retval) {
299 /* with extreme prejudice... */
300 file->f_pos = 0;
301 m->read_pos = 0;
302 m->version = 0;
303 m->index = 0;
304 m->count = 0;
305 } else {
306 m->read_pos = offset;
307 retval = file->f_pos = offset;
311 file->f_version = m->version;
312 mutex_unlock(&m->lock);
313 return retval;
315 EXPORT_SYMBOL(seq_lseek);
318 * seq_release - free the structures associated with sequential file.
319 * @file: file in question
320 * @inode: file->f_path.dentry->d_inode
322 * Frees the structures associated with sequential file; can be used
323 * as ->f_op->release() if you don't have private data to destroy.
325 int seq_release(struct inode *inode, struct file *file)
327 struct seq_file *m = (struct seq_file *)file->private_data;
329 if (m->seq_ops_allocated) {
330 struct dentry *dentry = file->f_dentry;
331 printk("memory leak: '%.*s'\n",
332 dentry->d_name.len, dentry->d_name.name);
333 WARN_ON(1);
335 kfree(m->buf);
336 kfree(m);
337 return 0;
339 EXPORT_SYMBOL(seq_release);
342 * seq_escape - print string into buffer, escaping some characters
343 * @m: target buffer
344 * @s: string
345 * @esc: set of characters that need escaping
347 * Puts string into buffer, replacing each occurrence of character from
348 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
349 * case of overflow.
351 int seq_escape(struct seq_file *m, const char *s, const char *esc)
353 char *end = m->buf + m->size;
354 char *p;
355 char c;
357 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
358 if (!strchr(esc, c)) {
359 *p++ = c;
360 continue;
362 if (p + 3 < end) {
363 *p++ = '\\';
364 *p++ = '0' + ((c & 0300) >> 6);
365 *p++ = '0' + ((c & 070) >> 3);
366 *p++ = '0' + (c & 07);
367 continue;
369 m->count = m->size;
370 return -1;
372 m->count = p - m->buf;
373 return 0;
375 EXPORT_SYMBOL(seq_escape);
377 int seq_printf(struct seq_file *m, const char *f, ...)
379 va_list args;
380 int len;
382 if (m->count < m->size) {
383 va_start(args, f);
384 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
385 va_end(args);
386 if (m->count + len < m->size) {
387 m->count += len;
388 return 0;
391 m->count = m->size;
392 return -1;
394 EXPORT_SYMBOL(seq_printf);
397 * mangle_path - mangle and copy path to buffer beginning
398 * @s: buffer start
399 * @p: beginning of path in above buffer
400 * @esc: set of characters that need escaping
402 * Copy the path from @p to @s, replacing each occurrence of character from
403 * @esc with usual octal escape.
404 * Returns pointer past last written character in @s, or NULL in case of
405 * failure.
407 char *mangle_path(char *s, char *p, char *esc)
409 while (s <= p) {
410 char c = *p++;
411 if (!c) {
412 return s;
413 } else if (!strchr(esc, c)) {
414 *s++ = c;
415 } else if (s + 4 > p) {
416 break;
417 } else {
418 *s++ = '\\';
419 *s++ = '0' + ((c & 0300) >> 6);
420 *s++ = '0' + ((c & 070) >> 3);
421 *s++ = '0' + (c & 07);
424 return NULL;
426 EXPORT_SYMBOL(mangle_path);
429 * seq_path - seq_file interface to print a pathname
430 * @m: the seq_file handle
431 * @path: the struct path to print
432 * @esc: set of characters to escape in the output
434 * return the absolute path of 'path', as represented by the
435 * dentry / mnt pair in the path parameter.
437 int seq_path(struct seq_file *m, struct path *path, char *esc)
439 char *buf;
440 size_t size = seq_get_buf(m, &buf);
441 int res = -1;
443 if (size) {
444 char *p = d_path(path, buf, size);
445 if (!IS_ERR(p)) {
446 char *end = mangle_path(buf, p, esc);
447 if (end)
448 res = end - buf;
451 seq_commit(m, res);
453 return res;
455 EXPORT_SYMBOL(seq_path);
458 * Same as seq_path, but relative to supplied root.
460 * root may be changed, see __d_path().
462 int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
463 char *esc)
465 char *buf;
466 size_t size = seq_get_buf(m, &buf);
467 int res = -ENAMETOOLONG;
469 if (size) {
470 char *p;
472 spin_lock(&dcache_lock);
473 p = __d_path(path, root, buf, size);
474 spin_unlock(&dcache_lock);
475 res = PTR_ERR(p);
476 if (!IS_ERR(p)) {
477 char *end = mangle_path(buf, p, esc);
478 if (end)
479 res = end - buf;
480 else
481 res = -ENAMETOOLONG;
484 seq_commit(m, res);
486 return res < 0 ? res : 0;
490 * returns the path of the 'dentry' from the root of its filesystem.
492 int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
494 char *buf;
495 size_t size = seq_get_buf(m, &buf);
496 int res = -1;
498 if (size) {
499 char *p = dentry_path(dentry, buf, size);
500 if (!IS_ERR(p)) {
501 char *end = mangle_path(buf, p, esc);
502 if (end)
503 res = end - buf;
506 seq_commit(m, res);
508 return res;
511 int seq_bitmap(struct seq_file *m, const unsigned long *bits,
512 unsigned int nr_bits)
514 if (m->count < m->size) {
515 int len = bitmap_scnprintf(m->buf + m->count,
516 m->size - m->count, bits, nr_bits);
517 if (m->count + len < m->size) {
518 m->count += len;
519 return 0;
522 m->count = m->size;
523 return -1;
525 EXPORT_SYMBOL(seq_bitmap);
527 int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
528 unsigned int nr_bits)
530 if (m->count < m->size) {
531 int len = bitmap_scnlistprintf(m->buf + m->count,
532 m->size - m->count, bits, nr_bits);
533 if (m->count + len < m->size) {
534 m->count += len;
535 return 0;
538 m->count = m->size;
539 return -1;
541 EXPORT_SYMBOL(seq_bitmap_list);
543 static void *single_start(struct seq_file *p, loff_t *pos)
545 return NULL + (*pos == 0);
548 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
550 ++*pos;
551 return NULL;
554 static void single_stop(struct seq_file *p, void *v)
558 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
559 void *data)
561 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
562 int res = -ENOMEM;
564 if (op) {
565 op->start = single_start;
566 op->next = single_next;
567 op->stop = single_stop;
568 op->show = show;
569 res = seq_open(file, op);
570 if (!res) {
571 struct seq_file *seq = file->private_data;
573 seq->private = data;
574 seq->seq_ops_allocated = 1;
575 } else
576 kfree(op);
578 return res;
580 EXPORT_SYMBOL(single_open);
582 int single_release(struct inode *inode, struct file *file)
584 struct seq_file *seq = file->private_data;
585 const struct seq_operations *op = seq->op;
586 int res;
588 /* All roads lead to seq_release(), so... */
589 seq->seq_ops_allocated = 0;
590 res = seq_release(inode, file);
591 kfree(op);
592 return res;
594 EXPORT_SYMBOL(single_release);
596 int seq_release_private(struct inode *inode, struct file *file)
598 struct seq_file *seq = file->private_data;
600 kfree(seq->private);
601 seq->private = NULL;
602 return seq_release(inode, file);
604 EXPORT_SYMBOL(seq_release_private);
606 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
607 int psize)
609 int rc;
610 void *private;
611 struct seq_file *seq;
613 private = kzalloc(psize, GFP_KERNEL);
614 if (private == NULL)
615 goto out;
617 rc = seq_open(f, ops);
618 if (rc < 0)
619 goto out_free;
621 seq = f->private_data;
622 seq->private = private;
623 return private;
625 out_free:
626 kfree(private);
627 out:
628 return NULL;
630 EXPORT_SYMBOL(__seq_open_private);
632 int seq_open_private(struct file *filp, const struct seq_operations *ops,
633 int psize)
635 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
637 EXPORT_SYMBOL(seq_open_private);
639 int seq_putc(struct seq_file *m, char c)
641 if (m->count < m->size) {
642 m->buf[m->count++] = c;
643 return 0;
645 return -1;
647 EXPORT_SYMBOL(seq_putc);
649 int seq_puts(struct seq_file *m, const char *s)
651 int len = strlen(s);
652 if (m->count + len < m->size) {
653 memcpy(m->buf + m->count, s, len);
654 m->count += len;
655 return 0;
657 m->count = m->size;
658 return -1;
660 EXPORT_SYMBOL(seq_puts);
663 * seq_write - write arbitrary data to buffer
664 * @seq: seq_file identifying the buffer to which data should be written
665 * @data: data address
666 * @len: number of bytes
668 * Return 0 on success, non-zero otherwise.
670 int seq_write(struct seq_file *seq, const void *data, size_t len)
672 if (seq->count + len < seq->size) {
673 memcpy(seq->buf + seq->count, data, len);
674 seq->count += len;
675 return 0;
677 seq->count = seq->size;
678 return -1;
680 EXPORT_SYMBOL(seq_write);
682 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
684 struct list_head *lh;
686 list_for_each(lh, head)
687 if (pos-- == 0)
688 return lh;
690 return NULL;
693 EXPORT_SYMBOL(seq_list_start);
695 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
697 if (!pos)
698 return head;
700 return seq_list_start(head, pos - 1);
703 EXPORT_SYMBOL(seq_list_start_head);
705 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
707 struct list_head *lh;
709 lh = ((struct list_head *)v)->next;
710 ++*ppos;
711 return lh == head ? NULL : lh;
714 EXPORT_SYMBOL(seq_list_next);