blob: 5059248f2d648c18c2d22c02cc14ab66bbe985dc [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{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -070035 return kvmalloc(size, GFP_KERNEL_ACCOUNT);
Heiko Carstens058504e2014-07-02 15:22:37 -070036}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/**
39 * seq_open - initialize sequential file
40 * @file: file we initialize
41 * @op: method table describing the sequence
42 *
43 * seq_open() sets @file, associating it with a sequence described
44 * by @op. @op->start() sets the iterator up and returns the first
45 * element of sequence. @op->stop() shuts it down. @op->next()
46 * returns the next element of sequence. @op->show() prints element
47 * into the buffer. In case of error ->start() and ->next() return
48 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
49 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040050 * Returning SEQ_SKIP means "discard this element and move on".
Yann Droneaud460b8652015-06-30 14:57:36 -070051 * Note: seq_open() will allocate a struct seq_file and store its
52 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080054int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Yann Droneaud189f9842015-06-30 14:57:33 -070056 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050057
Yann Droneaud189f9842015-06-30 14:57:33 -070058 WARN_ON(file->private_data);
59
Alexey Dobriyan0965232032018-04-10 16:34:45 -070060 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
Yann Droneaud189f9842015-06-30 14:57:33 -070061 if (!p)
62 return -ENOMEM;
63
64 file->private_data = p;
65
Ingo Molnar0ac17592006-03-23 03:00:37 -080066 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 p->op = op;
Linus Torvalds34dbbcd2016-04-14 11:22:00 -070068
69 // No refcounting: the lifetime of 'p' is constrained
70 // to the lifetime of the file.
71 p->file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /*
Eric Biederman8f19d472009-02-18 14:48:16 -080074 * seq_files support lseek() and pread(). They do not implement
75 * write() at all, but we clear FMODE_PWRITE here for historical
76 * reasons.
77 *
78 * If a client of seq_files a) implements file.write() and b) wishes to
79 * support pwrite() then that client will need to implement its own
80 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
81 */
82 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84}
85EXPORT_SYMBOL(seq_open);
86
Eric Biederman33da8892009-02-04 15:12:25 -080087static int traverse(struct seq_file *m, loff_t offset)
88{
NeilBrown1f4aace2018-08-17 15:44:41 -070089 loff_t pos = 0;
Eric Biederman33da8892009-02-04 15:12:25 -080090 int error = 0;
91 void *p;
92
NeilBrown1f4aace2018-08-17 15:44:41 -070093 m->index = 0;
Eric Biederman33da8892009-02-04 15:12:25 -080094 m->count = m->from = 0;
NeilBrown1f4aace2018-08-17 15:44:41 -070095 if (!offset)
Eric Biederman33da8892009-02-04 15:12:25 -080096 return 0;
NeilBrown1f4aace2018-08-17 15:44:41 -070097
Eric Biederman33da8892009-02-04 15:12:25 -080098 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -070099 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800100 if (!m->buf)
101 return -ENOMEM;
102 }
NeilBrown1f4aace2018-08-17 15:44:41 -0700103 p = m->op->start(m, &m->index);
Eric Biederman33da8892009-02-04 15:12:25 -0800104 while (p) {
105 error = PTR_ERR(p);
106 if (IS_ERR(p))
107 break;
108 error = m->op->show(m, p);
109 if (error < 0)
110 break;
111 if (unlikely(error)) {
112 error = 0;
113 m->count = 0;
114 }
Joe Perches1f33c412014-09-29 16:08:21 -0700115 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800116 goto Eoverflow;
NeilBrown6a2aeab2019-08-13 15:37:44 -0700117 p = m->op->next(m, p, &m->index);
Eric Biederman33da8892009-02-04 15:12:25 -0800118 if (pos + m->count > offset) {
119 m->from = offset - pos;
120 m->count -= m->from;
Eric Biederman33da8892009-02-04 15:12:25 -0800121 break;
122 }
123 pos += m->count;
124 m->count = 0;
NeilBrown1f4aace2018-08-17 15:44:41 -0700125 if (pos == offset)
Eric Biederman33da8892009-02-04 15:12:25 -0800126 break;
Eric Biederman33da8892009-02-04 15:12:25 -0800127 }
128 m->op->stop(m, p);
129 return error;
130
131Eoverflow:
132 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700133 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000134 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700135 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800136 return !m->buf ? -ENOMEM : -EAGAIN;
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/**
140 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700141 * @file: the file to read from
142 * @buf: the buffer to read to
143 * @size: the maximum number of bytes to read
144 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 *
146 * Ready-made ->f_op->read()
147 */
148ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
149{
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100150 struct iovec iov = { .iov_base = buf, .iov_len = size};
151 struct kiocb kiocb;
152 struct iov_iter iter;
153 ssize_t ret;
154
155 init_sync_kiocb(&kiocb, file);
156 iov_iter_init(&iter, READ, &iov, 1, size);
157
158 kiocb.ki_pos = *ppos;
159 ret = seq_read_iter(&kiocb, &iter);
160 *ppos = kiocb.ki_pos;
161 return ret;
162}
163EXPORT_SYMBOL(seq_read);
164
165/*
166 * Ready-made ->f_op->read_iter()
167 */
168ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
169{
170 struct seq_file *m = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 size_t copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 size_t n;
173 void *p;
174 int err = 0;
175
Al Viro4bbf4392020-11-12 14:40:37 -0500176 if (!iov_iter_count(iter))
177 return 0;
178
Ingo Molnar0ac17592006-03-23 03:00:37 -0800179 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100182 * if request is to read from zero offset, reset iterator to first
183 * record as it might have been already advanced by previous requests
184 */
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100185 if (iocb->ki_pos == 0) {
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100186 m->index = 0;
Miklos Szeredicf5eeba2017-11-15 11:34:58 +0100187 m->count = 0;
188 }
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100189
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100190 /* Don't assume ki_pos is where we left it */
191 if (unlikely(iocb->ki_pos != m->read_pos)) {
192 while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
Earl Chew7904ac82012-03-21 16:33:43 -0700193 ;
194 if (err) {
195 /* With prejudice... */
196 m->read_pos = 0;
Earl Chew7904ac82012-03-21 16:33:43 -0700197 m->index = 0;
198 m->count = 0;
199 goto Done;
200 } else {
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100201 m->read_pos = iocb->ki_pos;
Earl Chew7904ac82012-03-21 16:33:43 -0700202 }
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* grab buffer if we didn't have one */
206 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700207 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!m->buf)
209 goto Enomem;
210 }
Al Viro4bbf4392020-11-12 14:40:37 -0500211 // something left in the buffer - copy it out first
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (m->count) {
Al Viro4bbf4392020-11-12 14:40:37 -0500213 n = copy_to_iter(m->buf + m->from, m->count, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 m->count -= n;
215 m->from += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 copied += n;
Al Viro4bbf4392020-11-12 14:40:37 -0500217 if (m->count) // hadn't managed to copy everything
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto Done;
219 }
Al Viro4bbf4392020-11-12 14:40:37 -0500220 // get a non-empty record in the buffer
NeilBrown1f4aace2018-08-17 15:44:41 -0700221 m->from = 0;
222 p = m->op->start(m, &m->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = PTR_ERR(p);
Al Viro4bbf4392020-11-12 14:40:37 -0500225 if (!p || IS_ERR(p)) // EOF or an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 err = m->op->show(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500228 if (err < 0) // hard error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
Al Viro4bbf4392020-11-12 14:40:37 -0500230 if (unlikely(err)) // ->show() says "skip it"
Al Viro521b5d02008-03-28 00:46:41 -0400231 m->count = 0;
Al Viro4bbf4392020-11-12 14:40:37 -0500232 if (unlikely(!m->count)) { // empty record
NeilBrown1f4aace2018-08-17 15:44:41 -0700233 p = m->op->next(m, p, &m->index);
Al Viro4cdfe842008-08-24 07:45:33 -0400234 continue;
235 }
Al Viro4bbf4392020-11-12 14:40:37 -0500236 if (!seq_has_overflowed(m)) // got it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto Fill;
Al Viro4bbf4392020-11-12 14:40:37 -0500238 // need a bigger buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700240 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000241 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700242 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (!m->buf)
244 goto Enomem;
NeilBrown1f4aace2018-08-17 15:44:41 -0700245 p = m->op->start(m, &m->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Al Viro4bbf4392020-11-12 14:40:37 -0500247 // EOF or an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 m->op->stop(m, p);
249 m->count = 0;
250 goto Done;
251Fill:
Al Viro4bbf4392020-11-12 14:40:37 -0500252 // one non-empty record is in the buffer; if they want more,
253 // try to fit more in, but in any case we need to advance
254 // the iterator once for every record shown.
NeilBrown1f4aace2018-08-17 15:44:41 -0700255 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 size_t offs = m->count;
NeilBrown1f4aace2018-08-17 15:44:41 -0700257 loff_t pos = m->index;
258
259 p = m->op->next(m, p, &m->index);
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700260 if (pos == m->index) {
Joe Perchesa3963012020-06-04 16:51:02 -0700261 pr_info_ratelimited("buggy .next function %ps did not update position index\n",
262 m->op->next);
NeilBrown1f4aace2018-08-17 15:44:41 -0700263 m->index++;
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700264 }
Al Viro4bbf4392020-11-12 14:40:37 -0500265 if (!p || IS_ERR(p)) // no next record for us
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
Al Viro4bbf4392020-11-12 14:40:37 -0500267 if (m->count >= iov_iter_count(iter))
NeilBrown1f4aace2018-08-17 15:44:41 -0700268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 err = m->op->show(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500270 if (err > 0) { // ->show() says "skip it"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 m->count = offs;
Al Viro4bbf4392020-11-12 14:40:37 -0500272 } else if (err || seq_has_overflowed(m)) {
273 m->count = offs;
274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 m->op->stop(m, p);
Al Viro4bbf4392020-11-12 14:40:37 -0500278 n = copy_to_iter(m->buf, m->count, iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 copied += n;
280 m->count -= n;
NeilBrown1f4aace2018-08-17 15:44:41 -0700281 m->from = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282Done:
Al Viro4bbf4392020-11-12 14:40:37 -0500283 if (unlikely(!copied)) {
284 copied = m->count ? -EFAULT : err;
285 } else {
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100286 iocb->ki_pos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800287 m->read_pos += copied;
288 }
Ingo Molnar0ac17592006-03-23 03:00:37 -0800289 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return copied;
291Enomem:
292 err = -ENOMEM;
293 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100295EXPORT_SYMBOL(seq_read_iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/**
298 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700299 * @file: the file in question
300 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800301 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Ready-made ->f_op->llseek()
304 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800305loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Joe Perches8209e2f2010-09-04 18:52:49 -0700307 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200308 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Ingo Molnar0ac17592006-03-23 03:00:37 -0800310 mutex_lock(&m->lock);
Andrew Morton965c8e52012-12-17 15:59:39 -0800311 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800312 case SEEK_CUR:
313 offset += file->f_pos;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500314 fallthrough;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800315 case SEEK_SET:
316 if (offset < 0)
317 break;
318 retval = offset;
319 if (offset != m->read_pos) {
320 while ((retval = traverse(m, offset)) == -EAGAIN)
321 ;
322 if (retval) {
323 /* with extreme prejudice... */
324 file->f_pos = 0;
325 m->read_pos = 0;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800326 m->index = 0;
327 m->count = 0;
328 } else {
329 m->read_pos = offset;
330 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Gu Zheng05e16742013-10-25 18:15:06 +0800332 } else {
333 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700336 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return retval;
338}
339EXPORT_SYMBOL(seq_lseek);
340
341/**
342 * seq_release - free the structures associated with sequential file.
343 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500344 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *
346 * Frees the structures associated with sequential file; can be used
347 * as ->f_op->release() if you don't have private data to destroy.
348 */
349int seq_release(struct inode *inode, struct file *file)
350{
Joe Perches8209e2f2010-09-04 18:52:49 -0700351 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700352 kvfree(m->buf);
Alexey Dobriyan0965232032018-04-10 16:34:45 -0700353 kmem_cache_free(seq_file_cache, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356EXPORT_SYMBOL(seq_release);
357
358/**
359 * seq_escape - print string into buffer, escaping some characters
360 * @m: target buffer
361 * @s: string
362 * @esc: set of characters that need escaping
363 *
364 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700365 * @esc with usual octal escape.
366 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700368void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800370 char *buf;
371 size_t size = seq_get_buf(m, &buf);
372 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800374 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
375 seq_commit(m, ret < size ? ret : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377EXPORT_SYMBOL(seq_escape);
378
J. Bruce Fieldsea053e12019-06-19 12:30:13 -0400379void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
380{
381 char *buf;
382 size_t size = seq_get_buf(m, &buf);
383 int ret;
384
385 ret = string_escape_mem_ascii(src, isz, buf, size);
386 seq_commit(m, ret < size ? ret : -1);
387}
388EXPORT_SYMBOL(seq_escape_mem_ascii);
389
Joe Perches6798a8c2015-09-11 13:07:48 -0700390void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int len;
393
394 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (m->count + len < m->size) {
397 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700401 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Steven Whitehousea4808142012-06-11 13:16:35 +0100403EXPORT_SYMBOL(seq_vprintf);
404
Joe Perches6798a8c2015-09-11 13:07:48 -0700405void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100406{
Steven Whitehousea4808142012-06-11 13:16:35 +0100407 va_list args;
408
409 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700410 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100411 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413EXPORT_SYMBOL(seq_printf);
414
Florent Revest76d6a132021-04-27 19:43:12 +0200415#ifdef CONFIG_BINARY_PRINTF
416void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
417{
418 int len;
419
420 if (m->count < m->size) {
421 len = bstr_printf(m->buf + m->count, m->size - m->count, f,
422 binary);
423 if (m->count + len < m->size) {
424 m->count += len;
425 return;
426 }
427 }
428 seq_set_overflow(m);
429}
430EXPORT_SYMBOL(seq_bprintf);
431#endif /* CONFIG_BINARY_PRINTF */
432
Török Edwin74e2f332008-11-22 13:28:48 +0200433/**
Török Edwin958086d2008-11-23 23:24:53 +0200434 * mangle_path - mangle and copy path to buffer beginning
435 * @s: buffer start
436 * @p: beginning of path in above buffer
437 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200438 *
439 * Copy the path from @p to @s, replacing each occurrence of character from
440 * @esc with usual octal escape.
441 * Returns pointer past last written character in @s, or NULL in case of
442 * failure.
443 */
Al Viro8c9379e2011-12-08 20:18:57 -0500444char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100445{
446 while (s <= p) {
447 char c = *p++;
448 if (!c) {
449 return s;
450 } else if (!strchr(esc, c)) {
451 *s++ = c;
452 } else if (s + 4 > p) {
453 break;
454 } else {
455 *s++ = '\\';
456 *s++ = '0' + ((c & 0300) >> 6);
457 *s++ = '0' + ((c & 070) >> 3);
458 *s++ = '0' + (c & 07);
459 }
460 }
461 return NULL;
462}
Ingo Molnar604094f2008-11-28 18:03:22 +0100463EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100464
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800465/**
466 * seq_path - seq_file interface to print a pathname
467 * @m: the seq_file handle
468 * @path: the struct path to print
469 * @esc: set of characters to escape in the output
470 *
471 * return the absolute path of 'path', as represented by the
472 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100473 */
Al Viro8c9379e2011-12-08 20:18:57 -0500474int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Miklos Szeredif8439802009-09-21 14:48:36 +0200476 char *buf;
477 size_t size = seq_get_buf(m, &buf);
478 int res = -1;
479
480 if (size) {
481 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200483 char *end = mangle_path(buf, p, esc);
484 if (end)
485 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200488 seq_commit(m, res);
489
490 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492EXPORT_SYMBOL(seq_path);
493
Miklos Szeredi2726d562015-06-19 10:30:28 +0200494/**
495 * seq_file_path - seq_file interface to print a pathname of a file
496 * @m: the seq_file handle
497 * @file: the struct file to print
498 * @esc: set of characters to escape in the output
499 *
500 * return the absolute path to the file.
501 */
502int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
503{
504 return seq_path(m, &file->f_path, esc);
505}
506EXPORT_SYMBOL(seq_file_path);
507
Ram Pai6092d042008-03-27 13:06:20 +0100508/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100509 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100510 */
Al Viro8c9379e2011-12-08 20:18:57 -0500511int seq_path_root(struct seq_file *m, const struct path *path,
512 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100513{
Miklos Szeredif8439802009-09-21 14:48:36 +0200514 char *buf;
515 size_t size = seq_get_buf(m, &buf);
516 int res = -ENAMETOOLONG;
517
518 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100519 char *p;
520
Miklos Szeredif8439802009-09-21 14:48:36 +0200521 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500522 if (!p)
523 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200524 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100525 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200526 char *end = mangle_path(buf, p, esc);
527 if (end)
528 res = end - buf;
529 else
530 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100531 }
532 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200533 seq_commit(m, res);
534
Al Viro02125a82011-12-05 08:43:34 -0500535 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100536}
537
538/*
Ram Pai6092d042008-03-27 13:06:20 +0100539 * returns the path of the 'dentry' from the root of its filesystem.
540 */
Al Viro8c9379e2011-12-08 20:18:57 -0500541int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100542{
Miklos Szeredif8439802009-09-21 14:48:36 +0200543 char *buf;
544 size_t size = seq_get_buf(m, &buf);
545 int res = -1;
546
547 if (size) {
548 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100549 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200550 char *end = mangle_path(buf, p, esc);
551 if (end)
552 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100553 }
554 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200555 seq_commit(m, res);
556
557 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100558}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700559EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static void *single_start(struct seq_file *p, loff_t *pos)
562{
563 return NULL + (*pos == 0);
564}
565
566static void *single_next(struct seq_file *p, void *v, loff_t *pos)
567{
568 ++*pos;
569 return NULL;
570}
571
572static void single_stop(struct seq_file *p, void *v)
573{
574}
575
576int single_open(struct file *file, int (*show)(struct seq_file *, void *),
577 void *data)
578{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700579 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int res = -ENOMEM;
581
582 if (op) {
583 op->start = single_start;
584 op->next = single_next;
585 op->stop = single_stop;
586 op->show = show;
587 res = seq_open(file, op);
588 if (!res)
589 ((struct seq_file *)file->private_data)->private = data;
590 else
591 kfree(op);
592 }
593 return res;
594}
595EXPORT_SYMBOL(single_open);
596
Al Viro2043f492013-03-31 13:43:23 -0400597int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
598 void *data, size_t size)
599{
Heiko Carstens058504e2014-07-02 15:22:37 -0700600 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400601 int ret;
602 if (!buf)
603 return -ENOMEM;
604 ret = single_open(file, show, data);
605 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700606 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400607 return ret;
608 }
609 ((struct seq_file *)file->private_data)->buf = buf;
610 ((struct seq_file *)file->private_data)->size = size;
611 return 0;
612}
613EXPORT_SYMBOL(single_open_size);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615int single_release(struct inode *inode, struct file *file)
616{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800617 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int res = seq_release(inode, file);
619 kfree(op);
620 return res;
621}
622EXPORT_SYMBOL(single_release);
623
624int seq_release_private(struct inode *inode, struct file *file)
625{
626 struct seq_file *seq = file->private_data;
627
628 kfree(seq->private);
629 seq->private = NULL;
630 return seq_release(inode, file);
631}
632EXPORT_SYMBOL(seq_release_private);
633
Pavel Emelyanov39699032007-10-10 02:28:42 -0700634void *__seq_open_private(struct file *f, const struct seq_operations *ops,
635 int psize)
636{
637 int rc;
638 void *private;
639 struct seq_file *seq;
640
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700641 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
Pavel Emelyanov39699032007-10-10 02:28:42 -0700642 if (private == NULL)
643 goto out;
644
645 rc = seq_open(f, ops);
646 if (rc < 0)
647 goto out_free;
648
649 seq = f->private_data;
650 seq->private = private;
651 return private;
652
653out_free:
654 kfree(private);
655out:
656 return NULL;
657}
658EXPORT_SYMBOL(__seq_open_private);
659
660int seq_open_private(struct file *filp, const struct seq_operations *ops,
661 int psize)
662{
663 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
664}
665EXPORT_SYMBOL(seq_open_private);
666
Joe Perches6798a8c2015-09-11 13:07:48 -0700667void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Joe Perches6798a8c2015-09-11 13:07:48 -0700669 if (m->count >= m->size)
670 return;
671
672 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674EXPORT_SYMBOL(seq_putc);
675
Joe Perches6798a8c2015-09-11 13:07:48 -0700676void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700679
680 if (m->count + len >= m->size) {
681 seq_set_overflow(m);
682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700684 memcpy(m->buf + m->count, s, len);
685 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700688
Andrei Vagind1be35c2018-04-10 16:31:16 -0700689/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +0100690 * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
691 * without rich format of printf().
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700692 * only 'unsigned long long' is supported.
Andrei Vagind1be35c2018-04-10 16:31:16 -0700693 * @m: seq_file identifying the buffer to which data should be written
694 * @delimiter: a string which is printed before the number
695 * @num: the number
696 * @width: a minimum field width
697 *
698 * This routine will put strlen(delimiter) + number into seq_filed.
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700699 * This routine is very quick when you show lots of numbers.
700 * In usual cases, it will be better to use seq_printf(). It's easier to read.
701 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700702void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
703 unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700704{
705 int len;
706
707 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
708 goto overflow;
709
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700710 if (delimiter && delimiter[0]) {
711 if (delimiter[1] == 0)
712 seq_putc(m, delimiter[0]);
713 else
714 seq_puts(m, delimiter);
715 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700716
Andrei Vagind1be35c2018-04-10 16:31:16 -0700717 if (!width)
718 width = 1;
719
720 if (m->count + width >= m->size)
Joe Perches75ba1d02016-10-07 17:02:20 -0700721 goto overflow;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700722
Andrei Vagind1be35c2018-04-10 16:31:16 -0700723 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700724 if (!len)
725 goto overflow;
Joe Perches75ba1d02016-10-07 17:02:20 -0700726
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700727 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700728 return;
729
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700730overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700731 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700732}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700733
734void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
735 unsigned long long num)
736{
737 return seq_put_decimal_ull_width(m, delimiter, num, 0);
738}
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700739EXPORT_SYMBOL(seq_put_decimal_ull);
740
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700741/**
742 * seq_put_hex_ll - put a number in hexadecimal notation
743 * @m: seq_file identifying the buffer to which data should be written
744 * @delimiter: a string which is printed before the number
745 * @v: the number
746 * @width: a minimum field width
747 *
748 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
749 *
750 * This routine is very quick when you show lots of numbers.
751 * In usual cases, it will be better to use seq_printf(). It's easier to read.
752 */
753void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
754 unsigned long long v, unsigned int width)
755{
756 unsigned int len;
757 int i;
758
759 if (delimiter && delimiter[0]) {
760 if (delimiter[1] == 0)
761 seq_putc(m, delimiter[0]);
762 else
763 seq_puts(m, delimiter);
764 }
765
766 /* If x is 0, the result of __builtin_clzll is undefined */
767 if (v == 0)
768 len = 1;
769 else
770 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
771
772 if (len < width)
773 len = width;
774
775 if (m->count + len > m->size) {
776 seq_set_overflow(m);
777 return;
778 }
779
780 for (i = len - 1; i >= 0; i--) {
781 m->buf[m->count + i] = hex_asc[0xf & v];
782 v = v >> 4;
783 }
784 m->count += len;
785}
786
Joe Perches75ba1d02016-10-07 17:02:20 -0700787void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700788{
Joe Perches75ba1d02016-10-07 17:02:20 -0700789 int len;
790
791 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
792 goto overflow;
793
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700794 if (delimiter && delimiter[0]) {
795 if (delimiter[1] == 0)
796 seq_putc(m, delimiter[0]);
797 else
798 seq_puts(m, delimiter);
799 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700800
801 if (m->count + 2 >= m->size)
802 goto overflow;
803
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700804 if (num < 0) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700805 m->buf[m->count++] = '-';
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700806 num = -num;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700807 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700808
809 if (num < 10) {
810 m->buf[m->count++] = num + '0';
811 return;
812 }
813
Andrei Vagind1be35c2018-04-10 16:31:16 -0700814 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
Joe Perches75ba1d02016-10-07 17:02:20 -0700815 if (!len)
816 goto overflow;
817
818 m->count += len;
819 return;
820
821overflow:
822 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700823}
824EXPORT_SYMBOL(seq_put_decimal_ll);
825
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700826/**
827 * seq_write - write arbitrary data to buffer
828 * @seq: seq_file identifying the buffer to which data should be written
829 * @data: data address
830 * @len: number of bytes
831 *
832 * Return 0 on success, non-zero otherwise.
833 */
834int seq_write(struct seq_file *seq, const void *data, size_t len)
835{
836 if (seq->count + len < seq->size) {
837 memcpy(seq->buf + seq->count, data, len);
838 seq->count += len;
839 return 0;
840 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700841 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700842 return -1;
843}
844EXPORT_SYMBOL(seq_write);
845
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800846/**
847 * seq_pad - write padding spaces to buffer
848 * @m: seq_file identifying the buffer to which data should be written
849 * @c: the byte to append after padding if non-zero
850 */
851void seq_pad(struct seq_file *m, char c)
852{
853 int size = m->pad_until - m->count;
Andrei Vagin8cfa67b42018-04-10 16:30:47 -0700854 if (size > 0) {
855 if (size + m->count > m->size) {
856 seq_set_overflow(m);
857 return;
858 }
859 memset(m->buf + m->count, ' ', size);
860 m->count += size;
861 }
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800862 if (c)
863 seq_putc(m, c);
864}
865EXPORT_SYMBOL(seq_pad);
866
Andy Shevchenko37607102015-09-09 15:38:33 -0700867/* A complete analogue of print_hex_dump() */
868void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
869 int rowsize, int groupsize, const void *buf, size_t len,
870 bool ascii)
871{
872 const u8 *ptr = buf;
873 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800874 char *buffer;
875 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700876 int ret;
877
878 if (rowsize != 16 && rowsize != 32)
879 rowsize = 16;
880
881 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
882 linelen = min(remaining, rowsize);
883 remaining -= rowsize;
884
885 switch (prefix_type) {
886 case DUMP_PREFIX_ADDRESS:
887 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
888 break;
889 case DUMP_PREFIX_OFFSET:
890 seq_printf(m, "%s%.8x: ", prefix_str, i);
891 break;
892 default:
893 seq_printf(m, "%s", prefix_str);
894 break;
895 }
896
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800897 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700898 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800899 buffer, size, ascii);
900 seq_commit(m, ret < size ? ret : -1);
901
902 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700903 }
904}
905EXPORT_SYMBOL(seq_hex_dump);
906
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700907struct list_head *seq_list_start(struct list_head *head, loff_t pos)
908{
909 struct list_head *lh;
910
911 list_for_each(lh, head)
912 if (pos-- == 0)
913 return lh;
914
915 return NULL;
916}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700917EXPORT_SYMBOL(seq_list_start);
918
919struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
920{
921 if (!pos)
922 return head;
923
924 return seq_list_start(head, pos - 1);
925}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700926EXPORT_SYMBOL(seq_list_start_head);
927
928struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
929{
930 struct list_head *lh;
931
932 lh = ((struct list_head *)v)->next;
933 ++*ppos;
934 return lh == head ? NULL : lh;
935}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700936EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000937
938/**
939 * seq_hlist_start - start an iteration of a hlist
940 * @head: the head of the hlist
941 * @pos: the start position of the sequence
942 *
943 * Called at seq_file->op->start().
944 */
945struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
946{
947 struct hlist_node *node;
948
949 hlist_for_each(node, head)
950 if (pos-- == 0)
951 return node;
952 return NULL;
953}
954EXPORT_SYMBOL(seq_hlist_start);
955
956/**
957 * seq_hlist_start_head - start an iteration of a hlist
958 * @head: the head of the hlist
959 * @pos: the start position of the sequence
960 *
961 * Called at seq_file->op->start(). Call this function if you want to
962 * print a header at the top of the output.
963 */
964struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
965{
966 if (!pos)
967 return SEQ_START_TOKEN;
968
969 return seq_hlist_start(head, pos - 1);
970}
971EXPORT_SYMBOL(seq_hlist_start_head);
972
973/**
974 * seq_hlist_next - move to the next position of the hlist
975 * @v: the current iterator
976 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800977 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000978 *
979 * Called at seq_file->op->next().
980 */
981struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
982 loff_t *ppos)
983{
984 struct hlist_node *node = v;
985
986 ++*ppos;
987 if (v == SEQ_START_TOKEN)
988 return head->first;
989 else
990 return node->next;
991}
992EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000993
994/**
995 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
996 * @head: the head of the hlist
997 * @pos: the start position of the sequence
998 *
999 * Called at seq_file->op->start().
1000 *
1001 * This list-traversal primitive may safely run concurrently with
1002 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1003 * as long as the traversal is guarded by rcu_read_lock().
1004 */
1005struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
1006 loff_t pos)
1007{
1008 struct hlist_node *node;
1009
1010 __hlist_for_each_rcu(node, head)
1011 if (pos-- == 0)
1012 return node;
1013 return NULL;
1014}
1015EXPORT_SYMBOL(seq_hlist_start_rcu);
1016
1017/**
1018 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1019 * @head: the head of the hlist
1020 * @pos: the start position of the sequence
1021 *
1022 * Called at seq_file->op->start(). Call this function if you want to
1023 * print a header at the top of the output.
1024 *
1025 * This list-traversal primitive may safely run concurrently with
1026 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1027 * as long as the traversal is guarded by rcu_read_lock().
1028 */
1029struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1030 loff_t pos)
1031{
1032 if (!pos)
1033 return SEQ_START_TOKEN;
1034
1035 return seq_hlist_start_rcu(head, pos - 1);
1036}
1037EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1038
1039/**
1040 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1041 * @v: the current iterator
1042 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -08001043 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +00001044 *
1045 * Called at seq_file->op->next().
1046 *
1047 * This list-traversal primitive may safely run concurrently with
1048 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1049 * as long as the traversal is guarded by rcu_read_lock().
1050 */
1051struct hlist_node *seq_hlist_next_rcu(void *v,
1052 struct hlist_head *head,
1053 loff_t *ppos)
1054{
1055 struct hlist_node *node = v;
1056
1057 ++*ppos;
1058 if (v == SEQ_START_TOKEN)
1059 return rcu_dereference(head->first);
1060 else
1061 return rcu_dereference(node->next);
1062}
1063EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -04001064
1065/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001066 * seq_hlist_start_percpu - start an iteration of a percpu hlist array
Jeff Layton0bc77382013-06-21 08:58:21 -04001067 * @head: pointer to percpu array of struct hlist_heads
1068 * @cpu: pointer to cpu "cursor"
1069 * @pos: start position of sequence
1070 *
1071 * Called at seq_file->op->start().
1072 */
1073struct hlist_node *
1074seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1075{
1076 struct hlist_node *node;
1077
1078 for_each_possible_cpu(*cpu) {
1079 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1080 if (pos-- == 0)
1081 return node;
1082 }
1083 }
1084 return NULL;
1085}
1086EXPORT_SYMBOL(seq_hlist_start_percpu);
1087
1088/**
1089 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1090 * @v: pointer to current hlist_node
1091 * @head: pointer to percpu array of struct hlist_heads
1092 * @cpu: pointer to cpu "cursor"
1093 * @pos: start position of sequence
1094 *
1095 * Called at seq_file->op->next().
1096 */
1097struct hlist_node *
1098seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1099 int *cpu, loff_t *pos)
1100{
1101 struct hlist_node *node = v;
1102
1103 ++*pos;
1104
1105 if (node->next)
1106 return node->next;
1107
1108 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1109 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1110 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1111
1112 if (!hlist_empty(bucket))
1113 return bucket->first;
1114 }
1115 return NULL;
1116}
1117EXPORT_SYMBOL(seq_hlist_next_percpu);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001118
1119void __init seq_file_init(void)
1120{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -07001121 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001122}