Add "compatibility" property for zpool feature sets
[zfs.git] / cmd / zhack / zhack.c
blob08263120c7c441cf265f07f0a16dee4a9d18129c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * zhack is a debugging tool that can write changes to ZFS pool using libzpool
29 * for testing purposes. Altering pools with zhack is unsupported and may
30 * result in corrupted pools.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <sys/zfs_context.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/dmu.h>
40 #include <sys/zap.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/dsl_synctask.h>
43 #include <sys/vdev.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/dmu_objset.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/zio_checksum.h>
48 #include <sys/zio_compress.h>
49 #include <sys/zfeature.h>
50 #include <sys/dmu_tx.h>
51 #include <libzutil.h>
53 extern boolean_t zfeature_checks_disable;
55 const char cmdname[] = "zhack";
56 static importargs_t g_importargs;
57 static char *g_pool;
58 static boolean_t g_readonly;
60 static void
61 usage(void)
63 (void) fprintf(stderr,
64 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
65 "where <subcommand> <args> is one of the following:\n"
66 "\n", cmdname);
68 (void) fprintf(stderr,
69 " feature stat <pool>\n"
70 " print information about enabled features\n"
71 " feature enable [-r] [-d desc] <pool> <feature>\n"
72 " add a new enabled feature to the pool\n"
73 " -d <desc> sets the feature's description\n"
74 " -r set read-only compatible flag for feature\n"
75 " feature ref [-md] <pool> <feature>\n"
76 " change the refcount on the given feature\n"
77 " -d decrease instead of increase the refcount\n"
78 " -m add the feature to the label if increasing refcount\n"
79 "\n"
80 " <feature> : should be a feature guid\n");
81 exit(1);
85 static void
86 fatal(spa_t *spa, void *tag, const char *fmt, ...)
88 va_list ap;
90 if (spa != NULL) {
91 spa_close(spa, tag);
92 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
95 va_start(ap, fmt);
96 (void) fprintf(stderr, "%s: ", cmdname);
97 (void) vfprintf(stderr, fmt, ap);
98 va_end(ap);
99 (void) fprintf(stderr, "\n");
101 exit(1);
104 /* ARGSUSED */
105 static int
106 space_delta_cb(dmu_object_type_t bonustype, const void *data,
107 zfs_file_info_t *zoi)
110 * Is it a valid type of object to track?
112 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
113 return (ENOENT);
114 (void) fprintf(stderr, "modifying object that needs user accounting");
115 abort();
116 /* NOTREACHED */
120 * Target is the dataset whose pool we want to open.
122 static void
123 zhack_import(char *target, boolean_t readonly)
125 nvlist_t *config;
126 nvlist_t *props;
127 int error;
129 kernel_init(readonly ? SPA_MODE_READ :
130 (SPA_MODE_READ | SPA_MODE_WRITE));
132 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
134 g_readonly = readonly;
135 g_importargs.can_be_active = readonly;
136 g_pool = strdup(target);
138 error = zpool_find_config(NULL, target, &config, &g_importargs,
139 &libzpool_config_ops);
140 if (error)
141 fatal(NULL, FTAG, "cannot import '%s'", target);
143 props = NULL;
144 if (readonly) {
145 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
146 VERIFY(nvlist_add_uint64(props,
147 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
150 zfeature_checks_disable = B_TRUE;
151 error = spa_import(target, config, props,
152 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
153 fnvlist_free(config);
154 zfeature_checks_disable = B_FALSE;
155 if (error == EEXIST)
156 error = 0;
158 if (error)
159 fatal(NULL, FTAG, "can't import '%s': %s", target,
160 strerror(error));
163 static void
164 zhack_spa_open(char *target, boolean_t readonly, void *tag, spa_t **spa)
166 int err;
168 zhack_import(target, readonly);
170 zfeature_checks_disable = B_TRUE;
171 err = spa_open(target, spa, tag);
172 zfeature_checks_disable = B_FALSE;
174 if (err != 0)
175 fatal(*spa, FTAG, "cannot open '%s': %s", target,
176 strerror(err));
177 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
178 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
179 target, (int)spa_version(*spa));
183 static void
184 dump_obj(objset_t *os, uint64_t obj, const char *name)
186 zap_cursor_t zc;
187 zap_attribute_t za;
189 (void) printf("%s_obj:\n", name);
191 for (zap_cursor_init(&zc, os, obj);
192 zap_cursor_retrieve(&zc, &za) == 0;
193 zap_cursor_advance(&zc)) {
194 if (za.za_integer_length == 8) {
195 ASSERT(za.za_num_integers == 1);
196 (void) printf("\t%s = %llu\n",
197 za.za_name, (u_longlong_t)za.za_first_integer);
198 } else {
199 ASSERT(za.za_integer_length == 1);
200 char val[1024];
201 VERIFY(zap_lookup(os, obj, za.za_name,
202 1, sizeof (val), val) == 0);
203 (void) printf("\t%s = %s\n", za.za_name, val);
206 zap_cursor_fini(&zc);
209 static void
210 dump_mos(spa_t *spa)
212 nvlist_t *nv = spa->spa_label_features;
213 nvpair_t *pair;
215 (void) printf("label config:\n");
216 for (pair = nvlist_next_nvpair(nv, NULL);
217 pair != NULL;
218 pair = nvlist_next_nvpair(nv, pair)) {
219 (void) printf("\t%s\n", nvpair_name(pair));
223 static void
224 zhack_do_feature_stat(int argc, char **argv)
226 spa_t *spa;
227 objset_t *os;
228 char *target;
230 argc--;
231 argv++;
233 if (argc < 1) {
234 (void) fprintf(stderr, "error: missing pool name\n");
235 usage();
237 target = argv[0];
239 zhack_spa_open(target, B_TRUE, FTAG, &spa);
240 os = spa->spa_meta_objset;
242 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
243 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
244 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
245 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
246 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
248 dump_mos(spa);
250 spa_close(spa, FTAG);
253 static void
254 zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
256 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
257 zfeature_info_t *feature = arg;
259 feature_enable_sync(spa, feature, tx);
261 spa_history_log_internal(spa, "zhack enable feature", tx,
262 "name=%s flags=%u",
263 feature->fi_guid, feature->fi_flags);
266 static void
267 zhack_do_feature_enable(int argc, char **argv)
269 int c;
270 char *desc, *target;
271 spa_t *spa;
272 objset_t *mos;
273 zfeature_info_t feature;
274 spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
277 * Features are not added to the pool's label until their refcounts
278 * are incremented, so fi_mos can just be left as false for now.
280 desc = NULL;
281 feature.fi_uname = "zhack";
282 feature.fi_flags = 0;
283 feature.fi_depends = nodeps;
284 feature.fi_feature = SPA_FEATURE_NONE;
286 optind = 1;
287 while ((c = getopt(argc, argv, "+rd:")) != -1) {
288 switch (c) {
289 case 'r':
290 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
291 break;
292 case 'd':
293 desc = strdup(optarg);
294 break;
295 default:
296 usage();
297 break;
301 if (desc == NULL)
302 desc = strdup("zhack injected");
303 feature.fi_desc = desc;
305 argc -= optind;
306 argv += optind;
308 if (argc < 2) {
309 (void) fprintf(stderr, "error: missing feature or pool name\n");
310 usage();
312 target = argv[0];
313 feature.fi_guid = argv[1];
315 if (!zfeature_is_valid_guid(feature.fi_guid))
316 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
318 zhack_spa_open(target, B_FALSE, FTAG, &spa);
319 mos = spa->spa_meta_objset;
321 if (zfeature_is_supported(feature.fi_guid))
322 fatal(spa, FTAG, "'%s' is a real feature, will not enable");
323 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
324 fatal(spa, FTAG, "feature already enabled: %s",
325 feature.fi_guid);
327 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
328 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
330 spa_close(spa, FTAG);
332 free(desc);
335 static void
336 feature_incr_sync(void *arg, dmu_tx_t *tx)
338 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
339 zfeature_info_t *feature = arg;
340 uint64_t refcount;
342 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
343 feature_sync(spa, feature, refcount + 1, tx);
344 spa_history_log_internal(spa, "zhack feature incr", tx,
345 "name=%s", feature->fi_guid);
348 static void
349 feature_decr_sync(void *arg, dmu_tx_t *tx)
351 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
352 zfeature_info_t *feature = arg;
353 uint64_t refcount;
355 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
356 feature_sync(spa, feature, refcount - 1, tx);
357 spa_history_log_internal(spa, "zhack feature decr", tx,
358 "name=%s", feature->fi_guid);
361 static void
362 zhack_do_feature_ref(int argc, char **argv)
364 int c;
365 char *target;
366 boolean_t decr = B_FALSE;
367 spa_t *spa;
368 objset_t *mos;
369 zfeature_info_t feature;
370 spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
373 * fi_desc does not matter here because it was written to disk
374 * when the feature was enabled, but we need to properly set the
375 * feature for read or write based on the information we read off
376 * disk later.
378 feature.fi_uname = "zhack";
379 feature.fi_flags = 0;
380 feature.fi_desc = NULL;
381 feature.fi_depends = nodeps;
382 feature.fi_feature = SPA_FEATURE_NONE;
384 optind = 1;
385 while ((c = getopt(argc, argv, "+md")) != -1) {
386 switch (c) {
387 case 'm':
388 feature.fi_flags |= ZFEATURE_FLAG_MOS;
389 break;
390 case 'd':
391 decr = B_TRUE;
392 break;
393 default:
394 usage();
395 break;
398 argc -= optind;
399 argv += optind;
401 if (argc < 2) {
402 (void) fprintf(stderr, "error: missing feature or pool name\n");
403 usage();
405 target = argv[0];
406 feature.fi_guid = argv[1];
408 if (!zfeature_is_valid_guid(feature.fi_guid))
409 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
411 zhack_spa_open(target, B_FALSE, FTAG, &spa);
412 mos = spa->spa_meta_objset;
414 if (zfeature_is_supported(feature.fi_guid)) {
415 fatal(spa, FTAG,
416 "'%s' is a real feature, will not change refcount");
419 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
420 feature.fi_guid)) {
421 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
422 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
423 feature.fi_guid)) {
424 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
425 } else {
426 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
429 if (decr) {
430 uint64_t count;
431 if (feature_get_refcount_from_disk(spa, &feature,
432 &count) == 0 && count == 0) {
433 fatal(spa, FTAG, "feature refcount already 0: %s",
434 feature.fi_guid);
438 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
439 decr ? feature_decr_sync : feature_incr_sync, &feature,
440 5, ZFS_SPACE_CHECK_NORMAL));
442 spa_close(spa, FTAG);
445 static int
446 zhack_do_feature(int argc, char **argv)
448 char *subcommand;
450 argc--;
451 argv++;
452 if (argc == 0) {
453 (void) fprintf(stderr,
454 "error: no feature operation specified\n");
455 usage();
458 subcommand = argv[0];
459 if (strcmp(subcommand, "stat") == 0) {
460 zhack_do_feature_stat(argc, argv);
461 } else if (strcmp(subcommand, "enable") == 0) {
462 zhack_do_feature_enable(argc, argv);
463 } else if (strcmp(subcommand, "ref") == 0) {
464 zhack_do_feature_ref(argc, argv);
465 } else {
466 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
467 subcommand);
468 usage();
471 return (0);
474 #define MAX_NUM_PATHS 1024
477 main(int argc, char **argv)
479 extern void zfs_prop_init(void);
481 char *path[MAX_NUM_PATHS];
482 const char *subcommand;
483 int rv = 0;
484 int c;
486 g_importargs.path = path;
488 dprintf_setup(&argc, argv);
489 zfs_prop_init();
491 while ((c = getopt(argc, argv, "+c:d:")) != -1) {
492 switch (c) {
493 case 'c':
494 g_importargs.cachefile = optarg;
495 break;
496 case 'd':
497 assert(g_importargs.paths < MAX_NUM_PATHS);
498 g_importargs.path[g_importargs.paths++] = optarg;
499 break;
500 default:
501 usage();
502 break;
506 argc -= optind;
507 argv += optind;
508 optind = 1;
510 if (argc == 0) {
511 (void) fprintf(stderr, "error: no command specified\n");
512 usage();
515 subcommand = argv[0];
517 if (strcmp(subcommand, "feature") == 0) {
518 rv = zhack_do_feature(argc, argv);
519 } else {
520 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
521 subcommand);
522 usage();
525 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
526 fatal(NULL, FTAG, "pool export failed; "
527 "changes may not be committed to disk\n");
530 kernel_fini();
532 return (rv);