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