sh: Fix Kconfig of AP-325RXA
[linux/fpc-iii.git] / fs / seq_file.c
blob3f54dbd6c49b0c28506505bc41f99f20472fba8f
1 /*
2 * linux/fs/seq_file.c
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
8 #include <linux/fs.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
11 #include <linux/slab.h>
13 #include <asm/uaccess.h>
14 #include <asm/page.h>
16 /**
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;
34 if (!p) {
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
36 if (!p)
37 return -ENOMEM;
38 file->private_data = p;
40 memset(p, 0, sizeof(*p));
41 mutex_init(&p->lock);
42 p->op = op;
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.
49 file->f_version = 0;
51 /* SEQ files support lseek, but not pread/pwrite */
52 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
53 return 0;
55 EXPORT_SYMBOL(seq_open);
57 /**
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;
69 size_t copied = 0;
70 loff_t pos;
71 size_t n;
72 void *p;
73 int err = 0;
75 mutex_lock(&m->lock);
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 */
89 if (!m->buf) {
90 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
91 if (!m->buf)
92 goto Enomem;
94 /* if not empty - flush it first */
95 if (m->count) {
96 n = min(m->count, size);
97 err = copy_to_user(buf, m->buf + m->from, n);
98 if (err)
99 goto Efault;
100 m->count -= n;
101 m->from += n;
102 size -= n;
103 buf += n;
104 copied += n;
105 if (!m->count)
106 m->index++;
107 if (!size)
108 goto Done;
110 /* we need at least one record in buffer */
111 while (1) {
112 pos = m->index;
113 p = m->op->start(m, &pos);
114 err = PTR_ERR(p);
115 if (!p || IS_ERR(p))
116 break;
117 err = m->op->show(m, p);
118 if (err < 0)
119 break;
120 if (unlikely(err))
121 m->count = 0;
122 if (m->count < m->size)
123 goto Fill;
124 m->op->stop(m, p);
125 kfree(m->buf);
126 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
127 if (!m->buf)
128 goto Enomem;
129 m->count = 0;
130 m->version = 0;
132 m->op->stop(m, p);
133 m->count = 0;
134 goto Done;
135 Fill:
136 /* they want more? let's try to get some more */
137 while (m->count < size) {
138 size_t offs = m->count;
139 loff_t next = pos;
140 p = m->op->next(m, p, &next);
141 if (!p || IS_ERR(p)) {
142 err = PTR_ERR(p);
143 break;
145 err = m->op->show(m, p);
146 if (m->count == m->size || err) {
147 m->count = offs;
148 if (likely(err <= 0))
149 break;
151 pos = next;
153 m->op->stop(m, p);
154 n = min(m->count, size);
155 err = copy_to_user(buf, m->buf, n);
156 if (err)
157 goto Efault;
158 copied += n;
159 m->count -= n;
160 if (m->count)
161 m->from = n;
162 else
163 pos++;
164 m->index = pos;
165 Done:
166 if (!copied)
167 copied = err;
168 else
169 *ppos += copied;
170 file->f_version = m->version;
171 mutex_unlock(&m->lock);
172 return copied;
173 Enomem:
174 err = -ENOMEM;
175 goto Done;
176 Efault:
177 err = -EFAULT;
178 goto Done;
180 EXPORT_SYMBOL(seq_read);
182 static int traverse(struct seq_file *m, loff_t offset)
184 loff_t pos = 0, index;
185 int error = 0;
186 void *p;
188 m->version = 0;
189 index = 0;
190 m->count = m->from = 0;
191 if (!offset) {
192 m->index = index;
193 return 0;
195 if (!m->buf) {
196 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
197 if (!m->buf)
198 return -ENOMEM;
200 p = m->op->start(m, &index);
201 while (p) {
202 error = PTR_ERR(p);
203 if (IS_ERR(p))
204 break;
205 error = m->op->show(m, p);
206 if (error < 0)
207 break;
208 if (unlikely(error)) {
209 error = 0;
210 m->count = 0;
212 if (m->count == m->size)
213 goto Eoverflow;
214 if (pos + m->count > offset) {
215 m->from = offset - pos;
216 m->count -= m->from;
217 m->index = index;
218 break;
220 pos += m->count;
221 m->count = 0;
222 if (pos == offset) {
223 index++;
224 m->index = index;
225 break;
227 p = m->op->next(m, p, &index);
229 m->op->stop(m, p);
230 return error;
232 Eoverflow:
233 m->op->stop(m, p);
234 kfree(m->buf);
235 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
236 return !m->buf ? -ENOMEM : -EAGAIN;
240 * seq_lseek - ->llseek() method for sequential files.
241 * @file: the file in question
242 * @offset: new position
243 * @origin: 0 for absolute, 1 for relative position
245 * Ready-made ->f_op->llseek()
247 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
249 struct seq_file *m = (struct seq_file *)file->private_data;
250 loff_t retval = -EINVAL;
252 mutex_lock(&m->lock);
253 m->version = file->f_version;
254 switch (origin) {
255 case 1:
256 offset += file->f_pos;
257 case 0:
258 if (offset < 0)
259 break;
260 retval = offset;
261 if (offset != file->f_pos) {
262 while ((retval=traverse(m, offset)) == -EAGAIN)
264 if (retval) {
265 /* with extreme prejudice... */
266 file->f_pos = 0;
267 m->version = 0;
268 m->index = 0;
269 m->count = 0;
270 } else {
271 retval = file->f_pos = offset;
275 file->f_version = m->version;
276 mutex_unlock(&m->lock);
277 return retval;
279 EXPORT_SYMBOL(seq_lseek);
282 * seq_release - free the structures associated with sequential file.
283 * @file: file in question
284 * @inode: file->f_path.dentry->d_inode
286 * Frees the structures associated with sequential file; can be used
287 * as ->f_op->release() if you don't have private data to destroy.
289 int seq_release(struct inode *inode, struct file *file)
291 struct seq_file *m = (struct seq_file *)file->private_data;
292 kfree(m->buf);
293 kfree(m);
294 return 0;
296 EXPORT_SYMBOL(seq_release);
299 * seq_escape - print string into buffer, escaping some characters
300 * @m: target buffer
301 * @s: string
302 * @esc: set of characters that need escaping
304 * Puts string into buffer, replacing each occurrence of character from
305 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
306 * case of overflow.
308 int seq_escape(struct seq_file *m, const char *s, const char *esc)
310 char *end = m->buf + m->size;
311 char *p;
312 char c;
314 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
315 if (!strchr(esc, c)) {
316 *p++ = c;
317 continue;
319 if (p + 3 < end) {
320 *p++ = '\\';
321 *p++ = '0' + ((c & 0300) >> 6);
322 *p++ = '0' + ((c & 070) >> 3);
323 *p++ = '0' + (c & 07);
324 continue;
326 m->count = m->size;
327 return -1;
329 m->count = p - m->buf;
330 return 0;
332 EXPORT_SYMBOL(seq_escape);
334 int seq_printf(struct seq_file *m, const char *f, ...)
336 va_list args;
337 int len;
339 if (m->count < m->size) {
340 va_start(args, f);
341 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
342 va_end(args);
343 if (m->count + len < m->size) {
344 m->count += len;
345 return 0;
348 m->count = m->size;
349 return -1;
351 EXPORT_SYMBOL(seq_printf);
353 static char *mangle_path(char *s, char *p, char *esc)
355 while (s <= p) {
356 char c = *p++;
357 if (!c) {
358 return s;
359 } else if (!strchr(esc, c)) {
360 *s++ = c;
361 } else if (s + 4 > p) {
362 break;
363 } else {
364 *s++ = '\\';
365 *s++ = '0' + ((c & 0300) >> 6);
366 *s++ = '0' + ((c & 070) >> 3);
367 *s++ = '0' + (c & 07);
370 return NULL;
374 * return the absolute path of 'dentry' residing in mount 'mnt'.
376 int seq_path(struct seq_file *m, struct path *path, char *esc)
378 if (m->count < m->size) {
379 char *s = m->buf + m->count;
380 char *p = d_path(path, s, m->size - m->count);
381 if (!IS_ERR(p)) {
382 s = mangle_path(s, p, esc);
383 if (s) {
384 p = m->buf + m->count;
385 m->count = s - m->buf;
386 return s - p;
390 m->count = m->size;
391 return -1;
393 EXPORT_SYMBOL(seq_path);
396 * Same as seq_path, but relative to supplied root.
398 * root may be changed, see __d_path().
400 int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
401 char *esc)
403 int err = -ENAMETOOLONG;
404 if (m->count < m->size) {
405 char *s = m->buf + m->count;
406 char *p;
408 spin_lock(&dcache_lock);
409 p = __d_path(path, root, s, m->size - m->count);
410 spin_unlock(&dcache_lock);
411 err = PTR_ERR(p);
412 if (!IS_ERR(p)) {
413 s = mangle_path(s, p, esc);
414 if (s) {
415 p = m->buf + m->count;
416 m->count = s - m->buf;
417 return 0;
421 m->count = m->size;
422 return err;
426 * returns the path of the 'dentry' from the root of its filesystem.
428 int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
430 if (m->count < m->size) {
431 char *s = m->buf + m->count;
432 char *p = dentry_path(dentry, s, m->size - m->count);
433 if (!IS_ERR(p)) {
434 s = mangle_path(s, p, esc);
435 if (s) {
436 p = m->buf + m->count;
437 m->count = s - m->buf;
438 return s - p;
442 m->count = m->size;
443 return -1;
446 static void *single_start(struct seq_file *p, loff_t *pos)
448 return NULL + (*pos == 0);
451 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
453 ++*pos;
454 return NULL;
457 static void single_stop(struct seq_file *p, void *v)
461 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
462 void *data)
464 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
465 int res = -ENOMEM;
467 if (op) {
468 op->start = single_start;
469 op->next = single_next;
470 op->stop = single_stop;
471 op->show = show;
472 res = seq_open(file, op);
473 if (!res)
474 ((struct seq_file *)file->private_data)->private = data;
475 else
476 kfree(op);
478 return res;
480 EXPORT_SYMBOL(single_open);
482 int single_release(struct inode *inode, struct file *file)
484 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
485 int res = seq_release(inode, file);
486 kfree(op);
487 return res;
489 EXPORT_SYMBOL(single_release);
491 int seq_release_private(struct inode *inode, struct file *file)
493 struct seq_file *seq = file->private_data;
495 kfree(seq->private);
496 seq->private = NULL;
497 return seq_release(inode, file);
499 EXPORT_SYMBOL(seq_release_private);
501 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
502 int psize)
504 int rc;
505 void *private;
506 struct seq_file *seq;
508 private = kzalloc(psize, GFP_KERNEL);
509 if (private == NULL)
510 goto out;
512 rc = seq_open(f, ops);
513 if (rc < 0)
514 goto out_free;
516 seq = f->private_data;
517 seq->private = private;
518 return private;
520 out_free:
521 kfree(private);
522 out:
523 return NULL;
525 EXPORT_SYMBOL(__seq_open_private);
527 int seq_open_private(struct file *filp, const struct seq_operations *ops,
528 int psize)
530 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
532 EXPORT_SYMBOL(seq_open_private);
534 int seq_putc(struct seq_file *m, char c)
536 if (m->count < m->size) {
537 m->buf[m->count++] = c;
538 return 0;
540 return -1;
542 EXPORT_SYMBOL(seq_putc);
544 int seq_puts(struct seq_file *m, const char *s)
546 int len = strlen(s);
547 if (m->count + len < m->size) {
548 memcpy(m->buf + m->count, s, len);
549 m->count += len;
550 return 0;
552 m->count = m->size;
553 return -1;
555 EXPORT_SYMBOL(seq_puts);
557 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
559 struct list_head *lh;
561 list_for_each(lh, head)
562 if (pos-- == 0)
563 return lh;
565 return NULL;
568 EXPORT_SYMBOL(seq_list_start);
570 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
572 if (!pos)
573 return head;
575 return seq_list_start(head, pos - 1);
578 EXPORT_SYMBOL(seq_list_start_head);
580 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
582 struct list_head *lh;
584 lh = ((struct list_head *)v)->next;
585 ++*ppos;
586 return lh == head ? NULL : lh;
589 EXPORT_SYMBOL(seq_list_next);