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