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