zhack.c revision 263389
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/*
23263389Sdelphij * Copyright (c) 2013 by Delphix. All rights reserved.
24252764Sdelphij * 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>
50249643Smm#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
88263389Sdelphijfatal(spa_t *spa, void *tag, const char *fmt, ...)
89236884Smm{
90236884Smm	va_list ap;
91236884Smm
92263389Sdelphij	if (spa != NULL) {
93263389Sdelphij		spa_close(spa, tag);
94263389Sdelphij		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
95263389Sdelphij	}
96263389Sdelphij
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
162252764Sdelphij	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) {
167263389Sdelphij				fatal(spa, FTAG, "cannot import '%s': pool is "
168263389Sdelphij				    "active; run " "\"zpool export %s\" "
169263389Sdelphij				    "first\n", g_pool, g_pool);
170236884Smm			}
171236884Smm		}
172236884Smm
173263389Sdelphij		fatal(NULL, FTAG, "cannot import '%s': no such pool "
174263389Sdelphij		    "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)
195263389Sdelphij		fatal(NULL, FTAG, "can't import '%s': %s", name,
196263389Sdelphij		    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)
211263389Sdelphij		fatal(*spa, FTAG, "cannot open '%s': %s", target,
212263389Sdelphij		    strerror(err));
213236884Smm	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
214263389Sdelphij		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
215263389Sdelphij		    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");
280236884Smm	dump_mos(spa);
281236884Smm
282236884Smm	spa_close(spa, FTAG);
283236884Smm}
284236884Smm
285236884Smmstatic void
286249643Smmfeature_enable_sync(void *arg, dmu_tx_t *tx)
287236884Smm{
288249643Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
289249643Smm	zfeature_info_t *feature = arg;
290236884Smm
291236884Smm	spa_feature_enable(spa, feature, tx);
292249643Smm	spa_history_log_internal(spa, "zhack enable feature", tx,
293249643Smm	    "name=%s can_readonly=%u",
294249643Smm	    feature->fi_guid, feature->fi_can_readonly);
295236884Smm}
296236884Smm
297236884Smmstatic void
298236884Smmzhack_do_feature_enable(int argc, char **argv)
299236884Smm{
300236884Smm	char c;
301236884Smm	char *desc, *target;
302236884Smm	spa_t *spa;
303236884Smm	objset_t *mos;
304236884Smm	zfeature_info_t feature;
305236884Smm	zfeature_info_t *nodeps[] = { NULL };
306236884Smm
307236884Smm	/*
308236884Smm	 * Features are not added to the pool's label until their refcounts
309236884Smm	 * are incremented, so fi_mos can just be left as false for now.
310236884Smm	 */
311236884Smm	desc = NULL;
312236884Smm	feature.fi_uname = "zhack";
313236884Smm	feature.fi_mos = B_FALSE;
314236884Smm	feature.fi_can_readonly = B_FALSE;
315236884Smm	feature.fi_depends = nodeps;
316236884Smm
317236884Smm	optind = 1;
318236884Smm	while ((c = getopt(argc, argv, "rmd:")) != -1) {
319236884Smm		switch (c) {
320236884Smm		case 'r':
321236884Smm			feature.fi_can_readonly = B_TRUE;
322236884Smm			break;
323236884Smm		case 'd':
324236884Smm			desc = strdup(optarg);
325236884Smm			break;
326236884Smm		default:
327236884Smm			usage();
328236884Smm			break;
329236884Smm		}
330236884Smm	}
331236884Smm
332236884Smm	if (desc == NULL)
333236884Smm		desc = strdup("zhack injected");
334236884Smm	feature.fi_desc = desc;
335236884Smm
336236884Smm	argc -= optind;
337236884Smm	argv += optind;
338236884Smm
339236884Smm	if (argc < 2) {
340236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
341236884Smm		usage();
342236884Smm	}
343236884Smm	target = argv[0];
344236884Smm	feature.fi_guid = argv[1];
345236884Smm
346236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
347263389Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
348236884Smm
349236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
350236884Smm	mos = spa->spa_meta_objset;
351236884Smm
352236884Smm	if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
353263389Sdelphij		fatal(spa, FTAG, "'%s' is a real feature, will not enable");
354236884Smm	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
355263389Sdelphij		fatal(spa, FTAG, "feature already enabled: %s",
356263389Sdelphij		    feature.fi_guid);
357236884Smm
358249643Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
359249643Smm	    feature_enable_sync, &feature, 5));
360236884Smm
361236884Smm	spa_close(spa, FTAG);
362236884Smm
363236884Smm	free(desc);
364236884Smm}
365236884Smm
366236884Smmstatic void
367249643Smmfeature_incr_sync(void *arg, dmu_tx_t *tx)
368236884Smm{
369249643Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
370249643Smm	zfeature_info_t *feature = arg;
371236884Smm
372236884Smm	spa_feature_incr(spa, feature, tx);
373249643Smm	spa_history_log_internal(spa, "zhack feature incr", tx,
374249643Smm	    "name=%s", feature->fi_guid);
375236884Smm}
376236884Smm
377236884Smmstatic void
378249643Smmfeature_decr_sync(void *arg, dmu_tx_t *tx)
379236884Smm{
380249643Smm	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
381249643Smm	zfeature_info_t *feature = arg;
382236884Smm
383236884Smm	spa_feature_decr(spa, feature, tx);
384249643Smm	spa_history_log_internal(spa, "zhack feature decr", tx,
385249643Smm	    "name=%s", feature->fi_guid);
386236884Smm}
387236884Smm
388236884Smmstatic void
389236884Smmzhack_do_feature_ref(int argc, char **argv)
390236884Smm{
391236884Smm	char c;
392236884Smm	char *target;
393236884Smm	boolean_t decr = B_FALSE;
394236884Smm	spa_t *spa;
395236884Smm	objset_t *mos;
396236884Smm	zfeature_info_t feature;
397236884Smm	zfeature_info_t *nodeps[] = { NULL };
398236884Smm
399236884Smm	/*
400236884Smm	 * fi_desc does not matter here because it was written to disk
401236884Smm	 * when the feature was enabled, but we need to properly set the
402236884Smm	 * feature for read or write based on the information we read off
403236884Smm	 * disk later.
404236884Smm	 */
405236884Smm	feature.fi_uname = "zhack";
406236884Smm	feature.fi_mos = B_FALSE;
407236884Smm	feature.fi_desc = NULL;
408236884Smm	feature.fi_depends = nodeps;
409236884Smm
410236884Smm	optind = 1;
411236884Smm	while ((c = getopt(argc, argv, "md")) != -1) {
412236884Smm		switch (c) {
413236884Smm		case 'm':
414236884Smm			feature.fi_mos = B_TRUE;
415236884Smm			break;
416236884Smm		case 'd':
417236884Smm			decr = B_TRUE;
418236884Smm			break;
419236884Smm		default:
420236884Smm			usage();
421236884Smm			break;
422236884Smm		}
423236884Smm	}
424236884Smm	argc -= optind;
425236884Smm	argv += optind;
426236884Smm
427236884Smm	if (argc < 2) {
428236884Smm		(void) fprintf(stderr, "error: missing feature or pool name\n");
429236884Smm		usage();
430236884Smm	}
431236884Smm	target = argv[0];
432236884Smm	feature.fi_guid = argv[1];
433236884Smm
434236884Smm	if (!zfeature_is_valid_guid(feature.fi_guid))
435263389Sdelphij		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
436236884Smm
437236884Smm	zhack_spa_open(target, B_FALSE, FTAG, &spa);
438236884Smm	mos = spa->spa_meta_objset;
439236884Smm
440236884Smm	if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
441263389Sdelphij		fatal(spa, FTAG, "'%s' is a real feature, will not change "
442263389Sdelphij		    "refcount");
443236884Smm
444236884Smm	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
445236884Smm	    feature.fi_guid)) {
446236884Smm		feature.fi_can_readonly = B_FALSE;
447236884Smm	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
448236884Smm	    feature.fi_guid)) {
449236884Smm		feature.fi_can_readonly = B_TRUE;
450236884Smm	} else {
451263389Sdelphij		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
452236884Smm	}
453236884Smm
454236884Smm	if (decr && !spa_feature_is_active(spa, &feature))
455263389Sdelphij		fatal(spa, FTAG, "feature refcount already 0: %s",
456263389Sdelphij		    feature.fi_guid);
457236884Smm
458249643Smm	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
459249643Smm	    decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
460236884Smm
461236884Smm	spa_close(spa, FTAG);
462236884Smm}
463236884Smm
464236884Smmstatic int
465236884Smmzhack_do_feature(int argc, char **argv)
466236884Smm{
467236884Smm	char *subcommand;
468236884Smm
469236884Smm	argc--;
470236884Smm	argv++;
471236884Smm	if (argc == 0) {
472236884Smm		(void) fprintf(stderr,
473236884Smm		    "error: no feature operation specified\n");
474236884Smm		usage();
475236884Smm	}
476236884Smm
477236884Smm	subcommand = argv[0];
478236884Smm	if (strcmp(subcommand, "stat") == 0) {
479236884Smm		zhack_do_feature_stat(argc, argv);
480236884Smm	} else if (strcmp(subcommand, "enable") == 0) {
481236884Smm		zhack_do_feature_enable(argc, argv);
482236884Smm	} else if (strcmp(subcommand, "ref") == 0) {
483236884Smm		zhack_do_feature_ref(argc, argv);
484236884Smm	} else {
485236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
486236884Smm		    subcommand);
487236884Smm		usage();
488236884Smm	}
489236884Smm
490236884Smm	return (0);
491236884Smm}
492236884Smm
493236884Smm#define	MAX_NUM_PATHS 1024
494236884Smm
495236884Smmint
496236884Smmmain(int argc, char **argv)
497236884Smm{
498236884Smm	extern void zfs_prop_init(void);
499236884Smm
500236884Smm	char *path[MAX_NUM_PATHS];
501236884Smm	const char *subcommand;
502236884Smm	int rv = 0;
503236884Smm	char c;
504236884Smm
505236884Smm	g_importargs.path = path;
506236884Smm
507236884Smm	dprintf_setup(&argc, argv);
508236884Smm	zfs_prop_init();
509236884Smm
510236884Smm	while ((c = getopt(argc, argv, "c:d:")) != -1) {
511236884Smm		switch (c) {
512236884Smm		case 'c':
513236884Smm			g_importargs.cachefile = optarg;
514236884Smm			break;
515236884Smm		case 'd':
516236884Smm			assert(g_importargs.paths < MAX_NUM_PATHS);
517236884Smm			g_importargs.path[g_importargs.paths++] = optarg;
518236884Smm			break;
519236884Smm		default:
520236884Smm			usage();
521236884Smm			break;
522236884Smm		}
523236884Smm	}
524236884Smm
525236884Smm	argc -= optind;
526236884Smm	argv += optind;
527236884Smm	optind = 1;
528236884Smm
529236884Smm	if (argc == 0) {
530236884Smm		(void) fprintf(stderr, "error: no command specified\n");
531236884Smm		usage();
532236884Smm	}
533236884Smm
534236884Smm	subcommand = argv[0];
535236884Smm
536236884Smm	if (strcmp(subcommand, "feature") == 0) {
537236884Smm		rv = zhack_do_feature(argc, argv);
538236884Smm	} else {
539236884Smm		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
540236884Smm		    subcommand);
541236884Smm		usage();
542236884Smm	}
543236884Smm
544263389Sdelphij	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
545263389Sdelphij		fatal(NULL, FTAG, "pool export failed; "
546236884Smm		    "changes may not be committed to disk\n");
547236884Smm	}
548236884Smm
549236884Smm	libzfs_fini(g_zfs);
550236884Smm	kernel_fini();
551236884Smm
552236884Smm	return (rv);
553236884Smm}
554