]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/seq_file.c
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
, const struct seq_operations
*op
)
31 struct seq_file
*p
= file
->private_data
;
34 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
37 file
->private_data
= p
;
39 memset(p
, 0, sizeof(*p
));
44 * Wrappers around seq_open(e.g. swaps_open) need to be
45 * aware of this. If they set f_version themselves, they
46 * should call seq_open first and then set f_version.
50 /* SEQ files support lseek, but not pread/pwrite */
51 file
->f_mode
&= ~(FMODE_PREAD
| FMODE_PWRITE
);
54 EXPORT_SYMBOL(seq_open
);
57 * seq_read - ->read() method for sequential files.
58 * @file: the file to read from
59 * @buf: the buffer to read to
60 * @size: the maximum number of bytes to read
61 * @ppos: the current position in the file
63 * Ready-made ->f_op->read()
65 ssize_t
seq_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*ppos
)
67 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
76 * seq_file->op->..m_start/m_stop/m_next may do special actions
77 * or optimisations based on the file->f_version, so we want to
78 * pass the file->f_version to those methods.
80 * seq_file->version is just copy of f_version, and seq_file
81 * methods can treat it simply as file version.
82 * It is copied in first and copied out after all operations.
83 * It is convenient to have it as part of structure to avoid the
84 * need of passing another argument to all the seq_file methods.
86 m
->version
= file
->f_version
;
87 /* grab buffer if we didn't have one */
89 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
93 /* if not empty - flush it first */
95 n
= min(m
->count
, size
);
96 err
= copy_to_user(buf
, m
->buf
+ m
->from
, n
);
109 /* we need at least one record in buffer */
112 p
= m
->op
->start(m
, &pos
);
116 err
= m
->op
->show(m
, p
);
119 if (m
->count
< m
->size
)
123 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
133 /* they want more? let's try to get some more */
134 while (m
->count
< size
) {
135 size_t offs
= m
->count
;
137 p
= m
->op
->next(m
, p
, &next
);
138 if (!p
|| IS_ERR(p
)) {
142 err
= m
->op
->show(m
, p
);
143 if (err
|| m
->count
== m
->size
) {
150 n
= min(m
->count
, size
);
151 err
= copy_to_user(buf
, m
->buf
, n
);
166 file
->f_version
= m
->version
;
167 mutex_unlock(&m
->lock
);
176 EXPORT_SYMBOL(seq_read
);
178 static int traverse(struct seq_file
*m
, loff_t offset
)
180 loff_t pos
= 0, index
;
186 m
->count
= m
->from
= 0;
192 m
->buf
= kmalloc(m
->size
= PAGE_SIZE
, GFP_KERNEL
);
196 p
= m
->op
->start(m
, &index
);
201 error
= m
->op
->show(m
, p
);
204 if (m
->count
== m
->size
)
206 if (pos
+ m
->count
> offset
) {
207 m
->from
= offset
- pos
;
219 p
= m
->op
->next(m
, p
, &index
);
227 m
->buf
= kmalloc(m
->size
<<= 1, GFP_KERNEL
);
228 return !m
->buf
? -ENOMEM
: -EAGAIN
;
232 * seq_lseek - ->llseek() method for sequential files.
233 * @file: the file in question
234 * @offset: new position
235 * @origin: 0 for absolute, 1 for relative position
237 * Ready-made ->f_op->llseek()
239 loff_t
seq_lseek(struct file
*file
, loff_t offset
, int origin
)
241 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
242 long long retval
= -EINVAL
;
244 mutex_lock(&m
->lock
);
245 m
->version
= file
->f_version
;
248 offset
+= file
->f_pos
;
253 if (offset
!= file
->f_pos
) {
254 while ((retval
=traverse(m
, offset
)) == -EAGAIN
)
257 /* with extreme prejudice... */
263 retval
= file
->f_pos
= offset
;
267 file
->f_version
= m
->version
;
268 mutex_unlock(&m
->lock
);
271 EXPORT_SYMBOL(seq_lseek
);
274 * seq_release - free the structures associated with sequential file.
275 * @file: file in question
276 * @inode: file->f_path.dentry->d_inode
278 * Frees the structures associated with sequential file; can be used
279 * as ->f_op->release() if you don't have private data to destroy.
281 int seq_release(struct inode
*inode
, struct file
*file
)
283 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
288 EXPORT_SYMBOL(seq_release
);
291 * seq_escape - print string into buffer, escaping some characters
294 * @esc: set of characters that need escaping
296 * Puts string into buffer, replacing each occurrence of character from
297 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
300 int seq_escape(struct seq_file
*m
, const char *s
, const char *esc
)
302 char *end
= m
->buf
+ m
->size
;
306 for (p
= m
->buf
+ m
->count
; (c
= *s
) != '\0' && p
< end
; s
++) {
307 if (!strchr(esc
, c
)) {
313 *p
++ = '0' + ((c
& 0300) >> 6);
314 *p
++ = '0' + ((c
& 070) >> 3);
315 *p
++ = '0' + (c
& 07);
321 m
->count
= p
- m
->buf
;
324 EXPORT_SYMBOL(seq_escape
);
326 int seq_printf(struct seq_file
*m
, const char *f
, ...)
331 if (m
->count
< m
->size
) {
333 len
= vsnprintf(m
->buf
+ m
->count
, m
->size
- m
->count
, f
, args
);
335 if (m
->count
+ len
< m
->size
) {
343 EXPORT_SYMBOL(seq_printf
);
345 int seq_path(struct seq_file
*m
, struct path
*path
, char *esc
)
347 if (m
->count
< m
->size
) {
348 char *s
= m
->buf
+ m
->count
;
349 char *p
= d_path(path
, s
, m
->size
- m
->count
);
354 p
= m
->buf
+ m
->count
;
355 m
->count
= s
- m
->buf
;
357 } else if (!strchr(esc
, c
)) {
359 } else if (s
+ 4 > p
) {
363 *s
++ = '0' + ((c
& 0300) >> 6);
364 *s
++ = '0' + ((c
& 070) >> 3);
365 *s
++ = '0' + (c
& 07);
373 EXPORT_SYMBOL(seq_path
);
375 static void *single_start(struct seq_file
*p
, loff_t
*pos
)
377 return NULL
+ (*pos
== 0);
380 static void *single_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
386 static void single_stop(struct seq_file
*p
, void *v
)
390 int single_open(struct file
*file
, int (*show
)(struct seq_file
*, void *),
393 struct seq_operations
*op
= kmalloc(sizeof(*op
), GFP_KERNEL
);
397 op
->start
= single_start
;
398 op
->next
= single_next
;
399 op
->stop
= single_stop
;
401 res
= seq_open(file
, op
);
403 ((struct seq_file
*)file
->private_data
)->private = data
;
409 EXPORT_SYMBOL(single_open
);
411 int single_release(struct inode
*inode
, struct file
*file
)
413 const struct seq_operations
*op
= ((struct seq_file
*)file
->private_data
)->op
;
414 int res
= seq_release(inode
, file
);
418 EXPORT_SYMBOL(single_release
);
420 int seq_release_private(struct inode
*inode
, struct file
*file
)
422 struct seq_file
*seq
= file
->private_data
;
426 return seq_release(inode
, file
);
428 EXPORT_SYMBOL(seq_release_private
);
430 void *__seq_open_private(struct file
*f
, const struct seq_operations
*ops
,
435 struct seq_file
*seq
;
437 private = kzalloc(psize
, GFP_KERNEL
);
441 rc
= seq_open(f
, ops
);
445 seq
= f
->private_data
;
446 seq
->private = private;
454 EXPORT_SYMBOL(__seq_open_private
);
456 int seq_open_private(struct file
*filp
, const struct seq_operations
*ops
,
459 return __seq_open_private(filp
, ops
, psize
) ? 0 : -ENOMEM
;
461 EXPORT_SYMBOL(seq_open_private
);
463 int seq_putc(struct seq_file
*m
, char c
)
465 if (m
->count
< m
->size
) {
466 m
->buf
[m
->count
++] = c
;
471 EXPORT_SYMBOL(seq_putc
);
473 int seq_puts(struct seq_file
*m
, const char *s
)
476 if (m
->count
+ len
< m
->size
) {
477 memcpy(m
->buf
+ m
->count
, s
, len
);
484 EXPORT_SYMBOL(seq_puts
);
486 struct list_head
*seq_list_start(struct list_head
*head
, loff_t pos
)
488 struct list_head
*lh
;
490 list_for_each(lh
, head
)
497 EXPORT_SYMBOL(seq_list_start
);
499 struct list_head
*seq_list_start_head(struct list_head
*head
, loff_t pos
)
504 return seq_list_start(head
, pos
- 1);
507 EXPORT_SYMBOL(seq_list_start_head
);
509 struct list_head
*seq_list_next(void *v
, struct list_head
*head
, loff_t
*ppos
)
511 struct list_head
*lh
;
513 lh
= ((struct list_head
*)v
)->next
;
515 return lh
== head
? NULL
: lh
;
518 EXPORT_SYMBOL(seq_list_next
);