blob: 3b20e21604e74aaa65defc63b82f7fdbdb193953 [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;
171 size_t size = iov_iter_count(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 size_t copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 size_t n;
174 void *p;
175 int err = 0;
176
Ingo Molnar0ac17592006-03-23 03:00:37 -0800177 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100180 * if request is to read from zero offset, reset iterator to first
181 * record as it might have been already advanced by previous requests
182 */
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100183 if (iocb->ki_pos == 0) {
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100184 m->index = 0;
Miklos Szeredicf5eeba2017-11-15 11:34:58 +0100185 m->count = 0;
186 }
Tomasz Majchrzake5227512016-11-29 15:18:20 +0100187
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100188 /* Don't assume ki_pos is where we left it */
189 if (unlikely(iocb->ki_pos != m->read_pos)) {
190 while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
Earl Chew7904ac82012-03-21 16:33:43 -0700191 ;
192 if (err) {
193 /* With prejudice... */
194 m->read_pos = 0;
Earl Chew7904ac82012-03-21 16:33:43 -0700195 m->index = 0;
196 m->count = 0;
197 goto Done;
198 } else {
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100199 m->read_pos = iocb->ki_pos;
Earl Chew7904ac82012-03-21 16:33:43 -0700200 }
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* grab buffer if we didn't have one */
204 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700205 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!m->buf)
207 goto Enomem;
208 }
209 /* if not empty - flush it first */
210 if (m->count) {
211 n = min(m->count, size);
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100212 if (copy_to_iter(m->buf + m->from, n, iter) != n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto Efault;
214 m->count -= n;
215 m->from += n;
216 size -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 copied += n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!size)
219 goto Done;
220 }
221 /* we need at least one record in buffer */
NeilBrown1f4aace2018-08-17 15:44:41 -0700222 m->from = 0;
223 p = m->op->start(m, &m->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 err = PTR_ERR(p);
226 if (!p || IS_ERR(p))
227 break;
228 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400229 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
Al Viro521b5d02008-03-28 00:46:41 -0400231 if (unlikely(err))
232 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400233 if (unlikely(!m->count)) {
NeilBrown1f4aace2018-08-17 15:44:41 -0700234 p = m->op->next(m, p, &m->index);
Al Viro4cdfe842008-08-24 07:45:33 -0400235 continue;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (m->count < m->size)
238 goto Fill;
239 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 }
247 m->op->stop(m, p);
248 m->count = 0;
249 goto Done;
250Fill:
251 /* they want more? let's try to get some more */
NeilBrown1f4aace2018-08-17 15:44:41 -0700252 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 size_t offs = m->count;
NeilBrown1f4aace2018-08-17 15:44:41 -0700254 loff_t pos = m->index;
255
256 p = m->op->next(m, p, &m->index);
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700257 if (pos == m->index) {
Joe Perchesa3963012020-06-04 16:51:02 -0700258 pr_info_ratelimited("buggy .next function %ps did not update position index\n",
259 m->op->next);
NeilBrown1f4aace2018-08-17 15:44:41 -0700260 m->index++;
Vasily Averin3bfa7e12020-04-10 14:34:06 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!p || IS_ERR(p)) {
263 err = PTR_ERR(p);
264 break;
265 }
NeilBrown1f4aace2018-08-17 15:44:41 -0700266 if (m->count >= size)
267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700269 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400271 if (likely(err <= 0))
272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 m->op->stop(m, p);
276 n = min(m->count, size);
Christoph Hellwigd4d50712020-11-04 09:27:33 +0100277 if (copy_to_iter(m->buf, n, iter) != n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto Efault;
279 copied += n;
280 m->count -= n;
NeilBrown1f4aace2018-08-17 15:44:41 -0700281 m->from = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282Done:
283 if (!copied)
284 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800285 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;
294Efault:
295 err = -EFAULT;
296 goto Done;
297}
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/**
362 * seq_escape - print string into buffer, escaping some characters
363 * @m: target buffer
364 * @s: string
365 * @esc: set of characters that need escaping
366 *
367 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700368 * @esc with usual octal escape.
369 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700371void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800373 char *buf;
374 size_t size = seq_get_buf(m, &buf);
375 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800377 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
378 seq_commit(m, ret < size ? ret : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380EXPORT_SYMBOL(seq_escape);
381
J. Bruce Fieldsea053e12019-06-19 12:30:13 -0400382void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
383{
384 char *buf;
385 size_t size = seq_get_buf(m, &buf);
386 int ret;
387
388 ret = string_escape_mem_ascii(src, isz, buf, size);
389 seq_commit(m, ret < size ? ret : -1);
390}
391EXPORT_SYMBOL(seq_escape_mem_ascii);
392
Joe Perches6798a8c2015-09-11 13:07:48 -0700393void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int len;
396
397 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (m->count + len < m->size) {
400 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700404 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Steven Whitehousea4808142012-06-11 13:16:35 +0100406EXPORT_SYMBOL(seq_vprintf);
407
Joe Perches6798a8c2015-09-11 13:07:48 -0700408void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100409{
Steven Whitehousea4808142012-06-11 13:16:35 +0100410 va_list args;
411
412 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700413 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100414 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416EXPORT_SYMBOL(seq_printf);
417
Török Edwin74e2f332008-11-22 13:28:48 +0200418/**
Török Edwin958086d2008-11-23 23:24:53 +0200419 * mangle_path - mangle and copy path to buffer beginning
420 * @s: buffer start
421 * @p: beginning of path in above buffer
422 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200423 *
424 * Copy the path from @p to @s, replacing each occurrence of character from
425 * @esc with usual octal escape.
426 * Returns pointer past last written character in @s, or NULL in case of
427 * failure.
428 */
Al Viro8c9379e2011-12-08 20:18:57 -0500429char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100430{
431 while (s <= p) {
432 char c = *p++;
433 if (!c) {
434 return s;
435 } else if (!strchr(esc, c)) {
436 *s++ = c;
437 } else if (s + 4 > p) {
438 break;
439 } else {
440 *s++ = '\\';
441 *s++ = '0' + ((c & 0300) >> 6);
442 *s++ = '0' + ((c & 070) >> 3);
443 *s++ = '0' + (c & 07);
444 }
445 }
446 return NULL;
447}
Ingo Molnar604094f2008-11-28 18:03:22 +0100448EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100449
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800450/**
451 * seq_path - seq_file interface to print a pathname
452 * @m: the seq_file handle
453 * @path: the struct path to print
454 * @esc: set of characters to escape in the output
455 *
456 * return the absolute path of 'path', as represented by the
457 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100458 */
Al Viro8c9379e2011-12-08 20:18:57 -0500459int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Miklos Szeredif8439802009-09-21 14:48:36 +0200461 char *buf;
462 size_t size = seq_get_buf(m, &buf);
463 int res = -1;
464
465 if (size) {
466 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200468 char *end = mangle_path(buf, p, esc);
469 if (end)
470 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200473 seq_commit(m, res);
474
475 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477EXPORT_SYMBOL(seq_path);
478
Miklos Szeredi2726d562015-06-19 10:30:28 +0200479/**
480 * seq_file_path - seq_file interface to print a pathname of a file
481 * @m: the seq_file handle
482 * @file: the struct file to print
483 * @esc: set of characters to escape in the output
484 *
485 * return the absolute path to the file.
486 */
487int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
488{
489 return seq_path(m, &file->f_path, esc);
490}
491EXPORT_SYMBOL(seq_file_path);
492
Ram Pai6092d042008-03-27 13:06:20 +0100493/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100494 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100495 */
Al Viro8c9379e2011-12-08 20:18:57 -0500496int seq_path_root(struct seq_file *m, const struct path *path,
497 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100498{
Miklos Szeredif8439802009-09-21 14:48:36 +0200499 char *buf;
500 size_t size = seq_get_buf(m, &buf);
501 int res = -ENAMETOOLONG;
502
503 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100504 char *p;
505
Miklos Szeredif8439802009-09-21 14:48:36 +0200506 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500507 if (!p)
508 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200509 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100510 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200511 char *end = mangle_path(buf, p, esc);
512 if (end)
513 res = end - buf;
514 else
515 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100516 }
517 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200518 seq_commit(m, res);
519
Al Viro02125a82011-12-05 08:43:34 -0500520 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100521}
522
523/*
Ram Pai6092d042008-03-27 13:06:20 +0100524 * returns the path of the 'dentry' from the root of its filesystem.
525 */
Al Viro8c9379e2011-12-08 20:18:57 -0500526int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100527{
Miklos Szeredif8439802009-09-21 14:48:36 +0200528 char *buf;
529 size_t size = seq_get_buf(m, &buf);
530 int res = -1;
531
532 if (size) {
533 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +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;
Ram Pai6092d042008-03-27 13:06:20 +0100538 }
539 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200540 seq_commit(m, res);
541
542 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100543}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700544EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static void *single_start(struct seq_file *p, loff_t *pos)
547{
548 return NULL + (*pos == 0);
549}
550
551static void *single_next(struct seq_file *p, void *v, loff_t *pos)
552{
553 ++*pos;
554 return NULL;
555}
556
557static void single_stop(struct seq_file *p, void *v)
558{
559}
560
561int single_open(struct file *file, int (*show)(struct seq_file *, void *),
562 void *data)
563{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700564 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int res = -ENOMEM;
566
567 if (op) {
568 op->start = single_start;
569 op->next = single_next;
570 op->stop = single_stop;
571 op->show = show;
572 res = seq_open(file, op);
573 if (!res)
574 ((struct seq_file *)file->private_data)->private = data;
575 else
576 kfree(op);
577 }
578 return res;
579}
580EXPORT_SYMBOL(single_open);
581
Al Viro2043f492013-03-31 13:43:23 -0400582int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
583 void *data, size_t size)
584{
Heiko Carstens058504e2014-07-02 15:22:37 -0700585 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400586 int ret;
587 if (!buf)
588 return -ENOMEM;
589 ret = single_open(file, show, data);
590 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700591 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400592 return ret;
593 }
594 ((struct seq_file *)file->private_data)->buf = buf;
595 ((struct seq_file *)file->private_data)->size = size;
596 return 0;
597}
598EXPORT_SYMBOL(single_open_size);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600int single_release(struct inode *inode, struct file *file)
601{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800602 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int res = seq_release(inode, file);
604 kfree(op);
605 return res;
606}
607EXPORT_SYMBOL(single_release);
608
609int seq_release_private(struct inode *inode, struct file *file)
610{
611 struct seq_file *seq = file->private_data;
612
613 kfree(seq->private);
614 seq->private = NULL;
615 return seq_release(inode, file);
616}
617EXPORT_SYMBOL(seq_release_private);
618
Pavel Emelyanov39699032007-10-10 02:28:42 -0700619void *__seq_open_private(struct file *f, const struct seq_operations *ops,
620 int psize)
621{
622 int rc;
623 void *private;
624 struct seq_file *seq;
625
Alexey Dobriyand64d01a2018-04-10 16:34:49 -0700626 private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
Pavel Emelyanov39699032007-10-10 02:28:42 -0700627 if (private == NULL)
628 goto out;
629
630 rc = seq_open(f, ops);
631 if (rc < 0)
632 goto out_free;
633
634 seq = f->private_data;
635 seq->private = private;
636 return private;
637
638out_free:
639 kfree(private);
640out:
641 return NULL;
642}
643EXPORT_SYMBOL(__seq_open_private);
644
645int seq_open_private(struct file *filp, const struct seq_operations *ops,
646 int psize)
647{
648 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
649}
650EXPORT_SYMBOL(seq_open_private);
651
Joe Perches6798a8c2015-09-11 13:07:48 -0700652void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Joe Perches6798a8c2015-09-11 13:07:48 -0700654 if (m->count >= m->size)
655 return;
656
657 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659EXPORT_SYMBOL(seq_putc);
660
Joe Perches6798a8c2015-09-11 13:07:48 -0700661void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700664
665 if (m->count + len >= m->size) {
666 seq_set_overflow(m);
667 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700669 memcpy(m->buf + m->count, s, len);
670 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700673
Andrei Vagind1be35c2018-04-10 16:31:16 -0700674/**
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700675 * A helper routine for putting decimal numbers without rich format of printf().
676 * only 'unsigned long long' is supported.
Andrei Vagind1be35c2018-04-10 16:31:16 -0700677 * @m: seq_file identifying the buffer to which data should be written
678 * @delimiter: a string which is printed before the number
679 * @num: the number
680 * @width: a minimum field width
681 *
682 * This routine will put strlen(delimiter) + number into seq_filed.
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700683 * This routine is very quick when you show lots of numbers.
684 * In usual cases, it will be better to use seq_printf(). It's easier to read.
685 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700686void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
687 unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700688{
689 int len;
690
691 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
692 goto overflow;
693
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700694 if (delimiter && delimiter[0]) {
695 if (delimiter[1] == 0)
696 seq_putc(m, delimiter[0]);
697 else
698 seq_puts(m, delimiter);
699 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700700
Andrei Vagind1be35c2018-04-10 16:31:16 -0700701 if (!width)
702 width = 1;
703
704 if (m->count + width >= m->size)
Joe Perches75ba1d02016-10-07 17:02:20 -0700705 goto overflow;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700706
Andrei Vagind1be35c2018-04-10 16:31:16 -0700707 len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700708 if (!len)
709 goto overflow;
Joe Perches75ba1d02016-10-07 17:02:20 -0700710
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700711 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700712 return;
713
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700714overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700715 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700716}
Andrei Vagind1be35c2018-04-10 16:31:16 -0700717
718void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
719 unsigned long long num)
720{
721 return seq_put_decimal_ull_width(m, delimiter, num, 0);
722}
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700723EXPORT_SYMBOL(seq_put_decimal_ull);
724
Andrei Vagin0e3dc012018-04-10 16:30:44 -0700725/**
726 * seq_put_hex_ll - put a number in hexadecimal notation
727 * @m: seq_file identifying the buffer to which data should be written
728 * @delimiter: a string which is printed before the number
729 * @v: the number
730 * @width: a minimum field width
731 *
732 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
733 *
734 * This routine is very quick when you show lots of numbers.
735 * In usual cases, it will be better to use seq_printf(). It's easier to read.
736 */
737void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
738 unsigned long long v, unsigned int width)
739{
740 unsigned int len;
741 int i;
742
743 if (delimiter && delimiter[0]) {
744 if (delimiter[1] == 0)
745 seq_putc(m, delimiter[0]);
746 else
747 seq_puts(m, delimiter);
748 }
749
750 /* If x is 0, the result of __builtin_clzll is undefined */
751 if (v == 0)
752 len = 1;
753 else
754 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
755
756 if (len < width)
757 len = width;
758
759 if (m->count + len > m->size) {
760 seq_set_overflow(m);
761 return;
762 }
763
764 for (i = len - 1; i >= 0; i--) {
765 m->buf[m->count + i] = hex_asc[0xf & v];
766 v = v >> 4;
767 }
768 m->count += len;
769}
770
Joe Perches75ba1d02016-10-07 17:02:20 -0700771void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700772{
Joe Perches75ba1d02016-10-07 17:02:20 -0700773 int len;
774
775 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
776 goto overflow;
777
Andrei Vagin48dffbf2018-04-10 16:31:23 -0700778 if (delimiter && delimiter[0]) {
779 if (delimiter[1] == 0)
780 seq_putc(m, delimiter[0]);
781 else
782 seq_puts(m, delimiter);
783 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700784
785 if (m->count + 2 >= m->size)
786 goto overflow;
787
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700788 if (num < 0) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700789 m->buf[m->count++] = '-';
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700790 num = -num;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700791 }
Joe Perches75ba1d02016-10-07 17:02:20 -0700792
793 if (num < 10) {
794 m->buf[m->count++] = num + '0';
795 return;
796 }
797
Andrei Vagind1be35c2018-04-10 16:31:16 -0700798 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
Joe Perches75ba1d02016-10-07 17:02:20 -0700799 if (!len)
800 goto overflow;
801
802 m->count += len;
803 return;
804
805overflow:
806 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700807}
808EXPORT_SYMBOL(seq_put_decimal_ll);
809
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700810/**
811 * seq_write - write arbitrary data to buffer
812 * @seq: seq_file identifying the buffer to which data should be written
813 * @data: data address
814 * @len: number of bytes
815 *
816 * Return 0 on success, non-zero otherwise.
817 */
818int seq_write(struct seq_file *seq, const void *data, size_t len)
819{
820 if (seq->count + len < seq->size) {
821 memcpy(seq->buf + seq->count, data, len);
822 seq->count += len;
823 return 0;
824 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700825 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700826 return -1;
827}
828EXPORT_SYMBOL(seq_write);
829
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800830/**
831 * seq_pad - write padding spaces to buffer
832 * @m: seq_file identifying the buffer to which data should be written
833 * @c: the byte to append after padding if non-zero
834 */
835void seq_pad(struct seq_file *m, char c)
836{
837 int size = m->pad_until - m->count;
Andrei Vagin8cfa67b42018-04-10 16:30:47 -0700838 if (size > 0) {
839 if (size + m->count > m->size) {
840 seq_set_overflow(m);
841 return;
842 }
843 memset(m->buf + m->count, ' ', size);
844 m->count += size;
845 }
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800846 if (c)
847 seq_putc(m, c);
848}
849EXPORT_SYMBOL(seq_pad);
850
Andy Shevchenko37607102015-09-09 15:38:33 -0700851/* A complete analogue of print_hex_dump() */
852void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
853 int rowsize, int groupsize, const void *buf, size_t len,
854 bool ascii)
855{
856 const u8 *ptr = buf;
857 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800858 char *buffer;
859 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700860 int ret;
861
862 if (rowsize != 16 && rowsize != 32)
863 rowsize = 16;
864
865 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
866 linelen = min(remaining, rowsize);
867 remaining -= rowsize;
868
869 switch (prefix_type) {
870 case DUMP_PREFIX_ADDRESS:
871 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
872 break;
873 case DUMP_PREFIX_OFFSET:
874 seq_printf(m, "%s%.8x: ", prefix_str, i);
875 break;
876 default:
877 seq_printf(m, "%s", prefix_str);
878 break;
879 }
880
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800881 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700882 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800883 buffer, size, ascii);
884 seq_commit(m, ret < size ? ret : -1);
885
886 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700887 }
888}
889EXPORT_SYMBOL(seq_hex_dump);
890
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700891struct list_head *seq_list_start(struct list_head *head, loff_t pos)
892{
893 struct list_head *lh;
894
895 list_for_each(lh, head)
896 if (pos-- == 0)
897 return lh;
898
899 return NULL;
900}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700901EXPORT_SYMBOL(seq_list_start);
902
903struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
904{
905 if (!pos)
906 return head;
907
908 return seq_list_start(head, pos - 1);
909}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700910EXPORT_SYMBOL(seq_list_start_head);
911
912struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
913{
914 struct list_head *lh;
915
916 lh = ((struct list_head *)v)->next;
917 ++*ppos;
918 return lh == head ? NULL : lh;
919}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700920EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000921
922/**
923 * seq_hlist_start - start an iteration of a hlist
924 * @head: the head of the hlist
925 * @pos: the start position of the sequence
926 *
927 * Called at seq_file->op->start().
928 */
929struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
930{
931 struct hlist_node *node;
932
933 hlist_for_each(node, head)
934 if (pos-- == 0)
935 return node;
936 return NULL;
937}
938EXPORT_SYMBOL(seq_hlist_start);
939
940/**
941 * seq_hlist_start_head - start an iteration of a hlist
942 * @head: the head of the hlist
943 * @pos: the start position of the sequence
944 *
945 * Called at seq_file->op->start(). Call this function if you want to
946 * print a header at the top of the output.
947 */
948struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
949{
950 if (!pos)
951 return SEQ_START_TOKEN;
952
953 return seq_hlist_start(head, pos - 1);
954}
955EXPORT_SYMBOL(seq_hlist_start_head);
956
957/**
958 * seq_hlist_next - move to the next position of the hlist
959 * @v: the current iterator
960 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800961 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000962 *
963 * Called at seq_file->op->next().
964 */
965struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
966 loff_t *ppos)
967{
968 struct hlist_node *node = v;
969
970 ++*ppos;
971 if (v == SEQ_START_TOKEN)
972 return head->first;
973 else
974 return node->next;
975}
976EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000977
978/**
979 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
980 * @head: the head of the hlist
981 * @pos: the start position of the sequence
982 *
983 * Called at seq_file->op->start().
984 *
985 * This list-traversal primitive may safely run concurrently with
986 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
987 * as long as the traversal is guarded by rcu_read_lock().
988 */
989struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
990 loff_t pos)
991{
992 struct hlist_node *node;
993
994 __hlist_for_each_rcu(node, head)
995 if (pos-- == 0)
996 return node;
997 return NULL;
998}
999EXPORT_SYMBOL(seq_hlist_start_rcu);
1000
1001/**
1002 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
1003 * @head: the head of the hlist
1004 * @pos: the start position of the sequence
1005 *
1006 * Called at seq_file->op->start(). Call this function if you want to
1007 * print a header at the top of the output.
1008 *
1009 * This list-traversal primitive may safely run concurrently with
1010 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1011 * as long as the traversal is guarded by rcu_read_lock().
1012 */
1013struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
1014 loff_t pos)
1015{
1016 if (!pos)
1017 return SEQ_START_TOKEN;
1018
1019 return seq_hlist_start_rcu(head, pos - 1);
1020}
1021EXPORT_SYMBOL(seq_hlist_start_head_rcu);
1022
1023/**
1024 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
1025 * @v: the current iterator
1026 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -08001027 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +00001028 *
1029 * Called at seq_file->op->next().
1030 *
1031 * This list-traversal primitive may safely run concurrently with
1032 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
1033 * as long as the traversal is guarded by rcu_read_lock().
1034 */
1035struct hlist_node *seq_hlist_next_rcu(void *v,
1036 struct hlist_head *head,
1037 loff_t *ppos)
1038{
1039 struct hlist_node *node = v;
1040
1041 ++*ppos;
1042 if (v == SEQ_START_TOKEN)
1043 return rcu_dereference(head->first);
1044 else
1045 return rcu_dereference(node->next);
1046}
1047EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -04001048
1049/**
1050 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
1051 * @head: pointer to percpu array of struct hlist_heads
1052 * @cpu: pointer to cpu "cursor"
1053 * @pos: start position of sequence
1054 *
1055 * Called at seq_file->op->start().
1056 */
1057struct hlist_node *
1058seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
1059{
1060 struct hlist_node *node;
1061
1062 for_each_possible_cpu(*cpu) {
1063 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
1064 if (pos-- == 0)
1065 return node;
1066 }
1067 }
1068 return NULL;
1069}
1070EXPORT_SYMBOL(seq_hlist_start_percpu);
1071
1072/**
1073 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
1074 * @v: pointer to current hlist_node
1075 * @head: pointer to percpu array of struct hlist_heads
1076 * @cpu: pointer to cpu "cursor"
1077 * @pos: start position of sequence
1078 *
1079 * Called at seq_file->op->next().
1080 */
1081struct hlist_node *
1082seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
1083 int *cpu, loff_t *pos)
1084{
1085 struct hlist_node *node = v;
1086
1087 ++*pos;
1088
1089 if (node->next)
1090 return node->next;
1091
1092 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
1093 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
1094 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1095
1096 if (!hlist_empty(bucket))
1097 return bucket->first;
1098 }
1099 return NULL;
1100}
1101EXPORT_SYMBOL(seq_hlist_next_percpu);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001102
1103void __init seq_file_init(void)
1104{
Alexey Dobriyand64d01a2018-04-10 16:34:49 -07001105 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
Alexey Dobriyan0965232032018-04-10 16:34:45 -07001106}