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