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