Print this page
4168 ztest assertion failure in dbuf_undirty
4169 verbatim import causes zdb to segfault
4170 zhack leaves pool in ACTIVE state
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Eric Schrock <eric.schrock@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zhack/zhack.c
+++ new/usr/src/cmd/zhack/zhack.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 - * Copyright (c) 2012 by Delphix. All rights reserved.
23 + * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 25 */
26 26
27 27 /*
28 28 * zhack is a debugging tool that can write changes to ZFS pool using libzpool
29 29 * for testing purposes. Altering pools with zhack is unsupported and may
30 30 * result in corrupted pools.
31 31 */
32 32
33 33 #include <stdio.h>
34 34 #include <stdlib.h>
35 35 #include <ctype.h>
36 36 #include <sys/zfs_context.h>
37 37 #include <sys/spa.h>
38 38 #include <sys/spa_impl.h>
39 39 #include <sys/dmu.h>
40 40 #include <sys/zap.h>
41 41 #include <sys/zfs_znode.h>
42 42 #include <sys/dsl_synctask.h>
43 43 #include <sys/vdev.h>
44 44 #include <sys/fs/zfs.h>
45 45 #include <sys/dmu_objset.h>
46 46 #include <sys/dsl_pool.h>
47 47 #include <sys/zio_checksum.h>
48 48 #include <sys/zio_compress.h>
49 49 #include <sys/zfeature.h>
50 50 #include <sys/dmu_tx.h>
51 51 #undef ZFS_MAXNAMELEN
52 52 #undef verify
53 53 #include <libzfs.h>
54 54
55 55 extern boolean_t zfeature_checks_disable;
56 56
57 57 const char cmdname[] = "zhack";
58 58 libzfs_handle_t *g_zfs;
59 59 static importargs_t g_importargs;
60 60 static char *g_pool;
61 61 static boolean_t g_readonly;
62 62
63 63 static void
64 64 usage(void)
65 65 {
66 66 (void) fprintf(stderr,
67 67 "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n"
68 68 "where <subcommand> <args> is one of the following:\n"
69 69 "\n", cmdname);
70 70
71 71 (void) fprintf(stderr,
72 72 " feature stat <pool>\n"
73 73 " print information about enabled features\n"
74 74 " feature enable [-d desc] <pool> <feature>\n"
75 75 " add a new enabled feature to the pool\n"
76 76 " -d <desc> sets the feature's description\n"
77 77 " feature ref [-md] <pool> <feature>\n"
↓ open down ↓ |
44 lines elided |
↑ open up ↑ |
78 78 " change the refcount on the given feature\n"
79 79 " -d decrease instead of increase the refcount\n"
80 80 " -m add the feature to the label if increasing refcount\n"
81 81 "\n"
82 82 " <feature> : should be a feature guid\n");
83 83 exit(1);
84 84 }
85 85
86 86
87 87 static void
88 -fatal(const char *fmt, ...)
88 +fatal(spa_t *spa, void *tag, const char *fmt, ...)
89 89 {
90 90 va_list ap;
91 91
92 + if (spa != NULL) {
93 + spa_close(spa, tag);
94 + (void) spa_export(g_pool, NULL, B_TRUE, B_FALSE);
95 + }
96 +
92 97 va_start(ap, fmt);
93 98 (void) fprintf(stderr, "%s: ", cmdname);
94 99 (void) vfprintf(stderr, fmt, ap);
95 100 va_end(ap);
96 101 (void) fprintf(stderr, "\n");
97 102
98 103 exit(1);
99 104 }
100 105
101 106 /* ARGSUSED */
102 107 static int
103 108 space_delta_cb(dmu_object_type_t bonustype, void *data,
104 109 uint64_t *userp, uint64_t *groupp)
105 110 {
106 111 /*
107 112 * Is it a valid type of object to track?
108 113 */
109 114 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
110 115 return (ENOENT);
111 116 (void) fprintf(stderr, "modifying object that needs user accounting");
112 117 abort();
113 118 /* NOTREACHED */
114 119 }
115 120
116 121 /*
117 122 * Target is the dataset whose pool we want to open.
118 123 */
119 124 static void
120 125 import_pool(const char *target, boolean_t readonly)
121 126 {
122 127 nvlist_t *config;
123 128 nvlist_t *pools;
124 129 int error;
125 130 char *sepp;
126 131 spa_t *spa;
127 132 nvpair_t *elem;
128 133 nvlist_t *props;
129 134 const char *name;
130 135
131 136 kernel_init(readonly ? FREAD : (FREAD | FWRITE));
132 137 g_zfs = libzfs_init();
133 138 ASSERT(g_zfs != NULL);
134 139
135 140 dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb);
136 141
137 142 g_readonly = readonly;
138 143
139 144 /*
140 145 * If we only want readonly access, it's OK if we find
141 146 * a potentially-active (ie, imported into the kernel) pool from the
142 147 * default cachefile.
143 148 */
144 149 if (readonly && spa_open(target, &spa, FTAG) == 0) {
145 150 spa_close(spa, FTAG);
146 151 return;
147 152 }
148 153
149 154 g_importargs.unique = B_TRUE;
150 155 g_importargs.can_be_active = readonly;
151 156 g_pool = strdup(target);
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
152 157 if ((sepp = strpbrk(g_pool, "/@")) != NULL)
153 158 *sepp = '\0';
154 159 g_importargs.poolname = g_pool;
155 160 pools = zpool_search_import(g_zfs, &g_importargs);
156 161
157 162 if (nvlist_empty(pools)) {
158 163 if (!g_importargs.can_be_active) {
159 164 g_importargs.can_be_active = B_TRUE;
160 165 if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
161 166 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);
167 + fatal(spa, FTAG, "cannot import '%s': pool is "
168 + "active; run " "\"zpool export %s\" "
169 + "first\n", g_pool, g_pool);
165 170 }
166 171 }
167 172
168 - fatal("cannot import '%s': no such pool available\n", g_pool);
173 + fatal(NULL, FTAG, "cannot import '%s': no such pool "
174 + "available\n", g_pool);
169 175 }
170 176
171 177 elem = nvlist_next_nvpair(pools, NULL);
172 178 name = nvpair_name(elem);
173 179 verify(nvpair_value_nvlist(elem, &config) == 0);
174 180
175 181 props = NULL;
176 182 if (readonly) {
177 183 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
178 184 verify(nvlist_add_uint64(props,
179 185 zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0);
180 186 }
181 187
182 188 zfeature_checks_disable = B_TRUE;
183 189 error = spa_import(name, config, props, ZFS_IMPORT_NORMAL);
184 190 zfeature_checks_disable = B_FALSE;
185 191 if (error == EEXIST)
186 192 error = 0;
187 193
188 194 if (error)
189 - fatal("can't import '%s': %s", name, strerror(error));
195 + fatal(NULL, FTAG, "can't import '%s': %s", name,
196 + strerror(error));
190 197 }
191 198
192 199 static void
193 200 zhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa)
194 201 {
195 202 int err;
196 203
197 204 import_pool(target, readonly);
198 205
199 206 zfeature_checks_disable = B_TRUE;
200 207 err = spa_open(target, spa, tag);
201 208 zfeature_checks_disable = B_FALSE;
202 209
203 210 if (err != 0)
204 - fatal("cannot open '%s': %s", target, strerror(err));
211 + fatal(*spa, FTAG, "cannot open '%s': %s", target,
212 + strerror(err));
205 213 if (spa_version(*spa) < SPA_VERSION_FEATURES) {
206 - fatal("'%s' has version %d, features not enabled", target,
207 - (int)spa_version(*spa));
214 + fatal(*spa, FTAG, "'%s' has version %d, features not enabled",
215 + target, (int)spa_version(*spa));
208 216 }
209 217 }
210 218
211 219 static void
212 220 dump_obj(objset_t *os, uint64_t obj, const char *name)
213 221 {
214 222 zap_cursor_t zc;
215 223 zap_attribute_t za;
216 224
217 225 (void) printf("%s_obj:\n", name);
218 226
219 227 for (zap_cursor_init(&zc, os, obj);
220 228 zap_cursor_retrieve(&zc, &za) == 0;
221 229 zap_cursor_advance(&zc)) {
222 230 if (za.za_integer_length == 8) {
223 231 ASSERT(za.za_num_integers == 1);
224 232 (void) printf("\t%s = %llu\n",
225 233 za.za_name, (u_longlong_t)za.za_first_integer);
226 234 } else {
227 235 ASSERT(za.za_integer_length == 1);
228 236 char val[1024];
229 237 VERIFY(zap_lookup(os, obj, za.za_name,
230 238 1, sizeof (val), val) == 0);
231 239 (void) printf("\t%s = %s\n", za.za_name, val);
232 240 }
233 241 }
234 242 zap_cursor_fini(&zc);
235 243 }
236 244
237 245 static void
238 246 dump_mos(spa_t *spa)
239 247 {
240 248 nvlist_t *nv = spa->spa_label_features;
241 249
242 250 (void) printf("label config:\n");
243 251 for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL);
244 252 pair != NULL;
245 253 pair = nvlist_next_nvpair(nv, pair)) {
246 254 (void) printf("\t%s\n", nvpair_name(pair));
247 255 }
248 256 }
249 257
250 258 static void
251 259 zhack_do_feature_stat(int argc, char **argv)
252 260 {
253 261 spa_t *spa;
254 262 objset_t *os;
255 263 char *target;
256 264
257 265 argc--;
258 266 argv++;
259 267
260 268 if (argc < 1) {
261 269 (void) fprintf(stderr, "error: missing pool name\n");
262 270 usage();
263 271 }
264 272 target = argv[0];
265 273
266 274 zhack_spa_open(target, B_TRUE, FTAG, &spa);
267 275 os = spa->spa_meta_objset;
268 276
269 277 dump_obj(os, spa->spa_feat_for_read_obj, "for_read");
270 278 dump_obj(os, spa->spa_feat_for_write_obj, "for_write");
271 279 dump_obj(os, spa->spa_feat_desc_obj, "descriptions");
272 280 dump_mos(spa);
273 281
274 282 spa_close(spa, FTAG);
275 283 }
276 284
277 285 static void
278 286 feature_enable_sync(void *arg, dmu_tx_t *tx)
279 287 {
280 288 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
281 289 zfeature_info_t *feature = arg;
282 290
283 291 spa_feature_enable(spa, feature, tx);
284 292 spa_history_log_internal(spa, "zhack enable feature", tx,
285 293 "guid=%s can_readonly=%u",
286 294 feature->fi_guid, feature->fi_can_readonly);
287 295 }
288 296
289 297 static void
290 298 zhack_do_feature_enable(int argc, char **argv)
291 299 {
292 300 char c;
293 301 char *desc, *target;
294 302 spa_t *spa;
295 303 objset_t *mos;
296 304 zfeature_info_t feature;
297 305 zfeature_info_t *nodeps[] = { NULL };
298 306
299 307 /*
300 308 * Features are not added to the pool's label until their refcounts
301 309 * are incremented, so fi_mos can just be left as false for now.
302 310 */
303 311 desc = NULL;
304 312 feature.fi_uname = "zhack";
305 313 feature.fi_mos = B_FALSE;
306 314 feature.fi_can_readonly = B_FALSE;
307 315 feature.fi_depends = nodeps;
308 316
309 317 optind = 1;
310 318 while ((c = getopt(argc, argv, "rmd:")) != -1) {
311 319 switch (c) {
312 320 case 'r':
313 321 feature.fi_can_readonly = B_TRUE;
314 322 break;
315 323 case 'd':
316 324 desc = strdup(optarg);
317 325 break;
318 326 default:
319 327 usage();
320 328 break;
321 329 }
322 330 }
323 331
324 332 if (desc == NULL)
325 333 desc = strdup("zhack injected");
326 334 feature.fi_desc = desc;
327 335
328 336 argc -= optind;
↓ open down ↓ |
111 lines elided |
↑ open up ↑ |
329 337 argv += optind;
330 338
331 339 if (argc < 2) {
332 340 (void) fprintf(stderr, "error: missing feature or pool name\n");
333 341 usage();
334 342 }
335 343 target = argv[0];
336 344 feature.fi_guid = argv[1];
337 345
338 346 if (!zfeature_is_valid_guid(feature.fi_guid))
339 - fatal("invalid feature guid: %s", feature.fi_guid);
347 + fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
340 348
341 349 zhack_spa_open(target, B_FALSE, FTAG, &spa);
342 350 mos = spa->spa_meta_objset;
343 351
344 352 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
345 - fatal("'%s' is a real feature, will not enable");
353 + fatal(spa, FTAG, "'%s' is a real feature, will not enable");
346 354 if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid))
347 - fatal("feature already enabled: %s", feature.fi_guid);
355 + fatal(spa, FTAG, "feature already enabled: %s",
356 + feature.fi_guid);
348 357
349 358 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
350 359 feature_enable_sync, &feature, 5));
351 360
352 361 spa_close(spa, FTAG);
353 362
354 363 free(desc);
355 364 }
356 365
357 366 static void
358 367 feature_incr_sync(void *arg, dmu_tx_t *tx)
359 368 {
360 369 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
361 370 zfeature_info_t *feature = arg;
362 371
363 372 spa_feature_incr(spa, feature, tx);
364 373 spa_history_log_internal(spa, "zhack feature incr", tx,
365 374 "guid=%s", feature->fi_guid);
366 375 }
367 376
368 377 static void
369 378 feature_decr_sync(void *arg, dmu_tx_t *tx)
370 379 {
371 380 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
372 381 zfeature_info_t *feature = arg;
373 382
374 383 spa_feature_decr(spa, feature, tx);
375 384 spa_history_log_internal(spa, "zhack feature decr", tx,
376 385 "guid=%s", feature->fi_guid);
377 386 }
378 387
379 388 static void
380 389 zhack_do_feature_ref(int argc, char **argv)
381 390 {
382 391 char c;
383 392 char *target;
384 393 boolean_t decr = B_FALSE;
385 394 spa_t *spa;
386 395 objset_t *mos;
387 396 zfeature_info_t feature;
388 397 zfeature_info_t *nodeps[] = { NULL };
389 398
390 399 /*
391 400 * fi_desc does not matter here because it was written to disk
392 401 * when the feature was enabled, but we need to properly set the
393 402 * feature for read or write based on the information we read off
394 403 * disk later.
395 404 */
396 405 feature.fi_uname = "zhack";
397 406 feature.fi_mos = B_FALSE;
398 407 feature.fi_desc = NULL;
399 408 feature.fi_depends = nodeps;
400 409
401 410 optind = 1;
402 411 while ((c = getopt(argc, argv, "md")) != -1) {
403 412 switch (c) {
404 413 case 'm':
405 414 feature.fi_mos = B_TRUE;
406 415 break;
407 416 case 'd':
408 417 decr = B_TRUE;
409 418 break;
410 419 default:
411 420 usage();
412 421 break;
413 422 }
414 423 }
415 424 argc -= optind;
↓ open down ↓ |
58 lines elided |
↑ open up ↑ |
416 425 argv += optind;
417 426
418 427 if (argc < 2) {
419 428 (void) fprintf(stderr, "error: missing feature or pool name\n");
420 429 usage();
421 430 }
422 431 target = argv[0];
423 432 feature.fi_guid = argv[1];
424 433
425 434 if (!zfeature_is_valid_guid(feature.fi_guid))
426 - fatal("invalid feature guid: %s", feature.fi_guid);
435 + fatal(NULL, FTAG, "invalid feature guid: %s", feature.fi_guid);
427 436
428 437 zhack_spa_open(target, B_FALSE, FTAG, &spa);
429 438 mos = spa->spa_meta_objset;
430 439
431 440 if (0 == zfeature_lookup_guid(feature.fi_guid, NULL))
432 - fatal("'%s' is a real feature, will not change refcount");
441 + fatal(spa, FTAG, "'%s' is a real feature, will not change "
442 + "refcount");
433 443
434 444 if (0 == zap_contains(mos, spa->spa_feat_for_read_obj,
435 445 feature.fi_guid)) {
436 446 feature.fi_can_readonly = B_FALSE;
437 447 } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj,
438 448 feature.fi_guid)) {
439 449 feature.fi_can_readonly = B_TRUE;
440 450 } else {
441 - fatal("feature is not enabled: %s", feature.fi_guid);
451 + fatal(spa, FTAG, "feature is not enabled: %s", feature.fi_guid);
442 452 }
443 453
444 454 if (decr && !spa_feature_is_active(spa, &feature))
445 - fatal("feature refcount already 0: %s", feature.fi_guid);
455 + fatal(spa, FTAG, "feature refcount already 0: %s",
456 + feature.fi_guid);
446 457
447 458 VERIFY0(dsl_sync_task(spa_name(spa), NULL,
448 459 decr ? feature_decr_sync : feature_incr_sync, &feature, 5));
449 460
450 461 spa_close(spa, FTAG);
451 462 }
452 463
453 464 static int
454 465 zhack_do_feature(int argc, char **argv)
455 466 {
456 467 char *subcommand;
457 468
458 469 argc--;
459 470 argv++;
460 471 if (argc == 0) {
461 472 (void) fprintf(stderr,
462 473 "error: no feature operation specified\n");
463 474 usage();
464 475 }
465 476
466 477 subcommand = argv[0];
467 478 if (strcmp(subcommand, "stat") == 0) {
468 479 zhack_do_feature_stat(argc, argv);
469 480 } else if (strcmp(subcommand, "enable") == 0) {
470 481 zhack_do_feature_enable(argc, argv);
471 482 } else if (strcmp(subcommand, "ref") == 0) {
472 483 zhack_do_feature_ref(argc, argv);
473 484 } else {
474 485 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
475 486 subcommand);
476 487 usage();
477 488 }
478 489
479 490 return (0);
480 491 }
481 492
482 493 #define MAX_NUM_PATHS 1024
483 494
484 495 int
485 496 main(int argc, char **argv)
486 497 {
487 498 extern void zfs_prop_init(void);
488 499
489 500 char *path[MAX_NUM_PATHS];
490 501 const char *subcommand;
491 502 int rv = 0;
492 503 char c;
493 504
494 505 g_importargs.path = path;
495 506
496 507 dprintf_setup(&argc, argv);
497 508 zfs_prop_init();
498 509
499 510 while ((c = getopt(argc, argv, "c:d:")) != -1) {
500 511 switch (c) {
501 512 case 'c':
502 513 g_importargs.cachefile = optarg;
503 514 break;
504 515 case 'd':
505 516 assert(g_importargs.paths < MAX_NUM_PATHS);
506 517 g_importargs.path[g_importargs.paths++] = optarg;
507 518 break;
508 519 default:
509 520 usage();
510 521 break;
511 522 }
512 523 }
513 524
514 525 argc -= optind;
515 526 argv += optind;
516 527 optind = 1;
517 528
518 529 if (argc == 0) {
519 530 (void) fprintf(stderr, "error: no command specified\n");
520 531 usage();
521 532 }
522 533
↓ open down ↓ |
67 lines elided |
↑ open up ↑ |
523 534 subcommand = argv[0];
524 535
525 536 if (strcmp(subcommand, "feature") == 0) {
526 537 rv = zhack_do_feature(argc, argv);
527 538 } else {
528 539 (void) fprintf(stderr, "error: unknown subcommand: %s\n",
529 540 subcommand);
530 541 usage();
531 542 }
532 543
533 - if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_TRUE) != 0) {
534 - fatal("pool export failed; "
544 + if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_FALSE) != 0) {
545 + fatal(NULL, FTAG, "pool export failed; "
535 546 "changes may not be committed to disk\n");
536 547 }
537 548
538 549 libzfs_fini(g_zfs);
539 550 kernel_fini();
540 551
541 552 return (rv);
542 553 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX