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 /* SEQ files support lseek, but not pread/pwrite */
41 file
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
44 EXPORT_SYMBOL(seq_open
);
47 * seq_read - ->read() method for sequential files.
48 * @file, @buf, @size, @ppos: see file_operations method
50 * Ready-made ->f_op->read()
52 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
54 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
63 /* grab buffer if we didn't have one */
65 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
69 /* if not empty - flush it first */
71 n
= min(m
->count
, size
);
72 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
85 /* we need at least one record in buffer */
88 p
= m
->op
->start(m
, &pos
);
92 err
= m
->op
->show(m
, p
);
95 if (m
->count
< m
->size
)
99 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
108 /* they want more? let's try to get some more */
109 while (m
->count
< size
) {
110 size_t offs
= m
->count
;
112 p
= m
->op
->next(m
, p
, &next
);
113 if (!p
|| IS_ERR(p
)) {
117 err
= m
->op
->show(m
, p
);
118 if (err
|| m
->count
== m
->size
) {
125 n
= min(m
->count
, size
);
126 err
= copy_to_user(buf
, m
->buf
, n
);
150 EXPORT_SYMBOL(seq_read
);
152 static int traverse(struct seq_file
*m
, loff_t offset
)
159 m
->count
= m
->from
= 0;
163 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
167 p
= m
->op
->start(m
, &m
->index
);
172 error
= m
->op
->show(m
, p
);
175 if (m
->count
== m
->size
)
177 if (pos
+ m
->count
> offset
) {
178 m
->from
= offset
- pos
;
188 p
= m
->op
->next(m
, p
, &m
->index
);
196 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
197 return !m
->buf
? -ENOMEM
: -EAGAIN
;
201 * seq_lseek - ->llseek() method for sequential files.
202 * @file, @offset, @origin: see file_operations method
204 * Ready-made ->f_op->llseek()
206 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
208 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
209 long long retval
= -EINVAL
;
214 offset
+= file
->f_pos
;
219 if (offset
!= file
->f_pos
) {
220 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
223 /* with extreme prejudice... */
228 retval
= file
->f_pos
= offset
;
235 EXPORT_SYMBOL(seq_lseek
);
238 * seq_release - free the structures associated with sequential file.
239 * @file: file in question
240 * @inode: file->f_dentry->d_inode
242 * Frees the structures associated with sequential file; can be used
243 * as ->f_op->release() if you don't have private data to destroy.
245 int seq_release(struct inode
*inode
, struct file
*file
)
247 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
252 EXPORT_SYMBOL(seq_release
);
255 * seq_escape - print string into buffer, escaping some characters
258 * @esc: set of characters that need escaping
260 * Puts string into buffer, replacing each occurrence of character from
261 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
264 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
266 char *end
= m
->buf
+ m
->size
;
270 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
271 if (!strchr(esc
, c
)) {
277 *p
++ = '0' + ((c
& 0300) >> 6);
278 *p
++ = '0' + ((c
& 070) >> 3);
279 *p
++ = '0' + (c
& 07);
285 m
->count
= p
- m
->buf
;
288 EXPORT_SYMBOL(seq_escape
);
290 int seq_printf(struct seq_file
*m
, const char *f
, ...)
295 if (m
->count
< m
->size
) {
297 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
299 if (m
->count
+ len
< m
->size
) {
307 EXPORT_SYMBOL(seq_printf
);
309 int seq_path(struct seq_file
*m
,
310 struct vfsmount
*mnt
, struct dentry
*dentry
,
313 if (m
->count
< m
->size
) {
314 char *s
= m
->buf
+ m
->count
;
315 char *p
= d_path(dentry
, mnt
, s
, m
->size
- m
->count
);
320 p
= m
->buf
+ m
->count
;
321 m
->count
= s
- m
->buf
;
323 } else if (!strchr(esc
, c
)) {
325 } else if (s
+ 4 > p
) {
329 *s
++ = '0' + ((c
& 0300) >> 6);
330 *s
++ = '0' + ((c
& 070) >> 3);
331 *s
++ = '0' + (c
& 07);
339 EXPORT_SYMBOL(seq_path
);
341 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
343 return NULL
+ (*pos
== 0);
346 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
352 static void single_stop(struct seq_file
*p
, void *v
)
356 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
359 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
363 op
->start
= single_start
;
364 op
->next
= single_next
;
365 op
->stop
= single_stop
;
367 res
= seq_open(file
, op
);
369 ((struct seq_file
*)file
->private_data
)->private = data
;
375 EXPORT_SYMBOL(single_open
);
377 int single_release(struct inode
*inode
, struct file
*file
)
379 struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
380 int res
= seq_release(inode
, file
);
384 EXPORT_SYMBOL(single_release
);
386 int seq_release_private(struct inode
*inode
, struct file
*file
)
388 struct seq_file
*seq
= file
->private_data
;
392 return seq_release(inode
, file
);
394 EXPORT_SYMBOL(seq_release_private
);
396 int seq_putc(struct seq_file
*m
, char c
)
398 if (m
->count
< m
->size
) {
399 m
->buf
[m
->count
++] = c
;
404 EXPORT_SYMBOL(seq_putc
);
406 int seq_puts(struct seq_file
*m
, const char *s
)
409 if (m
->count
+ len
< m
->size
) {
410 memcpy(m
->buf
+ m
->count
, s
, len
);
417 EXPORT_SYMBOL(seq_puts
);