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