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