MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / seq_file.c
blobfb048199a0b8b01b7b0f8b25e14c067539099f19
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;
40 /* SEQ files support lseek, but not pread/pwrite */
41 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
42 return 0;
44 EXPORT_SYMBOL(seq_open);
46 /**
47 * seq_read - ->read() method for sequential files.
48 * @file, @buf, @size, @ppos: see file_operations method
50 * Ready-made ->f_op->read()
52 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
54 struct seq_file *m = (struct seq_file *)file->private_data;
55 size_t copied = 0;
56 loff_t pos;
57 size_t n;
58 void *p;
59 int err = 0;
62 down(&m->sem);
63 /* grab buffer if we didn't have one */
64 if (!m->buf) {
65 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
66 if (!m->buf)
67 goto Enomem;
69 /* if not empty - flush it first */
70 if (m->count) {
71 n = min(m->count, size);
72 err = copy_to_user(buf, m->buf + m->from, n);
73 if (err)
74 goto Efault;
75 m->count -= n;
76 m->from += n;
77 size -= n;
78 buf += n;
79 copied += n;
80 if (!m->count)
81 m->index++;
82 if (!size)
83 goto Done;
85 /* we need at least one record in buffer */
86 while (1) {
87 pos = m->index;
88 p = m->op->start(m, &pos);
89 err = PTR_ERR(p);
90 if (!p || IS_ERR(p))
91 break;
92 err = m->op->show(m, p);
93 if (err)
94 break;
95 if (m->count < m->size)
96 goto Fill;
97 m->op->stop(m, p);
98 kfree(m->buf);
99 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
100 if (!m->buf)
101 goto Enomem;
102 m->count = 0;
104 m->op->stop(m, p);
105 m->count = 0;
106 goto Done;
107 Fill:
108 /* they want more? let's try to get some more */
109 while (m->count < size) {
110 size_t offs = m->count;
111 loff_t next = pos;
112 p = m->op->next(m, p, &next);
113 if (!p || IS_ERR(p)) {
114 err = PTR_ERR(p);
115 break;
117 err = m->op->show(m, p);
118 if (err || m->count == m->size) {
119 m->count = offs;
120 break;
122 pos = next;
124 m->op->stop(m, p);
125 n = min(m->count, size);
126 err = copy_to_user(buf, m->buf, n);
127 if (err)
128 goto Efault;
129 copied += n;
130 m->count -= n;
131 if (m->count)
132 m->from = n;
133 else
134 pos++;
135 m->index = pos;
136 Done:
137 if (!copied)
138 copied = err;
139 else
140 *ppos += copied;
141 up(&m->sem);
142 return copied;
143 Enomem:
144 err = -ENOMEM;
145 goto Done;
146 Efault:
147 err = -EFAULT;
148 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;
235 EXPORT_SYMBOL(seq_lseek);
238 * seq_release - free the structures associated with sequential file.
239 * @file: file in question
240 * @inode: file->f_dentry->d_inode
242 * Frees the structures associated with sequential file; can be used
243 * as ->f_op->release() if you don't have private data to destroy.
245 int seq_release(struct inode *inode, struct file *file)
247 struct seq_file *m = (struct seq_file *)file->private_data;
248 kfree(m->buf);
249 kfree(m);
250 return 0;
252 EXPORT_SYMBOL(seq_release);
255 * seq_escape - print string into buffer, escaping some characters
256 * @m: target buffer
257 * @s: string
258 * @esc: set of characters that need escaping
260 * Puts string into buffer, replacing each occurrence of character from
261 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
262 * case of overflow.
264 int seq_escape(struct seq_file *m, const char *s, const char *esc)
266 char *end = m->buf + m->size;
267 char *p;
268 char c;
270 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
271 if (!strchr(esc, c)) {
272 *p++ = c;
273 continue;
275 if (p + 3 < end) {
276 *p++ = '\\';
277 *p++ = '0' + ((c & 0300) >> 6);
278 *p++ = '0' + ((c & 070) >> 3);
279 *p++ = '0' + (c & 07);
280 continue;
282 m->count = m->size;
283 return -1;
285 m->count = p - m->buf;
286 return 0;
288 EXPORT_SYMBOL(seq_escape);
290 int seq_printf(struct seq_file *m, const char *f, ...)
292 va_list args;
293 int len;
295 if (m->count < m->size) {
296 va_start(args, f);
297 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
298 va_end(args);
299 if (m->count + len < m->size) {
300 m->count += len;
301 return 0;
304 m->count = m->size;
305 return -1;
307 EXPORT_SYMBOL(seq_printf);
309 int seq_path(struct seq_file *m,
310 struct vfsmount *mnt, struct dentry *dentry,
311 char *esc)
313 if (m->count < m->size) {
314 char *s = m->buf + m->count;
315 char *p = d_path(dentry, mnt, s, m->size - m->count);
316 if (!IS_ERR(p)) {
317 while (s <= p) {
318 char c = *p++;
319 if (!c) {
320 p = m->buf + m->count;
321 m->count = s - m->buf;
322 return s - p;
323 } else if (!strchr(esc, c)) {
324 *s++ = c;
325 } else if (s + 4 > p) {
326 break;
327 } else {
328 *s++ = '\\';
329 *s++ = '0' + ((c & 0300) >> 6);
330 *s++ = '0' + ((c & 070) >> 3);
331 *s++ = '0' + (c & 07);
336 m->count = m->size;
337 return -1;
339 EXPORT_SYMBOL(seq_path);
341 static void *single_start(struct seq_file *p, loff_t *pos)
343 return NULL + (*pos == 0);
346 static void *single_next(struct seq_file *p, void *v, loff_t *pos)
348 ++*pos;
349 return NULL;
352 static void single_stop(struct seq_file *p, void *v)
356 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
357 void *data)
359 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
360 int res = -ENOMEM;
362 if (op) {
363 op->start = single_start;
364 op->next = single_next;
365 op->stop = single_stop;
366 op->show = show;
367 res = seq_open(file, op);
368 if (!res)
369 ((struct seq_file *)file->private_data)->private = data;
370 else
371 kfree(op);
373 return res;
375 EXPORT_SYMBOL(single_open);
377 int single_release(struct inode *inode, struct file *file)
379 struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
380 int res = seq_release(inode, file);
381 kfree(op);
382 return res;
384 EXPORT_SYMBOL(single_release);
386 int seq_release_private(struct inode *inode, struct file *file)
388 struct seq_file *seq = file->private_data;
390 kfree(seq->private);
391 seq->private = NULL;
392 return seq_release(inode, file);
394 EXPORT_SYMBOL(seq_release_private);
396 int seq_putc(struct seq_file *m, char c)
398 if (m->count < m->size) {
399 m->buf[m->count++] = c;
400 return 0;
402 return -1;
404 EXPORT_SYMBOL(seq_putc);
406 int seq_puts(struct seq_file *m, const char *s)
408 int len = strlen(s);
409 if (m->count + len < m->size) {
410 memcpy(m->buf + m->count, s, len);
411 m->count += len;
412 return 0;
414 m->count = m->size;
415 return -1;
417 EXPORT_SYMBOL(seq_puts);