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 /*
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
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>
68 "where <subcommand> <args> is one of the following:\n"
69 "\n", cmdname);
70
71 (void) fprintf(stderr,
72 " feature stat <pool>\n"
73 " print information about enabled features\n"
74 " feature enable [-d desc] <pool> <feature>\n"
75 " add a new enabled feature to the pool\n"
76 " -d <desc> sets the feature's description\n"
77 " feature ref [-md] <pool> <feature>\n"
78 " change the refcount on the given feature\n"
79 " -d decrease instead of increase the refcount\n"
80 " -m add the feature to the label if increasing refcount\n"
81 "\n"
82 " <feature> : should be a feature guid\n");
83 exit(1);
84 }
85
86
87 static void
88 fatal(const char *fmt, ...)
89 {
90 va_list ap;
91
92 va_start(ap, fmt);
93 (void) fprintf(stderr, "%s: ", cmdname);
94 (void) vfprintf(stderr, fmt, ap);
95 va_end(ap);
96 (void) fprintf(stderr, "\n");
97
98 exit(1);
99 }
100
101 /* ARGSUSED */
102 static int
103 space_delta_cb(dmu_object_type_t bonustype, void *data,
104 uint64_t *userp, uint64_t *groupp)
105 {
106 /*
107 * Is it a valid type of object to track?
108 */
109 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
110 return (ENOENT);
111 (void) fprintf(stderr, "modifying object that needs user accounting");
142 * default cachefile.
143 */
144 if (readonly && spa_open(target, &spa, FTAG) == 0) {
145 spa_close(spa, FTAG);
146 return;
147 }
148
149 g_importargs.unique = B_TRUE;
150 g_importargs.can_be_active = readonly;
151 g_pool = strdup(target);
152 if ((sepp = strpbrk(g_pool, "/@")) != NULL)
153 *sepp = '\0';
154 g_importargs.poolname = g_pool;
155 pools = zpool_search_import(g_zfs, &g_importargs);
156
157 if (nvlist_empty(pools)) {
158 if (!g_importargs.can_be_active) {
159 g_importargs.can_be_active = B_TRUE;
160 if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
161 spa_open(target, &spa, FTAG) == 0) {
162 fatal("cannot import '%s': pool is active; run "
163 "\"zpool export %s\" first\n",
164 g_pool, g_pool);
165 }
166 }
167
168 fatal("cannot import '%s': no such pool available\n", g_pool);
169 }
170
171 elem = nvlist_next_nvpair(pools, NULL);
172 name = nvpair_name(elem);
173 verify(nvpair_value_nvlist(elem, &config) == 0);
174
175 props = NULL;
176 if (readonly) {
177 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
178 verify(nvlist_add_uint64(props,
179 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
180 }
181
182 zfeature_checks_disable = B_TRUE;
183 error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
184 zfeature_checks_disable = B_FALSE;
185 if (error == EEXIST)
186 error = 0;
187
188 if (error)
189 fatal("can't import '%s': %s", name, strerror(error));
190 }
191
192 static void
193 zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
194 {
195 int err;
196
197 import_pool(target, readonly);
198
199 zfeature_checks_disable = B_TRUE;
200 err = spa_open(target, spa, tag);
201 zfeature_checks_disable = B_FALSE;
202
203 if (err != 0)
204 fatal("cannot open '%s': %s", target, strerror(err));
205 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
206 fatal("'%s' has version %d, features not enabled", target,
207 (int)spa_version(*spa));
208 }
209 }
210
211 static void
212 dump_obj(objset_t *os, uint64_t obj, const char *name)
213 {
214 zap_cursor_t zc;
215 zap_attribute_t za;
216
217 (void) printf("%s_obj:\n", name);
218
219 for (zap_cursor_init(&zc, os, obj);
220 zap_cursor_retrieve(&zc, &za) == 0;
221 zap_cursor_advance(&zc)) {
222 if (za.za_integer_length == 8) {
223 ASSERT(za.za_num_integers == 1);
224 (void) printf("\t%s = %llu\n",
225 za.za_name, (u_longlong_t)za.za_first_integer);
226 } else {
227 ASSERT(za.za_integer_length == 1);
319 usage();
320 break;
321 }
322 }
323
324 if (desc == NULL)
325 desc = strdup("zhack injected");
326 feature.fi_desc = desc;
327
328 argc -= optind;
329 argv += optind;
330
331 if (argc < 2) {
332 (void) fprintf(stderr, "error: missing feature or pool name\n");
333 usage();
334 }
335 target = argv[0];
336 feature.fi_guid = argv[1];
337
338 if (!zfeature_is_valid_guid(feature.fi_guid))
339 fatal("invalid feature guid: %s", feature.fi_guid);
340
341 zhack_spa_open(target, B_FALSE, FTAG, &spa);
342 mos = spa->spa_meta_objset;
343
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);
348
349 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
350 feature_enable_sync, &feature, 5));
351
352 spa_close(spa, FTAG);
353
354 free(desc);
355 }
356
357 static void
358 feature_incr_sync(void *arg, dmu_tx_t *tx)
359 {
360 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
361 zfeature_info_t *feature = arg;
362
363 spa_feature_incr(spa, feature, tx);
364 spa_history_log_internal(spa, "zhack feature incr", tx,
365 "guid=%s", feature->fi_guid);
366 }
367
406 break;
407 case 'd':
408 decr = B_TRUE;
409 break;
410 default:
411 usage();
412 break;
413 }
414 }
415 argc -= optind;
416 argv += optind;
417
418 if (argc < 2) {
419 (void) fprintf(stderr, "error: missing feature or pool name\n");
420 usage();
421 }
422 target = argv[0];
423 feature.fi_guid = argv[1];
424
425 if (!zfeature_is_valid_guid(feature.fi_guid))
426 fatal("invalid feature guid: %s", feature.fi_guid);
427
428 zhack_spa_open(target, B_FALSE, FTAG, &spa);
429 mos = spa->spa_meta_objset;
430
431 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
432 fatal("'%s' is a real feature, will not change refcount");
433
434 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
435 feature.fi_guid)) {
436 feature.fi_can_readonly = B_FALSE;
437 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
438 feature.fi_guid)) {
439 feature.fi_can_readonly = B_TRUE;
440 } else {
441 fatal("feature is not enabled: %s", feature.fi_guid);
442 }
443
444 if (decr && !spa_feature_is_active(spa, &feature))
445 fatal("feature refcount already 0: %s", feature.fi_guid);
446
447 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
448 decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
449
450 spa_close(spa, FTAG);
451 }
452
453 static int
454 zhack_do_feature(int argc, char **argv)
455 {
456 char *subcommand;
457
458 argc--;
459 argv++;
460 if (argc == 0) {
461 (void) fprintf(stderr,
462 "error: no feature operation specified\n");
463 usage();
464 }
465
513
514 argc -= optind;
515 argv += optind;
516 optind = 1;
517
518 if (argc == 0) {
519 (void) fprintf(stderr, "error: no command specified\n");
520 usage();
521 }
522
523 subcommand = argv[0];
524
525 if (strcmp(subcommand, "feature") == 0) {
526 rv = zhack_do_feature(argc, argv);
527 } else {
528 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
529 subcommand);
530 usage();
531 }
532
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");
536 }
537
538 libzfs_fini(g_zfs);
539 kernel_fini();
540
541 return (rv);
542 }
|
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 /*
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
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>
68 "where <subcommand> <args> is one of the following:\n"
69 "\n", cmdname);
70
71 (void) fprintf(stderr,
72 " feature stat <pool>\n"
73 " print information about enabled features\n"
74 " feature enable [-d desc] <pool> <feature>\n"
75 " add a new enabled feature to the pool\n"
76 " -d <desc> sets the feature's description\n"
77 " feature ref [-md] <pool> <feature>\n"
78 " change the refcount on the given feature\n"
79 " -d decrease instead of increase the refcount\n"
80 " -m add the feature to the label if increasing refcount\n"
81 "\n"
82 " <feature> : should be a feature guid\n");
83 exit(1);
84 }
85
86
87 static void
88 fatal(spa_t *spa, void *tag, const char *fmt, ...)
89 {
90 va_list ap;
91
92 if (spa != NULL) {
93 spa_close(spa, tag);
94 (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
95 }
96
97 va_start(ap, fmt);
98 (void) fprintf(stderr, "%s: ", cmdname);
99 (void) vfprintf(stderr, fmt, ap);
100 va_end(ap);
101 (void) fprintf(stderr, "\n");
102
103 exit(1);
104 }
105
106 /* ARGSUSED */
107 static int
108 space_delta_cb(dmu_object_type_t bonustype, void *data,
109 uint64_t *userp, uint64_t *groupp)
110 {
111 /*
112 * Is it a valid type of object to track?
113 */
114 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
115 return (ENOENT);
116 (void) fprintf(stderr, "modifying object that needs user accounting");
147 * default cachefile.
148 */
149 if (readonly && spa_open(target, &spa, FTAG) == 0) {
150 spa_close(spa, FTAG);
151 return;
152 }
153
154 g_importargs.unique = B_TRUE;
155 g_importargs.can_be_active = readonly;
156 g_pool = strdup(target);
157 if ((sepp = strpbrk(g_pool, "/@")) != NULL)
158 *sepp = '\0';
159 g_importargs.poolname = g_pool;
160 pools = zpool_search_import(g_zfs, &g_importargs);
161
162 if (nvlist_empty(pools)) {
163 if (!g_importargs.can_be_active) {
164 g_importargs.can_be_active = B_TRUE;
165 if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
166 spa_open(target, &spa, FTAG) == 0) {
167 fatal(spa, FTAG, "cannot import '%s': pool is "
168 "active; run " "\"zpool export %s\" "
169 "first\n", g_pool, g_pool);
170 }
171 }
172
173 fatal(NULL, FTAG, "cannot import '%s': no such pool "
174 "available\n", g_pool);
175 }
176
177 elem = nvlist_next_nvpair(pools, NULL);
178 name = nvpair_name(elem);
179 verify(nvpair_value_nvlist(elem, &config) == 0);
180
181 props = NULL;
182 if (readonly) {
183 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
184 verify(nvlist_add_uint64(props,
185 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
186 }
187
188 zfeature_checks_disable = B_TRUE;
189 error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
190 zfeature_checks_disable = B_FALSE;
191 if (error == EEXIST)
192 error = 0;
193
194 if (error)
195 fatal(NULL, FTAG, "can't import '%s': %s", name,
196 strerror(error));
197 }
198
199 static void
200 zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
201 {
202 int err;
203
204 import_pool(target, readonly);
205
206 zfeature_checks_disable = B_TRUE;
207 err = spa_open(target, spa, tag);
208 zfeature_checks_disable = B_FALSE;
209
210 if (err != 0)
211 fatal(*spa, FTAG, "cannot open '%s': %s", target,
212 strerror(err));
213 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
214 fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
215 target, (int)spa_version(*spa));
216 }
217 }
218
219 static void
220 dump_obj(objset_t *os, uint64_t obj, const char *name)
221 {
222 zap_cursor_t zc;
223 zap_attribute_t za;
224
225 (void) printf("%s_obj:\n", name);
226
227 for (zap_cursor_init(&zc, os, obj);
228 zap_cursor_retrieve(&zc, &za) == 0;
229 zap_cursor_advance(&zc)) {
230 if (za.za_integer_length == 8) {
231 ASSERT(za.za_num_integers == 1);
232 (void) printf("\t%s = %llu\n",
233 za.za_name, (u_longlong_t)za.za_first_integer);
234 } else {
235 ASSERT(za.za_integer_length == 1);
327 usage();
328 break;
329 }
330 }
331
332 if (desc == NULL)
333 desc = strdup("zhack injected");
334 feature.fi_desc = desc;
335
336 argc -= optind;
337 argv += optind;
338
339 if (argc < 2) {
340 (void) fprintf(stderr, "error: missing feature or pool name\n");
341 usage();
342 }
343 target = argv[0];
344 feature.fi_guid = argv[1];
345
346 if (!zfeature_is_valid_guid(feature.fi_guid))
347 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
348
349 zhack_spa_open(target, B_FALSE, FTAG, &spa);
350 mos = spa->spa_meta_objset;
351
352 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
353 fatal(spa, FTAG, "'%s' is a real feature, will not enable");
354 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
355 fatal(spa, FTAG, "feature already enabled: %s",
356 feature.fi_guid);
357
358 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
359 feature_enable_sync, &feature, 5));
360
361 spa_close(spa, FTAG);
362
363 free(desc);
364 }
365
366 static void
367 feature_incr_sync(void *arg, dmu_tx_t *tx)
368 {
369 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
370 zfeature_info_t *feature = arg;
371
372 spa_feature_incr(spa, feature, tx);
373 spa_history_log_internal(spa, "zhack feature incr", tx,
374 "guid=%s", feature->fi_guid);
375 }
376
415 break;
416 case 'd':
417 decr = B_TRUE;
418 break;
419 default:
420 usage();
421 break;
422 }
423 }
424 argc -= optind;
425 argv += optind;
426
427 if (argc < 2) {
428 (void) fprintf(stderr, "error: missing feature or pool name\n");
429 usage();
430 }
431 target = argv[0];
432 feature.fi_guid = argv[1];
433
434 if (!zfeature_is_valid_guid(feature.fi_guid))
435 fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
436
437 zhack_spa_open(target, B_FALSE, FTAG, &spa);
438 mos = spa->spa_meta_objset;
439
440 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
441 fatal(spa, FTAG, "'%s' is a real feature, will not change "
442 "refcount");
443
444 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
445 feature.fi_guid)) {
446 feature.fi_can_readonly = B_FALSE;
447 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
448 feature.fi_guid)) {
449 feature.fi_can_readonly = B_TRUE;
450 } else {
451 fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
452 }
453
454 if (decr && !spa_feature_is_active(spa, &feature))
455 fatal(spa, FTAG, "feature refcount already 0: %s",
456 feature.fi_guid);
457
458 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
459 decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
460
461 spa_close(spa, FTAG);
462 }
463
464 static int
465 zhack_do_feature(int argc, char **argv)
466 {
467 char *subcommand;
468
469 argc--;
470 argv++;
471 if (argc == 0) {
472 (void) fprintf(stderr,
473 "error: no feature operation specified\n");
474 usage();
475 }
476
524
525 argc -= optind;
526 argv += optind;
527 optind = 1;
528
529 if (argc == 0) {
530 (void) fprintf(stderr, "error: no command specified\n");
531 usage();
532 }
533
534 subcommand = argv[0];
535
536 if (strcmp(subcommand, "feature") == 0) {
537 rv = zhack_do_feature(argc, argv);
538 } else {
539 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
540 subcommand);
541 usage();
542 }
543
544 if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
545 fatal(NULL, FTAG, "pool export failed; "
546 "changes may not be committed to disk\n");
547 }
548
549 libzfs_fini(g_zfs);
550 kernel_fini();
551
552 return (rv);
553 }
|