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]
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.
36 #include <sys/zfs_context.h>
38 #include <sys/spa_impl.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/dsl_synctask.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>
53 extern boolean_t zfeature_checks_disable
;
55 const char cmdname
[] = "zhack";
56 libzfs_handle_t
*g_zfs
;
57 static importargs_t g_importargs
;
59 static boolean_t g_readonly
;
64 (void) fprintf(stderr
,
65 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
66 "where <subcommand> <args> is one of the following:\n"
69 (void) fprintf(stderr
,
70 " feature stat <pool>\n"
71 " print information about enabled features\n"
72 " feature enable [-r] [-d desc] <pool> <feature>\n"
73 " add a new enabled feature to the pool\n"
74 " -d <desc> sets the feature's description\n"
75 " -r set read-only compatible flag for feature\n"
76 " feature ref [-md] <pool> <feature>\n"
77 " change the refcount on the given feature\n"
78 " -d decrease instead of increase the refcount\n"
79 " -m add the feature to the label if increasing refcount\n"
81 " <feature> : should be a feature guid\n");
87 fatal(spa_t
*spa
, void *tag
, const char *fmt
, ...)
93 (void) spa_export(g_pool
, NULL
, B_TRUE
, B_FALSE
);
97 (void) fprintf(stderr
, "%s: ", cmdname
);
98 (void) vfprintf(stderr
, fmt
, ap
);
100 (void) fprintf(stderr
, "\n");
107 space_delta_cb(dmu_object_type_t bonustype
, void *data
,
108 uint64_t *userp
, uint64_t *groupp
)
111 * Is it a valid type of object to track?
113 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
115 (void) fprintf(stderr
, "modifying object that needs user accounting");
121 * Target is the dataset whose pool we want to open.
124 zhack_import(char *target
, boolean_t readonly
)
130 kernel_init(readonly
? FREAD
: (FREAD
| FWRITE
));
131 g_zfs
= libzfs_init();
132 ASSERT(g_zfs
!= NULL
);
134 dmu_objset_register_type(DMU_OST_ZFS
, space_delta_cb
);
136 g_readonly
= readonly
;
137 g_importargs
.unique
= B_TRUE
;
138 g_importargs
.can_be_active
= readonly
;
139 g_pool
= strdup(target
);
141 error
= zpool_tryimport(g_zfs
, target
, &config
, &g_importargs
);
143 fatal(NULL
, FTAG
, "cannot import '%s': %s", target
,
144 libzfs_error_description(g_zfs
));
148 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
149 VERIFY(nvlist_add_uint64(props
,
150 zpool_prop_to_name(ZPOOL_PROP_READONLY
), 1) == 0);
153 zfeature_checks_disable
= B_TRUE
;
154 error
= spa_import(target
, config
, props
, ZFS_IMPORT_NORMAL
);
155 zfeature_checks_disable
= B_FALSE
;
160 fatal(NULL
, FTAG
, "can't import '%s': %s", target
,
165 zhack_spa_open(char *target
, boolean_t readonly
, void *tag
, spa_t
**spa
)
169 zhack_import(target
, readonly
);
171 zfeature_checks_disable
= B_TRUE
;
172 err
= spa_open(target
, spa
, tag
);
173 zfeature_checks_disable
= B_FALSE
;
176 fatal(*spa
, FTAG
, "cannot open '%s': %s", target
,
178 if (spa_version(*spa
) < SPA_VERSION_FEATURES
) {
179 fatal(*spa
, FTAG
, "'%s' has version %d, features not enabled",
180 target
, (int)spa_version(*spa
));
185 dump_obj(objset_t
*os
, uint64_t obj
, const char *name
)
190 (void) printf("%s_obj:\n", name
);
192 for (zap_cursor_init(&zc
, os
, obj
);
193 zap_cursor_retrieve(&zc
, &za
) == 0;
194 zap_cursor_advance(&zc
)) {
195 if (za
.za_integer_length
== 8) {
196 ASSERT(za
.za_num_integers
== 1);
197 (void) printf("\t%s = %llu\n",
198 za
.za_name
, (u_longlong_t
)za
.za_first_integer
);
200 ASSERT(za
.za_integer_length
== 1);
202 VERIFY(zap_lookup(os
, obj
, za
.za_name
,
203 1, sizeof (val
), val
) == 0);
204 (void) printf("\t%s = %s\n", za
.za_name
, val
);
207 zap_cursor_fini(&zc
);
213 nvlist_t
*nv
= spa
->spa_label_features
;
216 (void) printf("label config:\n");
217 for (pair
= nvlist_next_nvpair(nv
, NULL
);
219 pair
= nvlist_next_nvpair(nv
, pair
)) {
220 (void) printf("\t%s\n", nvpair_name(pair
));
225 zhack_do_feature_stat(int argc
, char **argv
)
235 (void) fprintf(stderr
, "error: missing pool name\n");
240 zhack_spa_open(target
, B_TRUE
, FTAG
, &spa
);
241 os
= spa
->spa_meta_objset
;
243 dump_obj(os
, spa
->spa_feat_for_read_obj
, "for_read");
244 dump_obj(os
, spa
->spa_feat_for_write_obj
, "for_write");
245 dump_obj(os
, spa
->spa_feat_desc_obj
, "descriptions");
246 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
247 dump_obj(os
, spa
->spa_feat_enabled_txg_obj
, "enabled_txg");
251 spa_close(spa
, FTAG
);
255 zhack_feature_enable_sync(void *arg
, dmu_tx_t
*tx
)
257 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
258 zfeature_info_t
*feature
= arg
;
260 feature_enable_sync(spa
, feature
, tx
);
262 spa_history_log_internal(spa
, "zhack enable feature", tx
,
264 feature
->fi_guid
, feature
->fi_flags
);
268 zhack_do_feature_enable(int argc
, char **argv
)
274 zfeature_info_t feature
;
275 spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
278 * Features are not added to the pool's label until their refcounts
279 * are incremented, so fi_mos can just be left as false for now.
282 feature
.fi_uname
= "zhack";
283 feature
.fi_flags
= 0;
284 feature
.fi_depends
= nodeps
;
285 feature
.fi_feature
= SPA_FEATURE_NONE
;
288 while ((c
= getopt(argc
, argv
, "+rd:")) != -1) {
291 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
294 desc
= strdup(optarg
);
303 desc
= strdup("zhack injected");
304 feature
.fi_desc
= desc
;
310 (void) fprintf(stderr
, "error: missing feature or pool name\n");
314 feature
.fi_guid
= argv
[1];
316 if (!zfeature_is_valid_guid(feature
.fi_guid
))
317 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
319 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
320 mos
= spa
->spa_meta_objset
;
322 if (zfeature_is_supported(feature
.fi_guid
))
323 fatal(spa
, FTAG
, "'%s' is a real feature, will not enable");
324 if (0 == zap_contains(mos
, spa
->spa_feat_desc_obj
, feature
.fi_guid
))
325 fatal(spa
, FTAG
, "feature already enabled: %s",
328 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
329 zhack_feature_enable_sync
, &feature
, 5, ZFS_SPACE_CHECK_NORMAL
));
331 spa_close(spa
, FTAG
);
337 feature_incr_sync(void *arg
, dmu_tx_t
*tx
)
339 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
340 zfeature_info_t
*feature
= arg
;
343 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
344 feature_sync(spa
, feature
, refcount
+ 1, tx
);
345 spa_history_log_internal(spa
, "zhack feature incr", tx
,
346 "name=%s", feature
->fi_guid
);
350 feature_decr_sync(void *arg
, dmu_tx_t
*tx
)
352 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
353 zfeature_info_t
*feature
= arg
;
356 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
357 feature_sync(spa
, feature
, refcount
- 1, tx
);
358 spa_history_log_internal(spa
, "zhack feature decr", tx
,
359 "name=%s", feature
->fi_guid
);
363 zhack_do_feature_ref(int argc
, char **argv
)
367 boolean_t decr
= B_FALSE
;
370 zfeature_info_t feature
;
371 spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
374 * fi_desc does not matter here because it was written to disk
375 * when the feature was enabled, but we need to properly set the
376 * feature for read or write based on the information we read off
379 feature
.fi_uname
= "zhack";
380 feature
.fi_flags
= 0;
381 feature
.fi_desc
= NULL
;
382 feature
.fi_depends
= nodeps
;
383 feature
.fi_feature
= SPA_FEATURE_NONE
;
386 while ((c
= getopt(argc
, argv
, "+md")) != -1) {
389 feature
.fi_flags
|= ZFEATURE_FLAG_MOS
;
403 (void) fprintf(stderr
, "error: missing feature or pool name\n");
407 feature
.fi_guid
= argv
[1];
409 if (!zfeature_is_valid_guid(feature
.fi_guid
))
410 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
412 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
413 mos
= spa
->spa_meta_objset
;
415 if (zfeature_is_supported(feature
.fi_guid
)) {
417 "'%s' is a real feature, will not change refcount");
420 if (0 == zap_contains(mos
, spa
->spa_feat_for_read_obj
,
422 feature
.fi_flags
&= ~ZFEATURE_FLAG_READONLY_COMPAT
;
423 } else if (0 == zap_contains(mos
, spa
->spa_feat_for_write_obj
,
425 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
427 fatal(spa
, FTAG
, "feature is not enabled: %s", feature
.fi_guid
);
432 if (feature_get_refcount_from_disk(spa
, &feature
,
433 &count
) == 0 && count
== 0) {
434 fatal(spa
, FTAG
, "feature refcount already 0: %s",
439 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
440 decr
? feature_decr_sync
: feature_incr_sync
, &feature
,
441 5, ZFS_SPACE_CHECK_NORMAL
));
443 spa_close(spa
, FTAG
);
447 zhack_do_feature(int argc
, char **argv
)
454 (void) fprintf(stderr
,
455 "error: no feature operation specified\n");
459 subcommand
= argv
[0];
460 if (strcmp(subcommand
, "stat") == 0) {
461 zhack_do_feature_stat(argc
, argv
);
462 } else if (strcmp(subcommand
, "enable") == 0) {
463 zhack_do_feature_enable(argc
, argv
);
464 } else if (strcmp(subcommand
, "ref") == 0) {
465 zhack_do_feature_ref(argc
, argv
);
467 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
475 #define MAX_NUM_PATHS 1024
478 main(int argc
, char **argv
)
480 extern void zfs_prop_init(void);
482 char *path
[MAX_NUM_PATHS
];
483 const char *subcommand
;
487 g_importargs
.path
= path
;
489 dprintf_setup(&argc
, argv
);
492 while ((c
= getopt(argc
, argv
, "+c:d:")) != -1) {
495 g_importargs
.cachefile
= optarg
;
498 assert(g_importargs
.paths
< MAX_NUM_PATHS
);
499 g_importargs
.path
[g_importargs
.paths
++] = optarg
;
512 (void) fprintf(stderr
, "error: no command specified\n");
516 subcommand
= argv
[0];
518 if (strcmp(subcommand
, "feature") == 0) {
519 rv
= zhack_do_feature(argc
, argv
);
521 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
526 if (!g_readonly
&& spa_export(g_pool
, NULL
, B_TRUE
, B_FALSE
) != 0) {
527 fatal(NULL
, FTAG
, "pool export failed; "
528 "changes may not be committed to disk\n");