Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Perches | a396301 | 2020-06-04 16:51:02 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 11 | #include <linux/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/fs.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 13 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
Eric W. Biederman | adb37c4 | 2012-05-23 18:01:20 -0600 | [diff] [blame] | 17 | #include <linux/cred.h> |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 18 | #include <linux/mm.h> |
Andy Shevchenko | 3760710 | 2015-09-09 15:38:33 -0700 | [diff] [blame] | 19 | #include <linux/printk.h> |
Andy Shevchenko | 25c6bb7 | 2015-11-06 16:32:40 -0800 | [diff] [blame] | 20 | #include <linux/string_helpers.h> |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 21 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/page.h> |
| 25 | |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 26 | static struct kmem_cache *seq_file_cache __ro_after_init; |
| 27 | |
KAMEZAWA Hiroyuki | e075f59 | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 28 | static void seq_set_overflow(struct seq_file *m) |
| 29 | { |
| 30 | m->count = m->size; |
| 31 | } |
| 32 | |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 33 | static void *seq_buf_alloc(unsigned long size) |
| 34 | { |
Eric Sandeen | 8cae8cd | 2021-07-13 17:49:23 +0200 | [diff] [blame] | 35 | if (unlikely(size > MAX_RW_COUNT)) |
| 36 | return NULL; |
| 37 | |
Alexey Dobriyan | d64d01a | 2018-04-10 16:34:49 -0700 | [diff] [blame] | 38 | return kvmalloc(size, GFP_KERNEL_ACCOUNT); |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /** |
| 42 | * seq_open - initialize sequential file |
| 43 | * @file: file we initialize |
| 44 | * @op: method table describing the sequence |
| 45 | * |
| 46 | * seq_open() sets @file, associating it with a sequence described |
| 47 | * by @op. @op->start() sets the iterator up and returns the first |
| 48 | * element of sequence. @op->stop() shuts it down. @op->next() |
| 49 | * returns the next element of sequence. @op->show() prints element |
| 50 | * into the buffer. In case of error ->start() and ->next() return |
| 51 | * ERR_PTR(error). In the end of sequence they return %NULL. ->show() |
| 52 | * returns 0 in case of success and negative number in case of error. |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 53 | * Returning SEQ_SKIP means "discard this element and move on". |
Yann Droneaud | 460b865 | 2015-06-30 14:57:36 -0700 | [diff] [blame] | 54 | * Note: seq_open() will allocate a struct seq_file and store its |
| 55 | * pointer in @file->private_data. This pointer should not be modified. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 57 | int seq_open(struct file *file, const struct seq_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Yann Droneaud | 189f984 | 2015-06-30 14:57:33 -0700 | [diff] [blame] | 59 | struct seq_file *p; |
Al Viro | 1abe77b | 2005-11-07 17:15:34 -0500 | [diff] [blame] | 60 | |
Yann Droneaud | 189f984 | 2015-06-30 14:57:33 -0700 | [diff] [blame] | 61 | WARN_ON(file->private_data); |
| 62 | |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 63 | p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL); |
Yann Droneaud | 189f984 | 2015-06-30 14:57:33 -0700 | [diff] [blame] | 64 | if (!p) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | file->private_data = p; |
| 68 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 69 | mutex_init(&p->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | p->op = op; |
Linus Torvalds | 34dbbcd | 2016-04-14 11:22:00 -0700 | [diff] [blame] | 71 | |
| 72 | // No refcounting: the lifetime of 'p' is constrained |
| 73 | // to the lifetime of the file. |
| 74 | p->file = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
Eric Biederman | 8f19d47 | 2009-02-18 14:48:16 -0800 | [diff] [blame] | 77 | * seq_files support lseek() and pread(). They do not implement |
| 78 | * write() at all, but we clear FMODE_PWRITE here for historical |
| 79 | * reasons. |
| 80 | * |
| 81 | * If a client of seq_files a) implements file.write() and b) wishes to |
| 82 | * support pwrite() then that client will need to implement its own |
| 83 | * file.open() which calls seq_open() and then sets FMODE_PWRITE. |
| 84 | */ |
| 85 | file->f_mode &= ~FMODE_PWRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | EXPORT_SYMBOL(seq_open); |
| 89 | |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 90 | static int traverse(struct seq_file *m, loff_t offset) |
| 91 | { |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 92 | loff_t pos = 0; |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 93 | int error = 0; |
| 94 | void *p; |
| 95 | |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 96 | m->index = 0; |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 97 | m->count = m->from = 0; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 98 | if (!offset) |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 99 | return 0; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 100 | |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 101 | if (!m->buf) { |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 102 | m->buf = seq_buf_alloc(m->size = PAGE_SIZE); |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 103 | if (!m->buf) |
| 104 | return -ENOMEM; |
| 105 | } |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 106 | p = m->op->start(m, &m->index); |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 107 | while (p) { |
| 108 | error = PTR_ERR(p); |
| 109 | if (IS_ERR(p)) |
| 110 | break; |
| 111 | error = m->op->show(m, p); |
| 112 | if (error < 0) |
| 113 | break; |
| 114 | if (unlikely(error)) { |
| 115 | error = 0; |
| 116 | m->count = 0; |
| 117 | } |
Joe Perches | 1f33c41 | 2014-09-29 16:08:21 -0700 | [diff] [blame] | 118 | if (seq_has_overflowed(m)) |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 119 | goto Eoverflow; |
NeilBrown | 6a2aeab | 2019-08-13 15:37:44 -0700 | [diff] [blame] | 120 | p = m->op->next(m, p, &m->index); |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 121 | if (pos + m->count > offset) { |
| 122 | m->from = offset - pos; |
| 123 | m->count -= m->from; |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | pos += m->count; |
| 127 | m->count = 0; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 128 | if (pos == offset) |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 129 | break; |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 130 | } |
| 131 | m->op->stop(m, p); |
| 132 | return error; |
| 133 | |
| 134 | Eoverflow: |
| 135 | m->op->stop(m, p); |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 136 | kvfree(m->buf); |
Al Viro | 801a760 | 2013-11-19 01:20:43 +0000 | [diff] [blame] | 137 | m->count = 0; |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 138 | m->buf = seq_buf_alloc(m->size <<= 1); |
Eric Biederman | 33da889 | 2009-02-04 15:12:25 -0800 | [diff] [blame] | 139 | return !m->buf ? -ENOMEM : -EAGAIN; |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /** |
| 143 | * seq_read - ->read() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 144 | * @file: the file to read from |
| 145 | * @buf: the buffer to read to |
| 146 | * @size: the maximum number of bytes to read |
| 147 | * @ppos: the current position in the file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * |
| 149 | * Ready-made ->f_op->read() |
| 150 | */ |
| 151 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) |
| 152 | { |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 153 | struct iovec iov = { .iov_base = buf, .iov_len = size}; |
| 154 | struct kiocb kiocb; |
| 155 | struct iov_iter iter; |
| 156 | ssize_t ret; |
| 157 | |
| 158 | init_sync_kiocb(&kiocb, file); |
| 159 | iov_iter_init(&iter, READ, &iov, 1, size); |
| 160 | |
| 161 | kiocb.ki_pos = *ppos; |
| 162 | ret = seq_read_iter(&kiocb, &iter); |
| 163 | *ppos = kiocb.ki_pos; |
| 164 | return ret; |
| 165 | } |
| 166 | EXPORT_SYMBOL(seq_read); |
| 167 | |
| 168 | /* |
| 169 | * Ready-made ->f_op->read_iter() |
| 170 | */ |
| 171 | ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 172 | { |
| 173 | struct seq_file *m = iocb->ki_filp->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | size_t copied = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | size_t n; |
| 176 | void *p; |
| 177 | int err = 0; |
| 178 | |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 179 | if (!iov_iter_count(iter)) |
| 180 | return 0; |
| 181 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 182 | mutex_lock(&m->lock); |
Eric Biederman | 8f19d47 | 2009-02-18 14:48:16 -0800 | [diff] [blame] | 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | /* |
Tomasz Majchrzak | e522751 | 2016-11-29 15:18:20 +0100 | [diff] [blame] | 185 | * if request is to read from zero offset, reset iterator to first |
| 186 | * record as it might have been already advanced by previous requests |
| 187 | */ |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 188 | if (iocb->ki_pos == 0) { |
Tomasz Majchrzak | e522751 | 2016-11-29 15:18:20 +0100 | [diff] [blame] | 189 | m->index = 0; |
Miklos Szeredi | cf5eeba | 2017-11-15 11:34:58 +0100 | [diff] [blame] | 190 | m->count = 0; |
| 191 | } |
Tomasz Majchrzak | e522751 | 2016-11-29 15:18:20 +0100 | [diff] [blame] | 192 | |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 193 | /* Don't assume ki_pos is where we left it */ |
| 194 | if (unlikely(iocb->ki_pos != m->read_pos)) { |
| 195 | while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN) |
Earl Chew | 7904ac8 | 2012-03-21 16:33:43 -0700 | [diff] [blame] | 196 | ; |
| 197 | if (err) { |
| 198 | /* With prejudice... */ |
| 199 | m->read_pos = 0; |
Earl Chew | 7904ac8 | 2012-03-21 16:33:43 -0700 | [diff] [blame] | 200 | m->index = 0; |
| 201 | m->count = 0; |
| 202 | goto Done; |
| 203 | } else { |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 204 | m->read_pos = iocb->ki_pos; |
Earl Chew | 7904ac8 | 2012-03-21 16:33:43 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /* grab buffer if we didn't have one */ |
| 209 | if (!m->buf) { |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 210 | m->buf = seq_buf_alloc(m->size = PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (!m->buf) |
| 212 | goto Enomem; |
| 213 | } |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 214 | // something left in the buffer - copy it out first |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (m->count) { |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 216 | n = copy_to_iter(m->buf + m->from, m->count, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | m->count -= n; |
| 218 | m->from += n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | copied += n; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 220 | if (m->count) // hadn't managed to copy everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | goto Done; |
| 222 | } |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 223 | // get a non-empty record in the buffer |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 224 | m->from = 0; |
| 225 | p = m->op->start(m, &m->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | err = PTR_ERR(p); |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 228 | if (!p || IS_ERR(p)) // EOF or an error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | break; |
| 230 | err = m->op->show(m, p); |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 231 | if (err < 0) // hard error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 233 | if (unlikely(err)) // ->show() says "skip it" |
Al Viro | 521b5d0 | 2008-03-28 00:46:41 -0400 | [diff] [blame] | 234 | m->count = 0; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 235 | if (unlikely(!m->count)) { // empty record |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 236 | p = m->op->next(m, p, &m->index); |
Al Viro | 4cdfe84 | 2008-08-24 07:45:33 -0400 | [diff] [blame] | 237 | continue; |
| 238 | } |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 239 | if (!seq_has_overflowed(m)) // got it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | goto Fill; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 241 | // need a bigger buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | m->op->stop(m, p); |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 243 | kvfree(m->buf); |
Al Viro | 801a760 | 2013-11-19 01:20:43 +0000 | [diff] [blame] | 244 | m->count = 0; |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 245 | m->buf = seq_buf_alloc(m->size <<= 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (!m->buf) |
| 247 | goto Enomem; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 248 | p = m->op->start(m, &m->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 250 | // EOF or an error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | m->op->stop(m, p); |
| 252 | m->count = 0; |
| 253 | goto Done; |
| 254 | Fill: |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 255 | // one non-empty record is in the buffer; if they want more, |
| 256 | // try to fit more in, but in any case we need to advance |
| 257 | // the iterator once for every record shown. |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 258 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | size_t offs = m->count; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 260 | loff_t pos = m->index; |
| 261 | |
| 262 | p = m->op->next(m, p, &m->index); |
Vasily Averin | 3bfa7e1 | 2020-04-10 14:34:06 -0700 | [diff] [blame] | 263 | if (pos == m->index) { |
Joe Perches | a396301 | 2020-06-04 16:51:02 -0700 | [diff] [blame] | 264 | pr_info_ratelimited("buggy .next function %ps did not update position index\n", |
| 265 | m->op->next); |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 266 | m->index++; |
Vasily Averin | 3bfa7e1 | 2020-04-10 14:34:06 -0700 | [diff] [blame] | 267 | } |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 268 | if (!p || IS_ERR(p)) // no next record for us |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | break; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 270 | if (m->count >= iov_iter_count(iter)) |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 271 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | err = m->op->show(m, p); |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 273 | if (err > 0) { // ->show() says "skip it" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | m->count = offs; |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 275 | } else if (err || seq_has_overflowed(m)) { |
| 276 | m->count = offs; |
| 277 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | m->op->stop(m, p); |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 281 | n = copy_to_iter(m->buf, m->count, iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | copied += n; |
| 283 | m->count -= n; |
NeilBrown | 1f4aace | 2018-08-17 15:44:41 -0700 | [diff] [blame] | 284 | m->from = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | Done: |
Al Viro | 4bbf439 | 2020-11-12 14:40:37 -0500 | [diff] [blame] | 286 | if (unlikely(!copied)) { |
| 287 | copied = m->count ? -EFAULT : err; |
| 288 | } else { |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 289 | iocb->ki_pos += copied; |
Eric Biederman | 8f19d47 | 2009-02-18 14:48:16 -0800 | [diff] [blame] | 290 | m->read_pos += copied; |
| 291 | } |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 292 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return copied; |
| 294 | Enomem: |
| 295 | err = -ENOMEM; |
| 296 | goto Done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Christoph Hellwig | d4d5071 | 2020-11-04 09:27:33 +0100 | [diff] [blame] | 298 | EXPORT_SYMBOL(seq_read_iter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | /** |
| 301 | * seq_lseek - ->llseek() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 302 | * @file: the file in question |
| 303 | * @offset: new position |
Randy Dunlap | 254adaa | 2013-01-09 17:13:00 -0800 | [diff] [blame] | 304 | * @whence: 0 for absolute, 1 for relative position |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * |
| 306 | * Ready-made ->f_op->llseek() |
| 307 | */ |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 308 | loff_t seq_lseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Joe Perches | 8209e2f | 2010-09-04 18:52:49 -0700 | [diff] [blame] | 310 | struct seq_file *m = file->private_data; |
David Sterba | 16abef0 | 2008-04-22 15:09:22 +0200 | [diff] [blame] | 311 | loff_t retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 313 | mutex_lock(&m->lock); |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 314 | switch (whence) { |
Andrew Morton | 5e62ade | 2013-02-27 17:03:22 -0800 | [diff] [blame] | 315 | case SEEK_CUR: |
| 316 | offset += file->f_pos; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 317 | fallthrough; |
Andrew Morton | 5e62ade | 2013-02-27 17:03:22 -0800 | [diff] [blame] | 318 | 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 Morton | 5e62ade | 2013-02-27 17:03:22 -0800 | [diff] [blame] | 329 | m->index = 0; |
| 330 | m->count = 0; |
| 331 | } else { |
| 332 | m->read_pos = offset; |
| 333 | retval = file->f_pos = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
Gu Zheng | 05e1674 | 2013-10-25 18:15:06 +0800 | [diff] [blame] | 335 | } else { |
| 336 | file->f_pos = offset; |
Andrew Morton | 5e62ade | 2013-02-27 17:03:22 -0800 | [diff] [blame] | 337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Alexey Dobriyan | 00c5746 | 2007-07-15 23:40:22 -0700 | [diff] [blame] | 339 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return retval; |
| 341 | } |
| 342 | EXPORT_SYMBOL(seq_lseek); |
| 343 | |
| 344 | /** |
| 345 | * seq_release - free the structures associated with sequential file. |
| 346 | * @file: file in question |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 347 | * @inode: its inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * |
| 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 | */ |
| 352 | int seq_release(struct inode *inode, struct file *file) |
| 353 | { |
Joe Perches | 8209e2f | 2010-09-04 18:52:49 -0700 | [diff] [blame] | 354 | struct seq_file *m = file->private_data; |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 355 | kvfree(m->buf); |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 356 | kmem_cache_free(seq_file_cache, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | EXPORT_SYMBOL(seq_release); |
| 360 | |
| 361 | /** |
Andy Shevchenko | 1d31aa1 | 2021-06-30 18:55:34 -0700 | [diff] [blame] | 362 | * seq_escape_mem - print data into buffer, escaping some characters |
| 363 | * @m: target buffer |
| 364 | * @src: source buffer |
| 365 | * @len: size of source buffer |
| 366 | * @flags: flags to pass to string_escape_mem() |
| 367 | * @esc: set of characters that need escaping |
| 368 | * |
| 369 | * Puts data into buffer, replacing each occurrence of character from |
| 370 | * given class (defined by @flags and @esc) with printable escaped sequence. |
| 371 | * |
| 372 | * Use seq_has_overflowed() to check for errors. |
| 373 | */ |
| 374 | void seq_escape_mem(struct seq_file *m, const char *src, size_t len, |
| 375 | unsigned int flags, const char *esc) |
| 376 | { |
| 377 | char *buf; |
| 378 | size_t size = seq_get_buf(m, &buf); |
| 379 | int ret; |
| 380 | |
| 381 | ret = string_escape_mem(src, len, buf, size, flags, esc); |
| 382 | seq_commit(m, ret < size ? ret : -1); |
| 383 | } |
| 384 | EXPORT_SYMBOL(seq_escape_mem); |
| 385 | |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 386 | void seq_vprintf(struct seq_file *m, const char *f, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | int len; |
| 389 | |
| 390 | if (m->count < m->size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (m->count + len < m->size) { |
| 393 | m->count += len; |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 394 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
KAMEZAWA Hiroyuki | e075f59 | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 397 | seq_set_overflow(m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
Steven Whitehouse | a480814 | 2012-06-11 13:16:35 +0100 | [diff] [blame] | 399 | EXPORT_SYMBOL(seq_vprintf); |
| 400 | |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 401 | void seq_printf(struct seq_file *m, const char *f, ...) |
Steven Whitehouse | a480814 | 2012-06-11 13:16:35 +0100 | [diff] [blame] | 402 | { |
Steven Whitehouse | a480814 | 2012-06-11 13:16:35 +0100 | [diff] [blame] | 403 | va_list args; |
| 404 | |
| 405 | va_start(args, f); |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 406 | seq_vprintf(m, f, args); |
Steven Whitehouse | a480814 | 2012-06-11 13:16:35 +0100 | [diff] [blame] | 407 | va_end(args); |
Steven Whitehouse | a480814 | 2012-06-11 13:16:35 +0100 | [diff] [blame] | 408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | EXPORT_SYMBOL(seq_printf); |
| 410 | |
Florent Revest | 76d6a13 | 2021-04-27 19:43:12 +0200 | [diff] [blame] | 411 | #ifdef CONFIG_BINARY_PRINTF |
| 412 | void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary) |
| 413 | { |
| 414 | int len; |
| 415 | |
| 416 | if (m->count < m->size) { |
| 417 | len = bstr_printf(m->buf + m->count, m->size - m->count, f, |
| 418 | binary); |
| 419 | if (m->count + len < m->size) { |
| 420 | m->count += len; |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | seq_set_overflow(m); |
| 425 | } |
| 426 | EXPORT_SYMBOL(seq_bprintf); |
| 427 | #endif /* CONFIG_BINARY_PRINTF */ |
| 428 | |
Török Edwin | 74e2f33 | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 429 | /** |
Török Edwin | 958086d | 2008-11-23 23:24:53 +0200 | [diff] [blame] | 430 | * mangle_path - mangle and copy path to buffer beginning |
| 431 | * @s: buffer start |
| 432 | * @p: beginning of path in above buffer |
| 433 | * @esc: set of characters that need escaping |
Török Edwin | 74e2f33 | 2008-11-22 13:28:48 +0200 | [diff] [blame] | 434 | * |
| 435 | * Copy the path from @p to @s, replacing each occurrence of character from |
| 436 | * @esc with usual octal escape. |
| 437 | * Returns pointer past last written character in @s, or NULL in case of |
| 438 | * failure. |
| 439 | */ |
Al Viro | 8c9379e | 2011-12-08 20:18:57 -0500 | [diff] [blame] | 440 | char *mangle_path(char *s, const char *p, const char *esc) |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 441 | { |
| 442 | while (s <= p) { |
| 443 | char c = *p++; |
| 444 | if (!c) { |
| 445 | return s; |
| 446 | } else if (!strchr(esc, c)) { |
| 447 | *s++ = c; |
| 448 | } else if (s + 4 > p) { |
| 449 | break; |
| 450 | } else { |
| 451 | *s++ = '\\'; |
| 452 | *s++ = '0' + ((c & 0300) >> 6); |
| 453 | *s++ = '0' + ((c & 070) >> 3); |
| 454 | *s++ = '0' + (c & 07); |
| 455 | } |
| 456 | } |
| 457 | return NULL; |
| 458 | } |
Ingo Molnar | 604094f | 2008-11-28 18:03:22 +0100 | [diff] [blame] | 459 | EXPORT_SYMBOL(mangle_path); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 460 | |
Arjan van de Ven | 52afeef | 2008-12-01 14:35:00 -0800 | [diff] [blame] | 461 | /** |
| 462 | * seq_path - seq_file interface to print a pathname |
| 463 | * @m: the seq_file handle |
| 464 | * @path: the struct path to print |
| 465 | * @esc: set of characters to escape in the output |
| 466 | * |
| 467 | * return the absolute path of 'path', as represented by the |
| 468 | * dentry / mnt pair in the path parameter. |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 469 | */ |
Al Viro | 8c9379e | 2011-12-08 20:18:57 -0500 | [diff] [blame] | 470 | int seq_path(struct seq_file *m, const struct path *path, const char *esc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 472 | char *buf; |
| 473 | size_t size = seq_get_buf(m, &buf); |
| 474 | int res = -1; |
| 475 | |
| 476 | if (size) { |
| 477 | char *p = d_path(path, buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (!IS_ERR(p)) { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 479 | char *end = mangle_path(buf, p, esc); |
| 480 | if (end) |
| 481 | res = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 484 | seq_commit(m, res); |
| 485 | |
| 486 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | EXPORT_SYMBOL(seq_path); |
| 489 | |
Miklos Szeredi | 2726d56 | 2015-06-19 10:30:28 +0200 | [diff] [blame] | 490 | /** |
| 491 | * seq_file_path - seq_file interface to print a pathname of a file |
| 492 | * @m: the seq_file handle |
| 493 | * @file: the struct file to print |
| 494 | * @esc: set of characters to escape in the output |
| 495 | * |
| 496 | * return the absolute path to the file. |
| 497 | */ |
| 498 | int seq_file_path(struct seq_file *m, struct file *file, const char *esc) |
| 499 | { |
| 500 | return seq_path(m, &file->f_path, esc); |
| 501 | } |
| 502 | EXPORT_SYMBOL(seq_file_path); |
| 503 | |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 504 | /* |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 505 | * Same as seq_path, but relative to supplied root. |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 506 | */ |
Al Viro | 8c9379e | 2011-12-08 20:18:57 -0500 | [diff] [blame] | 507 | int seq_path_root(struct seq_file *m, const struct path *path, |
| 508 | const struct path *root, const char *esc) |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 509 | { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 510 | char *buf; |
| 511 | size_t size = seq_get_buf(m, &buf); |
| 512 | int res = -ENAMETOOLONG; |
| 513 | |
| 514 | if (size) { |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 515 | char *p; |
| 516 | |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 517 | p = __d_path(path, root, buf, size); |
Al Viro | 02125a8 | 2011-12-05 08:43:34 -0500 | [diff] [blame] | 518 | if (!p) |
| 519 | return SEQ_SKIP; |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 520 | res = PTR_ERR(p); |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 521 | if (!IS_ERR(p)) { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 522 | char *end = mangle_path(buf, p, esc); |
| 523 | if (end) |
| 524 | res = end - buf; |
| 525 | else |
| 526 | res = -ENAMETOOLONG; |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 527 | } |
| 528 | } |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 529 | seq_commit(m, res); |
| 530 | |
Al Viro | 02125a8 | 2011-12-05 08:43:34 -0500 | [diff] [blame] | 531 | return res < 0 && res != -ENAMETOOLONG ? res : 0; |
Miklos Szeredi | 9d1bc601 | 2008-03-27 13:06:21 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 535 | * returns the path of the 'dentry' from the root of its filesystem. |
| 536 | */ |
Al Viro | 8c9379e | 2011-12-08 20:18:57 -0500 | [diff] [blame] | 537 | int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 538 | { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 539 | char *buf; |
| 540 | size_t size = seq_get_buf(m, &buf); |
| 541 | int res = -1; |
| 542 | |
| 543 | if (size) { |
| 544 | char *p = dentry_path(dentry, buf, size); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 545 | if (!IS_ERR(p)) { |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 546 | char *end = mangle_path(buf, p, esc); |
| 547 | if (end) |
| 548 | res = end - buf; |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 549 | } |
| 550 | } |
Miklos Szeredi | f843980 | 2009-09-21 14:48:36 +0200 | [diff] [blame] | 551 | seq_commit(m, res); |
| 552 | |
| 553 | return res; |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 554 | } |
Omar Sandoval | c8d3fe0 | 2015-05-18 02:16:31 -0700 | [diff] [blame] | 555 | EXPORT_SYMBOL(seq_dentry); |
Ram Pai | 6092d04 | 2008-03-27 13:06:20 +0100 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | static void *single_start(struct seq_file *p, loff_t *pos) |
| 558 | { |
| 559 | return NULL + (*pos == 0); |
| 560 | } |
| 561 | |
| 562 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) |
| 563 | { |
| 564 | ++*pos; |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | static void single_stop(struct seq_file *p, void *v) |
| 569 | { |
| 570 | } |
| 571 | |
| 572 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), |
| 573 | void *data) |
| 574 | { |
Alexey Dobriyan | d64d01a | 2018-04-10 16:34:49 -0700 | [diff] [blame] | 575 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | int res = -ENOMEM; |
| 577 | |
| 578 | if (op) { |
| 579 | op->start = single_start; |
| 580 | op->next = single_next; |
| 581 | op->stop = single_stop; |
| 582 | op->show = show; |
| 583 | res = seq_open(file, op); |
| 584 | if (!res) |
| 585 | ((struct seq_file *)file->private_data)->private = data; |
| 586 | else |
| 587 | kfree(op); |
| 588 | } |
| 589 | return res; |
| 590 | } |
| 591 | EXPORT_SYMBOL(single_open); |
| 592 | |
Al Viro | 2043f49 | 2013-03-31 13:43:23 -0400 | [diff] [blame] | 593 | int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), |
| 594 | void *data, size_t size) |
| 595 | { |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 596 | char *buf = seq_buf_alloc(size); |
Al Viro | 2043f49 | 2013-03-31 13:43:23 -0400 | [diff] [blame] | 597 | int ret; |
| 598 | if (!buf) |
| 599 | return -ENOMEM; |
| 600 | ret = single_open(file, show, data); |
| 601 | if (ret) { |
Heiko Carstens | 058504e | 2014-07-02 15:22:37 -0700 | [diff] [blame] | 602 | kvfree(buf); |
Al Viro | 2043f49 | 2013-03-31 13:43:23 -0400 | [diff] [blame] | 603 | return ret; |
| 604 | } |
| 605 | ((struct seq_file *)file->private_data)->buf = buf; |
| 606 | ((struct seq_file *)file->private_data)->size = size; |
| 607 | return 0; |
| 608 | } |
| 609 | EXPORT_SYMBOL(single_open_size); |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | int single_release(struct inode *inode, struct file *file) |
| 612 | { |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 613 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | int res = seq_release(inode, file); |
| 615 | kfree(op); |
| 616 | return res; |
| 617 | } |
| 618 | EXPORT_SYMBOL(single_release); |
| 619 | |
| 620 | int seq_release_private(struct inode *inode, struct file *file) |
| 621 | { |
| 622 | struct seq_file *seq = file->private_data; |
| 623 | |
| 624 | kfree(seq->private); |
| 625 | seq->private = NULL; |
| 626 | return seq_release(inode, file); |
| 627 | } |
| 628 | EXPORT_SYMBOL(seq_release_private); |
| 629 | |
Pavel Emelyanov | 3969903 | 2007-10-10 02:28:42 -0700 | [diff] [blame] | 630 | void *__seq_open_private(struct file *f, const struct seq_operations *ops, |
| 631 | int psize) |
| 632 | { |
| 633 | int rc; |
| 634 | void *private; |
| 635 | struct seq_file *seq; |
| 636 | |
Alexey Dobriyan | d64d01a | 2018-04-10 16:34:49 -0700 | [diff] [blame] | 637 | private = kzalloc(psize, GFP_KERNEL_ACCOUNT); |
Pavel Emelyanov | 3969903 | 2007-10-10 02:28:42 -0700 | [diff] [blame] | 638 | if (private == NULL) |
| 639 | goto out; |
| 640 | |
| 641 | rc = seq_open(f, ops); |
| 642 | if (rc < 0) |
| 643 | goto out_free; |
| 644 | |
| 645 | seq = f->private_data; |
| 646 | seq->private = private; |
| 647 | return private; |
| 648 | |
| 649 | out_free: |
| 650 | kfree(private); |
| 651 | out: |
| 652 | return NULL; |
| 653 | } |
| 654 | EXPORT_SYMBOL(__seq_open_private); |
| 655 | |
| 656 | int seq_open_private(struct file *filp, const struct seq_operations *ops, |
| 657 | int psize) |
| 658 | { |
| 659 | return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; |
| 660 | } |
| 661 | EXPORT_SYMBOL(seq_open_private); |
| 662 | |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 663 | void seq_putc(struct seq_file *m, char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 665 | if (m->count >= m->size) |
| 666 | return; |
| 667 | |
| 668 | m->buf[m->count++] = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | EXPORT_SYMBOL(seq_putc); |
| 671 | |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 672 | void seq_puts(struct seq_file *m, const char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { |
| 674 | int len = strlen(s); |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 675 | |
| 676 | if (m->count + len >= m->size) { |
| 677 | seq_set_overflow(m); |
| 678 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 680 | memcpy(m->buf + m->count, s, len); |
| 681 | m->count += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | EXPORT_SYMBOL(seq_puts); |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 684 | |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 685 | /** |
Mauro Carvalho Chehab | 961f3c8 | 2021-01-14 09:04:39 +0100 | [diff] [blame] | 686 | * seq_put_decimal_ull_width - A helper routine for putting decimal numbers |
| 687 | * without rich format of printf(). |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 688 | * only 'unsigned long long' is supported. |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 689 | * @m: seq_file identifying the buffer to which data should be written |
| 690 | * @delimiter: a string which is printed before the number |
| 691 | * @num: the number |
| 692 | * @width: a minimum field width |
| 693 | * |
| 694 | * This routine will put strlen(delimiter) + number into seq_filed. |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 695 | * This routine is very quick when you show lots of numbers. |
| 696 | * In usual cases, it will be better to use seq_printf(). It's easier to read. |
| 697 | */ |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 698 | void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter, |
| 699 | unsigned long long num, unsigned int width) |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 700 | { |
| 701 | int len; |
| 702 | |
| 703 | if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */ |
| 704 | goto overflow; |
| 705 | |
Andrei Vagin | 48dffbf | 2018-04-10 16:31:23 -0700 | [diff] [blame] | 706 | if (delimiter && delimiter[0]) { |
| 707 | if (delimiter[1] == 0) |
| 708 | seq_putc(m, delimiter[0]); |
| 709 | else |
| 710 | seq_puts(m, delimiter); |
| 711 | } |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 712 | |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 713 | if (!width) |
| 714 | width = 1; |
| 715 | |
| 716 | if (m->count + width >= m->size) |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 717 | goto overflow; |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 718 | |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 719 | len = num_to_str(m->buf + m->count, m->size - m->count, num, width); |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 720 | if (!len) |
| 721 | goto overflow; |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 722 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 723 | m->count += len; |
Joe Perches | 6798a8c | 2015-09-11 13:07:48 -0700 | [diff] [blame] | 724 | return; |
| 725 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 726 | overflow: |
KAMEZAWA Hiroyuki | e075f59 | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 727 | seq_set_overflow(m); |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 728 | } |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 729 | |
| 730 | void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, |
| 731 | unsigned long long num) |
| 732 | { |
| 733 | return seq_put_decimal_ull_width(m, delimiter, num, 0); |
| 734 | } |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 735 | EXPORT_SYMBOL(seq_put_decimal_ull); |
| 736 | |
Andrei Vagin | 0e3dc01 | 2018-04-10 16:30:44 -0700 | [diff] [blame] | 737 | /** |
| 738 | * seq_put_hex_ll - put a number in hexadecimal notation |
| 739 | * @m: seq_file identifying the buffer to which data should be written |
| 740 | * @delimiter: a string which is printed before the number |
| 741 | * @v: the number |
| 742 | * @width: a minimum field width |
| 743 | * |
| 744 | * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v) |
| 745 | * |
| 746 | * This routine is very quick when you show lots of numbers. |
| 747 | * In usual cases, it will be better to use seq_printf(). It's easier to read. |
| 748 | */ |
| 749 | void seq_put_hex_ll(struct seq_file *m, const char *delimiter, |
| 750 | unsigned long long v, unsigned int width) |
| 751 | { |
| 752 | unsigned int len; |
| 753 | int i; |
| 754 | |
| 755 | if (delimiter && delimiter[0]) { |
| 756 | if (delimiter[1] == 0) |
| 757 | seq_putc(m, delimiter[0]); |
| 758 | else |
| 759 | seq_puts(m, delimiter); |
| 760 | } |
| 761 | |
| 762 | /* If x is 0, the result of __builtin_clzll is undefined */ |
| 763 | if (v == 0) |
| 764 | len = 1; |
| 765 | else |
| 766 | len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4; |
| 767 | |
| 768 | if (len < width) |
| 769 | len = width; |
| 770 | |
| 771 | if (m->count + len > m->size) { |
| 772 | seq_set_overflow(m); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | for (i = len - 1; i >= 0; i--) { |
| 777 | m->buf[m->count + i] = hex_asc[0xf & v]; |
| 778 | v = v >> 4; |
| 779 | } |
| 780 | m->count += len; |
| 781 | } |
| 782 | |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 783 | void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num) |
KAMEZAWA Hiroyuki | bda7bad | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 784 | { |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 785 | int len; |
| 786 | |
| 787 | if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */ |
| 788 | goto overflow; |
| 789 | |
Andrei Vagin | 48dffbf | 2018-04-10 16:31:23 -0700 | [diff] [blame] | 790 | if (delimiter && delimiter[0]) { |
| 791 | if (delimiter[1] == 0) |
| 792 | seq_putc(m, delimiter[0]); |
| 793 | else |
| 794 | seq_puts(m, delimiter); |
| 795 | } |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 796 | |
| 797 | if (m->count + 2 >= m->size) |
| 798 | goto overflow; |
| 799 | |
KAMEZAWA Hiroyuki | bda7bad | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 800 | if (num < 0) { |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 801 | m->buf[m->count++] = '-'; |
KAMEZAWA Hiroyuki | bda7bad | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 802 | num = -num; |
KAMEZAWA Hiroyuki | bda7bad | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 803 | } |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 804 | |
| 805 | if (num < 10) { |
| 806 | m->buf[m->count++] = num + '0'; |
| 807 | return; |
| 808 | } |
| 809 | |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 810 | len = num_to_str(m->buf + m->count, m->size - m->count, num, 0); |
Joe Perches | 75ba1d0 | 2016-10-07 17:02:20 -0700 | [diff] [blame] | 811 | if (!len) |
| 812 | goto overflow; |
| 813 | |
| 814 | m->count += len; |
| 815 | return; |
| 816 | |
| 817 | overflow: |
| 818 | seq_set_overflow(m); |
KAMEZAWA Hiroyuki | bda7bad | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 819 | } |
| 820 | EXPORT_SYMBOL(seq_put_decimal_ll); |
| 821 | |
Peter Oberparleiter | 0b92360 | 2009-06-17 16:28:05 -0700 | [diff] [blame] | 822 | /** |
| 823 | * seq_write - write arbitrary data to buffer |
| 824 | * @seq: seq_file identifying the buffer to which data should be written |
| 825 | * @data: data address |
| 826 | * @len: number of bytes |
| 827 | * |
| 828 | * Return 0 on success, non-zero otherwise. |
| 829 | */ |
| 830 | int seq_write(struct seq_file *seq, const void *data, size_t len) |
| 831 | { |
| 832 | if (seq->count + len < seq->size) { |
| 833 | memcpy(seq->buf + seq->count, data, len); |
| 834 | seq->count += len; |
| 835 | return 0; |
| 836 | } |
KAMEZAWA Hiroyuki | e075f59 | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 837 | seq_set_overflow(seq); |
Peter Oberparleiter | 0b92360 | 2009-06-17 16:28:05 -0700 | [diff] [blame] | 838 | return -1; |
| 839 | } |
| 840 | EXPORT_SYMBOL(seq_write); |
| 841 | |
Tetsuo Handa | 839cc2a | 2013-11-14 14:31:56 -0800 | [diff] [blame] | 842 | /** |
| 843 | * seq_pad - write padding spaces to buffer |
| 844 | * @m: seq_file identifying the buffer to which data should be written |
| 845 | * @c: the byte to append after padding if non-zero |
| 846 | */ |
| 847 | void seq_pad(struct seq_file *m, char c) |
| 848 | { |
| 849 | int size = m->pad_until - m->count; |
Andrei Vagin | 8cfa67b4 | 2018-04-10 16:30:47 -0700 | [diff] [blame] | 850 | if (size > 0) { |
| 851 | if (size + m->count > m->size) { |
| 852 | seq_set_overflow(m); |
| 853 | return; |
| 854 | } |
| 855 | memset(m->buf + m->count, ' ', size); |
| 856 | m->count += size; |
| 857 | } |
Tetsuo Handa | 839cc2a | 2013-11-14 14:31:56 -0800 | [diff] [blame] | 858 | if (c) |
| 859 | seq_putc(m, c); |
| 860 | } |
| 861 | EXPORT_SYMBOL(seq_pad); |
| 862 | |
Andy Shevchenko | 3760710 | 2015-09-09 15:38:33 -0700 | [diff] [blame] | 863 | /* A complete analogue of print_hex_dump() */ |
| 864 | void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type, |
| 865 | int rowsize, int groupsize, const void *buf, size_t len, |
| 866 | bool ascii) |
| 867 | { |
| 868 | const u8 *ptr = buf; |
| 869 | int i, linelen, remaining = len; |
Andy Shevchenko | 8b91a31 | 2015-11-06 16:32:37 -0800 | [diff] [blame] | 870 | char *buffer; |
| 871 | size_t size; |
Andy Shevchenko | 3760710 | 2015-09-09 15:38:33 -0700 | [diff] [blame] | 872 | int ret; |
| 873 | |
| 874 | if (rowsize != 16 && rowsize != 32) |
| 875 | rowsize = 16; |
| 876 | |
| 877 | for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) { |
| 878 | linelen = min(remaining, rowsize); |
| 879 | remaining -= rowsize; |
| 880 | |
| 881 | switch (prefix_type) { |
| 882 | case DUMP_PREFIX_ADDRESS: |
| 883 | seq_printf(m, "%s%p: ", prefix_str, ptr + i); |
| 884 | break; |
| 885 | case DUMP_PREFIX_OFFSET: |
| 886 | seq_printf(m, "%s%.8x: ", prefix_str, i); |
| 887 | break; |
| 888 | default: |
| 889 | seq_printf(m, "%s", prefix_str); |
| 890 | break; |
| 891 | } |
| 892 | |
Andy Shevchenko | 8b91a31 | 2015-11-06 16:32:37 -0800 | [diff] [blame] | 893 | size = seq_get_buf(m, &buffer); |
Andy Shevchenko | 3760710 | 2015-09-09 15:38:33 -0700 | [diff] [blame] | 894 | ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, |
Andy Shevchenko | 8b91a31 | 2015-11-06 16:32:37 -0800 | [diff] [blame] | 895 | buffer, size, ascii); |
| 896 | seq_commit(m, ret < size ? ret : -1); |
| 897 | |
| 898 | seq_putc(m, '\n'); |
Andy Shevchenko | 3760710 | 2015-09-09 15:38:33 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | EXPORT_SYMBOL(seq_hex_dump); |
| 902 | |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 903 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
| 904 | { |
| 905 | struct list_head *lh; |
| 906 | |
| 907 | list_for_each(lh, head) |
| 908 | if (pos-- == 0) |
| 909 | return lh; |
| 910 | |
| 911 | return NULL; |
| 912 | } |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 913 | EXPORT_SYMBOL(seq_list_start); |
| 914 | |
| 915 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) |
| 916 | { |
| 917 | if (!pos) |
| 918 | return head; |
| 919 | |
| 920 | return seq_list_start(head, pos - 1); |
| 921 | } |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 922 | EXPORT_SYMBOL(seq_list_start_head); |
| 923 | |
| 924 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) |
| 925 | { |
| 926 | struct list_head *lh; |
| 927 | |
| 928 | lh = ((struct list_head *)v)->next; |
| 929 | ++*ppos; |
| 930 | return lh == head ? NULL : lh; |
| 931 | } |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 932 | EXPORT_SYMBOL(seq_list_next); |
Li Zefan | 66655de | 2010-02-08 23:18:22 +0000 | [diff] [blame] | 933 | |
| 934 | /** |
| 935 | * seq_hlist_start - start an iteration of a hlist |
| 936 | * @head: the head of the hlist |
| 937 | * @pos: the start position of the sequence |
| 938 | * |
| 939 | * Called at seq_file->op->start(). |
| 940 | */ |
| 941 | struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos) |
| 942 | { |
| 943 | struct hlist_node *node; |
| 944 | |
| 945 | hlist_for_each(node, head) |
| 946 | if (pos-- == 0) |
| 947 | return node; |
| 948 | return NULL; |
| 949 | } |
| 950 | EXPORT_SYMBOL(seq_hlist_start); |
| 951 | |
| 952 | /** |
| 953 | * seq_hlist_start_head - start an iteration of a hlist |
| 954 | * @head: the head of the hlist |
| 955 | * @pos: the start position of the sequence |
| 956 | * |
| 957 | * Called at seq_file->op->start(). Call this function if you want to |
| 958 | * print a header at the top of the output. |
| 959 | */ |
| 960 | struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos) |
| 961 | { |
| 962 | if (!pos) |
| 963 | return SEQ_START_TOKEN; |
| 964 | |
| 965 | return seq_hlist_start(head, pos - 1); |
| 966 | } |
| 967 | EXPORT_SYMBOL(seq_hlist_start_head); |
| 968 | |
| 969 | /** |
| 970 | * seq_hlist_next - move to the next position of the hlist |
| 971 | * @v: the current iterator |
| 972 | * @head: the head of the hlist |
Randy Dunlap | 138860b | 2010-03-04 09:37:12 -0800 | [diff] [blame] | 973 | * @ppos: the current position |
Li Zefan | 66655de | 2010-02-08 23:18:22 +0000 | [diff] [blame] | 974 | * |
| 975 | * Called at seq_file->op->next(). |
| 976 | */ |
| 977 | struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head, |
| 978 | loff_t *ppos) |
| 979 | { |
| 980 | struct hlist_node *node = v; |
| 981 | |
| 982 | ++*ppos; |
| 983 | if (v == SEQ_START_TOKEN) |
| 984 | return head->first; |
| 985 | else |
| 986 | return node->next; |
| 987 | } |
| 988 | EXPORT_SYMBOL(seq_hlist_next); |
stephen hemminger | 1cc5232 | 2010-02-22 07:57:17 +0000 | [diff] [blame] | 989 | |
| 990 | /** |
| 991 | * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU |
| 992 | * @head: the head of the hlist |
| 993 | * @pos: the start position of the sequence |
| 994 | * |
| 995 | * Called at seq_file->op->start(). |
| 996 | * |
| 997 | * This list-traversal primitive may safely run concurrently with |
| 998 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
| 999 | * as long as the traversal is guarded by rcu_read_lock(). |
| 1000 | */ |
| 1001 | struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head, |
| 1002 | loff_t pos) |
| 1003 | { |
| 1004 | struct hlist_node *node; |
| 1005 | |
| 1006 | __hlist_for_each_rcu(node, head) |
| 1007 | if (pos-- == 0) |
| 1008 | return node; |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | EXPORT_SYMBOL(seq_hlist_start_rcu); |
| 1012 | |
| 1013 | /** |
| 1014 | * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU |
| 1015 | * @head: the head of the hlist |
| 1016 | * @pos: the start position of the sequence |
| 1017 | * |
| 1018 | * Called at seq_file->op->start(). Call this function if you want to |
| 1019 | * print a header at the top of the output. |
| 1020 | * |
| 1021 | * This list-traversal primitive may safely run concurrently with |
| 1022 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
| 1023 | * as long as the traversal is guarded by rcu_read_lock(). |
| 1024 | */ |
| 1025 | struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head, |
| 1026 | loff_t pos) |
| 1027 | { |
| 1028 | if (!pos) |
| 1029 | return SEQ_START_TOKEN; |
| 1030 | |
| 1031 | return seq_hlist_start_rcu(head, pos - 1); |
| 1032 | } |
| 1033 | EXPORT_SYMBOL(seq_hlist_start_head_rcu); |
| 1034 | |
| 1035 | /** |
| 1036 | * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU |
| 1037 | * @v: the current iterator |
| 1038 | * @head: the head of the hlist |
Randy Dunlap | 138860b | 2010-03-04 09:37:12 -0800 | [diff] [blame] | 1039 | * @ppos: the current position |
stephen hemminger | 1cc5232 | 2010-02-22 07:57:17 +0000 | [diff] [blame] | 1040 | * |
| 1041 | * Called at seq_file->op->next(). |
| 1042 | * |
| 1043 | * This list-traversal primitive may safely run concurrently with |
| 1044 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
| 1045 | * as long as the traversal is guarded by rcu_read_lock(). |
| 1046 | */ |
| 1047 | struct hlist_node *seq_hlist_next_rcu(void *v, |
| 1048 | struct hlist_head *head, |
| 1049 | loff_t *ppos) |
| 1050 | { |
| 1051 | struct hlist_node *node = v; |
| 1052 | |
| 1053 | ++*ppos; |
| 1054 | if (v == SEQ_START_TOKEN) |
| 1055 | return rcu_dereference(head->first); |
| 1056 | else |
| 1057 | return rcu_dereference(node->next); |
| 1058 | } |
| 1059 | EXPORT_SYMBOL(seq_hlist_next_rcu); |
Jeff Layton | 0bc7738 | 2013-06-21 08:58:21 -0400 | [diff] [blame] | 1060 | |
| 1061 | /** |
Mauro Carvalho Chehab | 961f3c8 | 2021-01-14 09:04:39 +0100 | [diff] [blame] | 1062 | * seq_hlist_start_percpu - start an iteration of a percpu hlist array |
Jeff Layton | 0bc7738 | 2013-06-21 08:58:21 -0400 | [diff] [blame] | 1063 | * @head: pointer to percpu array of struct hlist_heads |
| 1064 | * @cpu: pointer to cpu "cursor" |
| 1065 | * @pos: start position of sequence |
| 1066 | * |
| 1067 | * Called at seq_file->op->start(). |
| 1068 | */ |
| 1069 | struct hlist_node * |
| 1070 | seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos) |
| 1071 | { |
| 1072 | struct hlist_node *node; |
| 1073 | |
| 1074 | for_each_possible_cpu(*cpu) { |
| 1075 | hlist_for_each(node, per_cpu_ptr(head, *cpu)) { |
| 1076 | if (pos-- == 0) |
| 1077 | return node; |
| 1078 | } |
| 1079 | } |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL(seq_hlist_start_percpu); |
| 1083 | |
| 1084 | /** |
| 1085 | * seq_hlist_next_percpu - move to the next position of the percpu hlist array |
| 1086 | * @v: pointer to current hlist_node |
| 1087 | * @head: pointer to percpu array of struct hlist_heads |
| 1088 | * @cpu: pointer to cpu "cursor" |
| 1089 | * @pos: start position of sequence |
| 1090 | * |
| 1091 | * Called at seq_file->op->next(). |
| 1092 | */ |
| 1093 | struct hlist_node * |
| 1094 | seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, |
| 1095 | int *cpu, loff_t *pos) |
| 1096 | { |
| 1097 | struct hlist_node *node = v; |
| 1098 | |
| 1099 | ++*pos; |
| 1100 | |
| 1101 | if (node->next) |
| 1102 | return node->next; |
| 1103 | |
| 1104 | for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids; |
| 1105 | *cpu = cpumask_next(*cpu, cpu_possible_mask)) { |
| 1106 | struct hlist_head *bucket = per_cpu_ptr(head, *cpu); |
| 1107 | |
| 1108 | if (!hlist_empty(bucket)) |
| 1109 | return bucket->first; |
| 1110 | } |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | EXPORT_SYMBOL(seq_hlist_next_percpu); |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 1114 | |
| 1115 | void __init seq_file_init(void) |
| 1116 | { |
Alexey Dobriyan | d64d01a | 2018-04-10 16:34:49 -0700 | [diff] [blame] | 1117 | seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC); |
Alexey Dobriyan | 096523203 | 2018-04-10 16:34:45 -0700 | [diff] [blame] | 1118 | } |