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