153089abeschrock/*
253089abeschrock * CDDL HEADER START
353089abeschrock *
453089abeschrock * The contents of this file are subject to the terms of the
553089abeschrock * Common Development and Distribution License (the "License").
653089abeschrock * You may not use this file except in compliance with the License.
753089abeschrock *
853089abeschrock * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
953089abeschrock * or http://www.opensolaris.org/os/licensing.
1053089abeschrock * See the License for the specific language governing permissions
1153089abeschrock * and limitations under the License.
1253089abeschrock *
1353089abeschrock * When distributing Covered Code, include this CDDL HEADER in each
1453089abeschrock * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1553089abeschrock * If applicable, add the following below this CDDL HEADER, with the
1653089abeschrock * fields enclosed by brackets "[]" replaced with your own identifying
1753089abeschrock * information: Portions Copyright [yyyy] [name of copyright owner]
1853089abeschrock *
1953089abeschrock * CDDL HEADER END
2053089abeschrock */
2153089abeschrock
2253089abeschrock/*
23ca0cc39Matthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24a7a845eSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
2553089abeschrock */
2653089abeschrock
2753089abeschrock/*
2853089abeschrock * zhack is a debugging tool that can write changes to ZFS pool using libzpool
2953089abeschrock * for testing purposes. Altering pools with zhack is unsupported and may
3053089abeschrock * result in corrupted pools.
3153089abeschrock */
3253089abeschrock
3353089abeschrock#include <stdio.h>
3453089abeschrock#include <stdlib.h>
3553089abeschrock#include <ctype.h>
3653089abeschrock#include <sys/zfs_context.h>
3753089abeschrock#include <sys/spa.h>
3853089abeschrock#include <sys/spa_impl.h>
3953089abeschrock#include <sys/dmu.h>
4053089abeschrock#include <sys/zap.h>
4153089abeschrock#include <sys/zfs_znode.h>
4253089abeschrock#include <sys/dsl_synctask.h>
4353089abeschrock#include <sys/vdev.h>
4453089abeschrock#include <sys/fs/zfs.h>
4553089abeschrock#include <sys/dmu_objset.h>
4653089abeschrock#include <sys/dsl_pool.h>
4753089abeschrock#include <sys/zio_checksum.h>
4853089abeschrock#include <sys/zio_compress.h>
4953089abeschrock#include <sys/zfeature.h>
503b2aab1Matthew Ahrens#include <sys/dmu_tx.h>
5153089abeschrock#undef verify
52d8ab6e1Don Brady#include <libzutil.h>
5353089abeschrock
5453089abeschrockextern boolean_t zfeature_checks_disable;
5553089abeschrock
5653089abeschrockconst char cmdname[] = "zhack";
5753089abeschrockstatic importargs_t g_importargs;
5853089abeschrockstatic char *g_pool;
5953089abeschrockstatic boolean_t g_readonly;
6053089abeschrock
6153089abeschrockstatic void
6253089abeschrockusage(void)
6353089abeschrock{
6453089abeschrock	(void) fprintf(stderr,
6553089abeschrock	    "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
6653089abeschrock	    "where <subcommand> <args> is one of the following:\n"
6753089abeschrock	    "\n", cmdname);
6853089abeschrock
6953089abeschrock	(void) fprintf(stderr,
7053089abeschrock	    "    feature stat <pool>\n"
7153089abeschrock	    "        print information about enabled features\n"
7253089abeschrock	    "    feature enable [-d desc] <pool> <feature>\n"
7353089abeschrock	    "        add a new enabled feature to the pool\n"
7453089abeschrock	    "        -d <desc> sets the feature's description\n"
7553089abeschrock	    "    feature ref [-md] <pool> <feature>\n"
7653089abeschrock	    "        change the refcount on the given feature\n"
7753089abeschrock	    "        -d decrease instead of increase the refcount\n"
7853089abeschrock	    "        -m add the feature to the label if increasing refcount\n"
7953089abeschrock	    "\n"
8053089abeschrock	    "    <feature> : should be a feature guid\n");
8153089abeschrock	exit(1);
8253089abeschrock}
8353089abeschrock
8453089abeschrock
8553089abeschrockstatic void
867fdd916George Wilsonfatal(spa_t *spa, void *tag, const char *fmt, ...)
8753089abeschrock{
8853089abeschrock	va_list ap;
8953089abeschrock
907fdd916George Wilson	if (spa != NULL) {
917fdd916George Wilson		spa_close(spa, tag);
927fdd916George Wilson		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
937fdd916George Wilson	}
947fdd916George Wilson
9553089abeschrock	va_start(ap, fmt);
9653089abeschrock	(void) fprintf(stderr, "%s: ", cmdname);
9753089abeschrock	(void) vfprintf(stderr, fmt, ap);
9853089abeschrock	va_end(ap);
9953089abeschrock	(void) fprintf(stderr, "\n");
10053089abeschrock
10153089abeschrock	exit(1);
10253089abeschrock}
10353089abeschrock
10453089abeschrock/* ARGSUSED */
10553089abeschrockstatic int
10653089abeschrockspace_delta_cb(dmu_object_type_t bonustype, void *data,
107f67950bNasf-Fan    uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
10853089abeschrock{
10953089abeschrock	/*
11053089abeschrock	 * Is it a valid type of object to track?
11153089abeschrock	 */
11253089abeschrock	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
11353089abeschrock		return (ENOENT);
11453089abeschrock	(void) fprintf(stderr, "modifying object that needs user accounting");
11553089abeschrock	abort();
11653089abeschrock	/* NOTREACHED */
11753089abeschrock}
11853089abeschrock
11953089abeschrock/*
12053089abeschrock * Target is the dataset whose pool we want to open.
12153089abeschrock */
12253089abeschrockstatic void
123e0f1c0aOlaf Faalandzhack_import(char *target, boolean_t readonly)
12453089abeschrock{
12553089abeschrock	nvlist_t *config;
12653089abeschrock	nvlist_t *props;
127e0f1c0aOlaf Faaland	int error;
12853089abeschrock
12953089abeschrock	kernel_init(readonly ? FREAD : (FREAD | FWRITE));
13053089abeschrock
13153089abeschrock	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
13253089abeschrock
13353089abeschrock	g_readonly = readonly;
13453089abeschrock	g_importargs.can_be_active = readonly;
13553089abeschrock	g_pool = strdup(target);
13653089abeschrock
137d8ab6e1Don Brady	error = zpool_find_config(NULL, target, &config, &g_importargs,
138d8ab6e1Don Brady	    &libzpool_config_ops);
139e0f1c0aOlaf Faaland	if (error)
140d8ab6e1Don Brady		fatal(NULL, FTAG, "cannot import '%s'", target);
14153089abeschrock
14253089abeschrock	props = NULL;
14353089abeschrock	if (readonly) {
144e0f1c0aOlaf Faaland		VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
145e0f1c0aOlaf Faaland		VERIFY(nvlist_add_uint64(props,
14653089abeschrock		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
14753089abeschrock	}
14853089abeschrock
14953089abeschrock	zfeature_checks_disable = B_TRUE;
150e0f1c0aOlaf Faaland	error = spa_import(target, config, props,
151e0f1c0aOlaf Faaland	    (readonly ?  ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
15253089abeschrock	zfeature_checks_disable = B_FALSE;
15353089abeschrock	if (error == EEXIST)
15453089abeschrock		error = 0;
15553089abeschrock
15653089abeschrock	if (error)
157e0f1c0aOlaf Faaland		fatal(NULL, FTAG, "can't import '%s': %s", target,
1587fdd916George Wilson		    strerror(error));
15953089abeschrock}
16053089abeschrock
16153089abeschrockstatic void
162e0f1c0aOlaf Faalandzhack_spa_open(char *target, boolean_t readonly, void *tag, spa_t **spa)
16353089abeschrock{
16453089abeschrock	int err;
16553089abeschrock
166e0f1c0aOlaf Faaland	zhack_import(target, readonly);
16753089abeschrock
16853089abeschrock	zfeature_checks_disable = B_TRUE;
16953089abeschrock	err = spa_open(target, spa, tag);
17053089abeschrock	zfeature_checks_disable = B_FALSE;
17153089abeschrock
17253089abeschrock	if (err != 0)
1737fdd916George Wilson		fatal(*spa, FTAG, "cannot open '%s': %s", target,
1747fdd916George Wilson		    strerror(err));
17553089abeschrock	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
1767fdd916George Wilson		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
1777fdd916George Wilson		    target, (int)spa_version(*spa));
17853089abeschrock	}
17953089abeschrock}
18053089abeschrock
18153089abeschrockstatic void
18253089abeschrockdump_obj(objset_t *os, uint64_t obj, const char *name)
18353089abeschrock{
18453089abeschrock	zap_cursor_t zc;
18553089abeschrock	zap_attribute_t za;
18653089abeschrock
18753089abeschrock	(void) printf("%s_obj:\n", name);
18853089abeschrock
18953089abeschrock	for (zap_cursor_init(&zc, os, obj);
19053089abeschrock	    zap_cursor_retrieve(&zc, &za) == 0;
19153089abeschrock	    zap_cursor_advance(&zc)) {
19253089abeschrock		if (za.za_integer_length == 8) {
19353089abeschrock			ASSERT(za.za_num_integers == 1);
19453089abeschrock			(void) printf("\t%s = %llu\n",
19553089abeschrock			    za.za_name, (u_longlong_t)za.za_first_integer);
19653089abeschrock		} else {
19753089abeschrock			ASSERT(za.za_integer_length == 1);
19853089abeschrock			char val[1024];
19953089abeschrock			VERIFY(zap_lookup(os, obj, za.za_name,
20053089abeschrock			    1, sizeof (val), val) == 0);
20153089abeschrock			(void) printf("\t%s = %s\n", za.za_name, val);
20253089abeschrock		}
20353089abeschrock	}
20453089abeschrock	zap_cursor_fini(&zc);
20553089abeschrock}
20653089abeschrock
20753089abeschrockstatic void
20853089abeschrockdump_mos(spa_t *spa)
20953089abeschrock{
21053089abeschrock	nvlist_t *nv = spa->spa_label_features;
21153089abeschrock
21253089abeschrock	(void) printf("label config:\n");
21353089abeschrock	for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
21453089abeschrock	    pair != NULL;
21553089abeschrock	    pair = nvlist_next_nvpair(nv, pair)) {
21653089abeschrock		(void) printf("\t%s\n", nvpair_name(pair));
21753089abeschrock	}
21853089abeschrock}
21953089abeschrock
22053089abeschrockstatic void
22153089abeschrockzhack_do_feature_stat(int argc, char **argv)
22253089abeschrock{
22353089abeschrock	spa_t *spa;
22453089abeschrock	objset_t *os;
22553089abeschrock	char *target;
22653089abeschrock
22753089abeschrock	argc--;
22853089abeschrock	argv++;
22953089abeschrock
23053089abeschrock	if (argc < 1) {
23153089abeschrock		(void) fprintf(stderr, "error: missing pool name\n");
23253089abeschrock		usage();
23353089abeschrock	}
23453089abeschrock	target = argv[0];
23553089abeschrock
23653089abeschrock	zhack_spa_open(target, B_TRUE, FTAG, &spa);
23753089abeschrock	os = spa->spa_meta_objset;
23853089abeschrock
23953089abeschrock	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
24053089abeschrock	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
24153089abeschrock	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
24243466aaMax Grossman	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
24343466aaMax Grossman		dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
24443466aaMax Grossman	}
24553089abeschrock	dump_mos(spa);
24653089abeschrock
24753089abeschrock	spa_close(spa, FTAG);
24853089abeschrock}
24953089abeschrock
25053089abeschrockstatic void
2512acef22Matthew Ahrenszhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
25253089abeschrock{
2533b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2543b2aab1Matthew Ahrens	zfeature_info_t *feature = arg;
25553089abeschrock
2562acef22Matthew Ahrens	feature_enable_sync(spa, feature, tx);
2572acef22Matthew Ahrens
2584445fffMatthew Ahrens	spa_history_log_internal(spa, "zhack enable feature", tx,
259ca0cc39Matthew Ahrens	    "guid=%s flags=%x",
260ca0cc39Matthew Ahrens	    feature->fi_guid, feature->fi_flags);
26153089abeschrock}
26253089abeschrock
26353089abeschrockstatic void
26453089abeschrockzhack_do_feature_enable(int argc, char **argv)
26553089abeschrock{
26653089abeschrock	char c;
26753089abeschrock	char *desc, *target;
26853089abeschrock	spa_t *spa;
26953089abeschrock	objset_t *mos;
27053089abeschrock	zfeature_info_t feature;
2712acef22Matthew Ahrens	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
27253089abeschrock
27353089abeschrock	/*
27453089abeschrock	 * Features are not added to the pool's label until their refcounts
27553089abeschrock	 * are incremented, so fi_mos can just be left as false for now.
27653089abeschrock	 */
27753089abeschrock	desc = NULL;
27853089abeschrock	feature.fi_uname = "zhack";
279ca0cc39Matthew Ahrens	feature.fi_flags = 0;
28053089abeschrock	feature.fi_depends = nodeps;
28143466aaMax Grossman	feature.fi_feature = SPA_FEATURE_NONE;
28253089abeschrock
28353089abeschrock	optind = 1;
28453089abeschrock	while ((c = getopt(argc, argv, "rmd:")) != -1) {
28553089abeschrock		switch (c) {
28653089abeschrock		case 'r':
287ca0cc39Matthew Ahrens			feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
28853089abeschrock			break;
28953089abeschrock		case 'd':
29053089abeschrock			desc = strdup(optarg);
29153089abeschrock			break;
29253089abeschrock		default:
29353089abeschrock			usage();
29453089abeschrock			break;
29553089abeschrock		}
29653089abeschrock	}
29753089abeschrock
29853089abeschrock	if (desc == NULL)
29953089abeschrock		desc = strdup("zhack injected");
30053089abeschrock	feature.fi_desc = desc;
30153089abeschrock
30253089abeschrock	argc -= optind;
30353089abeschrock	argv += optind;
30453089abeschrock
30553089abeschrock	if (argc < 2) {
30653089abeschrock		(void) fprintf(stderr, "error: missing feature or pool name\n");
30753089abeschrock		usage();
30853089abeschrock	}
30953089abeschrock	target = argv[0];
31053089abeschrock	feature.fi_guid = argv[1];
31153089abeschrock
31253089abeschrock	if (!zfeature_is_valid_guid(feature.fi_guid))
3137fdd916George Wilson		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
31453089abeschrock
31553089abeschrock	zhack_spa_open(target, B_FALSE, FTAG, &spa);
31653089abeschrock	mos = spa->spa_meta_objset;
31753089abeschrock
3182acef22Matthew Ahrens	if (zfeature_is_supported(feature.fi_guid))
3197fdd916George Wilson		fatal(spa, FTAG, "'%s' is a real feature, will not enable");
32053089abeschrock	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
3217fdd916George Wilson		fatal(spa, FTAG, "feature already enabled: %s",
3227fdd916George Wilson		    feature.fi_guid);
32353089abeschrock
3243b2aab1Matthew Ahrens	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3257d46dc6Matthew Ahrens	    zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
32653089abeschrock
32753089abeschrock	spa_close(spa, FTAG);
32853089abeschrock
32953089abeschrock	free(desc);
33053089abeschrock}
33153089abeschrock
33253089abeschrockstatic void
3333b2aab1Matthew Ahrensfeature_incr_sync(void *arg, dmu_tx_t *tx)
33453089abeschrock{
3353b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3363b2aab1Matthew Ahrens	zfeature_info_t *feature = arg;
3372acef22Matthew Ahrens	uint64_t refcount;
33853089abeschrock
33943466aaMax Grossman	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
3402acef22Matthew Ahrens	feature_sync(spa, feature, refcount + 1, tx);
3414445fffMatthew Ahrens	spa_history_log_internal(spa, "zhack feature incr", tx,
3425722177Christopher Siden	    "guid=%s", feature->fi_guid);
34353089abeschrock}
34453089abeschrock
34553089abeschrockstatic void
3463b2aab1Matthew Ahrensfeature_decr_sync(void *arg, dmu_tx_t *tx)
34753089abeschrock{
3483b2aab1Matthew Ahrens	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3493b2aab1Matthew Ahrens	zfeature_info_t *feature = arg;
3502acef22Matthew Ahrens	uint64_t refcount;
35153089abeschrock
35243466aaMax Grossman	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
3532acef22Matthew Ahrens	feature_sync(spa, feature, refcount - 1, tx);
3544445fffMatthew Ahrens	spa_history_log_internal(spa, "zhack feature decr", tx,
3555722177Christopher Siden	    "guid=%s", feature->fi_guid);
35653089abeschrock}
35753089abeschrock
35853089abeschrockstatic void
35953089abeschrockzhack_do_feature_ref(int argc, char **argv)
36053089abeschrock{
36153089abeschrock	char c;
36253089abeschrock	char *target;
36353089abeschrock	boolean_t decr = B_FALSE;
36453089abeschrock	spa_t *spa;
36553089abeschrock	objset_t *mos;
36653089abeschrock	zfeature_info_t feature;
3672acef22Matthew Ahrens	spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
36853089abeschrock
36953089abeschrock	/*
37053089abeschrock	 * fi_desc does not matter here because it was written to disk
37153089abeschrock	 * when the feature was enabled, but we need to properly set the
37253089abeschrock	 * feature for read or write based on the information we read off
37353089abeschrock	 * disk later.
37453089abeschrock	 */
37553089abeschrock	feature.fi_uname = "zhack";
376ca0cc39Matthew Ahrens	feature.fi_flags = 0;
37753089abeschrock	feature.fi_desc = NULL;
37853089abeschrock	feature.fi_depends = nodeps;
37943466aaMax Grossman	feature.fi_feature = SPA_FEATURE_NONE;
38053089abeschrock
38153089abeschrock	optind = 1;
38253089abeschrock	while ((c = getopt(argc, argv, "md")) != -1) {
38353089abeschrock		switch (c) {
38453089abeschrock		case 'm':
385ca0cc39Matthew Ahrens			feature.fi_flags |= ZFEATURE_FLAG_MOS;
38653089abeschrock			break;
38753089abeschrock		case 'd':
38853089abeschrock			decr = B_TRUE;
38953089abeschrock			break;
39053089abeschrock		default:
39153089abeschrock			usage();
39253089abeschrock			break;
39353089abeschrock		}
39453089abeschrock	}
39553089abeschrock	argc -= optind;
39653089abeschrock	argv += optind;
39753089abeschrock
39853089abeschrock	if (argc < 2) {
39953089abeschrock		(void) fprintf(stderr, "error: missing feature or pool name\n");
40053089abeschrock		usage();
40153089abeschrock	}
40253089abeschrock	target = argv[0];
40353089abeschrock	feature.fi_guid = argv[1];
40453089abeschrock
40553089abeschrock	if (!zfeature_is_valid_guid(feature.fi_guid))
4067fdd916George Wilson		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
40753089abeschrock
40853089abeschrock	zhack_spa_open(target, B_FALSE, FTAG, &spa);
40953089abeschrock	mos = spa->spa_meta_objset;
41053089abeschrock
4112acef22Matthew Ahrens	if (zfeature_is_supported(feature.fi_guid)) {
4122acef22Matthew Ahrens		fatal(spa, FTAG,
4132acef22Matthew Ahrens		    "'%s' is a real feature, will not change refcount");
4142acef22Matthew Ahrens	}
41553089abeschrock
41653089abeschrock	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
41753089abeschrock	    feature.fi_guid)) {
418ca0cc39Matthew Ahrens		feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
41953089abeschrock	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
42053089abeschrock	    feature.fi_guid)) {
421ca0cc39Matthew Ahrens		feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
42253089abeschrock	} else {
4237fdd916George Wilson		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
42453089abeschrock	}
42553089abeschrock
4262acef22Matthew Ahrens	if (decr) {
4272acef22Matthew Ahrens		uint64_t count;
42843466aaMax Grossman		if (feature_get_refcount_from_disk(spa, &feature,
42943466aaMax Grossman		    &count) == 0 && count != 0) {
4302acef22Matthew Ahrens			fatal(spa, FTAG, "feature refcount already 0: %s",
4312acef22Matthew Ahrens			    feature.fi_guid);
4322acef22Matthew Ahrens		}
4332acef22Matthew Ahrens	}
43453089abeschrock
4353b2aab1Matthew Ahrens	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
4367d46dc6Matthew Ahrens	    decr ? feature_decr_sync : feature_incr_sync, &feature,
4377d46dc6Matthew Ahrens	    5, ZFS_SPACE_CHECK_NORMAL));
43853089abeschrock
43953089abeschrock	spa_close(spa, FTAG);
44053089abeschrock}
44153089abeschrock
44253089abeschrockstatic int
44353089abeschrockzhack_do_feature(int argc, char **argv)
44453089abeschrock{
44553089abeschrock	char *subcommand;
44653089abeschrock
44753089abeschrock	argc--;
44853089abeschrock	argv++;
44953089abeschrock	if (argc == 0) {
45053089abeschrock		(void) fprintf(stderr,
45153089abeschrock		    "error: no feature operation specified\n");
45253089abeschrock		usage();
45353089abeschrock	}
45453089abeschrock
45553089abeschrock	subcommand = argv[0];
45653089abeschrock	if (strcmp(subcommand, "stat") == 0) {
45753089abeschrock		zhack_do_feature_stat(argc, argv);
45853089abeschrock	} else if (strcmp(subcommand, "enable") == 0) {
45953089abeschrock		zhack_do_feature_enable(argc, argv);
46053089abeschrock	} else if (strcmp(subcommand, "ref") == 0) {
46153089abeschrock		zhack_do_feature_ref(argc, argv);
46253089abeschrock	} else {
46353089abeschrock		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
46453089abeschrock		    subcommand);
46553089abeschrock		usage();
46653089abeschrock	}
46753089abeschrock
46853089abeschrock	return (0);
46953089abeschrock}
47053089abeschrock
47153089abeschrock#define	MAX_NUM_PATHS 1024
47253089abeschrock
47353089abeschrockint
47453089abeschrockmain(int argc, char **argv)
47553089abeschrock{
47653089abeschrock	extern void zfs_prop_init(void);
47753089abeschrock
47853089abeschrock	char *path[MAX_NUM_PATHS];
47953089abeschrock	const char *subcommand;
48053089abeschrock	int rv = 0;
48153089abeschrock	char c;
48253089abeschrock
48353089abeschrock	g_importargs.path = path;
48453089abeschrock
48553089abeschrock	dprintf_setup(&argc, argv);
48653089abeschrock	zfs_prop_init();
48753089abeschrock
48853089abeschrock	while ((c = getopt(argc, argv, "c:d:")) != -1) {
48953089abeschrock		switch (c) {
49053089abeschrock		case 'c':
49153089abeschrock			g_importargs.cachefile = optarg;
49253089abeschrock			break;
49353089abeschrock		case 'd':
49453089abeschrock			assert(g_importargs.paths < MAX_NUM_PATHS);
49553089abeschrock			g_importargs.path[g_importargs.paths++] = optarg;
49653089abeschrock			break;
49753089abeschrock		default:
49853089abeschrock			usage();
49953089abeschrock			break;
50053089abeschrock		}
50153089abeschrock	}
50253089abeschrock
50353089abeschrock	argc -= optind;
50453089abeschrock	argv += optind;
50553089abeschrock	optind = 1;
50653089abeschrock
50753089abeschrock	if (argc == 0) {
50853089abeschrock		(void) fprintf(stderr, "error: no command specified\n");
50953089abeschrock		usage();
51053089abeschrock	}
51153089abeschrock
51253089abeschrock	subcommand = argv[0];
51353089abeschrock
51453089abeschrock	if (strcmp(subcommand, "feature") == 0) {
51553089abeschrock		rv = zhack_do_feature(argc, argv);
51653089abeschrock	} else {
51753089abeschrock		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
51853089abeschrock		    subcommand);
51953089abeschrock		usage();
52053089abeschrock	}
52153089abeschrock
5227fdd916George Wilson	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
5237fdd916George Wilson		fatal(NULL, FTAG, "pool export failed; "
52453089abeschrock		    "changes may not be committed to disk\n");
52553089abeschrock	}
52653089abeschrock
52753089abeschrock	kernel_fini();
52853089abeschrock
52953089abeschrock	return (rv);
53053089abeschrock}
531