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) 2012 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>
54 extern boolean_t zfeature_checks_disable
;
56 const char cmdname
[] = "zhack";
57 libzfs_handle_t
*g_zfs
;
58 static importargs_t g_importargs
;
60 static boolean_t g_readonly
;
65 (void) fprintf(stderr
,
66 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
67 "where <subcommand> <args> is one of the following:\n"
70 (void) fprintf(stderr
,
71 " feature stat <pool>\n"
72 " print information about enabled features\n"
73 " feature enable [-d desc] <pool> <feature>\n"
74 " add a new enabled feature to the pool\n"
75 " -d <desc> sets the feature's description\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(const char *fmt
, ...)
92 (void) fprintf(stderr
, "%s: ", cmdname
);
93 (void) vfprintf(stderr
, fmt
, ap
);
95 (void) fprintf(stderr
, "\n");
102 space_delta_cb(dmu_object_type_t bonustype
, void *data
,
103 uint64_t *userp
, uint64_t *groupp
)
106 * Is it a valid type of object to track?
108 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
110 (void) fprintf(stderr
, "modifying object that needs user accounting");
116 * Target is the dataset whose pool we want to open.
119 import_pool(const 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
;
139 * If we only want readonly access, it's OK if we find
140 * a potentially-active (ie, imported into the kernel) pool from the
143 if (readonly
&& spa_open(target
, &spa
, FTAG
) == 0) {
144 spa_close(spa
, FTAG
);
148 g_importargs
.unique
= B_TRUE
;
149 g_importargs
.can_be_active
= readonly
;
150 g_pool
= strdup(target
);
151 if ((sepp
= strpbrk(g_pool
, "/@")) != NULL
)
153 g_importargs
.poolname
= g_pool
;
154 pools
= zpool_search_import(g_zfs
, &g_importargs
);
156 if (nvlist_empty(pools
)) {
157 if (!g_importargs
.can_be_active
) {
158 g_importargs
.can_be_active
= B_TRUE
;
159 if (zpool_search_import(g_zfs
, &g_importargs
) != NULL
||
160 spa_open(target
, &spa
, FTAG
) == 0) {
161 fatal("cannot import '%s': pool is active; run "
162 "\"zpool export %s\" first\n",
167 fatal("cannot import '%s': no such pool available\n", g_pool
);
170 elem
= nvlist_next_nvpair(pools
, NULL
);
171 name
= nvpair_name(elem
);
172 VERIFY(nvpair_value_nvlist(elem
, &config
) == 0);
176 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
177 VERIFY(nvlist_add_uint64(props
,
178 zpool_prop_to_name(ZPOOL_PROP_READONLY
), 1) == 0);
181 zfeature_checks_disable
= B_TRUE
;
182 error
= spa_import(name
, config
, props
, ZFS_IMPORT_NORMAL
);
183 zfeature_checks_disable
= B_FALSE
;
188 fatal("can't import '%s': %s", name
, strerror(error
));
192 zhack_spa_open(const char *target
, boolean_t readonly
, void *tag
, spa_t
**spa
)
196 import_pool(target
, readonly
);
198 zfeature_checks_disable
= B_TRUE
;
199 err
= spa_open(target
, spa
, tag
);
200 zfeature_checks_disable
= B_FALSE
;
203 fatal("cannot open '%s': %s", target
, strerror(err
));
204 if (spa_version(*spa
) < SPA_VERSION_FEATURES
) {
205 fatal("'%s' has version %d, features not enabled", target
,
206 (int)spa_version(*spa
));
211 dump_obj(objset_t
*os
, uint64_t obj
, const char *name
)
216 (void) printf("%s_obj:\n", name
);
218 for (zap_cursor_init(&zc
, os
, obj
);
219 zap_cursor_retrieve(&zc
, &za
) == 0;
220 zap_cursor_advance(&zc
)) {
221 if (za
.za_integer_length
== 8) {
222 ASSERT(za
.za_num_integers
== 1);
223 (void) printf("\t%s = %llu\n",
224 za
.za_name
, (u_longlong_t
)za
.za_first_integer
);
226 ASSERT(za
.za_integer_length
== 1);
228 VERIFY(zap_lookup(os
, obj
, za
.za_name
,
229 1, sizeof (val
), val
) == 0);
230 (void) printf("\t%s = %s\n", za
.za_name
, val
);
233 zap_cursor_fini(&zc
);
239 nvlist_t
*nv
= spa
->spa_label_features
;
242 (void) printf("label config:\n");
243 for (pair
= nvlist_next_nvpair(nv
, NULL
);
245 pair
= nvlist_next_nvpair(nv
, pair
)) {
246 (void) printf("\t%s\n", nvpair_name(pair
));
251 zhack_do_feature_stat(int argc
, char **argv
)
261 (void) fprintf(stderr
, "error: missing pool name\n");
266 zhack_spa_open(target
, B_TRUE
, FTAG
, &spa
);
267 os
= spa
->spa_meta_objset
;
269 dump_obj(os
, spa
->spa_feat_for_read_obj
, "for_read");
270 dump_obj(os
, spa
->spa_feat_for_write_obj
, "for_write");
271 dump_obj(os
, spa
->spa_feat_desc_obj
, "descriptions");
274 spa_close(spa
, FTAG
);
278 feature_enable_sync(void *arg
, dmu_tx_t
*tx
)
280 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
281 zfeature_info_t
*feature
= arg
;
283 spa_feature_enable(spa
, feature
, tx
);
284 spa_history_log_internal(spa
, "zhack enable feature", tx
,
285 "name=%s can_readonly=%u",
286 feature
->fi_guid
, feature
->fi_can_readonly
);
290 zhack_do_feature_enable(int argc
, char **argv
)
296 zfeature_info_t feature
;
297 zfeature_info_t
*nodeps
[] = { NULL
};
300 * Features are not added to the pool's label until their refcounts
301 * are incremented, so fi_mos can just be left as false for now.
304 feature
.fi_uname
= "zhack";
305 feature
.fi_mos
= B_FALSE
;
306 feature
.fi_can_readonly
= B_FALSE
;
307 feature
.fi_depends
= nodeps
;
310 while ((c
= getopt(argc
, argv
, "rmd:")) != -1) {
313 feature
.fi_can_readonly
= B_TRUE
;
316 desc
= strdup(optarg
);
325 desc
= strdup("zhack injected");
326 feature
.fi_desc
= desc
;
332 (void) fprintf(stderr
, "error: missing feature or pool name\n");
336 feature
.fi_guid
= argv
[1];
338 if (!zfeature_is_valid_guid(feature
.fi_guid
))
339 fatal("invalid feature guid: %s", feature
.fi_guid
);
341 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
342 mos
= spa
->spa_meta_objset
;
344 if (0 == zfeature_lookup_guid(feature
.fi_guid
, NULL
))
345 fatal("'%s' is a real feature, will not enable");
346 if (0 == zap_contains(mos
, spa
->spa_feat_desc_obj
, feature
.fi_guid
))
347 fatal("feature already enabled: %s", feature
.fi_guid
);
349 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
350 feature_enable_sync
, &feature
, 5));
352 spa_close(spa
, FTAG
);
358 feature_incr_sync(void *arg
, dmu_tx_t
*tx
)
360 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
361 zfeature_info_t
*feature
= arg
;
363 spa_feature_incr(spa
, feature
, tx
);
364 spa_history_log_internal(spa
, "zhack feature incr", tx
,
365 "name=%s", feature
->fi_guid
);
369 feature_decr_sync(void *arg
, dmu_tx_t
*tx
)
371 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
372 zfeature_info_t
*feature
= arg
;
374 spa_feature_decr(spa
, feature
, tx
);
375 spa_history_log_internal(spa
, "zhack feature decr", tx
,
376 "name=%s", feature
->fi_guid
);
380 zhack_do_feature_ref(int argc
, char **argv
)
384 boolean_t decr
= B_FALSE
;
387 zfeature_info_t feature
;
388 zfeature_info_t
*nodeps
[] = { NULL
};
391 * fi_desc does not matter here because it was written to disk
392 * when the feature was enabled, but we need to properly set the
393 * feature for read or write based on the information we read off
396 feature
.fi_uname
= "zhack";
397 feature
.fi_mos
= B_FALSE
;
398 feature
.fi_desc
= NULL
;
399 feature
.fi_depends
= nodeps
;
402 while ((c
= getopt(argc
, argv
, "md")) != -1) {
405 feature
.fi_mos
= B_TRUE
;
419 (void) fprintf(stderr
, "error: missing feature or pool name\n");
423 feature
.fi_guid
= argv
[1];
425 if (!zfeature_is_valid_guid(feature
.fi_guid
))
426 fatal("invalid feature guid: %s", feature
.fi_guid
);
428 zhack_spa_open(target
, B_FALSE
, FTAG
, &spa
);
429 mos
= spa
->spa_meta_objset
;
431 if (0 == zfeature_lookup_guid(feature
.fi_guid
, NULL
))
432 fatal("'%s' is a real feature, will not change refcount");
434 if (0 == zap_contains(mos
, spa
->spa_feat_for_read_obj
,
436 feature
.fi_can_readonly
= B_FALSE
;
437 } else if (0 == zap_contains(mos
, spa
->spa_feat_for_write_obj
,
439 feature
.fi_can_readonly
= B_TRUE
;
441 fatal("feature is not enabled: %s", feature
.fi_guid
);
444 if (decr
&& !spa_feature_is_active(spa
, &feature
))
445 fatal("feature refcount already 0: %s", feature
.fi_guid
);
447 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
448 decr
? feature_decr_sync
: feature_incr_sync
, &feature
, 5));
450 spa_close(spa
, FTAG
);
454 zhack_do_feature(int argc
, char **argv
)
461 (void) fprintf(stderr
,
462 "error: no feature operation specified\n");
466 subcommand
= argv
[0];
467 if (strcmp(subcommand
, "stat") == 0) {
468 zhack_do_feature_stat(argc
, argv
);
469 } else if (strcmp(subcommand
, "enable") == 0) {
470 zhack_do_feature_enable(argc
, argv
);
471 } else if (strcmp(subcommand
, "ref") == 0) {
472 zhack_do_feature_ref(argc
, argv
);
474 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
482 #define MAX_NUM_PATHS 1024
485 main(int argc
, char **argv
)
487 extern void zfs_prop_init(void);
489 char *path
[MAX_NUM_PATHS
];
490 const char *subcommand
;
494 g_importargs
.path
= path
;
496 dprintf_setup(&argc
, argv
);
499 while ((c
= getopt(argc
, argv
, "c:d:")) != -1) {
502 g_importargs
.cachefile
= optarg
;
505 assert(g_importargs
.paths
< MAX_NUM_PATHS
);
506 g_importargs
.path
[g_importargs
.paths
++] = optarg
;
519 (void) fprintf(stderr
, "error: no command specified\n");
523 subcommand
= argv
[0];
525 if (strcmp(subcommand
, "feature") == 0) {
526 rv
= zhack_do_feature(argc
, argv
);
528 (void) fprintf(stderr
, "error: unknown subcommand: %s\n",
533 if (!g_readonly
&& spa_export(g_pool
, NULL
, B_TRUE
, B_TRUE
) != 0) {
534 fatal("pool export failed; "
535 "changes may not be committed to disk\n");