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) 2013 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.
36 #include <sys/zfs_context.h>
38 #include <sys/spa_impl.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/dsl_synctask.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/dmu_objset.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/zio_checksum.h>
48 #include <sys/zio_compress.h>
49 #include <sys/zfeature.h>
50 #include <sys/dmu_tx.h>
55 extern boolean_t zfeature_checks_disable;
57 const char cmdname[] = "zhack";
58 libzfs_handle_t *g_zfs;
59 static importargs_t g_importargs;
61 static boolean_t g_readonly;
66 (void) fprintf(stderr,
67 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
68 "where <subcommand> <args> is one of the following:\n"
71 (void) fprintf(stderr,
72 " feature stat <pool>\n"
73 " print information about enabled features\n"
74 " feature enable [-d desc] <pool> <feature>\n"
75 " add a new enabled feature to the pool\n"
76 " -d <desc> sets the feature's description\n"
77 " feature ref [-md] <pool> <feature>\n"
78 " change the refcount on the given feature\n"
79 " -d decrease instead of increase the refcount\n"
80 " -m add the feature to the label if increasing refcount\n"
82 " <feature> : should be a feature guid\n");
88 fatal(spa_t *spa, void *tag, const char *fmt, ...)
94 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
98 (void) fprintf(stderr, "%s: ", cmdname);
99 (void) vfprintf(stderr, fmt, ap);
101 (void) fprintf(stderr, "\n");
108 space_delta_cb(dmu_object_type_t bonustype, void *data,
109 uint64_t *userp, uint64_t *groupp)
112 * Is it a valid type of object to track?
114 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
116 (void) fprintf(stderr, "modifying object that needs user accounting");
122 * Target is the dataset whose pool we want to open.
125 import_pool(const char *target, boolean_t readonly)
136 kernel_init(readonly ? FREAD : (FREAD | FWRITE));
137 g_zfs = libzfs_init();
138 ASSERT(g_zfs != NULL);
140 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
142 g_readonly = readonly;
145 * If we only want readonly access, it's OK if we find
146 * a potentially-active (ie, imported into the kernel) pool from the
149 if (readonly && spa_open(target, &spa, FTAG) == 0) {
150 spa_close(spa, FTAG);
154 g_importargs.unique = B_TRUE;
155 g_importargs.can_be_active = readonly;
156 g_pool = strdup(target);
157 if ((sepp = strpbrk(g_pool, "/@")) != NULL)
159 g_importargs.poolname = g_pool;
160 pools = zpool_search_import(g_zfs, &g_importargs);
162 if (nvlist_empty(pools)) {
163 if (!g_importargs.can_be_active) {
164 g_importargs.can_be_active = B_TRUE;
165 if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
166 spa_open(target, &spa, FTAG) == 0) {
167 fatal(spa, FTAG, "cannot import '%s': pool is "
168 "active; run " "\"zpool export %s\" "
169 "first\n", g_pool, g_pool);
173 fatal(NULL, FTAG, "cannot import '%s': no such pool "
174 "available\n", g_pool);
177 elem = nvlist_next_nvpair(pools, NULL);
178 name = nvpair_name(elem);
179 verify(nvpair_value_nvlist(elem, &config) == 0);
183 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
184 verify(nvlist_add_uint64(props,
185 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
188 zfeature_checks_disable = B_TRUE;
189 error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
190 zfeature_checks_disable = B_FALSE;
195 fatal(NULL, FTAG, "can't import '%s': %s", name,
200 zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
204 import_pool(target, readonly);
206 zfeature_checks_disable = B_TRUE;
207 err = spa_open(target, spa, tag);
208 zfeature_checks_disable = B_FALSE;
211 fatal(*spa, FTAG, "cannot open '%s': %s", target,
213 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
214 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
215 target, (int)spa_version(*spa));
220 dump_obj(objset_t *os, uint64_t obj, const char *name)
225 (void) printf("%s_obj:\n", name);
227 for (zap_cursor_init(&zc, os, obj);
228 zap_cursor_retrieve(&zc, &za) == 0;
229 zap_cursor_advance(&zc)) {
230 if (za.za_integer_length == 8) {
231 ASSERT(za.za_num_integers == 1);
232 (void) printf("\t%s = %llu\n",
233 za.za_name, (u_longlong_t)za.za_first_integer);
235 ASSERT(za.za_integer_length == 1);
237 VERIFY(zap_lookup(os, obj, za.za_name,
238 1, sizeof (val), val) == 0);
239 (void) printf("\t%s = %s\n", za.za_name, val);
242 zap_cursor_fini(&zc);
248 nvlist_t *nv = spa->spa_label_features;
250 (void) printf("label config:\n");
251 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
253 pair = nvlist_next_nvpair(nv, pair)) {
254 (void) printf("\t%s\n", nvpair_name(pair));
259 zhack_do_feature_stat(int argc, char **argv)
269 (void) fprintf(stderr, "error: missing pool name\n");
274 zhack_spa_open(target, B_TRUE, FTAG, &spa);
275 os = spa->spa_meta_objset;
277 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
278 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
279 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
282 spa_close(spa, FTAG);
286 feature_enable_sync(void *arg, dmu_tx_t *tx)
288 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
289 zfeature_info_t *feature = arg;
291 spa_feature_enable(spa, feature, tx);
292 spa_history_log_internal(spa, "zhack enable feature", tx,
293 "name=%s can_readonly=%u",
294 feature->fi_guid, feature->fi_can_readonly);
298 zhack_do_feature_enable(int argc, char **argv)
304 zfeature_info_t feature;
305 zfeature_info_t *nodeps[] = { NULL };
308 * Features are not added to the pool's label until their refcounts
309 * are incremented, so fi_mos can just be left as false for now.
312 feature.fi_uname = "zhack";
313 feature.fi_mos = B_FALSE;
314 feature.fi_can_readonly = B_FALSE;
315 feature.fi_depends = nodeps;
318 while ((c = getopt(argc, argv, "rmd:")) != -1) {
321 feature.fi_can_readonly = B_TRUE;
324 desc = strdup(optarg);
333 desc = strdup("zhack injected");
334 feature.fi_desc = desc;
340 (void) fprintf(stderr, "error: missing feature or pool name\n");
344 feature.fi_guid = argv[1];
346 if (!zfeature_is_valid_guid(feature.fi_guid))
347 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
349 zhack_spa_open(target, B_FALSE, FTAG, &spa);
350 mos = spa->spa_meta_objset;
352 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
353 fatal(spa, FTAG, "'%s' is a real feature, will not enable");
354 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
355 fatal(spa, FTAG, "feature already enabled: %s",
358 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
359 feature_enable_sync, &feature, 5));
361 spa_close(spa, FTAG);
367 feature_incr_sync(void *arg, dmu_tx_t *tx)
369 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
370 zfeature_info_t *feature = arg;
372 spa_feature_incr(spa, feature, tx);
373 spa_history_log_internal(spa, "zhack feature incr", tx,
374 "name=%s", feature->fi_guid);
378 feature_decr_sync(void *arg, dmu_tx_t *tx)
380 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
381 zfeature_info_t *feature = arg;
383 spa_feature_decr(spa, feature, tx);
384 spa_history_log_internal(spa, "zhack feature decr", tx,
385 "name=%s", feature->fi_guid);
389 zhack_do_feature_ref(int argc, char **argv)
393 boolean_t decr = B_FALSE;
396 zfeature_info_t feature;
397 zfeature_info_t *nodeps[] = { NULL };
400 * fi_desc does not matter here because it was written to disk
401 * when the feature was enabled, but we need to properly set the
402 * feature for read or write based on the information we read off
405 feature.fi_uname = "zhack";
406 feature.fi_mos = B_FALSE;
407 feature.fi_desc = NULL;
408 feature.fi_depends = nodeps;
411 while ((c = getopt(argc, argv, "md")) != -1) {
414 feature.fi_mos = B_TRUE;
428 (void) fprintf(stderr, "error: missing feature or pool name\n");
432 feature.fi_guid = argv[1];
434 if (!zfeature_is_valid_guid(feature.fi_guid))
435 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
437 zhack_spa_open(target, B_FALSE, FTAG, &spa);
438 mos = spa->spa_meta_objset;
440 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
441 fatal(spa, FTAG, "'%s' is a real feature, will not change "
444 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
446 feature.fi_can_readonly = B_FALSE;
447 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
449 feature.fi_can_readonly = B_TRUE;
451 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
454 if (decr && !spa_feature_is_active(spa, &feature))
455 fatal(spa, FTAG, "feature refcount already 0: %s",
458 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
459 decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
461 spa_close(spa, FTAG);
465 zhack_do_feature(int argc, char **argv)
472 (void) fprintf(stderr,
473 "error: no feature operation specified\n");
477 subcommand = argv[0];
478 if (strcmp(subcommand, "stat") == 0) {
479 zhack_do_feature_stat(argc, argv);
480 } else if (strcmp(subcommand, "enable") == 0) {
481 zhack_do_feature_enable(argc, argv);
482 } else if (strcmp(subcommand, "ref") == 0) {
483 zhack_do_feature_ref(argc, argv);
485 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
493 #define MAX_NUM_PATHS 1024
496 main(int argc, char **argv)
498 extern void zfs_prop_init(void);
500 char *path[MAX_NUM_PATHS];
501 const char *subcommand;
505 g_importargs.path = path;
507 dprintf_setup(&argc, argv);
510 while ((c = getopt(argc, argv, "c:d:")) != -1) {
513 g_importargs.cachefile = optarg;
516 assert(g_importargs.paths < MAX_NUM_PATHS);
517 g_importargs.path[g_importargs.paths++] = optarg;
530 (void) fprintf(stderr, "error: no command specified\n");
534 subcommand = argv[0];
536 if (strcmp(subcommand, "feature") == 0) {
537 rv = zhack_do_feature(argc, argv);
539 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
544 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
545 fatal(NULL, FTAG, "pool export failed; "
546 "changes may not be committed to disk\n");