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 http://www.opensolaris.org/os/licensing.
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.
37 #include <sys/zfs_context.h>
39 #include <sys/spa_impl.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/dsl_synctask.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>
56 static importargs_t g_importargs;
58 static boolean_t g_readonly;
60 static __attribute__((noreturn)) void
63 (void) fprintf(stderr,
64 "Usage: zhack [-c cachefile] [-d dir] <subcommand> <args> ...\n"
65 "where <subcommand> <args> is one of the following:\n"
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"
80 " <feature> : should be a feature guid\n"
82 " label repair <device>\n"
83 " repair corrupted label checksums\n"
85 " <device> : path to vdev\n");
90 static __attribute__((format(printf, 3, 4))) __attribute__((noreturn)) void
91 fatal(spa_t *spa, void *tag, const char *fmt, ...)
97 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
101 (void) fputs("zhack: ", stderr);
102 (void) vfprintf(stderr, fmt, ap);
104 (void) fputc('\n', stderr);
110 space_delta_cb(dmu_object_type_t bonustype, const void *data,
111 zfs_file_info_t *zoi)
113 (void) data, (void) zoi;
116 * Is it a valid type of object to track?
118 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
120 (void) fprintf(stderr, "modifying object that needs user accounting");
125 * Target is the dataset whose pool we want to open.
128 zhack_import(char *target, boolean_t readonly)
134 kernel_init(readonly ? SPA_MODE_READ :
135 (SPA_MODE_READ | SPA_MODE_WRITE));
137 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
139 g_readonly = readonly;
140 g_importargs.can_be_active = readonly;
141 g_pool = strdup(target);
143 error = zpool_find_config(NULL, target, &config, &g_importargs,
144 &libzpool_config_ops);
146 fatal(NULL, FTAG, "cannot import '%s'", target);
150 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
151 VERIFY(nvlist_add_uint64(props,
152 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
155 zfeature_checks_disable = B_TRUE;
156 error = spa_import(target, config, props,
157 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
158 fnvlist_free(config);
159 zfeature_checks_disable = B_FALSE;
164 fatal(NULL, FTAG, "can't import '%s': %s", target,
169 zhack_spa_open(char *target, boolean_t readonly, void *tag, spa_t **spa)
173 zhack_import(target, readonly);
175 zfeature_checks_disable = B_TRUE;
176 err = spa_open(target, spa, tag);
177 zfeature_checks_disable = B_FALSE;
180 fatal(*spa, FTAG, "cannot open '%s': %s", target,
182 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
183 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
184 target, (int)spa_version(*spa));
189 dump_obj(objset_t *os, uint64_t obj, const char *name)
194 (void) printf("%s_obj:\n", name);
196 for (zap_cursor_init(&zc, os, obj);
197 zap_cursor_retrieve(&zc, &za) == 0;
198 zap_cursor_advance(&zc)) {
199 if (za.za_integer_length == 8) {
200 ASSERT(za.za_num_integers == 1);
201 (void) printf("\t%s = %llu\n",
202 za.za_name, (u_longlong_t)za.za_first_integer);
204 ASSERT(za.za_integer_length == 1);
206 VERIFY(zap_lookup(os, obj, za.za_name,
207 1, sizeof (val), val) == 0);
208 (void) printf("\t%s = %s\n", za.za_name, val);
211 zap_cursor_fini(&zc);
217 nvlist_t *nv = spa->spa_label_features;
220 (void) printf("label config:\n");
221 for (pair = nvlist_next_nvpair(nv, NULL);
223 pair = nvlist_next_nvpair(nv, pair)) {
224 (void) printf("\t%s\n", nvpair_name(pair));
229 zhack_do_feature_stat(int argc, char **argv)
239 (void) fprintf(stderr, "error: missing pool name\n");
244 zhack_spa_open(target, B_TRUE, FTAG, &spa);
245 os = spa->spa_meta_objset;
247 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
248 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
249 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
250 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
251 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
255 spa_close(spa, FTAG);
259 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
261 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
262 zfeature_info_t *feature = arg;
264 feature_enable_sync(spa, feature, tx);
266 spa_history_log_internal(spa, "zhack enable feature", tx,
268 feature->fi_guid, feature->fi_flags);
272 zhack_do_feature_enable(int argc, char **argv)
278 zfeature_info_t feature;
279 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
282 * Features are not added to the pool's label until their refcounts
283 * are incremented, so fi_mos can just be left as false for now.
286 feature.fi_uname = "zhack";
287 feature.fi_flags = 0;
288 feature.fi_depends = nodeps;
289 feature.fi_feature = SPA_FEATURE_NONE;
292 while ((c = getopt(argc, argv, "+rd:")) != -1) {
295 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
298 desc = strdup(optarg);
307 desc = strdup("zhack injected");
308 feature.fi_desc = desc;
314 (void) fprintf(stderr, "error: missing feature or pool name\n");
318 feature.fi_guid = argv[1];
320 if (!zfeature_is_valid_guid(feature.fi_guid))
321 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
323 zhack_spa_open(target, B_FALSE, FTAG, &spa);
324 mos = spa->spa_meta_objset;
326 if (zfeature_is_supported(feature.fi_guid))
327 fatal(spa, FTAG, "'%s' is a real feature, will not enable",
329 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
330 fatal(spa, FTAG, "feature already enabled: %s",
333 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
334 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
336 spa_close(spa, FTAG);
342 feature_incr_sync(void *arg, dmu_tx_t *tx)
344 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
345 zfeature_info_t *feature = arg;
348 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
349 feature_sync(spa, feature, refcount + 1, tx);
350 spa_history_log_internal(spa, "zhack feature incr", tx,
351 "name=%s", feature->fi_guid);
355 feature_decr_sync(void *arg, dmu_tx_t *tx)
357 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
358 zfeature_info_t *feature = arg;
361 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
362 feature_sync(spa, feature, refcount - 1, tx);
363 spa_history_log_internal(spa, "zhack feature decr", tx,
364 "name=%s", feature->fi_guid);
368 zhack_do_feature_ref(int argc, char **argv)
372 boolean_t decr = B_FALSE;
375 zfeature_info_t feature;
376 const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
379 * fi_desc does not matter here because it was written to disk
380 * when the feature was enabled, but we need to properly set the
381 * feature for read or write based on the information we read off
384 feature.fi_uname = "zhack";
385 feature.fi_flags = 0;
386 feature.fi_desc = NULL;
387 feature.fi_depends = nodeps;
388 feature.fi_feature = SPA_FEATURE_NONE;
391 while ((c = getopt(argc, argv, "+md")) != -1) {
394 feature.fi_flags |= ZFEATURE_FLAG_MOS;
408 (void) fprintf(stderr, "error: missing feature or pool name\n");
412 feature.fi_guid = argv[1];
414 if (!zfeature_is_valid_guid(feature.fi_guid))
415 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
417 zhack_spa_open(target, B_FALSE, FTAG, &spa);
418 mos = spa->spa_meta_objset;
420 if (zfeature_is_supported(feature.fi_guid)) {
422 "'%s' is a real feature, will not change refcount",
426 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
428 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
429 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
431 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
433 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
438 if (feature_get_refcount_from_disk(spa, &feature,
439 &count) == 0 && count == 0) {
440 fatal(spa, FTAG, "feature refcount already 0: %s",
445 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
446 decr ? feature_decr_sync : feature_incr_sync, &feature,
447 5, ZFS_SPACE_CHECK_NORMAL));
449 spa_close(spa, FTAG);
453 zhack_do_feature(int argc, char **argv)
460 (void) fprintf(stderr,
461 "error: no feature operation specified\n");
465 subcommand = argv[0];
466 if (strcmp(subcommand, "stat") == 0) {
467 zhack_do_feature_stat(argc, argv);
468 } else if (strcmp(subcommand, "enable") == 0) {
469 zhack_do_feature_enable(argc, argv);
470 } else if (strcmp(subcommand, "ref") == 0) {
471 zhack_do_feature_ref(argc, argv);
473 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
482 zhack_repair_label_cksum(int argc, char **argv)
484 zio_checksum_info_t *ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
485 const char *cfg_keys[] = { ZPOOL_CONFIG_VERSION,
486 ZPOOL_CONFIG_POOL_STATE, ZPOOL_CONFIG_GUID };
487 boolean_t labels_repaired[VDEV_LABELS] = {0};
488 boolean_t repaired = B_FALSE;
489 vdev_label_t labels[VDEV_LABELS] = {{{0}}};
499 (void) fprintf(stderr, "error: missing device\n");
503 if ((fd = open(argv[0], O_RDWR)) == -1)
504 fatal(NULL, FTAG, "cannot open '%s': %s", argv[0],
507 if (stat(argv[0], &st) != 0)
508 fatal(NULL, FTAG, "cannot stat '%s': %s", argv[0],
511 for (int l = 0; l < VDEV_LABELS; l++) {
512 uint64_t label_offset, offset;
513 zio_cksum_t expected_cksum;
514 zio_cksum_t actual_cksum;
515 zio_cksum_t verifier;
522 vdev_label_t *vl = &labels[l];
524 label_offset = vdev_label_offset(st.st_size, l, 0);
525 err = pread64(fd, vl, sizeof (vdev_label_t), label_offset);
527 (void) fprintf(stderr, "error: cannot read "
528 "label %d: %s\n", l, strerror(errno));
530 } else if (err != sizeof (vdev_label_t)) {
531 (void) fprintf(stderr, "error: bad label %d read size "
536 err = nvlist_unpack(vl->vl_vdev_phys.vp_nvlist,
537 VDEV_PHYS_SIZE - sizeof (zio_eck_t), &cfg, 0);
539 (void) fprintf(stderr, "error: cannot unpack nvlist "
544 for (int i = 0; i < ARRAY_SIZE(cfg_keys); i++) {
545 err = nvlist_lookup_uint64(cfg, cfg_keys[i], &val);
547 (void) fprintf(stderr, "error: label %d: "
548 "cannot find nvlist key %s\n",
554 void *data = (char *)vl + offsetof(vdev_label_t, vl_vdev_phys);
555 eck = (zio_eck_t *)((char *)(data) + VDEV_PHYS_SIZE) - 1;
557 offset = label_offset + offsetof(vdev_label_t, vl_vdev_phys);
558 ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0);
560 byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC));
562 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
564 expected_cksum = eck->zec_cksum;
565 eck->zec_cksum = verifier;
567 abd_t *abd = abd_get_from_buf(data, VDEV_PHYS_SIZE);
568 ci->ci_func[byteswap](abd, VDEV_PHYS_SIZE, NULL, &actual_cksum);
572 byteswap_uint64_array(&expected_cksum,
573 sizeof (zio_cksum_t));
575 if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
578 eck->zec_cksum = actual_cksum;
580 err = pwrite64(fd, data, VDEV_PHYS_SIZE, offset);
582 (void) fprintf(stderr, "error: cannot write "
583 "label %d: %s\n", l, strerror(errno));
585 } else if (err != VDEV_PHYS_SIZE) {
586 (void) fprintf(stderr, "error: bad write size "
593 labels_repaired[l] = B_TRUE;
600 for (int l = 0; l < VDEV_LABELS; l++) {
601 (void) printf("label %d: %s\n", l,
602 labels_repaired[l] ? "repaired" : "skipped");
603 repaired |= labels_repaired[l];
613 zhack_do_label(int argc, char **argv)
621 (void) fprintf(stderr,
622 "error: no label operation specified\n");
626 subcommand = argv[0];
627 if (strcmp(subcommand, "repair") == 0) {
628 err = zhack_repair_label_cksum(argc, argv);
630 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
638 #define MAX_NUM_PATHS 1024
641 main(int argc, char **argv)
643 extern void zfs_prop_init(void);
645 char *path[MAX_NUM_PATHS];
646 const char *subcommand;
650 g_importargs.path = path;
652 dprintf_setup(&argc, argv);
655 while ((c = getopt(argc, argv, "+c:d:")) != -1) {
658 g_importargs.cachefile = optarg;
661 assert(g_importargs.paths < MAX_NUM_PATHS);
662 g_importargs.path[g_importargs.paths++] = optarg;
675 (void) fprintf(stderr, "error: no command specified\n");
679 subcommand = argv[0];
681 if (strcmp(subcommand, "feature") == 0) {
682 rv = zhack_do_feature(argc, argv);
683 } else if (strcmp(subcommand, "label") == 0) {
684 return (zhack_do_label(argc, argv));
686 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
691 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
692 fatal(NULL, FTAG, "pool export failed; "
693 "changes may not be committed to disk\n");