xref: /freebsd/sys/contrib/openzfs/cmd/zhack.c (revision 8a62a2a5659d1839d8799b4274c04469d7f17c78)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25  * Copyright (c) 2013 Steven Hartland. All rights reserved.
26  */
27 
28 /*
29  * zhack is a debugging tool that can write changes to ZFS pool using libzpool
30  * for testing purposes. Altering pools with zhack is unsupported and may
31  * result in corrupted pools.
32  */
33 
34 #include <zfs_prop.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39 #include <sys/zfs_context.h>
40 #include <sys/spa.h>
41 #include <sys/spa_impl.h>
42 #include <sys/dmu.h>
43 #include <sys/zap.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/dsl_synctask.h>
46 #include <sys/vdev.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/dsl_pool.h>
51 #include <sys/zio_checksum.h>
52 #include <sys/zio_compress.h>
53 #include <sys/zfeature.h>
54 #include <sys/dmu_tx.h>
55 #include <sys/backtrace.h>
56 #include <zfeature_common.h>
57 #include <libzutil.h>
58 #include <sys/metaslab_impl.h>
59 #include <libzpool.h>
60 
61 static importargs_t g_importargs;
62 static char *g_pool;
63 static boolean_t g_readonly;
64 static boolean_t g_dump_dbgmsg;
65 
66 typedef enum {
67 	ZHACK_REPAIR_OP_UNKNOWN  = 0,
68 	ZHACK_REPAIR_OP_CKSUM    = (1 << 0),
69 	ZHACK_REPAIR_OP_UNDETACH = (1 << 1)
70 } zhack_repair_op_t;
71 
72 static __attribute__((noreturn)) void
usage(void)73 usage(void)
74 {
75 	(void) fprintf(stderr,
76 	    "Usage: zhack [-o tunable] [-c cachefile] [-d dir] [-G] "
77 	    "<subcommand> <args> ...\n"
78 	    "       where <subcommand> <args> is one of the following:\n"
79 	    "\n");
80 
81 	(void) fprintf(stderr,
82 	    "    global options:\n"
83 	    "    -c <cachefile>   reads config from the given cachefile\n"
84 	    "    -d <dir>         directory with vdevs for import\n"
85 	    "    -o var=value...  set global variable to an unsigned "
86 	    "32-bit integer\n"
87 	    "    -G               dump zfs_dbgmsg buffer before exiting\n"
88 	    "\n"
89 	    "    action idle <pool> [-f] [-t seconds]\n"
90 	    "        import the pool for a set time then export it\n"
91 	    "        -t <seconds> sets the time the pool is imported\n"
92 	    "\n"
93 	    "    feature stat <pool>\n"
94 	    "        print information about enabled features\n"
95 	    "    feature enable [-r] [-d desc] <pool> <feature>\n"
96 	    "        add a new enabled feature to the pool\n"
97 	    "        -d <desc> sets the feature's description\n"
98 	    "        -r set read-only compatible flag for feature\n"
99 	    "    feature ref [-md] <pool> <feature>\n"
100 	    "        change the refcount on the given feature\n"
101 	    "        -d decrease instead of increase the refcount\n"
102 	    "        -m add the feature to the label if increasing refcount\n"
103 	    "\n"
104 	    "    <feature> : should be a feature guid\n"
105 	    "\n"
106 	    "    label repair <device>\n"
107 	    "        repair labels of a specified device according to options\n"
108 	    "        which may be combined to do their functions in one call\n"
109 	    "        -c repair corrupted label checksums\n"
110 	    "        -u restore the label on a detached device\n"
111 	    "\n"
112 	    "    <device> : path to vdev\n"
113 	    "\n"
114 	    "    metaslab leak <pool>\n"
115 	    "        apply allocation map from zdb to specified pool\n");
116 	exit(1);
117 }
118 
119 static void
dump_debug_buffer(void)120 dump_debug_buffer(void)
121 {
122 	ssize_t ret __attribute__((unused));
123 
124 	if (!g_dump_dbgmsg)
125 		return;
126 
127 	/*
128 	 * We use write() instead of printf() so that this function
129 	 * is safe to call from a signal handler.
130 	 */
131 	ret = write(STDERR_FILENO, "\n", 1);
132 	zfs_dbgmsg_print(STDERR_FILENO, "zhack");
133 }
134 
sig_handler(int signo)135 static void sig_handler(int signo)
136 {
137 	struct sigaction action;
138 
139 	libspl_backtrace(STDERR_FILENO);
140 	dump_debug_buffer();
141 
142 	/*
143 	 * Restore default action and re-raise signal so SIGSEGV and
144 	 * SIGABRT can trigger a core dump.
145 	 */
146 	action.sa_handler = SIG_DFL;
147 	sigemptyset(&action.sa_mask);
148 	action.sa_flags = 0;
149 	(void) sigaction(signo, &action, NULL);
150 	raise(signo);
151 }
152 
153 static __attribute__((format(printf, 3, 4))) __attribute__((noreturn)) void
fatal(spa_t * spa,const void * tag,const char * fmt,...)154 fatal(spa_t *spa, const void *tag, const char *fmt, ...)
155 {
156 	va_list ap;
157 
158 	if (spa != NULL) {
159 		spa_close(spa, tag);
160 		(void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
161 	}
162 
163 	va_start(ap, fmt);
164 	(void) fputs("zhack: ", stderr);
165 	(void) vfprintf(stderr, fmt, ap);
166 	va_end(ap);
167 	(void) fputc('\n', stderr);
168 
169 	dump_debug_buffer();
170 
171 	exit(1);
172 }
173 
174 static int
space_delta_cb(dmu_object_type_t bonustype,const void * data,zfs_file_info_t * zoi)175 space_delta_cb(dmu_object_type_t bonustype, const void *data,
176     zfs_file_info_t *zoi)
177 {
178 	(void) data, (void) zoi;
179 
180 	/*
181 	 * Is it a valid type of object to track?
182 	 */
183 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
184 		return (ENOENT);
185 	(void) fprintf(stderr, "modifying object that needs user accounting");
186 	abort();
187 }
188 
189 /*
190  * Target is the dataset whose pool we want to open.
191  */
192 static void
zhack_import(char * target,boolean_t readonly)193 zhack_import(char *target, boolean_t readonly)
194 {
195 	nvlist_t *config;
196 	nvlist_t *props;
197 	int error;
198 
199 	kernel_init(readonly ? SPA_MODE_READ :
200 	    (SPA_MODE_READ | SPA_MODE_WRITE));
201 
202 	dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
203 
204 	g_readonly = readonly;
205 	g_importargs.can_be_active = readonly;
206 	g_pool = strdup(target);
207 
208 	libpc_handle_t lpch = {
209 		.lpc_lib_handle = NULL,
210 		.lpc_ops = &libzpool_config_ops,
211 		.lpc_printerr = B_TRUE
212 	};
213 	error = zpool_find_config(&lpch, target, &config, &g_importargs);
214 	if (error)
215 		fatal(NULL, FTAG, "cannot import '%s'", target);
216 
217 	props = NULL;
218 	if (readonly) {
219 		VERIFY0(nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
220 		VERIFY0(nvlist_add_uint64(props,
221 		    zpool_prop_to_name(ZPOOL_PROP_READONLY), 1));
222 	}
223 
224 	zfeature_checks_disable = B_TRUE;
225 	error = spa_import(target, config, props,
226 	    (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
227 	fnvlist_free(config);
228 	zfeature_checks_disable = B_FALSE;
229 	if (error == EEXIST)
230 		error = 0;
231 
232 	if (error)
233 		fatal(NULL, FTAG, "can't import '%s': %s", target,
234 		    strerror(error));
235 }
236 
237 static void
zhack_spa_open(char * target,boolean_t readonly,const void * tag,spa_t ** spa)238 zhack_spa_open(char *target, boolean_t readonly, const void *tag, spa_t **spa)
239 {
240 	int err;
241 
242 	zhack_import(target, readonly);
243 
244 	zfeature_checks_disable = B_TRUE;
245 	err = spa_open(target, spa, tag);
246 	zfeature_checks_disable = B_FALSE;
247 
248 	if (err != 0)
249 		fatal(*spa, FTAG, "cannot open '%s': %s", target,
250 		    strerror(err));
251 	if (spa_version(*spa) < SPA_VERSION_FEATURES) {
252 		fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
253 		    target, (int)spa_version(*spa));
254 	}
255 }
256 
257 static void
dump_obj(objset_t * os,uint64_t obj,const char * name)258 dump_obj(objset_t *os, uint64_t obj, const char *name)
259 {
260 	zap_cursor_t zc;
261 	zap_attribute_t *za = zap_attribute_long_alloc();
262 
263 	(void) printf("%s_obj:\n", name);
264 
265 	for (zap_cursor_init(&zc, os, obj);
266 	    zap_cursor_retrieve(&zc, za) == 0;
267 	    zap_cursor_advance(&zc)) {
268 		if (za->za_integer_length == 8) {
269 			ASSERT(za->za_num_integers == 1);
270 			(void) printf("\t%s = %llu\n",
271 			    za->za_name, (u_longlong_t)za->za_first_integer);
272 		} else {
273 			ASSERT(za->za_integer_length == 1);
274 			char val[1024];
275 			VERIFY0(zap_lookup(os, obj, za->za_name,
276 			    1, sizeof (val), val));
277 			(void) printf("\t%s = %s\n", za->za_name, val);
278 		}
279 	}
280 	zap_cursor_fini(&zc);
281 	zap_attribute_free(za);
282 }
283 
284 static void
dump_mos(spa_t * spa)285 dump_mos(spa_t *spa)
286 {
287 	nvlist_t *nv = spa->spa_label_features;
288 	nvpair_t *pair;
289 
290 	(void) printf("label config:\n");
291 	for (pair = nvlist_next_nvpair(nv, NULL);
292 	    pair != NULL;
293 	    pair = nvlist_next_nvpair(nv, pair)) {
294 		(void) printf("\t%s\n", nvpair_name(pair));
295 	}
296 }
297 
298 static void
zhack_do_feature_stat(int argc,char ** argv)299 zhack_do_feature_stat(int argc, char **argv)
300 {
301 	spa_t *spa;
302 	objset_t *os;
303 	char *target;
304 
305 	argc--;
306 	argv++;
307 
308 	if (argc < 1) {
309 		(void) fprintf(stderr, "error: missing pool name\n");
310 		usage();
311 	}
312 	target = argv[0];
313 
314 	zhack_spa_open(target, B_TRUE, FTAG, &spa);
315 	os = spa->spa_meta_objset;
316 
317 	dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
318 	dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
319 	dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
320 	if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
321 		dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
322 	}
323 	dump_mos(spa);
324 
325 	spa_close(spa, FTAG);
326 }
327 
328 static void
zhack_feature_enable_sync(void * arg,dmu_tx_t * tx)329 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
330 {
331 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
332 	zfeature_info_t *feature = arg;
333 
334 	feature_enable_sync(spa, feature, tx);
335 
336 	spa_history_log_internal(spa, "zhack enable feature", tx,
337 	    "name=%s flags=%u",
338 	    feature->fi_guid, feature->fi_flags);
339 }
340 
341 static void
zhack_do_feature_enable(int argc,char ** argv)342 zhack_do_feature_enable(int argc, char **argv)
343 {
344 	int c;
345 	char *desc, *target;
346 	spa_t *spa;
347 	objset_t *mos;
348 	zfeature_info_t feature;
349 	const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
350 
351 	/*
352 	 * Features are not added to the pool's label until their refcounts
353 	 * are incremented, so fi_mos can just be left as false for now.
354 	 */
355 	desc = NULL;
356 	feature.fi_uname = "zhack";
357 	feature.fi_flags = 0;
358 	feature.fi_depends = nodeps;
359 	feature.fi_feature = SPA_FEATURE_NONE;
360 
361 	optind = 1;
362 	while ((c = getopt(argc, argv, "+rd:")) != -1) {
363 		switch (c) {
364 		case 'r':
365 			feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
366 			break;
367 		case 'd':
368 			if (desc != NULL)
369 				free(desc);
370 			desc = strdup(optarg);
371 			break;
372 		default:
373 			usage();
374 			break;
375 		}
376 	}
377 
378 	if (desc == NULL)
379 		desc = strdup("zhack injected");
380 	feature.fi_desc = desc;
381 
382 	argc -= optind;
383 	argv += optind;
384 
385 	if (argc < 2) {
386 		(void) fprintf(stderr, "error: missing feature or pool name\n");
387 		usage();
388 	}
389 	target = argv[0];
390 	feature.fi_guid = argv[1];
391 
392 	if (!zfeature_is_valid_guid(feature.fi_guid))
393 		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
394 
395 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
396 	mos = spa->spa_meta_objset;
397 
398 	if (zfeature_is_supported(feature.fi_guid))
399 		fatal(spa, FTAG, "'%s' is a real feature, will not enable",
400 		    feature.fi_guid);
401 	if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
402 		fatal(spa, FTAG, "feature already enabled: %s",
403 		    feature.fi_guid);
404 
405 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
406 	    zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
407 
408 	spa_close(spa, FTAG);
409 
410 	free(desc);
411 }
412 
413 static void
feature_incr_sync(void * arg,dmu_tx_t * tx)414 feature_incr_sync(void *arg, dmu_tx_t *tx)
415 {
416 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
417 	zfeature_info_t *feature = arg;
418 	uint64_t refcount;
419 
420 	mutex_enter(&spa->spa_feat_stats_lock);
421 	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
422 	feature_sync(spa, feature, refcount + 1, tx);
423 	spa_history_log_internal(spa, "zhack feature incr", tx,
424 	    "name=%s", feature->fi_guid);
425 	mutex_exit(&spa->spa_feat_stats_lock);
426 }
427 
428 static void
feature_decr_sync(void * arg,dmu_tx_t * tx)429 feature_decr_sync(void *arg, dmu_tx_t *tx)
430 {
431 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
432 	zfeature_info_t *feature = arg;
433 	uint64_t refcount;
434 
435 	mutex_enter(&spa->spa_feat_stats_lock);
436 	VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
437 	feature_sync(spa, feature, refcount - 1, tx);
438 	spa_history_log_internal(spa, "zhack feature decr", tx,
439 	    "name=%s", feature->fi_guid);
440 	mutex_exit(&spa->spa_feat_stats_lock);
441 }
442 
443 static void
zhack_do_feature_ref(int argc,char ** argv)444 zhack_do_feature_ref(int argc, char **argv)
445 {
446 	int c;
447 	char *target;
448 	boolean_t decr = B_FALSE;
449 	spa_t *spa;
450 	objset_t *mos;
451 	zfeature_info_t feature;
452 	const spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
453 
454 	/*
455 	 * fi_desc does not matter here because it was written to disk
456 	 * when the feature was enabled, but we need to properly set the
457 	 * feature for read or write based on the information we read off
458 	 * disk later.
459 	 */
460 	feature.fi_uname = "zhack";
461 	feature.fi_flags = 0;
462 	feature.fi_desc = NULL;
463 	feature.fi_depends = nodeps;
464 	feature.fi_feature = SPA_FEATURE_NONE;
465 
466 	optind = 1;
467 	while ((c = getopt(argc, argv, "+md")) != -1) {
468 		switch (c) {
469 		case 'm':
470 			feature.fi_flags |= ZFEATURE_FLAG_MOS;
471 			break;
472 		case 'd':
473 			decr = B_TRUE;
474 			break;
475 		default:
476 			usage();
477 			break;
478 		}
479 	}
480 	argc -= optind;
481 	argv += optind;
482 
483 	if (argc < 2) {
484 		(void) fprintf(stderr, "error: missing feature or pool name\n");
485 		usage();
486 	}
487 	target = argv[0];
488 	feature.fi_guid = argv[1];
489 
490 	if (!zfeature_is_valid_guid(feature.fi_guid))
491 		fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
492 
493 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
494 	mos = spa->spa_meta_objset;
495 
496 	if (zfeature_is_supported(feature.fi_guid)) {
497 		fatal(spa, FTAG,
498 		    "'%s' is a real feature, will not change refcount",
499 		    feature.fi_guid);
500 	}
501 
502 	if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
503 	    feature.fi_guid)) {
504 		feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
505 	} else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
506 	    feature.fi_guid)) {
507 		feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
508 	} else {
509 		fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
510 	}
511 
512 	if (decr) {
513 		uint64_t count;
514 		if (feature_get_refcount_from_disk(spa, &feature,
515 		    &count) == 0 && count == 0) {
516 			fatal(spa, FTAG, "feature refcount already 0: %s",
517 			    feature.fi_guid);
518 		}
519 	}
520 
521 	VERIFY0(dsl_sync_task(spa_name(spa), NULL,
522 	    decr ? feature_decr_sync : feature_incr_sync, &feature,
523 	    5, ZFS_SPACE_CHECK_NORMAL));
524 
525 	spa_close(spa, FTAG);
526 }
527 
528 static int
zhack_do_feature(int argc,char ** argv)529 zhack_do_feature(int argc, char **argv)
530 {
531 	char *subcommand;
532 
533 	argc--;
534 	argv++;
535 	if (argc == 0) {
536 		(void) fprintf(stderr,
537 		    "error: no feature operation specified\n");
538 		usage();
539 	}
540 
541 	subcommand = argv[0];
542 	if (strcmp(subcommand, "stat") == 0) {
543 		zhack_do_feature_stat(argc, argv);
544 	} else if (strcmp(subcommand, "enable") == 0) {
545 		zhack_do_feature_enable(argc, argv);
546 	} else if (strcmp(subcommand, "ref") == 0) {
547 		zhack_do_feature_ref(argc, argv);
548 	} else {
549 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
550 		    subcommand);
551 		usage();
552 	}
553 
554 	return (0);
555 }
556 
557 static void
zhack_do_action_idle(int argc,char ** argv)558 zhack_do_action_idle(int argc, char **argv)
559 {
560 	spa_t *spa;
561 	char *target, *tmp;
562 	int idle_time = 0;
563 	int c;
564 
565 	optind = 1;
566 	while ((c = getopt(argc, argv, "+t:")) != -1) {
567 		switch (c) {
568 		case 't':
569 			idle_time = strtol(optarg, &tmp, 0);
570 			if (*tmp) {
571 				(void) fprintf(stderr, "error: time must "
572 				    "be an integer in seconds: %s\n", tmp);
573 				usage();
574 			}
575 			if (idle_time < 0) {
576 				(void) fprintf(stderr, "error: time must "
577 				    "not be negative: %d\n", idle_time);
578 				usage();
579 			}
580 			break;
581 		default:
582 			usage();
583 			break;
584 		}
585 	}
586 	argc -= optind;
587 	argv += optind;
588 
589 	if (argc < 1) {
590 		(void) fprintf(stderr, "error: missing pool name\n");
591 		usage();
592 	}
593 	target = argv[0];
594 
595 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
596 
597 	fprintf(stdout, "Imported pool %s, idle for %d seconds\n",
598 	    target, idle_time);
599 	sleep(idle_time);
600 
601 	spa_close(spa, FTAG);
602 }
603 
604 static int
zhack_do_action(int argc,char ** argv)605 zhack_do_action(int argc, char **argv)
606 {
607 	char *subcommand;
608 
609 	argc--;
610 	argv++;
611 	if (argc == 0) {
612 		(void) fprintf(stderr,
613 		    "error: no import operation specified\n");
614 		usage();
615 	}
616 
617 	subcommand = argv[0];
618 	if (strcmp(subcommand, "idle") == 0) {
619 		zhack_do_action_idle(argc, argv);
620 	} else {
621 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
622 		    subcommand);
623 		usage();
624 	}
625 
626 	return (0);
627 }
628 
629 
630 static boolean_t
strstarts(const char * a,const char * b)631 strstarts(const char *a, const char *b)
632 {
633 	return (strncmp(a, b, strlen(b)) == 0);
634 }
635 
636 static void
metaslab_force_alloc(metaslab_t * msp,uint64_t start,uint64_t size,dmu_tx_t * tx)637 metaslab_force_alloc(metaslab_t *msp, uint64_t start, uint64_t size,
638     dmu_tx_t *tx)
639 {
640 	ASSERT(msp->ms_disabled);
641 	ASSERT(MUTEX_HELD(&msp->ms_lock));
642 	uint64_t txg = dmu_tx_get_txg(tx);
643 
644 	uint64_t off = start;
645 	while (off < start + size) {
646 		uint64_t ostart, osize;
647 		boolean_t found = zfs_range_tree_find_in(msp->ms_allocatable,
648 		    off, start + size - off, &ostart, &osize);
649 		if (!found)
650 			break;
651 		zfs_range_tree_remove(msp->ms_allocatable, ostart, osize);
652 
653 		if (zfs_range_tree_is_empty(msp->ms_allocating[txg & TXG_MASK]))
654 			vdev_dirty(msp->ms_group->mg_vd, VDD_METASLAB, msp,
655 			    txg);
656 
657 		zfs_range_tree_add(msp->ms_allocating[txg & TXG_MASK], ostart,
658 		    osize);
659 		msp->ms_allocating_total += osize;
660 		off = ostart + osize;
661 	}
662 }
663 
664 static void
zhack_do_metaslab_leak(int argc,char ** argv)665 zhack_do_metaslab_leak(int argc, char **argv)
666 {
667 	int c;
668 	char *target;
669 	spa_t *spa;
670 
671 	optind = 1;
672 	boolean_t force = B_FALSE;
673 	while ((c = getopt(argc, argv, "f")) != -1) {
674 		switch (c) {
675 		case 'f':
676 			force = B_TRUE;
677 			break;
678 		default:
679 			usage();
680 			break;
681 		}
682 	}
683 
684 	argc -= optind;
685 	argv += optind;
686 
687 	if (argc < 1) {
688 		(void) fprintf(stderr, "error: missing pool name\n");
689 		usage();
690 	}
691 	target = argv[0];
692 
693 	zhack_spa_open(target, B_FALSE, FTAG, &spa);
694 	spa_config_enter(spa, SCL_VDEV | SCL_ALLOC, FTAG, RW_READER);
695 
696 	char *line = NULL;
697 	size_t cap = 0;
698 
699 	vdev_t *vd = NULL;
700 	metaslab_t *prev = NULL;
701 	dmu_tx_t *tx = NULL;
702 	while (getline(&line, &cap, stdin) > 0) {
703 		if (strstarts(line, "\tvdev ")) {
704 			uint64_t vdev_id, ms_shift;
705 			if (sscanf(line,
706 			    "\tvdev %10"PRIu64"\t%*s  metaslab shift %4"PRIu64,
707 			    &vdev_id, &ms_shift) == 1) {
708 				VERIFY3U(sscanf(line, "\tvdev %"PRIu64
709 				    "\t  metaslab shift %4"PRIu64,
710 				    &vdev_id, &ms_shift), ==, 2);
711 			}
712 			vd = vdev_lookup_top(spa, vdev_id);
713 			if (vd == NULL) {
714 				fprintf(stderr, "error: no such vdev with "
715 				    "id %"PRIu64"\n", vdev_id);
716 				break;
717 			}
718 			if (tx) {
719 				dmu_tx_commit(tx);
720 				mutex_exit(&prev->ms_lock);
721 				metaslab_enable(prev, B_FALSE, B_FALSE);
722 				tx = NULL;
723 				prev = NULL;
724 			}
725 			if (vd->vdev_ms_shift != ms_shift) {
726 				fprintf(stderr, "error: ms_shift mismatch: %"
727 				    PRIu64" != %"PRIu64"\n", vd->vdev_ms_shift,
728 				    ms_shift);
729 				break;
730 			}
731 		} else if (strstarts(line, "\tmetaslabs ")) {
732 			uint64_t ms_count;
733 			VERIFY3U(sscanf(line, "\tmetaslabs %"PRIu64, &ms_count),
734 			    ==, 1);
735 			ASSERT(vd);
736 			if (!force && vd->vdev_ms_count != ms_count) {
737 				fprintf(stderr, "error: ms_count mismatch: %"
738 				    PRIu64" != %"PRIu64"\n", vd->vdev_ms_count,
739 				    ms_count);
740 				break;
741 			}
742 		} else if (strstarts(line, "ALLOC:")) {
743 			uint64_t start, size;
744 			VERIFY3U(sscanf(line, "ALLOC: %"PRIu64" %"PRIu64"\n",
745 			    &start, &size), ==, 2);
746 
747 			ASSERT(vd);
748 			size_t idx;
749 			idx = start >> vd->vdev_ms_shift;
750 			if (idx >= vd->vdev_ms_count)
751 				continue;
752 			metaslab_t *cur = vd->vdev_ms[idx];
753 			if (prev != cur) {
754 				if (prev) {
755 					dmu_tx_commit(tx);
756 					mutex_exit(&prev->ms_lock);
757 					metaslab_enable(prev, B_FALSE, B_FALSE);
758 				}
759 				ASSERT(cur);
760 				metaslab_disable(cur);
761 				mutex_enter(&cur->ms_lock);
762 				metaslab_load(cur);
763 				prev = cur;
764 				tx = dmu_tx_create_dd(
765 				    spa_get_dsl(vd->vdev_spa)->dp_root_dir);
766 				dmu_tx_assign(tx, DMU_TX_WAIT);
767 			}
768 
769 			metaslab_force_alloc(cur, start, size, tx);
770 		} else {
771 			continue;
772 		}
773 	}
774 	if (tx) {
775 		dmu_tx_commit(tx);
776 		mutex_exit(&prev->ms_lock);
777 		metaslab_enable(prev, B_FALSE, B_FALSE);
778 		tx = NULL;
779 		prev = NULL;
780 	}
781 	if (line)
782 		free(line);
783 
784 	spa_config_exit(spa, SCL_VDEV | SCL_ALLOC, FTAG);
785 	spa_close(spa, FTAG);
786 }
787 
788 static int
zhack_do_metaslab(int argc,char ** argv)789 zhack_do_metaslab(int argc, char **argv)
790 {
791 	char *subcommand;
792 
793 	argc--;
794 	argv++;
795 	if (argc == 0) {
796 		(void) fprintf(stderr,
797 		    "error: no metaslab operation specified\n");
798 		usage();
799 	}
800 
801 	subcommand = argv[0];
802 	if (strcmp(subcommand, "leak") == 0) {
803 		zhack_do_metaslab_leak(argc, argv);
804 	} else {
805 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
806 		    subcommand);
807 		usage();
808 	}
809 
810 	return (0);
811 }
812 
813 #define	ASHIFT_UBERBLOCK_SHIFT(ashift)	\
814 	MIN(MAX(ashift, UBERBLOCK_SHIFT), \
815 	MAX_UBERBLOCK_SHIFT)
816 #define	ASHIFT_UBERBLOCK_SIZE(ashift) \
817 	(1ULL << ASHIFT_UBERBLOCK_SHIFT(ashift))
818 
819 #define	REPAIR_LABEL_STATUS_CKSUM (1 << 0)
820 #define	REPAIR_LABEL_STATUS_UB    (1 << 1)
821 
822 static int
zhack_repair_read_label(const int fd,vdev_label_t * vl,const uint64_t label_offset,const int l)823 zhack_repair_read_label(const int fd, vdev_label_t *vl,
824     const uint64_t label_offset, const int l)
825 {
826 	const int err = pread64(fd, vl, sizeof (vdev_label_t), label_offset);
827 
828 	if (err == -1) {
829 		(void) fprintf(stderr,
830 		    "error: cannot read label %d: %s\n",
831 		    l, strerror(errno));
832 		return (err);
833 	} else if (err != sizeof (vdev_label_t)) {
834 		(void) fprintf(stderr,
835 		    "error: bad label %d read size\n", l);
836 		return (err);
837 	}
838 
839 	return (0);
840 }
841 
842 static int
zhack_repair_get_byteswap(const zio_eck_t * vdev_eck,const int l,int * byteswap)843 zhack_repair_get_byteswap(const zio_eck_t *vdev_eck, const int l, int *byteswap)
844 {
845 	if (vdev_eck->zec_magic == ZEC_MAGIC) {
846 		*byteswap = B_FALSE;
847 	} else if (vdev_eck->zec_magic == BSWAP_64((uint64_t)ZEC_MAGIC)) {
848 		*byteswap = B_TRUE;
849 	} else {
850 		(void) fprintf(stderr, "error: label %d: "
851 		    "Expected the nvlist checksum magic number but instead got "
852 		    "0x%" PRIx64 "\n",
853 		    l, vdev_eck->zec_magic);
854 		return (1);
855 	}
856 	return (0);
857 }
858 
859 static void
zhack_repair_calc_cksum(const int byteswap,void * data,const uint64_t offset,const uint64_t abdsize,zio_eck_t * eck,zio_cksum_t * cksum)860 zhack_repair_calc_cksum(const int byteswap, void *data, const uint64_t offset,
861     const uint64_t abdsize, zio_eck_t *eck, zio_cksum_t *cksum)
862 {
863 	zio_cksum_t verifier;
864 	zio_cksum_t current_cksum;
865 	zio_checksum_info_t *ci;
866 	abd_t *abd;
867 
868 	ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0);
869 
870 	if (byteswap)
871 		byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
872 
873 	current_cksum = eck->zec_cksum;
874 	eck->zec_cksum = verifier;
875 
876 	ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL];
877 	abd = abd_get_from_buf(data, abdsize);
878 	ci->ci_func[byteswap](abd, abdsize, NULL, cksum);
879 	abd_free(abd);
880 
881 	eck->zec_cksum = current_cksum;
882 }
883 
884 static int
zhack_repair_get_ashift(nvlist_t * cfg,const int l,uint64_t * ashift)885 zhack_repair_get_ashift(nvlist_t *cfg, const int l, uint64_t *ashift)
886 {
887 	int err;
888 	nvlist_t *vdev_tree_cfg;
889 
890 	err = nvlist_lookup_nvlist(cfg,
891 	    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree_cfg);
892 	if (err) {
893 		(void) fprintf(stderr,
894 		    "error: label %d: cannot find nvlist key %s\n",
895 		    l, ZPOOL_CONFIG_VDEV_TREE);
896 		return (err);
897 	}
898 
899 	err = nvlist_lookup_uint64(vdev_tree_cfg,
900 	    ZPOOL_CONFIG_ASHIFT, ashift);
901 	if (err) {
902 		(void) fprintf(stderr,
903 		    "error: label %d: cannot find nvlist key %s\n",
904 		    l, ZPOOL_CONFIG_ASHIFT);
905 		return (err);
906 	}
907 
908 	if (*ashift == 0) {
909 		(void) fprintf(stderr,
910 		    "error: label %d: nvlist key %s is zero\n",
911 		    l, ZPOOL_CONFIG_ASHIFT);
912 		return (1);
913 	}
914 
915 	return (0);
916 }
917 
918 static int
zhack_repair_undetach(uberblock_t * ub,nvlist_t * cfg,const int l)919 zhack_repair_undetach(uberblock_t *ub, nvlist_t *cfg, const int l)
920 {
921 	/*
922 	 * Uberblock root block pointer has valid birth TXG.
923 	 * Copying it to the label NVlist
924 	 */
925 	if (BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp) != 0) {
926 		const uint64_t txg = BP_GET_LOGICAL_BIRTH(&ub->ub_rootbp);
927 		int err;
928 
929 		ub->ub_txg = txg;
930 
931 		err = nvlist_remove_all(cfg, ZPOOL_CONFIG_CREATE_TXG);
932 		if (err) {
933 			(void) fprintf(stderr,
934 			    "error: label %d: "
935 			    "Failed to remove pool creation TXG\n",
936 			    l);
937 			return (err);
938 		}
939 
940 		err = nvlist_remove_all(cfg, ZPOOL_CONFIG_POOL_TXG);
941 		if (err) {
942 			(void) fprintf(stderr,
943 			    "error: label %d: Failed to remove pool TXG to "
944 			    "be replaced.\n",
945 			    l);
946 			return (err);
947 		}
948 
949 		err = nvlist_add_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, txg);
950 		if (err) {
951 			(void) fprintf(stderr,
952 			    "error: label %d: "
953 			    "Failed to add pool TXG of %" PRIu64 "\n",
954 			    l, txg);
955 			return (err);
956 		}
957 	}
958 
959 	return (0);
960 }
961 
962 static boolean_t
zhack_repair_write_label(const int l,const int fd,const int byteswap,void * data,zio_eck_t * eck,const uint64_t offset,const uint64_t abdsize)963 zhack_repair_write_label(const int l, const int fd, const int byteswap,
964     void *data, zio_eck_t *eck, const uint64_t offset, const uint64_t abdsize)
965 {
966 	zio_cksum_t actual_cksum;
967 	zhack_repair_calc_cksum(byteswap, data, offset, abdsize, eck,
968 	    &actual_cksum);
969 	zio_cksum_t expected_cksum = eck->zec_cksum;
970 	ssize_t err;
971 
972 	if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
973 		return (B_FALSE);
974 
975 	eck->zec_cksum = actual_cksum;
976 
977 	err = pwrite64(fd, data, abdsize, offset);
978 	if (err == -1) {
979 		(void) fprintf(stderr, "error: cannot write label %d: %s\n",
980 		    l, strerror(errno));
981 		return (B_FALSE);
982 	} else if (err != abdsize) {
983 		(void) fprintf(stderr, "error: bad write size label %d\n", l);
984 		return (B_FALSE);
985 	} else {
986 		(void) fprintf(stderr,
987 		    "label %d: wrote %" PRIu64 " bytes at offset %" PRIu64 "\n",
988 		    l, abdsize, offset);
989 	}
990 
991 	return (B_TRUE);
992 }
993 
994 static void
zhack_repair_write_uberblock(vdev_label_t * vl,const int l,const uint64_t ashift,const int fd,const int byteswap,const uint64_t label_offset,uint32_t * labels_repaired)995 zhack_repair_write_uberblock(vdev_label_t *vl, const int l,
996     const uint64_t ashift, const int fd, const int byteswap,
997     const uint64_t label_offset, uint32_t *labels_repaired)
998 {
999 	void *ub_data =
1000 	    (char *)vl + offsetof(vdev_label_t, vl_uberblock);
1001 	zio_eck_t *ub_eck =
1002 	    (zio_eck_t *)
1003 	    ((char *)(ub_data) + (ASHIFT_UBERBLOCK_SIZE(ashift))) - 1;
1004 
1005 	if (ub_eck->zec_magic != 0) {
1006 		(void) fprintf(stderr,
1007 		    "error: label %d: "
1008 		    "Expected Uberblock checksum magic number to "
1009 		    "be 0, but got %" PRIu64 "\n",
1010 		    l, ub_eck->zec_magic);
1011 		(void) fprintf(stderr, "It would appear there's already "
1012 		    "a checksum for the uberblock.\n");
1013 		return;
1014 	}
1015 
1016 
1017 	ub_eck->zec_magic = byteswap ? BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC;
1018 
1019 	if (zhack_repair_write_label(l, fd, byteswap,
1020 	    ub_data, ub_eck,
1021 	    label_offset + offsetof(vdev_label_t, vl_uberblock),
1022 	    ASHIFT_UBERBLOCK_SIZE(ashift)))
1023 			labels_repaired[l] |= REPAIR_LABEL_STATUS_UB;
1024 }
1025 
1026 static void
zhack_repair_print_cksum(FILE * stream,const zio_cksum_t * cksum)1027 zhack_repair_print_cksum(FILE *stream, const zio_cksum_t *cksum)
1028 {
1029 	(void) fprintf(stream,
1030 	    "%016llx:%016llx:%016llx:%016llx",
1031 	    (u_longlong_t)cksum->zc_word[0],
1032 	    (u_longlong_t)cksum->zc_word[1],
1033 	    (u_longlong_t)cksum->zc_word[2],
1034 	    (u_longlong_t)cksum->zc_word[3]);
1035 }
1036 
1037 static int
zhack_repair_test_cksum(const int byteswap,void * vdev_data,zio_eck_t * vdev_eck,const uint64_t vdev_phys_offset,const int l)1038 zhack_repair_test_cksum(const int byteswap, void *vdev_data,
1039     zio_eck_t *vdev_eck, const uint64_t vdev_phys_offset, const int l)
1040 {
1041 	const zio_cksum_t expected_cksum = vdev_eck->zec_cksum;
1042 	zio_cksum_t actual_cksum;
1043 	zhack_repair_calc_cksum(byteswap, vdev_data, vdev_phys_offset,
1044 	    VDEV_PHYS_SIZE, vdev_eck, &actual_cksum);
1045 	const uint64_t expected_magic = byteswap ?
1046 	    BSWAP_64(ZEC_MAGIC) : ZEC_MAGIC;
1047 	const uint64_t actual_magic = vdev_eck->zec_magic;
1048 	int err = 0;
1049 
1050 	if (actual_magic != expected_magic) {
1051 		(void) fprintf(stderr, "error: label %d: "
1052 		    "Expected "
1053 		    "the nvlist checksum magic number to not be %"
1054 		    PRIu64 " not %" PRIu64 "\n",
1055 		    l, expected_magic, actual_magic);
1056 		err = ECKSUM;
1057 	}
1058 	if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) {
1059 		(void) fprintf(stderr, "error: label %d: "
1060 		    "Expected the nvlist checksum to be ", l);
1061 		(void) zhack_repair_print_cksum(stderr,
1062 		    &expected_cksum);
1063 		(void) fprintf(stderr, " not ");
1064 		zhack_repair_print_cksum(stderr, &actual_cksum);
1065 		(void) fprintf(stderr, "\n");
1066 		err = ECKSUM;
1067 	}
1068 	return (err);
1069 }
1070 
1071 static int
zhack_repair_unpack_cfg(vdev_label_t * vl,const int l,nvlist_t ** cfg)1072 zhack_repair_unpack_cfg(vdev_label_t *vl, const int l, nvlist_t **cfg)
1073 {
1074 	const char *cfg_keys[] = { ZPOOL_CONFIG_VERSION,
1075 	    ZPOOL_CONFIG_POOL_STATE, ZPOOL_CONFIG_GUID };
1076 	int err;
1077 
1078 	err = nvlist_unpack(vl->vl_vdev_phys.vp_nvlist,
1079 	    VDEV_PHYS_SIZE - sizeof (zio_eck_t), cfg, 0);
1080 	if (err) {
1081 		(void) fprintf(stderr,
1082 		    "error: cannot unpack nvlist label %d\n", l);
1083 		return (err);
1084 	}
1085 
1086 	for (int i = 0; i < ARRAY_SIZE(cfg_keys); i++) {
1087 		uint64_t val;
1088 		err = nvlist_lookup_uint64(*cfg, cfg_keys[i], &val);
1089 		if (err) {
1090 			(void) fprintf(stderr,
1091 			    "error: label %d, %d: "
1092 			    "cannot find nvlist key %s\n",
1093 			    l, i, cfg_keys[i]);
1094 			return (err);
1095 		}
1096 	}
1097 
1098 	return (0);
1099 }
1100 
1101 static void
zhack_repair_one_label(const zhack_repair_op_t op,const int fd,vdev_label_t * vl,const uint64_t label_offset,const int l,uint32_t * labels_repaired)1102 zhack_repair_one_label(const zhack_repair_op_t op, const int fd,
1103     vdev_label_t *vl, const uint64_t label_offset, const int l,
1104     uint32_t *labels_repaired)
1105 {
1106 	ssize_t err;
1107 	uberblock_t *ub = (uberblock_t *)vl->vl_uberblock;
1108 	void *vdev_data =
1109 	    (char *)vl + offsetof(vdev_label_t, vl_vdev_phys);
1110 	zio_eck_t *vdev_eck =
1111 	    (zio_eck_t *)((char *)(vdev_data) + VDEV_PHYS_SIZE) - 1;
1112 	const uint64_t vdev_phys_offset =
1113 	    label_offset + offsetof(vdev_label_t, vl_vdev_phys);
1114 	nvlist_t *cfg;
1115 	uint64_t ashift;
1116 	int byteswap;
1117 
1118 	err = zhack_repair_read_label(fd, vl, label_offset, l);
1119 	if (err)
1120 		return;
1121 
1122 	err = zhack_repair_get_byteswap(vdev_eck, l, &byteswap);
1123 	if (err)
1124 		return;
1125 
1126 	if (byteswap) {
1127 		byteswap_uint64_array(&vdev_eck->zec_cksum,
1128 		    sizeof (zio_cksum_t));
1129 		vdev_eck->zec_magic = BSWAP_64(vdev_eck->zec_magic);
1130 	}
1131 
1132 	if ((op & ZHACK_REPAIR_OP_CKSUM) == 0 &&
1133 	    zhack_repair_test_cksum(byteswap, vdev_data, vdev_eck,
1134 	    vdev_phys_offset, l) != 0) {
1135 		(void) fprintf(stderr, "It would appear checksums are "
1136 		    "corrupted. Try zhack repair label -c <device>\n");
1137 		return;
1138 	}
1139 
1140 	err = zhack_repair_unpack_cfg(vl, l, &cfg);
1141 	if (err)
1142 		return;
1143 
1144 	if ((op & ZHACK_REPAIR_OP_UNDETACH) != 0) {
1145 		char *buf;
1146 		size_t buflen;
1147 
1148 		if (ub->ub_txg != 0) {
1149 			(void) fprintf(stderr,
1150 			    "error: label %d: UB TXG of 0 expected, but got %"
1151 			    PRIu64 "\n", l, ub->ub_txg);
1152 			(void) fprintf(stderr, "It would appear the device was "
1153 			    "not properly detached.\n");
1154 			return;
1155 		}
1156 
1157 		err = zhack_repair_get_ashift(cfg, l, &ashift);
1158 		if (err)
1159 			return;
1160 
1161 		err = zhack_repair_undetach(ub, cfg, l);
1162 		if (err)
1163 			return;
1164 
1165 		buf = vl->vl_vdev_phys.vp_nvlist;
1166 		buflen = VDEV_PHYS_SIZE - sizeof (zio_eck_t);
1167 		if (nvlist_pack(cfg, &buf, &buflen, NV_ENCODE_XDR, 0) != 0) {
1168 			(void) fprintf(stderr,
1169 			    "error: label %d: Failed to pack nvlist\n", l);
1170 			return;
1171 		}
1172 
1173 		zhack_repair_write_uberblock(vl,
1174 		    l, ashift, fd, byteswap, label_offset, labels_repaired);
1175 	}
1176 
1177 	if (zhack_repair_write_label(l, fd, byteswap, vdev_data, vdev_eck,
1178 	    vdev_phys_offset, VDEV_PHYS_SIZE))
1179 			labels_repaired[l] |= REPAIR_LABEL_STATUS_CKSUM;
1180 
1181 	fsync(fd);
1182 }
1183 
1184 static const char *
zhack_repair_label_status(const uint32_t label_status,const uint32_t to_check)1185 zhack_repair_label_status(const uint32_t label_status,
1186     const uint32_t to_check)
1187 {
1188 	return ((label_status & to_check) != 0 ? "repaired" : "skipped");
1189 }
1190 
1191 static int
zhack_label_repair(const zhack_repair_op_t op,const int argc,char ** argv)1192 zhack_label_repair(const zhack_repair_op_t op, const int argc, char **argv)
1193 {
1194 	uint32_t labels_repaired[VDEV_LABELS] = {0};
1195 	vdev_label_t labels[VDEV_LABELS] = {{{0}}};
1196 	struct stat64 st;
1197 	int fd;
1198 	off_t filesize;
1199 	uint32_t repaired = 0;
1200 
1201 	abd_init();
1202 
1203 	if (argc < 1) {
1204 		(void) fprintf(stderr, "error: missing device\n");
1205 		usage();
1206 	}
1207 
1208 	if ((fd = open(argv[0], O_RDWR)) == -1)
1209 		fatal(NULL, FTAG, "cannot open '%s': %s", argv[0],
1210 		    strerror(errno));
1211 
1212 	if (fstat64_blk(fd, &st) != 0)
1213 		fatal(NULL, FTAG, "cannot stat '%s': %s", argv[0],
1214 		    strerror(errno));
1215 
1216 	filesize = st.st_size;
1217 	(void) fprintf(stderr, "Calculated filesize to be %jd\n",
1218 	    (intmax_t)filesize);
1219 
1220 	if (filesize % sizeof (vdev_label_t) != 0)
1221 		filesize =
1222 		    (filesize / sizeof (vdev_label_t)) * sizeof (vdev_label_t);
1223 
1224 	for (int l = 0; l < VDEV_LABELS; l++) {
1225 		zhack_repair_one_label(op, fd, &labels[l],
1226 		    vdev_label_offset(filesize, l, 0), l, labels_repaired);
1227 	}
1228 
1229 	close(fd);
1230 
1231 	abd_fini();
1232 
1233 	for (int l = 0; l < VDEV_LABELS; l++) {
1234 		const uint32_t lr = labels_repaired[l];
1235 		(void) printf("label %d: ", l);
1236 		(void) printf("uberblock: %s ",
1237 		    zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_UB));
1238 		(void) printf("checksum: %s\n",
1239 		    zhack_repair_label_status(lr, REPAIR_LABEL_STATUS_CKSUM));
1240 		repaired |= lr;
1241 	}
1242 
1243 	if (repaired > 0)
1244 		return (0);
1245 
1246 	return (1);
1247 }
1248 
1249 static int
zhack_do_label_repair(int argc,char ** argv)1250 zhack_do_label_repair(int argc, char **argv)
1251 {
1252 	zhack_repair_op_t op = ZHACK_REPAIR_OP_UNKNOWN;
1253 	int c;
1254 
1255 	optind = 1;
1256 	while ((c = getopt(argc, argv, "+cu")) != -1) {
1257 		switch (c) {
1258 		case 'c':
1259 			op |= ZHACK_REPAIR_OP_CKSUM;
1260 			break;
1261 		case 'u':
1262 			op |= ZHACK_REPAIR_OP_UNDETACH;
1263 			break;
1264 		default:
1265 			usage();
1266 			break;
1267 		}
1268 	}
1269 
1270 	argc -= optind;
1271 	argv += optind;
1272 
1273 	if (op == ZHACK_REPAIR_OP_UNKNOWN)
1274 		op = ZHACK_REPAIR_OP_CKSUM;
1275 
1276 	return (zhack_label_repair(op, argc, argv));
1277 }
1278 
1279 static int
zhack_do_label(int argc,char ** argv)1280 zhack_do_label(int argc, char **argv)
1281 {
1282 	char *subcommand;
1283 	int err;
1284 
1285 	argc--;
1286 	argv++;
1287 	if (argc == 0) {
1288 		(void) fprintf(stderr,
1289 		    "error: no label operation specified\n");
1290 		usage();
1291 	}
1292 
1293 	subcommand = argv[0];
1294 	if (strcmp(subcommand, "repair") == 0) {
1295 		err = zhack_do_label_repair(argc, argv);
1296 	} else {
1297 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
1298 		    subcommand);
1299 		usage();
1300 	}
1301 
1302 	return (err);
1303 }
1304 
1305 #define	MAX_NUM_PATHS 1024
1306 
1307 int
main(int argc,char ** argv)1308 main(int argc, char **argv)
1309 {
1310 	struct sigaction action;
1311 	char *path[MAX_NUM_PATHS];
1312 	const char *subcommand;
1313 	int rv = 0;
1314 	int c;
1315 
1316 	/*
1317 	 * Set up signal handlers, so if we crash due to bad on-disk data we
1318 	 * can get more info. Unlike ztest, we don't bail out if we can't set
1319 	 * up signal handlers, because zhack is very useful without them.
1320 	 */
1321 	action.sa_handler = sig_handler;
1322 	sigemptyset(&action.sa_mask);
1323 	action.sa_flags = 0;
1324 	if (sigaction(SIGSEGV, &action, NULL) < 0) {
1325 		(void) fprintf(stderr, "zhack: cannot catch SIGSEGV: %s\n",
1326 		    strerror(errno));
1327 	}
1328 	if (sigaction(SIGABRT, &action, NULL) < 0) {
1329 		(void) fprintf(stderr, "zhack: cannot catch SIGABRT: %s\n",
1330 		    strerror(errno));
1331 	}
1332 
1333 	g_importargs.path = path;
1334 
1335 	dprintf_setup(&argc, argv);
1336 	zfs_prop_init();
1337 
1338 	while ((c = getopt(argc, argv, "+c:d:Go:")) != -1) {
1339 		switch (c) {
1340 		case 'c':
1341 			g_importargs.cachefile = optarg;
1342 			break;
1343 		case 'd':
1344 			assert(g_importargs.paths < MAX_NUM_PATHS);
1345 			g_importargs.path[g_importargs.paths++] = optarg;
1346 			break;
1347 		case 'G':
1348 			g_dump_dbgmsg = B_TRUE;
1349 			break;
1350 		case 'o':
1351 			if (handle_tunable_option(optarg, B_FALSE) != 0)
1352 				exit(1);
1353 			break;
1354 		default:
1355 			usage();
1356 			break;
1357 		}
1358 	}
1359 
1360 	argc -= optind;
1361 	argv += optind;
1362 	optind = 1;
1363 
1364 	if (argc == 0) {
1365 		(void) fprintf(stderr, "error: no command specified\n");
1366 		usage();
1367 	}
1368 
1369 	subcommand = argv[0];
1370 
1371 	if (strcmp(subcommand, "action") == 0) {
1372 		rv = zhack_do_action(argc, argv);
1373 	} else if (strcmp(subcommand, "feature") == 0) {
1374 		rv = zhack_do_feature(argc, argv);
1375 	} else if (strcmp(subcommand, "label") == 0) {
1376 		return (zhack_do_label(argc, argv));
1377 	} else if (strcmp(subcommand, "metaslab") == 0) {
1378 		rv = zhack_do_metaslab(argc, argv);
1379 	} else {
1380 		(void) fprintf(stderr, "error: unknown subcommand: %s\n",
1381 		    subcommand);
1382 		usage();
1383 	}
1384 
1385 	if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
1386 		fatal(NULL, FTAG, "pool export failed; "
1387 		    "changes may not be committed to disk\n");
1388 	}
1389 
1390 	if (g_dump_dbgmsg)
1391 		dump_debug_buffer();
1392 
1393 	kernel_fini();
1394 
1395 	return (rv);
1396 }
1397