]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zhack/zhack.c
Add libzutil for libzfs or libzpool consumers
[mirror_zfs.git] / cmd / zhack / zhack.c
CommitLineData
9ae529ec
CS
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
241b5415 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
95fd54a1 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
9ae529ec
CS
25 */
26
27/*
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.
31 */
32
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>
13fe0198 50#include <sys/dmu_tx.h>
e89f1295 51#include <libzutil.h>
9ae529ec
CS
52
53extern boolean_t zfeature_checks_disable;
54
55const char cmdname[] = "zhack";
9ae529ec
CS
56static importargs_t g_importargs;
57static char *g_pool;
58static boolean_t g_readonly;
59
60static void
61usage(void)
62{
63 (void) fprintf(stderr,
ada82581 64 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
9ae529ec
CS
65 "where <subcommand> <args> is one of the following:\n"
66 "\n", cmdname);
67
68 (void) fprintf(stderr,
69 " feature stat <pool>\n"
70 " print information about enabled features\n"
9d69e9b2 71 " feature enable [-r] [-d desc] <pool> <feature>\n"
9ae529ec
CS
72 " add a new enabled feature to the pool\n"
73 " -d <desc> sets the feature's description\n"
9d69e9b2 74 " -r set read-only compatible flag for feature\n"
9ae529ec
CS
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);
82}
83
84
85static void
2fbc542e 86fatal(spa_t *spa, void *tag, const char *fmt, ...)
9ae529ec
CS
87{
88 va_list ap;
89
2fbc542e
GW
90 if (spa != NULL) {
91 spa_close(spa, tag);
92 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
93 }
94
9ae529ec
CS
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");
100
101 exit(1);
102}
103
104/* ARGSUSED */
105static int
106space_delta_cb(dmu_object_type_t bonustype, void *data,
9c5167d1 107 uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
9ae529ec
CS
108{
109 /*
110 * Is it a valid type of object to track?
111 */
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 */
117}
118
119/*
120 * Target is the dataset whose pool we want to open.
121 */
122static void
379ca9cf 123zhack_import(char *target, boolean_t readonly)
9ae529ec
CS
124{
125 nvlist_t *config;
9ae529ec 126 nvlist_t *props;
379ca9cf 127 int error;
9ae529ec
CS
128
129 kernel_init(readonly ? FREAD : (FREAD | FWRITE));
9ae529ec
CS
130
131 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
132
133 g_readonly = readonly;
9ae529ec
CS
134 g_importargs.can_be_active = readonly;
135 g_pool = strdup(target);
9ae529ec 136
e89f1295
DB
137 error = zpool_find_config(NULL, target, &config, &g_importargs,
138 &libzpool_config_ops);
379ca9cf 139 if (error)
e89f1295 140 fatal(NULL, FTAG, "cannot import '%s'", target);
9ae529ec
CS
141
142 props = NULL;
143 if (readonly) {
144 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
145 VERIFY(nvlist_add_uint64(props,
146 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
147 }
148
149 zfeature_checks_disable = B_TRUE;
60f51034
OF
150 error = spa_import(target, config, props,
151 (readonly ? ZFS_IMPORT_SKIP_MMP : ZFS_IMPORT_NORMAL));
9ae529ec
CS
152 zfeature_checks_disable = B_FALSE;
153 if (error == EEXIST)
154 error = 0;
155
156 if (error)
379ca9cf 157 fatal(NULL, FTAG, "can't import '%s': %s", target,
2fbc542e 158 strerror(error));
9ae529ec
CS
159}
160
161static void
379ca9cf 162zhack_spa_open(char *target, boolean_t readonly, void *tag, spa_t **spa)
9ae529ec
CS
163{
164 int err;
165
379ca9cf 166 zhack_import(target, readonly);
9ae529ec
CS
167
168 zfeature_checks_disable = B_TRUE;
169 err = spa_open(target, spa, tag);
170 zfeature_checks_disable = B_FALSE;
171
172 if (err != 0)
2fbc542e
GW
173 fatal(*spa, FTAG, "cannot open '%s': %s", target,
174 strerror(err));
9ae529ec 175 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
2fbc542e
GW
176 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
177 target, (int)spa_version(*spa));
9ae529ec
CS
178 }
179}
180
181static void
182dump_obj(objset_t *os, uint64_t obj, const char *name)
183{
184 zap_cursor_t zc;
185 zap_attribute_t za;
186
187 (void) printf("%s_obj:\n", name);
188
189 for (zap_cursor_init(&zc, os, obj);
190 zap_cursor_retrieve(&zc, &za) == 0;
191 zap_cursor_advance(&zc)) {
192 if (za.za_integer_length == 8) {
193 ASSERT(za.za_num_integers == 1);
194 (void) printf("\t%s = %llu\n",
195 za.za_name, (u_longlong_t)za.za_first_integer);
196 } else {
197 ASSERT(za.za_integer_length == 1);
198 char val[1024];
199 VERIFY(zap_lookup(os, obj, za.za_name,
200 1, sizeof (val), val) == 0);
201 (void) printf("\t%s = %s\n", za.za_name, val);
202 }
203 }
204 zap_cursor_fini(&zc);
205}
206
207static void
208dump_mos(spa_t *spa)
209{
210 nvlist_t *nv = spa->spa_label_features;
211 nvpair_t *pair;
212
213 (void) printf("label config:\n");
214 for (pair = nvlist_next_nvpair(nv, NULL);
215 pair != NULL;
216 pair = nvlist_next_nvpair(nv, pair)) {
217 (void) printf("\t%s\n", nvpair_name(pair));
218 }
219}
220
221static void
222zhack_do_feature_stat(int argc, char **argv)
223{
224 spa_t *spa;
225 objset_t *os;
226 char *target;
227
228 argc--;
229 argv++;
230
231 if (argc < 1) {
232 (void) fprintf(stderr, "error: missing pool name\n");
233 usage();
234 }
235 target = argv[0];
236
237 zhack_spa_open(target, B_TRUE, FTAG, &spa);
238 os = spa->spa_meta_objset;
239
240 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
241 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
242 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
b0bc7a84
MG
243 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
244 dump_obj(os, spa->spa_feat_enabled_txg_obj, "enabled_txg");
245 }
9ae529ec
CS
246 dump_mos(spa);
247
248 spa_close(spa, FTAG);
249}
250
251static void
fa86b5db 252zhack_feature_enable_sync(void *arg, dmu_tx_t *tx)
9ae529ec 253{
13fe0198
MA
254 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
255 zfeature_info_t *feature = arg;
9ae529ec 256
fa86b5db
MA
257 feature_enable_sync(spa, feature, tx);
258
6f1ffb06 259 spa_history_log_internal(spa, "zhack enable feature", tx,
241b5415
MA
260 "name=%s flags=%u",
261 feature->fi_guid, feature->fi_flags);
9ae529ec
CS
262}
263
264static void
265zhack_do_feature_enable(int argc, char **argv)
266{
8b20a9f9 267 int c;
9ae529ec
CS
268 char *desc, *target;
269 spa_t *spa;
270 objset_t *mos;
271 zfeature_info_t feature;
fa86b5db 272 spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
9ae529ec
CS
273
274 /*
275 * Features are not added to the pool's label until their refcounts
276 * are incremented, so fi_mos can just be left as false for now.
277 */
278 desc = NULL;
279 feature.fi_uname = "zhack";
241b5415 280 feature.fi_flags = 0;
9ae529ec 281 feature.fi_depends = nodeps;
b0bc7a84 282 feature.fi_feature = SPA_FEATURE_NONE;
9ae529ec
CS
283
284 optind = 1;
9d69e9b2 285 while ((c = getopt(argc, argv, "+rd:")) != -1) {
9ae529ec
CS
286 switch (c) {
287 case 'r':
241b5415 288 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
9ae529ec
CS
289 break;
290 case 'd':
291 desc = strdup(optarg);
292 break;
293 default:
294 usage();
295 break;
296 }
297 }
298
299 if (desc == NULL)
300 desc = strdup("zhack injected");
301 feature.fi_desc = desc;
302
303 argc -= optind;
304 argv += optind;
305
306 if (argc < 2) {
307 (void) fprintf(stderr, "error: missing feature or pool name\n");
308 usage();
309 }
310 target = argv[0];
311 feature.fi_guid = argv[1];
312
313 if (!zfeature_is_valid_guid(feature.fi_guid))
2fbc542e 314 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
9ae529ec
CS
315
316 zhack_spa_open(target, B_FALSE, FTAG, &spa);
317 mos = spa->spa_meta_objset;
318
ada82581
BB
319 if (zfeature_is_supported(feature.fi_guid))
320 fatal(spa, FTAG, "'%s' is a real feature, will not enable");
9ae529ec 321 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
2fbc542e
GW
322 fatal(spa, FTAG, "feature already enabled: %s",
323 feature.fi_guid);
9ae529ec 324
13fe0198 325 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3d45fdd6 326 zhack_feature_enable_sync, &feature, 5, ZFS_SPACE_CHECK_NORMAL));
9ae529ec
CS
327
328 spa_close(spa, FTAG);
329
330 free(desc);
331}
332
333static void
13fe0198 334feature_incr_sync(void *arg, dmu_tx_t *tx)
9ae529ec 335{
13fe0198
MA
336 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
337 zfeature_info_t *feature = arg;
fa86b5db 338 uint64_t refcount;
9ae529ec 339
b0bc7a84 340 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
fa86b5db 341 feature_sync(spa, feature, refcount + 1, tx);
6f1ffb06
MA
342 spa_history_log_internal(spa, "zhack feature incr", tx,
343 "name=%s", feature->fi_guid);
9ae529ec
CS
344}
345
346static void
13fe0198 347feature_decr_sync(void *arg, dmu_tx_t *tx)
9ae529ec 348{
13fe0198
MA
349 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
350 zfeature_info_t *feature = arg;
fa86b5db 351 uint64_t refcount;
9ae529ec 352
b0bc7a84 353 VERIFY0(feature_get_refcount_from_disk(spa, feature, &refcount));
fa86b5db 354 feature_sync(spa, feature, refcount - 1, tx);
6f1ffb06
MA
355 spa_history_log_internal(spa, "zhack feature decr", tx,
356 "name=%s", feature->fi_guid);
9ae529ec
CS
357}
358
359static void
360zhack_do_feature_ref(int argc, char **argv)
361{
8b20a9f9 362 int c;
9ae529ec
CS
363 char *target;
364 boolean_t decr = B_FALSE;
365 spa_t *spa;
366 objset_t *mos;
367 zfeature_info_t feature;
fa86b5db 368 spa_feature_t nodeps[] = { SPA_FEATURE_NONE };
9ae529ec
CS
369
370 /*
371 * fi_desc does not matter here because it was written to disk
372 * when the feature was enabled, but we need to properly set the
373 * feature for read or write based on the information we read off
374 * disk later.
375 */
376 feature.fi_uname = "zhack";
241b5415 377 feature.fi_flags = 0;
9ae529ec
CS
378 feature.fi_desc = NULL;
379 feature.fi_depends = nodeps;
b0bc7a84 380 feature.fi_feature = SPA_FEATURE_NONE;
9ae529ec
CS
381
382 optind = 1;
9d69e9b2 383 while ((c = getopt(argc, argv, "+md")) != -1) {
9ae529ec
CS
384 switch (c) {
385 case 'm':
241b5415 386 feature.fi_flags |= ZFEATURE_FLAG_MOS;
9ae529ec
CS
387 break;
388 case 'd':
389 decr = B_TRUE;
390 break;
391 default:
392 usage();
393 break;
394 }
395 }
396 argc -= optind;
397 argv += optind;
398
399 if (argc < 2) {
400 (void) fprintf(stderr, "error: missing feature or pool name\n");
401 usage();
402 }
403 target = argv[0];
404 feature.fi_guid = argv[1];
405
406 if (!zfeature_is_valid_guid(feature.fi_guid))
2fbc542e 407 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
9ae529ec
CS
408
409 zhack_spa_open(target, B_FALSE, FTAG, &spa);
410 mos = spa->spa_meta_objset;
411
fa86b5db
MA
412 if (zfeature_is_supported(feature.fi_guid)) {
413 fatal(spa, FTAG,
414 "'%s' is a real feature, will not change refcount");
415 }
9ae529ec
CS
416
417 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
418 feature.fi_guid)) {
241b5415 419 feature.fi_flags &= ~ZFEATURE_FLAG_READONLY_COMPAT;
9ae529ec
CS
420 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
421 feature.fi_guid)) {
241b5415 422 feature.fi_flags |= ZFEATURE_FLAG_READONLY_COMPAT;
9ae529ec 423 } else {
2fbc542e 424 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
9ae529ec
CS
425 }
426
fa86b5db
MA
427 if (decr) {
428 uint64_t count;
b0bc7a84 429 if (feature_get_refcount_from_disk(spa, &feature,
72c407f8 430 &count) == 0 && count == 0) {
fa86b5db
MA
431 fatal(spa, FTAG, "feature refcount already 0: %s",
432 feature.fi_guid);
433 }
434 }
9ae529ec 435
13fe0198 436 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
3d45fdd6
MA
437 decr ? feature_decr_sync : feature_incr_sync, &feature,
438 5, ZFS_SPACE_CHECK_NORMAL));
9ae529ec
CS
439
440 spa_close(spa, FTAG);
441}
442
443static int
444zhack_do_feature(int argc, char **argv)
445{
446 char *subcommand;
447
448 argc--;
449 argv++;
450 if (argc == 0) {
451 (void) fprintf(stderr,
452 "error: no feature operation specified\n");
453 usage();
454 }
455
456 subcommand = argv[0];
457 if (strcmp(subcommand, "stat") == 0) {
458 zhack_do_feature_stat(argc, argv);
459 } else if (strcmp(subcommand, "enable") == 0) {
460 zhack_do_feature_enable(argc, argv);
461 } else if (strcmp(subcommand, "ref") == 0) {
462 zhack_do_feature_ref(argc, argv);
463 } else {
464 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
465 subcommand);
466 usage();
467 }
468
469 return (0);
470}
471
472#define MAX_NUM_PATHS 1024
473
474int
475main(int argc, char **argv)
476{
477 extern void zfs_prop_init(void);
478
479 char *path[MAX_NUM_PATHS];
480 const char *subcommand;
481 int rv = 0;
8b20a9f9 482 int c;
9ae529ec
CS
483
484 g_importargs.path = path;
485
486 dprintf_setup(&argc, argv);
487 zfs_prop_init();
488
9d69e9b2 489 while ((c = getopt(argc, argv, "+c:d:")) != -1) {
9ae529ec
CS
490 switch (c) {
491 case 'c':
492 g_importargs.cachefile = optarg;
493 break;
494 case 'd':
495 assert(g_importargs.paths < MAX_NUM_PATHS);
496 g_importargs.path[g_importargs.paths++] = optarg;
497 break;
498 default:
499 usage();
500 break;
501 }
502 }
503
504 argc -= optind;
505 argv += optind;
506 optind = 1;
507
508 if (argc == 0) {
509 (void) fprintf(stderr, "error: no command specified\n");
510 usage();
511 }
512
513 subcommand = argv[0];
514
515 if (strcmp(subcommand, "feature") == 0) {
516 rv = zhack_do_feature(argc, argv);
517 } else {
518 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
519 subcommand);
520 usage();
521 }
522
2fbc542e
GW
523 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
524 fatal(NULL, FTAG, "pool export failed; "
9ae529ec
CS
525 "changes may not be committed to disk\n");
526 }
527
9ae529ec
CS
528 kernel_fini();
529
530 return (rv);
531}