ztest: Fix scrub check in ztest_raidz_expand_check()
[zfs.git] / cmd / zhack.c
blobf297afb65d47a38ffad6324818ee78e64d0435d5
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
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.
33 #include <zfs_prop.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <sys/stat.h>
38 #include <sys/zfs_context.h>
39 #include <sys/spa.h>
40 #include <sys/spa_impl.h>
41 #include <sys/dmu.h>
42 #include <sys/zap.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/dsl_synctask.h>
45 #include <sys/vdev.h>
46 #include <sys/vdev_impl.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/dmu_objset.h>
49 #include <sys/dsl_pool.h>
50 #include <sys/zio_checksum.h>
51 #include <sys/zio_compress.h>
52 #include <sys/zfeature.h>
53 #include <sys/dmu_tx.h>
54 #include <zfeature_common.h>
55 #include <libzutil.h>
57 static importargs_t g_importargs;
58 static char *g_pool;
59 static boolean_t g_readonly;
61 typedef enum {
62 ZHACK_REPAIR_OP_UNKNOWN = 0,
63 ZHACK_REPAIR_OP_CKSUM = (1 << 0),
64 ZHACK_REPAIR_OP_UNDETACH = (1 << 1)
65 } zhack_repair_op_t;
67 static __attribute__((noreturn)) void
68 usage(void)
70 (void) fprintf(stderr,
71 "Usage: zhack [-c cachefile] [-d dir] <subcommand> <args> ...\n"
72 "where <subcommand> <args> is one of the following:\n"
73 "\n");
75 (void) fprintf(stderr,
76 " feature stat <pool>\n"
77 " print information about enabled features\n"
78 " feature enable [-r] [-d desc] <pool> <feature>\n"
79 " add a new enabled feature to the pool\n"
80 " -d <desc> sets the feature's description\n"
81 " -r set read-only compatible flag for feature\n"
82 " feature ref [-md] <pool> <feature>\n"
83 " change the refcount on the given feature\n"
84 " -d decrease instead of increase the refcount\n"
85 " -m add the feature to the label if increasing refcount\n"
86 "\n"
87 " <feature> : should be a feature guid\n"
88 "\n"
89 " label repair <device>\n"
90 " repair labels of a specified device according to options\n"
91 " which may be combined to do their functions in one call\n"
92 " -c repair corrupted label checksums\n"
93 " -u restore the label on a detached device\n"
94 "\n"
95 " <device> : path to vdev\n");
96 exit(1);
100 static __attribute__((format(printf, 3, 4))) __attribute__((noreturn)) void
101 fatal(spa_t *spa, const void *tag, const char *fmt, ...)
103 va_list ap;
105 if (spa != NULL) {
106 spa_close(spa, tag);
107 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
110 va_start(ap, fmt);
111 (void) fputs("zhack: ", stderr);
112 (void) vfprintf(stderr, fmt, ap);
113 va_end(ap);
114 (void) fputc('\n', stderr);
116 exit(1);
119 static int
120 space_delta_cb(dmu_object_type_t bonustype, const void *data,
121 zfs_file_info_t *zoi)
123 (void) data, (void) zoi;
126 * Is it a valid type of object to track?
128 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
129 return (ENOENT);
130 (void) fprintf(stderr, "modifying object that needs user accounting");
131 abort();
135 * Target is the dataset whose pool we want to open.
137 static void
138 zhack_import(char *target, boolean_t readonly)
140 nvlist_t *config;
141 nvlist_t *props;
142 int error;
144 kernel_init(readonly ? SPA_MODE_READ :
145 (SPA_MODE_READ | SPA_MODE_WRITE));
147 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
149 g_readonly = readonly;
150 g_importargs.can_be_active = readonly;
151 g_pool = strdup(target);
153 libpc_handle_t lpch = {
154 .lpc_lib_handle = NULL,
155 .lpc_ops = &libzpool_config_ops,
156 .lpc_printerr = B_TRUE
158 error = zpool_find_config(&lpch, target, &config, &g_importargs);
159 if (error)
160 fatal(NULL, FTAG, "cannot import '%s'", target);
162 props = NULL;
163 if (readonly) {
164 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
165 VERIFY(nvlist_add_uint64(props,
166 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
169 zfeature_checks_disable = B_TRUE;
170 error = spa_import(target, config, props,
171 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
172 fnvlist_free(config);
173 zfeature_checks_disable = B_FALSE;
174 if (error == EEXIST)
175 error = 0;
177 if (error)
178 fatal(NULL, FTAG, "can't import '%s': %s", target,
179 strerror(error));
182 static void
183 zhack_spa_open(char *target, boolean_t readonly, const void *tag, spa_t **spa)
185 int err;
187 zhack_import(target, readonly);
189 zfeature_checks_disable = B_TRUE;
190 err = spa_open(target, spa, tag);
191 zfeature_checks_disable = B_FALSE;
193 if (err != 0)
194 fatal(*spa, FTAG, "cannot open '%s': %s", target,
195 strerror(err));
196 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
197 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
198 target, (int)spa_version(*spa));
202 static void
203 dump_obj(objset_t *os, uint64_t obj, const char *name)
205 zap_cursor_t zc;
206 zap_attribute_t *za = zap_attribute_long_alloc();
208 (void) printf("%s_obj:\n", name);
210 for (zap_cursor_init(&zc, os, obj);
211 zap_cursor_retrieve(&zc, za) == 0;
212 zap_cursor_advance(&zc)) {
213 if (za->za_integer_length == 8) {
214 ASSERT(za->za_num_integers == 1);
215 (void) printf("\t%s = %llu\n",
216 za->za_name, (u_longlong_t)za->za_first_integer);
217 } else {
218 ASSERT(za->za_integer_length == 1);
219 char val[1024];
220 VERIFY(zap_lookup(os, obj, za->za_name,
221 1, sizeof (val), val) == 0);
222 (void) printf("\t%s = %s\n", za->za_name, val);
225 zap_cursor_fini(&zc);
226 zap_attribute_free(za);
229 static void
230 dump_mos(spa_t *spa)
232 nvlist_t *nv = spa->spa_label_features;
233 nvpair_t *pair;
235 (void) printf("label config:\n");
236 for (pair = nvlist_next_nvpair(nv, NULL);
237 pair != NULL;
238 pair = nvlist_next_nvpair(nv, pair)) {
239 (void) printf("\t%s\n", nvpair_name(pair));
243 static void
244 zhack_do_feature_stat(int argc, char **argv)
246 spa_t *spa;
247 objset_t *os;
248 char *target;
250 argc--;
251 argv++;
253 if (argc < 1) {
254 (void) fprintf(stderr, "error: missing pool name\n");
255 usage();
257 target = argv[0];
259 zhack_spa_open(target, B_TRUE, FTAG, &spa);
260 os = spa->spa_meta_objset;
262 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
263 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
264 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
265 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
266 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
268 dump_mos(spa);
270 spa_close(spa, FTAG);
273 static void
274 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
276 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
277 zfeature_info_t *feature = arg;
279 feature_enable_sync(spa, feature, tx);
281 spa_history_log_internal(spa, "zhack enable feature", tx,
282 "name=%s flags=%u",
283 feature->fi_guid, feature->fi_flags);
286 static void
287 zhack_do_feature_enable(int argc, char **argv)
289 int c;
290 char *desc, *target;
291 spa_t *spa;
292 objset_t *mos;
293 zfeature_info_t feature;
294 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
297 * Features are not added to the pool's label until their refcounts
298 * are incremented, so fi_mos can just be left as false for now.
300 desc = NULL;
301 feature.fi_uname = "zhack";
302 feature.fi_flags = 0;
303 feature.fi_depends = nodeps;
304 feature.fi_feature = SPA_FEATURE_NONE;
306 optind = 1;
307 while ((c = getopt(argc, argv, "+rd:")) != -1) {
308 switch (c) {
309 case 'r':
310 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
311 break;
312 case 'd':
313 if (desc != NULL)
314 free(desc);
315 desc = strdup(optarg);
316 break;
317 default:
318 usage();
319 break;
323 if (desc == NULL)
324 desc = strdup("zhack injected");
325 feature.fi_desc = desc;
327 argc -= optind;
328 argv += optind;
330 if (argc < 2) {
331 (void) fprintf(stderr, "error: missing feature or pool name\n");
332 usage();
334 target = argv[0];
335 feature.fi_guid = argv[1];
337 if (!zfeature_is_valid_guid(feature.fi_guid))
338 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
340 zhack_spa_open(target, B_FALSE, FTAG, &spa);
341 mos = spa->spa_meta_objset;
343 if (zfeature_is_supported(feature.fi_guid))
344 fatal(spa, FTAG, "'%s' is a real feature, will not enable",
345 feature.fi_guid);
346 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
347 fatal(spa, FTAG, "feature already enabled: %s",
348 feature.fi_guid);
350 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
351 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
353 spa_close(spa, FTAG);
355 free(desc);
358 static void
359 feature_incr_sync(void *arg, dmu_tx_t *tx)
361 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
362 zfeature_info_t *feature = arg;
363 uint64_t refcount;
365 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
366 feature_sync(spa, feature, refcount + 1, tx);
367 spa_history_log_internal(spa, "zhack feature incr", tx,
368 "name=%s", feature->fi_guid);
371 static void
372 feature_decr_sync(void *arg, dmu_tx_t *tx)
374 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
375 zfeature_info_t *feature = arg;
376 uint64_t refcount;
378 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
379 feature_sync(spa, feature, refcount - 1, tx);
380 spa_history_log_internal(spa, "zhack feature decr", tx,
381 "name=%s", feature->fi_guid);
384 static void
385 zhack_do_feature_ref(int argc, char **argv)
387 int c;
388 char *target;
389 boolean_t decr = B_FALSE;
390 spa_t *spa;
391 objset_t *mos;
392 zfeature_info_t feature;
393 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
396 * fi_desc does not matter here because it was written to disk
397 * when the feature was enabled, but we need to properly set the
398 * feature for read or write based on the information we read off
399 * disk later.
401 feature.fi_uname = "zhack";
402 feature.fi_flags = 0;
403 feature.fi_desc = NULL;
404 feature.fi_depends = nodeps;
405 feature.fi_feature = SPA_FEATURE_NONE;
407 optind = 1;
408 while ((c = getopt(argc, argv, "+md")) != -1) {
409 switch (c) {
410 case 'm':
411 feature.fi_flags |= ZFEATURE_FLAG_MOS;
412 break;
413 case 'd':
414 decr = B_TRUE;
415 break;
416 default:
417 usage();
418 break;
421 argc -= optind;
422 argv += optind;
424 if (argc < 2) {
425 (void) fprintf(stderr, "error: missing feature or pool name\n");
426 usage();
428 target = argv[0];
429 feature.fi_guid = argv[1];
431 if (!zfeature_is_valid_guid(feature.fi_guid))
432 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
434 zhack_spa_open(target, B_FALSE, FTAG, &spa);
435 mos = spa->spa_meta_objset;
437 if (zfeature_is_supported(feature.fi_guid)) {
438 fatal(spa, FTAG,
439 "'%s' is a real feature, will not change refcount",
440 feature.fi_guid);
443 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
444 feature.fi_guid)) {
445 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
446 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
447 feature.fi_guid)) {
448 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
449 } else {
450 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
453 if (decr) {
454 uint64_t count;
455 if (feature_get_refcount_from_disk(spa, &feature,
456 &count) == 0 && count == 0) {
457 fatal(spa, FTAG, "feature refcount already 0: %s",
458 feature.fi_guid);
462 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
463 decr ? feature_decr_sync : feature_incr_sync, &feature,
464 5, ZFS_SPACE_CHECK_NORMAL));
466 spa_close(spa, FTAG);
469 static int
470 zhack_do_feature(int argc, char **argv)
472 char *subcommand;
474 argc--;
475 argv++;
476 if (argc == 0) {
477 (void) fprintf(stderr,
478 "error: no feature operation specified\n");
479 usage();
482 subcommand = argv[0];
483 if (strcmp(subcommand, "stat") == 0) {
484 zhack_do_feature_stat(argc, argv);
485 } else if (strcmp(subcommand, "enable") == 0) {
486 zhack_do_feature_enable(argc, argv);
487 } else if (strcmp(subcommand, "ref") == 0) {
488 zhack_do_feature_ref(argc, argv);
489 } else {
490 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
491 subcommand);
492 usage();
495 return (0);
498 #define ASHIFT_UBERBLOCK_SHIFT(ashift) \
499 MIN(MAX(ashift, UBERBLOCK_SHIFT), \
500 MAX_UBERBLOCK_SHIFT)
501 #define ASHIFT_UBERBLOCK_SIZE(ashift) \
502 (1ULL << ASHIFT_UBERBLOCK_SHIFT(ashift))
504 #define REPAIR_LABEL_STATUS_CKSUM (1 << 0)
505 #define REPAIR_LABEL_STATUS_UB (1 << 1)
507 static int
508 zhack_repair_read_label(const int fd, vdev_label_t *vl,
509 const uint64_t label_offset, const int l)
511 const int err = pread64(fd, vl, sizeof (vdev_label_t), label_offset);
513 if (err == -1) {
514 (void) fprintf(stderr,
515 "error: cannot read label %d: %s\n",
516 l, strerror(errno));
517 return (err);
518 } else if (err != sizeof (vdev_label_t)) {
519 (void) fprintf(stderr,
520 "error: bad label %d read size\n", l);
521 return (err);
524 return (0);
527 static void
528 zhack_repair_calc_cksum(const int byteswap, void *data, const uint64_t offset,
529 const uint64_t abdsize, zio_eck_t *eck, zio_cksum_t *cksum)
531 zio_cksum_t verifier;
532 zio_cksum_t current_cksum;
533 zio_checksum_info_t *ci;
534 abd_t *abd;
536 ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0);
538 if (byteswap)
539 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
541 current_cksum = eck->zec_cksum;
542 eck->zec_cksum = verifier;
544 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
545 abd = abd_get_from_buf(data, abdsize);
546 ci->ci_func[byteswap](abd, abdsize, NULL, cksum);
547 abd_free(abd);
549 eck->zec_cksum = current_cksum;
552 static int
553 zhack_repair_check_label(uberblock_t *ub, const int l, const char **cfg_keys,
554 const size_t cfg_keys_len, nvlist_t *cfg, nvlist_t *vdev_tree_cfg,
555 uint64_t *ashift)
557 int err;
559 if (ub->ub_txg != 0) {
560 (void) fprintf(stderr,
561 "error: label %d: UB TXG of 0 expected, but got %"
562 PRIu64 "\n",
563 l, ub->ub_txg);
564 (void) fprintf(stderr, "It would appear the device was not "
565 "properly removed.\n");
566 return (1);
569 for (int i = 0; i < cfg_keys_len; i++) {
570 uint64_t val;
571 err = nvlist_lookup_uint64(cfg, cfg_keys[i], &val);
572 if (err) {
573 (void) fprintf(stderr,
574 "error: label %d, %d: "
575 "cannot find nvlist key %s\n",
576 l, i, cfg_keys[i]);
577 return (err);
581 err = nvlist_lookup_nvlist(cfg,
582 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree_cfg);
583 if (err) {
584 (void) fprintf(stderr,
585 "error: label %d: cannot find nvlist key %s\n",
586 l, ZPOOL_CONFIG_VDEV_TREE);
587 return (err);
590 err = nvlist_lookup_uint64(vdev_tree_cfg,
591 ZPOOL_CONFIG_ASHIFT, ashift);
592 if (err) {
593 (void) fprintf(stderr,
594 "error: label %d: cannot find nvlist key %s\n",
595 l, ZPOOL_CONFIG_ASHIFT);
596 return (err);
599 if (*ashift == 0) {
600 (void) fprintf(stderr,
601 "error: label %d: nvlist key %s is zero\n",
602 l, ZPOOL_CONFIG_ASHIFT);
603 return (err);
606 return (0);
609 static int
610 zhack_repair_undetach(uberblock_t *ub, nvlist_t *cfg, const int l)
613 * Uberblock root block pointer has valid birth TXG.
614 * Copying it to the label NVlist
616 if (BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp) != 0) {
617 const uint64_t txg = BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp);
618 ub->ub_txg = txg;
620 if (nvlist_remove_all(cfg, ZPOOL_CONFIG_CREATE_TXG) != 0) {
621 (void) fprintf(stderr,
622 "error: label %d: "
623 "Failed to remove pool creation TXG\n",
625 return (1);
628 if (nvlist_remove_all(cfg, ZPOOL_CONFIG_POOL_TXG) != 0) {
629 (void) fprintf(stderr,
630 "error: label %d: Failed to remove pool TXG to "
631 "be replaced.\n",
633 return (1);
636 if (nvlist_add_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, txg) != 0) {
637 (void) fprintf(stderr,
638 "error: label %d: "
639 "Failed to add pool TXG of %" PRIu64 "\n",
640 l, txg);
641 return (1);
645 return (0);
648 static boolean_t
649 zhack_repair_write_label(const int l, const int fd, const int byteswap,
650 void *data, zio_eck_t *eck, const uint64_t offset, const uint64_t abdsize)
652 zio_cksum_t actual_cksum;
653 zhack_repair_calc_cksum(byteswap, data, offset, abdsize, eck,
654 &actual_cksum);
655 zio_cksum_t expected_cksum = eck->zec_cksum;
656 ssize_t err;
658 if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
659 return (B_FALSE);
661 eck->zec_cksum = actual_cksum;
663 err = pwrite64(fd, data, abdsize, offset);
664 if (err == -1) {
665 (void) fprintf(stderr, "error: cannot write label %d: %s\n",
666 l, strerror(errno));
667 return (B_FALSE);
668 } else if (err != abdsize) {
669 (void) fprintf(stderr, "error: bad write size label %d\n", l);
670 return (B_FALSE);
671 } else {
672 (void) fprintf(stderr,
673 "label %d: wrote %" PRIu64 " bytes at offset %" PRIu64 "\n",
674 l, abdsize, offset);
677 return (B_TRUE);
680 static void
681 zhack_repair_write_uberblock(vdev_label_t *vl, const int l,
682 const uint64_t ashift, const int fd, const int byteswap,
683 const uint64_t label_offset, uint32_t *labels_repaired)
685 void *ub_data =
686 (char *)vl + offsetof(vdev_label_t, vl_uberblock);
687 zio_eck_t *ub_eck =
688 (zio_eck_t *)
689 ((char *)(ub_data) + (ASHIFT_UBERBLOCK_SIZE(ashift))) - 1;
691 if (ub_eck->zec_magic != 0) {
692 (void) fprintf(stderr,
693 "error: label %d: "
694 "Expected Uberblock checksum magic number to "
695 "be 0, but got %" PRIu64 "\n",
696 l, ub_eck->zec_magic);
697 (void) fprintf(stderr, "It would appear there's already "
698 "a checksum for the uberblock.\n");
699 return;
703 ub_eck->zec_magic = byteswap ? BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC;
705 if (zhack_repair_write_label(l, fd, byteswap,
706 ub_data, ub_eck,
707 label_offset + offsetof(vdev_label_t, vl_uberblock),
708 ASHIFT_UBERBLOCK_SIZE(ashift)))
709 labels_repaired[l] |= REPAIR_LABEL_STATUS_UB;
712 static void
713 zhack_repair_print_cksum(FILE *stream, const zio_cksum_t *cksum)
715 (void) fprintf(stream,
716 "%016llx:%016llx:%016llx:%016llx",
717 (u_longlong_t)cksum->zc_word[0],
718 (u_longlong_t)cksum->zc_word[1],
719 (u_longlong_t)cksum->zc_word[2],
720 (u_longlong_t)cksum->zc_word[3]);
723 static int
724 zhack_repair_test_cksum(const int byteswap, void *vdev_data,
725 zio_eck_t *vdev_eck, const uint64_t vdev_phys_offset, const int l)
727 const zio_cksum_t expected_cksum = vdev_eck->zec_cksum;
728 zio_cksum_t actual_cksum;
729 zhack_repair_calc_cksum(byteswap, vdev_data, vdev_phys_offset,
730 VDEV_PHYS_SIZE, vdev_eck, &actual_cksum);
731 const uint64_t expected_magic = byteswap ?
732 BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC;
733 const uint64_t actual_magic = vdev_eck->zec_magic;
734 int err = 0;
735 if (actual_magic != expected_magic) {
736 (void) fprintf(stderr, "error: label %d: "
737 "Expected "
738 "the nvlist checksum magic number to not be %"
739 PRIu64 " not %" PRIu64 "\n",
740 l, expected_magic, actual_magic);
741 err = ECKSUM;
743 if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) {
744 (void) fprintf(stderr, "error: label %d: "
745 "Expected the nvlist checksum to be ", l);
746 (void) zhack_repair_print_cksum(stderr,
747 &expected_cksum);
748 (void) fprintf(stderr, " not ");
749 zhack_repair_print_cksum(stderr, &actual_cksum);
750 (void) fprintf(stderr, "\n");
751 err = ECKSUM;
753 return (err);
756 static void
757 zhack_repair_one_label(const zhack_repair_op_t op, const int fd,
758 vdev_label_t *vl, const uint64_t label_offset, const int l,
759 uint32_t *labels_repaired)
761 ssize_t err;
762 uberblock_t *ub = (uberblock_t *)vl->vl_uberblock;
763 void *vdev_data =
764 (char *)vl + offsetof(vdev_label_t, vl_vdev_phys);
765 zio_eck_t *vdev_eck =
766 (zio_eck_t *)((char *)(vdev_data) + VDEV_PHYS_SIZE) - 1;
767 const uint64_t vdev_phys_offset =
768 label_offset + offsetof(vdev_label_t, vl_vdev_phys);
769 const char *cfg_keys[] = { ZPOOL_CONFIG_VERSION,
770 ZPOOL_CONFIG_POOL_STATE, ZPOOL_CONFIG_GUID };
771 nvlist_t *cfg;
772 nvlist_t *vdev_tree_cfg = NULL;
773 uint64_t ashift;
774 int byteswap;
776 err = zhack_repair_read_label(fd, vl, label_offset, l);
777 if (err)
778 return;
780 if (vdev_eck->zec_magic == 0) {
781 (void) fprintf(stderr, "error: label %d: "
782 "Expected the nvlist checksum magic number to not be zero"
783 "\n",
785 (void) fprintf(stderr, "There should already be a checksum "
786 "for the label.\n");
787 return;
790 byteswap =
791 (vdev_eck->zec_magic == BSWAP_64((uint64_t)ZEC_MAGIC));
793 if (byteswap) {
794 byteswap_uint64_array(&vdev_eck->zec_cksum,
795 sizeof (zio_cksum_t));
796 vdev_eck->zec_magic = BSWAP_64(vdev_eck->zec_magic);
799 if ((op & ZHACK_REPAIR_OP_CKSUM) == 0 &&
800 zhack_repair_test_cksum(byteswap, vdev_data, vdev_eck,
801 vdev_phys_offset, l) != 0) {
802 (void) fprintf(stderr, "It would appear checksums are "
803 "corrupted. Try zhack repair label -c <device>\n");
804 return;
807 err = nvlist_unpack(vl->vl_vdev_phys.vp_nvlist,
808 VDEV_PHYS_SIZE - sizeof (zio_eck_t), &cfg, 0);
809 if (err) {
810 (void) fprintf(stderr,
811 "error: cannot unpack nvlist label %d\n", l);
812 return;
815 err = zhack_repair_check_label(ub,
816 l, cfg_keys, ARRAY_SIZE(cfg_keys), cfg, vdev_tree_cfg, &ashift);
817 if (err)
818 return;
820 if ((op & ZHACK_REPAIR_OP_UNDETACH) != 0) {
821 char *buf;
822 size_t buflen;
824 err = zhack_repair_undetach(ub, cfg, l);
825 if (err)
826 return;
828 buf = vl->vl_vdev_phys.vp_nvlist;
829 buflen = VDEV_PHYS_SIZE - sizeof (zio_eck_t);
830 if (nvlist_pack(cfg, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) {
831 (void) fprintf(stderr,
832 "error: label %d: Failed to pack nvlist\n", l);
833 return;
836 zhack_repair_write_uberblock(vl,
837 l, ashift, fd, byteswap, label_offset, labels_repaired);
840 if (zhack_repair_write_label(l, fd, byteswap, vdev_data, vdev_eck,
841 vdev_phys_offset, VDEV_PHYS_SIZE))
842 labels_repaired[l] |= REPAIR_LABEL_STATUS_CKSUM;
844 fsync(fd);
847 static const char *
848 zhack_repair_label_status(const uint32_t label_status,
849 const uint32_t to_check)
851 return ((label_status & to_check) != 0 ? "repaired" : "skipped");
854 static int
855 zhack_label_repair(const zhack_repair_op_t op, const int argc, char **argv)
857 uint32_t labels_repaired[VDEV_LABELS] = {0};
858 vdev_label_t labels[VDEV_LABELS] = {{{0}}};
859 struct stat64 st;
860 int fd;
861 off_t filesize;
862 uint32_t repaired = 0;
864 abd_init();
866 if (argc < 1) {
867 (void) fprintf(stderr, "error: missing device\n");
868 usage();
871 if ((fd = open(argv[0], O_RDWR)) == -1)
872 fatal(NULL, FTAG, "cannot open '%s': %s", argv[0],
873 strerror(errno));
875 if (fstat64_blk(fd, &st) != 0)
876 fatal(NULL, FTAG, "cannot stat '%s': %s", argv[0],
877 strerror(errno));
879 filesize = st.st_size;
880 (void) fprintf(stderr, "Calculated filesize to be %jd\n",
881 (intmax_t)filesize);
883 if (filesize % sizeof (vdev_label_t) != 0)
884 filesize =
885 (filesize / sizeof (vdev_label_t)) * sizeof (vdev_label_t);
887 for (int l = 0; l < VDEV_LABELS; l++) {
888 zhack_repair_one_label(op, fd, &labels[l],
889 vdev_label_offset(filesize, l, 0), l, labels_repaired);
892 close(fd);
894 abd_fini();
896 for (int l = 0; l < VDEV_LABELS; l++) {
897 const uint32_t lr = labels_repaired[l];
898 (void) printf("label %d: ", l);
899 (void) printf("uberblock: %s ",
900 zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_UB));
901 (void) printf("checksum: %s\n",
902 zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_CKSUM));
903 repaired |= lr;
906 if (repaired > 0)
907 return (0);
909 return (1);
912 static int
913 zhack_do_label_repair(int argc, char **argv)
915 zhack_repair_op_t op = ZHACK_REPAIR_OP_UNKNOWN;
916 int c;
918 optind = 1;
919 while ((c = getopt(argc, argv, "+cu")) != -1) {
920 switch (c) {
921 case 'c':
922 op |= ZHACK_REPAIR_OP_CKSUM;
923 break;
924 case 'u':
925 op |= ZHACK_REPAIR_OP_UNDETACH;
926 break;
927 default:
928 usage();
929 break;
933 argc -= optind;
934 argv += optind;
936 if (op == ZHACK_REPAIR_OP_UNKNOWN)
937 op = ZHACK_REPAIR_OP_CKSUM;
939 return (zhack_label_repair(op, argc, argv));
942 static int
943 zhack_do_label(int argc, char **argv)
945 char *subcommand;
946 int err;
948 argc--;
949 argv++;
950 if (argc == 0) {
951 (void) fprintf(stderr,
952 "error: no label operation specified\n");
953 usage();
956 subcommand = argv[0];
957 if (strcmp(subcommand, "repair") == 0) {
958 err = zhack_do_label_repair(argc, argv);
959 } else {
960 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
961 subcommand);
962 usage();
965 return (err);
968 #define MAX_NUM_PATHS 1024
971 main(int argc, char **argv)
973 char *path[MAX_NUM_PATHS];
974 const char *subcommand;
975 int rv = 0;
976 int c;
978 g_importargs.path = path;
980 dprintf_setup(&argc, argv);
981 zfs_prop_init();
983 while ((c = getopt(argc, argv, "+c:d:")) != -1) {
984 switch (c) {
985 case 'c':
986 g_importargs.cachefile = optarg;
987 break;
988 case 'd':
989 assert(g_importargs.paths < MAX_NUM_PATHS);
990 g_importargs.path[g_importargs.paths++] = optarg;
991 break;
992 default:
993 usage();
994 break;
998 argc -= optind;
999 argv += optind;
1000 optind = 1;
1002 if (argc == 0) {
1003 (void) fprintf(stderr, "error: no command specified\n");
1004 usage();
1007 subcommand = argv[0];
1009 if (strcmp(subcommand, "feature") == 0) {
1010 rv = zhack_do_feature(argc, argv);
1011 } else if (strcmp(subcommand, "label") == 0) {
1012 return (zhack_do_label(argc, argv));
1013 } else {
1014 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
1015 subcommand);
1016 usage();
1019 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
1020 fatal(NULL, FTAG, "pool export failed; "
1021 "changes may not be committed to disk\n");
1024 kernel_fini();
1026 return (rv);