blob: 4a2cda04d3e293b523549f72f535a0ddb9f5c631 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Joe Perchesa3963012020-06-04 16:51:02 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Alexey Dobriyan0965232032018-04-10 16:34:45 -070011#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/fs.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/seq_file.h>
Heiko Carstens058504e2014-07-02 15:22:37 -070015#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060017#include <linux/cred.h>
Heiko Carstens058504e2014-07-02 15:22:37 -070018#include <linux/mm.h>
Andy Shevchenko37607102015-09-09 15:38:33 -070019#include <linux/printk.h>
Andy Shevchenko25c6bb72015-11-06 16:32:40 -080020#include <linux/string_helpers.h>
Christoph Hellwigd4d50712020-11-04 09:27:33 +010021#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/page.h>
25
Alexey Dobriyan0965232032018-04-10 16:34:45 -070026static struct kmem_cache *seq_file_cache __ro_after_init;
27
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070028static void seq_set_overflow(struct seq_file *m)
29{
30 m->count = m->size;
31}
32
Heiko Carstens058504e2014-07-02 15:22:37 -070033static void *seq_buf_alloc(unsigned long size)
34{
Eric Sandeen8cae8cd2021-07-13 17:49:23 +020035 if (unlikely(size > MAX_RW_COUNT))
36 return NULL;
37
Alexey Dobriyand64d01a2018-04-10 16:34:49 -070038 return kvmalloc(size, GFP_KERNEL_ACCOUNT);
Heiko Carstens058504e2014-07-02 15:22:37 -070039}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/**
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.
Al Viro521b5d02008-03-28 00:46:41 -040053 * Returning SEQ_SKIP means "discard this element and move on".
Yann Droneaud460b8652015-06-30 14:57:36 -070054 * Note: seq_open() will allocate a struct seq_file and store its
55 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080057int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Yann Droneaud189f9842015-06-30 14:57:33 -070059 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050060
Yann Droneaud189f9842015-06-30 14:57:33 -070061 WARN_ON(file->private_data);
62
Alexey Dobriyan0965232032018-04-10 16:34:45 -070063 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
Yann Droneaud189f9842015-06-30 14:57:33 -070064 if (!p)
65 return -ENOMEM;
66
67 file->private_data = p;
68
Ingo Molnar0ac17592006-03-23 03:00:37 -080069 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 p->op = op;
Linus Torvalds34dbbcd2016-04-14 11:22:00 -070071
72 // No refcounting: the lifetime of 'p' is constrained
73 // to the lifetime of the file.
74 p->file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /*
Eric Biederman8f19d472009-02-18 14:48:16 -080077 * 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88EXPORT_SYMBOL(seq_open);
89
Eric Biederman33da8892009-02-04 15:12:25 -080090static int traverse(struct seq_file *m, loff_t offset)
91{
NeilBrown1f4aace2018-08-17 15:44:41 -070092 loff_t pos = 0;
Eric Biederman33da8892009-02-04 15:12:25 -080093 int error = 0;
94 void *p;
95
NeilBrown1f4aace2018-08-17 15:44:41 -070096 m->index = 0;
Eric Biederman33da8892009-02-04 15:12:25 -080097 m->count = m->from = 0;
NeilBrown1f4aace2018-08-17 15:44:41 -070098 if (!offset)
Eric Biederman33da8892009-02-04 15:12:25 -080099 return 0;
NeilBrown1f4aace2018-08-17 15:44:41 -0700100
Eric Biederman33da8892009-02-04 15:12:25 -0800101 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700102 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800103 if (!m->buf)
104 return -ENOMEM;
105 }
NeilBrown1f4aace2018-08-17 15:44:41 -0700106 p = m->op->start(m, &m->index);
Eric Biederman33da8892009-02-04 15:12:25 -0800107 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 }
Joe Perches1f33c412014-09-29 16:08:21 -0700118 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800119 goto Eoverflow;
NeilBrown6a2aeab2019-08-13 15:37:44 -0700120 p = m->op->next(m, p, &m->index);
Eric Biederman33da8892009-02-04 15:12:25 -0800121 if (pos + m->count > offset) {
122 m->from = offset - pos;
123 m->count -= m->from;
Eric Biederman33da8892009-02-04 15:12:25 -0800124 break;
125 }
126 pos += m->count;
127 m->count = 0;
NeilBrown1f4aace2018-08-17 15:44:41 -0700128 if (pos == offset)
Eric Biederman33da8892009-02-04 15:12:25 -0800129 break;
Eric Biederman33da8892009-02-04 15:12:25 -0800130 }
131 m->op->stop(m, p);
132 return error;
133
134Eoverflow:
135 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700136 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000137 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700138 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800139 return !m->buf ? -ENOMEM : -EAGAIN;
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/**
143 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700144 * @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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
149 * Ready-made ->f_op->read()
150 */
151ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
152{
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100153 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);
159 iov_iter_init(&iter, READ, &iov, 1, size);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 size_t n;
176 void *p;
177 int err = 0;
178
Al Viro4bbf4392020-11-12 14:40:37 -0500179 if (!iov_iter_count(iter))
180 return 0;
181
Ingo Molnar0ac17592006-03-23 03:00:37 -0800182 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100185 * 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 */
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100188 if (iocb->ki_pos == 0) {
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100189 m->index = 0;
Miklos Szeredicf5eeba2017-11-15 11:34:58 +0100190 m->count = 0;
191 }
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100192
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100193 /* 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)
Earl Chew7904ac82012-03-21 16:33:43 -0700196 ;
197 if (err) {
198 /* With prejudice... */
199 m->read_pos = 0;
Earl Chew7904ac82012-03-21 16:33:43 -0700200 m->index = 0;
201 m->count = 0;
202 goto Done;
203 } else {
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100204 m->read_pos = iocb->ki_pos;
Earl Chew7904ac82012-03-21 16:33:43 -0700205 }
206 }
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* grab buffer if we didn't have one */
209 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700210 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!m->buf)
212 goto Enomem;
213 }
Al Viro4bbf4392020-11-12 14:40:37 -0500214 // something left in the buffer - copy it out first
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (m->count) {
Al Viro4bbf4392020-11-12 14:40:37 -0500216 n = copy_to_iter(m->buf + m->from, m->count, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 m->count -= n;
218 m->from += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 copied += n;
Al Viro4bbf4392020-11-12 14:40:37 -0500220 if (m->count) // hadn't managed to copy everything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto Done;
222 }
Al Viro4bbf4392020-11-12 14:40:37 -0500223 // get a non-empty record in the buffer
NeilBrown1f4aace2018-08-17 15:44:41 -0700224 m->from = 0;
225 p = m->op->start(m, &m->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 err = PTR_ERR(p);
Al Viro4bbf4392020-11-12 14:40:37 -0500228 if (!p || IS_ERR(p)) // EOF or an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230 err = m->op->show(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500231 if (err < 0) // hard error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
Al Viro4bbf4392020-11-12 14:40:37 -0500233 if (unlikely(err)) // ->show() says "skip it"
Al Viro521b5d02008-03-28 00:46:41 -0400234 m->count = 0;
Al Viro4bbf4392020-11-12 14:40:37 -0500235 if (unlikely(!m->count)) { // empty record
NeilBrown1f4aace2018-08-17 15:44:41 -0700236 p = m->op->next(m, p, &m->index);
Al Viro4cdfe842008-08-24 07:45:33 -0400237 continue;
238 }
Al Viro4bbf4392020-11-12 14:40:37 -0500239 if (!seq_has_overflowed(m)) // got it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto Fill;
Al Viro4bbf4392020-11-12 14:40:37 -0500241 // need a bigger buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700243 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000244 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700245 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!m->buf)
247 goto Enomem;
NeilBrown1f4aace2018-08-17 15:44:41 -0700248 p = m->op->start(m, &m->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Al Viro4bbf4392020-11-12 14:40:37 -0500250 // EOF or an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 m->op->stop(m, p);
252 m->count = 0;
253 goto Done;
254Fill:
Al Viro4bbf4392020-11-12 14:40:37 -0500255 // 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.
NeilBrown1f4aace2018-08-17 15:44:41 -0700258 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 size_t offs = m->count;
NeilBrown1f4aace2018-08-17 15:44:41 -0700260 loff_t pos = m->index;
261
262 p = m->op->next(m, p, &m->index);
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700263 if (pos == m->index) {
Joe Perchesa3963012020-06-04 16:51:02 -0700264 pr_info_ratelimited("buggy .next function %ps did not update position index\n",
265 m->op->next);
NeilBrown1f4aace2018-08-17 15:44:41 -0700266 m->index++;
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700267 }
Al Viro4bbf4392020-11-12 14:40:37 -0500268 if (!p || IS_ERR(p)) // no next record for us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
Al Viro4bbf4392020-11-12 14:40:37 -0500270 if (m->count >= iov_iter_count(iter))
NeilBrown1f4aace2018-08-17 15:44:41 -0700271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 err = m->op->show(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500273 if (err > 0) { // ->show() says "skip it"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 m->count = offs;
Al Viro4bbf4392020-11-12 14:40:37 -0500275 } else if (err || seq_has_overflowed(m)) {
276 m->count = offs;
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 m->op->stop(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500281 n = copy_to_iter(m->buf, m->count, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 copied += n;
283 m->count -= n;
NeilBrown1f4aace2018-08-17 15:44:41 -0700284 m->from = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285Done:
Al Viro4bbf4392020-11-12 14:40:37 -0500286 if (unlikely(!copied)) {
287 copied = m->count ? -EFAULT : err;
288 } else {
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100289 iocb->ki_pos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800290 m->read_pos += copied;
291 }
Ingo Molnar0ac17592006-03-23 03:00:37 -0800292 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return copied;
294Enomem:
295 err = -ENOMEM;
296 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100298EXPORT_SYMBOL(seq_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/**
301 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700302 * @file: the file in question
303 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800304 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 *
306 * Ready-made ->f_op->llseek()
307 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800308loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Joe Perches8209e2f2010-09-04 18:52:49 -0700310 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200311 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ingo Molnar0ac17592006-03-23 03:00:37 -0800313 mutex_lock(&m->lock);
Andrew Morton965c8e52012-12-17 15:59:39 -0800314 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800315 case SEEK_CUR:
316 offset += file->f_pos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500317 fallthrough;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800318 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;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800329 m->index = 0;
330 m->count = 0;
331 } else {
332 m->read_pos = offset;
333 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Gu Zheng05e16742013-10-25 18:15:06 +0800335 } else {
336 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700339 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return retval;
341}
342EXPORT_SYMBOL(seq_lseek);
343
344/**
345 * seq_release - free the structures associated with sequential file.
346 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500347 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
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{
Joe Perches8209e2f2010-09-04 18:52:49 -0700354 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700355 kvfree(m->buf);
Alexey Dobriyan0965232032018-04-10 16:34:45 -0700356 kmem_cache_free(seq_file_cache, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359EXPORT_SYMBOL(seq_release);
360
361/**
Andy Shevchenko1d31aa12021-06-30 18:55:34 -0700362 * 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
386/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * seq_escape - print string into buffer, escaping some characters
388 * @m: target buffer
389 * @s: string
390 * @esc: set of characters that need escaping
391 *
392 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700393 * @esc with usual octal escape.
394 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700396void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Andy Shevchenkofc3de022021-06-30 18:55:40 -0700398 seq_escape_str(m, s, ESCAPE_OCTAL, esc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400EXPORT_SYMBOL(seq_escape);
401
Joe Perches6798a8c2015-09-11 13:07:48 -0700402void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int len;
405
406 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (m->count + len < m->size) {
409 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700413 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Steven Whitehousea4808142012-06-11 13:16:35 +0100415EXPORT_SYMBOL(seq_vprintf);
416
Joe Perches6798a8c2015-09-11 13:07:48 -0700417void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100418{
Steven Whitehousea4808142012-06-11 13:16:35 +0100419 va_list args;
420
421 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700422 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100423 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100424}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425EXPORT_SYMBOL(seq_printf);
426
Florent Revest76d6a132021-04-27 19:43:12 +0200427#ifdef CONFIG_BINARY_PRINTF
428void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
429{
430 int len;
431
432 if (m->count < m->size) {
433 len = bstr_printf(m->buf + m->count, m->size - m->count, f,
434 binary);
435 if (m->count + len < m->size) {
436 m->count += len;
437 return;
438 }
439 }
440 seq_set_overflow(m);
441}
442EXPORT_SYMBOL(seq_bprintf);
443#endif /* CONFIG_BINARY_PRINTF */
444
Török Edwin74e2f332008-11-22 13:28:48 +0200445/**
Török Edwin958086d2008-11-23 23:24:53 +0200446 * mangle_path - mangle and copy path to buffer beginning
447 * @s: buffer start
448 * @p: beginning of path in above buffer
449 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200450 *
451 * Copy the path from @p to @s, replacing each occurrence of character from
452 * @esc with usual octal escape.
453 * Returns pointer past last written character in @s, or NULL in case of
454 * failure.
455 */
Al Viro8c9379e2011-12-08 20:18:57 -0500456char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100457{
458 while (s <= p) {
459 char c = *p++;
460 if (!c) {
461 return s;
462 } else if (!strchr(esc, c)) {
463 *s++ = c;
464 } else if (s + 4 > p) {
465 break;
466 } else {
467 *s++ = '\\';
468 *s++ = '0' + ((c & 0300) >> 6);
469 *s++ = '0' + ((c & 070) >> 3);
470 *s++ = '0' + (c & 07);
471 }
472 }
473 return NULL;
474}
Ingo Molnar604094f2008-11-28 18:03:22 +0100475EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100476
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800477/**
478 * seq_path - seq_file interface to print a pathname
479 * @m: the seq_file handle
480 * @path: the struct path to print
481 * @esc: set of characters to escape in the output
482 *
483 * return the absolute path of 'path', as represented by the
484 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100485 */
Al Viro8c9379e2011-12-08 20:18:57 -0500486int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Miklos Szeredif8439802009-09-21 14:48:36 +0200488 char *buf;
489 size_t size = seq_get_buf(m, &buf);
490 int res = -1;
491
492 if (size) {
493 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200495 char *end = mangle_path(buf, p, esc);
496 if (end)
497 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200500 seq_commit(m, res);
501
502 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504EXPORT_SYMBOL(seq_path);
505
Miklos Szeredi2726d562015-06-19 10:30:28 +0200506/**
507 * seq_file_path - seq_file interface to print a pathname of a file
508 * @m: the seq_file handle
509 * @file: the struct file to print
510 * @esc: set of characters to escape in the output
511 *
512 * return the absolute path to the file.
513 */
514int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
515{
516 return seq_path(m, &file->f_path, esc);
517}
518EXPORT_SYMBOL(seq_file_path);
519
Ram Pai6092d042008-03-27 13:06:20 +0100520/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100521 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100522 */
Al Viro8c9379e2011-12-08 20:18:57 -0500523int seq_path_root(struct seq_file *m, const struct path *path,
524 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100525{
Miklos Szeredif8439802009-09-21 14:48:36 +0200526 char *buf;
527 size_t size = seq_get_buf(m, &buf);
528 int res = -ENAMETOOLONG;
529
530 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100531 char *p;
532
Miklos Szeredif8439802009-09-21 14:48:36 +0200533 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500534 if (!p)
535 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200536 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100537 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200538 char *end = mangle_path(buf, p, esc);
539 if (end)
540 res = end - buf;
541 else
542 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100543 }
544 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200545 seq_commit(m, res);
546
Al Viro02125a82011-12-05 08:43:34 -0500547 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100548}
549
550/*
Ram Pai6092d042008-03-27 13:06:20 +0100551 * returns the path of the 'dentry' from the root of its filesystem.
552 */
Al Viro8c9379e2011-12-08 20:18:57 -0500553int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100554{
Miklos Szeredif8439802009-09-21 14:48:36 +0200555 char *buf;
556 size_t size = seq_get_buf(m, &buf);
557 int res = -1;
558
559 if (size) {
560 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100561 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200562 char *end = mangle_path(buf, p, esc);
563 if (end)
564 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100565 }
566 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200567 seq_commit(m, res);
568
569 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100570}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700571EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static void *single_start(struct seq_file *p, loff_t *pos)
574{
575 return NULL + (*pos == 0);
576}
577
578static void *single_next(struct seq_file *p, void *v, loff_t *pos)
579{
580 ++*pos;
581 return NULL;
582}
583
584static void single_stop(struct seq_file *p, void *v)
585{
586}
587
588int single_open(struct file *file, int (*show)(struct seq_file *, void *),
589 void *data)
590{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700591 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int res = -ENOMEM;
593
594 if (op) {
595 op->start = single_start;
596 op->next = single_next;
597 op->stop = single_stop;
598 op->show = show;
599 res = seq_open(file, op);
600 if (!res)
601 ((struct seq_file *)file->private_data)->private = data;
602 else
603 kfree(op);
604 }
605 return res;
606}
607EXPORT_SYMBOL(single_open);
608
Al Viro2043f492013-03-31 13:43:23 -0400609int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
610 void *data, size_t size)
611{
Heiko Carstens058504e2014-07-02 15:22:37 -0700612 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400613 int ret;
614 if (!buf)
615 return -ENOMEM;
616 ret = single_open(file, show, data);
617 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700618 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400619 return ret;
620 }
621 ((struct seq_file *)file->private_data)->buf = buf;
622 ((struct seq_file *)file->private_data)->size = size;
623 return 0;
624}
625EXPORT_SYMBOL(single_open_size);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627int single_release(struct inode *inode, struct file *file)
628{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800629 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int res = seq_release(inode, file);
631 kfree(op);
632 return res;
633}
634EXPORT_SYMBOL(single_release);
635
636int seq_release_private(struct inode *inode, struct file *file)
637{
638 struct seq_file *seq = file->private_data;
639
640 kfree(seq->private);
641 seq->private = NULL;
642 return seq_release(inode, file);
643}
644EXPORT_SYMBOL(seq_release_private);
645
Pavel Emelyanov39699032007-10-10 02:28:42 -0700646void *__seq_open_private(struct file *f, const struct seq_operations *ops,
647 int psize)
648{
649 int rc;
650 void *private;
651 struct seq_file *seq;
652
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700653 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
Pavel Emelyanov39699032007-10-10 02:28:42 -0700654 if (private == NULL)
655 goto out;
656
657 rc = seq_open(f, ops);
658 if (rc < 0)
659 goto out_free;
660
661 seq = f->private_data;
662 seq->private = private;
663 return private;
664
665out_free:
666 kfree(private);
667out:
668 return NULL;
669}
670EXPORT_SYMBOL(__seq_open_private);
671
672int seq_open_private(struct file *filp, const struct seq_operations *ops,
673 int psize)
674{
675 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
676}
677EXPORT_SYMBOL(seq_open_private);
678
Joe Perches6798a8c2015-09-11 13:07:48 -0700679void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Joe Perches6798a8c2015-09-11 13:07:48 -0700681 if (m->count >= m->size)
682 return;
683
684 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686EXPORT_SYMBOL(seq_putc);
687
Joe Perches6798a8c2015-09-11 13:07:48 -0700688void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700691
692 if (m->count + len >= m->size) {
693 seq_set_overflow(m);
694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700696 memcpy(m->buf + m->count, s, len);
697 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700700
Andrei Vagind1be35c2018-04-10 16:31:16 -0700701/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +0100702 * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
703 * without rich format of printf().
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700704 * only 'unsigned long long' is supported.
Andrei Vagind1be35c2018-04-10 16:31:16 -0700705 * @m: seq_file identifying the buffer to which data should be written
706 * @delimiter: a string which is printed before the number
707 * @num: the number
708 * @width: a minimum field width
709 *
710 * This routine will put strlen(delimiter) + number into seq_filed.
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700711 * This routine is very quick when you show lots of numbers.
712 * In usual cases, it will be better to use seq_printf(). It's easier to read.
713 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700714void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
715 unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700716{
717 int len;
718
719 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
720 goto overflow;
721
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700722 if (delimiter && delimiter[0]) {
723 if (delimiter[1] == 0)
724 seq_putc(m, delimiter[0]);
725 else
726 seq_puts(m, delimiter);
727 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700728
Andrei Vagind1be35c2018-04-10 16:31:16 -0700729 if (!width)
730 width = 1;
731
732 if (m->count + width >= m->size)
Joe Perches75ba1d02016-10-07 17:02:20 -0700733 goto overflow;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700734
Andrei Vagind1be35c2018-04-10 16:31:16 -0700735 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700736 if (!len)
737 goto overflow;
Joe Perches75ba1d02016-10-07 17:02:20 -0700738
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700739 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700740 return;
741
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700742overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700743 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700744}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700745
746void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
747 unsigned long long num)
748{
749 return seq_put_decimal_ull_width(m, delimiter, num, 0);
750}
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700751EXPORT_SYMBOL(seq_put_decimal_ull);
752
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700753/**
754 * seq_put_hex_ll - put a number in hexadecimal notation
755 * @m: seq_file identifying the buffer to which data should be written
756 * @delimiter: a string which is printed before the number
757 * @v: the number
758 * @width: a minimum field width
759 *
760 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
761 *
762 * This routine is very quick when you show lots of numbers.
763 * In usual cases, it will be better to use seq_printf(). It's easier to read.
764 */
765void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
766 unsigned long long v, unsigned int width)
767{
768 unsigned int len;
769 int i;
770
771 if (delimiter && delimiter[0]) {
772 if (delimiter[1] == 0)
773 seq_putc(m, delimiter[0]);
774 else
775 seq_puts(m, delimiter);
776 }
777
778 /* If x is 0, the result of __builtin_clzll is undefined */
779 if (v == 0)
780 len = 1;
781 else
782 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
783
784 if (len < width)
785 len = width;
786
787 if (m->count + len > m->size) {
788 seq_set_overflow(m);
789 return;
790 }
791
792 for (i = len - 1; i >= 0; i--) {
793 m->buf[m->count + i] = hex_asc[0xf & v];
794 v = v >> 4;
795 }
796 m->count += len;
797}
798
Joe Perches75ba1d02016-10-07 17:02:20 -0700799void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700800{
Joe Perches75ba1d02016-10-07 17:02:20 -0700801 int len;
802
803 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
804 goto overflow;
805
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700806 if (delimiter && delimiter[0]) {
807 if (delimiter[1] == 0)
808 seq_putc(m, delimiter[0]);
809 else
810 seq_puts(m, delimiter);
811 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700812
813 if (m->count + 2 >= m->size)
814 goto overflow;
815
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700816 if (num < 0) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700817 m->buf[m->count++] = '-';
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700818 num = -num;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700819 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700820
821 if (num < 10) {
822 m->buf[m->count++] = num + '0';
823 return;
824 }
825
Andrei Vagind1be35c2018-04-10 16:31:16 -0700826 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
Joe Perches75ba1d02016-10-07 17:02:20 -0700827 if (!len)
828 goto overflow;
829
830 m->count += len;
831 return;
832
833overflow:
834 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700835}
836EXPORT_SYMBOL(seq_put_decimal_ll);
837
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700838/**
839 * seq_write - write arbitrary data to buffer
840 * @seq: seq_file identifying the buffer to which data should be written
841 * @data: data address
842 * @len: number of bytes
843 *
844 * Return 0 on success, non-zero otherwise.
845 */
846int seq_write(struct seq_file *seq, const void *data, size_t len)
847{
848 if (seq->count + len < seq->size) {
849 memcpy(seq->buf + seq->count, data, len);
850 seq->count += len;
851 return 0;
852 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700853 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700854 return -1;
855}
856EXPORT_SYMBOL(seq_write);
857
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800858/**
859 * seq_pad - write padding spaces to buffer
860 * @m: seq_file identifying the buffer to which data should be written
861 * @c: the byte to append after padding if non-zero
862 */
863void seq_pad(struct seq_file *m, char c)
864{
865 int size = m->pad_until - m->count;
Andrei Vagin8cfa67b42018-04-10 16:30:47 -0700866 if (size > 0) {
867 if (size + m->count > m->size) {
868 seq_set_overflow(m);
869 return;
870 }
871 memset(m->buf + m->count, ' ', size);
872 m->count += size;
873 }
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800874 if (c)
875 seq_putc(m, c);
876}
877EXPORT_SYMBOL(seq_pad);
878
Andy Shevchenko37607102015-09-09 15:38:33 -0700879/* A complete analogue of print_hex_dump() */
880void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
881 int rowsize, int groupsize, const void *buf, size_t len,
882 bool ascii)
883{
884 const u8 *ptr = buf;
885 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800886 char *buffer;
887 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700888 int ret;
889
890 if (rowsize != 16 && rowsize != 32)
891 rowsize = 16;
892
893 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
894 linelen = min(remaining, rowsize);
895 remaining -= rowsize;
896
897 switch (prefix_type) {
898 case DUMP_PREFIX_ADDRESS:
899 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
900 break;
901 case DUMP_PREFIX_OFFSET:
902 seq_printf(m, "%s%.8x: ", prefix_str, i);
903 break;
904 default:
905 seq_printf(m, "%s", prefix_str);
906 break;
907 }
908
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800909 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700910 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800911 buffer, size, ascii);
912 seq_commit(m, ret < size ? ret : -1);
913
914 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700915 }
916}
917EXPORT_SYMBOL(seq_hex_dump);
918
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700919struct list_head *seq_list_start(struct list_head *head, loff_t pos)
920{
921 struct list_head *lh;
922
923 list_for_each(lh, head)
924 if (pos-- == 0)
925 return lh;
926
927 return NULL;
928}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700929EXPORT_SYMBOL(seq_list_start);
930
931struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
932{
933 if (!pos)
934 return head;
935
936 return seq_list_start(head, pos - 1);
937}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700938EXPORT_SYMBOL(seq_list_start_head);
939
940struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
941{
942 struct list_head *lh;
943
944 lh = ((struct list_head *)v)->next;
945 ++*ppos;
946 return lh == head ? NULL : lh;
947}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700948EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000949
950/**
951 * seq_hlist_start - start an iteration of a hlist
952 * @head: the head of the hlist
953 * @pos: the start position of the sequence
954 *
955 * Called at seq_file->op->start().
956 */
957struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
958{
959 struct hlist_node *node;
960
961 hlist_for_each(node, head)
962 if (pos-- == 0)
963 return node;
964 return NULL;
965}
966EXPORT_SYMBOL(seq_hlist_start);
967
968/**
969 * seq_hlist_start_head - start an iteration of a hlist
970 * @head: the head of the hlist
971 * @pos: the start position of the sequence
972 *
973 * Called at seq_file->op->start(). Call this function if you want to
974 * print a header at the top of the output.
975 */
976struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
977{
978 if (!pos)
979 return SEQ_START_TOKEN;
980
981 return seq_hlist_start(head, pos - 1);
982}
983EXPORT_SYMBOL(seq_hlist_start_head);
984
985/**
986 * seq_hlist_next - move to the next position of the hlist
987 * @v: the current iterator
988 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800989 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000990 *
991 * Called at seq_file->op->next().
992 */
993struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
994 loff_t *ppos)
995{
996 struct hlist_node *node = v;
997
998 ++*ppos;
999 if (v == SEQ_START_TOKEN)
1000 return head->first;
1001 else
1002 return node->next;
1003}
1004EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +00001005
1006/**
1007 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
1008 * @head: the head of the hlist
1009 * @pos: the start position of the sequence
1010 *
1011 * Called at seq_file->op->start().
1012 *
1013 * This list-traversal primitive may safely run concurrently with
1014 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1015 * as long as the traversal is guarded by rcu_read_lock().
1016 */
1017struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
1018 loff_t pos)
1019{
1020 struct hlist_node *node;
1021
1022 __hlist_for_each_rcu(node, head)
1023 if (pos-- == 0)
1024 return node;
1025 return NULL;
1026}
1027EXPORT_SYMBOL(seq_hlist_start_rcu);
1028
1029/**
1030 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1031 * @head: the head of the hlist
1032 * @pos: the start position of the sequence
1033 *
1034 * Called at seq_file->op->start(). Call this function if you want to
1035 * print a header at the top of the output.
1036 *
1037 * This list-traversal primitive may safely run concurrently with
1038 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1039 * as long as the traversal is guarded by rcu_read_lock().
1040 */
1041struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1042 loff_t pos)
1043{
1044 if (!pos)
1045 return SEQ_START_TOKEN;
1046
1047 return seq_hlist_start_rcu(head, pos - 1);
1048}
1049EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1050
1051/**
1052 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1053 * @v: the current iterator
1054 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -08001055 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +00001056 *
1057 * Called at seq_file->op->next().
1058 *
1059 * This list-traversal primitive may safely run concurrently with
1060 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1061 * as long as the traversal is guarded by rcu_read_lock().
1062 */
1063struct hlist_node *seq_hlist_next_rcu(void *v,
1064 struct hlist_head *head,
1065 loff_t *ppos)
1066{
1067 struct hlist_node *node = v;
1068
1069 ++*ppos;
1070 if (v == SEQ_START_TOKEN)
1071 return rcu_dereference(head->first);
1072 else
1073 return rcu_dereference(node->next);
1074}
1075EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -04001076
1077/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001078 * seq_hlist_start_percpu - start an iteration of a percpu hlist array
Jeff Layton0bc77382013-06-21 08:58:21 -04001079 * @head: pointer to percpu array of struct hlist_heads
1080 * @cpu: pointer to cpu "cursor"
1081 * @pos: start position of sequence
1082 *
1083 * Called at seq_file->op->start().
1084 */
1085struct hlist_node *
1086seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1087{
1088 struct hlist_node *node;
1089
1090 for_each_possible_cpu(*cpu) {
1091 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1092 if (pos-- == 0)
1093 return node;
1094 }
1095 }
1096 return NULL;
1097}
1098EXPORT_SYMBOL(seq_hlist_start_percpu);
1099
1100/**
1101 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1102 * @v: pointer to current hlist_node
1103 * @head: pointer to percpu array of struct hlist_heads
1104 * @cpu: pointer to cpu "cursor"
1105 * @pos: start position of sequence
1106 *
1107 * Called at seq_file->op->next().
1108 */
1109struct hlist_node *
1110seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1111 int *cpu, loff_t *pos)
1112{
1113 struct hlist_node *node = v;
1114
1115 ++*pos;
1116
1117 if (node->next)
1118 return node->next;
1119
1120 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1121 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1122 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1123
1124 if (!hlist_empty(bucket))
1125 return bucket->first;
1126 }
1127 return NULL;
1128}
1129EXPORT_SYMBOL(seq_hlist_next_percpu);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001130
1131void __init seq_file_init(void)
1132{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -07001133 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001134}