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.
29 int seq_open(struct file *file, struct seq_operations *op)
31 struct seq_file *p = kmalloc(sizeof(*p), GFP_KERNEL);
34 memset(p, 0, sizeof(*p));
35 sema_init(&p->sem, 1);
37 file->private_data = p;
40 * Wrappers around seq_open(e.g. swaps_open) need to be
41 * aware of this. If they set f_version themselves, they
42 * should call seq_open first and then set f_version.
46 /* SEQ files support lseek, but not pread/pwrite */
47 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
50 EXPORT_SYMBOL(seq_open);
53 * seq_read - ->read() method for sequential files.
54 * @file, @buf, @size, @ppos: see file_operations method
56 * Ready-made ->f_op->read()
58 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
60 struct seq_file *m = (struct seq_file *)file->private_data;
69 * seq_file->op->..m_start/m_stop/m_next may do special actions
70 * or optimisations based on the file->f_version, so we want to
71 * pass the file->f_version to those methods.
73 * seq_file->version is just copy of f_version, and seq_file
74 * methods can treat it simply as file version.
75 * It is copied in first and copied out after all operations.
76 * It is convenient to have it as part of structure to avoid the
77 * need of passing another argument to all the seq_file methods.
79 m->version = file->f_version;
80 /* grab buffer if we didn't have one */
82 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
86 /* if not empty - flush it first */
88 n = min(m->count, size);
89 err = copy_to_user(buf, m->buf + m->from, n);
102 /* we need at least one record in buffer */
105 p = m->op->start(m, &pos);
109 err = m->op->show(m, p);
112 if (m->count < m->size)
116 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
126 /* they want more? let's try to get some more */
127 while (m->count < size) {
128 size_t offs = m->count;
130 p = m->op->next(m, p, &next);
131 if (!p || IS_ERR(p)) {
135 err = m->op->show(m, p);
136 if (err || m->count == m->size) {
143 n = min(m->count, size);
144 err = copy_to_user(buf, m->buf, n);
159 file->f_version = m->version;
169 EXPORT_SYMBOL(seq_read);
171 static int traverse(struct seq_file *m, loff_t offset)
179 m->count = m->from = 0;
183 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
187 p = m->op->start(m, &m->index);
192 error = m->op->show(m, p);
195 if (m->count == m->size)
197 if (pos + m->count > offset) {
198 m->from = offset - pos;
208 p = m->op->next(m, p, &m->index);
216 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
217 return !m->buf ? -ENOMEM : -EAGAIN;
221 * seq_lseek - ->llseek() method for sequential files.
222 * @file, @offset, @origin: see file_operations method
224 * Ready-made ->f_op->llseek()
226 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
228 struct seq_file *m = (struct seq_file *)file->private_data;
229 long long retval = -EINVAL;
232 m->version = file->f_version;
235 offset += file->f_pos;
240 if (offset != file->f_pos) {
241 while ((retval=traverse(m, offset)) == -EAGAIN)
244 /* with extreme prejudice... */
250 retval = file->f_pos = offset;
255 file->f_version = m->version;
258 EXPORT_SYMBOL(seq_lseek);
261 * seq_release - free the structures associated with sequential file.
262 * @file: file in question
263 * @inode: file->f_dentry->d_inode
265 * Frees the structures associated with sequential file; can be used
266 * as ->f_op->release() if you don't have private data to destroy.
268 int seq_release(struct inode *inode, struct file *file)
270 struct seq_file *m = (struct seq_file *)file->private_data;
275 EXPORT_SYMBOL(seq_release);
278 * seq_escape - print string into buffer, escaping some characters
281 * @esc: set of characters that need escaping
283 * Puts string into buffer, replacing each occurrence of character from
284 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
287 int seq_escape(struct seq_file *m, const char *s, const char *esc)
289 char *end = m->buf + m->size;
293 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
294 if (!strchr(esc, c)) {
300 *p++ = '0' + ((c & 0300) >> 6);
301 *p++ = '0' + ((c & 070) >> 3);
302 *p++ = '0' + (c & 07);
308 m->count = p - m->buf;
311 EXPORT_SYMBOL(seq_escape);
313 int seq_printf(struct seq_file *m, const char *f, ...)
318 if (m->count < m->size) {
320 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
322 if (m->count + len < m->size) {
330 EXPORT_SYMBOL(seq_printf);
332 int seq_path(struct seq_file *m,
333 struct vfsmount *mnt, struct dentry *dentry,
336 if (m->count < m->size) {
337 char *s = m->buf + m->count;
338 char *p = d_path(dentry, mnt, s, m->size - m->count);
343 p = m->buf + m->count;
344 m->count = s - m->buf;
346 } else if (!strchr(esc, c)) {
348 } else if (s + 4 > p) {
352 *s++ = '0' + ((c & 0300) >> 6);
353 *s++ = '0' + ((c & 070) >> 3);
354 *s++ = '0' + (c & 07);
362 EXPORT_SYMBOL(seq_path);
364 static void *single_start(struct seq_file *p, loff_t *pos)
366 return NULL + (*pos == 0);
369 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
375 static void single_stop(struct seq_file *p, void *v)
379 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
382 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
386 op->start = single_start;
387 op->next = single_next;
388 op->stop = single_stop;
390 res = seq_open(file, op);
392 ((struct seq_file *)file->private_data)->private = data;
398 EXPORT_SYMBOL(single_open);
400 int single_release(struct inode *inode, struct file *file)
402 struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
403 int res = seq_release(inode, file);
407 EXPORT_SYMBOL(single_release);
409 int seq_release_private(struct inode *inode, struct file *file)
411 struct seq_file *seq = file->private_data;
415 return seq_release(inode, file);
417 EXPORT_SYMBOL(seq_release_private);
419 int seq_putc(struct seq_file *m, char c)
421 if (m->count < m->size) {
422 m->buf[m->count++] = c;
427 EXPORT_SYMBOL(seq_putc);
429 int seq_puts(struct seq_file *m, const char *s)
432 if (m->count + len < m->size) {
433 memcpy(m->buf + m->count, s, len);
440 EXPORT_SYMBOL(seq_puts);