1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/seq_file.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * helper functions for making synthetic files from sequences of records.
61da177e4SLinus Torvalds * initial implementation -- AV, Oct 2001.
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
9a3963015SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10a3963015SJoe Perches
1109652320SAlexey Dobriyan #include <linux/cache.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
13630d9c47SPaul Gortmaker #include <linux/export.h>
141da177e4SLinus Torvalds #include <linux/seq_file.h>
15058504edSHeiko Carstens #include <linux/vmalloc.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
17adb37c4cSEric W. Biederman #include <linux/cred.h>
18058504edSHeiko Carstens #include <linux/mm.h>
1937607102SAndy Shevchenko #include <linux/printk.h>
2025c6bb76SAndy Shevchenko #include <linux/string_helpers.h>
21d4d50710SChristoph Hellwig #include <linux/uio.h>
221da177e4SLinus Torvalds
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
241da177e4SLinus Torvalds #include <asm/page.h>
251da177e4SLinus Torvalds
2609652320SAlexey Dobriyan static struct kmem_cache *seq_file_cache __ro_after_init;
2709652320SAlexey Dobriyan
seq_set_overflow(struct seq_file * m)28e075f591SKAMEZAWA Hiroyuki static void seq_set_overflow(struct seq_file *m)
29e075f591SKAMEZAWA Hiroyuki {
30e075f591SKAMEZAWA Hiroyuki m->count = m->size;
31e075f591SKAMEZAWA Hiroyuki }
32e075f591SKAMEZAWA Hiroyuki
seq_buf_alloc(unsigned long size)33058504edSHeiko Carstens static void *seq_buf_alloc(unsigned long size)
34058504edSHeiko Carstens {
358cae8cd8SEric Sandeen if (unlikely(size > MAX_RW_COUNT))
368cae8cd8SEric Sandeen return NULL;
378cae8cd8SEric Sandeen
38d64d01a1SAlexey Dobriyan return kvmalloc(size, GFP_KERNEL_ACCOUNT);
39058504edSHeiko Carstens }
40058504edSHeiko Carstens
411da177e4SLinus Torvalds /**
421da177e4SLinus Torvalds * seq_open - initialize sequential file
431da177e4SLinus Torvalds * @file: file we initialize
441da177e4SLinus Torvalds * @op: method table describing the sequence
451da177e4SLinus Torvalds *
461da177e4SLinus Torvalds * seq_open() sets @file, associating it with a sequence described
471da177e4SLinus Torvalds * by @op. @op->start() sets the iterator up and returns the first
481da177e4SLinus Torvalds * element of sequence. @op->stop() shuts it down. @op->next()
491da177e4SLinus Torvalds * returns the next element of sequence. @op->show() prints element
501da177e4SLinus Torvalds * into the buffer. In case of error ->start() and ->next() return
511da177e4SLinus Torvalds * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
521da177e4SLinus Torvalds * returns 0 in case of success and negative number in case of error.
53521b5d0cSAl Viro * Returning SEQ_SKIP means "discard this element and move on".
54460b865eSYann Droneaud * Note: seq_open() will allocate a struct seq_file and store its
55460b865eSYann Droneaud * pointer in @file->private_data. This pointer should not be modified.
561da177e4SLinus Torvalds */
seq_open(struct file * file,const struct seq_operations * op)5715ad7cdcSHelge Deller int seq_open(struct file *file, const struct seq_operations *op)
581da177e4SLinus Torvalds {
59189f9841SYann Droneaud struct seq_file *p;
601abe77b0SAl Viro
61189f9841SYann Droneaud WARN_ON(file->private_data);
62189f9841SYann Droneaud
6309652320SAlexey Dobriyan p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
641da177e4SLinus Torvalds if (!p)
651da177e4SLinus Torvalds return -ENOMEM;
66189f9841SYann Droneaud
671abe77b0SAl Viro file->private_data = p;
68189f9841SYann Droneaud
690ac1759aSIngo Molnar mutex_init(&p->lock);
701da177e4SLinus Torvalds p->op = op;
7134dbbcdbSLinus Torvalds
7234dbbcdbSLinus Torvalds // No refcounting: the lifetime of 'p' is constrained
7334dbbcdbSLinus Torvalds // to the lifetime of the file.
7434dbbcdbSLinus Torvalds p->file = file;
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds /*
778f19d472SEric Biederman * seq_files support lseek() and pread(). They do not implement
788f19d472SEric Biederman * write() at all, but we clear FMODE_PWRITE here for historical
798f19d472SEric Biederman * reasons.
808f19d472SEric Biederman *
818f19d472SEric Biederman * If a client of seq_files a) implements file.write() and b) wishes to
828f19d472SEric Biederman * support pwrite() then that client will need to implement its own
838f19d472SEric Biederman * file.open() which calls seq_open() and then sets FMODE_PWRITE.
848f19d472SEric Biederman */
858f19d472SEric Biederman file->f_mode &= ~FMODE_PWRITE;
861da177e4SLinus Torvalds return 0;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds EXPORT_SYMBOL(seq_open);
891da177e4SLinus Torvalds
traverse(struct seq_file * m,loff_t offset)9033da8892SEric Biederman static int traverse(struct seq_file *m, loff_t offset)
9133da8892SEric Biederman {
921f4aace6SNeilBrown loff_t pos = 0;
9333da8892SEric Biederman int error = 0;
9433da8892SEric Biederman void *p;
9533da8892SEric Biederman
961f4aace6SNeilBrown m->index = 0;
9733da8892SEric Biederman m->count = m->from = 0;
981f4aace6SNeilBrown if (!offset)
9933da8892SEric Biederman return 0;
1001f4aace6SNeilBrown
10133da8892SEric Biederman if (!m->buf) {
102058504edSHeiko Carstens m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
10333da8892SEric Biederman if (!m->buf)
10433da8892SEric Biederman return -ENOMEM;
10533da8892SEric Biederman }
1061f4aace6SNeilBrown p = m->op->start(m, &m->index);
10733da8892SEric Biederman while (p) {
10833da8892SEric Biederman error = PTR_ERR(p);
10933da8892SEric Biederman if (IS_ERR(p))
11033da8892SEric Biederman break;
11133da8892SEric Biederman error = m->op->show(m, p);
11233da8892SEric Biederman if (error < 0)
11333da8892SEric Biederman break;
11433da8892SEric Biederman if (unlikely(error)) {
11533da8892SEric Biederman error = 0;
11633da8892SEric Biederman m->count = 0;
11733da8892SEric Biederman }
1181f33c41cSJoe Perches if (seq_has_overflowed(m))
11933da8892SEric Biederman goto Eoverflow;
1206a2aeab5SNeilBrown p = m->op->next(m, p, &m->index);
12133da8892SEric Biederman if (pos + m->count > offset) {
12233da8892SEric Biederman m->from = offset - pos;
12333da8892SEric Biederman m->count -= m->from;
12433da8892SEric Biederman break;
12533da8892SEric Biederman }
12633da8892SEric Biederman pos += m->count;
12733da8892SEric Biederman m->count = 0;
1281f4aace6SNeilBrown if (pos == offset)
12933da8892SEric Biederman break;
13033da8892SEric Biederman }
13133da8892SEric Biederman m->op->stop(m, p);
13233da8892SEric Biederman return error;
13333da8892SEric Biederman
13433da8892SEric Biederman Eoverflow:
13533da8892SEric Biederman m->op->stop(m, p);
136058504edSHeiko Carstens kvfree(m->buf);
137801a7605SAl Viro m->count = 0;
138058504edSHeiko Carstens m->buf = seq_buf_alloc(m->size <<= 1);
13933da8892SEric Biederman return !m->buf ? -ENOMEM : -EAGAIN;
14033da8892SEric Biederman }
14133da8892SEric Biederman
1421da177e4SLinus Torvalds /**
1431da177e4SLinus Torvalds * seq_read - ->read() method for sequential files.
14467be2dd1SMartin Waitz * @file: the file to read from
14567be2dd1SMartin Waitz * @buf: the buffer to read to
14667be2dd1SMartin Waitz * @size: the maximum number of bytes to read
14767be2dd1SMartin Waitz * @ppos: the current position in the file
1481da177e4SLinus Torvalds *
1491da177e4SLinus Torvalds * Ready-made ->f_op->read()
1501da177e4SLinus Torvalds */
seq_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)1511da177e4SLinus Torvalds ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
1521da177e4SLinus Torvalds {
153d4d50710SChristoph Hellwig struct iovec iov = { .iov_base = buf, .iov_len = size};
154d4d50710SChristoph Hellwig struct kiocb kiocb;
155d4d50710SChristoph Hellwig struct iov_iter iter;
156d4d50710SChristoph Hellwig ssize_t ret;
157d4d50710SChristoph Hellwig
158d4d50710SChristoph Hellwig init_sync_kiocb(&kiocb, file);
159*de4eda9dSAl Viro iov_iter_init(&iter, ITER_DEST, &iov, 1, size);
160d4d50710SChristoph Hellwig
161d4d50710SChristoph Hellwig kiocb.ki_pos = *ppos;
162d4d50710SChristoph Hellwig ret = seq_read_iter(&kiocb, &iter);
163d4d50710SChristoph Hellwig *ppos = kiocb.ki_pos;
164d4d50710SChristoph Hellwig return ret;
165d4d50710SChristoph Hellwig }
166d4d50710SChristoph Hellwig EXPORT_SYMBOL(seq_read);
167d4d50710SChristoph Hellwig
168d4d50710SChristoph Hellwig /*
169d4d50710SChristoph Hellwig * Ready-made ->f_op->read_iter()
170d4d50710SChristoph Hellwig */
seq_read_iter(struct kiocb * iocb,struct iov_iter * iter)171d4d50710SChristoph Hellwig ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
172d4d50710SChristoph Hellwig {
173d4d50710SChristoph Hellwig struct seq_file *m = iocb->ki_filp->private_data;
1741da177e4SLinus Torvalds size_t copied = 0;
1751da177e4SLinus Torvalds size_t n;
1761da177e4SLinus Torvalds void *p;
1771da177e4SLinus Torvalds int err = 0;
1781da177e4SLinus Torvalds
1794bbf439bSAl Viro if (!iov_iter_count(iter))
1804bbf439bSAl Viro return 0;
1814bbf439bSAl Viro
1820ac1759aSIngo Molnar mutex_lock(&m->lock);
1838f19d472SEric Biederman
1841da177e4SLinus Torvalds /*
185e522751dSTomasz Majchrzak * if request is to read from zero offset, reset iterator to first
186e522751dSTomasz Majchrzak * record as it might have been already advanced by previous requests
187e522751dSTomasz Majchrzak */
188d4d50710SChristoph Hellwig if (iocb->ki_pos == 0) {
189e522751dSTomasz Majchrzak m->index = 0;
190cf5eebaeSMiklos Szeredi m->count = 0;
191cf5eebaeSMiklos Szeredi }
192e522751dSTomasz Majchrzak
193d4d50710SChristoph Hellwig /* Don't assume ki_pos is where we left it */
194d4d50710SChristoph Hellwig if (unlikely(iocb->ki_pos != m->read_pos)) {
195d4d50710SChristoph Hellwig while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
1967904ac84SEarl Chew ;
1977904ac84SEarl Chew if (err) {
1987904ac84SEarl Chew /* With prejudice... */
1997904ac84SEarl Chew m->read_pos = 0;
2007904ac84SEarl Chew m->index = 0;
2017904ac84SEarl Chew m->count = 0;
2027904ac84SEarl Chew goto Done;
2037904ac84SEarl Chew } else {
204d4d50710SChristoph Hellwig m->read_pos = iocb->ki_pos;
2057904ac84SEarl Chew }
2067904ac84SEarl Chew }
2077904ac84SEarl Chew
2081da177e4SLinus Torvalds /* grab buffer if we didn't have one */
2091da177e4SLinus Torvalds if (!m->buf) {
210058504edSHeiko Carstens m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
2111da177e4SLinus Torvalds if (!m->buf)
2121da177e4SLinus Torvalds goto Enomem;
2131da177e4SLinus Torvalds }
2144bbf439bSAl Viro // something left in the buffer - copy it out first
2151da177e4SLinus Torvalds if (m->count) {
2164bbf439bSAl Viro n = copy_to_iter(m->buf + m->from, m->count, iter);
2171da177e4SLinus Torvalds m->count -= n;
2181da177e4SLinus Torvalds m->from += n;
2191da177e4SLinus Torvalds copied += n;
2204bbf439bSAl Viro if (m->count) // hadn't managed to copy everything
2211da177e4SLinus Torvalds goto Done;
2221da177e4SLinus Torvalds }
2234bbf439bSAl Viro // get a non-empty record in the buffer
2241f4aace6SNeilBrown m->from = 0;
2251f4aace6SNeilBrown p = m->op->start(m, &m->index);
2264cdfe84bSAl Viro while (1) {
2271da177e4SLinus Torvalds err = PTR_ERR(p);
2284bbf439bSAl Viro if (!p || IS_ERR(p)) // EOF or an error
2291da177e4SLinus Torvalds break;
2301da177e4SLinus Torvalds err = m->op->show(m, p);
2314bbf439bSAl Viro if (err < 0) // hard error
2321da177e4SLinus Torvalds break;
2334bbf439bSAl Viro if (unlikely(err)) // ->show() says "skip it"
234521b5d0cSAl Viro m->count = 0;
2354bbf439bSAl Viro if (unlikely(!m->count)) { // empty record
2361f4aace6SNeilBrown p = m->op->next(m, p, &m->index);
2374cdfe84bSAl Viro continue;
2384cdfe84bSAl Viro }
2394bbf439bSAl Viro if (!seq_has_overflowed(m)) // got it
2401da177e4SLinus Torvalds goto Fill;
2414bbf439bSAl Viro // need a bigger buffer
2421da177e4SLinus Torvalds m->op->stop(m, p);
243058504edSHeiko Carstens kvfree(m->buf);
244801a7605SAl Viro m->count = 0;
245058504edSHeiko Carstens m->buf = seq_buf_alloc(m->size <<= 1);
2461da177e4SLinus Torvalds if (!m->buf)
2471da177e4SLinus Torvalds goto Enomem;
2481f4aace6SNeilBrown p = m->op->start(m, &m->index);
2491da177e4SLinus Torvalds }
2504bbf439bSAl Viro // EOF or an error
2511da177e4SLinus Torvalds m->op->stop(m, p);
2521da177e4SLinus Torvalds m->count = 0;
2531da177e4SLinus Torvalds goto Done;
2541da177e4SLinus Torvalds Fill:
2554bbf439bSAl Viro // one non-empty record is in the buffer; if they want more,
2564bbf439bSAl Viro // try to fit more in, but in any case we need to advance
2574bbf439bSAl Viro // the iterator once for every record shown.
2581f4aace6SNeilBrown while (1) {
2591da177e4SLinus Torvalds size_t offs = m->count;
2601f4aace6SNeilBrown loff_t pos = m->index;
2611f4aace6SNeilBrown
2621f4aace6SNeilBrown p = m->op->next(m, p, &m->index);
2633bfa7e14SVasily Averin if (pos == m->index) {
264a3963015SJoe Perches pr_info_ratelimited("buggy .next function %ps did not update position index\n",
2653bfa7e14SVasily Averin m->op->next);
2661f4aace6SNeilBrown m->index++;
2673bfa7e14SVasily Averin }
2684bbf439bSAl Viro if (!p || IS_ERR(p)) // no next record for us
2691da177e4SLinus Torvalds break;
2704bbf439bSAl Viro if (m->count >= iov_iter_count(iter))
2711f4aace6SNeilBrown break;
2721da177e4SLinus Torvalds err = m->op->show(m, p);
2734bbf439bSAl Viro if (err > 0) { // ->show() says "skip it"
2741da177e4SLinus Torvalds m->count = offs;
2754bbf439bSAl Viro } else if (err || seq_has_overflowed(m)) {
2764bbf439bSAl Viro m->count = offs;
2771da177e4SLinus Torvalds break;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds m->op->stop(m, p);
2814bbf439bSAl Viro n = copy_to_iter(m->buf, m->count, iter);
2821da177e4SLinus Torvalds copied += n;
2831da177e4SLinus Torvalds m->count -= n;
2841da177e4SLinus Torvalds m->from = n;
2851da177e4SLinus Torvalds Done:
2864bbf439bSAl Viro if (unlikely(!copied)) {
2874bbf439bSAl Viro copied = m->count ? -EFAULT : err;
2884bbf439bSAl Viro } else {
289d4d50710SChristoph Hellwig iocb->ki_pos += copied;
2908f19d472SEric Biederman m->read_pos += copied;
2918f19d472SEric Biederman }
2920ac1759aSIngo Molnar mutex_unlock(&m->lock);
2931da177e4SLinus Torvalds return copied;
2941da177e4SLinus Torvalds Enomem:
2951da177e4SLinus Torvalds err = -ENOMEM;
2961da177e4SLinus Torvalds goto Done;
2971da177e4SLinus Torvalds }
298d4d50710SChristoph Hellwig EXPORT_SYMBOL(seq_read_iter);
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds /**
3011da177e4SLinus Torvalds * seq_lseek - ->llseek() method for sequential files.
30267be2dd1SMartin Waitz * @file: the file in question
30367be2dd1SMartin Waitz * @offset: new position
304254adaa4SRandy Dunlap * @whence: 0 for absolute, 1 for relative position
3051da177e4SLinus Torvalds *
3061da177e4SLinus Torvalds * Ready-made ->f_op->llseek()
3071da177e4SLinus Torvalds */
seq_lseek(struct file * file,loff_t offset,int whence)308965c8e59SAndrew Morton loff_t seq_lseek(struct file *file, loff_t offset, int whence)
3091da177e4SLinus Torvalds {
3108209e2f4SJoe Perches struct seq_file *m = file->private_data;
31116abef0eSDavid Sterba loff_t retval = -EINVAL;
3121da177e4SLinus Torvalds
3130ac1759aSIngo Molnar mutex_lock(&m->lock);
314965c8e59SAndrew Morton switch (whence) {
31580de7f7aSCyrill Gorcunov case SEEK_CUR:
3161da177e4SLinus Torvalds offset += file->f_pos;
317df561f66SGustavo A. R. Silva fallthrough;
31880de7f7aSCyrill Gorcunov case SEEK_SET:
3191da177e4SLinus Torvalds if (offset < 0)
3201da177e4SLinus Torvalds break;
3211da177e4SLinus Torvalds retval = offset;
3228f19d472SEric Biederman if (offset != m->read_pos) {
3231da177e4SLinus Torvalds while ((retval = traverse(m, offset)) == -EAGAIN)
3241da177e4SLinus Torvalds ;
3251da177e4SLinus Torvalds if (retval) {
3261da177e4SLinus Torvalds /* with extreme prejudice... */
3271da177e4SLinus Torvalds file->f_pos = 0;
3288f19d472SEric Biederman m->read_pos = 0;
3291da177e4SLinus Torvalds m->index = 0;
3301da177e4SLinus Torvalds m->count = 0;
3311da177e4SLinus Torvalds } else {
3328f19d472SEric Biederman m->read_pos = offset;
3331da177e4SLinus Torvalds retval = file->f_pos = offset;
3341da177e4SLinus Torvalds }
33505e16745SGu Zheng } else {
33605e16745SGu Zheng file->f_pos = offset;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds }
33900c5746dSAlexey Dobriyan mutex_unlock(&m->lock);
3401da177e4SLinus Torvalds return retval;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds EXPORT_SYMBOL(seq_lseek);
3431da177e4SLinus Torvalds
3441da177e4SLinus Torvalds /**
3451da177e4SLinus Torvalds * seq_release - free the structures associated with sequential file.
3461da177e4SLinus Torvalds * @file: file in question
3476131ffaaSAl Viro * @inode: its inode
3481da177e4SLinus Torvalds *
3491da177e4SLinus Torvalds * Frees the structures associated with sequential file; can be used
3501da177e4SLinus Torvalds * as ->f_op->release() if you don't have private data to destroy.
3511da177e4SLinus Torvalds */
seq_release(struct inode * inode,struct file * file)3521da177e4SLinus Torvalds int seq_release(struct inode *inode, struct file *file)
3531da177e4SLinus Torvalds {
3548209e2f4SJoe Perches struct seq_file *m = file->private_data;
355058504edSHeiko Carstens kvfree(m->buf);
35609652320SAlexey Dobriyan kmem_cache_free(seq_file_cache, m);
3571da177e4SLinus Torvalds return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds EXPORT_SYMBOL(seq_release);
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds /**
3621d31aa17SAndy Shevchenko * seq_escape_mem - print data into buffer, escaping some characters
3631d31aa17SAndy Shevchenko * @m: target buffer
3641d31aa17SAndy Shevchenko * @src: source buffer
3651d31aa17SAndy Shevchenko * @len: size of source buffer
3661d31aa17SAndy Shevchenko * @flags: flags to pass to string_escape_mem()
3671d31aa17SAndy Shevchenko * @esc: set of characters that need escaping
3681d31aa17SAndy Shevchenko *
3691d31aa17SAndy Shevchenko * Puts data into buffer, replacing each occurrence of character from
3701d31aa17SAndy Shevchenko * given class (defined by @flags and @esc) with printable escaped sequence.
3711d31aa17SAndy Shevchenko *
3721d31aa17SAndy Shevchenko * Use seq_has_overflowed() to check for errors.
3731d31aa17SAndy Shevchenko */
seq_escape_mem(struct seq_file * m,const char * src,size_t len,unsigned int flags,const char * esc)3741d31aa17SAndy Shevchenko void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
3751d31aa17SAndy Shevchenko unsigned int flags, const char *esc)
3761d31aa17SAndy Shevchenko {
3771d31aa17SAndy Shevchenko char *buf;
3781d31aa17SAndy Shevchenko size_t size = seq_get_buf(m, &buf);
3791d31aa17SAndy Shevchenko int ret;
3801d31aa17SAndy Shevchenko
3811d31aa17SAndy Shevchenko ret = string_escape_mem(src, len, buf, size, flags, esc);
3821d31aa17SAndy Shevchenko seq_commit(m, ret < size ? ret : -1);
3831d31aa17SAndy Shevchenko }
3841d31aa17SAndy Shevchenko EXPORT_SYMBOL(seq_escape_mem);
3851d31aa17SAndy Shevchenko
seq_vprintf(struct seq_file * m,const char * f,va_list args)3866798a8caSJoe Perches void seq_vprintf(struct seq_file *m, const char *f, va_list args)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds int len;
3891da177e4SLinus Torvalds
3901da177e4SLinus Torvalds if (m->count < m->size) {
3911da177e4SLinus Torvalds len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
3921da177e4SLinus Torvalds if (m->count + len < m->size) {
3931da177e4SLinus Torvalds m->count += len;
3946798a8caSJoe Perches return;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds }
397e075f591SKAMEZAWA Hiroyuki seq_set_overflow(m);
3981da177e4SLinus Torvalds }
399a4808147SSteven Whitehouse EXPORT_SYMBOL(seq_vprintf);
400a4808147SSteven Whitehouse
seq_printf(struct seq_file * m,const char * f,...)4016798a8caSJoe Perches void seq_printf(struct seq_file *m, const char *f, ...)
402a4808147SSteven Whitehouse {
403a4808147SSteven Whitehouse va_list args;
404a4808147SSteven Whitehouse
405a4808147SSteven Whitehouse va_start(args, f);
4066798a8caSJoe Perches seq_vprintf(m, f, args);
407a4808147SSteven Whitehouse va_end(args);
408a4808147SSteven Whitehouse }
4091da177e4SLinus Torvalds EXPORT_SYMBOL(seq_printf);
4101da177e4SLinus Torvalds
41176d6a133SFlorent Revest #ifdef CONFIG_BINARY_PRINTF
seq_bprintf(struct seq_file * m,const char * f,const u32 * binary)41276d6a133SFlorent Revest void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
41376d6a133SFlorent Revest {
41476d6a133SFlorent Revest int len;
41576d6a133SFlorent Revest
41676d6a133SFlorent Revest if (m->count < m->size) {
41776d6a133SFlorent Revest len = bstr_printf(m->buf + m->count, m->size - m->count, f,
41876d6a133SFlorent Revest binary);
41976d6a133SFlorent Revest if (m->count + len < m->size) {
42076d6a133SFlorent Revest m->count += len;
42176d6a133SFlorent Revest return;
42276d6a133SFlorent Revest }
42376d6a133SFlorent Revest }
42476d6a133SFlorent Revest seq_set_overflow(m);
42576d6a133SFlorent Revest }
42676d6a133SFlorent Revest EXPORT_SYMBOL(seq_bprintf);
42776d6a133SFlorent Revest #endif /* CONFIG_BINARY_PRINTF */
42876d6a133SFlorent Revest
42974e2f334STörök Edwin /**
43074e2f334STörök Edwin * mangle_path - mangle and copy path to buffer beginning
431958086d1STörök Edwin * @s: buffer start
432958086d1STörök Edwin * @p: beginning of path in above buffer
433958086d1STörök Edwin * @esc: set of characters that need escaping
43474e2f334STörök Edwin *
43574e2f334STörök Edwin * Copy the path from @p to @s, replacing each occurrence of character from
43674e2f334STörök Edwin * @esc with usual octal escape.
43774e2f334STörök Edwin * Returns pointer past last written character in @s, or NULL in case of
43874e2f334STörök Edwin * failure.
43974e2f334STörök Edwin */
mangle_path(char * s,const char * p,const char * esc)4408c9379e9SAl Viro char *mangle_path(char *s, const char *p, const char *esc)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds while (s <= p) {
4431da177e4SLinus Torvalds char c = *p++;
4441da177e4SLinus Torvalds if (!c) {
4456092d048SRam Pai return s;
4461da177e4SLinus Torvalds } else if (!strchr(esc, c)) {
4471da177e4SLinus Torvalds *s++ = c;
4481da177e4SLinus Torvalds } else if (s + 4 > p) {
4491da177e4SLinus Torvalds break;
4501da177e4SLinus Torvalds } else {
4511da177e4SLinus Torvalds *s++ = '\\';
4521da177e4SLinus Torvalds *s++ = '0' + ((c & 0300) >> 6);
4531da177e4SLinus Torvalds *s++ = '0' + ((c & 070) >> 3);
4541da177e4SLinus Torvalds *s++ = '0' + (c & 07);
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds }
4576092d048SRam Pai return NULL;
4586092d048SRam Pai }
459604094f4SIngo Molnar EXPORT_SYMBOL(mangle_path);
4606092d048SRam Pai
46152afeefbSArjan van de Ven /**
46252afeefbSArjan van de Ven * seq_path - seq_file interface to print a pathname
46352afeefbSArjan van de Ven * @m: the seq_file handle
46452afeefbSArjan van de Ven * @path: the struct path to print
46552afeefbSArjan van de Ven * @esc: set of characters to escape in the output
46652afeefbSArjan van de Ven *
46752afeefbSArjan van de Ven * return the absolute path of 'path', as represented by the
46852afeefbSArjan van de Ven * dentry / mnt pair in the path parameter.
4696092d048SRam Pai */
seq_path(struct seq_file * m,const struct path * path,const char * esc)4708c9379e9SAl Viro int seq_path(struct seq_file *m, const struct path *path, const char *esc)
4716092d048SRam Pai {
472f8439806SMiklos Szeredi char *buf;
473f8439806SMiklos Szeredi size_t size = seq_get_buf(m, &buf);
474f8439806SMiklos Szeredi int res = -1;
475f8439806SMiklos Szeredi
476f8439806SMiklos Szeredi if (size) {
477f8439806SMiklos Szeredi char *p = d_path(path, buf, size);
4786092d048SRam Pai if (!IS_ERR(p)) {
479f8439806SMiklos Szeredi char *end = mangle_path(buf, p, esc);
480f8439806SMiklos Szeredi if (end)
481f8439806SMiklos Szeredi res = end - buf;
4826092d048SRam Pai }
4831da177e4SLinus Torvalds }
484f8439806SMiklos Szeredi seq_commit(m, res);
485f8439806SMiklos Szeredi
486f8439806SMiklos Szeredi return res;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds EXPORT_SYMBOL(seq_path);
4891da177e4SLinus Torvalds
4902726d566SMiklos Szeredi /**
4912726d566SMiklos Szeredi * seq_file_path - seq_file interface to print a pathname of a file
4922726d566SMiklos Szeredi * @m: the seq_file handle
4932726d566SMiklos Szeredi * @file: the struct file to print
4942726d566SMiklos Szeredi * @esc: set of characters to escape in the output
4952726d566SMiklos Szeredi *
4962726d566SMiklos Szeredi * return the absolute path to the file.
4972726d566SMiklos Szeredi */
seq_file_path(struct seq_file * m,struct file * file,const char * esc)4982726d566SMiklos Szeredi int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
4992726d566SMiklos Szeredi {
5002726d566SMiklos Szeredi return seq_path(m, &file->f_path, esc);
5012726d566SMiklos Szeredi }
5022726d566SMiklos Szeredi EXPORT_SYMBOL(seq_file_path);
5032726d566SMiklos Szeredi
5046092d048SRam Pai /*
5059d1bc601SMiklos Szeredi * Same as seq_path, but relative to supplied root.
5069d1bc601SMiklos Szeredi */
seq_path_root(struct seq_file * m,const struct path * path,const struct path * root,const char * esc)5078c9379e9SAl Viro int seq_path_root(struct seq_file *m, const struct path *path,
5088c9379e9SAl Viro const struct path *root, const char *esc)
5099d1bc601SMiklos Szeredi {
510f8439806SMiklos Szeredi char *buf;
511f8439806SMiklos Szeredi size_t size = seq_get_buf(m, &buf);
512f8439806SMiklos Szeredi int res = -ENAMETOOLONG;
513f8439806SMiklos Szeredi
514f8439806SMiklos Szeredi if (size) {
5159d1bc601SMiklos Szeredi char *p;
5169d1bc601SMiklos Szeredi
517f8439806SMiklos Szeredi p = __d_path(path, root, buf, size);
51802125a82SAl Viro if (!p)
51902125a82SAl Viro return SEQ_SKIP;
520f8439806SMiklos Szeredi res = PTR_ERR(p);
5219d1bc601SMiklos Szeredi if (!IS_ERR(p)) {
522f8439806SMiklos Szeredi char *end = mangle_path(buf, p, esc);
523f8439806SMiklos Szeredi if (end)
524f8439806SMiklos Szeredi res = end - buf;
525f8439806SMiklos Szeredi else
526f8439806SMiklos Szeredi res = -ENAMETOOLONG;
5279d1bc601SMiklos Szeredi }
5289d1bc601SMiklos Szeredi }
529f8439806SMiklos Szeredi seq_commit(m, res);
530f8439806SMiklos Szeredi
53102125a82SAl Viro return res < 0 && res != -ENAMETOOLONG ? res : 0;
5329d1bc601SMiklos Szeredi }
5339d1bc601SMiklos Szeredi
5349d1bc601SMiklos Szeredi /*
5356092d048SRam Pai * returns the path of the 'dentry' from the root of its filesystem.
5366092d048SRam Pai */
seq_dentry(struct seq_file * m,struct dentry * dentry,const char * esc)5378c9379e9SAl Viro int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
5386092d048SRam Pai {
539f8439806SMiklos Szeredi char *buf;
540f8439806SMiklos Szeredi size_t size = seq_get_buf(m, &buf);
541f8439806SMiklos Szeredi int res = -1;
542f8439806SMiklos Szeredi
543f8439806SMiklos Szeredi if (size) {
544f8439806SMiklos Szeredi char *p = dentry_path(dentry, buf, size);
5456092d048SRam Pai if (!IS_ERR(p)) {
546f8439806SMiklos Szeredi char *end = mangle_path(buf, p, esc);
547f8439806SMiklos Szeredi if (end)
548f8439806SMiklos Szeredi res = end - buf;
5496092d048SRam Pai }
5506092d048SRam Pai }
551f8439806SMiklos Szeredi seq_commit(m, res);
552f8439806SMiklos Szeredi
553f8439806SMiklos Szeredi return res;
5546092d048SRam Pai }
555c8d3fe02SOmar Sandoval EXPORT_SYMBOL(seq_dentry);
5566092d048SRam Pai
single_start(struct seq_file * p,loff_t * pos)55790b2433eSMaíra Canal void *single_start(struct seq_file *p, loff_t *pos)
5581da177e4SLinus Torvalds {
55990b2433eSMaíra Canal return *pos ? NULL : SEQ_START_TOKEN;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds
single_next(struct seq_file * p,void * v,loff_t * pos)5621da177e4SLinus Torvalds static void *single_next(struct seq_file *p, void *v, loff_t *pos)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds ++*pos;
5651da177e4SLinus Torvalds return NULL;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds
single_stop(struct seq_file * p,void * v)5681da177e4SLinus Torvalds static void single_stop(struct seq_file *p, void *v)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds
single_open(struct file * file,int (* show)(struct seq_file *,void *),void * data)5721da177e4SLinus Torvalds int single_open(struct file *file, int (*show)(struct seq_file *, void *),
5731da177e4SLinus Torvalds void *data)
5741da177e4SLinus Torvalds {
575d64d01a1SAlexey Dobriyan struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
5761da177e4SLinus Torvalds int res = -ENOMEM;
5771da177e4SLinus Torvalds
5781da177e4SLinus Torvalds if (op) {
5791da177e4SLinus Torvalds op->start = single_start;
5801da177e4SLinus Torvalds op->next = single_next;
5811da177e4SLinus Torvalds op->stop = single_stop;
5821da177e4SLinus Torvalds op->show = show;
5831da177e4SLinus Torvalds res = seq_open(file, op);
5841da177e4SLinus Torvalds if (!res)
5851da177e4SLinus Torvalds ((struct seq_file *)file->private_data)->private = data;
5861da177e4SLinus Torvalds else
5871da177e4SLinus Torvalds kfree(op);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds return res;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds EXPORT_SYMBOL(single_open);
5921da177e4SLinus Torvalds
single_open_size(struct file * file,int (* show)(struct seq_file *,void *),void * data,size_t size)5932043f495SAl Viro int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
5942043f495SAl Viro void *data, size_t size)
5952043f495SAl Viro {
596058504edSHeiko Carstens char *buf = seq_buf_alloc(size);
5972043f495SAl Viro int ret;
5982043f495SAl Viro if (!buf)
5992043f495SAl Viro return -ENOMEM;
6002043f495SAl Viro ret = single_open(file, show, data);
6012043f495SAl Viro if (ret) {
602058504edSHeiko Carstens kvfree(buf);
6032043f495SAl Viro return ret;
6042043f495SAl Viro }
6052043f495SAl Viro ((struct seq_file *)file->private_data)->buf = buf;
6062043f495SAl Viro ((struct seq_file *)file->private_data)->size = size;
6072043f495SAl Viro return 0;
6082043f495SAl Viro }
6092043f495SAl Viro EXPORT_SYMBOL(single_open_size);
6102043f495SAl Viro
single_release(struct inode * inode,struct file * file)6111da177e4SLinus Torvalds int single_release(struct inode *inode, struct file *file)
6121da177e4SLinus Torvalds {
61315ad7cdcSHelge Deller const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
6141da177e4SLinus Torvalds int res = seq_release(inode, file);
6151da177e4SLinus Torvalds kfree(op);
6161da177e4SLinus Torvalds return res;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds EXPORT_SYMBOL(single_release);
6191da177e4SLinus Torvalds
seq_release_private(struct inode * inode,struct file * file)6201da177e4SLinus Torvalds int seq_release_private(struct inode *inode, struct file *file)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds struct seq_file *seq = file->private_data;
6231da177e4SLinus Torvalds
6241da177e4SLinus Torvalds kfree(seq->private);
6251da177e4SLinus Torvalds seq->private = NULL;
6261da177e4SLinus Torvalds return seq_release(inode, file);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds EXPORT_SYMBOL(seq_release_private);
6291da177e4SLinus Torvalds
__seq_open_private(struct file * f,const struct seq_operations * ops,int psize)63039699037SPavel Emelyanov void *__seq_open_private(struct file *f, const struct seq_operations *ops,
63139699037SPavel Emelyanov int psize)
63239699037SPavel Emelyanov {
63339699037SPavel Emelyanov int rc;
63439699037SPavel Emelyanov void *private;
63539699037SPavel Emelyanov struct seq_file *seq;
63639699037SPavel Emelyanov
637d64d01a1SAlexey Dobriyan private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
63839699037SPavel Emelyanov if (private == NULL)
63939699037SPavel Emelyanov goto out;
64039699037SPavel Emelyanov
64139699037SPavel Emelyanov rc = seq_open(f, ops);
64239699037SPavel Emelyanov if (rc < 0)
64339699037SPavel Emelyanov goto out_free;
64439699037SPavel Emelyanov
64539699037SPavel Emelyanov seq = f->private_data;
64639699037SPavel Emelyanov seq->private = private;
64739699037SPavel Emelyanov return private;
64839699037SPavel Emelyanov
64939699037SPavel Emelyanov out_free:
65039699037SPavel Emelyanov kfree(private);
65139699037SPavel Emelyanov out:
65239699037SPavel Emelyanov return NULL;
65339699037SPavel Emelyanov }
65439699037SPavel Emelyanov EXPORT_SYMBOL(__seq_open_private);
65539699037SPavel Emelyanov
seq_open_private(struct file * filp,const struct seq_operations * ops,int psize)65639699037SPavel Emelyanov int seq_open_private(struct file *filp, const struct seq_operations *ops,
65739699037SPavel Emelyanov int psize)
65839699037SPavel Emelyanov {
65939699037SPavel Emelyanov return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
66039699037SPavel Emelyanov }
66139699037SPavel Emelyanov EXPORT_SYMBOL(seq_open_private);
66239699037SPavel Emelyanov
seq_putc(struct seq_file * m,char c)6636798a8caSJoe Perches void seq_putc(struct seq_file *m, char c)
6641da177e4SLinus Torvalds {
6656798a8caSJoe Perches if (m->count >= m->size)
6666798a8caSJoe Perches return;
6676798a8caSJoe Perches
6681da177e4SLinus Torvalds m->buf[m->count++] = c;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds EXPORT_SYMBOL(seq_putc);
6711da177e4SLinus Torvalds
seq_puts(struct seq_file * m,const char * s)6726798a8caSJoe Perches void seq_puts(struct seq_file *m, const char *s)
6731da177e4SLinus Torvalds {
6741da177e4SLinus Torvalds int len = strlen(s);
6756798a8caSJoe Perches
6766798a8caSJoe Perches if (m->count + len >= m->size) {
6776798a8caSJoe Perches seq_set_overflow(m);
6786798a8caSJoe Perches return;
6796798a8caSJoe Perches }
6801da177e4SLinus Torvalds memcpy(m->buf + m->count, s, len);
6811da177e4SLinus Torvalds m->count += len;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds EXPORT_SYMBOL(seq_puts);
684bcf67e16SPavel Emelianov
685d1be35cbSAndrei Vagin /**
686961f3c89SMauro Carvalho Chehab * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
687961f3c89SMauro Carvalho Chehab * without rich format of printf().
6881ac101a5SKAMEZAWA Hiroyuki * only 'unsigned long long' is supported.
689d1be35cbSAndrei Vagin * @m: seq_file identifying the buffer to which data should be written
690d1be35cbSAndrei Vagin * @delimiter: a string which is printed before the number
691d1be35cbSAndrei Vagin * @num: the number
692d1be35cbSAndrei Vagin * @width: a minimum field width
693d1be35cbSAndrei Vagin *
694d1be35cbSAndrei Vagin * This routine will put strlen(delimiter) + number into seq_filed.
6951ac101a5SKAMEZAWA Hiroyuki * This routine is very quick when you show lots of numbers.
6961ac101a5SKAMEZAWA Hiroyuki * In usual cases, it will be better to use seq_printf(). It's easier to read.
6971ac101a5SKAMEZAWA Hiroyuki */
seq_put_decimal_ull_width(struct seq_file * m,const char * delimiter,unsigned long long num,unsigned int width)698d1be35cbSAndrei Vagin void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
699d1be35cbSAndrei Vagin unsigned long long num, unsigned int width)
7001ac101a5SKAMEZAWA Hiroyuki {
7011ac101a5SKAMEZAWA Hiroyuki int len;
7021ac101a5SKAMEZAWA Hiroyuki
7031ac101a5SKAMEZAWA Hiroyuki if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
7041ac101a5SKAMEZAWA Hiroyuki goto overflow;
7051ac101a5SKAMEZAWA Hiroyuki
70648dffbf8SAndrei Vagin if (delimiter && delimiter[0]) {
70748dffbf8SAndrei Vagin if (delimiter[1] == 0)
70848dffbf8SAndrei Vagin seq_putc(m, delimiter[0]);
70948dffbf8SAndrei Vagin else
71048dffbf8SAndrei Vagin seq_puts(m, delimiter);
71148dffbf8SAndrei Vagin }
71275ba1d07SJoe Perches
713d1be35cbSAndrei Vagin if (!width)
714d1be35cbSAndrei Vagin width = 1;
715d1be35cbSAndrei Vagin
716d1be35cbSAndrei Vagin if (m->count + width >= m->size)
71775ba1d07SJoe Perches goto overflow;
7181ac101a5SKAMEZAWA Hiroyuki
719d1be35cbSAndrei Vagin len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
7201ac101a5SKAMEZAWA Hiroyuki if (!len)
7211ac101a5SKAMEZAWA Hiroyuki goto overflow;
72275ba1d07SJoe Perches
7231ac101a5SKAMEZAWA Hiroyuki m->count += len;
7246798a8caSJoe Perches return;
7256798a8caSJoe Perches
7261ac101a5SKAMEZAWA Hiroyuki overflow:
727e075f591SKAMEZAWA Hiroyuki seq_set_overflow(m);
7281ac101a5SKAMEZAWA Hiroyuki }
729d1be35cbSAndrei Vagin
seq_put_decimal_ull(struct seq_file * m,const char * delimiter,unsigned long long num)730d1be35cbSAndrei Vagin void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
731d1be35cbSAndrei Vagin unsigned long long num)
732d1be35cbSAndrei Vagin {
733d1be35cbSAndrei Vagin return seq_put_decimal_ull_width(m, delimiter, num, 0);
734d1be35cbSAndrei Vagin }
7351ac101a5SKAMEZAWA Hiroyuki EXPORT_SYMBOL(seq_put_decimal_ull);
7361ac101a5SKAMEZAWA Hiroyuki
7370e3dc019SAndrei Vagin /**
7380e3dc019SAndrei Vagin * seq_put_hex_ll - put a number in hexadecimal notation
7390e3dc019SAndrei Vagin * @m: seq_file identifying the buffer to which data should be written
7400e3dc019SAndrei Vagin * @delimiter: a string which is printed before the number
7410e3dc019SAndrei Vagin * @v: the number
7420e3dc019SAndrei Vagin * @width: a minimum field width
7430e3dc019SAndrei Vagin *
7440e3dc019SAndrei Vagin * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
7450e3dc019SAndrei Vagin *
7460e3dc019SAndrei Vagin * This routine is very quick when you show lots of numbers.
7470e3dc019SAndrei Vagin * In usual cases, it will be better to use seq_printf(). It's easier to read.
7480e3dc019SAndrei Vagin */
seq_put_hex_ll(struct seq_file * m,const char * delimiter,unsigned long long v,unsigned int width)7490e3dc019SAndrei Vagin void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
7500e3dc019SAndrei Vagin unsigned long long v, unsigned int width)
7510e3dc019SAndrei Vagin {
7520e3dc019SAndrei Vagin unsigned int len;
7530e3dc019SAndrei Vagin int i;
7540e3dc019SAndrei Vagin
7550e3dc019SAndrei Vagin if (delimiter && delimiter[0]) {
7560e3dc019SAndrei Vagin if (delimiter[1] == 0)
7570e3dc019SAndrei Vagin seq_putc(m, delimiter[0]);
7580e3dc019SAndrei Vagin else
7590e3dc019SAndrei Vagin seq_puts(m, delimiter);
7600e3dc019SAndrei Vagin }
7610e3dc019SAndrei Vagin
7620e3dc019SAndrei Vagin /* If x is 0, the result of __builtin_clzll is undefined */
7630e3dc019SAndrei Vagin if (v == 0)
7640e3dc019SAndrei Vagin len = 1;
7650e3dc019SAndrei Vagin else
7660e3dc019SAndrei Vagin len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
7670e3dc019SAndrei Vagin
7680e3dc019SAndrei Vagin if (len < width)
7690e3dc019SAndrei Vagin len = width;
7700e3dc019SAndrei Vagin
7710e3dc019SAndrei Vagin if (m->count + len > m->size) {
7720e3dc019SAndrei Vagin seq_set_overflow(m);
7730e3dc019SAndrei Vagin return;
7740e3dc019SAndrei Vagin }
7750e3dc019SAndrei Vagin
7760e3dc019SAndrei Vagin for (i = len - 1; i >= 0; i--) {
7770e3dc019SAndrei Vagin m->buf[m->count + i] = hex_asc[0xf & v];
7780e3dc019SAndrei Vagin v = v >> 4;
7790e3dc019SAndrei Vagin }
7800e3dc019SAndrei Vagin m->count += len;
7810e3dc019SAndrei Vagin }
7820e3dc019SAndrei Vagin
seq_put_decimal_ll(struct seq_file * m,const char * delimiter,long long num)78375ba1d07SJoe Perches void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
784bda7bad6SKAMEZAWA Hiroyuki {
78575ba1d07SJoe Perches int len;
78675ba1d07SJoe Perches
78775ba1d07SJoe Perches if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
78875ba1d07SJoe Perches goto overflow;
78975ba1d07SJoe Perches
79048dffbf8SAndrei Vagin if (delimiter && delimiter[0]) {
79148dffbf8SAndrei Vagin if (delimiter[1] == 0)
79248dffbf8SAndrei Vagin seq_putc(m, delimiter[0]);
79348dffbf8SAndrei Vagin else
79448dffbf8SAndrei Vagin seq_puts(m, delimiter);
79548dffbf8SAndrei Vagin }
79675ba1d07SJoe Perches
79775ba1d07SJoe Perches if (m->count + 2 >= m->size)
79875ba1d07SJoe Perches goto overflow;
79975ba1d07SJoe Perches
800bda7bad6SKAMEZAWA Hiroyuki if (num < 0) {
80175ba1d07SJoe Perches m->buf[m->count++] = '-';
80275ba1d07SJoe Perches num = -num;
80375ba1d07SJoe Perches }
80475ba1d07SJoe Perches
80575ba1d07SJoe Perches if (num < 10) {
80675ba1d07SJoe Perches m->buf[m->count++] = num + '0';
8076798a8caSJoe Perches return;
808bda7bad6SKAMEZAWA Hiroyuki }
80975ba1d07SJoe Perches
810d1be35cbSAndrei Vagin len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
81175ba1d07SJoe Perches if (!len)
81275ba1d07SJoe Perches goto overflow;
81375ba1d07SJoe Perches
81475ba1d07SJoe Perches m->count += len;
81575ba1d07SJoe Perches return;
81675ba1d07SJoe Perches
81775ba1d07SJoe Perches overflow:
81875ba1d07SJoe Perches seq_set_overflow(m);
819bda7bad6SKAMEZAWA Hiroyuki }
820bda7bad6SKAMEZAWA Hiroyuki EXPORT_SYMBOL(seq_put_decimal_ll);
821bda7bad6SKAMEZAWA Hiroyuki
8220b923606SPeter Oberparleiter /**
8230b923606SPeter Oberparleiter * seq_write - write arbitrary data to buffer
8240b923606SPeter Oberparleiter * @seq: seq_file identifying the buffer to which data should be written
8250b923606SPeter Oberparleiter * @data: data address
8260b923606SPeter Oberparleiter * @len: number of bytes
8270b923606SPeter Oberparleiter *
8280b923606SPeter Oberparleiter * Return 0 on success, non-zero otherwise.
8290b923606SPeter Oberparleiter */
seq_write(struct seq_file * seq,const void * data,size_t len)8300b923606SPeter Oberparleiter int seq_write(struct seq_file *seq, const void *data, size_t len)
8310b923606SPeter Oberparleiter {
8320b923606SPeter Oberparleiter if (seq->count + len < seq->size) {
8330b923606SPeter Oberparleiter memcpy(seq->buf + seq->count, data, len);
8340b923606SPeter Oberparleiter seq->count += len;
8350b923606SPeter Oberparleiter return 0;
8360b923606SPeter Oberparleiter }
837e075f591SKAMEZAWA Hiroyuki seq_set_overflow(seq);
8380b923606SPeter Oberparleiter return -1;
8390b923606SPeter Oberparleiter }
8400b923606SPeter Oberparleiter EXPORT_SYMBOL(seq_write);
8410b923606SPeter Oberparleiter
842839cc2a9STetsuo Handa /**
843839cc2a9STetsuo Handa * seq_pad - write padding spaces to buffer
844839cc2a9STetsuo Handa * @m: seq_file identifying the buffer to which data should be written
845839cc2a9STetsuo Handa * @c: the byte to append after padding if non-zero
846839cc2a9STetsuo Handa */
seq_pad(struct seq_file * m,char c)847839cc2a9STetsuo Handa void seq_pad(struct seq_file *m, char c)
848839cc2a9STetsuo Handa {
849839cc2a9STetsuo Handa int size = m->pad_until - m->count;
8508cfa67b4SAndrei Vagin if (size > 0) {
8518cfa67b4SAndrei Vagin if (size + m->count > m->size) {
8528cfa67b4SAndrei Vagin seq_set_overflow(m);
8538cfa67b4SAndrei Vagin return;
8548cfa67b4SAndrei Vagin }
8558cfa67b4SAndrei Vagin memset(m->buf + m->count, ' ', size);
8568cfa67b4SAndrei Vagin m->count += size;
8578cfa67b4SAndrei Vagin }
858839cc2a9STetsuo Handa if (c)
859839cc2a9STetsuo Handa seq_putc(m, c);
860839cc2a9STetsuo Handa }
861839cc2a9STetsuo Handa EXPORT_SYMBOL(seq_pad);
862839cc2a9STetsuo Handa
86337607102SAndy Shevchenko /* A complete analogue of print_hex_dump() */
seq_hex_dump(struct seq_file * m,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)86437607102SAndy Shevchenko void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
86537607102SAndy Shevchenko int rowsize, int groupsize, const void *buf, size_t len,
86637607102SAndy Shevchenko bool ascii)
86737607102SAndy Shevchenko {
86837607102SAndy Shevchenko const u8 *ptr = buf;
86937607102SAndy Shevchenko int i, linelen, remaining = len;
8708b91a318SAndy Shevchenko char *buffer;
8718b91a318SAndy Shevchenko size_t size;
87237607102SAndy Shevchenko int ret;
87337607102SAndy Shevchenko
87437607102SAndy Shevchenko if (rowsize != 16 && rowsize != 32)
87537607102SAndy Shevchenko rowsize = 16;
87637607102SAndy Shevchenko
87737607102SAndy Shevchenko for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
87837607102SAndy Shevchenko linelen = min(remaining, rowsize);
87937607102SAndy Shevchenko remaining -= rowsize;
88037607102SAndy Shevchenko
88137607102SAndy Shevchenko switch (prefix_type) {
88237607102SAndy Shevchenko case DUMP_PREFIX_ADDRESS:
88337607102SAndy Shevchenko seq_printf(m, "%s%p: ", prefix_str, ptr + i);
88437607102SAndy Shevchenko break;
88537607102SAndy Shevchenko case DUMP_PREFIX_OFFSET:
88637607102SAndy Shevchenko seq_printf(m, "%s%.8x: ", prefix_str, i);
88737607102SAndy Shevchenko break;
88837607102SAndy Shevchenko default:
88937607102SAndy Shevchenko seq_printf(m, "%s", prefix_str);
89037607102SAndy Shevchenko break;
89137607102SAndy Shevchenko }
89237607102SAndy Shevchenko
8938b91a318SAndy Shevchenko size = seq_get_buf(m, &buffer);
89437607102SAndy Shevchenko ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
8958b91a318SAndy Shevchenko buffer, size, ascii);
8968b91a318SAndy Shevchenko seq_commit(m, ret < size ? ret : -1);
8978b91a318SAndy Shevchenko
89837607102SAndy Shevchenko seq_putc(m, '\n');
89937607102SAndy Shevchenko }
90037607102SAndy Shevchenko }
90137607102SAndy Shevchenko EXPORT_SYMBOL(seq_hex_dump);
90237607102SAndy Shevchenko
seq_list_start(struct list_head * head,loff_t pos)903bcf67e16SPavel Emelianov struct list_head *seq_list_start(struct list_head *head, loff_t pos)
904bcf67e16SPavel Emelianov {
905bcf67e16SPavel Emelianov struct list_head *lh;
906bcf67e16SPavel Emelianov
907bcf67e16SPavel Emelianov list_for_each(lh, head)
908bcf67e16SPavel Emelianov if (pos-- == 0)
909bcf67e16SPavel Emelianov return lh;
910bcf67e16SPavel Emelianov
911bcf67e16SPavel Emelianov return NULL;
912bcf67e16SPavel Emelianov }
913bcf67e16SPavel Emelianov EXPORT_SYMBOL(seq_list_start);
914bcf67e16SPavel Emelianov
seq_list_start_head(struct list_head * head,loff_t pos)915bcf67e16SPavel Emelianov struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
916bcf67e16SPavel Emelianov {
917bcf67e16SPavel Emelianov if (!pos)
918bcf67e16SPavel Emelianov return head;
919bcf67e16SPavel Emelianov
920bcf67e16SPavel Emelianov return seq_list_start(head, pos - 1);
921bcf67e16SPavel Emelianov }
922bcf67e16SPavel Emelianov EXPORT_SYMBOL(seq_list_start_head);
923bcf67e16SPavel Emelianov
seq_list_next(void * v,struct list_head * head,loff_t * ppos)924bcf67e16SPavel Emelianov struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
925bcf67e16SPavel Emelianov {
926bcf67e16SPavel Emelianov struct list_head *lh;
927bcf67e16SPavel Emelianov
928bcf67e16SPavel Emelianov lh = ((struct list_head *)v)->next;
929bcf67e16SPavel Emelianov ++*ppos;
930bcf67e16SPavel Emelianov return lh == head ? NULL : lh;
931bcf67e16SPavel Emelianov }
932bcf67e16SPavel Emelianov EXPORT_SYMBOL(seq_list_next);
93366655de6SLi Zefan
seq_list_start_rcu(struct list_head * head,loff_t pos)934ad25f5cbSDavid Howells struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos)
935ad25f5cbSDavid Howells {
936ad25f5cbSDavid Howells struct list_head *lh;
937ad25f5cbSDavid Howells
938ad25f5cbSDavid Howells list_for_each_rcu(lh, head)
939ad25f5cbSDavid Howells if (pos-- == 0)
940ad25f5cbSDavid Howells return lh;
941ad25f5cbSDavid Howells
942ad25f5cbSDavid Howells return NULL;
943ad25f5cbSDavid Howells }
944ad25f5cbSDavid Howells EXPORT_SYMBOL(seq_list_start_rcu);
945ad25f5cbSDavid Howells
seq_list_start_head_rcu(struct list_head * head,loff_t pos)946ad25f5cbSDavid Howells struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos)
947ad25f5cbSDavid Howells {
948ad25f5cbSDavid Howells if (!pos)
949ad25f5cbSDavid Howells return head;
950ad25f5cbSDavid Howells
951ad25f5cbSDavid Howells return seq_list_start_rcu(head, pos - 1);
952ad25f5cbSDavid Howells }
953ad25f5cbSDavid Howells EXPORT_SYMBOL(seq_list_start_head_rcu);
954ad25f5cbSDavid Howells
seq_list_next_rcu(void * v,struct list_head * head,loff_t * ppos)955ad25f5cbSDavid Howells struct list_head *seq_list_next_rcu(void *v, struct list_head *head,
956ad25f5cbSDavid Howells loff_t *ppos)
957ad25f5cbSDavid Howells {
958ad25f5cbSDavid Howells struct list_head *lh;
959ad25f5cbSDavid Howells
960ad25f5cbSDavid Howells lh = list_next_rcu((struct list_head *)v);
961ad25f5cbSDavid Howells ++*ppos;
962ad25f5cbSDavid Howells return lh == head ? NULL : lh;
963ad25f5cbSDavid Howells }
964ad25f5cbSDavid Howells EXPORT_SYMBOL(seq_list_next_rcu);
965ad25f5cbSDavid Howells
96666655de6SLi Zefan /**
96766655de6SLi Zefan * seq_hlist_start - start an iteration of a hlist
96866655de6SLi Zefan * @head: the head of the hlist
96966655de6SLi Zefan * @pos: the start position of the sequence
97066655de6SLi Zefan *
97166655de6SLi Zefan * Called at seq_file->op->start().
97266655de6SLi Zefan */
seq_hlist_start(struct hlist_head * head,loff_t pos)97366655de6SLi Zefan struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
97466655de6SLi Zefan {
97566655de6SLi Zefan struct hlist_node *node;
97666655de6SLi Zefan
97766655de6SLi Zefan hlist_for_each(node, head)
97866655de6SLi Zefan if (pos-- == 0)
97966655de6SLi Zefan return node;
98066655de6SLi Zefan return NULL;
98166655de6SLi Zefan }
98266655de6SLi Zefan EXPORT_SYMBOL(seq_hlist_start);
98366655de6SLi Zefan
98466655de6SLi Zefan /**
98566655de6SLi Zefan * seq_hlist_start_head - start an iteration of a hlist
98666655de6SLi Zefan * @head: the head of the hlist
98766655de6SLi Zefan * @pos: the start position of the sequence
98866655de6SLi Zefan *
98966655de6SLi Zefan * Called at seq_file->op->start(). Call this function if you want to
99066655de6SLi Zefan * print a header at the top of the output.
99166655de6SLi Zefan */
seq_hlist_start_head(struct hlist_head * head,loff_t pos)99266655de6SLi Zefan struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
99366655de6SLi Zefan {
99466655de6SLi Zefan if (!pos)
99566655de6SLi Zefan return SEQ_START_TOKEN;
99666655de6SLi Zefan
99766655de6SLi Zefan return seq_hlist_start(head, pos - 1);
99866655de6SLi Zefan }
99966655de6SLi Zefan EXPORT_SYMBOL(seq_hlist_start_head);
100066655de6SLi Zefan
100166655de6SLi Zefan /**
100266655de6SLi Zefan * seq_hlist_next - move to the next position of the hlist
100366655de6SLi Zefan * @v: the current iterator
100466655de6SLi Zefan * @head: the head of the hlist
1005138860b9SRandy Dunlap * @ppos: the current position
100666655de6SLi Zefan *
100766655de6SLi Zefan * Called at seq_file->op->next().
100866655de6SLi Zefan */
seq_hlist_next(void * v,struct hlist_head * head,loff_t * ppos)100966655de6SLi Zefan struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
101066655de6SLi Zefan loff_t *ppos)
101166655de6SLi Zefan {
101266655de6SLi Zefan struct hlist_node *node = v;
101366655de6SLi Zefan
101466655de6SLi Zefan ++*ppos;
101566655de6SLi Zefan if (v == SEQ_START_TOKEN)
101666655de6SLi Zefan return head->first;
101766655de6SLi Zefan else
101866655de6SLi Zefan return node->next;
101966655de6SLi Zefan }
102066655de6SLi Zefan EXPORT_SYMBOL(seq_hlist_next);
10211cc52327Sstephen hemminger
10221cc52327Sstephen hemminger /**
10231cc52327Sstephen hemminger * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
10241cc52327Sstephen hemminger * @head: the head of the hlist
10251cc52327Sstephen hemminger * @pos: the start position of the sequence
10261cc52327Sstephen hemminger *
10271cc52327Sstephen hemminger * Called at seq_file->op->start().
10281cc52327Sstephen hemminger *
10291cc52327Sstephen hemminger * This list-traversal primitive may safely run concurrently with
10301cc52327Sstephen hemminger * the _rcu list-mutation primitives such as hlist_add_head_rcu()
10311cc52327Sstephen hemminger * as long as the traversal is guarded by rcu_read_lock().
10321cc52327Sstephen hemminger */
seq_hlist_start_rcu(struct hlist_head * head,loff_t pos)10331cc52327Sstephen hemminger struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
10341cc52327Sstephen hemminger loff_t pos)
10351cc52327Sstephen hemminger {
10361cc52327Sstephen hemminger struct hlist_node *node;
10371cc52327Sstephen hemminger
10381cc52327Sstephen hemminger __hlist_for_each_rcu(node, head)
10391cc52327Sstephen hemminger if (pos-- == 0)
10401cc52327Sstephen hemminger return node;
10411cc52327Sstephen hemminger return NULL;
10421cc52327Sstephen hemminger }
10431cc52327Sstephen hemminger EXPORT_SYMBOL(seq_hlist_start_rcu);
10441cc52327Sstephen hemminger
10451cc52327Sstephen hemminger /**
10461cc52327Sstephen hemminger * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
10471cc52327Sstephen hemminger * @head: the head of the hlist
10481cc52327Sstephen hemminger * @pos: the start position of the sequence
10491cc52327Sstephen hemminger *
10501cc52327Sstephen hemminger * Called at seq_file->op->start(). Call this function if you want to
10511cc52327Sstephen hemminger * print a header at the top of the output.
10521cc52327Sstephen hemminger *
10531cc52327Sstephen hemminger * This list-traversal primitive may safely run concurrently with
10541cc52327Sstephen hemminger * the _rcu list-mutation primitives such as hlist_add_head_rcu()
10551cc52327Sstephen hemminger * as long as the traversal is guarded by rcu_read_lock().
10561cc52327Sstephen hemminger */
seq_hlist_start_head_rcu(struct hlist_head * head,loff_t pos)10571cc52327Sstephen hemminger struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
10581cc52327Sstephen hemminger loff_t pos)
10591cc52327Sstephen hemminger {
10601cc52327Sstephen hemminger if (!pos)
10611cc52327Sstephen hemminger return SEQ_START_TOKEN;
10621cc52327Sstephen hemminger
10631cc52327Sstephen hemminger return seq_hlist_start_rcu(head, pos - 1);
10641cc52327Sstephen hemminger }
10651cc52327Sstephen hemminger EXPORT_SYMBOL(seq_hlist_start_head_rcu);
10661cc52327Sstephen hemminger
10671cc52327Sstephen hemminger /**
10681cc52327Sstephen hemminger * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
10691cc52327Sstephen hemminger * @v: the current iterator
10701cc52327Sstephen hemminger * @head: the head of the hlist
1071138860b9SRandy Dunlap * @ppos: the current position
10721cc52327Sstephen hemminger *
10731cc52327Sstephen hemminger * Called at seq_file->op->next().
10741cc52327Sstephen hemminger *
10751cc52327Sstephen hemminger * This list-traversal primitive may safely run concurrently with
10761cc52327Sstephen hemminger * the _rcu list-mutation primitives such as hlist_add_head_rcu()
10771cc52327Sstephen hemminger * as long as the traversal is guarded by rcu_read_lock().
10781cc52327Sstephen hemminger */
seq_hlist_next_rcu(void * v,struct hlist_head * head,loff_t * ppos)10791cc52327Sstephen hemminger struct hlist_node *seq_hlist_next_rcu(void *v,
10801cc52327Sstephen hemminger struct hlist_head *head,
10811cc52327Sstephen hemminger loff_t *ppos)
10821cc52327Sstephen hemminger {
10831cc52327Sstephen hemminger struct hlist_node *node = v;
10841cc52327Sstephen hemminger
10851cc52327Sstephen hemminger ++*ppos;
10861cc52327Sstephen hemminger if (v == SEQ_START_TOKEN)
10871cc52327Sstephen hemminger return rcu_dereference(head->first);
10881cc52327Sstephen hemminger else
10891cc52327Sstephen hemminger return rcu_dereference(node->next);
10901cc52327Sstephen hemminger }
10911cc52327Sstephen hemminger EXPORT_SYMBOL(seq_hlist_next_rcu);
10920bc77381SJeff Layton
10930bc77381SJeff Layton /**
1094961f3c89SMauro Carvalho Chehab * seq_hlist_start_percpu - start an iteration of a percpu hlist array
10950bc77381SJeff Layton * @head: pointer to percpu array of struct hlist_heads
10960bc77381SJeff Layton * @cpu: pointer to cpu "cursor"
10970bc77381SJeff Layton * @pos: start position of sequence
10980bc77381SJeff Layton *
10990bc77381SJeff Layton * Called at seq_file->op->start().
11000bc77381SJeff Layton */
11010bc77381SJeff Layton struct hlist_node *
seq_hlist_start_percpu(struct hlist_head __percpu * head,int * cpu,loff_t pos)11020bc77381SJeff Layton seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
11030bc77381SJeff Layton {
11040bc77381SJeff Layton struct hlist_node *node;
11050bc77381SJeff Layton
11060bc77381SJeff Layton for_each_possible_cpu(*cpu) {
11070bc77381SJeff Layton hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
11080bc77381SJeff Layton if (pos-- == 0)
11090bc77381SJeff Layton return node;
11100bc77381SJeff Layton }
11110bc77381SJeff Layton }
11120bc77381SJeff Layton return NULL;
11130bc77381SJeff Layton }
11140bc77381SJeff Layton EXPORT_SYMBOL(seq_hlist_start_percpu);
11150bc77381SJeff Layton
11160bc77381SJeff Layton /**
11170bc77381SJeff Layton * seq_hlist_next_percpu - move to the next position of the percpu hlist array
11180bc77381SJeff Layton * @v: pointer to current hlist_node
11190bc77381SJeff Layton * @head: pointer to percpu array of struct hlist_heads
11200bc77381SJeff Layton * @cpu: pointer to cpu "cursor"
11210bc77381SJeff Layton * @pos: start position of sequence
11220bc77381SJeff Layton *
11230bc77381SJeff Layton * Called at seq_file->op->next().
11240bc77381SJeff Layton */
11250bc77381SJeff Layton struct hlist_node *
seq_hlist_next_percpu(void * v,struct hlist_head __percpu * head,int * cpu,loff_t * pos)11260bc77381SJeff Layton seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
11270bc77381SJeff Layton int *cpu, loff_t *pos)
11280bc77381SJeff Layton {
11290bc77381SJeff Layton struct hlist_node *node = v;
11300bc77381SJeff Layton
11310bc77381SJeff Layton ++*pos;
11320bc77381SJeff Layton
11330bc77381SJeff Layton if (node->next)
11340bc77381SJeff Layton return node->next;
11350bc77381SJeff Layton
11360bc77381SJeff Layton for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
11370bc77381SJeff Layton *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
11380bc77381SJeff Layton struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
11390bc77381SJeff Layton
11400bc77381SJeff Layton if (!hlist_empty(bucket))
11410bc77381SJeff Layton return bucket->first;
11420bc77381SJeff Layton }
11430bc77381SJeff Layton return NULL;
11440bc77381SJeff Layton }
11450bc77381SJeff Layton EXPORT_SYMBOL(seq_hlist_next_percpu);
114609652320SAlexey Dobriyan
seq_file_init(void)114709652320SAlexey Dobriyan void __init seq_file_init(void)
114809652320SAlexey Dobriyan {
1149d64d01a1SAlexey Dobriyan seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
115009652320SAlexey Dobriyan }
1151