xref: /titanic_50/usr/src/cmd/zhack/zhack.c (revision a7a845e4bf22fd1b2a284729ccd95c7370a0438c)
153089ab7Seschrock /*
253089ab7Seschrock  * CDDL HEADER START
353089ab7Seschrock  *
453089ab7Seschrock  * The contents of this file are subject to the terms of the
553089ab7Seschrock  * Common Development and Distribution License (the "License").
653089ab7Seschrock  * You may not use this file except in compliance with the License.
753089ab7Seschrock  *
853089ab7Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
953089ab7Seschrock  * or http://www.opensolaris.org/os/licensing.
1053089ab7Seschrock  * See the License for the specific language governing permissions
1153089ab7Seschrock  * and limitations under the License.
1253089ab7Seschrock  *
1353089ab7Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
1453089ab7Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1553089ab7Seschrock  * If applicable, add the following below this CDDL HEADER, with the
1653089ab7Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
1753089ab7Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
1853089ab7Seschrock  *
1953089ab7Seschrock  * CDDL HEADER END
2053089ab7Seschrock  */
2153089ab7Seschrock 
2253089ab7Seschrock /*
2353089ab7Seschrock  * Copyright (c) 2012 by Delphix. All rights reserved.
24*a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
2553089ab7Seschrock  */
2653089ab7Seschrock 
2753089ab7Seschrock /*
2853089ab7Seschrock  * zhack is a debugging tool that can write changes to ZFS pool using libzpool
2953089ab7Seschrock  * for testing purposes. Altering pools with zhack is unsupported and may
3053089ab7Seschrock  * result in corrupted pools.
3153089ab7Seschrock  */
3253089ab7Seschrock 
3353089ab7Seschrock #include <stdio.h>
3453089ab7Seschrock #include <stdlib.h>
3553089ab7Seschrock #include <ctype.h>
3653089ab7Seschrock #include <sys/zfs_context.h>
3753089ab7Seschrock #include <sys/spa.h>
3853089ab7Seschrock #include <sys/spa_impl.h>
3953089ab7Seschrock #include <sys/dmu.h>
4053089ab7Seschrock #include <sys/zap.h>
4153089ab7Seschrock #include <sys/zfs_znode.h>
4253089ab7Seschrock #include <sys/dsl_synctask.h>
4353089ab7Seschrock #include <sys/vdev.h>
4453089ab7Seschrock #include <sys/fs/zfs.h>
4553089ab7Seschrock #include <sys/dmu_objset.h>
4653089ab7Seschrock #include <sys/dsl_pool.h>
4753089ab7Seschrock #include <sys/zio_checksum.h>
4853089ab7Seschrock #include <sys/zio_compress.h>
4953089ab7Seschrock #include <sys/zfeature.h>
503b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
5153089ab7Seschrock #undef ZFS_MAXNAMELEN
5253089ab7Seschrock #undef verify
5353089ab7Seschrock #include <libzfs.h>
5453089ab7Seschrock 
5553089ab7Seschrock extern boolean_t zfeature_checks_disable;
5653089ab7Seschrock 
5753089ab7Seschrock const char cmdname[] = "zhack";
5853089ab7Seschrock libzfs_handle_t *g_zfs;
5953089ab7Seschrock static importargs_t g_importargs;
6053089ab7Seschrock static char *g_pool;
6153089ab7Seschrock static boolean_t g_readonly;
6253089ab7Seschrock 
6353089ab7Seschrock static void
6453089ab7Seschrock usage(void)
6553089ab7Seschrock {
6653089ab7Seschrock 	(void) fprintf(stderr,
6753089ab7Seschrock 	    "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
6853089ab7Seschrock 	    "where <subcommand> <args> is one of the following:\n"
6953089ab7Seschrock 	    "\n", cmdname);
7053089ab7Seschrock 
7153089ab7Seschrock 	(void) fprintf(stderr,
7253089ab7Seschrock 	    "    feature stat <pool>\n"
7353089ab7Seschrock 	    "        print information about enabled features\n"
7453089ab7Seschrock 	    "    feature enable [-d desc] <pool> <feature>\n"
7553089ab7Seschrock 	    "        add a new enabled feature to the pool\n"
7653089ab7Seschrock 	    "        -d <desc> sets the feature's description\n"
7753089ab7Seschrock 	    "    feature ref [-md] <pool> <feature>\n"
7853089ab7Seschrock 	    "        change the refcount on the given feature\n"
7953089ab7Seschrock 	    "        -d decrease instead of increase the refcount\n"
8053089ab7Seschrock 	    "        -m add the feature to the label if increasing refcount\n"
8153089ab7Seschrock 	    "\n"
8253089ab7Seschrock 	    "    <feature> : should be a feature guid\n");
8353089ab7Seschrock 	exit(1);
8453089ab7Seschrock }
8553089ab7Seschrock 
8653089ab7Seschrock 
8753089ab7Seschrock static void
8853089ab7Seschrock fatal(const char *fmt, ...)
8953089ab7Seschrock {
9053089ab7Seschrock 	va_list ap;
9153089ab7Seschrock 
9253089ab7Seschrock 	va_start(ap, fmt);
9353089ab7Seschrock 	(void) fprintf(stderr, "%s: ", cmdname);
9453089ab7Seschrock 	(void) vfprintf(stderr, fmt, ap);
9553089ab7Seschrock 	va_end(ap);
9653089ab7Seschrock 	(void) fprintf(stderr, "\n");
9753089ab7Seschrock 
9853089ab7Seschrock 	exit(1);
9953089ab7Seschrock }
10053089ab7Seschrock 
10153089ab7Seschrock /* ARGSUSED */
10253089ab7Seschrock static int
10353089ab7Seschrock space_delta_cb(dmu_object_type_t bonustype, void *data,
10453089ab7Seschrock     uint64_t *userp, uint64_t *groupp)
10553089ab7Seschrock {
10653089ab7Seschrock 	/*
10753089ab7Seschrock 	 * Is it a valid type of object to track?
10853089ab7Seschrock 	 */
10953089ab7Seschrock 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
11053089ab7Seschrock 		return (ENOENT);
11153089ab7Seschrock 	(void) fprintf(stderr, "modifying object that needs user accounting");
11253089ab7Seschrock 	abort();
11353089ab7Seschrock 	/* NOTREACHED */
11453089ab7Seschrock }
11553089ab7Seschrock 
11653089ab7Seschrock /*
11753089ab7Seschrock  * Target is the dataset whose pool we want to open.
11853089ab7Seschrock  */
11953089ab7Seschrock static void
12053089ab7Seschrock import_pool(const char *target, boolean_t readonly)
12153089ab7Seschrock {
12253089ab7Seschrock 	nvlist_t *config;
12353089ab7Seschrock 	nvlist_t *pools;
12453089ab7Seschrock 	int error;
12553089ab7Seschrock 	char *sepp;
12653089ab7Seschrock 	spa_t *spa;
12753089ab7Seschrock 	nvpair_t *elem;
12853089ab7Seschrock 	nvlist_t *props;
12953089ab7Seschrock 	const char *name;
13053089ab7Seschrock 
13153089ab7Seschrock 	kernel_init(readonly ? FREAD : (FREAD | FWRITE));
13253089ab7Seschrock 	g_zfs = libzfs_init();
13353089ab7Seschrock 	ASSERT(g_zfs != NULL);
13453089ab7Seschrock 
13553089ab7Seschrock 	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
13653089ab7Seschrock 
13753089ab7Seschrock 	g_readonly = readonly;
13853089ab7Seschrock 
13953089ab7Seschrock 	/*
14053089ab7Seschrock 	 * If we only want readonly access, it's OK if we find
14153089ab7Seschrock 	 * a potentially-active (ie, imported into the kernel) pool from the
14253089ab7Seschrock 	 * default cachefile.
14353089ab7Seschrock 	 */
14453089ab7Seschrock 	if (readonly && spa_open(target, &spa, FTAG) == 0) {
14553089ab7Seschrock 		spa_close(spa, FTAG);
14653089ab7Seschrock 		return;
14753089ab7Seschrock 	}
14853089ab7Seschrock 
14953089ab7Seschrock 	g_importargs.unique = B_TRUE;
15053089ab7Seschrock 	g_importargs.can_be_active = readonly;
15153089ab7Seschrock 	g_pool = strdup(target);
15253089ab7Seschrock 	if ((sepp = strpbrk(g_pool, "/@")) != NULL)
15353089ab7Seschrock 		*sepp = '\0';
15453089ab7Seschrock 	g_importargs.poolname = g_pool;
15553089ab7Seschrock 	pools = zpool_search_import(g_zfs, &g_importargs);
15653089ab7Seschrock 
157*a7a845e4SSteven Hartland 	if (nvlist_empty(pools)) {
15853089ab7Seschrock 		if (!g_importargs.can_be_active) {
15953089ab7Seschrock 			g_importargs.can_be_active = B_TRUE;
16053089ab7Seschrock 			if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
16153089ab7Seschrock 			    spa_open(target, &spa, FTAG) == 0) {
16253089ab7Seschrock 				fatal("cannot import '%s': pool is active; run "
16353089ab7Seschrock 				    "\"zpool export %s\" first\n",
16453089ab7Seschrock 				    g_pool, g_pool);
16553089ab7Seschrock 			}
16653089ab7Seschrock 		}
16753089ab7Seschrock 
16853089ab7Seschrock 		fatal("cannot import '%s': no such pool available\n", g_pool);
16953089ab7Seschrock 	}
17053089ab7Seschrock 
17153089ab7Seschrock 	elem = nvlist_next_nvpair(pools, NULL);
17253089ab7Seschrock 	name = nvpair_name(elem);
17353089ab7Seschrock 	verify(nvpair_value_nvlist(elem, &config) == 0);
17453089ab7Seschrock 
17553089ab7Seschrock 	props = NULL;
17653089ab7Seschrock 	if (readonly) {
17753089ab7Seschrock 		verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
17853089ab7Seschrock 		verify(nvlist_add_uint64(props,
17953089ab7Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
18053089ab7Seschrock 	}
18153089ab7Seschrock 
18253089ab7Seschrock 	zfeature_checks_disable = B_TRUE;
18353089ab7Seschrock 	error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
18453089ab7Seschrock 	zfeature_checks_disable = B_FALSE;
18553089ab7Seschrock 	if (error == EEXIST)
18653089ab7Seschrock 		error = 0;
18753089ab7Seschrock 
18853089ab7Seschrock 	if (error)
18953089ab7Seschrock 		fatal("can't import '%s': %s", name, strerror(error));
19053089ab7Seschrock }
19153089ab7Seschrock 
19253089ab7Seschrock static void
19353089ab7Seschrock zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
19453089ab7Seschrock {
19553089ab7Seschrock 	int err;
19653089ab7Seschrock 
19753089ab7Seschrock 	import_pool(target, readonly);
19853089ab7Seschrock 
19953089ab7Seschrock 	zfeature_checks_disable = B_TRUE;
20053089ab7Seschrock 	err = spa_open(target, spa, tag);
20153089ab7Seschrock 	zfeature_checks_disable = B_FALSE;
20253089ab7Seschrock 
20353089ab7Seschrock 	if (err != 0)
20453089ab7Seschrock 		fatal("cannot open '%s': %s", target, strerror(err));
20553089ab7Seschrock 	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
20653089ab7Seschrock 		fatal("'%s' has version %d, features not enabled", target,
20753089ab7Seschrock 		    (int)spa_version(*spa));
20853089ab7Seschrock 	}
20953089ab7Seschrock }
21053089ab7Seschrock 
21153089ab7Seschrock static void
21253089ab7Seschrock dump_obj(objset_t *os, uint64_t obj, const char *name)
21353089ab7Seschrock {
21453089ab7Seschrock 	zap_cursor_t zc;
21553089ab7Seschrock 	zap_attribute_t za;
21653089ab7Seschrock 
21753089ab7Seschrock 	(void) printf("%s_obj:\n", name);
21853089ab7Seschrock 
21953089ab7Seschrock 	for (zap_cursor_init(&zc, os, obj);
22053089ab7Seschrock 	    zap_cursor_retrieve(&zc, &za) == 0;
22153089ab7Seschrock 	    zap_cursor_advance(&zc)) {
22253089ab7Seschrock 		if (za.za_integer_length == 8) {
22353089ab7Seschrock 			ASSERT(za.za_num_integers == 1);
22453089ab7Seschrock 			(void) printf("\t%s = %llu\n",
22553089ab7Seschrock 			    za.za_name, (u_longlong_t)za.za_first_integer);
22653089ab7Seschrock 		} else {
22753089ab7Seschrock 			ASSERT(za.za_integer_length == 1);
22853089ab7Seschrock 			char val[1024];
22953089ab7Seschrock 			VERIFY(zap_lookup(os, obj, za.za_name,
23053089ab7Seschrock 			    1, sizeof (val), val) == 0);
23153089ab7Seschrock 			(void) printf("\t%s = %s\n", za.za_name, val);
23253089ab7Seschrock 		}
23353089ab7Seschrock 	}
23453089ab7Seschrock 	zap_cursor_fini(&zc);
23553089ab7Seschrock }
23653089ab7Seschrock 
23753089ab7Seschrock static void
23853089ab7Seschrock dump_mos(spa_t *spa)
23953089ab7Seschrock {
24053089ab7Seschrock 	nvlist_t *nv = spa->spa_label_features;
24153089ab7Seschrock 
24253089ab7Seschrock 	(void) printf("label config:\n");
24353089ab7Seschrock 	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
24453089ab7Seschrock 	    pair != NULL;
24553089ab7Seschrock 	    pair = nvlist_next_nvpair(nv, pair)) {
24653089ab7Seschrock 		(void) printf("\t%s\n", nvpair_name(pair));
24753089ab7Seschrock 	}
24853089ab7Seschrock }
24953089ab7Seschrock 
25053089ab7Seschrock static void
25153089ab7Seschrock zhack_do_feature_stat(int argc, char **argv)
25253089ab7Seschrock {
25353089ab7Seschrock 	spa_t *spa;
25453089ab7Seschrock 	objset_t *os;
25553089ab7Seschrock 	char *target;
25653089ab7Seschrock 
25753089ab7Seschrock 	argc--;
25853089ab7Seschrock 	argv++;
25953089ab7Seschrock 
26053089ab7Seschrock 	if (argc < 1) {
26153089ab7Seschrock 		(void) fprintf(stderr, "error: missing pool name\n");
26253089ab7Seschrock 		usage();
26353089ab7Seschrock 	}
26453089ab7Seschrock 	target = argv[0];
26553089ab7Seschrock 
26653089ab7Seschrock 	zhack_spa_open(target, B_TRUE, FTAG, &spa);
26753089ab7Seschrock 	os = spa->spa_meta_objset;
26853089ab7Seschrock 
26953089ab7Seschrock 	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
27053089ab7Seschrock 	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
27153089ab7Seschrock 	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
27253089ab7Seschrock 	dump_mos(spa);
27353089ab7Seschrock 
27453089ab7Seschrock 	spa_close(spa, FTAG);
27553089ab7Seschrock }
27653089ab7Seschrock 
27753089ab7Seschrock static void
2783b2aab18SMatthew Ahrens feature_enable_sync(void *arg, dmu_tx_t *tx)
27953089ab7Seschrock {
2803b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2813b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
28253089ab7Seschrock 
28353089ab7Seschrock 	spa_feature_enable(spa, feature, tx);
2844445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack enable feature", tx,
28557221772SChristopher Siden 	    "guid=%s can_readonly=%u",
2864445fffbSMatthew Ahrens 	    feature->fi_guid, feature->fi_can_readonly);
28753089ab7Seschrock }
28853089ab7Seschrock 
28953089ab7Seschrock static void
29053089ab7Seschrock zhack_do_feature_enable(int argc, char **argv)
29153089ab7Seschrock {
29253089ab7Seschrock 	char c;
29353089ab7Seschrock 	char *desc, *target;
29453089ab7Seschrock 	spa_t *spa;
29553089ab7Seschrock 	objset_t *mos;
29653089ab7Seschrock 	zfeature_info_t feature;
29753089ab7Seschrock 	zfeature_info_t *nodeps[] = { NULL };
29853089ab7Seschrock 
29953089ab7Seschrock 	/*
30053089ab7Seschrock 	 * Features are not added to the pool's label until their refcounts
30153089ab7Seschrock 	 * are incremented, so fi_mos can just be left as false for now.
30253089ab7Seschrock 	 */
30353089ab7Seschrock 	desc = NULL;
30453089ab7Seschrock 	feature.fi_uname = "zhack";
30553089ab7Seschrock 	feature.fi_mos = B_FALSE;
30653089ab7Seschrock 	feature.fi_can_readonly = B_FALSE;
30753089ab7Seschrock 	feature.fi_depends = nodeps;
30853089ab7Seschrock 
30953089ab7Seschrock 	optind = 1;
31053089ab7Seschrock 	while ((c = getopt(argc, argv, "rmd:")) != -1) {
31153089ab7Seschrock 		switch (c) {
31253089ab7Seschrock 		case 'r':
31353089ab7Seschrock 			feature.fi_can_readonly = B_TRUE;
31453089ab7Seschrock 			break;
31553089ab7Seschrock 		case 'd':
31653089ab7Seschrock 			desc = strdup(optarg);
31753089ab7Seschrock 			break;
31853089ab7Seschrock 		default:
31953089ab7Seschrock 			usage();
32053089ab7Seschrock 			break;
32153089ab7Seschrock 		}
32253089ab7Seschrock 	}
32353089ab7Seschrock 
32453089ab7Seschrock 	if (desc == NULL)
32553089ab7Seschrock 		desc = strdup("zhack injected");
32653089ab7Seschrock 	feature.fi_desc = desc;
32753089ab7Seschrock 
32853089ab7Seschrock 	argc -= optind;
32953089ab7Seschrock 	argv += optind;
33053089ab7Seschrock 
33153089ab7Seschrock 	if (argc < 2) {
33253089ab7Seschrock 		(void) fprintf(stderr, "error: missing feature or pool name\n");
33353089ab7Seschrock 		usage();
33453089ab7Seschrock 	}
33553089ab7Seschrock 	target = argv[0];
33653089ab7Seschrock 	feature.fi_guid = argv[1];
33753089ab7Seschrock 
33853089ab7Seschrock 	if (!zfeature_is_valid_guid(feature.fi_guid))
33953089ab7Seschrock 		fatal("invalid feature guid: %s", feature.fi_guid);
34053089ab7Seschrock 
34153089ab7Seschrock 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
34253089ab7Seschrock 	mos = spa->spa_meta_objset;
34353089ab7Seschrock 
34453089ab7Seschrock 	if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
34553089ab7Seschrock 		fatal("'%s' is a real feature, will not enable");
34653089ab7Seschrock 	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
34753089ab7Seschrock 		fatal("feature already enabled: %s", feature.fi_guid);
34853089ab7Seschrock 
3493b2aab18SMatthew Ahrens 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3503b2aab18SMatthew Ahrens 	    feature_enable_sync, &feature, 5));
35153089ab7Seschrock 
35253089ab7Seschrock 	spa_close(spa, FTAG);
35353089ab7Seschrock 
35453089ab7Seschrock 	free(desc);
35553089ab7Seschrock }
35653089ab7Seschrock 
35753089ab7Seschrock static void
3583b2aab18SMatthew Ahrens feature_incr_sync(void *arg, dmu_tx_t *tx)
35953089ab7Seschrock {
3603b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3613b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
36253089ab7Seschrock 
36353089ab7Seschrock 	spa_feature_incr(spa, feature, tx);
3644445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack feature incr", tx,
36557221772SChristopher Siden 	    "guid=%s", feature->fi_guid);
36653089ab7Seschrock }
36753089ab7Seschrock 
36853089ab7Seschrock static void
3693b2aab18SMatthew Ahrens feature_decr_sync(void *arg, dmu_tx_t *tx)
37053089ab7Seschrock {
3713b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3723b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
37353089ab7Seschrock 
37453089ab7Seschrock 	spa_feature_decr(spa, feature, tx);
3754445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack feature decr", tx,
37657221772SChristopher Siden 	    "guid=%s", feature->fi_guid);
37753089ab7Seschrock }
37853089ab7Seschrock 
37953089ab7Seschrock static void
38053089ab7Seschrock zhack_do_feature_ref(int argc, char **argv)
38153089ab7Seschrock {
38253089ab7Seschrock 	char c;
38353089ab7Seschrock 	char *target;
38453089ab7Seschrock 	boolean_t decr = B_FALSE;
38553089ab7Seschrock 	spa_t *spa;
38653089ab7Seschrock 	objset_t *mos;
38753089ab7Seschrock 	zfeature_info_t feature;
38853089ab7Seschrock 	zfeature_info_t *nodeps[] = { NULL };
38953089ab7Seschrock 
39053089ab7Seschrock 	/*
39153089ab7Seschrock 	 * fi_desc does not matter here because it was written to disk
39253089ab7Seschrock 	 * when the feature was enabled, but we need to properly set the
39353089ab7Seschrock 	 * feature for read or write based on the information we read off
39453089ab7Seschrock 	 * disk later.
39553089ab7Seschrock 	 */
39653089ab7Seschrock 	feature.fi_uname = "zhack";
39753089ab7Seschrock 	feature.fi_mos = B_FALSE;
39853089ab7Seschrock 	feature.fi_desc = NULL;
39953089ab7Seschrock 	feature.fi_depends = nodeps;
40053089ab7Seschrock 
40153089ab7Seschrock 	optind = 1;
40253089ab7Seschrock 	while ((c = getopt(argc, argv, "md")) != -1) {
40353089ab7Seschrock 		switch (c) {
40453089ab7Seschrock 		case 'm':
40553089ab7Seschrock 			feature.fi_mos = B_TRUE;
40653089ab7Seschrock 			break;
40753089ab7Seschrock 		case 'd':
40853089ab7Seschrock 			decr = B_TRUE;
40953089ab7Seschrock 			break;
41053089ab7Seschrock 		default:
41153089ab7Seschrock 			usage();
41253089ab7Seschrock 			break;
41353089ab7Seschrock 		}
41453089ab7Seschrock 	}
41553089ab7Seschrock 	argc -= optind;
41653089ab7Seschrock 	argv += optind;
41753089ab7Seschrock 
41853089ab7Seschrock 	if (argc < 2) {
41953089ab7Seschrock 		(void) fprintf(stderr, "error: missing feature or pool name\n");
42053089ab7Seschrock 		usage();
42153089ab7Seschrock 	}
42253089ab7Seschrock 	target = argv[0];
42353089ab7Seschrock 	feature.fi_guid = argv[1];
42453089ab7Seschrock 
42553089ab7Seschrock 	if (!zfeature_is_valid_guid(feature.fi_guid))
42653089ab7Seschrock 		fatal("invalid feature guid: %s", feature.fi_guid);
42753089ab7Seschrock 
42853089ab7Seschrock 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
42953089ab7Seschrock 	mos = spa->spa_meta_objset;
43053089ab7Seschrock 
43153089ab7Seschrock 	if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
43253089ab7Seschrock 		fatal("'%s' is a real feature, will not change refcount");
43353089ab7Seschrock 
43453089ab7Seschrock 	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
43553089ab7Seschrock 	    feature.fi_guid)) {
43653089ab7Seschrock 		feature.fi_can_readonly = B_FALSE;
43753089ab7Seschrock 	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
43853089ab7Seschrock 	    feature.fi_guid)) {
43953089ab7Seschrock 		feature.fi_can_readonly = B_TRUE;
44053089ab7Seschrock 	} else {
44153089ab7Seschrock 		fatal("feature is not enabled: %s", feature.fi_guid);
44253089ab7Seschrock 	}
44353089ab7Seschrock 
44453089ab7Seschrock 	if (decr && !spa_feature_is_active(spa, &feature))
44553089ab7Seschrock 		fatal("feature refcount already 0: %s", feature.fi_guid);
44653089ab7Seschrock 
4473b2aab18SMatthew Ahrens 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
4483b2aab18SMatthew Ahrens 	    decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
44953089ab7Seschrock 
45053089ab7Seschrock 	spa_close(spa, FTAG);
45153089ab7Seschrock }
45253089ab7Seschrock 
45353089ab7Seschrock static int
45453089ab7Seschrock zhack_do_feature(int argc, char **argv)
45553089ab7Seschrock {
45653089ab7Seschrock 	char *subcommand;
45753089ab7Seschrock 
45853089ab7Seschrock 	argc--;
45953089ab7Seschrock 	argv++;
46053089ab7Seschrock 	if (argc == 0) {
46153089ab7Seschrock 		(void) fprintf(stderr,
46253089ab7Seschrock 		    "error: no feature operation specified\n");
46353089ab7Seschrock 		usage();
46453089ab7Seschrock 	}
46553089ab7Seschrock 
46653089ab7Seschrock 	subcommand = argv[0];
46753089ab7Seschrock 	if (strcmp(subcommand, "stat") == 0) {
46853089ab7Seschrock 		zhack_do_feature_stat(argc, argv);
46953089ab7Seschrock 	} else if (strcmp(subcommand, "enable") == 0) {
47053089ab7Seschrock 		zhack_do_feature_enable(argc, argv);
47153089ab7Seschrock 	} else if (strcmp(subcommand, "ref") == 0) {
47253089ab7Seschrock 		zhack_do_feature_ref(argc, argv);
47353089ab7Seschrock 	} else {
47453089ab7Seschrock 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
47553089ab7Seschrock 		    subcommand);
47653089ab7Seschrock 		usage();
47753089ab7Seschrock 	}
47853089ab7Seschrock 
47953089ab7Seschrock 	return (0);
48053089ab7Seschrock }
48153089ab7Seschrock 
48253089ab7Seschrock #define	MAX_NUM_PATHS 1024
48353089ab7Seschrock 
48453089ab7Seschrock int
48553089ab7Seschrock main(int argc, char **argv)
48653089ab7Seschrock {
48753089ab7Seschrock 	extern void zfs_prop_init(void);
48853089ab7Seschrock 
48953089ab7Seschrock 	char *path[MAX_NUM_PATHS];
49053089ab7Seschrock 	const char *subcommand;
49153089ab7Seschrock 	int rv = 0;
49253089ab7Seschrock 	char c;
49353089ab7Seschrock 
49453089ab7Seschrock 	g_importargs.path = path;
49553089ab7Seschrock 
49653089ab7Seschrock 	dprintf_setup(&argc, argv);
49753089ab7Seschrock 	zfs_prop_init();
49853089ab7Seschrock 
49953089ab7Seschrock 	while ((c = getopt(argc, argv, "c:d:")) != -1) {
50053089ab7Seschrock 		switch (c) {
50153089ab7Seschrock 		case 'c':
50253089ab7Seschrock 			g_importargs.cachefile = optarg;
50353089ab7Seschrock 			break;
50453089ab7Seschrock 		case 'd':
50553089ab7Seschrock 			assert(g_importargs.paths < MAX_NUM_PATHS);
50653089ab7Seschrock 			g_importargs.path[g_importargs.paths++] = optarg;
50753089ab7Seschrock 			break;
50853089ab7Seschrock 		default:
50953089ab7Seschrock 			usage();
51053089ab7Seschrock 			break;
51153089ab7Seschrock 		}
51253089ab7Seschrock 	}
51353089ab7Seschrock 
51453089ab7Seschrock 	argc -= optind;
51553089ab7Seschrock 	argv += optind;
51653089ab7Seschrock 	optind = 1;
51753089ab7Seschrock 
51853089ab7Seschrock 	if (argc == 0) {
51953089ab7Seschrock 		(void) fprintf(stderr, "error: no command specified\n");
52053089ab7Seschrock 		usage();
52153089ab7Seschrock 	}
52253089ab7Seschrock 
52353089ab7Seschrock 	subcommand = argv[0];
52453089ab7Seschrock 
52553089ab7Seschrock 	if (strcmp(subcommand, "feature") == 0) {
52653089ab7Seschrock 		rv = zhack_do_feature(argc, argv);
52753089ab7Seschrock 	} else {
52853089ab7Seschrock 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
52953089ab7Seschrock 		    subcommand);
53053089ab7Seschrock 		usage();
53153089ab7Seschrock 	}
53253089ab7Seschrock 
53353089ab7Seschrock 	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_TRUE) != 0) {
53453089ab7Seschrock 		fatal("pool export failed; "
53553089ab7Seschrock 		    "changes may not be committed to disk\n");
53653089ab7Seschrock 	}
53753089ab7Seschrock 
53853089ab7Seschrock 	libzfs_fini(g_zfs);
53953089ab7Seschrock 	kernel_fini();
54053089ab7Seschrock 
54153089ab7Seschrock 	return (rv);
54253089ab7Seschrock }
543