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