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