blob: b117b212ef2887eca0f3c2412f761228740fb81d [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/**
Andy Shevchenko1d31aa12021-06-30 18:55:34 -0700359 * seq_escape_mem - print data into buffer, escaping some characters
360 * @m: target buffer
361 * @src: source buffer
362 * @len: size of source buffer
363 * @flags: flags to pass to string_escape_mem()
364 * @esc: set of characters that need escaping
365 *
366 * Puts data into buffer, replacing each occurrence of character from
367 * given class (defined by @flags and @esc) with printable escaped sequence.
368 *
369 * Use seq_has_overflowed() to check for errors.
370 */
371void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
372 unsigned int flags, const char *esc)
373{
374 char *buf;
375 size_t size = seq_get_buf(m, &buf);
376 int ret;
377
378 ret = string_escape_mem(src, len, buf, size, flags, esc);
379 seq_commit(m, ret < size ? ret : -1);
380}
381EXPORT_SYMBOL(seq_escape_mem);
382
383/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * seq_escape - print string into buffer, escaping some characters
385 * @m: target buffer
386 * @s: string
387 * @esc: set of characters that need escaping
388 *
389 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700390 * @esc with usual octal escape.
391 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700393void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Andy Shevchenkofc3de022021-06-30 18:55:40 -0700395 seq_escape_str(m, s, ESCAPE_OCTAL, esc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397EXPORT_SYMBOL(seq_escape);
398
Joe Perches6798a8c2015-09-11 13:07:48 -0700399void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int len;
402
403 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (m->count + len < m->size) {
406 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700410 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Steven Whitehousea4808142012-06-11 13:16:35 +0100412EXPORT_SYMBOL(seq_vprintf);
413
Joe Perches6798a8c2015-09-11 13:07:48 -0700414void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100415{
Steven Whitehousea4808142012-06-11 13:16:35 +0100416 va_list args;
417
418 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700419 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100420 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422EXPORT_SYMBOL(seq_printf);
423
Florent Revest76d6a132021-04-27 19:43:12 +0200424#ifdef CONFIG_BINARY_PRINTF
425void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
426{
427 int len;
428
429 if (m->count < m->size) {
430 len = bstr_printf(m->buf + m->count, m->size - m->count, f,
431 binary);
432 if (m->count + len < m->size) {
433 m->count += len;
434 return;
435 }
436 }
437 seq_set_overflow(m);
438}
439EXPORT_SYMBOL(seq_bprintf);
440#endif /* CONFIG_BINARY_PRINTF */
441
Török Edwin74e2f332008-11-22 13:28:48 +0200442/**
Török Edwin958086d2008-11-23 23:24:53 +0200443 * mangle_path - mangle and copy path to buffer beginning
444 * @s: buffer start
445 * @p: beginning of path in above buffer
446 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200447 *
448 * Copy the path from @p to @s, replacing each occurrence of character from
449 * @esc with usual octal escape.
450 * Returns pointer past last written character in @s, or NULL in case of
451 * failure.
452 */
Al Viro8c9379e2011-12-08 20:18:57 -0500453char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100454{
455 while (s <= p) {
456 char c = *p++;
457 if (!c) {
458 return s;
459 } else if (!strchr(esc, c)) {
460 *s++ = c;
461 } else if (s + 4 > p) {
462 break;
463 } else {
464 *s++ = '\\';
465 *s++ = '0' + ((c & 0300) >> 6);
466 *s++ = '0' + ((c & 070) >> 3);
467 *s++ = '0' + (c & 07);
468 }
469 }
470 return NULL;
471}
Ingo Molnar604094f2008-11-28 18:03:22 +0100472EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100473
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800474/**
475 * seq_path - seq_file interface to print a pathname
476 * @m: the seq_file handle
477 * @path: the struct path to print
478 * @esc: set of characters to escape in the output
479 *
480 * return the absolute path of 'path', as represented by the
481 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100482 */
Al Viro8c9379e2011-12-08 20:18:57 -0500483int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Miklos Szeredif8439802009-09-21 14:48:36 +0200485 char *buf;
486 size_t size = seq_get_buf(m, &buf);
487 int res = -1;
488
489 if (size) {
490 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200492 char *end = mangle_path(buf, p, esc);
493 if (end)
494 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200497 seq_commit(m, res);
498
499 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501EXPORT_SYMBOL(seq_path);
502
Miklos Szeredi2726d562015-06-19 10:30:28 +0200503/**
504 * seq_file_path - seq_file interface to print a pathname of a file
505 * @m: the seq_file handle
506 * @file: the struct file to print
507 * @esc: set of characters to escape in the output
508 *
509 * return the absolute path to the file.
510 */
511int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
512{
513 return seq_path(m, &file->f_path, esc);
514}
515EXPORT_SYMBOL(seq_file_path);
516
Ram Pai6092d042008-03-27 13:06:20 +0100517/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100518 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100519 */
Al Viro8c9379e2011-12-08 20:18:57 -0500520int seq_path_root(struct seq_file *m, const struct path *path,
521 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100522{
Miklos Szeredif8439802009-09-21 14:48:36 +0200523 char *buf;
524 size_t size = seq_get_buf(m, &buf);
525 int res = -ENAMETOOLONG;
526
527 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100528 char *p;
529
Miklos Szeredif8439802009-09-21 14:48:36 +0200530 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500531 if (!p)
532 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200533 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100534 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200535 char *end = mangle_path(buf, p, esc);
536 if (end)
537 res = end - buf;
538 else
539 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100540 }
541 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200542 seq_commit(m, res);
543
Al Viro02125a82011-12-05 08:43:34 -0500544 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100545}
546
547/*
Ram Pai6092d042008-03-27 13:06:20 +0100548 * returns the path of the 'dentry' from the root of its filesystem.
549 */
Al Viro8c9379e2011-12-08 20:18:57 -0500550int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100551{
Miklos Szeredif8439802009-09-21 14:48:36 +0200552 char *buf;
553 size_t size = seq_get_buf(m, &buf);
554 int res = -1;
555
556 if (size) {
557 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100558 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200559 char *end = mangle_path(buf, p, esc);
560 if (end)
561 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100562 }
563 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200564 seq_commit(m, res);
565
566 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100567}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700568EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static void *single_start(struct seq_file *p, loff_t *pos)
571{
572 return NULL + (*pos == 0);
573}
574
575static void *single_next(struct seq_file *p, void *v, loff_t *pos)
576{
577 ++*pos;
578 return NULL;
579}
580
581static void single_stop(struct seq_file *p, void *v)
582{
583}
584
585int single_open(struct file *file, int (*show)(struct seq_file *, void *),
586 void *data)
587{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700588 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int res = -ENOMEM;
590
591 if (op) {
592 op->start = single_start;
593 op->next = single_next;
594 op->stop = single_stop;
595 op->show = show;
596 res = seq_open(file, op);
597 if (!res)
598 ((struct seq_file *)file->private_data)->private = data;
599 else
600 kfree(op);
601 }
602 return res;
603}
604EXPORT_SYMBOL(single_open);
605
Al Viro2043f492013-03-31 13:43:23 -0400606int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
607 void *data, size_t size)
608{
Heiko Carstens058504e2014-07-02 15:22:37 -0700609 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400610 int ret;
611 if (!buf)
612 return -ENOMEM;
613 ret = single_open(file, show, data);
614 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700615 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400616 return ret;
617 }
618 ((struct seq_file *)file->private_data)->buf = buf;
619 ((struct seq_file *)file->private_data)->size = size;
620 return 0;
621}
622EXPORT_SYMBOL(single_open_size);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624int single_release(struct inode *inode, struct file *file)
625{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800626 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int res = seq_release(inode, file);
628 kfree(op);
629 return res;
630}
631EXPORT_SYMBOL(single_release);
632
633int seq_release_private(struct inode *inode, struct file *file)
634{
635 struct seq_file *seq = file->private_data;
636
637 kfree(seq->private);
638 seq->private = NULL;
639 return seq_release(inode, file);
640}
641EXPORT_SYMBOL(seq_release_private);
642
Pavel Emelyanov39699032007-10-10 02:28:42 -0700643void *__seq_open_private(struct file *f, const struct seq_operations *ops,
644 int psize)
645{
646 int rc;
647 void *private;
648 struct seq_file *seq;
649
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700650 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
Pavel Emelyanov39699032007-10-10 02:28:42 -0700651 if (private == NULL)
652 goto out;
653
654 rc = seq_open(f, ops);
655 if (rc < 0)
656 goto out_free;
657
658 seq = f->private_data;
659 seq->private = private;
660 return private;
661
662out_free:
663 kfree(private);
664out:
665 return NULL;
666}
667EXPORT_SYMBOL(__seq_open_private);
668
669int seq_open_private(struct file *filp, const struct seq_operations *ops,
670 int psize)
671{
672 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
673}
674EXPORT_SYMBOL(seq_open_private);
675
Joe Perches6798a8c2015-09-11 13:07:48 -0700676void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Joe Perches6798a8c2015-09-11 13:07:48 -0700678 if (m->count >= m->size)
679 return;
680
681 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683EXPORT_SYMBOL(seq_putc);
684
Joe Perches6798a8c2015-09-11 13:07:48 -0700685void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700688
689 if (m->count + len >= m->size) {
690 seq_set_overflow(m);
691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700693 memcpy(m->buf + m->count, s, len);
694 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700697
Andrei Vagind1be35c2018-04-10 16:31:16 -0700698/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +0100699 * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
700 * without rich format of printf().
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700701 * only 'unsigned long long' is supported.
Andrei Vagind1be35c2018-04-10 16:31:16 -0700702 * @m: seq_file identifying the buffer to which data should be written
703 * @delimiter: a string which is printed before the number
704 * @num: the number
705 * @width: a minimum field width
706 *
707 * This routine will put strlen(delimiter) + number into seq_filed.
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700708 * This routine is very quick when you show lots of numbers.
709 * In usual cases, it will be better to use seq_printf(). It's easier to read.
710 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700711void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
712 unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700713{
714 int len;
715
716 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
717 goto overflow;
718
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700719 if (delimiter && delimiter[0]) {
720 if (delimiter[1] == 0)
721 seq_putc(m, delimiter[0]);
722 else
723 seq_puts(m, delimiter);
724 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700725
Andrei Vagind1be35c2018-04-10 16:31:16 -0700726 if (!width)
727 width = 1;
728
729 if (m->count + width >= m->size)
Joe Perches75ba1d02016-10-07 17:02:20 -0700730 goto overflow;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700731
Andrei Vagind1be35c2018-04-10 16:31:16 -0700732 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700733 if (!len)
734 goto overflow;
Joe Perches75ba1d02016-10-07 17:02:20 -0700735
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700736 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700737 return;
738
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700739overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700740 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700741}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700742
743void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
744 unsigned long long num)
745{
746 return seq_put_decimal_ull_width(m, delimiter, num, 0);
747}
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700748EXPORT_SYMBOL(seq_put_decimal_ull);
749
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700750/**
751 * seq_put_hex_ll - put a number in hexadecimal notation
752 * @m: seq_file identifying the buffer to which data should be written
753 * @delimiter: a string which is printed before the number
754 * @v: the number
755 * @width: a minimum field width
756 *
757 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
758 *
759 * This routine is very quick when you show lots of numbers.
760 * In usual cases, it will be better to use seq_printf(). It's easier to read.
761 */
762void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
763 unsigned long long v, unsigned int width)
764{
765 unsigned int len;
766 int i;
767
768 if (delimiter && delimiter[0]) {
769 if (delimiter[1] == 0)
770 seq_putc(m, delimiter[0]);
771 else
772 seq_puts(m, delimiter);
773 }
774
775 /* If x is 0, the result of __builtin_clzll is undefined */
776 if (v == 0)
777 len = 1;
778 else
779 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
780
781 if (len < width)
782 len = width;
783
784 if (m->count + len > m->size) {
785 seq_set_overflow(m);
786 return;
787 }
788
789 for (i = len - 1; i >= 0; i--) {
790 m->buf[m->count + i] = hex_asc[0xf & v];
791 v = v >> 4;
792 }
793 m->count += len;
794}
795
Joe Perches75ba1d02016-10-07 17:02:20 -0700796void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700797{
Joe Perches75ba1d02016-10-07 17:02:20 -0700798 int len;
799
800 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
801 goto overflow;
802
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700803 if (delimiter && delimiter[0]) {
804 if (delimiter[1] == 0)
805 seq_putc(m, delimiter[0]);
806 else
807 seq_puts(m, delimiter);
808 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700809
810 if (m->count + 2 >= m->size)
811 goto overflow;
812
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700813 if (num < 0) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700814 m->buf[m->count++] = '-';
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700815 num = -num;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700816 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700817
818 if (num < 10) {
819 m->buf[m->count++] = num + '0';
820 return;
821 }
822
Andrei Vagind1be35c2018-04-10 16:31:16 -0700823 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
Joe Perches75ba1d02016-10-07 17:02:20 -0700824 if (!len)
825 goto overflow;
826
827 m->count += len;
828 return;
829
830overflow:
831 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700832}
833EXPORT_SYMBOL(seq_put_decimal_ll);
834
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700835/**
836 * seq_write - write arbitrary data to buffer
837 * @seq: seq_file identifying the buffer to which data should be written
838 * @data: data address
839 * @len: number of bytes
840 *
841 * Return 0 on success, non-zero otherwise.
842 */
843int seq_write(struct seq_file *seq, const void *data, size_t len)
844{
845 if (seq->count + len < seq->size) {
846 memcpy(seq->buf + seq->count, data, len);
847 seq->count += len;
848 return 0;
849 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700850 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700851 return -1;
852}
853EXPORT_SYMBOL(seq_write);
854
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800855/**
856 * seq_pad - write padding spaces to buffer
857 * @m: seq_file identifying the buffer to which data should be written
858 * @c: the byte to append after padding if non-zero
859 */
860void seq_pad(struct seq_file *m, char c)
861{
862 int size = m->pad_until - m->count;
Andrei Vagin8cfa67b42018-04-10 16:30:47 -0700863 if (size > 0) {
864 if (size + m->count > m->size) {
865 seq_set_overflow(m);
866 return;
867 }
868 memset(m->buf + m->count, ' ', size);
869 m->count += size;
870 }
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800871 if (c)
872 seq_putc(m, c);
873}
874EXPORT_SYMBOL(seq_pad);
875
Andy Shevchenko37607102015-09-09 15:38:33 -0700876/* A complete analogue of print_hex_dump() */
877void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
878 int rowsize, int groupsize, const void *buf, size_t len,
879 bool ascii)
880{
881 const u8 *ptr = buf;
882 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800883 char *buffer;
884 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700885 int ret;
886
887 if (rowsize != 16 && rowsize != 32)
888 rowsize = 16;
889
890 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
891 linelen = min(remaining, rowsize);
892 remaining -= rowsize;
893
894 switch (prefix_type) {
895 case DUMP_PREFIX_ADDRESS:
896 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
897 break;
898 case DUMP_PREFIX_OFFSET:
899 seq_printf(m, "%s%.8x: ", prefix_str, i);
900 break;
901 default:
902 seq_printf(m, "%s", prefix_str);
903 break;
904 }
905
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800906 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700907 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800908 buffer, size, ascii);
909 seq_commit(m, ret < size ? ret : -1);
910
911 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700912 }
913}
914EXPORT_SYMBOL(seq_hex_dump);
915
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700916struct list_head *seq_list_start(struct list_head *head, loff_t pos)
917{
918 struct list_head *lh;
919
920 list_for_each(lh, head)
921 if (pos-- == 0)
922 return lh;
923
924 return NULL;
925}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700926EXPORT_SYMBOL(seq_list_start);
927
928struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
929{
930 if (!pos)
931 return head;
932
933 return seq_list_start(head, pos - 1);
934}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700935EXPORT_SYMBOL(seq_list_start_head);
936
937struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
938{
939 struct list_head *lh;
940
941 lh = ((struct list_head *)v)->next;
942 ++*ppos;
943 return lh == head ? NULL : lh;
944}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700945EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000946
947/**
948 * seq_hlist_start - start an iteration of a hlist
949 * @head: the head of the hlist
950 * @pos: the start position of the sequence
951 *
952 * Called at seq_file->op->start().
953 */
954struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
955{
956 struct hlist_node *node;
957
958 hlist_for_each(node, head)
959 if (pos-- == 0)
960 return node;
961 return NULL;
962}
963EXPORT_SYMBOL(seq_hlist_start);
964
965/**
966 * seq_hlist_start_head - start an iteration of a hlist
967 * @head: the head of the hlist
968 * @pos: the start position of the sequence
969 *
970 * Called at seq_file->op->start(). Call this function if you want to
971 * print a header at the top of the output.
972 */
973struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
974{
975 if (!pos)
976 return SEQ_START_TOKEN;
977
978 return seq_hlist_start(head, pos - 1);
979}
980EXPORT_SYMBOL(seq_hlist_start_head);
981
982/**
983 * seq_hlist_next - move to the next position of the hlist
984 * @v: the current iterator
985 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800986 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000987 *
988 * Called at seq_file->op->next().
989 */
990struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
991 loff_t *ppos)
992{
993 struct hlist_node *node = v;
994
995 ++*ppos;
996 if (v == SEQ_START_TOKEN)
997 return head->first;
998 else
999 return node->next;
1000}
1001EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +00001002
1003/**
1004 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
1005 * @head: the head of the hlist
1006 * @pos: the start position of the sequence
1007 *
1008 * Called at seq_file->op->start().
1009 *
1010 * This list-traversal primitive may safely run concurrently with
1011 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1012 * as long as the traversal is guarded by rcu_read_lock().
1013 */
1014struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
1015 loff_t pos)
1016{
1017 struct hlist_node *node;
1018
1019 __hlist_for_each_rcu(node, head)
1020 if (pos-- == 0)
1021 return node;
1022 return NULL;
1023}
1024EXPORT_SYMBOL(seq_hlist_start_rcu);
1025
1026/**
1027 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1028 * @head: the head of the hlist
1029 * @pos: the start position of the sequence
1030 *
1031 * Called at seq_file->op->start(). Call this function if you want to
1032 * print a header at the top of the output.
1033 *
1034 * This list-traversal primitive may safely run concurrently with
1035 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1036 * as long as the traversal is guarded by rcu_read_lock().
1037 */
1038struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1039 loff_t pos)
1040{
1041 if (!pos)
1042 return SEQ_START_TOKEN;
1043
1044 return seq_hlist_start_rcu(head, pos - 1);
1045}
1046EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1047
1048/**
1049 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1050 * @v: the current iterator
1051 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -08001052 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +00001053 *
1054 * Called at seq_file->op->next().
1055 *
1056 * This list-traversal primitive may safely run concurrently with
1057 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1058 * as long as the traversal is guarded by rcu_read_lock().
1059 */
1060struct hlist_node *seq_hlist_next_rcu(void *v,
1061 struct hlist_head *head,
1062 loff_t *ppos)
1063{
1064 struct hlist_node *node = v;
1065
1066 ++*ppos;
1067 if (v == SEQ_START_TOKEN)
1068 return rcu_dereference(head->first);
1069 else
1070 return rcu_dereference(node->next);
1071}
1072EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -04001073
1074/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001075 * seq_hlist_start_percpu - start an iteration of a percpu hlist array
Jeff Layton0bc77382013-06-21 08:58:21 -04001076 * @head: pointer to percpu array of struct hlist_heads
1077 * @cpu: pointer to cpu "cursor"
1078 * @pos: start position of sequence
1079 *
1080 * Called at seq_file->op->start().
1081 */
1082struct hlist_node *
1083seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1084{
1085 struct hlist_node *node;
1086
1087 for_each_possible_cpu(*cpu) {
1088 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1089 if (pos-- == 0)
1090 return node;
1091 }
1092 }
1093 return NULL;
1094}
1095EXPORT_SYMBOL(seq_hlist_start_percpu);
1096
1097/**
1098 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1099 * @v: pointer to current hlist_node
1100 * @head: pointer to percpu array of struct hlist_heads
1101 * @cpu: pointer to cpu "cursor"
1102 * @pos: start position of sequence
1103 *
1104 * Called at seq_file->op->next().
1105 */
1106struct hlist_node *
1107seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1108 int *cpu, loff_t *pos)
1109{
1110 struct hlist_node *node = v;
1111
1112 ++*pos;
1113
1114 if (node->next)
1115 return node->next;
1116
1117 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1118 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1119 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1120
1121 if (!hlist_empty(bucket))
1122 return bucket->first;
1123 }
1124 return NULL;
1125}
1126EXPORT_SYMBOL(seq_hlist_next_percpu);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001127
1128void __init seq_file_init(void)
1129{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -07001130 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001131}