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