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]
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.
38 #include <sys/zfs_context.h>
40 #include <sys/spa_impl.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/dsl_synctask.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>
57 static importargs_t g_importargs
;
59 static boolean_t g_readonly
;
62 ZHACK_REPAIR_OP_UNKNOWN
= 0,
63 ZHACK_REPAIR_OP_CKSUM
= (1 << 0),
64 ZHACK_REPAIR_OP_UNDETACH
= (1 << 1)
67 static __attribute__((noreturn
)) 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"
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"
87 " <feature> : should be a feature guid\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"
95 " <device> : path to vdev\n");
100 static __attribute__((format(printf
, 3, 4))) __attribute__((noreturn
)) void
101 fatal(spa_t
*spa
, const void *tag
, const char *fmt
, ...)
107 (void) spa_export(g_pool
, NULL
, B_TRUE
, B_FALSE
);
111 (void) fputs("zhack: ", stderr
);
112 (void) vfprintf(stderr
, fmt
, ap
);
114 (void) fputc('\n', stderr
);
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
)
130 (void) fprintf(stderr
, "modifying object that needs user accounting");
135 * Target is the dataset whose pool we want to open.
138 zhack_import(char *target
, boolean_t readonly
)
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
);
160 fatal(NULL
, FTAG
, "cannot import '%s'", target
);
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
;
178 fatal(NULL
, FTAG
, "can't import '%s': %s", target
,
183 zhack_spa_open(char *target
, boolean_t readonly
, const void *tag
, spa_t
**spa
)
187 zhack_import(target
, readonly
);
189 zfeature_checks_disable
= B_TRUE
;
190 err
= spa_open(target
, spa
, tag
);
191 zfeature_checks_disable
= B_FALSE
;
194 fatal(*spa
, FTAG
, "cannot open '%s': %s", target
,
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
));
203 dump_obj(objset_t
*os
, uint64_t obj
, const char *name
)
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
);
218 ASSERT(za
->za_integer_length
== 1);
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
);
232 nvlist_t
*nv
= spa
->spa_label_features
;
235 (void) printf("label config:\n");
236 for (pair
= nvlist_next_nvpair(nv
, NULL
);
238 pair
= nvlist_next_nvpair(nv
, pair
)) {
239 (void) printf("\t%s\n", nvpair_name(pair
));
244 zhack_do_feature_stat(int argc
, char **argv
)
254 (void) fprintf(stderr
, "error: missing pool name\n");
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");
270 spa_close(spa
, FTAG
);
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
,
283 feature
->fi_guid
, feature
->fi_flags
);
287 zhack_do_feature_enable(int argc
, char **argv
)
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.
301 feature
.fi_uname
= "zhack";
302 feature
.fi_flags
= 0;
303 feature
.fi_depends
= nodeps
;
304 feature
.fi_feature
= SPA_FEATURE_NONE
;
307 while ((c
= getopt(argc
, argv
, "+rd:")) != -1) {
310 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
315 desc
= strdup(optarg
);
324 desc
= strdup("zhack injected");
325 feature
.fi_desc
= desc
;
331 (void) fprintf(stderr
, "error: missing feature or pool name\n");
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",
346 if (0 == zap_contains(mos
, spa
->spa_feat_desc_obj
, feature
.fi_guid
))
347 fatal(spa
, FTAG
, "feature already enabled: %s",
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
);
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
;
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
);
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
;
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
);
385 zhack_do_feature_ref(int argc
, char **argv
)
389 boolean_t decr
= B_FALSE
;
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
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
;
408 while ((c
= getopt(argc
, argv
, "+md")) != -1) {
411 feature
.fi_flags
|= ZFEATURE_FLAG_MOS
;
425 (void) fprintf(stderr
, "error: missing feature or pool name\n");
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
)) {
439 "'%s' is a real feature, will not change refcount",
443 if (0 == zap_contains(mos
, spa
->spa_feat_for_read_obj
,
445 feature
.fi_flags
&= ~ZFEATURE_FLAG_READONLY_COMPAT
;
446 } else if (0 == zap_contains(mos
, spa
->spa_feat_for_write_obj
,
448 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
450 fatal(spa
, FTAG
, "feature is not enabled: %s", feature
.fi_guid
);
455 if (feature_get_refcount_from_disk(spa
, &feature
,
456 &count
) == 0 && count
== 0) {
457 fatal(spa
, FTAG
, "feature refcount already 0: %s",
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
);
470 zhack_do_feature(int argc
, char **argv
)
477 (void) fprintf(stderr
,
478 "error: no feature operation specified\n");
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
);
490 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
498 #define ASHIFT_UBERBLOCK_SHIFT(ashift) \
499 MIN(MAX(ashift, 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)
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
);
514 (void) fprintf(stderr
,
515 "error: cannot read label %d: %s\n",
518 } else if (err
!= sizeof (vdev_label_t
)) {
519 (void) fprintf(stderr
,
520 "error: bad label %d read size\n", l
);
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
;
536 ZIO_SET_CHECKSUM(&verifier
, offset
, 0, 0, 0);
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
);
549 eck
->zec_cksum
= current_cksum
;
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
,
559 if (ub
->ub_txg
!= 0) {
560 (void) fprintf(stderr
,
561 "error: label %d: UB TXG of 0 expected, but got %"
564 (void) fprintf(stderr
, "It would appear the device was not "
565 "properly removed.\n");
569 for (int i
= 0; i
< cfg_keys_len
; i
++) {
571 err
= nvlist_lookup_uint64(cfg
, cfg_keys
[i
], &val
);
573 (void) fprintf(stderr
,
574 "error: label %d, %d: "
575 "cannot find nvlist key %s\n",
581 err
= nvlist_lookup_nvlist(cfg
,
582 ZPOOL_CONFIG_VDEV_TREE
, &vdev_tree_cfg
);
584 (void) fprintf(stderr
,
585 "error: label %d: cannot find nvlist key %s\n",
586 l
, ZPOOL_CONFIG_VDEV_TREE
);
590 err
= nvlist_lookup_uint64(vdev_tree_cfg
,
591 ZPOOL_CONFIG_ASHIFT
, ashift
);
593 (void) fprintf(stderr
,
594 "error: label %d: cannot find nvlist key %s\n",
595 l
, ZPOOL_CONFIG_ASHIFT
);
600 (void) fprintf(stderr
,
601 "error: label %d: nvlist key %s is zero\n",
602 l
, ZPOOL_CONFIG_ASHIFT
);
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
);
620 if (nvlist_remove_all(cfg
, ZPOOL_CONFIG_CREATE_TXG
) != 0) {
621 (void) fprintf(stderr
,
623 "Failed to remove pool creation TXG\n",
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 "
636 if (nvlist_add_uint64(cfg
, ZPOOL_CONFIG_POOL_TXG
, txg
) != 0) {
637 (void) fprintf(stderr
,
639 "Failed to add pool TXG of %" PRIu64
"\n",
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
,
655 zio_cksum_t expected_cksum
= eck
->zec_cksum
;
658 if (ZIO_CHECKSUM_EQUAL(actual_cksum
, expected_cksum
))
661 eck
->zec_cksum
= actual_cksum
;
663 err
= pwrite64(fd
, data
, abdsize
, offset
);
665 (void) fprintf(stderr
, "error: cannot write label %d: %s\n",
668 } else if (err
!= abdsize
) {
669 (void) fprintf(stderr
, "error: bad write size label %d\n", l
);
672 (void) fprintf(stderr
,
673 "label %d: wrote %" PRIu64
" bytes at offset %" PRIu64
"\n",
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
)
686 (char *)vl
+ offsetof(vdev_label_t
, vl_uberblock
);
689 ((char *)(ub_data
) + (ASHIFT_UBERBLOCK_SIZE(ashift
))) - 1;
691 if (ub_eck
->zec_magic
!= 0) {
692 (void) fprintf(stderr
,
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");
703 ub_eck
->zec_magic
= byteswap
? BSWAP_64(ZEC_MAGIC
) : ZEC_MAGIC
;
705 if (zhack_repair_write_label(l
, fd
, byteswap
,
707 label_offset
+ offsetof(vdev_label_t
, vl_uberblock
),
708 ASHIFT_UBERBLOCK_SIZE(ashift
)))
709 labels_repaired
[l
] |= REPAIR_LABEL_STATUS_UB
;
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]);
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
;
735 if (actual_magic
!= expected_magic
) {
736 (void) fprintf(stderr
, "error: label %d: "
738 "the nvlist checksum magic number to not be %"
739 PRIu64
" not %" PRIu64
"\n",
740 l
, expected_magic
, actual_magic
);
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
,
748 (void) fprintf(stderr
, " not ");
749 zhack_repair_print_cksum(stderr
, &actual_cksum
);
750 (void) fprintf(stderr
, "\n");
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
)
762 uberblock_t
*ub
= (uberblock_t
*)vl
->vl_uberblock
;
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
};
772 nvlist_t
*vdev_tree_cfg
= NULL
;
776 err
= zhack_repair_read_label(fd
, vl
, label_offset
, l
);
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"
785 (void) fprintf(stderr
, "There should already be a checksum "
791 (vdev_eck
->zec_magic
== BSWAP_64((uint64_t)ZEC_MAGIC
));
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");
807 err
= nvlist_unpack(vl
->vl_vdev_phys
.vp_nvlist
,
808 VDEV_PHYS_SIZE
- sizeof (zio_eck_t
), &cfg
, 0);
810 (void) fprintf(stderr
,
811 "error: cannot unpack nvlist label %d\n", l
);
815 err
= zhack_repair_check_label(ub
,
816 l
, cfg_keys
, ARRAY_SIZE(cfg_keys
), cfg
, vdev_tree_cfg
, &ashift
);
820 if ((op
& ZHACK_REPAIR_OP_UNDETACH
) != 0) {
824 err
= zhack_repair_undetach(ub
, cfg
, l
);
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
);
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
;
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");
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}}};
862 uint32_t repaired
= 0;
867 (void) fprintf(stderr
, "error: missing device\n");
871 if ((fd
= open(argv
[0], O_RDWR
)) == -1)
872 fatal(NULL
, FTAG
, "cannot open '%s': %s", argv
[0],
875 if (fstat64_blk(fd
, &st
) != 0)
876 fatal(NULL
, FTAG
, "cannot stat '%s': %s", argv
[0],
879 filesize
= st
.st_size
;
880 (void) fprintf(stderr
, "Calculated filesize to be %jd\n",
883 if (filesize
% sizeof (vdev_label_t
) != 0)
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
);
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
));
913 zhack_do_label_repair(int argc
, char **argv
)
915 zhack_repair_op_t op
= ZHACK_REPAIR_OP_UNKNOWN
;
919 while ((c
= getopt(argc
, argv
, "+cu")) != -1) {
922 op
|= ZHACK_REPAIR_OP_CKSUM
;
925 op
|= ZHACK_REPAIR_OP_UNDETACH
;
936 if (op
== ZHACK_REPAIR_OP_UNKNOWN
)
937 op
= ZHACK_REPAIR_OP_CKSUM
;
939 return (zhack_label_repair(op
, argc
, argv
));
943 zhack_do_label(int argc
, char **argv
)
951 (void) fprintf(stderr
,
952 "error: no label operation specified\n");
956 subcommand
= argv
[0];
957 if (strcmp(subcommand
, "repair") == 0) {
958 err
= zhack_do_label_repair(argc
, argv
);
960 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
968 #define MAX_NUM_PATHS 1024
971 main(int argc
, char **argv
)
973 char *path
[MAX_NUM_PATHS
];
974 const char *subcommand
;
978 g_importargs
.path
= path
;
980 dprintf_setup(&argc
, argv
);
983 while ((c
= getopt(argc
, argv
, "+c:d:")) != -1) {
986 g_importargs
.cachefile
= optarg
;
989 assert(g_importargs
.paths
< MAX_NUM_PATHS
);
990 g_importargs
.path
[g_importargs
.paths
++] = optarg
;
1003 (void) fprintf(stderr
, "error: no command specified\n");
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
));
1014 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
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");