[PATCH] DVB: Update documentation and credits
[linux-2.6/history.git] / fs / seq_file.c
blob678c2725a38a90ecc5c24e3778752d2bf4b8e801
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.
29 int seq_open(struct file *file, struct seq_operations *op)
31 struct seq_file *p = kmalloc(sizeof(*p), GFP_KERNEL);
32 if (!p)
33 return -ENOMEM;
34 memset(p, 0, sizeof(*p));
35 sema_init(&p->sem, 1);
36 p->op = op;
37 file->private_data = p;
38 return 0;
41 EXPORT_SYMBOL(seq_open);
43 /**
44 * seq_read - ->read() method for sequential files.
45 * @file, @buf, @size, @ppos: see file_operations method
47 * Ready-made ->f_op->read()
49 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
51 struct seq_file *m = (struct seq_file *)file->private_data;
52 size_t copied = 0;
53 loff_t pos;
54 size_t n;
55 void *p;
56 int err = 0;
58 if (ppos != &file->f_pos)
59 return -EPIPE;
61 down(&m->sem);
62 /* grab buffer if we didn't have one */
63 if (!m->buf) {
64 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
65 if (!m->buf)
66 goto Enomem;
68 /* if not empty - flush it first */
69 if (m->count) {
70 n = min(m->count, size);
71 err = copy_to_user(buf, m->buf + m->from, n);
72 if (err)
73 goto Efault;
74 m->count -= n;
75 m->from += n;
76 size -= n;
77 buf += n;
78 copied += n;
79 if (!m->count)
80 m->index++;
81 if (!size)
82 goto Done;
84 /* we need at least one record in buffer */
85 while (1) {
86 pos = m->index;
87 p = m->op->start(m, &pos);
88 err = PTR_ERR(p);
89 if (!p || IS_ERR(p))
90 break;
91 err = m->op->show(m, p);
92 if (err)
93 break;
94 if (m->count < m->size)
95 goto Fill;
96 m->op->stop(m, p);
97 kfree(m->buf);
98 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
99 if (!m->buf)
100 goto Enomem;
101 m->count = 0;
103 m->op->stop(m, p);
104 m->count = 0;
105 goto Done;
106 Fill:
107 /* they want more? let's try to get some more */
108 while (m->count < size) {
109 size_t offs = m->count;
110 loff_t next = pos;
111 p = m->op->next(m, p, &next);
112 if (!p || IS_ERR(p)) {
113 err = PTR_ERR(p);
114 break;
116 err = m->op->show(m, p);
117 if (err || m->count == m->size) {
118 m->count = offs;
119 break;
121 pos = next;
123 m->op->stop(m, p);
124 n = min(m->count, size);
125 err = copy_to_user(buf, m->buf, n);
126 if (err)
127 goto Efault;
128 copied += n;
129 m->count -= n;
130 if (m->count)
131 m->from = n;
132 else
133 pos++;
134 m->index = pos;
135 Done:
136 if (!copied)
137 copied = err;
138 else
139 *ppos += copied;
140 up(&m->sem);
141 return copied;
142 Enomem:
143 err = -ENOMEM;
144 goto Done;
145 Efault:
146 err = -EFAULT;
147 goto Done;
150 EXPORT_SYMBOL(seq_read);
152 static int traverse(struct seq_file *m, loff_t offset)
154 loff_t pos = 0;
155 int error = 0;
156 void *p;
158 m->index = 0;
159 m->count = m->from = 0;
160 if (!offset)
161 return 0;
162 if (!m->buf) {
163 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
164 if (!m->buf)
165 return -ENOMEM;
167 p = m->op->start(m, &m->index);
168 while (p) {
169 error = PTR_ERR(p);
170 if (IS_ERR(p))
171 break;
172 error = m->op->show(m, p);
173 if (error)
174 break;
175 if (m->count == m->size)
176 goto Eoverflow;
177 if (pos + m->count > offset) {
178 m->from = offset - pos;
179 m->count -= m->from;
180 break;
182 pos += m->count;
183 m->count = 0;
184 if (pos == offset) {
185 m->index++;
186 break;
188 p = m->op->next(m, p, &m->index);
190 m->op->stop(m, p);
191 return error;
193 Eoverflow:
194 m->op->stop(m, p);
195 kfree(m->buf);
196 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
197 return !m->buf ? -ENOMEM : -EAGAIN;
201 * seq_lseek - ->llseek() method for sequential files.
202 * @file, @offset, @origin: see file_operations method
204 * Ready-made ->f_op->llseek()
206 loff_t seq_lseek(struct file *file, loff_t offset, int origin)
208 struct seq_file *m = (struct seq_file *)file->private_data;
209 long long retval = -EINVAL;
211 down(&m->sem);
212 switch (origin) {
213 case 1:
214 offset += file->f_pos;
215 case 0:
216 if (offset < 0)
217 break;
218 retval = offset;
219 if (offset != file->f_pos) {
220 while ((retval=traverse(m, offset)) == -EAGAIN)
222 if (retval) {
223 /* with extreme prejudice... */
224 file->f_pos = 0;
225 m->index = 0;
226 m->count = 0;
227 } else {
228 retval = file->f_pos = offset;
232 up(&m->sem);
233 return retval;
236 EXPORT_SYMBOL(seq_lseek);
239 * seq_release - free the structures associated with sequential file.
240 * @file: file in question
241 * @inode: file->f_dentry->d_inode
243 * Frees the structures associated with sequential file; can be used
244 * as ->f_op->release() if you don't have private data to destroy.
246 int seq_release(struct inode *inode, struct file *file)
248 struct seq_file *m = (struct seq_file *)file->private_data;
249 kfree(m->buf);
250 kfree(m);
251 return 0;
254 EXPORT_SYMBOL(seq_release);
257 * seq_escape - print string into buffer, escaping some characters
258 * @m: target buffer
259 * @s: string
260 * @esc: set of characters that need escaping
262 * Puts string into buffer, replacing each occurrence of character from
263 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
264 * case of overflow.
266 int seq_escape(struct seq_file *m, const char *s, const char *esc)
268 char *end = m->buf + m->size;
269 char *p;
270 char c;
272 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
273 if (!strchr(esc, c)) {
274 *p++ = c;
275 continue;
277 if (p + 3 < end) {
278 *p++ = '\\';
279 *p++ = '0' + ((c & 0300) >> 6);
280 *p++ = '0' + ((c & 070) >> 3);
281 *p++ = '0' + (c & 07);
282 continue;
284 m->count = m->size;
285 return -1;
287 m->count = p - m->buf;
288 return 0;
291 EXPORT_SYMBOL(seq_escape);
293 int seq_printf(struct seq_file *m, const char *f, ...)
295 va_list args;
296 int len;
298 if (m->count < m->size) {
299 va_start(args, f);
300 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
301 va_end(args);
302 if (m->count + len < m->size) {
303 m->count += len;
304 return 0;
307 m->count = m->size;
308 return -1;
311 EXPORT_SYMBOL(seq_printf);
313 int seq_path(struct seq_file *m,
314 struct vfsmount *mnt, struct dentry *dentry,
315 char *esc)
317 if (m->count < m->size) {
318 char *s = m->buf + m->count;
319 char *p = d_path(dentry, mnt, s, m->size - m->count);
320 if (!IS_ERR(p)) {
321 while (s <= p) {
322 char c = *p++;
323 if (!c) {
324 p = m->buf + m->count;
325 m->count = s - m->buf;
326 return s - p;
327 } else if (!strchr(esc, c)) {
328 *s++ = c;
329 } else if (s + 4 > p) {
330 break;
331 } else {
332 *s++ = '\\';
333 *s++ = '0' + ((c & 0300) >> 6);
334 *s++ = '0' + ((c & 070) >> 3);
335 *s++ = '0' + (c & 07);
340 m->count = m->size;
341 return -1;
344 EXPORT_SYMBOL(seq_path);
346 static void *single_start(struct seq_file *p, loff_t *pos)
348 return NULL + (*pos == 0);
351 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
353 ++*pos;
354 return NULL;
357 static void single_stop(struct seq_file *p, void *v)
361 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
362 void *data)
364 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
365 int res = -ENOMEM;
367 if (op) {
368 op->start = single_start;
369 op->next = single_next;
370 op->stop = single_stop;
371 op->show = show;
372 res = seq_open(file, op);
373 if (!res)
374 ((struct seq_file *)file->private_data)->private = data;
375 else
376 kfree(op);
378 return res;
381 EXPORT_SYMBOL(single_open);
383 int single_release(struct inode *inode, struct file *file)
385 struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
386 int res = seq_release(inode, file);
387 kfree(op);
388 return res;
391 EXPORT_SYMBOL(single_release);
393 int seq_release_private(struct inode *inode, struct file *file)
395 struct seq_file *seq = file->private_data;
397 kfree(seq->private);
398 seq->private = NULL;
399 return seq_release(inode, file);
402 EXPORT_SYMBOL(seq_release_private);