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