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
);
57 static int traverse(struct seq_file
*m
, loff_t offset
)
59 loff_t pos
= 0, index
;
65 m
->count
= m
->from
= 0;
71 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
75 p
= m
->op
->start(m
, &index
);
80 error
= m
->op
->show(m
, p
);
83 if (unlikely(error
)) {
87 if (m
->count
== m
->size
)
89 if (pos
+ m
->count
> offset
) {
90 m
->from
= offset
- pos
;
102 p
= m
->op
->next(m
, p
, &index
);
111 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
112 return !m
->buf
? -ENOMEM
: -EAGAIN
;
116 * seq_read - ->read() method for sequential files.
117 * @file: the file to read from
118 * @buf: the buffer to read to
119 * @size: the maximum number of bytes to read
120 * @ppos: the current position in the file
122 * Ready-made ->f_op->read()
124 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
126 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
133 mutex_lock(&m
->lock
);
135 * seq_file->op->..m_start/m_stop/m_next may do special actions
136 * or optimisations based on the file->f_version, so we want to
137 * pass the file->f_version to those methods.
139 * seq_file->version is just copy of f_version, and seq_file
140 * methods can treat it simply as file version.
141 * It is copied in first and copied out after all operations.
142 * It is convenient to have it as part of structure to avoid the
143 * need of passing another argument to all the seq_file methods.
145 m
->version
= file
->f_version
;
146 /* grab buffer if we didn't have one */
148 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
152 /* if not empty - flush it first */
154 n
= min(m
->count
, size
);
155 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
168 /* we need at least one record in buffer */
170 p
= m
->op
->start(m
, &pos
);
175 err
= m
->op
->show(m
, p
);
180 if (unlikely(!m
->count
)) {
181 p
= m
->op
->next(m
, p
, &pos
);
185 if (m
->count
< m
->size
)
189 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
195 p
= m
->op
->start(m
, &pos
);
201 /* they want more? let's try to get some more */
202 while (m
->count
< size
) {
203 size_t offs
= m
->count
;
205 p
= m
->op
->next(m
, p
, &next
);
206 if (!p
|| IS_ERR(p
)) {
210 err
= m
->op
->show(m
, p
);
211 if (m
->count
== m
->size
|| err
) {
213 if (likely(err
<= 0))
219 n
= min(m
->count
, size
);
220 err
= copy_to_user(buf
, m
->buf
, n
);
235 file
->f_version
= m
->version
;
236 mutex_unlock(&m
->lock
);
245 EXPORT_SYMBOL(seq_read
);
248 * seq_lseek - ->llseek() method for sequential files.
249 * @file: the file in question
250 * @offset: new position
251 * @origin: 0 for absolute, 1 for relative position
253 * Ready-made ->f_op->llseek()
255 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
257 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
258 loff_t retval
= -EINVAL
;
260 mutex_lock(&m
->lock
);
261 m
->version
= file
->f_version
;
264 offset
+= file
->f_pos
;
269 if (offset
!= file
->f_pos
) {
270 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
273 /* with extreme prejudice... */
279 retval
= file
->f_pos
= offset
;
283 file
->f_version
= m
->version
;
284 mutex_unlock(&m
->lock
);
287 EXPORT_SYMBOL(seq_lseek
);
290 * seq_release - free the structures associated with sequential file.
291 * @file: file in question
292 * @inode: file->f_path.dentry->d_inode
294 * Frees the structures associated with sequential file; can be used
295 * as ->f_op->release() if you don't have private data to destroy.
297 int seq_release(struct inode
*inode
, struct file
*file
)
299 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
304 EXPORT_SYMBOL(seq_release
);
307 * seq_escape - print string into buffer, escaping some characters
310 * @esc: set of characters that need escaping
312 * Puts string into buffer, replacing each occurrence of character from
313 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
316 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
318 char *end
= m
->buf
+ m
->size
;
322 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
323 if (!strchr(esc
, c
)) {
329 *p
++ = '0' + ((c
& 0300) >> 6);
330 *p
++ = '0' + ((c
& 070) >> 3);
331 *p
++ = '0' + (c
& 07);
337 m
->count
= p
- m
->buf
;
340 EXPORT_SYMBOL(seq_escape
);
342 int seq_printf(struct seq_file
*m
, const char *f
, ...)
347 if (m
->count
< m
->size
) {
349 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
351 if (m
->count
+ len
< m
->size
) {
359 EXPORT_SYMBOL(seq_printf
);
362 * mangle_path - mangle and copy path to buffer beginning
364 * @p: beginning of path in above buffer
365 * @esc: set of characters that need escaping
367 * Copy the path from @p to @s, replacing each occurrence of character from
368 * @esc with usual octal escape.
369 * Returns pointer past last written character in @s, or NULL in case of
372 char *mangle_path(char *s
, char *p
, char *esc
)
378 } else if (!strchr(esc
, c
)) {
380 } else if (s
+ 4 > p
) {
384 *s
++ = '0' + ((c
& 0300) >> 6);
385 *s
++ = '0' + ((c
& 070) >> 3);
386 *s
++ = '0' + (c
& 07);
391 EXPORT_SYMBOL(mangle_path
);
394 * seq_path - seq_file interface to print a pathname
395 * @m: the seq_file handle
396 * @path: the struct path to print
397 * @esc: set of characters to escape in the output
399 * return the absolute path of 'path', as represented by the
400 * dentry / mnt pair in the path parameter.
402 int seq_path(struct seq_file
*m
, struct path
*path
, char *esc
)
404 if (m
->count
< m
->size
) {
405 char *s
= m
->buf
+ m
->count
;
406 char *p
= d_path(path
, s
, m
->size
- m
->count
);
408 s
= mangle_path(s
, p
, esc
);
410 p
= m
->buf
+ m
->count
;
411 m
->count
= s
- m
->buf
;
419 EXPORT_SYMBOL(seq_path
);
422 * Same as seq_path, but relative to supplied root.
424 * root may be changed, see __d_path().
426 int seq_path_root(struct seq_file
*m
, struct path
*path
, struct path
*root
,
429 int err
= -ENAMETOOLONG
;
430 if (m
->count
< m
->size
) {
431 char *s
= m
->buf
+ m
->count
;
434 spin_lock(&dcache_lock
);
435 p
= __d_path(path
, root
, s
, m
->size
- m
->count
);
436 spin_unlock(&dcache_lock
);
439 s
= mangle_path(s
, p
, esc
);
441 p
= m
->buf
+ m
->count
;
442 m
->count
= s
- m
->buf
;
452 * returns the path of the 'dentry' from the root of its filesystem.
454 int seq_dentry(struct seq_file
*m
, struct dentry
*dentry
, char *esc
)
456 if (m
->count
< m
->size
) {
457 char *s
= m
->buf
+ m
->count
;
458 char *p
= dentry_path(dentry
, s
, m
->size
- m
->count
);
460 s
= mangle_path(s
, p
, esc
);
462 p
= m
->buf
+ m
->count
;
463 m
->count
= s
- m
->buf
;
472 int seq_bitmap(struct seq_file
*m
, const unsigned long *bits
,
473 unsigned int nr_bits
)
475 if (m
->count
< m
->size
) {
476 int len
= bitmap_scnprintf(m
->buf
+ m
->count
,
477 m
->size
- m
->count
, bits
, nr_bits
);
478 if (m
->count
+ len
< m
->size
) {
486 EXPORT_SYMBOL(seq_bitmap
);
488 int seq_bitmap_list(struct seq_file
*m
, unsigned long *bits
,
489 unsigned int nr_bits
)
491 if (m
->count
< m
->size
) {
492 int len
= bitmap_scnlistprintf(m
->buf
+ m
->count
,
493 m
->size
- m
->count
, bits
, nr_bits
);
494 if (m
->count
+ len
< m
->size
) {
502 EXPORT_SYMBOL(seq_bitmap_list
);
504 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
506 return NULL
+ (*pos
== 0);
509 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
515 static void single_stop(struct seq_file
*p
, void *v
)
519 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
522 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
526 op
->start
= single_start
;
527 op
->next
= single_next
;
528 op
->stop
= single_stop
;
530 res
= seq_open(file
, op
);
532 ((struct seq_file
*)file
->private_data
)->private = data
;
538 EXPORT_SYMBOL(single_open
);
540 int single_release(struct inode
*inode
, struct file
*file
)
542 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
543 int res
= seq_release(inode
, file
);
547 EXPORT_SYMBOL(single_release
);
549 int seq_release_private(struct inode
*inode
, struct file
*file
)
551 struct seq_file
*seq
= file
->private_data
;
555 return seq_release(inode
, file
);
557 EXPORT_SYMBOL(seq_release_private
);
559 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
564 struct seq_file
*seq
;
566 private = kzalloc(psize
, GFP_KERNEL
);
570 rc
= seq_open(f
, ops
);
574 seq
= f
->private_data
;
575 seq
->private = private;
583 EXPORT_SYMBOL(__seq_open_private
);
585 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
588 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
590 EXPORT_SYMBOL(seq_open_private
);
592 int seq_putc(struct seq_file
*m
, char c
)
594 if (m
->count
< m
->size
) {
595 m
->buf
[m
->count
++] = c
;
600 EXPORT_SYMBOL(seq_putc
);
602 int seq_puts(struct seq_file
*m
, const char *s
)
605 if (m
->count
+ len
< m
->size
) {
606 memcpy(m
->buf
+ m
->count
, s
, len
);
613 EXPORT_SYMBOL(seq_puts
);
615 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
617 struct list_head
*lh
;
619 list_for_each(lh
, head
)
626 EXPORT_SYMBOL(seq_list_start
);
628 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
633 return seq_list_start(head
, pos
- 1);
636 EXPORT_SYMBOL(seq_list_start_head
);
638 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
640 struct list_head
*lh
;
642 lh
= ((struct list_head
*)v
)->next
;
644 return lh
== head
? NULL
: lh
;
647 EXPORT_SYMBOL(seq_list_next
);