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