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