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
)
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
);
231 nvlist_t
*nv
= spa
->spa_label_features
;
234 (void) printf("label config:\n");
235 for (pair
= nvlist_next_nvpair(nv
, NULL
);
237 pair
= nvlist_next_nvpair(nv
, pair
)) {
238 (void) printf("\t%s\n", nvpair_name(pair
));
243 zhack_do_feature_stat(int argc
, char **argv
)
253 (void) fprintf(stderr
, "error: missing pool name\n");
258 zhack_spa_open(target
, B_TRUE
, FTAG
, &spa
);
259 os
= spa
->spa_meta_objset
;
261 dump_obj(os
, spa
->spa_feat_for_read_obj
, "for_read");
262 dump_obj(os
, spa
->spa_feat_for_write_obj
, "for_write");
263 dump_obj(os
, spa
->spa_feat_desc_obj
, "descriptions");
264 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
265 dump_obj(os
, spa
->spa_feat_enabled_txg_obj
, "enabled_txg");
269 spa_close(spa
, FTAG
);
273 zhack_feature_enable_sync(void *arg
, dmu_tx_t
*tx
)
275 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
276 zfeature_info_t
*feature
= arg
;
278 feature_enable_sync(spa
, feature
, tx
);
280 spa_history_log_internal(spa
, "zhack enable feature", tx
,
282 feature
->fi_guid
, feature
->fi_flags
);
286 zhack_do_feature_enable(int argc
, char **argv
)
292 zfeature_info_t feature
;
293 const spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
296 * Features are not added to the pool's label until their refcounts
297 * are incremented, so fi_mos can just be left as false for now.
300 feature
.fi_uname
= "zhack";
301 feature
.fi_flags
= 0;
302 feature
.fi_depends
= nodeps
;
303 feature
.fi_feature
= SPA_FEATURE_NONE
;
306 while ((c
= getopt(argc
, argv
, "+rd:")) != -1) {
309 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
314 desc
= strdup(optarg
);
323 desc
= strdup("zhack injected");
324 feature
.fi_desc
= desc
;
330 (void) fprintf(stderr
, "error: missing feature or pool name\n");
334 feature
.fi_guid
= argv
[1];
336 if (!zfeature_is_valid_guid(feature
.fi_guid
))
337 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
339 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
340 mos
= spa
->spa_meta_objset
;
342 if (zfeature_is_supported(feature
.fi_guid
))
343 fatal(spa
, FTAG
, "'%s' is a real feature, will not enable",
345 if (0 == zap_contains(mos
, spa
->spa_feat_desc_obj
, feature
.fi_guid
))
346 fatal(spa
, FTAG
, "feature already enabled: %s",
349 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
350 zhack_feature_enable_sync
, &feature
, 5, ZFS_SPACE_CHECK_NORMAL
));
352 spa_close(spa
, FTAG
);
358 feature_incr_sync(void *arg
, dmu_tx_t
*tx
)
360 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
361 zfeature_info_t
*feature
= arg
;
364 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
365 feature_sync(spa
, feature
, refcount
+ 1, tx
);
366 spa_history_log_internal(spa
, "zhack feature incr", tx
,
367 "name=%s", feature
->fi_guid
);
371 feature_decr_sync(void *arg
, dmu_tx_t
*tx
)
373 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
374 zfeature_info_t
*feature
= arg
;
377 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
378 feature_sync(spa
, feature
, refcount
- 1, tx
);
379 spa_history_log_internal(spa
, "zhack feature decr", tx
,
380 "name=%s", feature
->fi_guid
);
384 zhack_do_feature_ref(int argc
, char **argv
)
388 boolean_t decr
= B_FALSE
;
391 zfeature_info_t feature
;
392 const spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
395 * fi_desc does not matter here because it was written to disk
396 * when the feature was enabled, but we need to properly set the
397 * feature for read or write based on the information we read off
400 feature
.fi_uname
= "zhack";
401 feature
.fi_flags
= 0;
402 feature
.fi_desc
= NULL
;
403 feature
.fi_depends
= nodeps
;
404 feature
.fi_feature
= SPA_FEATURE_NONE
;
407 while ((c
= getopt(argc
, argv
, "+md")) != -1) {
410 feature
.fi_flags
|= ZFEATURE_FLAG_MOS
;
424 (void) fprintf(stderr
, "error: missing feature or pool name\n");
428 feature
.fi_guid
= argv
[1];
430 if (!zfeature_is_valid_guid(feature
.fi_guid
))
431 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
433 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
434 mos
= spa
->spa_meta_objset
;
436 if (zfeature_is_supported(feature
.fi_guid
)) {
438 "'%s' is a real feature, will not change refcount",
442 if (0 == zap_contains(mos
, spa
->spa_feat_for_read_obj
,
444 feature
.fi_flags
&= ~ZFEATURE_FLAG_READONLY_COMPAT
;
445 } else if (0 == zap_contains(mos
, spa
->spa_feat_for_write_obj
,
447 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
449 fatal(spa
, FTAG
, "feature is not enabled: %s", feature
.fi_guid
);
454 if (feature_get_refcount_from_disk(spa
, &feature
,
455 &count
) == 0 && count
== 0) {
456 fatal(spa
, FTAG
, "feature refcount already 0: %s",
461 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
462 decr
? feature_decr_sync
: feature_incr_sync
, &feature
,
463 5, ZFS_SPACE_CHECK_NORMAL
));
465 spa_close(spa
, FTAG
);
469 zhack_do_feature(int argc
, char **argv
)
476 (void) fprintf(stderr
,
477 "error: no feature operation specified\n");
481 subcommand
= argv
[0];
482 if (strcmp(subcommand
, "stat") == 0) {
483 zhack_do_feature_stat(argc
, argv
);
484 } else if (strcmp(subcommand
, "enable") == 0) {
485 zhack_do_feature_enable(argc
, argv
);
486 } else if (strcmp(subcommand
, "ref") == 0) {
487 zhack_do_feature_ref(argc
, argv
);
489 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
497 #define ASHIFT_UBERBLOCK_SHIFT(ashift) \
498 MIN(MAX(ashift, UBERBLOCK_SHIFT), \
500 #define ASHIFT_UBERBLOCK_SIZE(ashift) \
501 (1ULL << ASHIFT_UBERBLOCK_SHIFT(ashift))
503 #define REPAIR_LABEL_STATUS_CKSUM (1 << 0)
504 #define REPAIR_LABEL_STATUS_UB (1 << 1)
507 zhack_repair_read_label(const int fd
, vdev_label_t
*vl
,
508 const uint64_t label_offset
, const int l
)
510 const int err
= pread64(fd
, vl
, sizeof (vdev_label_t
), label_offset
);
513 (void) fprintf(stderr
,
514 "error: cannot read label %d: %s\n",
517 } else if (err
!= sizeof (vdev_label_t
)) {
518 (void) fprintf(stderr
,
519 "error: bad label %d read size\n", l
);
527 zhack_repair_calc_cksum(const int byteswap
, void *data
, const uint64_t offset
,
528 const uint64_t abdsize
, zio_eck_t
*eck
, zio_cksum_t
*cksum
)
530 zio_cksum_t verifier
;
531 zio_cksum_t current_cksum
;
532 zio_checksum_info_t
*ci
;
535 ZIO_SET_CHECKSUM(&verifier
, offset
, 0, 0, 0);
538 byteswap_uint64_array(&verifier
, sizeof (zio_cksum_t
));
540 current_cksum
= eck
->zec_cksum
;
541 eck
->zec_cksum
= verifier
;
543 ci
= &zio_checksum_table
[ZIO_CHECKSUM_LABEL
];
544 abd
= abd_get_from_buf(data
, abdsize
);
545 ci
->ci_func
[byteswap
](abd
, abdsize
, NULL
, cksum
);
548 eck
->zec_cksum
= current_cksum
;
552 zhack_repair_check_label(uberblock_t
*ub
, const int l
, const char **cfg_keys
,
553 const size_t cfg_keys_len
, nvlist_t
*cfg
, nvlist_t
*vdev_tree_cfg
,
558 if (ub
->ub_txg
!= 0) {
559 (void) fprintf(stderr
,
560 "error: label %d: UB TXG of 0 expected, but got %"
563 (void) fprintf(stderr
, "It would appear the device was not "
564 "properly removed.\n");
568 for (int i
= 0; i
< cfg_keys_len
; i
++) {
570 err
= nvlist_lookup_uint64(cfg
, cfg_keys
[i
], &val
);
572 (void) fprintf(stderr
,
573 "error: label %d, %d: "
574 "cannot find nvlist key %s\n",
580 err
= nvlist_lookup_nvlist(cfg
,
581 ZPOOL_CONFIG_VDEV_TREE
, &vdev_tree_cfg
);
583 (void) fprintf(stderr
,
584 "error: label %d: cannot find nvlist key %s\n",
585 l
, ZPOOL_CONFIG_VDEV_TREE
);
589 err
= nvlist_lookup_uint64(vdev_tree_cfg
,
590 ZPOOL_CONFIG_ASHIFT
, ashift
);
592 (void) fprintf(stderr
,
593 "error: label %d: cannot find nvlist key %s\n",
594 l
, ZPOOL_CONFIG_ASHIFT
);
599 (void) fprintf(stderr
,
600 "error: label %d: nvlist key %s is zero\n",
601 l
, ZPOOL_CONFIG_ASHIFT
);
609 zhack_repair_undetach(uberblock_t
*ub
, nvlist_t
*cfg
, const int l
)
612 * Uberblock root block pointer has valid birth TXG.
613 * Copying it to the label NVlist
615 if (ub
->ub_rootbp
.blk_birth
!= 0) {
616 const uint64_t txg
= ub
->ub_rootbp
.blk_birth
;
619 if (nvlist_remove_all(cfg
, ZPOOL_CONFIG_CREATE_TXG
) != 0) {
620 (void) fprintf(stderr
,
622 "Failed to remove pool creation TXG\n",
627 if (nvlist_remove_all(cfg
, ZPOOL_CONFIG_POOL_TXG
) != 0) {
628 (void) fprintf(stderr
,
629 "error: label %d: Failed to remove pool TXG to "
635 if (nvlist_add_uint64(cfg
, ZPOOL_CONFIG_POOL_TXG
, txg
) != 0) {
636 (void) fprintf(stderr
,
638 "Failed to add pool TXG of %" PRIu64
"\n",
648 zhack_repair_write_label(const int l
, const int fd
, const int byteswap
,
649 void *data
, zio_eck_t
*eck
, const uint64_t offset
, const uint64_t abdsize
)
651 zio_cksum_t actual_cksum
;
652 zhack_repair_calc_cksum(byteswap
, data
, offset
, abdsize
, eck
,
654 zio_cksum_t expected_cksum
= eck
->zec_cksum
;
657 if (ZIO_CHECKSUM_EQUAL(actual_cksum
, expected_cksum
))
660 eck
->zec_cksum
= actual_cksum
;
662 err
= pwrite64(fd
, data
, abdsize
, offset
);
664 (void) fprintf(stderr
, "error: cannot write label %d: %s\n",
667 } else if (err
!= abdsize
) {
668 (void) fprintf(stderr
, "error: bad write size label %d\n", l
);
671 (void) fprintf(stderr
,
672 "label %d: wrote %" PRIu64
" bytes at offset %" PRIu64
"\n",
680 zhack_repair_write_uberblock(vdev_label_t
*vl
, const int l
,
681 const uint64_t ashift
, const int fd
, const int byteswap
,
682 const uint64_t label_offset
, uint32_t *labels_repaired
)
685 (char *)vl
+ offsetof(vdev_label_t
, vl_uberblock
);
688 ((char *)(ub_data
) + (ASHIFT_UBERBLOCK_SIZE(ashift
))) - 1;
690 if (ub_eck
->zec_magic
!= 0) {
691 (void) fprintf(stderr
,
693 "Expected Uberblock checksum magic number to "
694 "be 0, but got %" PRIu64
"\n",
695 l
, ub_eck
->zec_magic
);
696 (void) fprintf(stderr
, "It would appear there's already "
697 "a checksum for the uberblock.\n");
702 ub_eck
->zec_magic
= byteswap
? BSWAP_64(ZEC_MAGIC
) : ZEC_MAGIC
;
704 if (zhack_repair_write_label(l
, fd
, byteswap
,
706 label_offset
+ offsetof(vdev_label_t
, vl_uberblock
),
707 ASHIFT_UBERBLOCK_SIZE(ashift
)))
708 labels_repaired
[l
] |= REPAIR_LABEL_STATUS_UB
;
712 zhack_repair_print_cksum(FILE *stream
, const zio_cksum_t
*cksum
)
714 (void) fprintf(stream
,
715 "%016llx:%016llx:%016llx:%016llx",
716 (u_longlong_t
)cksum
->zc_word
[0],
717 (u_longlong_t
)cksum
->zc_word
[1],
718 (u_longlong_t
)cksum
->zc_word
[2],
719 (u_longlong_t
)cksum
->zc_word
[3]);
723 zhack_repair_test_cksum(const int byteswap
, void *vdev_data
,
724 zio_eck_t
*vdev_eck
, const uint64_t vdev_phys_offset
, const int l
)
726 const zio_cksum_t expected_cksum
= vdev_eck
->zec_cksum
;
727 zio_cksum_t actual_cksum
;
728 zhack_repair_calc_cksum(byteswap
, vdev_data
, vdev_phys_offset
,
729 VDEV_PHYS_SIZE
, vdev_eck
, &actual_cksum
);
730 const uint64_t expected_magic
= byteswap
?
731 BSWAP_64(ZEC_MAGIC
) : ZEC_MAGIC
;
732 const uint64_t actual_magic
= vdev_eck
->zec_magic
;
734 if (actual_magic
!= expected_magic
) {
735 (void) fprintf(stderr
, "error: label %d: "
737 "the nvlist checksum magic number to not be %"
738 PRIu64
" not %" PRIu64
"\n",
739 l
, expected_magic
, actual_magic
);
742 if (!ZIO_CHECKSUM_EQUAL(actual_cksum
, expected_cksum
)) {
743 (void) fprintf(stderr
, "error: label %d: "
744 "Expected the nvlist checksum to be ", l
);
745 (void) zhack_repair_print_cksum(stderr
,
747 (void) fprintf(stderr
, " not ");
748 zhack_repair_print_cksum(stderr
, &actual_cksum
);
749 (void) fprintf(stderr
, "\n");
756 zhack_repair_one_label(const zhack_repair_op_t op
, const int fd
,
757 vdev_label_t
*vl
, const uint64_t label_offset
, const int l
,
758 uint32_t *labels_repaired
)
761 uberblock_t
*ub
= (uberblock_t
*)vl
->vl_uberblock
;
763 (char *)vl
+ offsetof(vdev_label_t
, vl_vdev_phys
);
764 zio_eck_t
*vdev_eck
=
765 (zio_eck_t
*)((char *)(vdev_data
) + VDEV_PHYS_SIZE
) - 1;
766 const uint64_t vdev_phys_offset
=
767 label_offset
+ offsetof(vdev_label_t
, vl_vdev_phys
);
768 const char *cfg_keys
[] = { ZPOOL_CONFIG_VERSION
,
769 ZPOOL_CONFIG_POOL_STATE
, ZPOOL_CONFIG_GUID
};
771 nvlist_t
*vdev_tree_cfg
= NULL
;
775 err
= zhack_repair_read_label(fd
, vl
, label_offset
, l
);
779 if (vdev_eck
->zec_magic
== 0) {
780 (void) fprintf(stderr
, "error: label %d: "
781 "Expected the nvlist checksum magic number to not be zero"
784 (void) fprintf(stderr
, "There should already be a checksum "
790 (vdev_eck
->zec_magic
== BSWAP_64((uint64_t)ZEC_MAGIC
));
793 byteswap_uint64_array(&vdev_eck
->zec_cksum
,
794 sizeof (zio_cksum_t
));
795 vdev_eck
->zec_magic
= BSWAP_64(vdev_eck
->zec_magic
);
798 if ((op
& ZHACK_REPAIR_OP_CKSUM
) == 0 &&
799 zhack_repair_test_cksum(byteswap
, vdev_data
, vdev_eck
,
800 vdev_phys_offset
, l
) != 0) {
801 (void) fprintf(stderr
, "It would appear checksums are "
802 "corrupted. Try zhack repair label -c <device>\n");
806 err
= nvlist_unpack(vl
->vl_vdev_phys
.vp_nvlist
,
807 VDEV_PHYS_SIZE
- sizeof (zio_eck_t
), &cfg
, 0);
809 (void) fprintf(stderr
,
810 "error: cannot unpack nvlist label %d\n", l
);
814 err
= zhack_repair_check_label(ub
,
815 l
, cfg_keys
, ARRAY_SIZE(cfg_keys
), cfg
, vdev_tree_cfg
, &ashift
);
819 if ((op
& ZHACK_REPAIR_OP_UNDETACH
) != 0) {
823 err
= zhack_repair_undetach(ub
, cfg
, l
);
827 buf
= vl
->vl_vdev_phys
.vp_nvlist
;
828 buflen
= VDEV_PHYS_SIZE
- sizeof (zio_eck_t
);
829 if (nvlist_pack(cfg
, &buf
, &buflen
, NV_ENCODE_XDR
, 0) != 0) {
830 (void) fprintf(stderr
,
831 "error: label %d: Failed to pack nvlist\n", l
);
835 zhack_repair_write_uberblock(vl
,
836 l
, ashift
, fd
, byteswap
, label_offset
, labels_repaired
);
839 if (zhack_repair_write_label(l
, fd
, byteswap
, vdev_data
, vdev_eck
,
840 vdev_phys_offset
, VDEV_PHYS_SIZE
))
841 labels_repaired
[l
] |= REPAIR_LABEL_STATUS_CKSUM
;
847 zhack_repair_label_status(const uint32_t label_status
,
848 const uint32_t to_check
)
850 return ((label_status
& to_check
) != 0 ? "repaired" : "skipped");
854 zhack_label_repair(const zhack_repair_op_t op
, const int argc
, char **argv
)
856 uint32_t labels_repaired
[VDEV_LABELS
] = {0};
857 vdev_label_t labels
[VDEV_LABELS
] = {{{0}}};
861 uint32_t repaired
= 0;
866 (void) fprintf(stderr
, "error: missing device\n");
870 if ((fd
= open(argv
[0], O_RDWR
)) == -1)
871 fatal(NULL
, FTAG
, "cannot open '%s': %s", argv
[0],
874 if (fstat64_blk(fd
, &st
) != 0)
875 fatal(NULL
, FTAG
, "cannot stat '%s': %s", argv
[0],
878 filesize
= st
.st_size
;
879 (void) fprintf(stderr
, "Calculated filesize to be %jd\n",
882 if (filesize
% sizeof (vdev_label_t
) != 0)
884 (filesize
/ sizeof (vdev_label_t
)) * sizeof (vdev_label_t
);
886 for (int l
= 0; l
< VDEV_LABELS
; l
++) {
887 zhack_repair_one_label(op
, fd
, &labels
[l
],
888 vdev_label_offset(filesize
, l
, 0), l
, labels_repaired
);
895 for (int l
= 0; l
< VDEV_LABELS
; l
++) {
896 const uint32_t lr
= labels_repaired
[l
];
897 (void) printf("label %d: ", l
);
898 (void) printf("uberblock: %s ",
899 zhack_repair_label_status(lr
, REPAIR_LABEL_STATUS_UB
));
900 (void) printf("checksum: %s\n",
901 zhack_repair_label_status(lr
, REPAIR_LABEL_STATUS_CKSUM
));
912 zhack_do_label_repair(int argc
, char **argv
)
914 zhack_repair_op_t op
= ZHACK_REPAIR_OP_UNKNOWN
;
918 while ((c
= getopt(argc
, argv
, "+cu")) != -1) {
921 op
|= ZHACK_REPAIR_OP_CKSUM
;
924 op
|= ZHACK_REPAIR_OP_UNDETACH
;
935 if (op
== ZHACK_REPAIR_OP_UNKNOWN
)
936 op
= ZHACK_REPAIR_OP_CKSUM
;
938 return (zhack_label_repair(op
, argc
, argv
));
942 zhack_do_label(int argc
, char **argv
)
950 (void) fprintf(stderr
,
951 "error: no label operation specified\n");
955 subcommand
= argv
[0];
956 if (strcmp(subcommand
, "repair") == 0) {
957 err
= zhack_do_label_repair(argc
, argv
);
959 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
967 #define MAX_NUM_PATHS 1024
970 main(int argc
, char **argv
)
972 char *path
[MAX_NUM_PATHS
];
973 const char *subcommand
;
977 g_importargs
.path
= path
;
979 dprintf_setup(&argc
, argv
);
982 while ((c
= getopt(argc
, argv
, "+c:d:")) != -1) {
985 g_importargs
.cachefile
= optarg
;
988 assert(g_importargs
.paths
< MAX_NUM_PATHS
);
989 g_importargs
.path
[g_importargs
.paths
++] = optarg
;
1002 (void) fprintf(stderr
, "error: no command specified\n");
1006 subcommand
= argv
[0];
1008 if (strcmp(subcommand
, "feature") == 0) {
1009 rv
= zhack_do_feature(argc
, argv
);
1010 } else if (strcmp(subcommand
, "label") == 0) {
1011 return (zhack_do_label(argc
, argv
));
1013 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
1018 if (!g_readonly
&& spa_export(g_pool
, NULL
, B_TRUE
, B_FALSE
) != 0) {
1019 fatal(NULL
, FTAG
, "pool export failed; "
1020 "changes may not be committed to disk\n");