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: the file to read from
55 * @buf: the buffer to read to
56 * @size: the maximum number of bytes to read
57 * @ppos: the current position in the file
59 * Ready-made ->f_op->read()
61 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
63 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
72 * seq_file->op->..m_start/m_stop/m_next may do special actions
73 * or optimisations based on the file->f_version, so we want to
74 * pass the file->f_version to those methods.
76 * seq_file->version is just copy of f_version, and seq_file
77 * methods can treat it simply as file version.
78 * It is copied in first and copied out after all operations.
79 * It is convenient to have it as part of structure to avoid the
80 * need of passing another argument to all the seq_file methods.
82 m
->version
= file
->f_version
;
83 /* grab buffer if we didn't have one */
85 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
89 /* if not empty - flush it first */
91 n
= min(m
->count
, size
);
92 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
105 /* we need at least one record in buffer */
108 p
= m
->op
->start(m
, &pos
);
112 err
= m
->op
->show(m
, p
);
115 if (m
->count
< m
->size
)
119 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
129 /* they want more? let's try to get some more */
130 while (m
->count
< size
) {
131 size_t offs
= m
->count
;
133 p
= m
->op
->next(m
, p
, &next
);
134 if (!p
|| IS_ERR(p
)) {
138 err
= m
->op
->show(m
, p
);
139 if (err
|| m
->count
== m
->size
) {
146 n
= min(m
->count
, size
);
147 err
= copy_to_user(buf
, m
->buf
, n
);
162 file
->f_version
= m
->version
;
172 EXPORT_SYMBOL(seq_read
);
174 static int traverse(struct seq_file
*m
, loff_t offset
)
182 m
->count
= m
->from
= 0;
186 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
190 p
= m
->op
->start(m
, &m
->index
);
195 error
= m
->op
->show(m
, p
);
198 if (m
->count
== m
->size
)
200 if (pos
+ m
->count
> offset
) {
201 m
->from
= offset
- pos
;
211 p
= m
->op
->next(m
, p
, &m
->index
);
219 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
220 return !m
->buf
? -ENOMEM
: -EAGAIN
;
224 * seq_lseek - ->llseek() method for sequential files.
225 * @file: the file in question
226 * @offset: new position
227 * @origin: 0 for absolute, 1 for relative position
229 * Ready-made ->f_op->llseek()
231 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
233 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
234 long long retval
= -EINVAL
;
237 m
->version
= file
->f_version
;
240 offset
+= file
->f_pos
;
245 if (offset
!= file
->f_pos
) {
246 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
249 /* with extreme prejudice... */
255 retval
= file
->f_pos
= offset
;
260 file
->f_version
= m
->version
;
263 EXPORT_SYMBOL(seq_lseek
);
266 * seq_release - free the structures associated with sequential file.
267 * @file: file in question
268 * @inode: file->f_dentry->d_inode
270 * Frees the structures associated with sequential file; can be used
271 * as ->f_op->release() if you don't have private data to destroy.
273 int seq_release(struct inode
*inode
, struct file
*file
)
275 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
280 EXPORT_SYMBOL(seq_release
);
283 * seq_escape - print string into buffer, escaping some characters
286 * @esc: set of characters that need escaping
288 * Puts string into buffer, replacing each occurrence of character from
289 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
292 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
294 char *end
= m
->buf
+ m
->size
;
298 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
299 if (!strchr(esc
, c
)) {
305 *p
++ = '0' + ((c
& 0300) >> 6);
306 *p
++ = '0' + ((c
& 070) >> 3);
307 *p
++ = '0' + (c
& 07);
313 m
->count
= p
- m
->buf
;
316 EXPORT_SYMBOL(seq_escape
);
318 int seq_printf(struct seq_file
*m
, const char *f
, ...)
323 if (m
->count
< m
->size
) {
325 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
327 if (m
->count
+ len
< m
->size
) {
335 EXPORT_SYMBOL(seq_printf
);
337 int seq_path(struct seq_file
*m
,
338 struct vfsmount
*mnt
, struct dentry
*dentry
,
341 if (m
->count
< m
->size
) {
342 char *s
= m
->buf
+ m
->count
;
343 char *p
= d_path(dentry
, mnt
, s
, m
->size
- m
->count
);
348 p
= m
->buf
+ m
->count
;
349 m
->count
= s
- m
->buf
;
351 } else if (!strchr(esc
, c
)) {
353 } else if (s
+ 4 > p
) {
357 *s
++ = '0' + ((c
& 0300) >> 6);
358 *s
++ = '0' + ((c
& 070) >> 3);
359 *s
++ = '0' + (c
& 07);
367 EXPORT_SYMBOL(seq_path
);
369 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
371 return NULL
+ (*pos
== 0);
374 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
380 static void single_stop(struct seq_file
*p
, void *v
)
384 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
387 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
391 op
->start
= single_start
;
392 op
->next
= single_next
;
393 op
->stop
= single_stop
;
395 res
= seq_open(file
, op
);
397 ((struct seq_file
*)file
->private_data
)->private = data
;
403 EXPORT_SYMBOL(single_open
);
405 int single_release(struct inode
*inode
, struct file
*file
)
407 struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
408 int res
= seq_release(inode
, file
);
412 EXPORT_SYMBOL(single_release
);
414 int seq_release_private(struct inode
*inode
, struct file
*file
)
416 struct seq_file
*seq
= file
->private_data
;
420 return seq_release(inode
, file
);
422 EXPORT_SYMBOL(seq_release_private
);
424 int seq_putc(struct seq_file
*m
, char c
)
426 if (m
->count
< m
->size
) {
427 m
->buf
[m
->count
++] = c
;
432 EXPORT_SYMBOL(seq_putc
);
434 int seq_puts(struct seq_file
*m
, const char *s
)
437 if (m
->count
+ len
< m
->size
) {
438 memcpy(m
->buf
+ m
->count
, s
, len
);
445 EXPORT_SYMBOL(seq_puts
);