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.
28 * Returning SEQ_SKIP means "discard this element and move on".
30 int seq_open(struct file
*file
, const struct seq_operations
*op
)
32 struct seq_file
*p
= file
->private_data
;
35 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
38 file
->private_data
= p
;
40 memset(p
, 0, sizeof(*p
));
45 * Wrappers around seq_open(e.g. swaps_open) need to be
46 * aware of this. If they set f_version themselves, they
47 * should call seq_open first and then set f_version.
51 /* SEQ files support lseek, but not pread/pwrite */
52 file
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
55 EXPORT_SYMBOL(seq_open
);
58 * seq_read - ->read() method for sequential files.
59 * @file: the file to read from
60 * @buf: the buffer to read to
61 * @size: the maximum number of bytes to read
62 * @ppos: the current position in the file
64 * Ready-made ->f_op->read()
66 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
68 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
77 * seq_file->op->..m_start/m_stop/m_next may do special actions
78 * or optimisations based on the file->f_version, so we want to
79 * pass the file->f_version to those methods.
81 * seq_file->version is just copy of f_version, and seq_file
82 * methods can treat it simply as file version.
83 * It is copied in first and copied out after all operations.
84 * It is convenient to have it as part of structure to avoid the
85 * need of passing another argument to all the seq_file methods.
87 m
->version
= file
->f_version
;
88 /* grab buffer if we didn't have one */
90 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
94 /* if not empty - flush it first */
96 n
= min(m
->count
, size
);
97 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
110 /* we need at least one record in buffer */
112 p
= m
->op
->start(m
, &pos
);
117 err
= m
->op
->show(m
, p
);
122 if (unlikely(!m
->count
)) {
123 p
= m
->op
->next(m
, p
, &pos
);
127 if (m
->count
< m
->size
)
131 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
137 p
= m
->op
->start(m
, &pos
);
143 /* they want more? let's try to get some more */
144 while (m
->count
< size
) {
145 size_t offs
= m
->count
;
147 p
= m
->op
->next(m
, p
, &next
);
148 if (!p
|| IS_ERR(p
)) {
152 err
= m
->op
->show(m
, p
);
153 if (m
->count
== m
->size
|| err
) {
155 if (likely(err
<= 0))
161 n
= min(m
->count
, size
);
162 err
= copy_to_user(buf
, m
->buf
, n
);
177 file
->f_version
= m
->version
;
178 mutex_unlock(&m
->lock
);
187 EXPORT_SYMBOL(seq_read
);
189 static int traverse(struct seq_file
*m
, loff_t offset
)
191 loff_t pos
= 0, index
;
197 m
->count
= m
->from
= 0;
203 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
207 p
= m
->op
->start(m
, &index
);
212 error
= m
->op
->show(m
, p
);
215 if (unlikely(error
)) {
219 if (m
->count
== m
->size
)
221 if (pos
+ m
->count
> offset
) {
222 m
->from
= offset
- pos
;
234 p
= m
->op
->next(m
, p
, &index
);
242 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
243 return !m
->buf
? -ENOMEM
: -EAGAIN
;
247 * seq_lseek - ->llseek() method for sequential files.
248 * @file: the file in question
249 * @offset: new position
250 * @origin: 0 for absolute, 1 for relative position
252 * Ready-made ->f_op->llseek()
254 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
256 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
257 loff_t retval
= -EINVAL
;
259 mutex_lock(&m
->lock
);
260 m
->version
= file
->f_version
;
263 offset
+= file
->f_pos
;
268 if (offset
!= file
->f_pos
) {
269 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
272 /* with extreme prejudice... */
278 retval
= file
->f_pos
= offset
;
282 file
->f_version
= m
->version
;
283 mutex_unlock(&m
->lock
);
286 EXPORT_SYMBOL(seq_lseek
);
289 * seq_release - free the structures associated with sequential file.
290 * @file: file in question
291 * @inode: file->f_path.dentry->d_inode
293 * Frees the structures associated with sequential file; can be used
294 * as ->f_op->release() if you don't have private data to destroy.
296 int seq_release(struct inode
*inode
, struct file
*file
)
298 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
303 EXPORT_SYMBOL(seq_release
);
306 * seq_escape - print string into buffer, escaping some characters
309 * @esc: set of characters that need escaping
311 * Puts string into buffer, replacing each occurrence of character from
312 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
315 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
317 char *end
= m
->buf
+ m
->size
;
321 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
322 if (!strchr(esc
, c
)) {
328 *p
++ = '0' + ((c
& 0300) >> 6);
329 *p
++ = '0' + ((c
& 070) >> 3);
330 *p
++ = '0' + (c
& 07);
336 m
->count
= p
- m
->buf
;
339 EXPORT_SYMBOL(seq_escape
);
341 int seq_printf(struct seq_file
*m
, const char *f
, ...)
346 if (m
->count
< m
->size
) {
348 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
350 if (m
->count
+ len
< m
->size
) {
358 EXPORT_SYMBOL(seq_printf
);
361 * mangle_path - mangle and copy path to buffer beginning
363 * @p: beginning of path in above buffer
364 * @esc: set of characters that need escaping
366 * Copy the path from @p to @s, replacing each occurrence of character from
367 * @esc with usual octal escape.
368 * Returns pointer past last written character in @s, or NULL in case of
371 char *mangle_path(char *s
, char *p
, char *esc
)
377 } else if (!strchr(esc
, c
)) {
379 } else if (s
+ 4 > p
) {
383 *s
++ = '0' + ((c
& 0300) >> 6);
384 *s
++ = '0' + ((c
& 070) >> 3);
385 *s
++ = '0' + (c
& 07);
390 EXPORT_SYMBOL(mangle_path
);
393 * return the absolute path of 'dentry' residing in mount 'mnt'.
395 int seq_path(struct seq_file
*m
, struct path
*path
, char *esc
)
397 if (m
->count
< m
->size
) {
398 char *s
= m
->buf
+ m
->count
;
399 char *p
= d_path(path
, s
, m
->size
- m
->count
);
401 s
= mangle_path(s
, p
, esc
);
403 p
= m
->buf
+ m
->count
;
404 m
->count
= s
- m
->buf
;
412 EXPORT_SYMBOL(seq_path
);
415 * Same as seq_path, but relative to supplied root.
417 * root may be changed, see __d_path().
419 int seq_path_root(struct seq_file
*m
, struct path
*path
, struct path
*root
,
422 int err
= -ENAMETOOLONG
;
423 if (m
->count
< m
->size
) {
424 char *s
= m
->buf
+ m
->count
;
427 spin_lock(&dcache_lock
);
428 p
= __d_path(path
, root
, s
, m
->size
- m
->count
);
429 spin_unlock(&dcache_lock
);
432 s
= mangle_path(s
, p
, esc
);
434 p
= m
->buf
+ m
->count
;
435 m
->count
= s
- m
->buf
;
445 * returns the path of the 'dentry' from the root of its filesystem.
447 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, char *esc
)
449 if (m
->count
< m
->size
) {
450 char *s
= m
->buf
+ m
->count
;
451 char *p
= dentry_path(dentry
, s
, m
->size
- m
->count
);
453 s
= mangle_path(s
, p
, esc
);
455 p
= m
->buf
+ m
->count
;
456 m
->count
= s
- m
->buf
;
465 int seq_bitmap(struct seq_file
*m
, unsigned long *bits
, unsigned int nr_bits
)
467 if (m
->count
< m
->size
) {
468 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
469 m
->size
- m
->count
, bits
, nr_bits
);
470 if (m
->count
+ len
< m
->size
) {
478 EXPORT_SYMBOL(seq_bitmap
);
480 int seq_bitmap_list(struct seq_file
*m
, unsigned long *bits
,
481 unsigned int nr_bits
)
483 if (m
->count
< m
->size
) {
484 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
485 m
->size
- m
->count
, bits
, nr_bits
);
486 if (m
->count
+ len
< m
->size
) {
494 EXPORT_SYMBOL(seq_bitmap_list
);
496 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
498 return NULL
+ (*pos
== 0);
501 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
507 static void single_stop(struct seq_file
*p
, void *v
)
511 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
514 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
518 op
->start
= single_start
;
519 op
->next
= single_next
;
520 op
->stop
= single_stop
;
522 res
= seq_open(file
, op
);
524 ((struct seq_file
*)file
->private_data
)->private = data
;
530 EXPORT_SYMBOL(single_open
);
532 int single_release(struct inode
*inode
, struct file
*file
)
534 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
535 int res
= seq_release(inode
, file
);
539 EXPORT_SYMBOL(single_release
);
541 int seq_release_private(struct inode
*inode
, struct file
*file
)
543 struct seq_file
*seq
= file
->private_data
;
547 return seq_release(inode
, file
);
549 EXPORT_SYMBOL(seq_release_private
);
551 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
556 struct seq_file
*seq
;
558 private = kzalloc(psize
, GFP_KERNEL
);
562 rc
= seq_open(f
, ops
);
566 seq
= f
->private_data
;
567 seq
->private = private;
575 EXPORT_SYMBOL(__seq_open_private
);
577 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
580 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
582 EXPORT_SYMBOL(seq_open_private
);
584 int seq_putc(struct seq_file
*m
, char c
)
586 if (m
->count
< m
->size
) {
587 m
->buf
[m
->count
++] = c
;
592 EXPORT_SYMBOL(seq_putc
);
594 int seq_puts(struct seq_file
*m
, const char *s
)
597 if (m
->count
+ len
< m
->size
) {
598 memcpy(m
->buf
+ m
->count
, s
, len
);
605 EXPORT_SYMBOL(seq_puts
);
607 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
609 struct list_head
*lh
;
611 list_for_each(lh
, head
)
618 EXPORT_SYMBOL(seq_list_start
);
620 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
625 return seq_list_start(head
, pos
- 1);
628 EXPORT_SYMBOL(seq_list_start_head
);
630 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
632 struct list_head
*lh
;
634 lh
= ((struct list_head
*)v
)->next
;
636 return lh
== head
? NULL
: lh
;
639 EXPORT_SYMBOL(seq_list_next
);