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