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