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