]>
Commit | Line | Data |
---|---|---|
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 | 30 | int 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 | } | |
63 | EXPORT_SYMBOL(seq_open); | |
64 | ||
33da8892 EB |
65 | static 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 | ||
116 | Eoverflow: | |
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 | */ | |
132 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) | |
133 | { | |
8209e2f4 | 134 | struct seq_file *m = file->private_data; |
1da177e4 LT |
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 | 142 | |
7904ac84 EC |
143 | /* |
144 | * seq_file->op->..m_start/m_stop/m_next may do special actions | |
145 | * or optimisations based on the file->f_version, so we want to | |
146 | * pass the file->f_version to those methods. | |
147 | * | |
148 | * seq_file->version is just copy of f_version, and seq_file | |
149 | * methods can treat it simply as file version. | |
150 | * It is copied in first and copied out after all operations. | |
151 | * It is convenient to have it as part of structure to avoid the | |
152 | * need of passing another argument to all the seq_file methods. | |
153 | */ | |
154 | m->version = file->f_version; | |
155 | ||
8f19d472 EB |
156 | /* Don't assume *ppos is where we left it */ |
157 | if (unlikely(*ppos != m->read_pos)) { | |
8f19d472 EB |
158 | while ((err = traverse(m, *ppos)) == -EAGAIN) |
159 | ; | |
160 | if (err) { | |
161 | /* With prejudice... */ | |
162 | m->read_pos = 0; | |
163 | m->version = 0; | |
164 | m->index = 0; | |
165 | m->count = 0; | |
166 | goto Done; | |
7904ac84 EC |
167 | } else { |
168 | m->read_pos = *ppos; | |
8f19d472 EB |
169 | } |
170 | } | |
171 | ||
1da177e4 LT |
172 | /* grab buffer if we didn't have one */ |
173 | if (!m->buf) { | |
174 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); | |
175 | if (!m->buf) | |
176 | goto Enomem; | |
177 | } | |
178 | /* if not empty - flush it first */ | |
179 | if (m->count) { | |
180 | n = min(m->count, size); | |
181 | err = copy_to_user(buf, m->buf + m->from, n); | |
182 | if (err) | |
183 | goto Efault; | |
184 | m->count -= n; | |
185 | m->from += n; | |
186 | size -= n; | |
187 | buf += n; | |
188 | copied += n; | |
189 | if (!m->count) | |
190 | m->index++; | |
191 | if (!size) | |
192 | goto Done; | |
193 | } | |
194 | /* we need at least one record in buffer */ | |
4cdfe84b AV |
195 | pos = m->index; |
196 | p = m->op->start(m, &pos); | |
1da177e4 | 197 | while (1) { |
1da177e4 LT |
198 | err = PTR_ERR(p); |
199 | if (!p || IS_ERR(p)) | |
200 | break; | |
201 | err = m->op->show(m, p); | |
521b5d0c | 202 | if (err < 0) |
1da177e4 | 203 | break; |
521b5d0c AV |
204 | if (unlikely(err)) |
205 | m->count = 0; | |
4cdfe84b AV |
206 | if (unlikely(!m->count)) { |
207 | p = m->op->next(m, p, &pos); | |
208 | m->index = pos; | |
209 | continue; | |
210 | } | |
1da177e4 LT |
211 | if (m->count < m->size) |
212 | goto Fill; | |
213 | m->op->stop(m, p); | |
214 | kfree(m->buf); | |
215 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); | |
216 | if (!m->buf) | |
217 | goto Enomem; | |
218 | m->count = 0; | |
219 | m->version = 0; | |
4cdfe84b AV |
220 | pos = m->index; |
221 | p = m->op->start(m, &pos); | |
1da177e4 LT |
222 | } |
223 | m->op->stop(m, p); | |
224 | m->count = 0; | |
225 | goto Done; | |
226 | Fill: | |
227 | /* they want more? let's try to get some more */ | |
228 | while (m->count < size) { | |
229 | size_t offs = m->count; | |
230 | loff_t next = pos; | |
231 | p = m->op->next(m, p, &next); | |
232 | if (!p || IS_ERR(p)) { | |
233 | err = PTR_ERR(p); | |
234 | break; | |
235 | } | |
236 | err = m->op->show(m, p); | |
521b5d0c | 237 | if (m->count == m->size || err) { |
1da177e4 | 238 | m->count = offs; |
521b5d0c AV |
239 | if (likely(err <= 0)) |
240 | break; | |
1da177e4 LT |
241 | } |
242 | pos = next; | |
243 | } | |
244 | m->op->stop(m, p); | |
245 | n = min(m->count, size); | |
246 | err = copy_to_user(buf, m->buf, n); | |
247 | if (err) | |
248 | goto Efault; | |
249 | copied += n; | |
250 | m->count -= n; | |
251 | if (m->count) | |
252 | m->from = n; | |
253 | else | |
254 | pos++; | |
255 | m->index = pos; | |
256 | Done: | |
257 | if (!copied) | |
258 | copied = err; | |
8f19d472 | 259 | else { |
1da177e4 | 260 | *ppos += copied; |
8f19d472 EB |
261 | m->read_pos += copied; |
262 | } | |
1da177e4 | 263 | file->f_version = m->version; |
0ac1759a | 264 | mutex_unlock(&m->lock); |
1da177e4 LT |
265 | return copied; |
266 | Enomem: | |
267 | err = -ENOMEM; | |
268 | goto Done; | |
269 | Efault: | |
270 | err = -EFAULT; | |
271 | goto Done; | |
272 | } | |
273 | EXPORT_SYMBOL(seq_read); | |
274 | ||
1da177e4 LT |
275 | /** |
276 | * seq_lseek - ->llseek() method for sequential files. | |
67be2dd1 MW |
277 | * @file: the file in question |
278 | * @offset: new position | |
279 | * @origin: 0 for absolute, 1 for relative position | |
1da177e4 LT |
280 | * |
281 | * Ready-made ->f_op->llseek() | |
282 | */ | |
283 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) | |
284 | { | |
8209e2f4 | 285 | struct seq_file *m = file->private_data; |
16abef0e | 286 | loff_t retval = -EINVAL; |
1da177e4 | 287 | |
0ac1759a | 288 | mutex_lock(&m->lock); |
1da177e4 LT |
289 | m->version = file->f_version; |
290 | switch (origin) { | |
291 | case 1: | |
292 | offset += file->f_pos; | |
293 | case 0: | |
294 | if (offset < 0) | |
295 | break; | |
296 | retval = offset; | |
8f19d472 | 297 | if (offset != m->read_pos) { |
1da177e4 LT |
298 | while ((retval=traverse(m, offset)) == -EAGAIN) |
299 | ; | |
300 | if (retval) { | |
301 | /* with extreme prejudice... */ | |
302 | file->f_pos = 0; | |
8f19d472 | 303 | m->read_pos = 0; |
1da177e4 LT |
304 | m->version = 0; |
305 | m->index = 0; | |
306 | m->count = 0; | |
307 | } else { | |
8f19d472 | 308 | m->read_pos = offset; |
1da177e4 LT |
309 | retval = file->f_pos = offset; |
310 | } | |
311 | } | |
312 | } | |
1da177e4 | 313 | file->f_version = m->version; |
00c5746d | 314 | mutex_unlock(&m->lock); |
1da177e4 LT |
315 | return retval; |
316 | } | |
317 | EXPORT_SYMBOL(seq_lseek); | |
318 | ||
319 | /** | |
320 | * seq_release - free the structures associated with sequential file. | |
321 | * @file: file in question | |
0f7fc9e4 | 322 | * @inode: file->f_path.dentry->d_inode |
1da177e4 LT |
323 | * |
324 | * Frees the structures associated with sequential file; can be used | |
325 | * as ->f_op->release() if you don't have private data to destroy. | |
326 | */ | |
327 | int seq_release(struct inode *inode, struct file *file) | |
328 | { | |
8209e2f4 | 329 | struct seq_file *m = file->private_data; |
1da177e4 LT |
330 | kfree(m->buf); |
331 | kfree(m); | |
332 | return 0; | |
333 | } | |
334 | EXPORT_SYMBOL(seq_release); | |
335 | ||
336 | /** | |
337 | * seq_escape - print string into buffer, escaping some characters | |
338 | * @m: target buffer | |
339 | * @s: string | |
340 | * @esc: set of characters that need escaping | |
341 | * | |
342 | * Puts string into buffer, replacing each occurrence of character from | |
343 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in | |
344 | * case of overflow. | |
345 | */ | |
346 | int seq_escape(struct seq_file *m, const char *s, const char *esc) | |
347 | { | |
348 | char *end = m->buf + m->size; | |
349 | char *p; | |
350 | char c; | |
351 | ||
352 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { | |
353 | if (!strchr(esc, c)) { | |
354 | *p++ = c; | |
355 | continue; | |
356 | } | |
357 | if (p + 3 < end) { | |
358 | *p++ = '\\'; | |
359 | *p++ = '0' + ((c & 0300) >> 6); | |
360 | *p++ = '0' + ((c & 070) >> 3); | |
361 | *p++ = '0' + (c & 07); | |
362 | continue; | |
363 | } | |
364 | m->count = m->size; | |
365 | return -1; | |
366 | } | |
367 | m->count = p - m->buf; | |
368 | return 0; | |
369 | } | |
370 | EXPORT_SYMBOL(seq_escape); | |
371 | ||
372 | int seq_printf(struct seq_file *m, const char *f, ...) | |
373 | { | |
374 | va_list args; | |
375 | int len; | |
376 | ||
377 | if (m->count < m->size) { | |
378 | va_start(args, f); | |
379 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); | |
380 | va_end(args); | |
381 | if (m->count + len < m->size) { | |
382 | m->count += len; | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | m->count = m->size; | |
387 | return -1; | |
388 | } | |
389 | EXPORT_SYMBOL(seq_printf); | |
390 | ||
74e2f334 | 391 | /** |
958086d1 TE |
392 | * mangle_path - mangle and copy path to buffer beginning |
393 | * @s: buffer start | |
394 | * @p: beginning of path in above buffer | |
395 | * @esc: set of characters that need escaping | |
74e2f334 TE |
396 | * |
397 | * Copy the path from @p to @s, replacing each occurrence of character from | |
398 | * @esc with usual octal escape. | |
399 | * Returns pointer past last written character in @s, or NULL in case of | |
400 | * failure. | |
401 | */ | |
8c9379e9 | 402 | char *mangle_path(char *s, const char *p, const char *esc) |
6092d048 RP |
403 | { |
404 | while (s <= p) { | |
405 | char c = *p++; | |
406 | if (!c) { | |
407 | return s; | |
408 | } else if (!strchr(esc, c)) { | |
409 | *s++ = c; | |
410 | } else if (s + 4 > p) { | |
411 | break; | |
412 | } else { | |
413 | *s++ = '\\'; | |
414 | *s++ = '0' + ((c & 0300) >> 6); | |
415 | *s++ = '0' + ((c & 070) >> 3); | |
416 | *s++ = '0' + (c & 07); | |
417 | } | |
418 | } | |
419 | return NULL; | |
420 | } | |
604094f4 | 421 | EXPORT_SYMBOL(mangle_path); |
6092d048 | 422 | |
52afeefb AV |
423 | /** |
424 | * seq_path - seq_file interface to print a pathname | |
425 | * @m: the seq_file handle | |
426 | * @path: the struct path to print | |
427 | * @esc: set of characters to escape in the output | |
428 | * | |
429 | * return the absolute path of 'path', as represented by the | |
430 | * dentry / mnt pair in the path parameter. | |
6092d048 | 431 | */ |
8c9379e9 | 432 | int seq_path(struct seq_file *m, const struct path *path, const char *esc) |
1da177e4 | 433 | { |
f8439806 MS |
434 | char *buf; |
435 | size_t size = seq_get_buf(m, &buf); | |
436 | int res = -1; | |
437 | ||
438 | if (size) { | |
439 | char *p = d_path(path, buf, size); | |
1da177e4 | 440 | if (!IS_ERR(p)) { |
f8439806 MS |
441 | char *end = mangle_path(buf, p, esc); |
442 | if (end) | |
443 | res = end - buf; | |
1da177e4 LT |
444 | } |
445 | } | |
f8439806 MS |
446 | seq_commit(m, res); |
447 | ||
448 | return res; | |
1da177e4 LT |
449 | } |
450 | EXPORT_SYMBOL(seq_path); | |
451 | ||
9d1bc601 MS |
452 | /* |
453 | * Same as seq_path, but relative to supplied root. | |
9d1bc601 | 454 | */ |
8c9379e9 AV |
455 | int seq_path_root(struct seq_file *m, const struct path *path, |
456 | const struct path *root, const char *esc) | |
9d1bc601 | 457 | { |
f8439806 MS |
458 | char *buf; |
459 | size_t size = seq_get_buf(m, &buf); | |
460 | int res = -ENAMETOOLONG; | |
461 | ||
462 | if (size) { | |
9d1bc601 MS |
463 | char *p; |
464 | ||
f8439806 | 465 | p = __d_path(path, root, buf, size); |
02125a82 AV |
466 | if (!p) |
467 | return SEQ_SKIP; | |
f8439806 | 468 | res = PTR_ERR(p); |
9d1bc601 | 469 | if (!IS_ERR(p)) { |
f8439806 MS |
470 | char *end = mangle_path(buf, p, esc); |
471 | if (end) | |
472 | res = end - buf; | |
473 | else | |
474 | res = -ENAMETOOLONG; | |
9d1bc601 MS |
475 | } |
476 | } | |
f8439806 MS |
477 | seq_commit(m, res); |
478 | ||
02125a82 | 479 | return res < 0 && res != -ENAMETOOLONG ? res : 0; |
9d1bc601 MS |
480 | } |
481 | ||
6092d048 RP |
482 | /* |
483 | * returns the path of the 'dentry' from the root of its filesystem. | |
484 | */ | |
8c9379e9 | 485 | int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) |
6092d048 | 486 | { |
f8439806 MS |
487 | char *buf; |
488 | size_t size = seq_get_buf(m, &buf); | |
489 | int res = -1; | |
490 | ||
491 | if (size) { | |
492 | char *p = dentry_path(dentry, buf, size); | |
6092d048 | 493 | if (!IS_ERR(p)) { |
f8439806 MS |
494 | char *end = mangle_path(buf, p, esc); |
495 | if (end) | |
496 | res = end - buf; | |
6092d048 RP |
497 | } |
498 | } | |
f8439806 MS |
499 | seq_commit(m, res); |
500 | ||
501 | return res; | |
6092d048 RP |
502 | } |
503 | ||
cb78a0ce RR |
504 | int seq_bitmap(struct seq_file *m, const unsigned long *bits, |
505 | unsigned int nr_bits) | |
50ac2d69 | 506 | { |
85dd030e LJ |
507 | if (m->count < m->size) { |
508 | int len = bitmap_scnprintf(m->buf + m->count, | |
509 | m->size - m->count, bits, nr_bits); | |
510 | if (m->count + len < m->size) { | |
511 | m->count += len; | |
512 | return 0; | |
513 | } | |
50ac2d69 AD |
514 | } |
515 | m->count = m->size; | |
516 | return -1; | |
517 | } | |
85dd030e | 518 | EXPORT_SYMBOL(seq_bitmap); |
50ac2d69 | 519 | |
af76aba0 | 520 | int seq_bitmap_list(struct seq_file *m, const unsigned long *bits, |
3eda2011 LJ |
521 | unsigned int nr_bits) |
522 | { | |
523 | if (m->count < m->size) { | |
524 | int len = bitmap_scnlistprintf(m->buf + m->count, | |
525 | m->size - m->count, bits, nr_bits); | |
526 | if (m->count + len < m->size) { | |
527 | m->count += len; | |
528 | return 0; | |
529 | } | |
530 | } | |
531 | m->count = m->size; | |
532 | return -1; | |
533 | } | |
534 | EXPORT_SYMBOL(seq_bitmap_list); | |
535 | ||
1da177e4 LT |
536 | static void *single_start(struct seq_file *p, loff_t *pos) |
537 | { | |
538 | return NULL + (*pos == 0); | |
539 | } | |
540 | ||
541 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) | |
542 | { | |
543 | ++*pos; | |
544 | return NULL; | |
545 | } | |
546 | ||
547 | static void single_stop(struct seq_file *p, void *v) | |
548 | { | |
549 | } | |
550 | ||
551 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), | |
552 | void *data) | |
553 | { | |
554 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); | |
555 | int res = -ENOMEM; | |
556 | ||
557 | if (op) { | |
558 | op->start = single_start; | |
559 | op->next = single_next; | |
560 | op->stop = single_stop; | |
561 | op->show = show; | |
562 | res = seq_open(file, op); | |
563 | if (!res) | |
564 | ((struct seq_file *)file->private_data)->private = data; | |
565 | else | |
566 | kfree(op); | |
567 | } | |
568 | return res; | |
569 | } | |
570 | EXPORT_SYMBOL(single_open); | |
571 | ||
572 | int single_release(struct inode *inode, struct file *file) | |
573 | { | |
15ad7cdc | 574 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
1da177e4 LT |
575 | int res = seq_release(inode, file); |
576 | kfree(op); | |
577 | return res; | |
578 | } | |
579 | EXPORT_SYMBOL(single_release); | |
580 | ||
581 | int seq_release_private(struct inode *inode, struct file *file) | |
582 | { | |
583 | struct seq_file *seq = file->private_data; | |
584 | ||
585 | kfree(seq->private); | |
586 | seq->private = NULL; | |
587 | return seq_release(inode, file); | |
588 | } | |
589 | EXPORT_SYMBOL(seq_release_private); | |
590 | ||
39699037 PE |
591 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
592 | int psize) | |
593 | { | |
594 | int rc; | |
595 | void *private; | |
596 | struct seq_file *seq; | |
597 | ||
598 | private = kzalloc(psize, GFP_KERNEL); | |
599 | if (private == NULL) | |
600 | goto out; | |
601 | ||
602 | rc = seq_open(f, ops); | |
603 | if (rc < 0) | |
604 | goto out_free; | |
605 | ||
606 | seq = f->private_data; | |
607 | seq->private = private; | |
608 | return private; | |
609 | ||
610 | out_free: | |
611 | kfree(private); | |
612 | out: | |
613 | return NULL; | |
614 | } | |
615 | EXPORT_SYMBOL(__seq_open_private); | |
616 | ||
617 | int seq_open_private(struct file *filp, const struct seq_operations *ops, | |
618 | int psize) | |
619 | { | |
620 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; | |
621 | } | |
622 | EXPORT_SYMBOL(seq_open_private); | |
623 | ||
1da177e4 LT |
624 | int seq_putc(struct seq_file *m, char c) |
625 | { | |
626 | if (m->count < m->size) { | |
627 | m->buf[m->count++] = c; | |
628 | return 0; | |
629 | } | |
630 | return -1; | |
631 | } | |
632 | EXPORT_SYMBOL(seq_putc); | |
633 | ||
634 | int seq_puts(struct seq_file *m, const char *s) | |
635 | { | |
636 | int len = strlen(s); | |
637 | if (m->count + len < m->size) { | |
638 | memcpy(m->buf + m->count, s, len); | |
639 | m->count += len; | |
640 | return 0; | |
641 | } | |
642 | m->count = m->size; | |
643 | return -1; | |
644 | } | |
645 | EXPORT_SYMBOL(seq_puts); | |
bcf67e16 | 646 | |
1ac101a5 KH |
647 | /* |
648 | * A helper routine for putting decimal numbers without rich format of printf(). | |
649 | * only 'unsigned long long' is supported. | |
650 | * This routine will put one byte delimiter + number into seq_file. | |
651 | * This routine is very quick when you show lots of numbers. | |
652 | * In usual cases, it will be better to use seq_printf(). It's easier to read. | |
653 | */ | |
654 | int seq_put_decimal_ull(struct seq_file *m, char delimiter, | |
655 | unsigned long long num) | |
656 | { | |
657 | int len; | |
658 | ||
659 | if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */ | |
660 | goto overflow; | |
661 | ||
bda7bad6 KH |
662 | if (delimiter) |
663 | m->buf[m->count++] = delimiter; | |
1ac101a5 KH |
664 | |
665 | if (num < 10) { | |
666 | m->buf[m->count++] = num + '0'; | |
667 | return 0; | |
668 | } | |
669 | ||
670 | len = num_to_str(m->buf + m->count, m->size - m->count, num); | |
671 | if (!len) | |
672 | goto overflow; | |
673 | m->count += len; | |
674 | return 0; | |
675 | overflow: | |
676 | m->count = m->size; | |
677 | return -1; | |
678 | } | |
679 | EXPORT_SYMBOL(seq_put_decimal_ull); | |
680 | ||
bda7bad6 KH |
681 | int seq_put_decimal_ll(struct seq_file *m, char delimiter, |
682 | long long num) | |
683 | { | |
684 | if (num < 0) { | |
685 | if (m->count + 3 >= m->size) { | |
686 | m->count = m->size; | |
687 | return -1; | |
688 | } | |
689 | if (delimiter) | |
690 | m->buf[m->count++] = delimiter; | |
691 | num = -num; | |
692 | delimiter = '-'; | |
693 | } | |
694 | return seq_put_decimal_ull(m, delimiter, num); | |
695 | ||
696 | } | |
697 | EXPORT_SYMBOL(seq_put_decimal_ll); | |
698 | ||
0b923606 PO |
699 | /** |
700 | * seq_write - write arbitrary data to buffer | |
701 | * @seq: seq_file identifying the buffer to which data should be written | |
702 | * @data: data address | |
703 | * @len: number of bytes | |
704 | * | |
705 | * Return 0 on success, non-zero otherwise. | |
706 | */ | |
707 | int seq_write(struct seq_file *seq, const void *data, size_t len) | |
708 | { | |
709 | if (seq->count + len < seq->size) { | |
710 | memcpy(seq->buf + seq->count, data, len); | |
711 | seq->count += len; | |
712 | return 0; | |
713 | } | |
714 | seq->count = seq->size; | |
715 | return -1; | |
716 | } | |
717 | EXPORT_SYMBOL(seq_write); | |
718 | ||
bcf67e16 PE |
719 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
720 | { | |
721 | struct list_head *lh; | |
722 | ||
723 | list_for_each(lh, head) | |
724 | if (pos-- == 0) | |
725 | return lh; | |
726 | ||
727 | return NULL; | |
728 | } | |
bcf67e16 PE |
729 | EXPORT_SYMBOL(seq_list_start); |
730 | ||
731 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) | |
732 | { | |
733 | if (!pos) | |
734 | return head; | |
735 | ||
736 | return seq_list_start(head, pos - 1); | |
737 | } | |
bcf67e16 PE |
738 | EXPORT_SYMBOL(seq_list_start_head); |
739 | ||
740 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) | |
741 | { | |
742 | struct list_head *lh; | |
743 | ||
744 | lh = ((struct list_head *)v)->next; | |
745 | ++*ppos; | |
746 | return lh == head ? NULL : lh; | |
747 | } | |
bcf67e16 | 748 | EXPORT_SYMBOL(seq_list_next); |
66655de6 LZ |
749 | |
750 | /** | |
751 | * seq_hlist_start - start an iteration of a hlist | |
752 | * @head: the head of the hlist | |
753 | * @pos: the start position of the sequence | |
754 | * | |
755 | * Called at seq_file->op->start(). | |
756 | */ | |
757 | struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos) | |
758 | { | |
759 | struct hlist_node *node; | |
760 | ||
761 | hlist_for_each(node, head) | |
762 | if (pos-- == 0) | |
763 | return node; | |
764 | return NULL; | |
765 | } | |
766 | EXPORT_SYMBOL(seq_hlist_start); | |
767 | ||
768 | /** | |
769 | * seq_hlist_start_head - start an iteration of a hlist | |
770 | * @head: the head of the hlist | |
771 | * @pos: the start position of the sequence | |
772 | * | |
773 | * Called at seq_file->op->start(). Call this function if you want to | |
774 | * print a header at the top of the output. | |
775 | */ | |
776 | struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos) | |
777 | { | |
778 | if (!pos) | |
779 | return SEQ_START_TOKEN; | |
780 | ||
781 | return seq_hlist_start(head, pos - 1); | |
782 | } | |
783 | EXPORT_SYMBOL(seq_hlist_start_head); | |
784 | ||
785 | /** | |
786 | * seq_hlist_next - move to the next position of the hlist | |
787 | * @v: the current iterator | |
788 | * @head: the head of the hlist | |
138860b9 | 789 | * @ppos: the current position |
66655de6 LZ |
790 | * |
791 | * Called at seq_file->op->next(). | |
792 | */ | |
793 | struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head, | |
794 | loff_t *ppos) | |
795 | { | |
796 | struct hlist_node *node = v; | |
797 | ||
798 | ++*ppos; | |
799 | if (v == SEQ_START_TOKEN) | |
800 | return head->first; | |
801 | else | |
802 | return node->next; | |
803 | } | |
804 | EXPORT_SYMBOL(seq_hlist_next); | |
1cc52327 | 805 | |
806 | /** | |
807 | * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU | |
808 | * @head: the head of the hlist | |
809 | * @pos: the start position of the sequence | |
810 | * | |
811 | * Called at seq_file->op->start(). | |
812 | * | |
813 | * This list-traversal primitive may safely run concurrently with | |
814 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
815 | * as long as the traversal is guarded by rcu_read_lock(). | |
816 | */ | |
817 | struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head, | |
818 | loff_t pos) | |
819 | { | |
820 | struct hlist_node *node; | |
821 | ||
822 | __hlist_for_each_rcu(node, head) | |
823 | if (pos-- == 0) | |
824 | return node; | |
825 | return NULL; | |
826 | } | |
827 | EXPORT_SYMBOL(seq_hlist_start_rcu); | |
828 | ||
829 | /** | |
830 | * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU | |
831 | * @head: the head of the hlist | |
832 | * @pos: the start position of the sequence | |
833 | * | |
834 | * Called at seq_file->op->start(). Call this function if you want to | |
835 | * print a header at the top of the output. | |
836 | * | |
837 | * This list-traversal primitive may safely run concurrently with | |
838 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
839 | * as long as the traversal is guarded by rcu_read_lock(). | |
840 | */ | |
841 | struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head, | |
842 | loff_t pos) | |
843 | { | |
844 | if (!pos) | |
845 | return SEQ_START_TOKEN; | |
846 | ||
847 | return seq_hlist_start_rcu(head, pos - 1); | |
848 | } | |
849 | EXPORT_SYMBOL(seq_hlist_start_head_rcu); | |
850 | ||
851 | /** | |
852 | * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU | |
853 | * @v: the current iterator | |
854 | * @head: the head of the hlist | |
138860b9 | 855 | * @ppos: the current position |
1cc52327 | 856 | * |
857 | * Called at seq_file->op->next(). | |
858 | * | |
859 | * This list-traversal primitive may safely run concurrently with | |
860 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |
861 | * as long as the traversal is guarded by rcu_read_lock(). | |
862 | */ | |
863 | struct hlist_node *seq_hlist_next_rcu(void *v, | |
864 | struct hlist_head *head, | |
865 | loff_t *ppos) | |
866 | { | |
867 | struct hlist_node *node = v; | |
868 | ||
869 | ++*ppos; | |
870 | if (v == SEQ_START_TOKEN) | |
871 | return rcu_dereference(head->first); | |
872 | else | |
873 | return rcu_dereference(node->next); | |
874 | } | |
875 | EXPORT_SYMBOL(seq_hlist_next_rcu); |