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 EXPORT_SYMBOL(seq_open);
43 * seq_read - ->read() method for sequential files.
44 * @file, @buf, @size, @ppos: see file_operations method
46 * Ready-made ->f_op->read()
48 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
50 struct seq_file *m = (struct seq_file *)file->private_data;
57 if (ppos != &file->f_pos)
61 /* grab buffer if we didn't have one */
63 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
67 /* if not empty - flush it first */
69 n = min(m->count, size);
70 err = copy_to_user(buf, m->buf + m->from, n);
83 /* we need at least one record in buffer */
86 p = m->op->start(m, &pos);
90 err = m->op->show(m, p);
93 if (m->count < m->size)
97 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
106 /* they want more? let's try to get some more */
107 while (m->count < size) {
108 size_t offs = m->count;
110 p = m->op->next(m, p, &next);
111 if (!p || IS_ERR(p)) {
115 err = m->op->show(m, p);
116 if (err || m->count == m->size) {
123 n = min(m->count, size);
124 err = copy_to_user(buf, m->buf, n);
148 EXPORT_SYMBOL(seq_read);
150 static int traverse(struct seq_file *m, loff_t offset)
157 m->count = m->from = 0;
161 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
165 p = m->op->start(m, &m->index);
170 error = m->op->show(m, p);
173 if (m->count == m->size)
175 if (pos + m->count > offset) {
176 m->from = offset - pos;
186 p = m->op->next(m, p, &m->index);
194 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
195 return !m->buf ? -ENOMEM : -EAGAIN;
199 * seq_lseek - ->llseek() method for sequential files.
200 * @file, @offset, @origin: see file_operations method
202 * Ready-made ->f_op->llseek()
204 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
206 struct seq_file *m = (struct seq_file *)file->private_data;
207 long long retval = -EINVAL;
212 offset += file->f_pos;
217 if (offset != file->f_pos) {
218 while ((retval=traverse(m, offset)) == -EAGAIN)
221 /* with extreme prejudice... */
226 retval = file->f_pos = offset;
233 EXPORT_SYMBOL(seq_lseek);
236 * seq_release - free the structures associated with sequential file.
237 * @file: file in question
238 * @inode: file->f_dentry->d_inode
240 * Frees the structures associated with sequential file; can be used
241 * as ->f_op->release() if you don't have private data to destroy.
243 int seq_release(struct inode *inode, struct file *file)
245 struct seq_file *m = (struct seq_file *)file->private_data;
250 EXPORT_SYMBOL(seq_release);
253 * seq_escape - print string into buffer, escaping some characters
256 * @esc: set of characters that need escaping
258 * Puts string into buffer, replacing each occurrence of character from
259 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
262 int seq_escape(struct seq_file *m, const char *s, const char *esc)
264 char *end = m->buf + m->size;
268 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
269 if (!strchr(esc, c)) {
275 *p++ = '0' + ((c & 0300) >> 6);
276 *p++ = '0' + ((c & 070) >> 3);
277 *p++ = '0' + (c & 07);
283 m->count = p - m->buf;
286 EXPORT_SYMBOL(seq_escape);
288 int seq_printf(struct seq_file *m, const char *f, ...)
293 if (m->count < m->size) {
295 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
297 if (m->count + len < m->size) {
305 EXPORT_SYMBOL(seq_printf);
307 int seq_path(struct seq_file *m,
308 struct vfsmount *mnt, struct dentry *dentry,
311 if (m->count < m->size) {
312 char *s = m->buf + m->count;
313 char *p = d_path(dentry, mnt, s, m->size - m->count);
318 p = m->buf + m->count;
319 m->count = s - m->buf;
321 } else if (!strchr(esc, c)) {
323 } else if (s + 4 > p) {
327 *s++ = '0' + ((c & 0300) >> 6);
328 *s++ = '0' + ((c & 070) >> 3);
329 *s++ = '0' + (c & 07);
337 EXPORT_SYMBOL(seq_path);
339 static void *single_start(struct seq_file *p, loff_t *pos)
341 return NULL + (*pos == 0);
344 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
350 static void single_stop(struct seq_file *p, void *v)
354 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
357 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
361 op->start = single_start;
362 op->next = single_next;
363 op->stop = single_stop;
365 res = seq_open(file, op);
367 ((struct seq_file *)file->private_data)->private = data;
373 EXPORT_SYMBOL(single_open);
375 int single_release(struct inode *inode, struct file *file)
377 struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
378 int res = seq_release(inode, file);
382 EXPORT_SYMBOL(single_release);
384 int seq_release_private(struct inode *inode, struct file *file)
386 struct seq_file *seq = file->private_data;
390 return seq_release(inode, file);
392 EXPORT_SYMBOL(seq_release_private);
394 int seq_putc(struct seq_file *m, char c)
396 if (m->count < m->size) {
397 m->buf[m->count++] = c;
402 EXPORT_SYMBOL(seq_putc);
404 int seq_puts(struct seq_file *m, const char *s)
407 if (m->count + len < m->size) {
408 memcpy(m->buf + m->count, s, len);
415 EXPORT_SYMBOL(seq_puts);