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. |
| 28 | */ |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 29 | int seq_open(struct file *file, const struct seq_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Al Viro | 1abe77b | 2005-11-07 17:15:34 -0500 | [diff] [blame] | 31 | struct seq_file *p = file->private_data; |
| 32 | |
| 33 | if (!p) { |
| 34 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 35 | if (!p) |
| 36 | return -ENOMEM; |
| 37 | file->private_data = p; |
| 38 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | memset(p, 0, sizeof(*p)); |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 40 | mutex_init(&p->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | p->op = op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Wrappers around seq_open(e.g. swaps_open) need to be |
| 45 | * aware of this. If they set f_version themselves, they |
| 46 | * should call seq_open first and then set f_version. |
| 47 | */ |
| 48 | file->f_version = 0; |
| 49 | |
| 50 | /* SEQ files support lseek, but not pread/pwrite */ |
| 51 | file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); |
| 52 | return 0; |
| 53 | } |
| 54 | EXPORT_SYMBOL(seq_open); |
| 55 | |
| 56 | /** |
| 57 | * seq_read - ->read() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 58 | * @file: the file to read from |
| 59 | * @buf: the buffer to read to |
| 60 | * @size: the maximum number of bytes to read |
| 61 | * @ppos: the current position in the file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * |
| 63 | * Ready-made ->f_op->read() |
| 64 | */ |
| 65 | ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) |
| 66 | { |
| 67 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 68 | size_t copied = 0; |
| 69 | loff_t pos; |
| 70 | size_t n; |
| 71 | void *p; |
| 72 | int err = 0; |
| 73 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 74 | mutex_lock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
| 76 | * seq_file->op->..m_start/m_stop/m_next may do special actions |
| 77 | * or optimisations based on the file->f_version, so we want to |
| 78 | * pass the file->f_version to those methods. |
| 79 | * |
| 80 | * seq_file->version is just copy of f_version, and seq_file |
| 81 | * methods can treat it simply as file version. |
| 82 | * It is copied in first and copied out after all operations. |
| 83 | * It is convenient to have it as part of structure to avoid the |
| 84 | * need of passing another argument to all the seq_file methods. |
| 85 | */ |
| 86 | m->version = file->f_version; |
| 87 | /* grab buffer if we didn't have one */ |
| 88 | if (!m->buf) { |
| 89 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
| 90 | if (!m->buf) |
| 91 | goto Enomem; |
| 92 | } |
| 93 | /* if not empty - flush it first */ |
| 94 | if (m->count) { |
| 95 | n = min(m->count, size); |
| 96 | err = copy_to_user(buf, m->buf + m->from, n); |
| 97 | if (err) |
| 98 | goto Efault; |
| 99 | m->count -= n; |
| 100 | m->from += n; |
| 101 | size -= n; |
| 102 | buf += n; |
| 103 | copied += n; |
| 104 | if (!m->count) |
| 105 | m->index++; |
| 106 | if (!size) |
| 107 | goto Done; |
| 108 | } |
| 109 | /* we need at least one record in buffer */ |
| 110 | while (1) { |
| 111 | pos = m->index; |
| 112 | p = m->op->start(m, &pos); |
| 113 | err = PTR_ERR(p); |
| 114 | if (!p || IS_ERR(p)) |
| 115 | break; |
| 116 | err = m->op->show(m, p); |
| 117 | if (err) |
| 118 | break; |
| 119 | if (m->count < m->size) |
| 120 | goto Fill; |
| 121 | m->op->stop(m, p); |
| 122 | kfree(m->buf); |
| 123 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 124 | if (!m->buf) |
| 125 | goto Enomem; |
| 126 | m->count = 0; |
| 127 | m->version = 0; |
| 128 | } |
| 129 | m->op->stop(m, p); |
| 130 | m->count = 0; |
| 131 | goto Done; |
| 132 | Fill: |
| 133 | /* they want more? let's try to get some more */ |
| 134 | while (m->count < size) { |
| 135 | size_t offs = m->count; |
| 136 | loff_t next = pos; |
| 137 | p = m->op->next(m, p, &next); |
| 138 | if (!p || IS_ERR(p)) { |
| 139 | err = PTR_ERR(p); |
| 140 | break; |
| 141 | } |
| 142 | err = m->op->show(m, p); |
| 143 | if (err || m->count == m->size) { |
| 144 | m->count = offs; |
| 145 | break; |
| 146 | } |
| 147 | pos = next; |
| 148 | } |
| 149 | m->op->stop(m, p); |
| 150 | n = min(m->count, size); |
| 151 | err = copy_to_user(buf, m->buf, n); |
| 152 | if (err) |
| 153 | goto Efault; |
| 154 | copied += n; |
| 155 | m->count -= n; |
| 156 | if (m->count) |
| 157 | m->from = n; |
| 158 | else |
| 159 | pos++; |
| 160 | m->index = pos; |
| 161 | Done: |
| 162 | if (!copied) |
| 163 | copied = err; |
| 164 | else |
| 165 | *ppos += copied; |
| 166 | file->f_version = m->version; |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 167 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return copied; |
| 169 | Enomem: |
| 170 | err = -ENOMEM; |
| 171 | goto Done; |
| 172 | Efault: |
| 173 | err = -EFAULT; |
| 174 | goto Done; |
| 175 | } |
| 176 | EXPORT_SYMBOL(seq_read); |
| 177 | |
| 178 | static int traverse(struct seq_file *m, loff_t offset) |
| 179 | { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 180 | loff_t pos = 0, index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int error = 0; |
| 182 | void *p; |
| 183 | |
| 184 | m->version = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 185 | index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | m->count = m->from = 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 187 | if (!offset) { |
| 188 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return 0; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (!m->buf) { |
| 192 | m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL); |
| 193 | if (!m->buf) |
| 194 | return -ENOMEM; |
| 195 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 196 | p = m->op->start(m, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | while (p) { |
| 198 | error = PTR_ERR(p); |
| 199 | if (IS_ERR(p)) |
| 200 | break; |
| 201 | error = m->op->show(m, p); |
| 202 | if (error) |
| 203 | break; |
| 204 | if (m->count == m->size) |
| 205 | goto Eoverflow; |
| 206 | if (pos + m->count > offset) { |
| 207 | m->from = offset - pos; |
| 208 | m->count -= m->from; |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 209 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | pos += m->count; |
| 213 | m->count = 0; |
| 214 | if (pos == offset) { |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 215 | index++; |
| 216 | m->index = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | break; |
| 218 | } |
Alexey Dobriyan | cb510b8 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 219 | p = m->op->next(m, p, &index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | m->op->stop(m, p); |
| 222 | return error; |
| 223 | |
| 224 | Eoverflow: |
| 225 | m->op->stop(m, p); |
| 226 | kfree(m->buf); |
| 227 | m->buf = kmalloc(m->size <<= 1, GFP_KERNEL); |
| 228 | return !m->buf ? -ENOMEM : -EAGAIN; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * seq_lseek - ->llseek() method for sequential files. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 233 | * @file: the file in question |
| 234 | * @offset: new position |
| 235 | * @origin: 0 for absolute, 1 for relative position |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * |
| 237 | * Ready-made ->f_op->llseek() |
| 238 | */ |
| 239 | loff_t seq_lseek(struct file *file, loff_t offset, int origin) |
| 240 | { |
| 241 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 242 | long long retval = -EINVAL; |
| 243 | |
Ingo Molnar | 0ac1759 | 2006-03-23 03:00:37 -0800 | [diff] [blame] | 244 | mutex_lock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | m->version = file->f_version; |
| 246 | switch (origin) { |
| 247 | case 1: |
| 248 | offset += file->f_pos; |
| 249 | case 0: |
| 250 | if (offset < 0) |
| 251 | break; |
| 252 | retval = offset; |
| 253 | if (offset != file->f_pos) { |
| 254 | while ((retval=traverse(m, offset)) == -EAGAIN) |
| 255 | ; |
| 256 | if (retval) { |
| 257 | /* with extreme prejudice... */ |
| 258 | file->f_pos = 0; |
| 259 | m->version = 0; |
| 260 | m->index = 0; |
| 261 | m->count = 0; |
| 262 | } else { |
| 263 | retval = file->f_pos = offset; |
| 264 | } |
| 265 | } |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | file->f_version = m->version; |
Alexey Dobriyan | 00c5746 | 2007-07-15 23:40:22 -0700 | [diff] [blame] | 268 | mutex_unlock(&m->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return retval; |
| 270 | } |
| 271 | EXPORT_SYMBOL(seq_lseek); |
| 272 | |
| 273 | /** |
| 274 | * seq_release - free the structures associated with sequential file. |
| 275 | * @file: file in question |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 276 | * @inode: file->f_path.dentry->d_inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | * |
| 278 | * Frees the structures associated with sequential file; can be used |
| 279 | * as ->f_op->release() if you don't have private data to destroy. |
| 280 | */ |
| 281 | int seq_release(struct inode *inode, struct file *file) |
| 282 | { |
| 283 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 284 | kfree(m->buf); |
| 285 | kfree(m); |
| 286 | return 0; |
| 287 | } |
| 288 | EXPORT_SYMBOL(seq_release); |
| 289 | |
| 290 | /** |
| 291 | * seq_escape - print string into buffer, escaping some characters |
| 292 | * @m: target buffer |
| 293 | * @s: string |
| 294 | * @esc: set of characters that need escaping |
| 295 | * |
| 296 | * Puts string into buffer, replacing each occurrence of character from |
| 297 | * @esc with usual octal escape. Returns 0 in case of success, -1 - in |
| 298 | * case of overflow. |
| 299 | */ |
| 300 | int seq_escape(struct seq_file *m, const char *s, const char *esc) |
| 301 | { |
| 302 | char *end = m->buf + m->size; |
| 303 | char *p; |
| 304 | char c; |
| 305 | |
| 306 | for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) { |
| 307 | if (!strchr(esc, c)) { |
| 308 | *p++ = c; |
| 309 | continue; |
| 310 | } |
| 311 | if (p + 3 < end) { |
| 312 | *p++ = '\\'; |
| 313 | *p++ = '0' + ((c & 0300) >> 6); |
| 314 | *p++ = '0' + ((c & 070) >> 3); |
| 315 | *p++ = '0' + (c & 07); |
| 316 | continue; |
| 317 | } |
| 318 | m->count = m->size; |
| 319 | return -1; |
| 320 | } |
| 321 | m->count = p - m->buf; |
| 322 | return 0; |
| 323 | } |
| 324 | EXPORT_SYMBOL(seq_escape); |
| 325 | |
| 326 | int seq_printf(struct seq_file *m, const char *f, ...) |
| 327 | { |
| 328 | va_list args; |
| 329 | int len; |
| 330 | |
| 331 | if (m->count < m->size) { |
| 332 | va_start(args, f); |
| 333 | len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); |
| 334 | va_end(args); |
| 335 | if (m->count + len < m->size) { |
| 336 | m->count += len; |
| 337 | return 0; |
| 338 | } |
| 339 | } |
| 340 | m->count = m->size; |
| 341 | return -1; |
| 342 | } |
| 343 | EXPORT_SYMBOL(seq_printf); |
| 344 | |
| 345 | int seq_path(struct seq_file *m, |
| 346 | struct vfsmount *mnt, struct dentry *dentry, |
| 347 | char *esc) |
| 348 | { |
| 349 | if (m->count < m->size) { |
| 350 | char *s = m->buf + m->count; |
| 351 | char *p = d_path(dentry, mnt, s, m->size - m->count); |
| 352 | if (!IS_ERR(p)) { |
| 353 | while (s <= p) { |
| 354 | char c = *p++; |
| 355 | if (!c) { |
| 356 | p = m->buf + m->count; |
| 357 | m->count = s - m->buf; |
| 358 | return s - p; |
| 359 | } else if (!strchr(esc, c)) { |
| 360 | *s++ = c; |
| 361 | } else if (s + 4 > p) { |
| 362 | break; |
| 363 | } else { |
| 364 | *s++ = '\\'; |
| 365 | *s++ = '0' + ((c & 0300) >> 6); |
| 366 | *s++ = '0' + ((c & 070) >> 3); |
| 367 | *s++ = '0' + (c & 07); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | m->count = m->size; |
| 373 | return -1; |
| 374 | } |
| 375 | EXPORT_SYMBOL(seq_path); |
| 376 | |
| 377 | static void *single_start(struct seq_file *p, loff_t *pos) |
| 378 | { |
| 379 | return NULL + (*pos == 0); |
| 380 | } |
| 381 | |
| 382 | static void *single_next(struct seq_file *p, void *v, loff_t *pos) |
| 383 | { |
| 384 | ++*pos; |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | static void single_stop(struct seq_file *p, void *v) |
| 389 | { |
| 390 | } |
| 391 | |
| 392 | int single_open(struct file *file, int (*show)(struct seq_file *, void *), |
| 393 | void *data) |
| 394 | { |
| 395 | struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); |
| 396 | int res = -ENOMEM; |
| 397 | |
| 398 | if (op) { |
| 399 | op->start = single_start; |
| 400 | op->next = single_next; |
| 401 | op->stop = single_stop; |
| 402 | op->show = show; |
| 403 | res = seq_open(file, op); |
| 404 | if (!res) |
| 405 | ((struct seq_file *)file->private_data)->private = data; |
| 406 | else |
| 407 | kfree(op); |
| 408 | } |
| 409 | return res; |
| 410 | } |
| 411 | EXPORT_SYMBOL(single_open); |
| 412 | |
| 413 | int single_release(struct inode *inode, struct file *file) |
| 414 | { |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 415 | const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | int res = seq_release(inode, file); |
| 417 | kfree(op); |
| 418 | return res; |
| 419 | } |
| 420 | EXPORT_SYMBOL(single_release); |
| 421 | |
| 422 | int seq_release_private(struct inode *inode, struct file *file) |
| 423 | { |
| 424 | struct seq_file *seq = file->private_data; |
| 425 | |
| 426 | kfree(seq->private); |
| 427 | seq->private = NULL; |
| 428 | return seq_release(inode, file); |
| 429 | } |
| 430 | EXPORT_SYMBOL(seq_release_private); |
| 431 | |
| 432 | int seq_putc(struct seq_file *m, char c) |
| 433 | { |
| 434 | if (m->count < m->size) { |
| 435 | m->buf[m->count++] = c; |
| 436 | return 0; |
| 437 | } |
| 438 | return -1; |
| 439 | } |
| 440 | EXPORT_SYMBOL(seq_putc); |
| 441 | |
| 442 | int seq_puts(struct seq_file *m, const char *s) |
| 443 | { |
| 444 | int len = strlen(s); |
| 445 | if (m->count + len < m->size) { |
| 446 | memcpy(m->buf + m->count, s, len); |
| 447 | m->count += len; |
| 448 | return 0; |
| 449 | } |
| 450 | m->count = m->size; |
| 451 | return -1; |
| 452 | } |
| 453 | EXPORT_SYMBOL(seq_puts); |
Pavel Emelianov | bcf67e1 | 2007-07-10 17:22:26 -0700 | [diff] [blame] | 454 | |
| 455 | struct list_head *seq_list_start(struct list_head *head, loff_t pos) |
| 456 | { |
| 457 | struct list_head *lh; |
| 458 | |
| 459 | list_for_each(lh, head) |
| 460 | if (pos-- == 0) |
| 461 | return lh; |
| 462 | |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | EXPORT_SYMBOL(seq_list_start); |
| 467 | |
| 468 | struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) |
| 469 | { |
| 470 | if (!pos) |
| 471 | return head; |
| 472 | |
| 473 | return seq_list_start(head, pos - 1); |
| 474 | } |
| 475 | |
| 476 | EXPORT_SYMBOL(seq_list_start_head); |
| 477 | |
| 478 | struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) |
| 479 | { |
| 480 | struct list_head *lh; |
| 481 | |
| 482 | lh = ((struct list_head *)v)->next; |
| 483 | ++*ppos; |
| 484 | return lh == head ? NULL : lh; |
| 485 | } |
| 486 | |
| 487 | EXPORT_SYMBOL(seq_list_next); |