1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/seq_file.c 4 * 5 * helper functions for making synthetic files from sequences of records. 6 * initial implementation -- AV, Oct 2001. 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/cache.h> 12 #include <linux/fs.h> 13 #include <linux/export.h> 14 #include <linux/hex.h> 15 #include <linux/seq_file.h> 16 #include <linux/vmalloc.h> 17 #include <linux/slab.h> 18 #include <linux/cred.h> 19 #include <linux/mm.h> 20 #include <linux/printk.h> 21 #include <linux/string_helpers.h> 22 #include <linux/uio.h> 23 24 #include <linux/uaccess.h> 25 #include <asm/page.h> 26 27 static struct kmem_cache *seq_file_cache __ro_after_init; 28 29 static void seq_set_overflow(struct seq_file *m) 30 { 31 m->count = m->size; 32 } 33 34 static void *seq_buf_alloc(unsigned long size) 35 { 36 if (unlikely(size > MAX_RW_COUNT)) 37 return NULL; 38 39 return kvmalloc(size, GFP_KERNEL_ACCOUNT); 40 } 41 42 /** 43 * seq_open - initialize sequential file 44 * @file: file we initialize 45 * @op: method table describing the sequence 46 * 47 * seq_open() sets @file, associating it with a sequence described 48 * by @op. @op->start() sets the iterator up and returns the first 49 * element of sequence. @op->stop() shuts it down. @op->next() 50 * returns the next element of sequence. @op->show() prints element 51 * into the buffer. In case of error ->start() and ->next() return 52 * ERR_PTR(error). In the end of sequence they return %NULL. ->show() 53 * returns 0 in case of success and negative number in case of error. 54 * Returning SEQ_SKIP means "discard this element and move on". 55 * Note: seq_open() will allocate a struct seq_file and store its 56 * pointer in @file->private_data. This pointer should not be modified. 57 */ 58 int seq_open(struct file *file, const struct seq_operations *op) 59 { 60 struct seq_file *p; 61 62 WARN_ON(file->private_data); 63 64 p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL); 65 if (!p) 66 return -ENOMEM; 67 68 file->private_data = p; 69 70 mutex_init(&p->lock); 71 p->op = op; 72 73 // No refcounting: the lifetime of 'p' is constrained 74 // to the lifetime of the file. 75 p->file = file; 76 77 /* 78 * seq_files support lseek() and pread(). They do not implement 79 * write() at all, but we clear FMODE_PWRITE here for historical 80 * reasons. 81 * 82 * If a client of seq_files a) implements file.write() and b) wishes to 83 * support pwrite() then that client will need to implement its own 84 * file.open() which calls seq_open() and then sets FMODE_PWRITE. 85 */ 86 file->f_mode &= ~FMODE_PWRITE; 87 return 0; 88 } 89 EXPORT_SYMBOL(seq_open); 90 91 static int traverse(struct seq_file *m, loff_t offset) 92 { 93 loff_t pos = 0; 94 int error = 0; 95 void *p; 96 97 m->index = 0; 98 m->count = m->from = 0; 99 if (!offset) 100 return 0; 101 102 if (!m->buf) { 103 m->buf = seq_buf_alloc(m->size = PAGE_SIZE); 104 if (!m->buf) 105 return -ENOMEM; 106 } 107 p = m->op->start(m, &m->index); 108 while (p) { 109 error = PTR_ERR(p); 110 if (IS_ERR(p)) 111 break; 112 error = m->op->show(m, p); 113 if (error < 0) 114 break; 115 if (unlikely(error)) { 116 error = 0; 117 m->count = 0; 118 } 119 if (seq_has_overflowed(m)) 120 goto Eoverflow; 121 p = m->op->next(m, p, &m->index); 122 if (pos + m->count > offset) { 123 m->from = offset - pos; 124 m->count -= m->from; 125 break; 126 } 127 pos += m->count; 128 m->count = 0; 129 if (pos == offset) 130 break; 131 } 132 m->op->stop(m, p); 133 return error; 134 135 Eoverflow: 136 m->op->stop(m, p); 137 kvfree(m->buf); 138 m->count = 0; 139 m->buf = seq_buf_alloc(m->size <<= 1); 140 return !m->buf ? -ENOMEM : -EAGAIN; 141 } 142 143 /** 144 * seq_read - ->read() method for sequential files. 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 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 { 154 struct iovec iov = { .iov_base = buf, .iov_len = size}; 155 struct kiocb kiocb; 156 struct iov_iter iter; 157 ssize_t ret; 158 159 init_sync_kiocb(&kiocb, file); 160 iov_iter_init(&iter, ITER_DEST, &iov, 1, size); 161 162 kiocb.ki_pos = *ppos; 163 ret = seq_read_iter(&kiocb, &iter); 164 *ppos = kiocb.ki_pos; 165 return ret; 166 } 167 EXPORT_SYMBOL(seq_read); 168 169 /* 170 * Ready-made ->f_op->read_iter() 171 */ 172 ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter) 173 { 174 struct seq_file *m = iocb->ki_filp->private_data; 175 size_t copied = 0; 176 size_t n; 177 void *p; 178 int err = 0; 179 180 if (!iov_iter_count(iter)) 181 return 0; 182 183 mutex_lock(&m->lock); 184 185 /* 186 * if request is to read from zero offset, reset iterator to first 187 * record as it might have been already advanced by previous requests 188 */ 189 if (iocb->ki_pos == 0) { 190 m->index = 0; 191 m->count = 0; 192 } 193 194 /* Don't assume ki_pos is where we left it */ 195 if (unlikely(iocb->ki_pos != m->read_pos)) { 196 while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN) 197 ; 198 if (err) { 199 /* With prejudice... */ 200 m->read_pos = 0; 201 m->index = 0; 202 m->count = 0; 203 goto Done; 204 } else { 205 m->read_pos = iocb->ki_pos; 206 } 207 } 208 209 /* grab buffer if we didn't have one */ 210 if (!m->buf) { 211 m->buf = seq_buf_alloc(m->size = PAGE_SIZE); 212 if (!m->buf) 213 goto Enomem; 214 } 215 // something left in the buffer - copy it out first 216 if (m->count) { 217 n = copy_to_iter(m->buf + m->from, m->count, iter); 218 m->count -= n; 219 m->from += n; 220 copied += n; 221 if (m->count) // hadn't managed to copy everything 222 goto Done; 223 } 224 // get a non-empty record in the buffer 225 m->from = 0; 226 p = m->op->start(m, &m->index); 227 while (1) { 228 err = PTR_ERR(p); 229 if (!p || IS_ERR(p)) // EOF or an error 230 break; 231 err = m->op->show(m, p); 232 if (err < 0) // hard error 233 break; 234 if (unlikely(err)) // ->show() says "skip it" 235 m->count = 0; 236 if (unlikely(!m->count)) { // empty record 237 p = m->op->next(m, p, &m->index); 238 continue; 239 } 240 if (!seq_has_overflowed(m)) // got it 241 goto Fill; 242 // need a bigger buffer 243 m->op->stop(m, p); 244 kvfree(m->buf); 245 m->count = 0; 246 m->buf = seq_buf_alloc(m->size <<= 1); 247 if (!m->buf) 248 goto Enomem; 249 p = m->op->start(m, &m->index); 250 } 251 // EOF or an error 252 m->op->stop(m, p); 253 m->count = 0; 254 goto Done; 255 Fill: 256 // one non-empty record is in the buffer; if they want more, 257 // try to fit more in, but in any case we need to advance 258 // the iterator once for every record shown. 259 while (1) { 260 size_t offs = m->count; 261 loff_t pos = m->index; 262 263 p = m->op->next(m, p, &m->index); 264 if (pos == m->index) { 265 pr_info_ratelimited("buggy .next function %ps did not update position index\n", 266 m->op->next); 267 m->index++; 268 } 269 if (!p || IS_ERR(p)) // no next record for us 270 break; 271 if (m->count >= iov_iter_count(iter)) 272 break; 273 err = m->op->show(m, p); 274 if (err > 0) { // ->show() says "skip it" 275 m->count = offs; 276 } else if (err || seq_has_overflowed(m)) { 277 m->count = offs; 278 break; 279 } 280 } 281 m->op->stop(m, p); 282 n = copy_to_iter(m->buf, m->count, iter); 283 copied += n; 284 m->count -= n; 285 m->from = n; 286 Done: 287 if (unlikely(!copied)) { 288 copied = m->count ? -EFAULT : err; 289 } else { 290 iocb->ki_pos += copied; 291 m->read_pos += copied; 292 } 293 mutex_unlock(&m->lock); 294 return copied; 295 Enomem: 296 err = -ENOMEM; 297 goto Done; 298 } 299 EXPORT_SYMBOL(seq_read_iter); 300 301 /** 302 * seq_lseek - ->llseek() method for sequential files. 303 * @file: the file in question 304 * @offset: new position 305 * @whence: 0 for absolute, 1 for relative position 306 * 307 * Ready-made ->f_op->llseek() 308 */ 309 loff_t seq_lseek(struct file *file, loff_t offset, int whence) 310 { 311 struct seq_file *m = file->private_data; 312 loff_t retval = -EINVAL; 313 314 mutex_lock(&m->lock); 315 switch (whence) { 316 case SEEK_CUR: 317 offset += file->f_pos; 318 fallthrough; 319 case SEEK_SET: 320 if (offset < 0) 321 break; 322 retval = offset; 323 if (offset != m->read_pos) { 324 while ((retval = traverse(m, offset)) == -EAGAIN) 325 ; 326 if (retval) { 327 /* with extreme prejudice... */ 328 file->f_pos = 0; 329 m->read_pos = 0; 330 m->index = 0; 331 m->count = 0; 332 } else { 333 m->read_pos = offset; 334 retval = file->f_pos = offset; 335 } 336 } else { 337 file->f_pos = offset; 338 } 339 } 340 mutex_unlock(&m->lock); 341 return retval; 342 } 343 EXPORT_SYMBOL(seq_lseek); 344 345 /** 346 * seq_release - free the structures associated with sequential file. 347 * @inode: its inode 348 * @file: file in question 349 * 350 * Frees the structures associated with sequential file; can be used 351 * as ->f_op->release() if you don't have private data to destroy. 352 */ 353 int seq_release(struct inode *inode, struct file *file) 354 { 355 struct seq_file *m = file->private_data; 356 kvfree(m->buf); 357 kmem_cache_free(seq_file_cache, m); 358 return 0; 359 } 360 EXPORT_SYMBOL(seq_release); 361 362 /** 363 * seq_escape_mem - print data into buffer, escaping some characters 364 * @m: target buffer 365 * @src: source buffer 366 * @len: size of source buffer 367 * @flags: flags to pass to string_escape_mem() 368 * @esc: set of characters that need escaping 369 * 370 * Puts data into buffer, replacing each occurrence of character from 371 * given class (defined by @flags and @esc) with printable escaped sequence. 372 * 373 * Use seq_has_overflowed() to check for errors. 374 */ 375 void seq_escape_mem(struct seq_file *m, const char *src, size_t len, 376 unsigned int flags, const char *esc) 377 { 378 char *buf; 379 size_t size = seq_get_buf(m, &buf); 380 int ret; 381 382 ret = string_escape_mem(src, len, buf, size, flags, esc); 383 seq_commit(m, ret < size ? ret : -1); 384 } 385 EXPORT_SYMBOL(seq_escape_mem); 386 387 void seq_vprintf(struct seq_file *m, const char *f, va_list args) 388 { 389 int len; 390 391 if (m->count < m->size) { 392 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args); 393 if (m->count + len < m->size) { 394 m->count += len; 395 return; 396 } 397 } 398 seq_set_overflow(m); 399 } 400 EXPORT_SYMBOL(seq_vprintf); 401 402 void seq_printf(struct seq_file *m, const char *f, ...) 403 { 404 va_list args; 405 406 va_start(args, f); 407 seq_vprintf(m, f, args); 408 va_end(args); 409 } 410 EXPORT_SYMBOL(seq_printf); 411 412 #ifdef CONFIG_BINARY_PRINTF 413 void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary) 414 { 415 int len; 416 417 if (m->count < m->size) { 418 len = bstr_printf(m->buf + m->count, m->size - m->count, f, 419 binary); 420 if (m->count + len < m->size) { 421 m->count += len; 422 return; 423 } 424 } 425 seq_set_overflow(m); 426 } 427 EXPORT_SYMBOL(seq_bprintf); 428 #endif /* CONFIG_BINARY_PRINTF */ 429 430 /** 431 * mangle_path - mangle and copy path to buffer beginning 432 * @s: buffer start 433 * @p: beginning of path in above buffer 434 * @esc: set of characters that need escaping 435 * 436 * Copy the path from @p to @s, replacing each occurrence of character from 437 * @esc with usual octal escape. 438 * Returns pointer past last written character in @s, or NULL in case of 439 * failure. 440 */ 441 char *mangle_path(char *s, const char *p, const char *esc) 442 { 443 while (s <= p) { 444 char c = *p++; 445 if (!c) { 446 return s; 447 } else if (!strchr(esc, c)) { 448 *s++ = c; 449 } else if (s + 4 > p) { 450 break; 451 } else { 452 *s++ = '\\'; 453 *s++ = '0' + ((c & 0300) >> 6); 454 *s++ = '0' + ((c & 070) >> 3); 455 *s++ = '0' + (c & 07); 456 } 457 } 458 return NULL; 459 } 460 EXPORT_SYMBOL(mangle_path); 461 462 /** 463 * seq_path - seq_file interface to print a pathname 464 * @m: the seq_file handle 465 * @path: the struct path to print 466 * @esc: set of characters to escape in the output 467 * 468 * return the absolute path of 'path', as represented by the 469 * dentry / mnt pair in the path parameter. 470 */ 471 int seq_path(struct seq_file *m, const struct path *path, const char *esc) 472 { 473 char *buf; 474 size_t size = seq_get_buf(m, &buf); 475 int res = -1; 476 477 if (size) { 478 char *p = d_path(path, buf, size); 479 if (!IS_ERR(p)) { 480 char *end = mangle_path(buf, p, esc); 481 if (end) 482 res = end - buf; 483 } 484 } 485 seq_commit(m, res); 486 487 return res; 488 } 489 EXPORT_SYMBOL(seq_path); 490 491 /** 492 * seq_file_path - seq_file interface to print a pathname of a file 493 * @m: the seq_file handle 494 * @file: the struct file to print 495 * @esc: set of characters to escape in the output 496 * 497 * return the absolute path to the file. 498 */ 499 int seq_file_path(struct seq_file *m, struct file *file, const char *esc) 500 { 501 return seq_path(m, &file->f_path, esc); 502 } 503 EXPORT_SYMBOL(seq_file_path); 504 505 /* 506 * Same as seq_path, but relative to supplied root. 507 */ 508 int seq_path_root(struct seq_file *m, const struct path *path, 509 const struct path *root, const char *esc) 510 { 511 char *buf; 512 size_t size = seq_get_buf(m, &buf); 513 int res = -ENAMETOOLONG; 514 515 if (size) { 516 char *p; 517 518 p = __d_path(path, root, buf, size); 519 if (!p) 520 return SEQ_SKIP; 521 res = PTR_ERR(p); 522 if (!IS_ERR(p)) { 523 char *end = mangle_path(buf, p, esc); 524 if (end) 525 res = end - buf; 526 else 527 res = -ENAMETOOLONG; 528 } 529 } 530 seq_commit(m, res); 531 532 return res < 0 && res != -ENAMETOOLONG ? res : 0; 533 } 534 535 /* 536 * returns the path of the 'dentry' from the root of its filesystem. 537 */ 538 int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) 539 { 540 char *buf; 541 size_t size = seq_get_buf(m, &buf); 542 int res = -1; 543 544 if (size) { 545 char *p = dentry_path(dentry, buf, size); 546 if (!IS_ERR(p)) { 547 char *end = mangle_path(buf, p, esc); 548 if (end) 549 res = end - buf; 550 } 551 } 552 seq_commit(m, res); 553 554 return res; 555 } 556 EXPORT_SYMBOL(seq_dentry); 557 558 void *single_start(struct seq_file *p, loff_t *pos) 559 { 560 return *pos ? NULL : SEQ_START_TOKEN; 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_ACCOUNT); 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_open_size(struct file *file, int (*show)(struct seq_file *, void *), 595 void *data, size_t size) 596 { 597 char *buf = seq_buf_alloc(size); 598 int ret; 599 if (!buf) 600 return -ENOMEM; 601 ret = single_open(file, show, data); 602 if (ret) { 603 kvfree(buf); 604 return ret; 605 } 606 ((struct seq_file *)file->private_data)->buf = buf; 607 ((struct seq_file *)file->private_data)->size = size; 608 return 0; 609 } 610 EXPORT_SYMBOL(single_open_size); 611 612 int single_release(struct inode *inode, struct file *file) 613 { 614 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op; 615 int res = seq_release(inode, file); 616 kfree(op); 617 return res; 618 } 619 EXPORT_SYMBOL(single_release); 620 621 int seq_release_private(struct inode *inode, struct file *file) 622 { 623 struct seq_file *seq = file->private_data; 624 625 kfree(seq->private); 626 seq->private = NULL; 627 return seq_release(inode, file); 628 } 629 EXPORT_SYMBOL(seq_release_private); 630 631 void *__seq_open_private(struct file *f, const struct seq_operations *ops, 632 int psize) 633 { 634 int rc; 635 void *private; 636 struct seq_file *seq; 637 638 private = kzalloc(psize, GFP_KERNEL_ACCOUNT); 639 if (private == NULL) 640 goto out; 641 642 rc = seq_open(f, ops); 643 if (rc < 0) 644 goto out_free; 645 646 seq = f->private_data; 647 seq->private = private; 648 return private; 649 650 out_free: 651 kfree(private); 652 out: 653 return NULL; 654 } 655 EXPORT_SYMBOL(__seq_open_private); 656 657 int seq_open_private(struct file *filp, const struct seq_operations *ops, 658 int psize) 659 { 660 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM; 661 } 662 EXPORT_SYMBOL(seq_open_private); 663 664 void seq_putc(struct seq_file *m, char c) 665 { 666 if (m->count >= m->size) 667 return; 668 669 m->buf[m->count++] = c; 670 } 671 EXPORT_SYMBOL(seq_putc); 672 673 void __seq_puts(struct seq_file *m, const char *s) 674 { 675 seq_write(m, s, strlen(s)); 676 } 677 EXPORT_SYMBOL(__seq_puts); 678 679 /** 680 * seq_put_decimal_ull_width - A helper routine for putting decimal numbers 681 * without rich format of printf(). 682 * only 'unsigned long long' is supported. 683 * @m: seq_file identifying the buffer to which data should be written 684 * @delimiter: a string which is printed before the number 685 * @num: the number 686 * @width: a minimum field width 687 * 688 * This routine will put strlen(delimiter) + number into seq_filed. 689 * This routine is very quick when you show lots of numbers. 690 * In usual cases, it will be better to use seq_printf(). It's easier to read. 691 */ 692 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter, 693 unsigned long long num, unsigned int width) 694 { 695 int len; 696 697 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */ 698 goto overflow; 699 700 if (delimiter && delimiter[0]) { 701 if (delimiter[1] == 0) 702 seq_putc(m, delimiter[0]); 703 else 704 seq_puts(m, delimiter); 705 } 706 707 if (!width) 708 width = 1; 709 710 if (m->count + width >= m->size) 711 goto overflow; 712 713 len = num_to_str(m->buf + m->count, m->size - m->count, num, width); 714 if (!len) 715 goto overflow; 716 717 m->count += len; 718 return; 719 720 overflow: 721 seq_set_overflow(m); 722 } 723 724 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter, 725 unsigned long long num) 726 { 727 return seq_put_decimal_ull_width(m, delimiter, num, 0); 728 } 729 EXPORT_SYMBOL(seq_put_decimal_ull); 730 731 /** 732 * seq_put_hex_ll - put a number in hexadecimal notation 733 * @m: seq_file identifying the buffer to which data should be written 734 * @delimiter: a string which is printed before the number 735 * @v: the number 736 * @width: a minimum field width 737 * 738 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v) 739 * 740 * This routine is very quick when you show lots of numbers. 741 * In usual cases, it will be better to use seq_printf(). It's easier to read. 742 */ 743 void seq_put_hex_ll(struct seq_file *m, const char *delimiter, 744 unsigned long long v, unsigned int width) 745 { 746 unsigned int len; 747 int i; 748 749 if (delimiter && delimiter[0]) { 750 if (delimiter[1] == 0) 751 seq_putc(m, delimiter[0]); 752 else 753 seq_puts(m, delimiter); 754 } 755 756 /* If x is 0, the result of __builtin_clzll is undefined */ 757 if (v == 0) 758 len = 1; 759 else 760 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4; 761 762 if (len < width) 763 len = width; 764 765 if (m->count + len > m->size) { 766 seq_set_overflow(m); 767 return; 768 } 769 770 for (i = len - 1; i >= 0; i--) { 771 m->buf[m->count + i] = hex_asc[0xf & v]; 772 v = v >> 4; 773 } 774 m->count += len; 775 } 776 777 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num) 778 { 779 int len; 780 781 if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */ 782 goto overflow; 783 784 if (delimiter && delimiter[0]) { 785 if (delimiter[1] == 0) 786 seq_putc(m, delimiter[0]); 787 else 788 seq_puts(m, delimiter); 789 } 790 791 if (m->count + 2 >= m->size) 792 goto overflow; 793 794 if (num < 0) { 795 m->buf[m->count++] = '-'; 796 num = -num; 797 } 798 799 if (num < 10) { 800 m->buf[m->count++] = num + '0'; 801 return; 802 } 803 804 len = num_to_str(m->buf + m->count, m->size - m->count, num, 0); 805 if (!len) 806 goto overflow; 807 808 m->count += len; 809 return; 810 811 overflow: 812 seq_set_overflow(m); 813 } 814 EXPORT_SYMBOL(seq_put_decimal_ll); 815 816 /** 817 * seq_write - write arbitrary data to buffer 818 * @seq: seq_file identifying the buffer to which data should be written 819 * @data: data address 820 * @len: number of bytes 821 * 822 * Return 0 on success, non-zero otherwise. 823 */ 824 int seq_write(struct seq_file *seq, const void *data, size_t len) 825 { 826 if (seq->count + len < seq->size) { 827 memcpy(seq->buf + seq->count, data, len); 828 seq->count += len; 829 return 0; 830 } 831 seq_set_overflow(seq); 832 return -1; 833 } 834 EXPORT_SYMBOL(seq_write); 835 836 /** 837 * seq_pad - write padding spaces to buffer 838 * @m: seq_file identifying the buffer to which data should be written 839 * @c: the byte to append after padding if non-zero 840 */ 841 void seq_pad(struct seq_file *m, char c) 842 { 843 int size = m->pad_until - m->count; 844 if (size > 0) { 845 if (size + m->count > m->size) { 846 seq_set_overflow(m); 847 return; 848 } 849 memset(m->buf + m->count, ' ', size); 850 m->count += size; 851 } 852 if (c) 853 seq_putc(m, c); 854 } 855 EXPORT_SYMBOL(seq_pad); 856 857 /* A complete analogue of print_hex_dump() */ 858 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type, 859 int rowsize, int groupsize, const void *buf, size_t len, 860 bool ascii) 861 { 862 const u8 *ptr = buf; 863 int i, linelen, remaining = len; 864 char *buffer; 865 size_t size; 866 int ret; 867 868 if (rowsize != 16 && rowsize != 32) 869 rowsize = 16; 870 871 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) { 872 linelen = min(remaining, rowsize); 873 remaining -= rowsize; 874 875 switch (prefix_type) { 876 case DUMP_PREFIX_ADDRESS: 877 seq_printf(m, "%s%p: ", prefix_str, ptr + i); 878 break; 879 case DUMP_PREFIX_OFFSET: 880 seq_printf(m, "%s%.8x: ", prefix_str, i); 881 break; 882 default: 883 seq_printf(m, "%s", prefix_str); 884 break; 885 } 886 887 size = seq_get_buf(m, &buffer); 888 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, 889 buffer, size, ascii); 890 seq_commit(m, ret < size ? ret : -1); 891 892 seq_putc(m, '\n'); 893 } 894 } 895 EXPORT_SYMBOL(seq_hex_dump); 896 897 struct list_head *seq_list_start(struct list_head *head, loff_t pos) 898 { 899 struct list_head *lh; 900 901 list_for_each(lh, head) 902 if (pos-- == 0) 903 return lh; 904 905 return NULL; 906 } 907 EXPORT_SYMBOL(seq_list_start); 908 909 struct list_head *seq_list_start_head(struct list_head *head, loff_t pos) 910 { 911 if (!pos) 912 return head; 913 914 return seq_list_start(head, pos - 1); 915 } 916 EXPORT_SYMBOL(seq_list_start_head); 917 918 struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos) 919 { 920 struct list_head *lh; 921 922 lh = ((struct list_head *)v)->next; 923 ++*ppos; 924 return lh == head ? NULL : lh; 925 } 926 EXPORT_SYMBOL(seq_list_next); 927 928 struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos) 929 { 930 struct list_head *lh; 931 932 list_for_each_rcu(lh, head) 933 if (pos-- == 0) 934 return lh; 935 936 return NULL; 937 } 938 EXPORT_SYMBOL(seq_list_start_rcu); 939 940 struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos) 941 { 942 if (!pos) 943 return head; 944 945 return seq_list_start_rcu(head, pos - 1); 946 } 947 EXPORT_SYMBOL(seq_list_start_head_rcu); 948 949 struct list_head *seq_list_next_rcu(void *v, struct list_head *head, 950 loff_t *ppos) 951 { 952 struct list_head *lh; 953 954 lh = list_next_rcu((struct list_head *)v); 955 ++*ppos; 956 return lh == head ? NULL : lh; 957 } 958 EXPORT_SYMBOL(seq_list_next_rcu); 959 960 /** 961 * seq_hlist_start - start an iteration of a hlist 962 * @head: the head of the hlist 963 * @pos: the start position of the sequence 964 * 965 * Called at seq_file->op->start(). 966 */ 967 struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos) 968 { 969 struct hlist_node *node; 970 971 hlist_for_each(node, head) 972 if (pos-- == 0) 973 return node; 974 return NULL; 975 } 976 EXPORT_SYMBOL(seq_hlist_start); 977 978 /** 979 * seq_hlist_start_head - start an iteration of a hlist 980 * @head: the head of the hlist 981 * @pos: the start position of the sequence 982 * 983 * Called at seq_file->op->start(). Call this function if you want to 984 * print a header at the top of the output. 985 */ 986 struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos) 987 { 988 if (!pos) 989 return SEQ_START_TOKEN; 990 991 return seq_hlist_start(head, pos - 1); 992 } 993 EXPORT_SYMBOL(seq_hlist_start_head); 994 995 /** 996 * seq_hlist_next - move to the next position of the hlist 997 * @v: the current iterator 998 * @head: the head of the hlist 999 * @ppos: the current position 1000 * 1001 * Called at seq_file->op->next(). 1002 */ 1003 struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head, 1004 loff_t *ppos) 1005 { 1006 struct hlist_node *node = v; 1007 1008 ++*ppos; 1009 if (v == SEQ_START_TOKEN) 1010 return head->first; 1011 else 1012 return node->next; 1013 } 1014 EXPORT_SYMBOL(seq_hlist_next); 1015 1016 /** 1017 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU 1018 * @head: the head of the hlist 1019 * @pos: the start position of the sequence 1020 * 1021 * Called at seq_file->op->start(). 1022 * 1023 * This list-traversal primitive may safely run concurrently with 1024 * the _rcu list-mutation primitives such as hlist_add_head_rcu() 1025 * as long as the traversal is guarded by rcu_read_lock(). 1026 */ 1027 struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head, 1028 loff_t pos) 1029 { 1030 struct hlist_node *node; 1031 1032 __hlist_for_each_rcu(node, head) 1033 if (pos-- == 0) 1034 return node; 1035 return NULL; 1036 } 1037 EXPORT_SYMBOL(seq_hlist_start_rcu); 1038 1039 /** 1040 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU 1041 * @head: the head of the hlist 1042 * @pos: the start position of the sequence 1043 * 1044 * Called at seq_file->op->start(). Call this function if you want to 1045 * print a header at the top of the output. 1046 * 1047 * This list-traversal primitive may safely run concurrently with 1048 * the _rcu list-mutation primitives such as hlist_add_head_rcu() 1049 * as long as the traversal is guarded by rcu_read_lock(). 1050 */ 1051 struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head, 1052 loff_t pos) 1053 { 1054 if (!pos) 1055 return SEQ_START_TOKEN; 1056 1057 return seq_hlist_start_rcu(head, pos - 1); 1058 } 1059 EXPORT_SYMBOL(seq_hlist_start_head_rcu); 1060 1061 /** 1062 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU 1063 * @v: the current iterator 1064 * @head: the head of the hlist 1065 * @ppos: the current position 1066 * 1067 * Called at seq_file->op->next(). 1068 * 1069 * This list-traversal primitive may safely run concurrently with 1070 * the _rcu list-mutation primitives such as hlist_add_head_rcu() 1071 * as long as the traversal is guarded by rcu_read_lock(). 1072 */ 1073 struct hlist_node *seq_hlist_next_rcu(void *v, 1074 struct hlist_head *head, 1075 loff_t *ppos) 1076 { 1077 struct hlist_node *node = v; 1078 1079 ++*ppos; 1080 if (v == SEQ_START_TOKEN) 1081 return rcu_dereference(head->first); 1082 else 1083 return rcu_dereference(node->next); 1084 } 1085 EXPORT_SYMBOL(seq_hlist_next_rcu); 1086 1087 /** 1088 * seq_hlist_start_percpu - start an iteration of a percpu hlist array 1089 * @head: pointer to percpu array of struct hlist_heads 1090 * @cpu: pointer to cpu "cursor" 1091 * @pos: start position of sequence 1092 * 1093 * Called at seq_file->op->start(). 1094 */ 1095 struct hlist_node * 1096 seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos) 1097 { 1098 struct hlist_node *node; 1099 1100 for_each_possible_cpu(*cpu) { 1101 hlist_for_each(node, per_cpu_ptr(head, *cpu)) { 1102 if (pos-- == 0) 1103 return node; 1104 } 1105 } 1106 return NULL; 1107 } 1108 EXPORT_SYMBOL(seq_hlist_start_percpu); 1109 1110 /** 1111 * seq_hlist_next_percpu - move to the next position of the percpu hlist array 1112 * @v: pointer to current hlist_node 1113 * @head: pointer to percpu array of struct hlist_heads 1114 * @cpu: pointer to cpu "cursor" 1115 * @pos: start position of sequence 1116 * 1117 * Called at seq_file->op->next(). 1118 */ 1119 struct hlist_node * 1120 seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, 1121 int *cpu, loff_t *pos) 1122 { 1123 struct hlist_node *node = v; 1124 1125 ++*pos; 1126 1127 if (node->next) 1128 return node->next; 1129 1130 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids; 1131 *cpu = cpumask_next(*cpu, cpu_possible_mask)) { 1132 struct hlist_head *bucket = per_cpu_ptr(head, *cpu); 1133 1134 if (!hlist_empty(bucket)) 1135 return bucket->first; 1136 } 1137 return NULL; 1138 } 1139 EXPORT_SYMBOL(seq_hlist_next_percpu); 1140 1141 void __init seq_file_init(void) 1142 { 1143 seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC); 1144 } 1145