xref: /illumos-gate/usr/src/cmd/zhack/zhack.c (revision ef150c2b)
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 /*
23ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24a7a845e4SSteven 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 verify
52d8ab6e12SDon Brady #include <libzutil.h>
5353089ab7Seschrock 
5453089ab7Seschrock extern boolean_t zfeature_checks_disable;
5553089ab7Seschrock 
5653089ab7Seschrock const char cmdname[] = "zhack";
5753089ab7Seschrock static importargs_t g_importargs;
5853089ab7Seschrock static char *g_pool;
5953089ab7Seschrock static boolean_t g_readonly;
6053089ab7Seschrock 
6153089ab7Seschrock static void
usage(void)6253089ab7Seschrock usage(void)
6353089ab7Seschrock {
6453089ab7Seschrock 	(void) fprintf(stderr,
6553089ab7Seschrock 	    "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
6653089ab7Seschrock 	    "where <subcommand> <args> is one of the following:\n"
6753089ab7Seschrock 	    "\n", cmdname);
6853089ab7Seschrock 
6953089ab7Seschrock 	(void) fprintf(stderr,
7053089ab7Seschrock 	    "    feature stat <pool>\n"
7153089ab7Seschrock 	    "        print information about enabled features\n"
7253089ab7Seschrock 	    "    feature enable [-d desc] <pool> <feature>\n"
7353089ab7Seschrock 	    "        add a new enabled feature to the pool\n"
7453089ab7Seschrock 	    "        -d <desc> sets the feature's description\n"
7553089ab7Seschrock 	    "    feature ref [-md] <pool> <feature>\n"
7653089ab7Seschrock 	    "        change the refcount on the given feature\n"
7753089ab7Seschrock 	    "        -d decrease instead of increase the refcount\n"
7853089ab7Seschrock 	    "        -m add the feature to the label if increasing refcount\n"
7953089ab7Seschrock 	    "\n"
8053089ab7Seschrock 	    "    <feature> : should be a feature guid\n");
8153089ab7Seschrock 	exit(1);
8253089ab7Seschrock }
8353089ab7Seschrock 
8453089ab7Seschrock 
8553089ab7Seschrock static void
fatal(spa_t * spa,void * tag,const char * fmt,...)867fdd916cSGeorge Wilson fatal(spa_t *spa, void *tag, const char *fmt, ...)
8753089ab7Seschrock {
8853089ab7Seschrock 	va_list ap;
8953089ab7Seschrock 
907fdd916cSGeorge Wilson 	if (spa != NULL) {
917fdd916cSGeorge Wilson 		spa_close(spa, tag);
927fdd916cSGeorge Wilson 		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
937fdd916cSGeorge Wilson 	}
947fdd916cSGeorge Wilson 
9553089ab7Seschrock 	va_start(ap, fmt);
9653089ab7Seschrock 	(void) fprintf(stderr, "%s: ", cmdname);
9753089ab7Seschrock 	(void) vfprintf(stderr, fmt, ap);
9853089ab7Seschrock 	va_end(ap);
9953089ab7Seschrock 	(void) fprintf(stderr, "\n");
10053089ab7Seschrock 
10153089ab7Seschrock 	exit(1);
10253089ab7Seschrock }
10353089ab7Seschrock 
10453089ab7Seschrock /* ARGSUSED */
10553089ab7Seschrock static int
space_delta_cb(dmu_object_type_t bonustype,void * data,uint64_t * userp,uint64_t * groupp,uint64_t * projectp)10653089ab7Seschrock space_delta_cb(dmu_object_type_t bonustype, void *data,
107f67950b2SNasf-Fan     uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
10853089ab7Seschrock {
10953089ab7Seschrock 	/*
11053089ab7Seschrock 	 * Is it a valid type of object to track?
11153089ab7Seschrock 	 */
11253089ab7Seschrock 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
11353089ab7Seschrock 		return (ENOENT);
11453089ab7Seschrock 	(void) fprintf(stderr, "modifying object that needs user accounting");
11553089ab7Seschrock 	abort();
11653089ab7Seschrock 	/* NOTREACHED */
11753089ab7Seschrock }
11853089ab7Seschrock 
11953089ab7Seschrock /*
12053089ab7Seschrock  * Target is the dataset whose pool we want to open.
12153089ab7Seschrock  */
12253089ab7Seschrock static void
zhack_import(char * target,boolean_t readonly)123e0f1c0afSOlaf Faaland zhack_import(char *target, boolean_t readonly)
12453089ab7Seschrock {
12553089ab7Seschrock 	nvlist_t *config;
12653089ab7Seschrock 	nvlist_t *props;
127e0f1c0afSOlaf Faaland 	int error;
12853089ab7Seschrock 
12953089ab7Seschrock 	kernel_init(readonly ? FREAD : (FREAD | FWRITE));
13053089ab7Seschrock 
13153089ab7Seschrock 	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
13253089ab7Seschrock 
13353089ab7Seschrock 	g_readonly = readonly;
13453089ab7Seschrock 	g_importargs.can_be_active = readonly;
13553089ab7Seschrock 	g_pool = strdup(target);
13653089ab7Seschrock 
137d8ab6e12SDon Brady 	error = zpool_find_config(NULL, target, &config, &g_importargs,
138d8ab6e12SDon Brady 	    &libzpool_config_ops);
139e0f1c0afSOlaf Faaland 	if (error)
140d8ab6e12SDon Brady 		fatal(NULL, FTAG, "cannot import '%s'", target);
14153089ab7Seschrock 
14253089ab7Seschrock 	props = NULL;
14353089ab7Seschrock 	if (readonly) {
144e0f1c0afSOlaf Faaland 		VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
145e0f1c0afSOlaf Faaland 		VERIFY(nvlist_add_uint64(props,
14653089ab7Seschrock 		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
14753089ab7Seschrock 	}
14853089ab7Seschrock 
14953089ab7Seschrock 	zfeature_checks_disable = B_TRUE;
150e0f1c0afSOlaf Faaland 	error = spa_import(target, config, props,
151e0f1c0afSOlaf Faaland 	    (readonly ?  ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
15253089ab7Seschrock 	zfeature_checks_disable = B_FALSE;
15353089ab7Seschrock 	if (error == EEXIST)
15453089ab7Seschrock 		error = 0;
15553089ab7Seschrock 
15653089ab7Seschrock 	if (error)
157e0f1c0afSOlaf Faaland 		fatal(NULL, FTAG, "can't import '%s': %s", target,
1587fdd916cSGeorge Wilson 		    strerror(error));
15953089ab7Seschrock }
16053089ab7Seschrock 
16153089ab7Seschrock static void
zhack_spa_open(char * target,boolean_t readonly,void * tag,spa_t ** spa)162e0f1c0afSOlaf Faaland zhack_spa_open(char *target, boolean_t readonly, void *tag, spa_t **spa)
16353089ab7Seschrock {
16453089ab7Seschrock 	int err;
16553089ab7Seschrock 
166e0f1c0afSOlaf Faaland 	zhack_import(target, readonly);
16753089ab7Seschrock 
16853089ab7Seschrock 	zfeature_checks_disable = B_TRUE;
16953089ab7Seschrock 	err = spa_open(target, spa, tag);
17053089ab7Seschrock 	zfeature_checks_disable = B_FALSE;
17153089ab7Seschrock 
17253089ab7Seschrock 	if (err != 0)
1737fdd916cSGeorge Wilson 		fatal(*spa, FTAG, "cannot open '%s': %s", target,
1747fdd916cSGeorge Wilson 		    strerror(err));
17553089ab7Seschrock 	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
1767fdd916cSGeorge Wilson 		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
1777fdd916cSGeorge Wilson 		    target, (int)spa_version(*spa));
17853089ab7Seschrock 	}
17953089ab7Seschrock }
18053089ab7Seschrock 
18153089ab7Seschrock static void
dump_obj(objset_t * os,uint64_t obj,const char * name)18253089ab7Seschrock dump_obj(objset_t *os, uint64_t obj, const char *name)
18353089ab7Seschrock {
18453089ab7Seschrock 	zap_cursor_t zc;
18553089ab7Seschrock 	zap_attribute_t za;
18653089ab7Seschrock 
18753089ab7Seschrock 	(void) printf("%s_obj:\n", name);
18853089ab7Seschrock 
18953089ab7Seschrock 	for (zap_cursor_init(&zc, os, obj);
19053089ab7Seschrock 	    zap_cursor_retrieve(&zc, &za) == 0;
19153089ab7Seschrock 	    zap_cursor_advance(&zc)) {
19253089ab7Seschrock 		if (za.za_integer_length == 8) {
19353089ab7Seschrock 			ASSERT(za.za_num_integers == 1);
19453089ab7Seschrock 			(void) printf("\t%s = %llu\n",
19553089ab7Seschrock 			    za.za_name, (u_longlong_t)za.za_first_integer);
19653089ab7Seschrock 		} else {
19753089ab7Seschrock 			ASSERT(za.za_integer_length == 1);
19853089ab7Seschrock 			char val[1024];
19953089ab7Seschrock 			VERIFY(zap_lookup(os, obj, za.za_name,
20053089ab7Seschrock 			    1, sizeof (val), val) == 0);
20153089ab7Seschrock 			(void) printf("\t%s = %s\n", za.za_name, val);
20253089ab7Seschrock 		}
20353089ab7Seschrock 	}
20453089ab7Seschrock 	zap_cursor_fini(&zc);
20553089ab7Seschrock }
20653089ab7Seschrock 
20753089ab7Seschrock static void
dump_mos(spa_t * spa)20853089ab7Seschrock dump_mos(spa_t *spa)
20953089ab7Seschrock {
21053089ab7Seschrock 	nvlist_t *nv = spa->spa_label_features;
21153089ab7Seschrock 
21253089ab7Seschrock 	(void) printf("label config:\n");
21353089ab7Seschrock 	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
21453089ab7Seschrock 	    pair != NULL;
21553089ab7Seschrock 	    pair = nvlist_next_nvpair(nv, pair)) {
21653089ab7Seschrock 		(void) printf("\t%s\n", nvpair_name(pair));
21753089ab7Seschrock 	}
21853089ab7Seschrock }
21953089ab7Seschrock 
22053089ab7Seschrock static void
zhack_do_feature_stat(int argc,char ** argv)22153089ab7Seschrock zhack_do_feature_stat(int argc, char **argv)
22253089ab7Seschrock {
22353089ab7Seschrock 	spa_t *spa;
22453089ab7Seschrock 	objset_t *os;
22553089ab7Seschrock 	char *target;
22653089ab7Seschrock 
22753089ab7Seschrock 	argc--;
22853089ab7Seschrock 	argv++;
22953089ab7Seschrock 
23053089ab7Seschrock 	if (argc < 1) {
23153089ab7Seschrock 		(void) fprintf(stderr, "error: missing pool name\n");
23253089ab7Seschrock 		usage();
23353089ab7Seschrock 	}
23453089ab7Seschrock 	target = argv[0];
23553089ab7Seschrock 
23653089ab7Seschrock 	zhack_spa_open(target, B_TRUE, FTAG, &spa);
23753089ab7Seschrock 	os = spa->spa_meta_objset;
23853089ab7Seschrock 
23953089ab7Seschrock 	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
24053089ab7Seschrock 	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
24153089ab7Seschrock 	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
24243466aaeSMax Grossman 	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
24343466aaeSMax Grossman 		dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
24443466aaeSMax Grossman 	}
24553089ab7Seschrock 	dump_mos(spa);
24653089ab7Seschrock 
24753089ab7Seschrock 	spa_close(spa, FTAG);
24853089ab7Seschrock }
24953089ab7Seschrock 
25053089ab7Seschrock static void
zhack_feature_enable_sync(void * arg,dmu_tx_t * tx)2512acef22dSMatthew Ahrens zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
25253089ab7Seschrock {
2533b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2543b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
25553089ab7Seschrock 
2562acef22dSMatthew Ahrens 	feature_enable_sync(spa, feature, tx);
2572acef22dSMatthew Ahrens 
2584445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack enable feature", tx,
259ca0cc391SMatthew Ahrens 	    "guid=%s flags=%x",
260ca0cc391SMatthew Ahrens 	    feature->fi_guid, feature->fi_flags);
26153089ab7Seschrock }
26253089ab7Seschrock 
26353089ab7Seschrock static void
zhack_do_feature_enable(int argc,char ** argv)26453089ab7Seschrock zhack_do_feature_enable(int argc, char **argv)
26553089ab7Seschrock {
266*ef150c2bSRichard Lowe 	int c;
26753089ab7Seschrock 	char *desc, *target;
26853089ab7Seschrock 	spa_t *spa;
26953089ab7Seschrock 	objset_t *mos;
27053089ab7Seschrock 	zfeature_info_t feature;
2712acef22dSMatthew Ahrens 	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
27253089ab7Seschrock 
27353089ab7Seschrock 	/*
27453089ab7Seschrock 	 * Features are not added to the pool's label until their refcounts
27553089ab7Seschrock 	 * are incremented, so fi_mos can just be left as false for now.
27653089ab7Seschrock 	 */
27753089ab7Seschrock 	desc = NULL;
27853089ab7Seschrock 	feature.fi_uname = "zhack";
279ca0cc391SMatthew Ahrens 	feature.fi_flags = 0;
28053089ab7Seschrock 	feature.fi_depends = nodeps;
28143466aaeSMax Grossman 	feature.fi_feature = SPA_FEATURE_NONE;
28253089ab7Seschrock 
28353089ab7Seschrock 	optind = 1;
28453089ab7Seschrock 	while ((c = getopt(argc, argv, "rmd:")) != -1) {
28553089ab7Seschrock 		switch (c) {
28653089ab7Seschrock 		case 'r':
287ca0cc391SMatthew Ahrens 			feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
28853089ab7Seschrock 			break;
28953089ab7Seschrock 		case 'd':
29053089ab7Seschrock 			desc = strdup(optarg);
29153089ab7Seschrock 			break;
29253089ab7Seschrock 		default:
29353089ab7Seschrock 			usage();
29453089ab7Seschrock 			break;
29553089ab7Seschrock 		}
29653089ab7Seschrock 	}
29753089ab7Seschrock 
29853089ab7Seschrock 	if (desc == NULL)
29953089ab7Seschrock 		desc = strdup("zhack injected");
30053089ab7Seschrock 	feature.fi_desc = desc;
30153089ab7Seschrock 
30253089ab7Seschrock 	argc -= optind;
30353089ab7Seschrock 	argv += optind;
30453089ab7Seschrock 
30553089ab7Seschrock 	if (argc < 2) {
30653089ab7Seschrock 		(void) fprintf(stderr, "error: missing feature or pool name\n");
30753089ab7Seschrock 		usage();
30853089ab7Seschrock 	}
30953089ab7Seschrock 	target = argv[0];
31053089ab7Seschrock 	feature.fi_guid = argv[1];
31153089ab7Seschrock 
31253089ab7Seschrock 	if (!zfeature_is_valid_guid(feature.fi_guid))
3137fdd916cSGeorge Wilson 		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
31453089ab7Seschrock 
31553089ab7Seschrock 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
31653089ab7Seschrock 	mos = spa->spa_meta_objset;
31753089ab7Seschrock 
3182acef22dSMatthew Ahrens 	if (zfeature_is_supported(feature.fi_guid))
3197fdd916cSGeorge Wilson 		fatal(spa, FTAG, "'%s' is a real feature, will not enable");
32053089ab7Seschrock 	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
3217fdd916cSGeorge Wilson 		fatal(spa, FTAG, "feature already enabled: %s",
3227fdd916cSGeorge Wilson 		    feature.fi_guid);
32353089ab7Seschrock 
3243b2aab18SMatthew Ahrens 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3257d46dc6cSMatthew Ahrens 	    zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
32653089ab7Seschrock 
32753089ab7Seschrock 	spa_close(spa, FTAG);
32853089ab7Seschrock 
32953089ab7Seschrock 	free(desc);
33053089ab7Seschrock }
33153089ab7Seschrock 
33253089ab7Seschrock static void
feature_incr_sync(void * arg,dmu_tx_t * tx)3333b2aab18SMatthew Ahrens feature_incr_sync(void *arg, dmu_tx_t *tx)
33453089ab7Seschrock {
3353b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3363b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
3372acef22dSMatthew Ahrens 	uint64_t refcount;
33853089ab7Seschrock 
33943466aaeSMax Grossman 	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
3402acef22dSMatthew Ahrens 	feature_sync(spa, feature, refcount + 1, tx);
3414445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack feature incr", tx,
34257221772SChristopher Siden 	    "guid=%s", feature->fi_guid);
34353089ab7Seschrock }
34453089ab7Seschrock 
34553089ab7Seschrock static void
feature_decr_sync(void * arg,dmu_tx_t * tx)3463b2aab18SMatthew Ahrens feature_decr_sync(void *arg, dmu_tx_t *tx)
34753089ab7Seschrock {
3483b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3493b2aab18SMatthew Ahrens 	zfeature_info_t *feature = arg;
3502acef22dSMatthew Ahrens 	uint64_t refcount;
35153089ab7Seschrock 
35243466aaeSMax Grossman 	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
3532acef22dSMatthew Ahrens 	feature_sync(spa, feature, refcount - 1, tx);
3544445fffbSMatthew Ahrens 	spa_history_log_internal(spa, "zhack feature decr", tx,
35557221772SChristopher Siden 	    "guid=%s", feature->fi_guid);
35653089ab7Seschrock }
35753089ab7Seschrock 
35853089ab7Seschrock static void
zhack_do_feature_ref(int argc,char ** argv)35953089ab7Seschrock zhack_do_feature_ref(int argc, char **argv)
36053089ab7Seschrock {
361*ef150c2bSRichard Lowe 	int c;
36253089ab7Seschrock 	char *target;
36353089ab7Seschrock 	boolean_t decr = B_FALSE;
36453089ab7Seschrock 	spa_t *spa;
36553089ab7Seschrock 	objset_t *mos;
36653089ab7Seschrock 	zfeature_info_t feature;
3672acef22dSMatthew Ahrens 	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
36853089ab7Seschrock 
36953089ab7Seschrock 	/*
37053089ab7Seschrock 	 * fi_desc does not matter here because it was written to disk
37153089ab7Seschrock 	 * when the feature was enabled, but we need to properly set the
37253089ab7Seschrock 	 * feature for read or write based on the information we read off
37353089ab7Seschrock 	 * disk later.
37453089ab7Seschrock 	 */
37553089ab7Seschrock 	feature.fi_uname = "zhack";
376ca0cc391SMatthew Ahrens 	feature.fi_flags = 0;
37753089ab7Seschrock 	feature.fi_desc = NULL;
37853089ab7Seschrock 	feature.fi_depends = nodeps;
37943466aaeSMax Grossman 	feature.fi_feature = SPA_FEATURE_NONE;
38053089ab7Seschrock 
38153089ab7Seschrock 	optind = 1;
38253089ab7Seschrock 	while ((c = getopt(argc, argv, "md")) != -1) {
38353089ab7Seschrock 		switch (c) {
38453089ab7Seschrock 		case 'm':
385ca0cc391SMatthew Ahrens 			feature.fi_flags |= ZFEATURE_FLAG_MOS;
38653089ab7Seschrock 			break;
38753089ab7Seschrock 		case 'd':
38853089ab7Seschrock 			decr = B_TRUE;
38953089ab7Seschrock 			break;
39053089ab7Seschrock 		default:
39153089ab7Seschrock 			usage();
39253089ab7Seschrock 			break;
39353089ab7Seschrock 		}
39453089ab7Seschrock 	}
39553089ab7Seschrock 	argc -= optind;
39653089ab7Seschrock 	argv += optind;
39753089ab7Seschrock 
39853089ab7Seschrock 	if (argc < 2) {
39953089ab7Seschrock 		(void) fprintf(stderr, "error: missing feature or pool name\n");
40053089ab7Seschrock 		usage();
40153089ab7Seschrock 	}
40253089ab7Seschrock 	target = argv[0];
40353089ab7Seschrock 	feature.fi_guid = argv[1];
40453089ab7Seschrock 
40553089ab7Seschrock 	if (!zfeature_is_valid_guid(feature.fi_guid))
4067fdd916cSGeorge Wilson 		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
40753089ab7Seschrock 
40853089ab7Seschrock 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
40953089ab7Seschrock 	mos = spa->spa_meta_objset;
41053089ab7Seschrock 
4112acef22dSMatthew Ahrens 	if (zfeature_is_supported(feature.fi_guid)) {
4122acef22dSMatthew Ahrens 		fatal(spa, FTAG,
4132acef22dSMatthew Ahrens 		    "'%s' is a real feature, will not change refcount");
4142acef22dSMatthew Ahrens 	}
41553089ab7Seschrock 
41653089ab7Seschrock 	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
41753089ab7Seschrock 	    feature.fi_guid)) {
418ca0cc391SMatthew Ahrens 		feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
41953089ab7Seschrock 	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
42053089ab7Seschrock 	    feature.fi_guid)) {
421ca0cc391SMatthew Ahrens 		feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
42253089ab7Seschrock 	} else {
4237fdd916cSGeorge Wilson 		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
42453089ab7Seschrock 	}
42553089ab7Seschrock 
4262acef22dSMatthew Ahrens 	if (decr) {
4272acef22dSMatthew Ahrens 		uint64_t count;
42843466aaeSMax Grossman 		if (feature_get_refcount_from_disk(spa, &feature,
42943466aaeSMax Grossman 		    &count) == 0 && count != 0) {
4302acef22dSMatthew Ahrens 			fatal(spa, FTAG, "feature refcount already 0: %s",
4312acef22dSMatthew Ahrens 			    feature.fi_guid);
4322acef22dSMatthew Ahrens 		}
4332acef22dSMatthew Ahrens 	}
43453089ab7Seschrock 
4353b2aab18SMatthew Ahrens 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
4367d46dc6cSMatthew Ahrens 	    decr ? feature_decr_sync : feature_incr_sync, &feature,
4377d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
43853089ab7Seschrock 
43953089ab7Seschrock 	spa_close(spa, FTAG);
44053089ab7Seschrock }
44153089ab7Seschrock 
44253089ab7Seschrock static int
zhack_do_feature(int argc,char ** argv)44353089ab7Seschrock zhack_do_feature(int argc, char **argv)
44453089ab7Seschrock {
44553089ab7Seschrock 	char *subcommand;
44653089ab7Seschrock 
44753089ab7Seschrock 	argc--;
44853089ab7Seschrock 	argv++;
44953089ab7Seschrock 	if (argc == 0) {
45053089ab7Seschrock 		(void) fprintf(stderr,
45153089ab7Seschrock 		    "error: no feature operation specified\n");
45253089ab7Seschrock 		usage();
45353089ab7Seschrock 	}
45453089ab7Seschrock 
45553089ab7Seschrock 	subcommand = argv[0];
45653089ab7Seschrock 	if (strcmp(subcommand, "stat") == 0) {
45753089ab7Seschrock 		zhack_do_feature_stat(argc, argv);
45853089ab7Seschrock 	} else if (strcmp(subcommand, "enable") == 0) {
45953089ab7Seschrock 		zhack_do_feature_enable(argc, argv);
46053089ab7Seschrock 	} else if (strcmp(subcommand, "ref") == 0) {
46153089ab7Seschrock 		zhack_do_feature_ref(argc, argv);
46253089ab7Seschrock 	} else {
46353089ab7Seschrock 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
46453089ab7Seschrock 		    subcommand);
46553089ab7Seschrock 		usage();
46653089ab7Seschrock 	}
46753089ab7Seschrock 
46853089ab7Seschrock 	return (0);
46953089ab7Seschrock }
47053089ab7Seschrock 
47153089ab7Seschrock #define	MAX_NUM_PATHS 1024
47253089ab7Seschrock 
47353089ab7Seschrock int
main(int argc,char ** argv)47453089ab7Seschrock main(int argc, char **argv)
47553089ab7Seschrock {
47653089ab7Seschrock 	extern void zfs_prop_init(void);
47753089ab7Seschrock 
47853089ab7Seschrock 	char *path[MAX_NUM_PATHS];
47953089ab7Seschrock 	const char *subcommand;
48053089ab7Seschrock 	int rv = 0;
481*ef150c2bSRichard Lowe 	int c;
48253089ab7Seschrock 
48353089ab7Seschrock 	g_importargs.path = path;
48453089ab7Seschrock 
48553089ab7Seschrock 	dprintf_setup(&argc, argv);
48653089ab7Seschrock 	zfs_prop_init();
48753089ab7Seschrock 
48853089ab7Seschrock 	while ((c = getopt(argc, argv, "c:d:")) != -1) {
48953089ab7Seschrock 		switch (c) {
49053089ab7Seschrock 		case 'c':
49153089ab7Seschrock 			g_importargs.cachefile = optarg;
49253089ab7Seschrock 			break;
49353089ab7Seschrock 		case 'd':
49453089ab7Seschrock 			assert(g_importargs.paths < MAX_NUM_PATHS);
49553089ab7Seschrock 			g_importargs.path[g_importargs.paths++] = optarg;
49653089ab7Seschrock 			break;
49753089ab7Seschrock 		default:
49853089ab7Seschrock 			usage();
49953089ab7Seschrock 			break;
50053089ab7Seschrock 		}
50153089ab7Seschrock 	}
50253089ab7Seschrock 
50353089ab7Seschrock 	argc -= optind;
50453089ab7Seschrock 	argv += optind;
50553089ab7Seschrock 	optind = 1;
50653089ab7Seschrock 
50753089ab7Seschrock 	if (argc == 0) {
50853089ab7Seschrock 		(void) fprintf(stderr, "error: no command specified\n");
50953089ab7Seschrock 		usage();
51053089ab7Seschrock 	}
51153089ab7Seschrock 
51253089ab7Seschrock 	subcommand = argv[0];
51353089ab7Seschrock 
51453089ab7Seschrock 	if (strcmp(subcommand, "feature") == 0) {
51553089ab7Seschrock 		rv = zhack_do_feature(argc, argv);
51653089ab7Seschrock 	} else {
51753089ab7Seschrock 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
51853089ab7Seschrock 		    subcommand);
51953089ab7Seschrock 		usage();
52053089ab7Seschrock 	}
52153089ab7Seschrock 
5227fdd916cSGeorge Wilson 	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
5237fdd916cSGeorge Wilson 		fatal(NULL, FTAG, "pool export failed; "
52453089ab7Seschrock 		    "changes may not be committed to disk\n");
52553089ab7Seschrock 	}
52653089ab7Seschrock 
52753089ab7Seschrock 	kernel_fini();
52853089ab7Seschrock 
52953089ab7Seschrock 	return (rv);
53053089ab7Seschrock }
531