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
;
41 EXPORT_SYMBOL(seq_open
);
44 * seq_read - ->read() method for sequential files.
45 * @file, @buf, @size, @ppos: see file_operations method
47 * Ready-made ->f_op->read()
49 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
51 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
58 if (ppos
!= &file
->f_pos
)
62 /* grab buffer if we didn't have one */
64 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
68 /* if not empty - flush it first */
70 n
= min(m
->count
, size
);
71 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
84 /* we need at least one record in buffer */
87 p
= m
->op
->start(m
, &pos
);
91 err
= m
->op
->show(m
, p
);
94 if (m
->count
< m
->size
)
98 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
107 /* they want more? let's try to get some more */
108 while (m
->count
< size
) {
109 size_t offs
= m
->count
;
111 p
= m
->op
->next(m
, p
, &next
);
112 if (!p
|| IS_ERR(p
)) {
116 err
= m
->op
->show(m
, p
);
117 if (err
|| m
->count
== m
->size
) {
124 n
= min(m
->count
, size
);
125 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
;
236 EXPORT_SYMBOL(seq_lseek
);
239 * seq_release - free the structures associated with sequential file.
240 * @file: file in question
241 * @inode: file->f_dentry->d_inode
243 * Frees the structures associated with sequential file; can be used
244 * as ->f_op->release() if you don't have private data to destroy.
246 int seq_release(struct inode
*inode
, struct file
*file
)
248 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
254 EXPORT_SYMBOL(seq_release
);
257 * seq_escape - print string into buffer, escaping some characters
260 * @esc: set of characters that need escaping
262 * Puts string into buffer, replacing each occurrence of character from
263 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
266 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
268 char *end
= m
->buf
+ m
->size
;
272 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
273 if (!strchr(esc
, c
)) {
279 *p
++ = '0' + ((c
& 0300) >> 6);
280 *p
++ = '0' + ((c
& 070) >> 3);
281 *p
++ = '0' + (c
& 07);
287 m
->count
= p
- m
->buf
;
291 EXPORT_SYMBOL(seq_escape
);
293 int seq_printf(struct seq_file
*m
, const char *f
, ...)
298 if (m
->count
< m
->size
) {
300 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
302 if (m
->count
+ len
< m
->size
) {
311 EXPORT_SYMBOL(seq_printf
);
313 int seq_path(struct seq_file
*m
,
314 struct vfsmount
*mnt
, struct dentry
*dentry
,
317 if (m
->count
< m
->size
) {
318 char *s
= m
->buf
+ m
->count
;
319 char *p
= d_path(dentry
, mnt
, s
, m
->size
- m
->count
);
324 p
= m
->buf
+ m
->count
;
325 m
->count
= s
- m
->buf
;
327 } else if (!strchr(esc
, c
)) {
329 } else if (s
+ 4 > p
) {
333 *s
++ = '0' + ((c
& 0300) >> 6);
334 *s
++ = '0' + ((c
& 070) >> 3);
335 *s
++ = '0' + (c
& 07);
344 EXPORT_SYMBOL(seq_path
);
346 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
348 return NULL
+ (*pos
== 0);
351 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
357 static void single_stop(struct seq_file
*p
, void *v
)
361 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
364 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
368 op
->start
= single_start
;
369 op
->next
= single_next
;
370 op
->stop
= single_stop
;
372 res
= seq_open(file
, op
);
374 ((struct seq_file
*)file
->private_data
)->private = data
;
381 EXPORT_SYMBOL(single_open
);
383 int single_release(struct inode
*inode
, struct file
*file
)
385 struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
386 int res
= seq_release(inode
, file
);
391 EXPORT_SYMBOL(single_release
);
393 int seq_release_private(struct inode
*inode
, struct file
*file
)
395 struct seq_file
*seq
= file
->private_data
;
399 return seq_release(inode
, file
);
402 EXPORT_SYMBOL(seq_release_private
);