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) 2012 by Delphix. All rights reserved.
27 * zhack is a debugging tool that can write changes to ZFS pool using libzpool
28 * for testing purposes. Altering pools with zhack is unsupported and may
29 * result in corrupted pools.
35 #include <sys/zfs_context.h>
37 #include <sys/spa_impl.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/dsl_synctask.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/dmu_objset.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/zio_checksum.h>
47 #include <sys/zio_compress.h>
48 #include <sys/zfeature.h>
52 extern boolean_t zfeature_checks_disable;
54 const char cmdname[] = "zhack";
55 libzfs_handle_t *g_zfs;
56 static importargs_t g_importargs;
58 static boolean_t g_readonly;
63 (void) fprintf(stderr,
64 "Usage: %s [-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 [-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 " feature ref [-md] <pool> <feature>\n"
75 " change the refcount on the given feature\n"
76 " -d decrease instead of increase the refcount\n"
77 " -m add the feature to the label if increasing refcount\n"
79 " <feature> : should be a feature guid\n");
85 fatal(const char *fmt, ...)
90 (void) fprintf(stderr, "%s: ", cmdname);
91 (void) vfprintf(stderr, fmt, ap);
93 (void) fprintf(stderr, "\n");
100 space_delta_cb(dmu_object_type_t bonustype, void *data,
101 uint64_t *userp, uint64_t *groupp)
104 * Is it a valid type of object to track?
106 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
108 (void) fprintf(stderr, "modifying object that needs user accounting");
114 * Target is the dataset whose pool we want to open.
117 import_pool(const char *target, boolean_t readonly)
128 kernel_init(readonly ? FREAD : (FREAD | FWRITE));
129 g_zfs = libzfs_init();
130 ASSERT(g_zfs != NULL);
132 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
134 g_readonly = readonly;
137 * If we only want readonly access, it's OK if we find
138 * a potentially-active (ie, imported into the kernel) pool from the
141 if (readonly && spa_open(target, &spa, FTAG) == 0) {
142 spa_close(spa, FTAG);
146 g_importargs.unique = B_TRUE;
147 g_importargs.can_be_active = readonly;
148 g_pool = strdup(target);
149 if ((sepp = strpbrk(g_pool, "/@")) != NULL)
151 g_importargs.poolname = g_pool;
152 pools = zpool_search_import(g_zfs, &g_importargs);
154 if (pools == NULL || nvlist_next_nvpair(pools, NULL) == NULL) {
155 if (!g_importargs.can_be_active) {
156 g_importargs.can_be_active = B_TRUE;
157 if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
158 spa_open(target, &spa, FTAG) == 0) {
159 fatal("cannot import '%s': pool is active; run "
160 "\"zpool export %s\" first\n",
165 fatal("cannot import '%s': no such pool available\n", g_pool);
168 elem = nvlist_next_nvpair(pools, NULL);
169 name = nvpair_name(elem);
170 VERIFY(nvpair_value_nvlist(elem, &config) == 0);
174 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
175 VERIFY(nvlist_add_uint64(props,
176 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
179 zfeature_checks_disable = B_TRUE;
180 error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
181 zfeature_checks_disable = B_FALSE;
186 fatal("can't import '%s': %s", name, strerror(error));
190 zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
194 import_pool(target, readonly);
196 zfeature_checks_disable = B_TRUE;
197 err = spa_open(target, spa, tag);
198 zfeature_checks_disable = B_FALSE;
201 fatal("cannot open '%s': %s", target, strerror(err));
202 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
203 fatal("'%s' has version %d, features not enabled", target,
204 (int)spa_version(*spa));
209 dump_obj(objset_t *os, uint64_t obj, const char *name)
214 (void) printf("%s_obj:\n", name);
216 for (zap_cursor_init(&zc, os, obj);
217 zap_cursor_retrieve(&zc, &za) == 0;
218 zap_cursor_advance(&zc)) {
219 if (za.za_integer_length == 8) {
220 ASSERT(za.za_num_integers == 1);
221 (void) printf("\t%s = %llu\n",
222 za.za_name, (u_longlong_t)za.za_first_integer);
224 ASSERT(za.za_integer_length == 1);
226 VERIFY(zap_lookup(os, obj, za.za_name,
227 1, sizeof (val), val) == 0);
228 (void) printf("\t%s = %s\n", za.za_name, val);
231 zap_cursor_fini(&zc);
237 nvlist_t *nv = spa->spa_label_features;
240 (void) printf("label config:\n");
241 for (pair = nvlist_next_nvpair(nv, NULL);
243 pair = nvlist_next_nvpair(nv, pair)) {
244 (void) printf("\t%s\n", nvpair_name(pair));
249 zhack_do_feature_stat(int argc, char **argv)
259 (void) fprintf(stderr, "error: missing pool name\n");
264 zhack_spa_open(target, B_TRUE, FTAG, &spa);
265 os = spa->spa_meta_objset;
267 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
268 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
269 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
272 spa_close(spa, FTAG);
276 feature_enable_sync(void *arg1, void *arg2, dmu_tx_t *tx)
279 zfeature_info_t *feature = arg2;
281 spa_feature_enable(spa, feature, tx);
285 zhack_do_feature_enable(int argc, char **argv)
291 zfeature_info_t feature;
292 zfeature_info_t *nodeps[] = { NULL };
295 * Features are not added to the pool's label until their refcounts
296 * are incremented, so fi_mos can just be left as false for now.
299 feature.fi_uname = "zhack";
300 feature.fi_mos = B_FALSE;
301 feature.fi_can_readonly = B_FALSE;
302 feature.fi_depends = nodeps;
305 while ((c = getopt(argc, argv, "rmd:")) != -1) {
308 feature.fi_can_readonly = B_TRUE;
311 desc = strdup(optarg);
320 desc = strdup("zhack injected");
321 feature.fi_desc = desc;
327 (void) fprintf(stderr, "error: missing feature or pool name\n");
331 feature.fi_guid = argv[1];
333 if (!zfeature_is_valid_guid(feature.fi_guid))
334 fatal("invalid feature guid: %s", feature.fi_guid);
336 zhack_spa_open(target, B_FALSE, FTAG, &spa);
337 mos = spa->spa_meta_objset;
339 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
340 fatal("'%s' is a real feature, will not enable");
341 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
342 fatal("feature already enabled: %s", feature.fi_guid);
344 VERIFY3U(0, ==, dsl_sync_task_do(spa->spa_dsl_pool, NULL,
345 feature_enable_sync, spa, &feature, 5));
347 spa_close(spa, FTAG);
353 feature_incr_sync(void *arg1, void *arg2, dmu_tx_t *tx)
356 zfeature_info_t *feature = arg2;
358 spa_feature_incr(spa, feature, tx);
362 feature_decr_sync(void *arg1, void *arg2, dmu_tx_t *tx)
365 zfeature_info_t *feature = arg2;
367 spa_feature_decr(spa, feature, tx);
371 zhack_do_feature_ref(int argc, char **argv)
375 boolean_t decr = B_FALSE;
378 zfeature_info_t feature;
379 zfeature_info_t *nodeps[] = { NULL };
382 * fi_desc does not matter here because it was written to disk
383 * when the feature was enabled, but we need to properly set the
384 * feature for read or write based on the information we read off
387 feature.fi_uname = "zhack";
388 feature.fi_mos = B_FALSE;
389 feature.fi_desc = NULL;
390 feature.fi_depends = nodeps;
393 while ((c = getopt(argc, argv, "md")) != -1) {
396 feature.fi_mos = B_TRUE;
410 (void) fprintf(stderr, "error: missing feature or pool name\n");
414 feature.fi_guid = argv[1];
416 if (!zfeature_is_valid_guid(feature.fi_guid))
417 fatal("invalid feature guid: %s", feature.fi_guid);
419 zhack_spa_open(target, B_FALSE, FTAG, &spa);
420 mos = spa->spa_meta_objset;
422 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
423 fatal("'%s' is a real feature, will not change refcount");
425 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
427 feature.fi_can_readonly = B_FALSE;
428 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
430 feature.fi_can_readonly = B_TRUE;
432 fatal("feature is not enabled: %s", feature.fi_guid);
435 if (decr && !spa_feature_is_active(spa, &feature))
436 fatal("feature refcount already 0: %s", feature.fi_guid);
438 VERIFY3U(0, ==, dsl_sync_task_do(spa->spa_dsl_pool, NULL,
439 decr ? feature_decr_sync : feature_incr_sync, spa, &feature, 5));
441 spa_close(spa, FTAG);
445 zhack_do_feature(int argc, char **argv)
452 (void) fprintf(stderr,
453 "error: no feature operation specified\n");
457 subcommand = argv[0];
458 if (strcmp(subcommand, "stat") == 0) {
459 zhack_do_feature_stat(argc, argv);
460 } else if (strcmp(subcommand, "enable") == 0) {
461 zhack_do_feature_enable(argc, argv);
462 } else if (strcmp(subcommand, "ref") == 0) {
463 zhack_do_feature_ref(argc, argv);
465 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
473 #define MAX_NUM_PATHS 1024
476 main(int argc, char **argv)
478 extern void zfs_prop_init(void);
480 char *path[MAX_NUM_PATHS];
481 const char *subcommand;
485 g_importargs.path = path;
487 dprintf_setup(&argc, argv);
490 while ((c = getopt(argc, argv, "c:d:")) != -1) {
493 g_importargs.cachefile = optarg;
496 assert(g_importargs.paths < MAX_NUM_PATHS);
497 g_importargs.path[g_importargs.paths++] = optarg;
510 (void) fprintf(stderr, "error: no command specified\n");
514 subcommand = argv[0];
516 if (strcmp(subcommand, "feature") == 0) {
517 rv = zhack_do_feature(argc, argv);
519 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
524 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_TRUE) != 0) {
525 fatal("pool export failed; "
526 "changes may not be committed to disk\n");