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