]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/seq_file.c
vfs: remove redundant position check in do_sendfile
[net-next-2.6.git] / fs / seq_file.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/seq_file.c
3 *
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
7
8#include <linux/fs.h>
9#include <linux/module.h>
10#include <linux/seq_file.h>
11#include <linux/slab.h>
12
13#include <asm/uaccess.h>
14#include <asm/page.h>
15
16/**
17 * seq_open - initialize sequential file
18 * @file: file we initialize
19 * @op: method table describing the sequence
20 *
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.
521b5d0c 28 * Returning SEQ_SKIP means "discard this element and move on".
1da177e4 29 */
15ad7cdc 30int seq_open(struct file *file, const struct seq_operations *op)
1da177e4 31{
1abe77b0
AV
32 struct seq_file *p = file->private_data;
33
34 if (!p) {
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
36 if (!p)
37 return -ENOMEM;
38 file->private_data = p;
39 }
1da177e4 40 memset(p, 0, sizeof(*p));
0ac1759a 41 mutex_init(&p->lock);
1da177e4 42 p->op = op;
1da177e4
LT
43
44 /*
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.
48 */
49 file->f_version = 0;
50
8f19d472
EB
51 /*
52 * seq_files support lseek() and pread(). They do not implement
53 * write() at all, but we clear FMODE_PWRITE here for historical
54 * reasons.
55 *
56 * If a client of seq_files a) implements file.write() and b) wishes to
57 * support pwrite() then that client will need to implement its own
58 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
59 */
60 file->f_mode &= ~FMODE_PWRITE;
1da177e4
LT
61 return 0;
62}
63EXPORT_SYMBOL(seq_open);
64
33da8892
EB
65static int traverse(struct seq_file *m, loff_t offset)
66{
67 loff_t pos = 0, index;
68 int error = 0;
69 void *p;
70
71 m->version = 0;
72 index = 0;
73 m->count = m->from = 0;
74 if (!offset) {
75 m->index = index;
76 return 0;
77 }
78 if (!m->buf) {
79 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
80 if (!m->buf)
81 return -ENOMEM;
82 }
83 p = m->op->start(m, &index);
84 while (p) {
85 error = PTR_ERR(p);
86 if (IS_ERR(p))
87 break;
88 error = m->op->show(m, p);
89 if (error < 0)
90 break;
91 if (unlikely(error)) {
92 error = 0;
93 m->count = 0;
94 }
95 if (m->count == m->size)
96 goto Eoverflow;
97 if (pos + m->count > offset) {
98 m->from = offset - pos;
99 m->count -= m->from;
100 m->index = index;
101 break;
102 }
103 pos += m->count;
104 m->count = 0;
105 if (pos == offset) {
106 index++;
107 m->index = index;
108 break;
109 }
110 p = m->op->next(m, p, &index);
111 }
112 m->op->stop(m, p);
f01d1d54 113 m->index = index;
33da8892
EB
114 return error;
115
116Eoverflow:
117 m->op->stop(m, p);
118 kfree(m->buf);
119 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
120 return !m->buf ? -ENOMEM : -EAGAIN;
121}
122
1da177e4
LT
123/**
124 * seq_read - ->read() method for sequential files.
67be2dd1
MW
125 * @file: the file to read from
126 * @buf: the buffer to read to
127 * @size: the maximum number of bytes to read
128 * @ppos: the current position in the file
1da177e4
LT
129 *
130 * Ready-made ->f_op->read()
131 */
132ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
133{
134 struct seq_file *m = (struct seq_file *)file->private_data;
135 size_t copied = 0;
136 loff_t pos;
137 size_t n;
138 void *p;
139 int err = 0;
140
0ac1759a 141 mutex_lock(&m->lock);
8f19d472
EB
142
143 /* Don't assume *ppos is where we left it */
144 if (unlikely(*ppos != m->read_pos)) {
145 m->read_pos = *ppos;
146 while ((err = traverse(m, *ppos)) == -EAGAIN)
147 ;
148 if (err) {
149 /* With prejudice... */
150 m->read_pos = 0;
151 m->version = 0;
152 m->index = 0;
153 m->count = 0;
154 goto Done;
155 }
156 }
157
1da177e4
LT
158 /*
159 * seq_file->op->..m_start/m_stop/m_next may do special actions
160 * or optimisations based on the file->f_version, so we want to
161 * pass the file->f_version to those methods.
162 *
163 * seq_file->version is just copy of f_version, and seq_file
164 * methods can treat it simply as file version.
165 * It is copied in first and copied out after all operations.
166 * It is convenient to have it as part of structure to avoid the
167 * need of passing another argument to all the seq_file methods.
168 */
169 m->version = file->f_version;
170 /* grab buffer if we didn't have one */
171 if (!m->buf) {
172 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
173 if (!m->buf)
174 goto Enomem;
175 }
176 /* if not empty - flush it first */
177 if (m->count) {
178 n = min(m->count, size);
179 err = copy_to_user(buf, m->buf + m->from, n);
180 if (err)
181 goto Efault;
182 m->count -= n;
183 m->from += n;
184 size -= n;
185 buf += n;
186 copied += n;
187 if (!m->count)
188 m->index++;
189 if (!size)
190 goto Done;
191 }
192 /* we need at least one record in buffer */
4cdfe84b
AV
193 pos = m->index;
194 p = m->op->start(m, &pos);
1da177e4 195 while (1) {
1da177e4
LT
196 err = PTR_ERR(p);
197 if (!p || IS_ERR(p))
198 break;
199 err = m->op->show(m, p);
521b5d0c 200 if (err < 0)
1da177e4 201 break;
521b5d0c
AV
202 if (unlikely(err))
203 m->count = 0;
4cdfe84b
AV
204 if (unlikely(!m->count)) {
205 p = m->op->next(m, p, &pos);
206 m->index = pos;
207 continue;
208 }
1da177e4
LT
209 if (m->count < m->size)
210 goto Fill;
211 m->op->stop(m, p);
212 kfree(m->buf);
213 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
214 if (!m->buf)
215 goto Enomem;
216 m->count = 0;
217 m->version = 0;
4cdfe84b
AV
218 pos = m->index;
219 p = m->op->start(m, &pos);
1da177e4
LT
220 }
221 m->op->stop(m, p);
222 m->count = 0;
223 goto Done;
224Fill:
225 /* they want more? let's try to get some more */
226 while (m->count < size) {
227 size_t offs = m->count;
228 loff_t next = pos;
229 p = m->op->next(m, p, &next);
230 if (!p || IS_ERR(p)) {
231 err = PTR_ERR(p);
232 break;
233 }
234 err = m->op->show(m, p);
521b5d0c 235 if (m->count == m->size || err) {
1da177e4 236 m->count = offs;
521b5d0c
AV
237 if (likely(err <= 0))
238 break;
1da177e4
LT
239 }
240 pos = next;
241 }
242 m->op->stop(m, p);
243 n = min(m->count, size);
244 err = copy_to_user(buf, m->buf, n);
245 if (err)
246 goto Efault;
247 copied += n;
248 m->count -= n;
249 if (m->count)
250 m->from = n;
251 else
252 pos++;
253 m->index = pos;
254Done:
255 if (!copied)
256 copied = err;
8f19d472 257 else {
1da177e4 258 *ppos += copied;
8f19d472
EB
259 m->read_pos += copied;
260 }
1da177e4 261 file->f_version = m->version;
0ac1759a 262 mutex_unlock(&m->lock);
1da177e4
LT
263 return copied;
264Enomem:
265 err = -ENOMEM;
266 goto Done;
267Efault:
268 err = -EFAULT;
269 goto Done;
270}
271EXPORT_SYMBOL(seq_read);
272
1da177e4
LT
273/**
274 * seq_lseek - ->llseek() method for sequential files.
67be2dd1
MW
275 * @file: the file in question
276 * @offset: new position
277 * @origin: 0 for absolute, 1 for relative position
1da177e4
LT
278 *
279 * Ready-made ->f_op->llseek()
280 */
281loff_t seq_lseek(struct file *file, loff_t offset, int origin)
282{
283 struct seq_file *m = (struct seq_file *)file->private_data;
16abef0e 284 loff_t retval = -EINVAL;
1da177e4 285
0ac1759a 286 mutex_lock(&m->lock);
1da177e4
LT
287 m->version = file->f_version;
288 switch (origin) {
289 case 1:
290 offset += file->f_pos;
291 case 0:
292 if (offset < 0)
293 break;
294 retval = offset;
8f19d472 295 if (offset != m->read_pos) {
1da177e4
LT
296 while ((retval=traverse(m, offset)) == -EAGAIN)
297 ;
298 if (retval) {
299 /* with extreme prejudice... */
300 file->f_pos = 0;
8f19d472 301 m->read_pos = 0;
1da177e4
LT
302 m->version = 0;
303 m->index = 0;
304 m->count = 0;
305 } else {
8f19d472 306 m->read_pos = offset;
1da177e4
LT
307 retval = file->f_pos = offset;
308 }
309 }
310 }
1da177e4 311 file->f_version = m->version;
00c5746d 312 mutex_unlock(&m->lock);
1da177e4
LT
313 return retval;
314}
315EXPORT_SYMBOL(seq_lseek);
316
317/**
318 * seq_release - free the structures associated with sequential file.
319 * @file: file in question
0f7fc9e4 320 * @inode: file->f_path.dentry->d_inode
1da177e4
LT
321 *
322 * Frees the structures associated with sequential file; can be used
323 * as ->f_op->release() if you don't have private data to destroy.
324 */
325int seq_release(struct inode *inode, struct file *file)
326{
327 struct seq_file *m = (struct seq_file *)file->private_data;
328 kfree(m->buf);
329 kfree(m);
330 return 0;
331}
332EXPORT_SYMBOL(seq_release);
333
334/**
335 * seq_escape - print string into buffer, escaping some characters
336 * @m: target buffer
337 * @s: string
338 * @esc: set of characters that need escaping
339 *
340 * Puts string into buffer, replacing each occurrence of character from
341 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
342 * case of overflow.
343 */
344int seq_escape(struct seq_file *m, const char *s, const char *esc)
345{
346 char *end = m->buf + m->size;
347 char *p;
348 char c;
349
350 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
351 if (!strchr(esc, c)) {
352 *p++ = c;
353 continue;
354 }
355 if (p + 3 < end) {
356 *p++ = '\\';
357 *p++ = '0' + ((c & 0300) >> 6);
358 *p++ = '0' + ((c & 070) >> 3);
359 *p++ = '0' + (c & 07);
360 continue;
361 }
362 m->count = m->size;
363 return -1;
364 }
365 m->count = p - m->buf;
366 return 0;
367}
368EXPORT_SYMBOL(seq_escape);
369
370int seq_printf(struct seq_file *m, const char *f, ...)
371{
372 va_list args;
373 int len;
374
375 if (m->count < m->size) {
376 va_start(args, f);
377 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
378 va_end(args);
379 if (m->count + len < m->size) {
380 m->count += len;
381 return 0;
382 }
383 }
384 m->count = m->size;
385 return -1;
386}
387EXPORT_SYMBOL(seq_printf);
388
74e2f334 389/**
958086d1
TE
390 * mangle_path - mangle and copy path to buffer beginning
391 * @s: buffer start
392 * @p: beginning of path in above buffer
393 * @esc: set of characters that need escaping
74e2f334
TE
394 *
395 * Copy the path from @p to @s, replacing each occurrence of character from
396 * @esc with usual octal escape.
397 * Returns pointer past last written character in @s, or NULL in case of
398 * failure.
399 */
400char *mangle_path(char *s, char *p, char *esc)
6092d048
RP
401{
402 while (s <= p) {
403 char c = *p++;
404 if (!c) {
405 return s;
406 } else if (!strchr(esc, c)) {
407 *s++ = c;
408 } else if (s + 4 > p) {
409 break;
410 } else {
411 *s++ = '\\';
412 *s++ = '0' + ((c & 0300) >> 6);
413 *s++ = '0' + ((c & 070) >> 3);
414 *s++ = '0' + (c & 07);
415 }
416 }
417 return NULL;
418}
604094f4 419EXPORT_SYMBOL(mangle_path);
6092d048 420
52afeefb
AV
421/**
422 * seq_path - seq_file interface to print a pathname
423 * @m: the seq_file handle
424 * @path: the struct path to print
425 * @esc: set of characters to escape in the output
426 *
427 * return the absolute path of 'path', as represented by the
428 * dentry / mnt pair in the path parameter.
6092d048 429 */
c32c2f63 430int seq_path(struct seq_file *m, struct path *path, char *esc)
1da177e4
LT
431{
432 if (m->count < m->size) {
433 char *s = m->buf + m->count;
cf28b486 434 char *p = d_path(path, s, m->size - m->count);
1da177e4 435 if (!IS_ERR(p)) {
6092d048
RP
436 s = mangle_path(s, p, esc);
437 if (s) {
438 p = m->buf + m->count;
439 m->count = s - m->buf;
440 return s - p;
1da177e4
LT
441 }
442 }
443 }
444 m->count = m->size;
445 return -1;
446}
447EXPORT_SYMBOL(seq_path);
448
9d1bc601
MS
449/*
450 * Same as seq_path, but relative to supplied root.
451 *
452 * root may be changed, see __d_path().
453 */
454int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
455 char *esc)
456{
457 int err = -ENAMETOOLONG;
458 if (m->count < m->size) {
459 char *s = m->buf + m->count;
460 char *p;
461
462 spin_lock(&dcache_lock);
463 p = __d_path(path, root, s, m->size - m->count);
464 spin_unlock(&dcache_lock);
465 err = PTR_ERR(p);
466 if (!IS_ERR(p)) {
467 s = mangle_path(s, p, esc);
468 if (s) {
469 p = m->buf + m->count;
470 m->count = s - m->buf;
471 return 0;
472 }
7a62cc10 473 err = -ENAMETOOLONG;
9d1bc601
MS
474 }
475 }
476 m->count = m->size;
477 return err;
478}
479
6092d048
RP
480/*
481 * returns the path of the 'dentry' from the root of its filesystem.
482 */
483int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
484{
485 if (m->count < m->size) {
486 char *s = m->buf + m->count;
487 char *p = dentry_path(dentry, s, m->size - m->count);
488 if (!IS_ERR(p)) {
489 s = mangle_path(s, p, esc);
490 if (s) {
491 p = m->buf + m->count;
492 m->count = s - m->buf;
493 return s - p;
494 }
495 }
496 }
497 m->count = m->size;
498 return -1;
499}
500
cb78a0ce
RR
501int seq_bitmap(struct seq_file *m, const unsigned long *bits,
502 unsigned int nr_bits)
50ac2d69 503{
85dd030e
LJ
504 if (m->count < m->size) {
505 int len = bitmap_scnprintf(m->buf + m->count,
506 m->size - m->count, bits, nr_bits);
507 if (m->count + len < m->size) {
508 m->count += len;
509 return 0;
510 }
50ac2d69
AD
511 }
512 m->count = m->size;
513 return -1;
514}
85dd030e 515EXPORT_SYMBOL(seq_bitmap);
50ac2d69 516
af76aba0 517int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
3eda2011
LJ
518 unsigned int nr_bits)
519{
520 if (m->count < m->size) {
521 int len = bitmap_scnlistprintf(m->buf + m->count,
522 m->size - m->count, bits, nr_bits);
523 if (m->count + len < m->size) {
524 m->count += len;
525 return 0;
526 }
527 }
528 m->count = m->size;
529 return -1;
530}
531EXPORT_SYMBOL(seq_bitmap_list);
532
1da177e4
LT
533static void *single_start(struct seq_file *p, loff_t *pos)
534{
535 return NULL + (*pos == 0);
536}
537
538static void *single_next(struct seq_file *p, void *v, loff_t *pos)
539{
540 ++*pos;
541 return NULL;
542}
543
544static void single_stop(struct seq_file *p, void *v)
545{
546}
547
548int single_open(struct file *file, int (*show)(struct seq_file *, void *),
549 void *data)
550{
551 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
552 int res = -ENOMEM;
553
554 if (op) {
555 op->start = single_start;
556 op->next = single_next;
557 op->stop = single_stop;
558 op->show = show;
559 res = seq_open(file, op);
560 if (!res)
561 ((struct seq_file *)file->private_data)->private = data;
562 else
563 kfree(op);
564 }
565 return res;
566}
567EXPORT_SYMBOL(single_open);
568
569int single_release(struct inode *inode, struct file *file)
570{
15ad7cdc 571 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
1da177e4
LT
572 int res = seq_release(inode, file);
573 kfree(op);
574 return res;
575}
576EXPORT_SYMBOL(single_release);
577
578int seq_release_private(struct inode *inode, struct file *file)
579{
580 struct seq_file *seq = file->private_data;
581
582 kfree(seq->private);
583 seq->private = NULL;
584 return seq_release(inode, file);
585}
586EXPORT_SYMBOL(seq_release_private);
587
39699037
PE
588void *__seq_open_private(struct file *f, const struct seq_operations *ops,
589 int psize)
590{
591 int rc;
592 void *private;
593 struct seq_file *seq;
594
595 private = kzalloc(psize, GFP_KERNEL);
596 if (private == NULL)
597 goto out;
598
599 rc = seq_open(f, ops);
600 if (rc < 0)
601 goto out_free;
602
603 seq = f->private_data;
604 seq->private = private;
605 return private;
606
607out_free:
608 kfree(private);
609out:
610 return NULL;
611}
612EXPORT_SYMBOL(__seq_open_private);
613
614int seq_open_private(struct file *filp, const struct seq_operations *ops,
615 int psize)
616{
617 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
618}
619EXPORT_SYMBOL(seq_open_private);
620
1da177e4
LT
621int seq_putc(struct seq_file *m, char c)
622{
623 if (m->count < m->size) {
624 m->buf[m->count++] = c;
625 return 0;
626 }
627 return -1;
628}
629EXPORT_SYMBOL(seq_putc);
630
631int seq_puts(struct seq_file *m, const char *s)
632{
633 int len = strlen(s);
634 if (m->count + len < m->size) {
635 memcpy(m->buf + m->count, s, len);
636 m->count += len;
637 return 0;
638 }
639 m->count = m->size;
640 return -1;
641}
642EXPORT_SYMBOL(seq_puts);
bcf67e16 643
0b923606
PO
644/**
645 * seq_write - write arbitrary data to buffer
646 * @seq: seq_file identifying the buffer to which data should be written
647 * @data: data address
648 * @len: number of bytes
649 *
650 * Return 0 on success, non-zero otherwise.
651 */
652int seq_write(struct seq_file *seq, const void *data, size_t len)
653{
654 if (seq->count + len < seq->size) {
655 memcpy(seq->buf + seq->count, data, len);
656 seq->count += len;
657 return 0;
658 }
659 seq->count = seq->size;
660 return -1;
661}
662EXPORT_SYMBOL(seq_write);
663
bcf67e16
PE
664struct list_head *seq_list_start(struct list_head *head, loff_t pos)
665{
666 struct list_head *lh;
667
668 list_for_each(lh, head)
669 if (pos-- == 0)
670 return lh;
671
672 return NULL;
673}
674
675EXPORT_SYMBOL(seq_list_start);
676
677struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
678{
679 if (!pos)
680 return head;
681
682 return seq_list_start(head, pos - 1);
683}
684
685EXPORT_SYMBOL(seq_list_start_head);
686
687struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
688{
689 struct list_head *lh;
690
691 lh = ((struct list_head *)v)->next;
692 ++*ppos;
693 return lh == head ? NULL : lh;
694}
695
696EXPORT_SYMBOL(seq_list_next);