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
,
155 (readonly
? ZFS_IMPORT_SKIP_MMP
: ZFS_IMPORT_NORMAL
));
156 zfeature_checks_disable
= B_FALSE
;
161 fatal(NULL
, FTAG
, "can't import '%s': %s", target
,
166 zhack_spa_open(char *target
, boolean_t readonly
, void *tag
, spa_t
**spa
)
170 zhack_import(target
, readonly
);
172 zfeature_checks_disable
= B_TRUE
;
173 err
= spa_open(target
, spa
, tag
);
174 zfeature_checks_disable
= B_FALSE
;
177 fatal(*spa
, FTAG
, "cannot open '%s': %s", target
,
179 if (spa_version(*spa
) < SPA_VERSION_FEATURES
) {
180 fatal(*spa
, FTAG
, "'%s' has version %d, features not enabled",
181 target
, (int)spa_version(*spa
));
186 dump_obj(objset_t
*os
, uint64_t obj
, const char *name
)
191 (void) printf("%s_obj:\n", name
);
193 for (zap_cursor_init(&zc
, os
, obj
);
194 zap_cursor_retrieve(&zc
, &za
) == 0;
195 zap_cursor_advance(&zc
)) {
196 if (za
.za_integer_length
== 8) {
197 ASSERT(za
.za_num_integers
== 1);
198 (void) printf("\t%s = %llu\n",
199 za
.za_name
, (u_longlong_t
)za
.za_first_integer
);
201 ASSERT(za
.za_integer_length
== 1);
203 VERIFY(zap_lookup(os
, obj
, za
.za_name
,
204 1, sizeof (val
), val
) == 0);
205 (void) printf("\t%s = %s\n", za
.za_name
, val
);
208 zap_cursor_fini(&zc
);
214 nvlist_t
*nv
= spa
->spa_label_features
;
217 (void) printf("label config:\n");
218 for (pair
= nvlist_next_nvpair(nv
, NULL
);
220 pair
= nvlist_next_nvpair(nv
, pair
)) {
221 (void) printf("\t%s\n", nvpair_name(pair
));
226 zhack_do_feature_stat(int argc
, char **argv
)
236 (void) fprintf(stderr
, "error: missing pool name\n");
241 zhack_spa_open(target
, B_TRUE
, FTAG
, &spa
);
242 os
= spa
->spa_meta_objset
;
244 dump_obj(os
, spa
->spa_feat_for_read_obj
, "for_read");
245 dump_obj(os
, spa
->spa_feat_for_write_obj
, "for_write");
246 dump_obj(os
, spa
->spa_feat_desc_obj
, "descriptions");
247 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
248 dump_obj(os
, spa
->spa_feat_enabled_txg_obj
, "enabled_txg");
252 spa_close(spa
, FTAG
);
256 zhack_feature_enable_sync(void *arg
, dmu_tx_t
*tx
)
258 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
259 zfeature_info_t
*feature
= arg
;
261 feature_enable_sync(spa
, feature
, tx
);
263 spa_history_log_internal(spa
, "zhack enable feature", tx
,
265 feature
->fi_guid
, feature
->fi_flags
);
269 zhack_do_feature_enable(int argc
, char **argv
)
275 zfeature_info_t feature
;
276 spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
279 * Features are not added to the pool's label until their refcounts
280 * are incremented, so fi_mos can just be left as false for now.
283 feature
.fi_uname
= "zhack";
284 feature
.fi_flags
= 0;
285 feature
.fi_depends
= nodeps
;
286 feature
.fi_feature
= SPA_FEATURE_NONE
;
289 while ((c
= getopt(argc
, argv
, "+rd:")) != -1) {
292 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
295 desc
= strdup(optarg
);
304 desc
= strdup("zhack injected");
305 feature
.fi_desc
= desc
;
311 (void) fprintf(stderr
, "error: missing feature or pool name\n");
315 feature
.fi_guid
= argv
[1];
317 if (!zfeature_is_valid_guid(feature
.fi_guid
))
318 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
320 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
321 mos
= spa
->spa_meta_objset
;
323 if (zfeature_is_supported(feature
.fi_guid
))
324 fatal(spa
, FTAG
, "'%s' is a real feature, will not enable");
325 if (0 == zap_contains(mos
, spa
->spa_feat_desc_obj
, feature
.fi_guid
))
326 fatal(spa
, FTAG
, "feature already enabled: %s",
329 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
330 zhack_feature_enable_sync
, &feature
, 5, ZFS_SPACE_CHECK_NORMAL
));
332 spa_close(spa
, FTAG
);
338 feature_incr_sync(void *arg
, dmu_tx_t
*tx
)
340 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
341 zfeature_info_t
*feature
= arg
;
344 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
345 feature_sync(spa
, feature
, refcount
+ 1, tx
);
346 spa_history_log_internal(spa
, "zhack feature incr", tx
,
347 "name=%s", feature
->fi_guid
);
351 feature_decr_sync(void *arg
, dmu_tx_t
*tx
)
353 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
354 zfeature_info_t
*feature
= arg
;
357 VERIFY0(feature_get_refcount_from_disk(spa
, feature
, &refcount
));
358 feature_sync(spa
, feature
, refcount
- 1, tx
);
359 spa_history_log_internal(spa
, "zhack feature decr", tx
,
360 "name=%s", feature
->fi_guid
);
364 zhack_do_feature_ref(int argc
, char **argv
)
368 boolean_t decr
= B_FALSE
;
371 zfeature_info_t feature
;
372 spa_feature_t nodeps
[] = { SPA_FEATURE_NONE
};
375 * fi_desc does not matter here because it was written to disk
376 * when the feature was enabled, but we need to properly set the
377 * feature for read or write based on the information we read off
380 feature
.fi_uname
= "zhack";
381 feature
.fi_flags
= 0;
382 feature
.fi_desc
= NULL
;
383 feature
.fi_depends
= nodeps
;
384 feature
.fi_feature
= SPA_FEATURE_NONE
;
387 while ((c
= getopt(argc
, argv
, "+md")) != -1) {
390 feature
.fi_flags
|= ZFEATURE_FLAG_MOS
;
404 (void) fprintf(stderr
, "error: missing feature or pool name\n");
408 feature
.fi_guid
= argv
[1];
410 if (!zfeature_is_valid_guid(feature
.fi_guid
))
411 fatal(NULL
, FTAG
, "invalid feature guid: %s", feature
.fi_guid
);
413 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
414 mos
= spa
->spa_meta_objset
;
416 if (zfeature_is_supported(feature
.fi_guid
)) {
418 "'%s' is a real feature, will not change refcount");
421 if (0 == zap_contains(mos
, spa
->spa_feat_for_read_obj
,
423 feature
.fi_flags
&= ~ZFEATURE_FLAG_READONLY_COMPAT
;
424 } else if (0 == zap_contains(mos
, spa
->spa_feat_for_write_obj
,
426 feature
.fi_flags
|= ZFEATURE_FLAG_READONLY_COMPAT
;
428 fatal(spa
, FTAG
, "feature is not enabled: %s", feature
.fi_guid
);
433 if (feature_get_refcount_from_disk(spa
, &feature
,
434 &count
) == 0 && count
== 0) {
435 fatal(spa
, FTAG
, "feature refcount already 0: %s",
440 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
441 decr
? feature_decr_sync
: feature_incr_sync
, &feature
,
442 5, ZFS_SPACE_CHECK_NORMAL
));
444 spa_close(spa
, FTAG
);
448 zhack_do_feature(int argc
, char **argv
)
455 (void) fprintf(stderr
,
456 "error: no feature operation specified\n");
460 subcommand
= argv
[0];
461 if (strcmp(subcommand
, "stat") == 0) {
462 zhack_do_feature_stat(argc
, argv
);
463 } else if (strcmp(subcommand
, "enable") == 0) {
464 zhack_do_feature_enable(argc
, argv
);
465 } else if (strcmp(subcommand
, "ref") == 0) {
466 zhack_do_feature_ref(argc
, argv
);
468 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
476 #define MAX_NUM_PATHS 1024
479 main(int argc
, char **argv
)
481 extern void zfs_prop_init(void);
483 char *path
[MAX_NUM_PATHS
];
484 const char *subcommand
;
488 g_importargs
.path
= path
;
490 dprintf_setup(&argc
, argv
);
493 while ((c
= getopt(argc
, argv
, "+c:d:")) != -1) {
496 g_importargs
.cachefile
= optarg
;
499 assert(g_importargs
.paths
< MAX_NUM_PATHS
);
500 g_importargs
.path
[g_importargs
.paths
++] = optarg
;
513 (void) fprintf(stderr
, "error: no command specified\n");
517 subcommand
= argv
[0];
519 if (strcmp(subcommand
, "feature") == 0) {
520 rv
= zhack_do_feature(argc
, argv
);
522 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
527 if (!g_readonly
&& spa_export(g_pool
, NULL
, B_TRUE
, B_FALSE
) != 0) {
528 fatal(NULL
, FTAG
, "pool export failed; "
529 "changes may not be committed to disk\n");