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 ZFS_MAXNAMELEN
52236884Smm#undef verify
53236884Smm#include <libzfs.h>
54236884Smm
55236884Smmextern boolean_t zfeature_checks_disable;
56236884Smm
57236884Smmconst char cmdname[] = "zhack";
58236884Smmlibzfs_handle_t *g_zfs;
59236884Smmstatic importargs_t g_importargs;
60236884Smmstatic char *g_pool;
61236884Smmstatic boolean_t g_readonly;
62236884Smm
63236884Smmstatic void
64236884Smmusage(void)
65236884Smm{
66236884Smm	(void) fprintf(stderr,
67236884Smm	    "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
68236884Smm	    "where <subcommand> <args> is one of the following:\n"
69236884Smm	    "\n", cmdname);
70236884Smm
71236884Smm	(void) fprintf(stderr,
72236884Smm	    "    feature stat <pool>\n"
73236884Smm	    "        print information about enabled features\n"
74236884Smm	    "    feature enable [-d desc] <pool> <feature>\n"
75236884Smm	    "        add a new enabled feature to the pool\n"
76236884Smm	    "        -d <desc> sets the feature's description\n"
77236884Smm	    "    feature ref [-md] <pool> <feature>\n"
78236884Smm	    "        change the refcount on the given feature\n"
79236884Smm	    "        -d decrease instead of increase the refcount\n"
80236884Smm	    "        -m add the feature to the label if increasing refcount\n"
81236884Smm	    "\n"
82236884Smm	    "    <feature> : should be a feature guid\n");
83236884Smm	exit(1);
84236884Smm}
85236884Smm
86236884Smm
87236884Smmstatic void
88260617Sdelphijfatal(spa_t *spa, void *tag, const char *fmt, ...)
89236884Smm{
90236884Smm	va_list ap;
91236884Smm
92260617Sdelphij	if (spa != NULL) {
93260617Sdelphij		spa_close(spa, tag);
94260617Sdelphij		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
95260617Sdelphij	}
96260617Sdelphij
97236884Smm	va_start(ap, fmt);
98236884Smm	(void) fprintf(stderr, "%s: ", cmdname);
99236884Smm	(void) vfprintf(stderr, fmt, ap);
100236884Smm	va_end(ap);
101236884Smm	(void) fprintf(stderr, "\n");
102236884Smm
103236884Smm	exit(1);
104236884Smm}
105236884Smm
106236884Smm/* ARGSUSED */
107236884Smmstatic int
108236884Smmspace_delta_cb(dmu_object_type_t bonustype, void *data,
109236884Smm    uint64_t *userp, uint64_t *groupp)
110236884Smm{
111236884Smm	/*
112236884Smm	 * Is it a valid type of object to track?
113236884Smm	 */
114236884Smm	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
115236884Smm		return (ENOENT);
116236884Smm	(void) fprintf(stderr, "modifying object that needs user accounting");
117236884Smm	abort();
118236884Smm	/* NOTREACHED */
119236884Smm}
120236884Smm
121236884Smm/*
122236884Smm * Target is the dataset whose pool we want to open.
123236884Smm */
124236884Smmstatic void
125236884Smmimport_pool(const char *target, boolean_t readonly)
126236884Smm{
127236884Smm	nvlist_t *config;
128236884Smm	nvlist_t *pools;
129236884Smm	int error;
130236884Smm	char *sepp;
131236884Smm	spa_t *spa;
132236884Smm	nvpair_t *elem;
133236884Smm	nvlist_t *props;
134236884Smm	const char *name;
135236884Smm
136236884Smm	kernel_init(readonly ? FREAD : (FREAD | FWRITE));
137236884Smm	g_zfs = libzfs_init();
138236884Smm	ASSERT(g_zfs != NULL);
139236884Smm
140236884Smm	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
141236884Smm
142236884Smm	g_readonly = readonly;
143236884Smm
144236884Smm	/*
145236884Smm	 * If we only want readonly access, it's OK if we find
146236884Smm	 * a potentially-active (ie, imported into the kernel) pool from the
147236884Smm	 * default cachefile.
148236884Smm	 */
149236884Smm	if (readonly && spa_open(target, &spa, FTAG) == 0) {
150236884Smm		spa_close(spa, FTAG);
151236884Smm		return;
152236884Smm	}
153236884Smm
154236884Smm	g_importargs.unique = B_TRUE;
155236884Smm	g_importargs.can_be_active = readonly;
156236884Smm	g_pool = strdup(target);
157236884Smm	if ((sepp = strpbrk(g_pool, "/@")) != NULL)
158236884Smm		*sepp = '\0';
159236884Smm	g_importargs.poolname = g_pool;
160236884Smm	pools = zpool_search_import(g_zfs, &g_importargs);
161236884Smm
162251646Sdelphij	if (nvlist_empty(pools)) {
163236884Smm		if (!g_importargs.can_be_active) {
164236884Smm			g_importargs.can_be_active = B_TRUE;
165236884Smm			if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
166236884Smm			    spa_open(target, &spa, FTAG) == 0) {
167260617Sdelphij				fatal(spa, FTAG, "cannot import '%s': pool is "
168260617Sdelphij				    "active; run " "\"zpool export %s\" "
169260617Sdelphij				    "first\n", g_pool, g_pool);
170236884Smm			}
171236884Smm		}
172236884Smm
173260617Sdelphij		fatal(NULL, FTAG, "cannot import '%s': no such pool "
174260617Sdelphij		    "available\n", g_pool);
175236884Smm	}
176236884Smm
177236884Smm	elem = nvlist_next_nvpair(pools, NULL);
178236884Smm	name = nvpair_name(elem);
179236884Smm	verify(nvpair_value_nvlist(elem, &config) == 0);
180236884Smm
181236884Smm	props = NULL;
182236884Smm	if (readonly) {
183236884Smm		verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
184236884Smm		verify(nvlist_add_uint64(props,
185236884Smm		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
186236884Smm	}
187236884Smm
188236884Smm	zfeature_checks_disable = B_TRUE;
189236884Smm	error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
190236884Smm	zfeature_checks_disable = B_FALSE;
191236884Smm	if (error == EEXIST)
192236884Smm		error = 0;
193236884Smm
194236884Smm	if (error)
195260617Sdelphij		fatal(NULL, FTAG, "can't import '%s': %s", name,
196260617Sdelphij		    strerror(error));
197236884Smm}
198236884Smm
199236884Smmstatic void
200236884Smmzhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
201236884Smm{
202236884Smm	int err;
203236884Smm
204236884Smm	import_pool(target, readonly);
205236884Smm
206236884Smm	zfeature_checks_disable = B_TRUE;
207236884Smm	err = spa_open(target, spa, tag);
208236884Smm	zfeature_checks_disable = B_FALSE;
209236884Smm
210236884Smm	if (err != 0)
211260617Sdelphij		fatal(*spa, FTAG, "cannot open '%s': %s", target,
212260617Sdelphij		    strerror(err));
213236884Smm	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
214260617Sdelphij		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
215260617Sdelphij		    target, (int)spa_version(*spa));
216236884Smm	}
217236884Smm}
218236884Smm
219236884Smmstatic void
220236884Smmdump_obj(objset_t *os, uint64_t obj, const char *name)
221236884Smm{
222236884Smm	zap_cursor_t zc;
223236884Smm	zap_attribute_t za;
224236884Smm
225236884Smm	(void) printf("%s_obj:\n", name);
226236884Smm
227236884Smm	for (zap_cursor_init(&zc, os, obj);
228236884Smm	    zap_cursor_retrieve(&zc, &za) == 0;
229236884Smm	    zap_cursor_advance(&zc)) {
230236884Smm		if (za.za_integer_length == 8) {
231236884Smm			ASSERT(za.za_num_integers == 1);
232236884Smm			(void) printf("\t%s = %llu\n",
233236884Smm			    za.za_name, (u_longlong_t)za.za_first_integer);
234236884Smm		} else {
235236884Smm			ASSERT(za.za_integer_length == 1);
236236884Smm			char val[1024];
237236884Smm			VERIFY(zap_lookup(os, obj, za.za_name,
238236884Smm			    1, sizeof (val), val) == 0);
239236884Smm			(void) printf("\t%s = %s\n", za.za_name, val);
240236884Smm		}
241236884Smm	}
242236884Smm	zap_cursor_fini(&zc);
243236884Smm}
244236884Smm
245236884Smmstatic void
246236884Smmdump_mos(spa_t *spa)
247236884Smm{
248236884Smm	nvlist_t *nv = spa->spa_label_features;
249236884Smm
250236884Smm	(void) printf("label config:\n");
251236884Smm	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
252236884Smm	    pair != NULL;
253236884Smm	    pair = nvlist_next_nvpair(nv, pair)) {
254236884Smm		(void) printf("\t%s\n", nvpair_name(pair));
255236884Smm	}
256236884Smm}
257236884Smm
258236884Smmstatic void
259236884Smmzhack_do_feature_stat(int argc, char **argv)
260236884Smm{
261236884Smm	spa_t *spa;
262236884Smm	objset_t *os;
263236884Smm	char *target;
264236884Smm
265236884Smm	argc--;
266236884Smm	argv++;
267236884Smm
268236884Smm	if (argc < 1) {
269236884Smm		(void) fprintf(stderr, "error: missing pool name\n");
270236884Smm		usage();
271236884Smm	}
272236884Smm	target = argv[0];
273236884Smm
274236884Smm	zhack_spa_open(target, B_TRUE, FTAG, &spa);
275236884Smm	os = spa->spa_meta_objset;
276236884Smm
277236884Smm	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
278236884Smm	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
279236884Smm	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
280263397Sdelphij	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
281263397Sdelphij		dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
282263397Sdelphij	}
283236884Smm	dump_mos(spa);
284236884Smm
285236884Smm	spa_close(spa, FTAG);
286236884Smm}
287236884Smm
288236884Smmstatic void
289263390Sdelphijzhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
290236884Smm{
291248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
292248571Smm	zfeature_info_t *feature = arg;
293236884Smm
294263390Sdelphij	feature_enable_sync(spa, feature, tx);
295263390Sdelphij
296248571Smm	spa_history_log_internal(spa, "zhack enable feature", tx,
297288572Smav	    "guid=%s flags=%x",
298288572Smav	    feature->fi_guid, feature->fi_flags);
299236884Smm}
300236884Smm
301236884Smmstatic void
302236884Smmzhack_do_feature_enable(int argc, char **argv)
303236884Smm{
304236884Smm	char c;
305236884Smm	char *desc, *target;
306236884Smm	spa_t *spa;
307236884Smm	objset_t *mos;
308236884Smm	zfeature_info_t feature;
309263390Sdelphij	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
310236884Smm
311236884Smm	/*
312236884Smm	 * Features are not added to the pool's label until their refcounts
313236884Smm	 * are incremented, so fi_mos can just be left as false for now.
314236884Smm	 */
315236884Smm	desc = NULL;
316236884Smm	feature.fi_uname = "zhack";
317288572Smav	feature.fi_flags = 0;
318236884Smm	feature.fi_depends = nodeps;
319263397Sdelphij	feature.fi_feature = SPA_FEATURE_NONE;
320236884Smm
321236884Smm	optind = 1;
322236884Smm	while ((c = getopt(argc, argv, "rmd:")) != -1) {
323236884Smm		switch (c) {
324236884Smm		case 'r':
325288572Smav			feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
326236884Smm			break;
327236884Smm		case 'd':
328236884Smm			desc = strdup(optarg);
329236884Smm			break;
330236884Smm		default:
331236884Smm			usage();
332236884Smm			break;
333236884Smm		}
334236884Smm	}
335236884Smm
336236884Smm	if (desc == NULL)
337236884Smm		desc = strdup("zhack injected");
338236884Smm	feature.fi_desc = desc;
339236884Smm
340236884Smm	argc -= optind;
341236884Smm	argv += optind;
342236884Smm
343236884Smm	if (argc < 2) {
344236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
345236884Smm		usage();
346236884Smm	}
347236884Smm	target = argv[0];
348236884Smm	feature.fi_guid = argv[1];
349236884Smm
350236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
351260617Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
352236884Smm
353236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
354236884Smm	mos = spa->spa_meta_objset;
355236884Smm
356263390Sdelphij	if (zfeature_is_supported(feature.fi_guid))
357260617Sdelphij		fatal(spa, FTAG, "'%s' is a real feature, will not enable");
358236884Smm	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
359260617Sdelphij		fatal(spa, FTAG, "feature already enabled: %s",
360260617Sdelphij		    feature.fi_guid);
361236884Smm
362248571Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
363269006Sdelphij	    zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
364236884Smm
365236884Smm	spa_close(spa, FTAG);
366236884Smm
367236884Smm	free(desc);
368236884Smm}
369236884Smm
370236884Smmstatic void
371248571Smmfeature_incr_sync(void *arg, dmu_tx_t *tx)
372236884Smm{
373248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
374248571Smm	zfeature_info_t *feature = arg;
375263390Sdelphij	uint64_t refcount;
376236884Smm
377263397Sdelphij	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
378263390Sdelphij	feature_sync(spa, feature, refcount + 1, tx);
379248571Smm	spa_history_log_internal(spa, "zhack feature incr", tx,
380248571Smm	    "name=%s", feature->fi_guid);
381236884Smm}
382236884Smm
383236884Smmstatic void
384248571Smmfeature_decr_sync(void *arg, dmu_tx_t *tx)
385236884Smm{
386248571Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
387248571Smm	zfeature_info_t *feature = arg;
388263390Sdelphij	uint64_t refcount;
389236884Smm
390263397Sdelphij	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
391263390Sdelphij	feature_sync(spa, feature, refcount - 1, tx);
392248571Smm	spa_history_log_internal(spa, "zhack feature decr", tx,
393248571Smm	    "name=%s", feature->fi_guid);
394236884Smm}
395236884Smm
396236884Smmstatic void
397236884Smmzhack_do_feature_ref(int argc, char **argv)
398236884Smm{
399236884Smm	char c;
400236884Smm	char *target;
401236884Smm	boolean_t decr = B_FALSE;
402236884Smm	spa_t *spa;
403236884Smm	objset_t *mos;
404236884Smm	zfeature_info_t feature;
405263390Sdelphij	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
406236884Smm
407236884Smm	/*
408236884Smm	 * fi_desc does not matter here because it was written to disk
409236884Smm	 * when the feature was enabled, but we need to properly set the
410236884Smm	 * feature for read or write based on the information we read off
411236884Smm	 * disk later.
412236884Smm	 */
413236884Smm	feature.fi_uname = "zhack";
414288572Smav	feature.fi_flags = 0;
415236884Smm	feature.fi_desc = NULL;
416236884Smm	feature.fi_depends = nodeps;
417263397Sdelphij	feature.fi_feature = SPA_FEATURE_NONE;
418236884Smm
419236884Smm	optind = 1;
420236884Smm	while ((c = getopt(argc, argv, "md")) != -1) {
421236884Smm		switch (c) {
422236884Smm		case 'm':
423288572Smav			feature.fi_flags |= ZFEATURE_FLAG_MOS;
424236884Smm			break;
425236884Smm		case 'd':
426236884Smm			decr = B_TRUE;
427236884Smm			break;
428236884Smm		default:
429236884Smm			usage();
430236884Smm			break;
431236884Smm		}
432236884Smm	}
433236884Smm	argc -= optind;
434236884Smm	argv += optind;
435236884Smm
436236884Smm	if (argc < 2) {
437236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
438236884Smm		usage();
439236884Smm	}
440236884Smm	target = argv[0];
441236884Smm	feature.fi_guid = argv[1];
442236884Smm
443236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
444260617Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
445236884Smm
446236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
447236884Smm	mos = spa->spa_meta_objset;
448236884Smm
449263390Sdelphij	if (zfeature_is_supported(feature.fi_guid)) {
450263390Sdelphij		fatal(spa, FTAG,
451263390Sdelphij		    "'%s' is a real feature, will not change refcount");
452263390Sdelphij	}
453236884Smm
454236884Smm	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
455236884Smm	    feature.fi_guid)) {
456288572Smav		feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
457236884Smm	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
458236884Smm	    feature.fi_guid)) {
459288572Smav		feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
460236884Smm	} else {
461260617Sdelphij		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
462236884Smm	}
463236884Smm
464263390Sdelphij	if (decr) {
465263390Sdelphij		uint64_t count;
466263397Sdelphij		if (feature_get_refcount_from_disk(spa, &feature,
467263397Sdelphij		    &count) == 0 && count != 0) {
468263390Sdelphij			fatal(spa, FTAG, "feature refcount already 0: %s",
469263390Sdelphij			    feature.fi_guid);
470263390Sdelphij		}
471263390Sdelphij	}
472236884Smm
473248571Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
474269006Sdelphij	    decr ? feature_decr_sync : feature_incr_sync, &feature,
475269006Sdelphij	    5, ZFS_SPACE_CHECK_NORMAL));
476236884Smm
477236884Smm	spa_close(spa, FTAG);
478236884Smm}
479236884Smm
480236884Smmstatic int
481236884Smmzhack_do_feature(int argc, char **argv)
482236884Smm{
483236884Smm	char *subcommand;
484236884Smm
485236884Smm	argc--;
486236884Smm	argv++;
487236884Smm	if (argc == 0) {
488236884Smm		(void) fprintf(stderr,
489236884Smm		    "error: no feature operation specified\n");
490236884Smm		usage();
491236884Smm	}
492236884Smm
493236884Smm	subcommand = argv[0];
494236884Smm	if (strcmp(subcommand, "stat") == 0) {
495236884Smm		zhack_do_feature_stat(argc, argv);
496236884Smm	} else if (strcmp(subcommand, "enable") == 0) {
497236884Smm		zhack_do_feature_enable(argc, argv);
498236884Smm	} else if (strcmp(subcommand, "ref") == 0) {
499236884Smm		zhack_do_feature_ref(argc, argv);
500236884Smm	} else {
501236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
502236884Smm		    subcommand);
503236884Smm		usage();
504236884Smm	}
505236884Smm
506236884Smm	return (0);
507236884Smm}
508236884Smm
509236884Smm#define	MAX_NUM_PATHS 1024
510236884Smm
511236884Smmint
512236884Smmmain(int argc, char **argv)
513236884Smm{
514236884Smm	extern void zfs_prop_init(void);
515236884Smm
516236884Smm	char *path[MAX_NUM_PATHS];
517236884Smm	const char *subcommand;
518236884Smm	int rv = 0;
519236884Smm	char c;
520236884Smm
521236884Smm	g_importargs.path = path;
522236884Smm
523236884Smm	dprintf_setup(&argc, argv);
524236884Smm	zfs_prop_init();
525236884Smm
526236884Smm	while ((c = getopt(argc, argv, "c:d:")) != -1) {
527236884Smm		switch (c) {
528236884Smm		case 'c':
529236884Smm			g_importargs.cachefile = optarg;
530236884Smm			break;
531236884Smm		case 'd':
532236884Smm			assert(g_importargs.paths < MAX_NUM_PATHS);
533236884Smm			g_importargs.path[g_importargs.paths++] = optarg;
534236884Smm			break;
535236884Smm		default:
536236884Smm			usage();
537236884Smm			break;
538236884Smm		}
539236884Smm	}
540236884Smm
541236884Smm	argc -= optind;
542236884Smm	argv += optind;
543236884Smm	optind = 1;
544236884Smm
545236884Smm	if (argc == 0) {
546236884Smm		(void) fprintf(stderr, "error: no command specified\n");
547236884Smm		usage();
548236884Smm	}
549236884Smm
550236884Smm	subcommand = argv[0];
551236884Smm
552236884Smm	if (strcmp(subcommand, "feature") == 0) {
553236884Smm		rv = zhack_do_feature(argc, argv);
554236884Smm	} else {
555236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
556236884Smm		    subcommand);
557236884Smm		usage();
558236884Smm	}
559236884Smm
560260617Sdelphij	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
561260617Sdelphij		fatal(NULL, FTAG, "pool export failed; "
562236884Smm		    "changes may not be committed to disk\n");
563236884Smm	}
564236884Smm
565236884Smm	libzfs_fini(g_zfs);
566236884Smm	kernel_fini();
567236884Smm
568236884Smm	return (rv);
569236884Smm}
570