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