1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 24 * Copyright (c) 2013 Steven Hartland. All rights reserved. 25 */ 26 27 /* 28 * zhack is a debugging tool that can write changes to ZFS pool using libzpool 29 * for testing purposes. Altering pools with zhack is unsupported and may 30 * result in corrupted pools. 31 */ 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <ctype.h> 36 #include <sys/stat.h> 37 #include <sys/zfs_context.h> 38 #include <sys/spa.h> 39 #include <sys/spa_impl.h> 40 #include <sys/dmu.h> 41 #include <sys/zap.h> 42 #include <sys/zfs_znode.h> 43 #include <sys/dsl_synctask.h> 44 #include <sys/vdev.h> 45 #include <sys/vdev_impl.h> 46 #include <sys/fs/zfs.h> 47 #include <sys/dmu_objset.h> 48 #include <sys/dsl_pool.h> 49 #include <sys/zio_checksum.h> 50 #include <sys/zio_compress.h> 51 #include <sys/zfeature.h> 52 #include <sys/dmu_tx.h> 53 #include <zfeature_common.h> 54 #include <libzutil.h> 55 56 static importargs_t g_importargs; 57 static char *g_pool; 58 static boolean_t g_readonly; 59 60 static __attribute__((noreturn)) void 61 usage(void) 62 { 63 (void) fprintf(stderr, 64 "Usage: zhack [-c cachefile] [-d dir] <subcommand> <args> ...\n" 65 "where <subcommand> <args> is one of the following:\n" 66 "\n"); 67 68 (void) fprintf(stderr, 69 " feature stat <pool>\n" 70 " print information about enabled features\n" 71 " feature enable [-r] [-d desc] <pool> <feature>\n" 72 " add a new enabled feature to the pool\n" 73 " -d <desc> sets the feature's description\n" 74 " -r set read-only compatible flag for feature\n" 75 " feature ref [-md] <pool> <feature>\n" 76 " change the refcount on the given feature\n" 77 " -d decrease instead of increase the refcount\n" 78 " -m add the feature to the label if increasing refcount\n" 79 "\n" 80 " <feature> : should be a feature guid\n" 81 "\n" 82 " label repair <device>\n" 83 " repair corrupted label checksums\n" 84 "\n" 85 " <device> : path to vdev\n"); 86 exit(1); 87 } 88 89 90 static __attribute__((format(printf, 3, 4))) __attribute__((noreturn)) void 91 fatal(spa_t *spa, const void *tag, const char *fmt, ...) 92 { 93 va_list ap; 94 95 if (spa != NULL) { 96 spa_close(spa, tag); 97 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE); 98 } 99 100 va_start(ap, fmt); 101 (void) fputs("zhack: ", stderr); 102 (void) vfprintf(stderr, fmt, ap); 103 va_end(ap); 104 (void) fputc('\n', stderr); 105 106 exit(1); 107 } 108 109 static int 110 space_delta_cb(dmu_object_type_t bonustype, const void *data, 111 zfs_file_info_t *zoi) 112 { 113 (void) data, (void) zoi; 114 115 /* 116 * Is it a valid type of object to track? 117 */ 118 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA) 119 return (ENOENT); 120 (void) fprintf(stderr, "modifying object that needs user accounting"); 121 abort(); 122 } 123 124 /* 125 * Target is the dataset whose pool we want to open. 126 */ 127 static void 128 zhack_import(char *target, boolean_t readonly) 129 { 130 nvlist_t *config; 131 nvlist_t *props; 132 int error; 133 134 kernel_init(readonly ? SPA_MODE_READ : 135 (SPA_MODE_READ | SPA_MODE_WRITE)); 136 137 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb); 138 139 g_readonly = readonly; 140 g_importargs.can_be_active = readonly; 141 g_pool = strdup(target); 142 143 libpc_handle_t lpch = { 144 .lpc_lib_handle = NULL, 145 .lpc_ops = &libzpool_config_ops, 146 .lpc_printerr = B_TRUE 147 }; 148 error = zpool_find_config(&lpch, target, &config, &g_importargs); 149 if (error) 150 fatal(NULL, FTAG, "cannot import '%s'", target); 151 152 props = NULL; 153 if (readonly) { 154 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0); 155 VERIFY(nvlist_add_uint64(props, 156 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0); 157 } 158 159 zfeature_checks_disable = B_TRUE; 160 error = spa_import(target, config, props, 161 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL)); 162 fnvlist_free(config); 163 zfeature_checks_disable = B_FALSE; 164 if (error == EEXIST) 165 error = 0; 166 167 if (error) 168 fatal(NULL, FTAG, "can't import '%s': %s", target, 169 strerror(error)); 170 } 171 172 static void 173 zhack_spa_open(char *target, boolean_t readonly, const void *tag, spa_t **spa) 174 { 175 int err; 176 177 zhack_import(target, readonly); 178 179 zfeature_checks_disable = B_TRUE; 180 err = spa_open(target, spa, tag); 181 zfeature_checks_disable = B_FALSE; 182 183 if (err != 0) 184 fatal(*spa, FTAG, "cannot open '%s': %s", target, 185 strerror(err)); 186 if (spa_version(*spa) < SPA_VERSION_FEATURES) { 187 fatal(*spa, FTAG, "'%s' has version %d, features not enabled", 188 target, (int)spa_version(*spa)); 189 } 190 } 191 192 static void 193 dump_obj(objset_t *os, uint64_t obj, const char *name) 194 { 195 zap_cursor_t zc; 196 zap_attribute_t za; 197 198 (void) printf("%s_obj:\n", name); 199 200 for (zap_cursor_init(&zc, os, obj); 201 zap_cursor_retrieve(&zc, &za) == 0; 202 zap_cursor_advance(&zc)) { 203 if (za.za_integer_length == 8) { 204 ASSERT(za.za_num_integers == 1); 205 (void) printf("\t%s = %llu\n", 206 za.za_name, (u_longlong_t)za.za_first_integer); 207 } else { 208 ASSERT(za.za_integer_length == 1); 209 char val[1024]; 210 VERIFY(zap_lookup(os, obj, za.za_name, 211 1, sizeof (val), val) == 0); 212 (void) printf("\t%s = %s\n", za.za_name, val); 213 } 214 } 215 zap_cursor_fini(&zc); 216 } 217 218 static void 219 dump_mos(spa_t *spa) 220 { 221 nvlist_t *nv = spa->spa_label_features; 222 nvpair_t *pair; 223 224 (void) printf("label config:\n"); 225 for (pair = nvlist_next_nvpair(nv, NULL); 226 pair != NULL; 227 pair = nvlist_next_nvpair(nv, pair)) { 228 (void) printf("\t%s\n", nvpair_name(pair)); 229 } 230 } 231 232 static void 233 zhack_do_feature_stat(int argc, char **argv) 234 { 235 spa_t *spa; 236 objset_t *os; 237 char *target; 238 239 argc--; 240 argv++; 241 242 if (argc < 1) { 243 (void) fprintf(stderr, "error: missing pool name\n"); 244 usage(); 245 } 246 target = argv[0]; 247 248 zhack_spa_open(target, B_TRUE, FTAG, &spa); 249 os = spa->spa_meta_objset; 250 251 dump_obj(os, spa->spa_feat_for_read_obj, "for_read"); 252 dump_obj(os, spa->spa_feat_for_write_obj, "for_write"); 253 dump_obj(os, spa->spa_feat_desc_obj, "descriptions"); 254 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { 255 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg"); 256 } 257 dump_mos(spa); 258 259 spa_close(spa, FTAG); 260 } 261 262 static void 263 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx) 264 { 265 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 266 zfeature_info_t *feature = arg; 267 268 feature_enable_sync(spa, feature, tx); 269 270 spa_history_log_internal(spa, "zhack enable feature", tx, 271 "name=%s flags=%u", 272 feature->fi_guid, feature->fi_flags); 273 } 274 275 static void 276 zhack_do_feature_enable(int argc, char **argv) 277 { 278 int c; 279 char *desc, *target; 280 spa_t *spa; 281 objset_t *mos; 282 zfeature_info_t feature; 283 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE }; 284 285 /* 286 * Features are not added to the pool's label until their refcounts 287 * are incremented, so fi_mos can just be left as false for now. 288 */ 289 desc = NULL; 290 feature.fi_uname = "zhack"; 291 feature.fi_flags = 0; 292 feature.fi_depends = nodeps; 293 feature.fi_feature = SPA_FEATURE_NONE; 294 295 optind = 1; 296 while ((c = getopt(argc, argv, "+rd:")) != -1) { 297 switch (c) { 298 case 'r': 299 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT; 300 break; 301 case 'd': 302 if (desc != NULL) 303 free(desc); 304 desc = strdup(optarg); 305 break; 306 default: 307 usage(); 308 break; 309 } 310 } 311 312 if (desc == NULL) 313 desc = strdup("zhack injected"); 314 feature.fi_desc = desc; 315 316 argc -= optind; 317 argv += optind; 318 319 if (argc < 2) { 320 (void) fprintf(stderr, "error: missing feature or pool name\n"); 321 usage(); 322 } 323 target = argv[0]; 324 feature.fi_guid = argv[1]; 325 326 if (!zfeature_is_valid_guid(feature.fi_guid)) 327 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid); 328 329 zhack_spa_open(target, B_FALSE, FTAG, &spa); 330 mos = spa->spa_meta_objset; 331 332 if (zfeature_is_supported(feature.fi_guid)) 333 fatal(spa, FTAG, "'%s' is a real feature, will not enable", 334 feature.fi_guid); 335 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid)) 336 fatal(spa, FTAG, "feature already enabled: %s", 337 feature.fi_guid); 338 339 VERIFY0(dsl_sync_task(spa_name(spa), NULL, 340 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL)); 341 342 spa_close(spa, FTAG); 343 344 free(desc); 345 } 346 347 static void 348 feature_incr_sync(void *arg, dmu_tx_t *tx) 349 { 350 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 351 zfeature_info_t *feature = arg; 352 uint64_t refcount; 353 354 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount)); 355 feature_sync(spa, feature, refcount + 1, tx); 356 spa_history_log_internal(spa, "zhack feature incr", tx, 357 "name=%s", feature->fi_guid); 358 } 359 360 static void 361 feature_decr_sync(void *arg, dmu_tx_t *tx) 362 { 363 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 364 zfeature_info_t *feature = arg; 365 uint64_t refcount; 366 367 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount)); 368 feature_sync(spa, feature, refcount - 1, tx); 369 spa_history_log_internal(spa, "zhack feature decr", tx, 370 "name=%s", feature->fi_guid); 371 } 372 373 static void 374 zhack_do_feature_ref(int argc, char **argv) 375 { 376 int c; 377 char *target; 378 boolean_t decr = B_FALSE; 379 spa_t *spa; 380 objset_t *mos; 381 zfeature_info_t feature; 382 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE }; 383 384 /* 385 * fi_desc does not matter here because it was written to disk 386 * when the feature was enabled, but we need to properly set the 387 * feature for read or write based on the information we read off 388 * disk later. 389 */ 390 feature.fi_uname = "zhack"; 391 feature.fi_flags = 0; 392 feature.fi_desc = NULL; 393 feature.fi_depends = nodeps; 394 feature.fi_feature = SPA_FEATURE_NONE; 395 396 optind = 1; 397 while ((c = getopt(argc, argv, "+md")) != -1) { 398 switch (c) { 399 case 'm': 400 feature.fi_flags |= ZFEATURE_FLAG_MOS; 401 break; 402 case 'd': 403 decr = B_TRUE; 404 break; 405 default: 406 usage(); 407 break; 408 } 409 } 410 argc -= optind; 411 argv += optind; 412 413 if (argc < 2) { 414 (void) fprintf(stderr, "error: missing feature or pool name\n"); 415 usage(); 416 } 417 target = argv[0]; 418 feature.fi_guid = argv[1]; 419 420 if (!zfeature_is_valid_guid(feature.fi_guid)) 421 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid); 422 423 zhack_spa_open(target, B_FALSE, FTAG, &spa); 424 mos = spa->spa_meta_objset; 425 426 if (zfeature_is_supported(feature.fi_guid)) { 427 fatal(spa, FTAG, 428 "'%s' is a real feature, will not change refcount", 429 feature.fi_guid); 430 } 431 432 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj, 433 feature.fi_guid)) { 434 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT; 435 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj, 436 feature.fi_guid)) { 437 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT; 438 } else { 439 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid); 440 } 441 442 if (decr) { 443 uint64_t count; 444 if (feature_get_refcount_from_disk(spa, &feature, 445 &count) == 0 && count == 0) { 446 fatal(spa, FTAG, "feature refcount already 0: %s", 447 feature.fi_guid); 448 } 449 } 450 451 VERIFY0(dsl_sync_task(spa_name(spa), NULL, 452 decr ? feature_decr_sync : feature_incr_sync, &feature, 453 5, ZFS_SPACE_CHECK_NORMAL)); 454 455 spa_close(spa, FTAG); 456 } 457 458 static int 459 zhack_do_feature(int argc, char **argv) 460 { 461 char *subcommand; 462 463 argc--; 464 argv++; 465 if (argc == 0) { 466 (void) fprintf(stderr, 467 "error: no feature operation specified\n"); 468 usage(); 469 } 470 471 subcommand = argv[0]; 472 if (strcmp(subcommand, "stat") == 0) { 473 zhack_do_feature_stat(argc, argv); 474 } else if (strcmp(subcommand, "enable") == 0) { 475 zhack_do_feature_enable(argc, argv); 476 } else if (strcmp(subcommand, "ref") == 0) { 477 zhack_do_feature_ref(argc, argv); 478 } else { 479 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 480 subcommand); 481 usage(); 482 } 483 484 return (0); 485 } 486 487 static int 488 zhack_repair_label_cksum(int argc, char **argv) 489 { 490 zio_checksum_info_t *ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL]; 491 const char *cfg_keys[] = { ZPOOL_CONFIG_VERSION, 492 ZPOOL_CONFIG_POOL_STATE, ZPOOL_CONFIG_GUID }; 493 boolean_t labels_repaired[VDEV_LABELS] = {0}; 494 boolean_t repaired = B_FALSE; 495 vdev_label_t labels[VDEV_LABELS] = {{{0}}}; 496 struct stat st; 497 int fd; 498 499 abd_init(); 500 501 argc -= 1; 502 argv += 1; 503 504 if (argc < 1) { 505 (void) fprintf(stderr, "error: missing device\n"); 506 usage(); 507 } 508 509 if ((fd = open(argv[0], O_RDWR)) == -1) 510 fatal(NULL, FTAG, "cannot open '%s': %s", argv[0], 511 strerror(errno)); 512 513 if (stat(argv[0], &st) != 0) 514 fatal(NULL, FTAG, "cannot stat '%s': %s", argv[0], 515 strerror(errno)); 516 517 for (int l = 0; l < VDEV_LABELS; l++) { 518 uint64_t label_offset, offset; 519 zio_cksum_t expected_cksum; 520 zio_cksum_t actual_cksum; 521 zio_cksum_t verifier; 522 zio_eck_t *eck; 523 nvlist_t *cfg; 524 int byteswap; 525 uint64_t val; 526 ssize_t err; 527 528 vdev_label_t *vl = &labels[l]; 529 530 label_offset = vdev_label_offset(st.st_size, l, 0); 531 err = pread64(fd, vl, sizeof (vdev_label_t), label_offset); 532 if (err == -1) { 533 (void) fprintf(stderr, "error: cannot read " 534 "label %d: %s\n", l, strerror(errno)); 535 continue; 536 } else if (err != sizeof (vdev_label_t)) { 537 (void) fprintf(stderr, "error: bad label %d read size " 538 "\n", l); 539 continue; 540 } 541 542 err = nvlist_unpack(vl->vl_vdev_phys.vp_nvlist, 543 VDEV_PHYS_SIZE - sizeof (zio_eck_t), &cfg, 0); 544 if (err) { 545 (void) fprintf(stderr, "error: cannot unpack nvlist " 546 "label %d\n", l); 547 continue; 548 } 549 550 for (int i = 0; i < ARRAY_SIZE(cfg_keys); i++) { 551 err = nvlist_lookup_uint64(cfg, cfg_keys[i], &val); 552 if (err) { 553 (void) fprintf(stderr, "error: label %d: " 554 "cannot find nvlist key %s\n", 555 l, cfg_keys[i]); 556 continue; 557 } 558 } 559 560 void *data = (char *)vl + offsetof(vdev_label_t, vl_vdev_phys); 561 eck = (zio_eck_t *)((char *)(data) + VDEV_PHYS_SIZE) - 1; 562 563 offset = label_offset + offsetof(vdev_label_t, vl_vdev_phys); 564 ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0); 565 566 byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC)); 567 if (byteswap) 568 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t)); 569 570 expected_cksum = eck->zec_cksum; 571 eck->zec_cksum = verifier; 572 573 abd_t *abd = abd_get_from_buf(data, VDEV_PHYS_SIZE); 574 ci->ci_func[byteswap](abd, VDEV_PHYS_SIZE, NULL, &actual_cksum); 575 abd_free(abd); 576 577 if (byteswap) 578 byteswap_uint64_array(&expected_cksum, 579 sizeof (zio_cksum_t)); 580 581 if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) 582 continue; 583 584 eck->zec_cksum = actual_cksum; 585 586 err = pwrite64(fd, data, VDEV_PHYS_SIZE, offset); 587 if (err == -1) { 588 (void) fprintf(stderr, "error: cannot write " 589 "label %d: %s\n", l, strerror(errno)); 590 continue; 591 } else if (err != VDEV_PHYS_SIZE) { 592 (void) fprintf(stderr, "error: bad write size " 593 "label %d\n", l); 594 continue; 595 } 596 597 fsync(fd); 598 599 labels_repaired[l] = B_TRUE; 600 } 601 602 close(fd); 603 604 abd_fini(); 605 606 for (int l = 0; l < VDEV_LABELS; l++) { 607 (void) printf("label %d: %s\n", l, 608 labels_repaired[l] ? "repaired" : "skipped"); 609 repaired |= labels_repaired[l]; 610 } 611 612 if (repaired) 613 return (0); 614 615 return (1); 616 } 617 618 static int 619 zhack_do_label(int argc, char **argv) 620 { 621 char *subcommand; 622 int err; 623 624 argc--; 625 argv++; 626 if (argc == 0) { 627 (void) fprintf(stderr, 628 "error: no label operation specified\n"); 629 usage(); 630 } 631 632 subcommand = argv[0]; 633 if (strcmp(subcommand, "repair") == 0) { 634 err = zhack_repair_label_cksum(argc, argv); 635 } else { 636 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 637 subcommand); 638 usage(); 639 } 640 641 return (err); 642 } 643 644 #define MAX_NUM_PATHS 1024 645 646 int 647 main(int argc, char **argv) 648 { 649 extern void zfs_prop_init(void); 650 651 char *path[MAX_NUM_PATHS]; 652 const char *subcommand; 653 int rv = 0; 654 int c; 655 656 g_importargs.path = path; 657 658 dprintf_setup(&argc, argv); 659 zfs_prop_init(); 660 661 while ((c = getopt(argc, argv, "+c:d:")) != -1) { 662 switch (c) { 663 case 'c': 664 g_importargs.cachefile = optarg; 665 break; 666 case 'd': 667 assert(g_importargs.paths < MAX_NUM_PATHS); 668 g_importargs.path[g_importargs.paths++] = optarg; 669 break; 670 default: 671 usage(); 672 break; 673 } 674 } 675 676 argc -= optind; 677 argv += optind; 678 optind = 1; 679 680 if (argc == 0) { 681 (void) fprintf(stderr, "error: no command specified\n"); 682 usage(); 683 } 684 685 subcommand = argv[0]; 686 687 if (strcmp(subcommand, "feature") == 0) { 688 rv = zhack_do_feature(argc, argv); 689 } else if (strcmp(subcommand, "label") == 0) { 690 return (zhack_do_label(argc, argv)); 691 } else { 692 (void) fprintf(stderr, "error: unknown subcommand: %s\n", 693 subcommand); 694 usage(); 695 } 696 697 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) { 698 fatal(NULL, FTAG, "pool export failed; " 699 "changes may not be committed to disk\n"); 700 } 701 702 kernel_fini(); 703 704 return (rv); 705 } 706