1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25 * Copyright (c) 2013 Steven Hartland. All rights reserved. 26 */ 27 28 /* 29 * zhack is a debugging tool that can write changes to ZFS pool using libzpool 30 * for testing purposes. Altering pools with zhack is unsupported and may 31 * result in corrupted pools. 32 */ 33 34 #include <zfs_prop.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <ctype.h> 38 #include <sys/stat.h> 39 #include <sys/zfs_context.h> 40 #include <sys/spa.h> 41 #include <sys/spa_impl.h> 42 #include <sys/dmu.h> 43 #include <sys/zap.h> 44 #include <sys/zfs_znode.h> 45 #include <sys/dsl_synctask.h> 46 #include <sys/vdev.h> 47 #include <sys/vdev_impl.h> 48 #include <sys/fs/zfs.h> 49 #include <sys/dmu_objset.h> 50 #include <sys/dsl_pool.h> 51 #include <sys/zio_checksum.h> 52 #include <sys/zio_compress.h> 53 #include <sys/zfeature.h> 54 #include <sys/dmu_tx.h> 55 #include <zfeature_common.h> 56 #include <libzutil.h> 57 #include <sys/metaslab_impl.h> 58 59 static importargs_t g_importargs; 60 static char *g_pool; 61 static boolean_t g_readonly; 62 63 typedef enum { 64 ZHACK_REPAIR_OP_UNKNOWN = 0, 65 ZHACK_REPAIR_OP_CKSUM = (1 << 0), 66 ZHACK_REPAIR_OP_UNDETACH = (1 << 1) 67 } zhack_repair_op_t; 68 69 static __attribute__((noreturn)) void 70 usage(void) 71 { 72 (void) fprintf(stderr, 73 "Usage: zhack [-o tunable] [-c cachefile] [-d dir] <subcommand> " 74 "<args> ...\n" 75 "where <subcommand> <args> is one of the following:\n" 76 "\n"); 77 78 (void) fprintf(stderr, 79 " feature stat <pool>\n" 80 " print information about enabled features\n" 81 " feature enable [-r] [-d desc] <pool> <feature>\n" 82 " add a new enabled feature to the pool\n" 83 " -d <desc> sets the feature's description\n" 84 " -r set read-only compatible flag for feature\n" 85 " feature ref [-md] <pool> <feature>\n" 86 " change the refcount on the given feature\n" 87 " -d decrease instead of increase the refcount\n" 88 " -m add the feature to the label if increasing refcount\n" 89 "\n" 90 " <feature> : should be a feature guid\n" 91 "\n" 92 " label repair <device>\n" 93 " repair labels of a specified device according to options\n" 94 " which may be combined to do their functions in one call\n" 95 " -c repair corrupted label checksums\n" 96 " -u restore the label on a detached device\n" 97 "\n" 98 " <device> : path to vdev\n" 99 "\n" 100 " metaslab leak <pool>\n" 101 " apply allocation map from zdb to specified pool\n"); 102 exit(1); 103 } 104 105 106 static __attribute__((format(printf, 3, 4))) __attribute__((noreturn)) void 107 fatal(spa_t *spa, const void *tag, const char *fmt, ...) 108 { 109 va_list ap; 110 111 if (spa != NULL) { 112 spa_close(spa, tag); 113 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE); 114 } 115 116 va_start(ap, fmt); 117 (void) fputs("zhack: ", stderr); 118 (void) vfprintf(stderr, fmt, ap); 119 va_end(ap); 120 (void) fputc('\n', stderr); 121 122 exit(1); 123 } 124 125 static int 126 space_delta_cb(dmu_object_type_t bonustype, const void *data, 127 zfs_file_info_t *zoi) 128 { 129 (void) data, (void) zoi; 130 131 /* 132 * Is it a valid type of object to track? 133 */ 134 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA) 135 return (ENOENT); 136 (void) fprintf(stderr, "modifying object that needs user accounting"); 137 abort(); 138 } 139 140 /* 141 * Target is the dataset whose pool we want to open. 142 */ 143 static void 144 zhack_import(char *target, boolean_t readonly) 145 { 146 nvlist_t *config; 147 nvlist_t *props; 148 int error; 149 150 kernel_init(readonly ? SPA_MODE_READ : 151 (SPA_MODE_READ | SPA_MODE_WRITE)); 152 153 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb); 154 155 g_readonly = readonly; 156 g_importargs.can_be_active = readonly; 157 g_pool = strdup(target); 158 159 libpc_handle_t lpch = { 160 .lpc_lib_handle = NULL, 161 .lpc_ops = &libzpool_config_ops, 162 .lpc_printerr = B_TRUE 163 }; 164 error = zpool_find_config(&lpch, target, &config, &g_importargs); 165 if (error) 166 fatal(NULL, FTAG, "cannot import '%s'", target); 167 168 props = NULL; 169 if (readonly) { 170 VERIFY0(nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); 171 VERIFY0(nvlist_add_uint64(props, 172 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1)); 173 } 174 175 zfeature_checks_disable = B_TRUE; 176 error = spa_import(target, config, props, 177 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL)); 178 fnvlist_free(config); 179 zfeature_checks_disable = B_FALSE; 180 if (error == EEXIST) 181 error = 0; 182 183 if (error) 184 fatal(NULL, FTAG, "can't import '%s': %s", target, 185 strerror(error)); 186 } 187 188 static void 189 zhack_spa_open(char *target, boolean_t readonly, const void *tag, spa_t **spa) 190 { 191 int err; 192 193 zhack_import(target, readonly); 194 195 zfeature_checks_disable = B_TRUE; 196 err = spa_open(target, spa, tag); 197 zfeature_checks_disable = B_FALSE; 198 199 if (err != 0) 200 fatal(*spa, FTAG, "cannot open '%s': %s", target, 201 strerror(err)); 202 if (spa_version(*spa) < SPA_VERSION_FEATURES) { 203 fatal(*spa, FTAG, "'%s' has version %d, features not enabled", 204 target, (int)spa_version(*spa)); 205 } 206 } 207 208 static void 209 dump_obj(objset_t *os, uint64_t obj, const char *name) 210 { 211 zap_cursor_t zc; 212 zap_attribute_t *za = zap_attribute_long_alloc(); 213 214 (void) printf("%s_obj:\n", name); 215 216 for (zap_cursor_init(&zc, os, obj); 217 zap_cursor_retrieve(&zc, za) == 0; 218 zap_cursor_advance(&zc)) { 219 if (za->za_integer_length == 8) { 220 ASSERT(za->za_num_integers == 1); 221 (void) printf("\t%s = %llu\n", 222 za->za_name, (u_longlong_t)za->za_first_integer); 223 } else { 224 ASSERT(za->za_integer_length == 1); 225 char val[1024]; 226 VERIFY0(zap_lookup(os, obj, za->za_name, 227 1, sizeof (val), val)); 228 (void) printf("\t%s = %s\n", za->za_name, val); 229 } 230 } 231 zap_cursor_fini(&zc); 232 zap_attribute_free(za); 233 } 234 235 static void 236 dump_mos(spa_t *spa) 237 { 238 nvlist_t *nv = spa->spa_label_features; 239 nvpair_t *pair; 240 241 (void) printf("label config:\n"); 242 for (pair = nvlist_next_nvpair(nv, NULL); 243 pair != NULL; 244 pair = nvlist_next_nvpair(nv, pair)) { 245 (void) printf("\t%s\n", nvpair_name(pair)); 246 } 247 } 248 249 static void 250 zhack_do_feature_stat(int argc, char **argv) 251 { 252 spa_t *spa; 253 objset_t *os; 254 char *target; 255 256 argc--; 257 argv++; 258 259 if (argc < 1) { 260 (void) fprintf(stderr, "error: missing pool name\n"); 261 usage(); 262 } 263 target = argv[0]; 264 265 zhack_spa_open(target, B_TRUE, FTAG, &spa); 266 os = spa->spa_meta_objset; 267 268 dump_obj(os, spa->spa_feat_for_read_obj, "for_read"); 269 dump_obj(os, spa->spa_feat_for_write_obj, "for_write"); 270 dump_obj(os, spa->spa_feat_desc_obj, "descriptions"); 271 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 272 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg"); 273 } 274 dump_mos(spa); 275 276 spa_close(spa, FTAG); 277 } 278 279 static void 280 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx) 281 { 282 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 283 zfeature_info_t *feature = arg; 284 285 feature_enable_sync(spa, feature, tx); 286 287 spa_history_log_internal(spa, "zhack enable feature", tx, 288 "name=%s flags=%u", 289 feature->fi_guid, feature->fi_flags); 290 } 291 292 static void 293 zhack_do_feature_enable(int argc, char **argv) 294 { 295 int c; 296 char *desc, *target; 297 spa_t *spa; 298 objset_t *mos; 299 zfeature_info_t feature; 300 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE }; 301 302 /* 303 * Features are not added to the pool's label until their refcounts 304 * are incremented, so fi_mos can just be left as false for now. 305 */ 306 desc = NULL; 307 feature.fi_uname = "zhack"; 308 feature.fi_flags = 0; 309 feature.fi_depends = nodeps; 310 feature.fi_feature = SPA_FEATURE_NONE; 311 312 optind = 1; 313 while ((c = getopt(argc, argv, "+rd:")) != -1) { 314 switch (c) { 315 case 'r': 316 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT; 317 break; 318 case 'd': 319 if (desc != NULL) 320 free(desc); 321 desc = strdup(optarg); 322 break; 323 default: 324 usage(); 325 break; 326 } 327 } 328 329 if (desc == NULL) 330 desc = strdup("zhack injected"); 331 feature.fi_desc = desc; 332 333 argc -= optind; 334 argv += optind; 335 336 if (argc < 2) { 337 (void) fprintf(stderr, "error: missing feature or pool name\n"); 338 usage(); 339 } 340 target = argv[0]; 341 feature.fi_guid = argv[1]; 342 343 if (!zfeature_is_valid_guid(feature.fi_guid)) 344 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid); 345 346 zhack_spa_open(target, B_FALSE, FTAG, &spa); 347 mos = spa->spa_meta_objset; 348 349 if (zfeature_is_supported(feature.fi_guid)) 350 fatal(spa, FTAG, "'%s' is a real feature, will not enable", 351 feature.fi_guid); 352 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid)) 353 fatal(spa, FTAG, "feature already enabled: %s", 354 feature.fi_guid); 355 356 VERIFY0(dsl_sync_task(spa_name(spa), NULL, 357 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL)); 358 359 spa_close(spa, FTAG); 360 361 free(desc); 362 } 363 364 static void 365 feature_incr_sync(void *arg, dmu_tx_t *tx) 366 { 367 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 368 zfeature_info_t *feature = arg; 369 uint64_t refcount; 370 371 mutex_enter(&spa->spa_feat_stats_lock); 372 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount)); 373 feature_sync(spa, feature, refcount + 1, tx); 374 spa_history_log_internal(spa, "zhack feature incr", tx, 375 "name=%s", feature->fi_guid); 376 mutex_exit(&spa->spa_feat_stats_lock); 377 } 378 379 static void 380 feature_decr_sync(void *arg, dmu_tx_t *tx) 381 { 382 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 383 zfeature_info_t *feature = arg; 384 uint64_t refcount; 385 386 mutex_enter(&spa->spa_feat_stats_lock); 387 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount)); 388 feature_sync(spa, feature, refcount - 1, tx); 389 spa_history_log_internal(spa, "zhack feature decr", tx, 390 "name=%s", feature->fi_guid); 391 mutex_exit(&spa->spa_feat_stats_lock); 392 } 393 394 static void 395 zhack_do_feature_ref(int argc, char **argv) 396 { 397 int c; 398 char *target; 399 boolean_t decr = B_FALSE; 400 spa_t *spa; 401 objset_t *mos; 402 zfeature_info_t feature; 403 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE }; 404 405 /* 406 * fi_desc does not matter here because it was written to disk 407 * when the feature was enabled, but we need to properly set the 408 * feature for read or write based on the information we read off 409 * disk later. 410 */ 411 feature.fi_uname = "zhack"; 412 feature.fi_flags = 0; 413 feature.fi_desc = NULL; 414 feature.fi_depends = nodeps; 415 feature.fi_feature = SPA_FEATURE_NONE; 416 417 optind = 1; 418 while ((c = getopt(argc, argv, "+md")) != -1) { 419 switch (c) { 420 case 'm': 421 feature.fi_flags |= ZFEATURE_FLAG_MOS; 422 break; 423 case 'd': 424 decr = B_TRUE; 425 break; 426 default: 427 usage(); 428 break; 429 } 430 } 431 argc -= optind; 432 argv += optind; 433 434 if (argc < 2) { 435 (void) fprintf(stderr, "error: missing feature or pool name\n"); 436 usage(); 437 } 438 target = argv[0]; 439 feature.fi_guid = argv[1]; 440 441 if (!zfeature_is_valid_guid(feature.fi_guid)) 442 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid); 443 444 zhack_spa_open(target, B_FALSE, FTAG, &spa); 445 mos = spa->spa_meta_objset; 446 447 if (zfeature_is_supported(feature.fi_guid)) { 448 fatal(spa, FTAG, 449 "'%s' is a real feature, will not change refcount", 450 feature.fi_guid); 451 } 452 453 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj, 454 feature.fi_guid)) { 455 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT; 456 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj, 457 feature.fi_guid)) { 458 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT; 459 } else { 460 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid); 461 } 462 463 if (decr) { 464 uint64_t count; 465 if (feature_get_refcount_from_disk(spa, &feature, 466 &count) == 0 && count == 0) { 467 fatal(spa, FTAG, "feature refcount already 0: %s", 468 feature.fi_guid); 469 } 470 } 471 472 VERIFY0(dsl_sync_task(spa_name(spa), NULL, 473 decr ? feature_decr_sync : feature_incr_sync, &feature, 474 5, ZFS_SPACE_CHECK_NORMAL)); 475 476 spa_close(spa, FTAG); 477 } 478 479 static int 480 zhack_do_feature(int argc, char **argv) 481 { 482 char *subcommand; 483 484 argc--; 485 argv++; 486 if (argc == 0) { 487 (void) fprintf(stderr, 488 "error: no feature operation specified\n"); 489 usage(); 490 } 491 492 subcommand = argv[0]; 493 if (strcmp(subcommand, "stat") == 0) { 494 zhack_do_feature_stat(argc, argv); 495 } else if (strcmp(subcommand, "enable") == 0) { 496 zhack_do_feature_enable(argc, argv); 497 } else if (strcmp(subcommand, "ref") == 0) { 498 zhack_do_feature_ref(argc, argv); 499 } else { 500 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 501 subcommand); 502 usage(); 503 } 504 505 return (0); 506 } 507 508 static boolean_t 509 strstarts(const char *a, const char *b) 510 { 511 return (strncmp(a, b, strlen(b)) == 0); 512 } 513 514 static void 515 metaslab_force_alloc(metaslab_t *msp, uint64_t start, uint64_t size, 516 dmu_tx_t *tx) 517 { 518 ASSERT(msp->ms_disabled); 519 ASSERT(MUTEX_HELD(&msp->ms_lock)); 520 uint64_t txg = dmu_tx_get_txg(tx); 521 522 uint64_t off = start; 523 while (off < start + size) { 524 uint64_t ostart, osize; 525 boolean_t found = zfs_range_tree_find_in(msp->ms_allocatable, 526 off, start + size - off, &ostart, &osize); 527 if (!found) 528 break; 529 zfs_range_tree_remove(msp->ms_allocatable, ostart, osize); 530 531 if (zfs_range_tree_is_empty(msp->ms_allocating[txg & TXG_MASK])) 532 vdev_dirty(msp->ms_group->mg_vd, VDD_METASLAB, msp, 533 txg); 534 535 zfs_range_tree_add(msp->ms_allocating[txg & TXG_MASK], ostart, 536 osize); 537 msp->ms_allocating_total += osize; 538 off = ostart + osize; 539 } 540 } 541 542 static void 543 zhack_do_metaslab_leak(int argc, char **argv) 544 { 545 int c; 546 char *target; 547 spa_t *spa; 548 549 optind = 1; 550 boolean_t force = B_FALSE; 551 while ((c = getopt(argc, argv, "f")) != -1) { 552 switch (c) { 553 case 'f': 554 force = B_TRUE; 555 break; 556 default: 557 usage(); 558 break; 559 } 560 } 561 562 argc -= optind; 563 argv += optind; 564 565 if (argc < 1) { 566 (void) fprintf(stderr, "error: missing pool name\n"); 567 usage(); 568 } 569 target = argv[0]; 570 571 zhack_spa_open(target, B_FALSE, FTAG, &spa); 572 spa_config_enter(spa, SCL_VDEV | SCL_ALLOC, FTAG, RW_READER); 573 574 char *line = NULL; 575 size_t cap = 0; 576 577 vdev_t *vd = NULL; 578 metaslab_t *prev = NULL; 579 dmu_tx_t *tx = NULL; 580 while (getline(&line, &cap, stdin) > 0) { 581 if (strstarts(line, "\tvdev ")) { 582 uint64_t vdev_id, ms_shift; 583 if (sscanf(line, 584 "\tvdev %10"PRIu64"\t%*s metaslab shift %4"PRIu64, 585 &vdev_id, &ms_shift) == 1) { 586 VERIFY3U(sscanf(line, "\tvdev %"PRIu64 587 "\t metaslab shift %4"PRIu64, 588 &vdev_id, &ms_shift), ==, 2); 589 } 590 vd = vdev_lookup_top(spa, vdev_id); 591 if (vd == NULL) { 592 fprintf(stderr, "error: no such vdev with " 593 "id %"PRIu64"\n", vdev_id); 594 break; 595 } 596 if (tx) { 597 dmu_tx_commit(tx); 598 mutex_exit(&prev->ms_lock); 599 metaslab_enable(prev, B_FALSE, B_FALSE); 600 tx = NULL; 601 prev = NULL; 602 } 603 if (vd->vdev_ms_shift != ms_shift) { 604 fprintf(stderr, "error: ms_shift mismatch: %" 605 PRIu64" != %"PRIu64"\n", vd->vdev_ms_shift, 606 ms_shift); 607 break; 608 } 609 } else if (strstarts(line, "\tmetaslabs ")) { 610 uint64_t ms_count; 611 VERIFY3U(sscanf(line, "\tmetaslabs %"PRIu64, &ms_count), 612 ==, 1); 613 ASSERT(vd); 614 if (!force && vd->vdev_ms_count != ms_count) { 615 fprintf(stderr, "error: ms_count mismatch: %" 616 PRIu64" != %"PRIu64"\n", vd->vdev_ms_count, 617 ms_count); 618 break; 619 } 620 } else if (strstarts(line, "ALLOC:")) { 621 uint64_t start, size; 622 VERIFY3U(sscanf(line, "ALLOC: %"PRIu64" %"PRIu64"\n", 623 &start, &size), ==, 2); 624 625 ASSERT(vd); 626 metaslab_t *cur = 627 vd->vdev_ms[start >> vd->vdev_ms_shift]; 628 if (prev != cur) { 629 if (prev) { 630 dmu_tx_commit(tx); 631 mutex_exit(&prev->ms_lock); 632 metaslab_enable(prev, B_FALSE, B_FALSE); 633 } 634 ASSERT(cur); 635 metaslab_disable(cur); 636 mutex_enter(&cur->ms_lock); 637 metaslab_load(cur); 638 prev = cur; 639 tx = dmu_tx_create_dd( 640 spa_get_dsl(vd->vdev_spa)->dp_root_dir); 641 dmu_tx_assign(tx, DMU_TX_WAIT); 642 } 643 644 metaslab_force_alloc(cur, start, size, tx); 645 } else { 646 continue; 647 } 648 } 649 if (tx) { 650 dmu_tx_commit(tx); 651 mutex_exit(&prev->ms_lock); 652 metaslab_enable(prev, B_FALSE, B_FALSE); 653 tx = NULL; 654 prev = NULL; 655 } 656 if (line) 657 free(line); 658 659 spa_config_exit(spa, SCL_VDEV | SCL_ALLOC, FTAG); 660 spa_close(spa, FTAG); 661 } 662 663 static int 664 zhack_do_metaslab(int argc, char **argv) 665 { 666 char *subcommand; 667 668 argc--; 669 argv++; 670 if (argc == 0) { 671 (void) fprintf(stderr, 672 "error: no metaslab operation specified\n"); 673 usage(); 674 } 675 676 subcommand = argv[0]; 677 if (strcmp(subcommand, "leak") == 0) { 678 zhack_do_metaslab_leak(argc, argv); 679 } else { 680 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 681 subcommand); 682 usage(); 683 } 684 685 return (0); 686 } 687 688 #define ASHIFT_UBERBLOCK_SHIFT(ashift) \ 689 MIN(MAX(ashift, UBERBLOCK_SHIFT), \ 690 MAX_UBERBLOCK_SHIFT) 691 #define ASHIFT_UBERBLOCK_SIZE(ashift) \ 692 (1ULL << ASHIFT_UBERBLOCK_SHIFT(ashift)) 693 694 #define REPAIR_LABEL_STATUS_CKSUM (1 << 0) 695 #define REPAIR_LABEL_STATUS_UB (1 << 1) 696 697 static int 698 zhack_repair_read_label(const int fd, vdev_label_t *vl, 699 const uint64_t label_offset, const int l) 700 { 701 const int err = pread64(fd, vl, sizeof (vdev_label_t), label_offset); 702 703 if (err == -1) { 704 (void) fprintf(stderr, 705 "error: cannot read label %d: %s\n", 706 l, strerror(errno)); 707 return (err); 708 } else if (err != sizeof (vdev_label_t)) { 709 (void) fprintf(stderr, 710 "error: bad label %d read size\n", l); 711 return (err); 712 } 713 714 return (0); 715 } 716 717 static void 718 zhack_repair_calc_cksum(const int byteswap, void *data, const uint64_t offset, 719 const uint64_t abdsize, zio_eck_t *eck, zio_cksum_t *cksum) 720 { 721 zio_cksum_t verifier; 722 zio_cksum_t current_cksum; 723 zio_checksum_info_t *ci; 724 abd_t *abd; 725 726 ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0); 727 728 if (byteswap) 729 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t)); 730 731 current_cksum = eck->zec_cksum; 732 eck->zec_cksum = verifier; 733 734 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL]; 735 abd = abd_get_from_buf(data, abdsize); 736 ci->ci_func[byteswap](abd, abdsize, NULL, cksum); 737 abd_free(abd); 738 739 eck->zec_cksum = current_cksum; 740 } 741 742 static int 743 zhack_repair_check_label(uberblock_t *ub, const int l, const char **cfg_keys, 744 const size_t cfg_keys_len, nvlist_t *cfg, nvlist_t *vdev_tree_cfg, 745 uint64_t *ashift) 746 { 747 int err; 748 749 if (ub->ub_txg != 0) { 750 (void) fprintf(stderr, 751 "error: label %d: UB TXG of 0 expected, but got %" 752 PRIu64 "\n", 753 l, ub->ub_txg); 754 (void) fprintf(stderr, "It would appear the device was not " 755 "properly removed.\n"); 756 return (1); 757 } 758 759 for (int i = 0; i < cfg_keys_len; i++) { 760 uint64_t val; 761 err = nvlist_lookup_uint64(cfg, cfg_keys[i], &val); 762 if (err) { 763 (void) fprintf(stderr, 764 "error: label %d, %d: " 765 "cannot find nvlist key %s\n", 766 l, i, cfg_keys[i]); 767 return (err); 768 } 769 } 770 771 err = nvlist_lookup_nvlist(cfg, 772 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree_cfg); 773 if (err) { 774 (void) fprintf(stderr, 775 "error: label %d: cannot find nvlist key %s\n", 776 l, ZPOOL_CONFIG_VDEV_TREE); 777 return (err); 778 } 779 780 err = nvlist_lookup_uint64(vdev_tree_cfg, 781 ZPOOL_CONFIG_ASHIFT, ashift); 782 if (err) { 783 (void) fprintf(stderr, 784 "error: label %d: cannot find nvlist key %s\n", 785 l, ZPOOL_CONFIG_ASHIFT); 786 return (err); 787 } 788 789 if (*ashift == 0) { 790 (void) fprintf(stderr, 791 "error: label %d: nvlist key %s is zero\n", 792 l, ZPOOL_CONFIG_ASHIFT); 793 return (err); 794 } 795 796 return (0); 797 } 798 799 static int 800 zhack_repair_undetach(uberblock_t *ub, nvlist_t *cfg, const int l) 801 { 802 /* 803 * Uberblock root block pointer has valid birth TXG. 804 * Copying it to the label NVlist 805 */ 806 if (BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp) != 0) { 807 const uint64_t txg = BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp); 808 ub->ub_txg = txg; 809 810 if (nvlist_remove_all(cfg, ZPOOL_CONFIG_CREATE_TXG) != 0) { 811 (void) fprintf(stderr, 812 "error: label %d: " 813 "Failed to remove pool creation TXG\n", 814 l); 815 return (1); 816 } 817 818 if (nvlist_remove_all(cfg, ZPOOL_CONFIG_POOL_TXG) != 0) { 819 (void) fprintf(stderr, 820 "error: label %d: Failed to remove pool TXG to " 821 "be replaced.\n", 822 l); 823 return (1); 824 } 825 826 if (nvlist_add_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, txg) != 0) { 827 (void) fprintf(stderr, 828 "error: label %d: " 829 "Failed to add pool TXG of %" PRIu64 "\n", 830 l, txg); 831 return (1); 832 } 833 } 834 835 return (0); 836 } 837 838 static boolean_t 839 zhack_repair_write_label(const int l, const int fd, const int byteswap, 840 void *data, zio_eck_t *eck, const uint64_t offset, const uint64_t abdsize) 841 { 842 zio_cksum_t actual_cksum; 843 zhack_repair_calc_cksum(byteswap, data, offset, abdsize, eck, 844 &actual_cksum); 845 zio_cksum_t expected_cksum = eck->zec_cksum; 846 ssize_t err; 847 848 if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) 849 return (B_FALSE); 850 851 eck->zec_cksum = actual_cksum; 852 853 err = pwrite64(fd, data, abdsize, offset); 854 if (err == -1) { 855 (void) fprintf(stderr, "error: cannot write label %d: %s\n", 856 l, strerror(errno)); 857 return (B_FALSE); 858 } else if (err != abdsize) { 859 (void) fprintf(stderr, "error: bad write size label %d\n", l); 860 return (B_FALSE); 861 } else { 862 (void) fprintf(stderr, 863 "label %d: wrote %" PRIu64 " bytes at offset %" PRIu64 "\n", 864 l, abdsize, offset); 865 } 866 867 return (B_TRUE); 868 } 869 870 static void 871 zhack_repair_write_uberblock(vdev_label_t *vl, const int l, 872 const uint64_t ashift, const int fd, const int byteswap, 873 const uint64_t label_offset, uint32_t *labels_repaired) 874 { 875 void *ub_data = 876 (char *)vl + offsetof(vdev_label_t, vl_uberblock); 877 zio_eck_t *ub_eck = 878 (zio_eck_t *) 879 ((char *)(ub_data) + (ASHIFT_UBERBLOCK_SIZE(ashift))) - 1; 880 881 if (ub_eck->zec_magic != 0) { 882 (void) fprintf(stderr, 883 "error: label %d: " 884 "Expected Uberblock checksum magic number to " 885 "be 0, but got %" PRIu64 "\n", 886 l, ub_eck->zec_magic); 887 (void) fprintf(stderr, "It would appear there's already " 888 "a checksum for the uberblock.\n"); 889 return; 890 } 891 892 893 ub_eck->zec_magic = byteswap ? BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC; 894 895 if (zhack_repair_write_label(l, fd, byteswap, 896 ub_data, ub_eck, 897 label_offset + offsetof(vdev_label_t, vl_uberblock), 898 ASHIFT_UBERBLOCK_SIZE(ashift))) 899 labels_repaired[l] |= REPAIR_LABEL_STATUS_UB; 900 } 901 902 static void 903 zhack_repair_print_cksum(FILE *stream, const zio_cksum_t *cksum) 904 { 905 (void) fprintf(stream, 906 "%016llx:%016llx:%016llx:%016llx", 907 (u_longlong_t)cksum->zc_word[0], 908 (u_longlong_t)cksum->zc_word[1], 909 (u_longlong_t)cksum->zc_word[2], 910 (u_longlong_t)cksum->zc_word[3]); 911 } 912 913 static int 914 zhack_repair_test_cksum(const int byteswap, void *vdev_data, 915 zio_eck_t *vdev_eck, const uint64_t vdev_phys_offset, const int l) 916 { 917 const zio_cksum_t expected_cksum = vdev_eck->zec_cksum; 918 zio_cksum_t actual_cksum; 919 zhack_repair_calc_cksum(byteswap, vdev_data, vdev_phys_offset, 920 VDEV_PHYS_SIZE, vdev_eck, &actual_cksum); 921 const uint64_t expected_magic = byteswap ? 922 BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC; 923 const uint64_t actual_magic = vdev_eck->zec_magic; 924 int err = 0; 925 if (actual_magic != expected_magic) { 926 (void) fprintf(stderr, "error: label %d: " 927 "Expected " 928 "the nvlist checksum magic number to not be %" 929 PRIu64 " not %" PRIu64 "\n", 930 l, expected_magic, actual_magic); 931 err = ECKSUM; 932 } 933 if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) { 934 (void) fprintf(stderr, "error: label %d: " 935 "Expected the nvlist checksum to be ", l); 936 (void) zhack_repair_print_cksum(stderr, 937 &expected_cksum); 938 (void) fprintf(stderr, " not "); 939 zhack_repair_print_cksum(stderr, &actual_cksum); 940 (void) fprintf(stderr, "\n"); 941 err = ECKSUM; 942 } 943 return (err); 944 } 945 946 static void 947 zhack_repair_one_label(const zhack_repair_op_t op, const int fd, 948 vdev_label_t *vl, const uint64_t label_offset, const int l, 949 uint32_t *labels_repaired) 950 { 951 ssize_t err; 952 uberblock_t *ub = (uberblock_t *)vl->vl_uberblock; 953 void *vdev_data = 954 (char *)vl + offsetof(vdev_label_t, vl_vdev_phys); 955 zio_eck_t *vdev_eck = 956 (zio_eck_t *)((char *)(vdev_data) + VDEV_PHYS_SIZE) - 1; 957 const uint64_t vdev_phys_offset = 958 label_offset + offsetof(vdev_label_t, vl_vdev_phys); 959 const char *cfg_keys[] = { ZPOOL_CONFIG_VERSION, 960 ZPOOL_CONFIG_POOL_STATE, ZPOOL_CONFIG_GUID }; 961 nvlist_t *cfg; 962 nvlist_t *vdev_tree_cfg = NULL; 963 uint64_t ashift; 964 int byteswap; 965 966 err = zhack_repair_read_label(fd, vl, label_offset, l); 967 if (err) 968 return; 969 970 if (vdev_eck->zec_magic == 0) { 971 (void) fprintf(stderr, "error: label %d: " 972 "Expected the nvlist checksum magic number to not be zero" 973 "\n", 974 l); 975 (void) fprintf(stderr, "There should already be a checksum " 976 "for the label.\n"); 977 return; 978 } 979 980 byteswap = 981 (vdev_eck->zec_magic == BSWAP_64((uint64_t)ZEC_MAGIC)); 982 983 if (byteswap) { 984 byteswap_uint64_array(&vdev_eck->zec_cksum, 985 sizeof (zio_cksum_t)); 986 vdev_eck->zec_magic = BSWAP_64(vdev_eck->zec_magic); 987 } 988 989 if ((op & ZHACK_REPAIR_OP_CKSUM) == 0 && 990 zhack_repair_test_cksum(byteswap, vdev_data, vdev_eck, 991 vdev_phys_offset, l) != 0) { 992 (void) fprintf(stderr, "It would appear checksums are " 993 "corrupted. Try zhack repair label -c <device>\n"); 994 return; 995 } 996 997 err = nvlist_unpack(vl->vl_vdev_phys.vp_nvlist, 998 VDEV_PHYS_SIZE - sizeof (zio_eck_t), &cfg, 0); 999 if (err) { 1000 (void) fprintf(stderr, 1001 "error: cannot unpack nvlist label %d\n", l); 1002 return; 1003 } 1004 1005 err = zhack_repair_check_label(ub, 1006 l, cfg_keys, ARRAY_SIZE(cfg_keys), cfg, vdev_tree_cfg, &ashift); 1007 if (err) 1008 return; 1009 1010 if ((op & ZHACK_REPAIR_OP_UNDETACH) != 0) { 1011 char *buf; 1012 size_t buflen; 1013 1014 err = zhack_repair_undetach(ub, cfg, l); 1015 if (err) 1016 return; 1017 1018 buf = vl->vl_vdev_phys.vp_nvlist; 1019 buflen = VDEV_PHYS_SIZE - sizeof (zio_eck_t); 1020 if (nvlist_pack(cfg, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) { 1021 (void) fprintf(stderr, 1022 "error: label %d: Failed to pack nvlist\n", l); 1023 return; 1024 } 1025 1026 zhack_repair_write_uberblock(vl, 1027 l, ashift, fd, byteswap, label_offset, labels_repaired); 1028 } 1029 1030 if (zhack_repair_write_label(l, fd, byteswap, vdev_data, vdev_eck, 1031 vdev_phys_offset, VDEV_PHYS_SIZE)) 1032 labels_repaired[l] |= REPAIR_LABEL_STATUS_CKSUM; 1033 1034 fsync(fd); 1035 } 1036 1037 static const char * 1038 zhack_repair_label_status(const uint32_t label_status, 1039 const uint32_t to_check) 1040 { 1041 return ((label_status & to_check) != 0 ? "repaired" : "skipped"); 1042 } 1043 1044 static int 1045 zhack_label_repair(const zhack_repair_op_t op, const int argc, char **argv) 1046 { 1047 uint32_t labels_repaired[VDEV_LABELS] = {0}; 1048 vdev_label_t labels[VDEV_LABELS] = {{{0}}}; 1049 struct stat64 st; 1050 int fd; 1051 off_t filesize; 1052 uint32_t repaired = 0; 1053 1054 abd_init(); 1055 1056 if (argc < 1) { 1057 (void) fprintf(stderr, "error: missing device\n"); 1058 usage(); 1059 } 1060 1061 if ((fd = open(argv[0], O_RDWR)) == -1) 1062 fatal(NULL, FTAG, "cannot open '%s': %s", argv[0], 1063 strerror(errno)); 1064 1065 if (fstat64_blk(fd, &st) != 0) 1066 fatal(NULL, FTAG, "cannot stat '%s': %s", argv[0], 1067 strerror(errno)); 1068 1069 filesize = st.st_size; 1070 (void) fprintf(stderr, "Calculated filesize to be %jd\n", 1071 (intmax_t)filesize); 1072 1073 if (filesize % sizeof (vdev_label_t) != 0) 1074 filesize = 1075 (filesize / sizeof (vdev_label_t)) * sizeof (vdev_label_t); 1076 1077 for (int l = 0; l < VDEV_LABELS; l++) { 1078 zhack_repair_one_label(op, fd, &labels[l], 1079 vdev_label_offset(filesize, l, 0), l, labels_repaired); 1080 } 1081 1082 close(fd); 1083 1084 abd_fini(); 1085 1086 for (int l = 0; l < VDEV_LABELS; l++) { 1087 const uint32_t lr = labels_repaired[l]; 1088 (void) printf("label %d: ", l); 1089 (void) printf("uberblock: %s ", 1090 zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_UB)); 1091 (void) printf("checksum: %s\n", 1092 zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_CKSUM)); 1093 repaired |= lr; 1094 } 1095 1096 if (repaired > 0) 1097 return (0); 1098 1099 return (1); 1100 } 1101 1102 static int 1103 zhack_do_label_repair(int argc, char **argv) 1104 { 1105 zhack_repair_op_t op = ZHACK_REPAIR_OP_UNKNOWN; 1106 int c; 1107 1108 optind = 1; 1109 while ((c = getopt(argc, argv, "+cu")) != -1) { 1110 switch (c) { 1111 case 'c': 1112 op |= ZHACK_REPAIR_OP_CKSUM; 1113 break; 1114 case 'u': 1115 op |= ZHACK_REPAIR_OP_UNDETACH; 1116 break; 1117 default: 1118 usage(); 1119 break; 1120 } 1121 } 1122 1123 argc -= optind; 1124 argv += optind; 1125 1126 if (op == ZHACK_REPAIR_OP_UNKNOWN) 1127 op = ZHACK_REPAIR_OP_CKSUM; 1128 1129 return (zhack_label_repair(op, argc, argv)); 1130 } 1131 1132 static int 1133 zhack_do_label(int argc, char **argv) 1134 { 1135 char *subcommand; 1136 int err; 1137 1138 argc--; 1139 argv++; 1140 if (argc == 0) { 1141 (void) fprintf(stderr, 1142 "error: no label operation specified\n"); 1143 usage(); 1144 } 1145 1146 subcommand = argv[0]; 1147 if (strcmp(subcommand, "repair") == 0) { 1148 err = zhack_do_label_repair(argc, argv); 1149 } else { 1150 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 1151 subcommand); 1152 usage(); 1153 } 1154 1155 return (err); 1156 } 1157 1158 #define MAX_NUM_PATHS 1024 1159 1160 int 1161 main(int argc, char **argv) 1162 { 1163 char *path[MAX_NUM_PATHS]; 1164 const char *subcommand; 1165 int rv = 0; 1166 int c; 1167 1168 g_importargs.path = path; 1169 1170 dprintf_setup(&argc, argv); 1171 zfs_prop_init(); 1172 1173 while ((c = getopt(argc, argv, "+c:d:o:")) != -1) { 1174 switch (c) { 1175 case 'c': 1176 g_importargs.cachefile = optarg; 1177 break; 1178 case 'd': 1179 assert(g_importargs.paths < MAX_NUM_PATHS); 1180 g_importargs.path[g_importargs.paths++] = optarg; 1181 break; 1182 case 'o': 1183 if (handle_tunable_option(optarg, B_FALSE) != 0) 1184 exit(1); 1185 break; 1186 default: 1187 usage(); 1188 break; 1189 } 1190 } 1191 1192 argc -= optind; 1193 argv += optind; 1194 optind = 1; 1195 1196 if (argc == 0) { 1197 (void) fprintf(stderr, "error: no command specified\n"); 1198 usage(); 1199 } 1200 1201 subcommand = argv[0]; 1202 1203 if (strcmp(subcommand, "feature") == 0) { 1204 rv = zhack_do_feature(argc, argv); 1205 } else if (strcmp(subcommand, "label") == 0) { 1206 return (zhack_do_label(argc, argv)); 1207 } else if (strcmp(subcommand, "metaslab") == 0) { 1208 rv = zhack_do_metaslab(argc, argv); 1209 } else { 1210 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 1211 subcommand); 1212 usage(); 1213 } 1214 1215 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) { 1216 fatal(NULL, FTAG, "pool export failed; " 1217 "changes may not be committed to disk\n"); 1218 } 1219 1220 kernel_fini(); 1221 1222 return (rv); 1223 } 1224