1236884Smm/*
2236884Smm * CDDL HEADER START
3236884Smm *
4236884Smm * The contents of this file are subject to the terms of the
5236884Smm * Common Development and Distribution License (the "License").
6236884Smm * You may not use this file except in compliance with the License.
7236884Smm *
8236884Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9236884Smm * or http://www.opensolaris.org/os/licensing.
10236884Smm * See the License for the specific language governing permissions
11236884Smm * and limitations under the License.
12236884Smm *
13236884Smm * When distributing Covered Code, include this CDDL HEADER in each
14236884Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15236884Smm * If applicable, add the following below this CDDL HEADER, with the
16236884Smm * fields enclosed by brackets "[]" replaced with your own identifying
17236884Smm * information: Portions Copyright [yyyy] [name of copyright owner]
18236884Smm *
19236884Smm * CDDL HEADER END
20236884Smm */
21236884Smm
22236884Smm/*
23288572Smav * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24251646Sdelphij * Copyright (c) 2013 Steven Hartland. All rights reserved.
25236884Smm */
26236884Smm
27236884Smm/*
28236884Smm * zhack is a debugging tool that can write changes to ZFS pool using libzpool
29236884Smm * for testing purposes. Altering pools with zhack is unsupported and may
30236884Smm * result in corrupted pools.
31236884Smm */
32236884Smm
33236884Smm#include <stdio.h>
34236884Smm#include <stdlib.h>
35236884Smm#include <ctype.h>
36236884Smm#include <sys/zfs_context.h>
37236884Smm#include <sys/spa.h>
38236884Smm#include <sys/spa_impl.h>
39236884Smm#include <sys/dmu.h>
40236884Smm#include <sys/zap.h>
41236884Smm#include <sys/zfs_znode.h>
42236884Smm#include <sys/dsl_synctask.h>
43236884Smm#include <sys/vdev.h>
44236884Smm#include <sys/fs/zfs.h>
45236884Smm#include <sys/dmu_objset.h>
46236884Smm#include <sys/dsl_pool.h>
47236884Smm#include <sys/zio_checksum.h>
48236884Smm#include <sys/zio_compress.h>
49236884Smm#include <sys/zfeature.h>
50248571Smm#include <sys/dmu_tx.h>
51236884Smm#undef verify
52236884Smm#include <libzfs.h>
53236884Smm
54236884Smmextern boolean_t zfeature_checks_disable;
55236884Smm
56236884Smmconst char cmdname[] = "zhack";
57236884Smmlibzfs_handle_t *g_zfs;
58236884Smmstatic importargs_t g_importargs;
59236884Smmstatic char *g_pool;
60236884Smmstatic boolean_t g_readonly;
61236884Smm
62236884Smmstatic void
63236884Smmusage(void)
64236884Smm{
65236884Smm	(void) fprintf(stderr,
66236884Smm	    "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
67236884Smm	    "where <subcommand> <args> is one of the following:\n"
68236884Smm	    "\n", cmdname);
69236884Smm
70236884Smm	(void) fprintf(stderr,
71236884Smm	    "    feature stat <pool>\n"
72236884Smm	    "        print information about enabled features\n"
73236884Smm	    "    feature enable [-d desc] <pool> <feature>\n"
74236884Smm	    "        add a new enabled feature to the pool\n"
75236884Smm	    "        -d <desc> sets the feature's description\n"
76236884Smm	    "    feature ref [-md] <pool> <feature>\n"
77236884Smm	    "        change the refcount on the given feature\n"
78236884Smm	    "        -d decrease instead of increase the refcount\n"
79236884Smm	    "        -m add the feature to the label if increasing refcount\n"
80236884Smm	    "\n"
81236884Smm	    "    <feature> : should be a feature guid\n");
82236884Smm	exit(1);
83236884Smm}
84236884Smm
85236884Smm
86236884Smmstatic void
87260617Sdelphijfatal(spa_t *spa, void *tag, const char *fmt, ...)
88236884Smm{
89236884Smm	va_list ap;
90236884Smm
91260617Sdelphij	if (spa != NULL) {
92260617Sdelphij		spa_close(spa, tag);
93260617Sdelphij		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
94260617Sdelphij	}
95260617Sdelphij
96236884Smm	va_start(ap, fmt);
97236884Smm	(void) fprintf(stderr, "%s: ", cmdname);
98236884Smm	(void) vfprintf(stderr, fmt, ap);
99236884Smm	va_end(ap);
100236884Smm	(void) fprintf(stderr, "\n");
101236884Smm
102236884Smm	exit(1);
103236884Smm}
104236884Smm
105236884Smm/* ARGSUSED */
106236884Smmstatic int
107236884Smmspace_delta_cb(dmu_object_type_t bonustype, void *data,
108236884Smm    uint64_t *userp, uint64_t *groupp)
109236884Smm{
110236884Smm	/*
111236884Smm	 * Is it a valid type of object to track?
112236884Smm	 */
113236884Smm	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
114236884Smm		return (ENOENT);
115236884Smm	(void) fprintf(stderr, "modifying object that needs user accounting");
116236884Smm	abort();
117236884Smm	/* NOTREACHED */
118236884Smm}
119236884Smm
120236884Smm/*
121236884Smm * Target is the dataset whose pool we want to open.
122236884Smm */
123236884Smmstatic void
124236884Smmimport_pool(const char *target, boolean_t readonly)
125236884Smm{
126236884Smm	nvlist_t *config;
127236884Smm	nvlist_t *pools;
128236884Smm	int error;
129236884Smm	char *sepp;
130236884Smm	spa_t *spa;
131236884Smm	nvpair_t *elem;
132236884Smm	nvlist_t *props;
133236884Smm	const char *name;
134236884Smm
135236884Smm	kernel_init(readonly ? FREAD : (FREAD | FWRITE));
136236884Smm	g_zfs = libzfs_init();
137236884Smm	ASSERT(g_zfs != NULL);
138236884Smm
139236884Smm	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
140236884Smm
141236884Smm	g_readonly = readonly;
142236884Smm
143236884Smm	/*
144236884Smm	 * If we only want readonly access, it's OK if we find
145236884Smm	 * a potentially-active (ie, imported into the kernel) pool from the
146236884Smm	 * default cachefile.
147236884Smm	 */
148236884Smm	if (readonly && spa_open(target, &spa, FTAG) == 0) {
149236884Smm		spa_close(spa, FTAG);
150236884Smm		return;
151236884Smm	}
152236884Smm
153236884Smm	g_importargs.unique = B_TRUE;
154236884Smm	g_importargs.can_be_active = readonly;
155236884Smm	g_pool = strdup(target);
156236884Smm	if ((sepp = strpbrk(g_pool, "/@")) != NULL)
157236884Smm		*sepp = '\0';
158236884Smm	g_importargs.poolname = g_pool;
159236884Smm	pools = zpool_search_import(g_zfs, &g_importargs);
160236884Smm
161251646Sdelphij	if (nvlist_empty(pools)) {
162236884Smm		if (!g_importargs.can_be_active) {
163236884Smm			g_importargs.can_be_active = B_TRUE;
164236884Smm			if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
165236884Smm			    spa_open(target, &spa, FTAG) == 0) {
166260617Sdelphij				fatal(spa, FTAG, "cannot import '%s': pool is "
167260617Sdelphij				    "active; run " "\"zpool export %s\" "
168260617Sdelphij				    "first\n", g_pool, g_pool);
169236884Smm			}
170236884Smm		}
171236884Smm
172260617Sdelphij		fatal(NULL, FTAG, "cannot import '%s': no such pool "
173260617Sdelphij		    "available\n", g_pool);
174236884Smm	}
175236884Smm
176236884Smm	elem = nvlist_next_nvpair(pools, NULL);
177236884Smm	name = nvpair_name(elem);
178236884Smm	verify(nvpair_value_nvlist(elem, &config) == 0);
179236884Smm
180236884Smm	props = NULL;
181236884Smm	if (readonly) {
182236884Smm		verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
183236884Smm		verify(nvlist_add_uint64(props,
184236884Smm		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
185236884Smm	}
186236884Smm
187236884Smm	zfeature_checks_disable = B_TRUE;
188236884Smm	error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
189236884Smm	zfeature_checks_disable = B_FALSE;
190236884Smm	if (error == EEXIST)
191236884Smm		error = 0;
192236884Smm
193236884Smm	if (error)
194260617Sdelphij		fatal(NULL, FTAG, "can't import '%s': %s", name,
195260617Sdelphij		    strerror(error));
196236884Smm}
197236884Smm
198236884Smmstatic void
199236884Smmzhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
200236884Smm{
201236884Smm	int err;
202236884Smm
203236884Smm	import_pool(target, readonly);
204236884Smm
205236884Smm	zfeature_checks_disable = B_TRUE;
206236884Smm	err = spa_open(target, spa, tag);
207236884Smm	zfeature_checks_disable = B_FALSE;
208236884Smm
209236884Smm	if (err != 0)
210260617Sdelphij		fatal(*spa, FTAG, "cannot open '%s': %s", target,
211260617Sdelphij		    strerror(err));
212236884Smm	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
213260617Sdelphij		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
214260617Sdelphij		    target, (int)spa_version(*spa));
215236884Smm	}
216236884Smm}
217236884Smm
218236884Smmstatic void
219236884Smmdump_obj(objset_t *os, uint64_t obj, const char *name)
220236884Smm{
221236884Smm	zap_cursor_t zc;
222236884Smm	zap_attribute_t za;
223236884Smm
224236884Smm	(void) printf("%s_obj:\n", name);
225236884Smm
226236884Smm	for (zap_cursor_init(&zc, os, obj);
227236884Smm	    zap_cursor_retrieve(&zc, &za) == 0;
228236884Smm	    zap_cursor_advance(&zc)) {
229236884Smm		if (za.za_integer_length == 8) {
230236884Smm			ASSERT(za.za_num_integers == 1);
231236884Smm			(void) printf("\t%s = %llu\n",
232236884Smm			    za.za_name, (u_longlong_t)za.za_first_integer);
233236884Smm		} else {
234236884Smm			ASSERT(za.za_integer_length == 1);
235236884Smm			char val[1024];
236236884Smm			VERIFY(zap_lookup(os, obj, za.za_name,
237236884Smm			    1, sizeof (val), val) == 0);
238236884Smm			(void) printf("\t%s = %s\n", za.za_name, val);
239236884Smm		}
240236884Smm	}
241236884Smm	zap_cursor_fini(&zc);
242236884Smm}
243236884Smm
244236884Smmstatic void
245236884Smmdump_mos(spa_t *spa)
246236884Smm{
247236884Smm	nvlist_t *nv = spa->spa_label_features;
248236884Smm
249236884Smm	(void) printf("label config:\n");
250236884Smm	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
251236884Smm	    pair != NULL;
252236884Smm	    pair = nvlist_next_nvpair(nv, pair)) {
253236884Smm		(void) printf("\t%s\n", nvpair_name(pair));
254236884Smm	}
255236884Smm}
256236884Smm
257236884Smmstatic void
258236884Smmzhack_do_feature_stat(int argc, char **argv)
259236884Smm{
260236884Smm	spa_t *spa;
261236884Smm	objset_t *os;
262236884Smm	char *target;
263236884Smm
264236884Smm	argc--;
265236884Smm	argv++;
266236884Smm
267236884Smm	if (argc < 1) {
268236884Smm		(void) fprintf(stderr, "error: missing pool name\n");
269236884Smm		usage();
270236884Smm	}
271236884Smm	target = argv[0];
272236884Smm
273236884Smm	zhack_spa_open(target, B_TRUE, FTAG, &spa);
274236884Smm	os = spa->spa_meta_objset;
275236884Smm
276236884Smm	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
277236884Smm	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
278236884Smm	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
279263397Sdelphij	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
280263397Sdelphij		dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
281263397Sdelphij	}
282236884Smm	dump_mos(spa);
283236884Smm
284236884Smm	spa_close(spa, FTAG);
285236884Smm}
286236884Smm
287236884Smmstatic void
288263390Sdelphijzhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
289236884Smm{
290248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
291248571Smm	zfeature_info_t *feature = arg;
292236884Smm
293263390Sdelphij	feature_enable_sync(spa, feature, tx);
294263390Sdelphij
295248571Smm	spa_history_log_internal(spa, "zhack enable feature", tx,
296288572Smav	    "guid=%s flags=%x",
297288572Smav	    feature->fi_guid, feature->fi_flags);
298236884Smm}
299236884Smm
300236884Smmstatic void
301236884Smmzhack_do_feature_enable(int argc, char **argv)
302236884Smm{
303236884Smm	char c;
304236884Smm	char *desc, *target;
305236884Smm	spa_t *spa;
306236884Smm	objset_t *mos;
307236884Smm	zfeature_info_t feature;
308263390Sdelphij	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
309236884Smm
310236884Smm	/*
311236884Smm	 * Features are not added to the pool's label until their refcounts
312236884Smm	 * are incremented, so fi_mos can just be left as false for now.
313236884Smm	 */
314236884Smm	desc = NULL;
315236884Smm	feature.fi_uname = "zhack";
316288572Smav	feature.fi_flags = 0;
317236884Smm	feature.fi_depends = nodeps;
318263397Sdelphij	feature.fi_feature = SPA_FEATURE_NONE;
319236884Smm
320236884Smm	optind = 1;
321236884Smm	while ((c = getopt(argc, argv, "rmd:")) != -1) {
322236884Smm		switch (c) {
323236884Smm		case 'r':
324288572Smav			feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
325236884Smm			break;
326236884Smm		case 'd':
327236884Smm			desc = strdup(optarg);
328236884Smm			break;
329236884Smm		default:
330236884Smm			usage();
331236884Smm			break;
332236884Smm		}
333236884Smm	}
334236884Smm
335236884Smm	if (desc == NULL)
336236884Smm		desc = strdup("zhack injected");
337236884Smm	feature.fi_desc = desc;
338236884Smm
339236884Smm	argc -= optind;
340236884Smm	argv += optind;
341236884Smm
342236884Smm	if (argc < 2) {
343236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
344236884Smm		usage();
345236884Smm	}
346236884Smm	target = argv[0];
347236884Smm	feature.fi_guid = argv[1];
348236884Smm
349236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
350260617Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
351236884Smm
352236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
353236884Smm	mos = spa->spa_meta_objset;
354236884Smm
355263390Sdelphij	if (zfeature_is_supported(feature.fi_guid))
356260617Sdelphij		fatal(spa, FTAG, "'%s' is a real feature, will not enable");
357236884Smm	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
358260617Sdelphij		fatal(spa, FTAG, "feature already enabled: %s",
359260617Sdelphij		    feature.fi_guid);
360236884Smm
361248571Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
362269006Sdelphij	    zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
363236884Smm
364236884Smm	spa_close(spa, FTAG);
365236884Smm
366236884Smm	free(desc);
367236884Smm}
368236884Smm
369236884Smmstatic void
370248571Smmfeature_incr_sync(void *arg, dmu_tx_t *tx)
371236884Smm{
372248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
373248571Smm	zfeature_info_t *feature = arg;
374263390Sdelphij	uint64_t refcount;
375236884Smm
376263397Sdelphij	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
377263390Sdelphij	feature_sync(spa, feature, refcount + 1, tx);
378248571Smm	spa_history_log_internal(spa, "zhack feature incr", tx,
379248571Smm	    "name=%s", feature->fi_guid);
380236884Smm}
381236884Smm
382236884Smmstatic void
383248571Smmfeature_decr_sync(void *arg, dmu_tx_t *tx)
384236884Smm{
385248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
386248571Smm	zfeature_info_t *feature = arg;
387263390Sdelphij	uint64_t refcount;
388236884Smm
389263397Sdelphij	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
390263390Sdelphij	feature_sync(spa, feature, refcount - 1, tx);
391248571Smm	spa_history_log_internal(spa, "zhack feature decr", tx,
392248571Smm	    "name=%s", feature->fi_guid);
393236884Smm}
394236884Smm
395236884Smmstatic void
396236884Smmzhack_do_feature_ref(int argc, char **argv)
397236884Smm{
398236884Smm	char c;
399236884Smm	char *target;
400236884Smm	boolean_t decr = B_FALSE;
401236884Smm	spa_t *spa;
402236884Smm	objset_t *mos;
403236884Smm	zfeature_info_t feature;
404263390Sdelphij	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
405236884Smm
406236884Smm	/*
407236884Smm	 * fi_desc does not matter here because it was written to disk
408236884Smm	 * when the feature was enabled, but we need to properly set the
409236884Smm	 * feature for read or write based on the information we read off
410236884Smm	 * disk later.
411236884Smm	 */
412236884Smm	feature.fi_uname = "zhack";
413288572Smav	feature.fi_flags = 0;
414236884Smm	feature.fi_desc = NULL;
415236884Smm	feature.fi_depends = nodeps;
416263397Sdelphij	feature.fi_feature = SPA_FEATURE_NONE;
417236884Smm
418236884Smm	optind = 1;
419236884Smm	while ((c = getopt(argc, argv, "md")) != -1) {
420236884Smm		switch (c) {
421236884Smm		case 'm':
422288572Smav			feature.fi_flags |= ZFEATURE_FLAG_MOS;
423236884Smm			break;
424236884Smm		case 'd':
425236884Smm			decr = B_TRUE;
426236884Smm			break;
427236884Smm		default:
428236884Smm			usage();
429236884Smm			break;
430236884Smm		}
431236884Smm	}
432236884Smm	argc -= optind;
433236884Smm	argv += optind;
434236884Smm
435236884Smm	if (argc < 2) {
436236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
437236884Smm		usage();
438236884Smm	}
439236884Smm	target = argv[0];
440236884Smm	feature.fi_guid = argv[1];
441236884Smm
442236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
443260617Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
444236884Smm
445236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
446236884Smm	mos = spa->spa_meta_objset;
447236884Smm
448263390Sdelphij	if (zfeature_is_supported(feature.fi_guid)) {
449263390Sdelphij		fatal(spa, FTAG,
450263390Sdelphij		    "'%s' is a real feature, will not change refcount");
451263390Sdelphij	}
452236884Smm
453236884Smm	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
454236884Smm	    feature.fi_guid)) {
455288572Smav		feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
456236884Smm	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
457236884Smm	    feature.fi_guid)) {
458288572Smav		feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
459236884Smm	} else {
460260617Sdelphij		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
461236884Smm	}
462236884Smm
463263390Sdelphij	if (decr) {
464263390Sdelphij		uint64_t count;
465263397Sdelphij		if (feature_get_refcount_from_disk(spa, &feature,
466263397Sdelphij		    &count) == 0 && count != 0) {
467263390Sdelphij			fatal(spa, FTAG, "feature refcount already 0: %s",
468263390Sdelphij			    feature.fi_guid);
469263390Sdelphij		}
470263390Sdelphij	}
471236884Smm
472248571Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
473269006Sdelphij	    decr ? feature_decr_sync : feature_incr_sync, &feature,
474269006Sdelphij	    5, ZFS_SPACE_CHECK_NORMAL));
475236884Smm
476236884Smm	spa_close(spa, FTAG);
477236884Smm}
478236884Smm
479236884Smmstatic int
480236884Smmzhack_do_feature(int argc, char **argv)
481236884Smm{
482236884Smm	char *subcommand;
483236884Smm
484236884Smm	argc--;
485236884Smm	argv++;
486236884Smm	if (argc == 0) {
487236884Smm		(void) fprintf(stderr,
488236884Smm		    "error: no feature operation specified\n");
489236884Smm		usage();
490236884Smm	}
491236884Smm
492236884Smm	subcommand = argv[0];
493236884Smm	if (strcmp(subcommand, "stat") == 0) {
494236884Smm		zhack_do_feature_stat(argc, argv);
495236884Smm	} else if (strcmp(subcommand, "enable") == 0) {
496236884Smm		zhack_do_feature_enable(argc, argv);
497236884Smm	} else if (strcmp(subcommand, "ref") == 0) {
498236884Smm		zhack_do_feature_ref(argc, argv);
499236884Smm	} else {
500236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
501236884Smm		    subcommand);
502236884Smm		usage();
503236884Smm	}
504236884Smm
505236884Smm	return (0);
506236884Smm}
507236884Smm
508236884Smm#define	MAX_NUM_PATHS 1024
509236884Smm
510236884Smmint
511236884Smmmain(int argc, char **argv)
512236884Smm{
513236884Smm	extern void zfs_prop_init(void);
514236884Smm
515236884Smm	char *path[MAX_NUM_PATHS];
516236884Smm	const char *subcommand;
517236884Smm	int rv = 0;
518236884Smm	char c;
519236884Smm
520236884Smm	g_importargs.path = path;
521236884Smm
522236884Smm	dprintf_setup(&argc, argv);
523236884Smm	zfs_prop_init();
524236884Smm
525236884Smm	while ((c = getopt(argc, argv, "c:d:")) != -1) {
526236884Smm		switch (c) {
527236884Smm		case 'c':
528236884Smm			g_importargs.cachefile = optarg;
529236884Smm			break;
530236884Smm		case 'd':
531236884Smm			assert(g_importargs.paths < MAX_NUM_PATHS);
532236884Smm			g_importargs.path[g_importargs.paths++] = optarg;
533236884Smm			break;
534236884Smm		default:
535236884Smm			usage();
536236884Smm			break;
537236884Smm		}
538236884Smm	}
539236884Smm
540236884Smm	argc -= optind;
541236884Smm	argv += optind;
542236884Smm	optind = 1;
543236884Smm
544236884Smm	if (argc == 0) {
545236884Smm		(void) fprintf(stderr, "error: no command specified\n");
546236884Smm		usage();
547236884Smm	}
548236884Smm
549236884Smm	subcommand = argv[0];
550236884Smm
551236884Smm	if (strcmp(subcommand, "feature") == 0) {
552236884Smm		rv = zhack_do_feature(argc, argv);
553236884Smm	} else {
554236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
555236884Smm		    subcommand);
556236884Smm		usage();
557236884Smm	}
558236884Smm
559260617Sdelphij	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
560260617Sdelphij		fatal(NULL, FTAG, "pool export failed; "
561236884Smm		    "changes may not be committed to disk\n");
562236884Smm	}
563236884Smm
564236884Smm	libzfs_fini(g_zfs);
565236884Smm	kernel_fini();
566236884Smm
567236884Smm	return (rv);
568236884Smm}
569