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