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