1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2014 Red Hat, Inc. 4 * All Rights Reserved. 5 */ 6 7 #include "xfs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_sysfs.h" 13 #include "xfs_log.h" 14 #include "xfs_log_priv.h" 15 #include "xfs_mount.h" 16 17 struct xfs_sysfs_attr { 18 struct attribute attr; 19 ssize_t (*show)(struct kobject *kobject, char *buf); 20 ssize_t (*store)(struct kobject *kobject, const char *buf, 21 size_t count); 22 }; 23 24 static inline struct xfs_sysfs_attr * 25 to_attr(struct attribute *attr) 26 { 27 return container_of(attr, struct xfs_sysfs_attr, attr); 28 } 29 30 #define XFS_SYSFS_ATTR_RW(name) \ 31 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name) 32 #define XFS_SYSFS_ATTR_RO(name) \ 33 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name) 34 #define XFS_SYSFS_ATTR_WO(name) \ 35 static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name) 36 37 #define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr 38 39 STATIC ssize_t 40 xfs_sysfs_object_show( 41 struct kobject *kobject, 42 struct attribute *attr, 43 char *buf) 44 { 45 struct xfs_sysfs_attr *xfs_attr = to_attr(attr); 46 47 return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0; 48 } 49 50 STATIC ssize_t 51 xfs_sysfs_object_store( 52 struct kobject *kobject, 53 struct attribute *attr, 54 const char *buf, 55 size_t count) 56 { 57 struct xfs_sysfs_attr *xfs_attr = to_attr(attr); 58 59 return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0; 60 } 61 62 static const struct sysfs_ops xfs_sysfs_ops = { 63 .show = xfs_sysfs_object_show, 64 .store = xfs_sysfs_object_store, 65 }; 66 67 static struct attribute *xfs_mp_attrs[] = { 68 NULL, 69 }; 70 ATTRIBUTE_GROUPS(xfs_mp); 71 72 const struct kobj_type xfs_mp_ktype = { 73 .release = xfs_sysfs_release, 74 .sysfs_ops = &xfs_sysfs_ops, 75 .default_groups = xfs_mp_groups, 76 }; 77 78 #ifdef DEBUG 79 /* debug */ 80 81 STATIC ssize_t 82 bug_on_assert_store( 83 struct kobject *kobject, 84 const char *buf, 85 size_t count) 86 { 87 int ret; 88 int val; 89 90 ret = kstrtoint(buf, 0, &val); 91 if (ret) 92 return ret; 93 94 if (val == 1) 95 xfs_globals.bug_on_assert = true; 96 else if (val == 0) 97 xfs_globals.bug_on_assert = false; 98 else 99 return -EINVAL; 100 101 return count; 102 } 103 104 STATIC ssize_t 105 bug_on_assert_show( 106 struct kobject *kobject, 107 char *buf) 108 { 109 return sysfs_emit(buf, "%d\n", xfs_globals.bug_on_assert); 110 } 111 XFS_SYSFS_ATTR_RW(bug_on_assert); 112 113 STATIC ssize_t 114 log_recovery_delay_store( 115 struct kobject *kobject, 116 const char *buf, 117 size_t count) 118 { 119 int ret; 120 int val; 121 122 ret = kstrtoint(buf, 0, &val); 123 if (ret) 124 return ret; 125 126 if (val < 0 || val > 60) 127 return -EINVAL; 128 129 xfs_globals.log_recovery_delay = val; 130 131 return count; 132 } 133 134 STATIC ssize_t 135 log_recovery_delay_show( 136 struct kobject *kobject, 137 char *buf) 138 { 139 return sysfs_emit(buf, "%d\n", xfs_globals.log_recovery_delay); 140 } 141 XFS_SYSFS_ATTR_RW(log_recovery_delay); 142 143 STATIC ssize_t 144 mount_delay_store( 145 struct kobject *kobject, 146 const char *buf, 147 size_t count) 148 { 149 int ret; 150 int val; 151 152 ret = kstrtoint(buf, 0, &val); 153 if (ret) 154 return ret; 155 156 if (val < 0 || val > 60) 157 return -EINVAL; 158 159 xfs_globals.mount_delay = val; 160 161 return count; 162 } 163 164 STATIC ssize_t 165 mount_delay_show( 166 struct kobject *kobject, 167 char *buf) 168 { 169 return sysfs_emit(buf, "%d\n", xfs_globals.mount_delay); 170 } 171 XFS_SYSFS_ATTR_RW(mount_delay); 172 173 static ssize_t 174 always_cow_store( 175 struct kobject *kobject, 176 const char *buf, 177 size_t count) 178 { 179 ssize_t ret; 180 181 ret = kstrtobool(buf, &xfs_globals.always_cow); 182 if (ret < 0) 183 return ret; 184 return count; 185 } 186 187 static ssize_t 188 always_cow_show( 189 struct kobject *kobject, 190 char *buf) 191 { 192 return sysfs_emit(buf, "%d\n", xfs_globals.always_cow); 193 } 194 XFS_SYSFS_ATTR_RW(always_cow); 195 196 #ifdef DEBUG 197 /* 198 * Override how many threads the parallel work queue is allowed to create. 199 * This has to be a debug-only global (instead of an errortag) because one of 200 * the main users of parallel workqueues is mount time quotacheck. 201 */ 202 STATIC ssize_t 203 pwork_threads_store( 204 struct kobject *kobject, 205 const char *buf, 206 size_t count) 207 { 208 int ret; 209 int val; 210 211 ret = kstrtoint(buf, 0, &val); 212 if (ret) 213 return ret; 214 215 if (val < -1 || val > num_possible_cpus()) 216 return -EINVAL; 217 218 xfs_globals.pwork_threads = val; 219 220 return count; 221 } 222 223 STATIC ssize_t 224 pwork_threads_show( 225 struct kobject *kobject, 226 char *buf) 227 { 228 return sysfs_emit(buf, "%d\n", xfs_globals.pwork_threads); 229 } 230 XFS_SYSFS_ATTR_RW(pwork_threads); 231 232 /* 233 * The "LARP" (Logged extended Attribute Recovery Persistence) debugging knob 234 * sets the XFS_DA_OP_LOGGED flag on all xfs_attr_set operations performed on 235 * V5 filesystems. As a result, the intermediate progress of all setxattr and 236 * removexattr operations are tracked via the log and can be restarted during 237 * recovery. This is useful for testing xattr recovery prior to merging of the 238 * parent pointer feature which requires it to maintain consistency, and may be 239 * enabled for userspace xattrs in the future. 240 */ 241 static ssize_t 242 larp_store( 243 struct kobject *kobject, 244 const char *buf, 245 size_t count) 246 { 247 ssize_t ret; 248 249 ret = kstrtobool(buf, &xfs_globals.larp); 250 if (ret < 0) 251 return ret; 252 return count; 253 } 254 255 STATIC ssize_t 256 larp_show( 257 struct kobject *kobject, 258 char *buf) 259 { 260 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.larp); 261 } 262 XFS_SYSFS_ATTR_RW(larp); 263 #endif /* DEBUG */ 264 265 STATIC ssize_t 266 bload_leaf_slack_store( 267 struct kobject *kobject, 268 const char *buf, 269 size_t count) 270 { 271 int ret; 272 int val; 273 274 ret = kstrtoint(buf, 0, &val); 275 if (ret) 276 return ret; 277 278 xfs_globals.bload_leaf_slack = val; 279 return count; 280 } 281 282 STATIC ssize_t 283 bload_leaf_slack_show( 284 struct kobject *kobject, 285 char *buf) 286 { 287 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bload_leaf_slack); 288 } 289 XFS_SYSFS_ATTR_RW(bload_leaf_slack); 290 291 STATIC ssize_t 292 bload_node_slack_store( 293 struct kobject *kobject, 294 const char *buf, 295 size_t count) 296 { 297 int ret; 298 int val; 299 300 ret = kstrtoint(buf, 0, &val); 301 if (ret) 302 return ret; 303 304 xfs_globals.bload_node_slack = val; 305 return count; 306 } 307 308 STATIC ssize_t 309 bload_node_slack_show( 310 struct kobject *kobject, 311 char *buf) 312 { 313 return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bload_node_slack); 314 } 315 XFS_SYSFS_ATTR_RW(bload_node_slack); 316 317 static struct attribute *xfs_dbg_attrs[] = { 318 ATTR_LIST(bug_on_assert), 319 ATTR_LIST(log_recovery_delay), 320 ATTR_LIST(mount_delay), 321 ATTR_LIST(always_cow), 322 #ifdef DEBUG 323 ATTR_LIST(pwork_threads), 324 ATTR_LIST(larp), 325 #endif 326 ATTR_LIST(bload_leaf_slack), 327 ATTR_LIST(bload_node_slack), 328 NULL, 329 }; 330 ATTRIBUTE_GROUPS(xfs_dbg); 331 332 const struct kobj_type xfs_dbg_ktype = { 333 .release = xfs_sysfs_release, 334 .sysfs_ops = &xfs_sysfs_ops, 335 .default_groups = xfs_dbg_groups, 336 }; 337 338 #endif /* DEBUG */ 339 340 /* stats */ 341 342 static inline struct xstats * 343 to_xstats(struct kobject *kobject) 344 { 345 struct xfs_kobj *kobj = to_kobj(kobject); 346 347 return container_of(kobj, struct xstats, xs_kobj); 348 } 349 350 STATIC ssize_t 351 stats_show( 352 struct kobject *kobject, 353 char *buf) 354 { 355 struct xstats *stats = to_xstats(kobject); 356 357 return xfs_stats_format(stats->xs_stats, buf); 358 } 359 XFS_SYSFS_ATTR_RO(stats); 360 361 STATIC ssize_t 362 stats_clear_store( 363 struct kobject *kobject, 364 const char *buf, 365 size_t count) 366 { 367 int ret; 368 int val; 369 struct xstats *stats = to_xstats(kobject); 370 371 ret = kstrtoint(buf, 0, &val); 372 if (ret) 373 return ret; 374 375 if (val != 1) 376 return -EINVAL; 377 378 xfs_stats_clearall(stats->xs_stats); 379 return count; 380 } 381 XFS_SYSFS_ATTR_WO(stats_clear); 382 383 static struct attribute *xfs_stats_attrs[] = { 384 ATTR_LIST(stats), 385 ATTR_LIST(stats_clear), 386 NULL, 387 }; 388 ATTRIBUTE_GROUPS(xfs_stats); 389 390 const struct kobj_type xfs_stats_ktype = { 391 .release = xfs_sysfs_release, 392 .sysfs_ops = &xfs_sysfs_ops, 393 .default_groups = xfs_stats_groups, 394 }; 395 396 /* xlog */ 397 398 static inline struct xlog * 399 to_xlog(struct kobject *kobject) 400 { 401 struct xfs_kobj *kobj = to_kobj(kobject); 402 403 return container_of(kobj, struct xlog, l_kobj); 404 } 405 406 STATIC ssize_t 407 log_head_lsn_show( 408 struct kobject *kobject, 409 char *buf) 410 { 411 int cycle; 412 int block; 413 struct xlog *log = to_xlog(kobject); 414 415 spin_lock(&log->l_icloglock); 416 cycle = log->l_curr_cycle; 417 block = log->l_curr_block; 418 spin_unlock(&log->l_icloglock); 419 420 return sysfs_emit(buf, "%d:%d\n", cycle, block); 421 } 422 XFS_SYSFS_ATTR_RO(log_head_lsn); 423 424 STATIC ssize_t 425 log_tail_lsn_show( 426 struct kobject *kobject, 427 char *buf) 428 { 429 int cycle; 430 int block; 431 struct xlog *log = to_xlog(kobject); 432 433 xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block); 434 return sysfs_emit(buf, "%d:%d\n", cycle, block); 435 } 436 XFS_SYSFS_ATTR_RO(log_tail_lsn); 437 438 STATIC ssize_t 439 reserve_grant_head_show( 440 struct kobject *kobject, 441 char *buf) 442 443 { 444 int cycle; 445 int bytes; 446 struct xlog *log = to_xlog(kobject); 447 448 xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes); 449 return sysfs_emit(buf, "%d:%d\n", cycle, bytes); 450 } 451 XFS_SYSFS_ATTR_RO(reserve_grant_head); 452 453 STATIC ssize_t 454 write_grant_head_show( 455 struct kobject *kobject, 456 char *buf) 457 { 458 int cycle; 459 int bytes; 460 struct xlog *log = to_xlog(kobject); 461 462 xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes); 463 return sysfs_emit(buf, "%d:%d\n", cycle, bytes); 464 } 465 XFS_SYSFS_ATTR_RO(write_grant_head); 466 467 static struct attribute *xfs_log_attrs[] = { 468 ATTR_LIST(log_head_lsn), 469 ATTR_LIST(log_tail_lsn), 470 ATTR_LIST(reserve_grant_head), 471 ATTR_LIST(write_grant_head), 472 NULL, 473 }; 474 ATTRIBUTE_GROUPS(xfs_log); 475 476 const struct kobj_type xfs_log_ktype = { 477 .release = xfs_sysfs_release, 478 .sysfs_ops = &xfs_sysfs_ops, 479 .default_groups = xfs_log_groups, 480 }; 481 482 /* 483 * Metadata IO error configuration 484 * 485 * The sysfs structure here is: 486 * ...xfs/<dev>/error/<class>/<errno>/<error_attrs> 487 * 488 * where <class> allows us to discriminate between data IO and metadata IO, 489 * and any other future type of IO (e.g. special inode or directory error 490 * handling) we care to support. 491 */ 492 static inline struct xfs_error_cfg * 493 to_error_cfg(struct kobject *kobject) 494 { 495 struct xfs_kobj *kobj = to_kobj(kobject); 496 return container_of(kobj, struct xfs_error_cfg, kobj); 497 } 498 499 static inline struct xfs_mount * 500 err_to_mp(struct kobject *kobject) 501 { 502 struct xfs_kobj *kobj = to_kobj(kobject); 503 return container_of(kobj, struct xfs_mount, m_error_kobj); 504 } 505 506 static ssize_t 507 max_retries_show( 508 struct kobject *kobject, 509 char *buf) 510 { 511 int retries; 512 struct xfs_error_cfg *cfg = to_error_cfg(kobject); 513 514 if (cfg->max_retries == XFS_ERR_RETRY_FOREVER) 515 retries = -1; 516 else 517 retries = cfg->max_retries; 518 519 return sysfs_emit(buf, "%d\n", retries); 520 } 521 522 static ssize_t 523 max_retries_store( 524 struct kobject *kobject, 525 const char *buf, 526 size_t count) 527 { 528 struct xfs_error_cfg *cfg = to_error_cfg(kobject); 529 int ret; 530 int val; 531 532 ret = kstrtoint(buf, 0, &val); 533 if (ret) 534 return ret; 535 536 if (val < -1) 537 return -EINVAL; 538 539 if (val == -1) 540 cfg->max_retries = XFS_ERR_RETRY_FOREVER; 541 else 542 cfg->max_retries = val; 543 return count; 544 } 545 XFS_SYSFS_ATTR_RW(max_retries); 546 547 static ssize_t 548 retry_timeout_seconds_show( 549 struct kobject *kobject, 550 char *buf) 551 { 552 int timeout; 553 struct xfs_error_cfg *cfg = to_error_cfg(kobject); 554 555 if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER) 556 timeout = -1; 557 else 558 timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC; 559 560 return sysfs_emit(buf, "%d\n", timeout); 561 } 562 563 static ssize_t 564 retry_timeout_seconds_store( 565 struct kobject *kobject, 566 const char *buf, 567 size_t count) 568 { 569 struct xfs_error_cfg *cfg = to_error_cfg(kobject); 570 int ret; 571 int val; 572 573 ret = kstrtoint(buf, 0, &val); 574 if (ret) 575 return ret; 576 577 /* 1 day timeout maximum, -1 means infinite */ 578 if (val < -1 || val > 86400) 579 return -EINVAL; 580 581 if (val == -1) 582 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER; 583 else { 584 cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC); 585 ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX); 586 } 587 return count; 588 } 589 XFS_SYSFS_ATTR_RW(retry_timeout_seconds); 590 591 static ssize_t 592 fail_at_unmount_show( 593 struct kobject *kobject, 594 char *buf) 595 { 596 struct xfs_mount *mp = err_to_mp(kobject); 597 598 return sysfs_emit(buf, "%d\n", mp->m_fail_unmount); 599 } 600 601 static ssize_t 602 fail_at_unmount_store( 603 struct kobject *kobject, 604 const char *buf, 605 size_t count) 606 { 607 struct xfs_mount *mp = err_to_mp(kobject); 608 int ret; 609 int val; 610 611 ret = kstrtoint(buf, 0, &val); 612 if (ret) 613 return ret; 614 615 if (val < 0 || val > 1) 616 return -EINVAL; 617 618 mp->m_fail_unmount = val; 619 return count; 620 } 621 XFS_SYSFS_ATTR_RW(fail_at_unmount); 622 623 static struct attribute *xfs_error_attrs[] = { 624 ATTR_LIST(max_retries), 625 ATTR_LIST(retry_timeout_seconds), 626 NULL, 627 }; 628 ATTRIBUTE_GROUPS(xfs_error); 629 630 static const struct kobj_type xfs_error_cfg_ktype = { 631 .release = xfs_sysfs_release, 632 .sysfs_ops = &xfs_sysfs_ops, 633 .default_groups = xfs_error_groups, 634 }; 635 636 static const struct kobj_type xfs_error_ktype = { 637 .release = xfs_sysfs_release, 638 .sysfs_ops = &xfs_sysfs_ops, 639 }; 640 641 /* 642 * Error initialization tables. These need to be ordered in the same 643 * order as the enums used to index the array. All class init tables need to 644 * define a "default" behaviour as the first entry, all other entries can be 645 * empty. 646 */ 647 struct xfs_error_init { 648 char *name; 649 int max_retries; 650 int retry_timeout; /* in seconds */ 651 }; 652 653 static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = { 654 { .name = "default", 655 .max_retries = XFS_ERR_RETRY_FOREVER, 656 .retry_timeout = XFS_ERR_RETRY_FOREVER, 657 }, 658 { .name = "EIO", 659 .max_retries = XFS_ERR_RETRY_FOREVER, 660 .retry_timeout = XFS_ERR_RETRY_FOREVER, 661 }, 662 { .name = "ENOSPC", 663 .max_retries = XFS_ERR_RETRY_FOREVER, 664 .retry_timeout = XFS_ERR_RETRY_FOREVER, 665 }, 666 { .name = "ENODEV", 667 .max_retries = 0, /* We can't recover from devices disappearing */ 668 .retry_timeout = 0, 669 }, 670 }; 671 672 static int 673 xfs_error_sysfs_init_class( 674 struct xfs_mount *mp, 675 int class, 676 const char *parent_name, 677 struct xfs_kobj *parent_kobj, 678 const struct xfs_error_init init[]) 679 { 680 struct xfs_error_cfg *cfg; 681 int error; 682 int i; 683 684 ASSERT(class < XFS_ERR_CLASS_MAX); 685 686 error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype, 687 &mp->m_error_kobj, parent_name); 688 if (error) 689 return error; 690 691 for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) { 692 cfg = &mp->m_error_cfg[class][i]; 693 error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype, 694 parent_kobj, init[i].name); 695 if (error) 696 goto out_error; 697 698 cfg->max_retries = init[i].max_retries; 699 if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER) 700 cfg->retry_timeout = XFS_ERR_RETRY_FOREVER; 701 else 702 cfg->retry_timeout = msecs_to_jiffies( 703 init[i].retry_timeout * MSEC_PER_SEC); 704 } 705 return 0; 706 707 out_error: 708 /* unwind the entries that succeeded */ 709 for (i--; i >= 0; i--) { 710 cfg = &mp->m_error_cfg[class][i]; 711 xfs_sysfs_del(&cfg->kobj); 712 } 713 xfs_sysfs_del(parent_kobj); 714 return error; 715 } 716 717 int 718 xfs_error_sysfs_init( 719 struct xfs_mount *mp) 720 { 721 int error; 722 723 /* .../xfs/<dev>/error/ */ 724 error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype, 725 &mp->m_kobj, "error"); 726 if (error) 727 return error; 728 729 error = sysfs_create_file(&mp->m_error_kobj.kobject, 730 ATTR_LIST(fail_at_unmount)); 731 732 if (error) 733 goto out_error; 734 735 /* .../xfs/<dev>/error/metadata/ */ 736 error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA, 737 "metadata", &mp->m_error_meta_kobj, 738 xfs_error_meta_init); 739 if (error) 740 goto out_error; 741 742 return 0; 743 744 out_error: 745 xfs_sysfs_del(&mp->m_error_kobj); 746 return error; 747 } 748 749 void 750 xfs_error_sysfs_del( 751 struct xfs_mount *mp) 752 { 753 struct xfs_error_cfg *cfg; 754 int i, j; 755 756 for (i = 0; i < XFS_ERR_CLASS_MAX; i++) { 757 for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) { 758 cfg = &mp->m_error_cfg[i][j]; 759 760 xfs_sysfs_del(&cfg->kobj); 761 } 762 } 763 xfs_sysfs_del(&mp->m_error_meta_kobj); 764 xfs_sysfs_del(&mp->m_error_kobj); 765 } 766 767 struct xfs_error_cfg * 768 xfs_error_get_cfg( 769 struct xfs_mount *mp, 770 int error_class, 771 int error) 772 { 773 struct xfs_error_cfg *cfg; 774 775 if (error < 0) 776 error = -error; 777 778 switch (error) { 779 case EIO: 780 cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO]; 781 break; 782 case ENOSPC: 783 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC]; 784 break; 785 case ENODEV: 786 cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV]; 787 break; 788 default: 789 cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT]; 790 break; 791 } 792 793 return cfg; 794 } 795