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