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