4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
13 #include <asm/uaccess.h>
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;
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
38 file->private_data = p;
40 memset(p, 0, sizeof(*p));
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.
51 /* SEQ files support lseek, but not pread/pwrite */
52 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
55 EXPORT_SYMBOL(seq_open);
58 * seq_read - ->read() method for sequential files.
59 * @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
64 * Ready-made ->f_op->read()
66 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
68 struct seq_file *m = (struct seq_file *)file->private_data;
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.
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.
87 m->version = file->f_version;
88 /* grab buffer if we didn't have one */
90 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
94 /* if not empty - flush it first */
96 n = min(m->count, size);
97 err = copy_to_user(buf, m->buf + m->from, n);
110 /* we need at least one record in buffer */
113 p = m->op->start(m, &pos);
117 err = m->op->show(m, p);
122 if (m->count < m->size)
126 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
136 /* they want more? let's try to get some more */
137 while (m->count < size) {
138 size_t offs = m->count;
140 p = m->op->next(m, p, &next);
141 if (!p || IS_ERR(p)) {
145 err = m->op->show(m, p);
146 if (m->count == m->size || err) {
148 if (likely(err <= 0))
154 n = min(m->count, size);
155 err = copy_to_user(buf, m->buf, n);
170 file->f_version = m->version;
171 mutex_unlock(&m->lock);
180 EXPORT_SYMBOL(seq_read);
182 static int traverse(struct seq_file *m, loff_t offset)
184 loff_t pos = 0, index;
190 m->count = m->from = 0;
196 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
200 p = m->op->start(m, &index);
205 error = m->op->show(m, p);
208 if (unlikely(error)) {
212 if (m->count == m->size)
214 if (pos + m->count > offset) {
215 m->from = offset - pos;
227 p = m->op->next(m, p, &index);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
236 return !m->buf ? -ENOMEM : -EAGAIN;
240 * seq_lseek - ->llseek() method for sequential files.
241 * @file: the file in question
242 * @offset: new position
243 * @origin: 0 for absolute, 1 for relative position
245 * Ready-made ->f_op->llseek()
247 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
249 struct seq_file *m = (struct seq_file *)file->private_data;
250 loff_t retval = -EINVAL;
252 mutex_lock(&m->lock);
253 m->version = file->f_version;
256 offset += file->f_pos;
261 if (offset != file->f_pos) {
262 while ((retval=traverse(m, offset)) == -EAGAIN)
265 /* with extreme prejudice... */
271 retval = file->f_pos = offset;
275 file->f_version = m->version;
276 mutex_unlock(&m->lock);
279 EXPORT_SYMBOL(seq_lseek);
282 * seq_release - free the structures associated with sequential file.
283 * @file: file in question
284 * @inode: file->f_path.dentry->d_inode
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.
289 int seq_release(struct inode *inode, struct file *file)
291 struct seq_file *m = (struct seq_file *)file->private_data;
296 EXPORT_SYMBOL(seq_release);
299 * seq_escape - print string into buffer, escaping some characters
302 * @esc: set of characters that need escaping
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
308 int seq_escape(struct seq_file *m, const char *s, const char *esc)
310 char *end = m->buf + m->size;
314 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
315 if (!strchr(esc, c)) {
321 *p++ = '0' + ((c & 0300) >> 6);
322 *p++ = '0' + ((c & 070) >> 3);
323 *p++ = '0' + (c & 07);
329 m->count = p - m->buf;
332 EXPORT_SYMBOL(seq_escape);
334 int seq_printf(struct seq_file *m, const char *f, ...)
339 if (m->count < m->size) {
341 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
343 if (m->count + len < m->size) {
351 EXPORT_SYMBOL(seq_printf);
353 static char *mangle_path(char *s, char *p, char *esc)
359 } else if (!strchr(esc, c)) {
361 } else if (s + 4 > p) {
365 *s++ = '0' + ((c & 0300) >> 6);
366 *s++ = '0' + ((c & 070) >> 3);
367 *s++ = '0' + (c & 07);
374 * return the absolute path of 'dentry' residing in mount 'mnt'.
376 int seq_path(struct seq_file *m, struct path *path, char *esc)
378 if (m->count < m->size) {
379 char *s = m->buf + m->count;
380 char *p = d_path(path, s, m->size - m->count);
382 s = mangle_path(s, p, esc);
384 p = m->buf + m->count;
385 m->count = s - m->buf;
393 EXPORT_SYMBOL(seq_path);
396 * Same as seq_path, but relative to supplied root.
398 * root may be changed, see __d_path().
400 int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
403 int err = -ENAMETOOLONG;
404 if (m->count < m->size) {
405 char *s = m->buf + m->count;
408 spin_lock(&dcache_lock);
409 p = __d_path(path, root, s, m->size - m->count);
410 spin_unlock(&dcache_lock);
413 s = mangle_path(s, p, esc);
415 p = m->buf + m->count;
416 m->count = s - m->buf;
426 * returns the path of the 'dentry' from the root of its filesystem.
428 int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
430 if (m->count < m->size) {
431 char *s = m->buf + m->count;
432 char *p = dentry_path(dentry, s, m->size - m->count);
434 s = mangle_path(s, p, esc);
436 p = m->buf + m->count;
437 m->count = s - m->buf;
446 int seq_bitmap(struct seq_file *m, unsigned long *bits, unsigned int nr_bits)
448 size_t len = bitmap_scnprintf_len(nr_bits);
450 if (m->count + len < m->size) {
451 bitmap_scnprintf(m->buf + m->count, m->size - m->count,
460 static void *single_start(struct seq_file *p, loff_t *pos)
462 return NULL + (*pos == 0);
465 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
471 static void single_stop(struct seq_file *p, void *v)
475 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
478 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
482 op->start = single_start;
483 op->next = single_next;
484 op->stop = single_stop;
486 res = seq_open(file, op);
488 ((struct seq_file *)file->private_data)->private = data;
494 EXPORT_SYMBOL(single_open);
496 int single_release(struct inode *inode, struct file *file)
498 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
499 int res = seq_release(inode, file);
503 EXPORT_SYMBOL(single_release);
505 int seq_release_private(struct inode *inode, struct file *file)
507 struct seq_file *seq = file->private_data;
511 return seq_release(inode, file);
513 EXPORT_SYMBOL(seq_release_private);
515 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
520 struct seq_file *seq;
522 private = kzalloc(psize, GFP_KERNEL);
526 rc = seq_open(f, ops);
530 seq = f->private_data;
531 seq->private = private;
539 EXPORT_SYMBOL(__seq_open_private);
541 int seq_open_private(struct file *filp, const struct seq_operations *ops,
544 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
546 EXPORT_SYMBOL(seq_open_private);
548 int seq_putc(struct seq_file *m, char c)
550 if (m->count < m->size) {
551 m->buf[m->count++] = c;
556 EXPORT_SYMBOL(seq_putc);
558 int seq_puts(struct seq_file *m, const char *s)
561 if (m->count + len < m->size) {
562 memcpy(m->buf + m->count, s, len);
569 EXPORT_SYMBOL(seq_puts);
571 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
573 struct list_head *lh;
575 list_for_each(lh, head)
582 EXPORT_SYMBOL(seq_list_start);
584 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
589 return seq_list_start(head, pos - 1);
592 EXPORT_SYMBOL(seq_list_start_head);
594 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
596 struct list_head *lh;
598 lh = ((struct list_head *)v)->next;
600 return lh == head ? NULL : lh;
603 EXPORT_SYMBOL(seq_list_next);