4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
9 #include <linux/seq_file.h>
10 #include <linux/slab.h>
12 #include <asm/uaccess.h>
16 * seq_open - initialize sequential file
17 * @file: file we initialize
18 * @op: method table describing the sequence
20 * seq_open() sets @file, associating it with a sequence described
21 * by @op. @op->start() sets the iterator up and returns the first
22 * element of sequence. @op->stop() shuts it down. @op->next()
23 * returns the next element of sequence. @op->show() prints element
24 * into the buffer. In case of error ->start() and ->next() return
25 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
26 * returns 0 in case of success and negative number in case of error.
28 int seq_open(struct file
*file
, struct seq_operations
*op
)
30 struct seq_file
*p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
33 memset(p
, 0, sizeof(*p
));
34 sema_init(&p
->sem
, 1);
36 file
->private_data
= p
;
41 * seq_read - ->read() method for sequential files.
42 * @file, @buf, @size, @ppos: see file_operations method
44 * Ready-made ->f_op->read()
46 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
48 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
55 if (ppos
!= &file
->f_pos
)
59 /* grab buffer if we didn't have one */
61 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
65 /* if not empty - flush it first */
67 n
= min(m
->count
, size
);
68 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
81 /* we need at least one record in buffer */
84 p
= m
->op
->start(m
, &pos
);
88 err
= m
->op
->show(m
, p
);
91 if (m
->count
< m
->size
)
95 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
104 /* they want more? let's try to get some more */
105 while (m
->count
< size
) {
106 size_t offs
= m
->count
;
108 p
= m
->op
->next(m
, p
, &next
);
109 if (!p
|| IS_ERR(p
)) {
113 err
= m
->op
->show(m
, p
);
114 if (err
|| m
->count
== m
->size
) {
121 n
= min(m
->count
, size
);
122 err
= copy_to_user(buf
, m
->buf
, n
);
147 static int traverse(struct seq_file
*m
, loff_t offset
)
154 m
->count
= m
->from
= 0;
158 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
162 p
= m
->op
->start(m
, &m
->index
);
167 error
= m
->op
->show(m
, p
);
170 if (m
->count
== m
->size
)
172 if (pos
+ m
->count
> offset
) {
173 m
->from
= offset
- pos
;
183 p
= m
->op
->next(m
, p
, &m
->index
);
191 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
192 return !m
->buf
? -ENOMEM
: -EAGAIN
;
196 * seq_lseek - ->llseek() method for sequential files.
197 * @file, @offset, @origin: see file_operations method
199 * Ready-made ->f_op->llseek()
201 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
203 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
204 long long retval
= -EINVAL
;
209 offset
+= file
->f_pos
;
214 if (offset
!= file
->f_pos
) {
215 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
218 /* with extreme prejudice... */
223 retval
= file
->f_pos
= offset
;
232 * seq_release - free the structures associated with sequential file.
233 * @file: file in question
234 * @inode: file->f_dentry->d_inode
236 * Frees the structures associated with sequential file; can be used
237 * as ->f_op->release() if you don't have private data to destroy.
239 int seq_release(struct inode
*inode
, struct file
*file
)
241 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
248 * seq_escape - print string into buffer, escaping some characters
251 * @esc: set of characters that need escaping
253 * Puts string into buffer, replacing each occurrence of character from
254 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
257 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
259 char *end
= m
->buf
+ m
->size
;
263 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
264 if (!strchr(esc
, c
)) {
270 *p
++ = '0' + ((c
& 0300) >> 6);
271 *p
++ = '0' + ((c
& 070) >> 3);
272 *p
++ = '0' + (c
& 07);
278 m
->count
= p
- m
->buf
;
282 int seq_printf(struct seq_file
*m
, const char *f
, ...)
287 if (m
->count
< m
->size
) {
289 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
291 if (m
->count
+ len
< m
->size
) {
300 int seq_path(struct seq_file
*m
,
301 struct vfsmount
*mnt
, struct dentry
*dentry
,
304 if (m
->count
< m
->size
) {
305 char *s
= m
->buf
+ m
->count
;
306 char *p
= d_path(dentry
, mnt
, s
, m
->size
- m
->count
);
311 p
= m
->buf
+ m
->count
;
312 m
->count
= s
- m
->buf
;
314 } else if (!strchr(esc
, c
)) {
316 } else if (s
+ 4 > p
) {
320 *s
++ = '0' + ((c
& 0300) >> 6);
321 *s
++ = '0' + ((c
& 070) >> 3);
322 *s
++ = '0' + (c
& 07);
331 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
333 return NULL
+ (*pos
== 0);
336 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
342 static void single_stop(struct seq_file
*p
, void *v
)
346 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void*), void *data
)
348 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
352 op
->start
= single_start
;
353 op
->next
= single_next
;
354 op
->stop
= single_stop
;
356 res
= seq_open(file
, op
);
358 ((struct seq_file
*)file
->private_data
)->private = data
;
365 int single_release(struct inode
*inode
, struct file
*file
)
367 struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
368 int res
= seq_release(inode
, file
);
373 int seq_release_private(struct inode
*inode
, struct file
*file
)
375 struct seq_file
*seq
= file
->private_data
;
379 return seq_release(inode
, file
);