1236884Smm/* 2236884Smm * CDDL HEADER START 3236884Smm * 4236884Smm * The contents of this file are subject to the terms of the 5236884Smm * Common Development and Distribution License (the "License"). 6236884Smm * You may not use this file except in compliance with the License. 7236884Smm * 8236884Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9236884Smm * or http://www.opensolaris.org/os/licensing. 10236884Smm * See the License for the specific language governing permissions 11236884Smm * and limitations under the License. 12236884Smm * 13236884Smm * When distributing Covered Code, include this CDDL HEADER in each 14236884Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15236884Smm * If applicable, add the following below this CDDL HEADER, with the 16236884Smm * fields enclosed by brackets "[]" replaced with your own identifying 17236884Smm * information: Portions Copyright [yyyy] [name of copyright owner] 18236884Smm * 19236884Smm * CDDL HEADER END 20236884Smm */ 21236884Smm 22236884Smm/* 23236884Smm * Copyright (c) 2012 by Delphix. All rights reserved. 24251646Sdelphij * Copyright (c) 2013 Steven Hartland. All rights reserved. 25236884Smm */ 26236884Smm 27236884Smm/* 28236884Smm * zhack is a debugging tool that can write changes to ZFS pool using libzpool 29236884Smm * for testing purposes. Altering pools with zhack is unsupported and may 30236884Smm * result in corrupted pools. 31236884Smm */ 32236884Smm 33236884Smm#include <stdio.h> 34236884Smm#include <stdlib.h> 35236884Smm#include <ctype.h> 36236884Smm#include <sys/zfs_context.h> 37236884Smm#include <sys/spa.h> 38236884Smm#include <sys/spa_impl.h> 39236884Smm#include <sys/dmu.h> 40236884Smm#include <sys/zap.h> 41236884Smm#include <sys/zfs_znode.h> 42236884Smm#include <sys/dsl_synctask.h> 43236884Smm#include <sys/vdev.h> 44236884Smm#include <sys/fs/zfs.h> 45236884Smm#include <sys/dmu_objset.h> 46236884Smm#include <sys/dsl_pool.h> 47236884Smm#include <sys/zio_checksum.h> 48236884Smm#include <sys/zio_compress.h> 49236884Smm#include <sys/zfeature.h> 50248571Smm#include <sys/dmu_tx.h> 51236884Smm#undef ZFS_MAXNAMELEN 52236884Smm#undef verify 53236884Smm#include <libzfs.h> 54236884Smm 55236884Smmextern boolean_t zfeature_checks_disable; 56236884Smm 57236884Smmconst char cmdname[] = "zhack"; 58236884Smmlibzfs_handle_t *g_zfs; 59236884Smmstatic importargs_t g_importargs; 60236884Smmstatic char *g_pool; 61236884Smmstatic boolean_t g_readonly; 62236884Smm 63236884Smmstatic void 64236884Smmusage(void) 65236884Smm{ 66236884Smm (void) fprintf(stderr, 67236884Smm "Usage: %s [-c cachefile] [-d dir] <subcommand> <args> ...\n" 68236884Smm "where <subcommand> <args> is one of the following:\n" 69236884Smm "\n", cmdname); 70236884Smm 71236884Smm (void) fprintf(stderr, 72236884Smm " feature stat <pool>\n" 73236884Smm " print information about enabled features\n" 74236884Smm " feature enable [-d desc] <pool> <feature>\n" 75236884Smm " add a new enabled feature to the pool\n" 76236884Smm " -d <desc> sets the feature's description\n" 77236884Smm " feature ref [-md] <pool> <feature>\n" 78236884Smm " change the refcount on the given feature\n" 79236884Smm " -d decrease instead of increase the refcount\n" 80236884Smm " -m add the feature to the label if increasing refcount\n" 81236884Smm "\n" 82236884Smm " <feature> : should be a feature guid\n"); 83236884Smm exit(1); 84236884Smm} 85236884Smm 86236884Smm 87236884Smmstatic void 88236884Smmfatal(const char *fmt, ...) 89236884Smm{ 90236884Smm va_list ap; 91236884Smm 92236884Smm va_start(ap, fmt); 93236884Smm (void) fprintf(stderr, "%s: ", cmdname); 94236884Smm (void) vfprintf(stderr, fmt, ap); 95236884Smm va_end(ap); 96236884Smm (void) fprintf(stderr, "\n"); 97236884Smm 98236884Smm exit(1); 99236884Smm} 100236884Smm 101236884Smm/* ARGSUSED */ 102236884Smmstatic int 103236884Smmspace_delta_cb(dmu_object_type_t bonustype, void *data, 104236884Smm uint64_t *userp, uint64_t *groupp) 105236884Smm{ 106236884Smm /* 107236884Smm * Is it a valid type of object to track? 108236884Smm */ 109236884Smm if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA) 110236884Smm return (ENOENT); 111236884Smm (void) fprintf(stderr, "modifying object that needs user accounting"); 112236884Smm abort(); 113236884Smm /* NOTREACHED */ 114236884Smm} 115236884Smm 116236884Smm/* 117236884Smm * Target is the dataset whose pool we want to open. 118236884Smm */ 119236884Smmstatic void 120236884Smmimport_pool(const char *target, boolean_t readonly) 121236884Smm{ 122236884Smm nvlist_t *config; 123236884Smm nvlist_t *pools; 124236884Smm int error; 125236884Smm char *sepp; 126236884Smm spa_t *spa; 127236884Smm nvpair_t *elem; 128236884Smm nvlist_t *props; 129236884Smm const char *name; 130236884Smm 131236884Smm kernel_init(readonly ? FREAD : (FREAD | FWRITE)); 132236884Smm g_zfs = libzfs_init(); 133236884Smm ASSERT(g_zfs != NULL); 134236884Smm 135236884Smm dmu_objset_register_type(DMU_OST_ZFS, space_delta_cb); 136236884Smm 137236884Smm g_readonly = readonly; 138236884Smm 139236884Smm /* 140236884Smm * If we only want readonly access, it's OK if we find 141236884Smm * a potentially-active (ie, imported into the kernel) pool from the 142236884Smm * default cachefile. 143236884Smm */ 144236884Smm if (readonly && spa_open(target, &spa, FTAG) == 0) { 145236884Smm spa_close(spa, FTAG); 146236884Smm return; 147236884Smm } 148236884Smm 149236884Smm g_importargs.unique = B_TRUE; 150236884Smm g_importargs.can_be_active = readonly; 151236884Smm g_pool = strdup(target); 152236884Smm if ((sepp = strpbrk(g_pool, "/@")) != NULL) 153236884Smm *sepp = '\0'; 154236884Smm g_importargs.poolname = g_pool; 155236884Smm pools = zpool_search_import(g_zfs, &g_importargs); 156236884Smm 157251646Sdelphij if (nvlist_empty(pools)) { 158236884Smm if (!g_importargs.can_be_active) { 159236884Smm g_importargs.can_be_active = B_TRUE; 160236884Smm if (zpool_search_import(g_zfs, &g_importargs) != NULL || 161236884Smm spa_open(target, &spa, FTAG) == 0) { 162236884Smm fatal("cannot import '%s': pool is active; run " 163236884Smm "\"zpool export %s\" first\n", 164236884Smm g_pool, g_pool); 165236884Smm } 166236884Smm } 167236884Smm 168236884Smm fatal("cannot import '%s': no such pool available\n", g_pool); 169236884Smm } 170236884Smm 171236884Smm elem = nvlist_next_nvpair(pools, NULL); 172236884Smm name = nvpair_name(elem); 173236884Smm verify(nvpair_value_nvlist(elem, &config) == 0); 174236884Smm 175236884Smm props = NULL; 176236884Smm if (readonly) { 177236884Smm verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0); 178236884Smm verify(nvlist_add_uint64(props, 179236884Smm zpool_prop_to_name(ZPOOL_PROP_READONLY), 1) == 0); 180236884Smm } 181236884Smm 182236884Smm zfeature_checks_disable = B_TRUE; 183236884Smm error = spa_import(name, config, props, ZFS_IMPORT_NORMAL); 184236884Smm zfeature_checks_disable = B_FALSE; 185236884Smm if (error == EEXIST) 186236884Smm error = 0; 187236884Smm 188236884Smm if (error) 189236884Smm fatal("can't import '%s': %s", name, strerror(error)); 190236884Smm} 191236884Smm 192236884Smmstatic void 193236884Smmzhack_spa_open(const char *target, boolean_t readonly, void *tag, spa_t **spa) 194236884Smm{ 195236884Smm int err; 196236884Smm 197236884Smm import_pool(target, readonly); 198236884Smm 199236884Smm zfeature_checks_disable = B_TRUE; 200236884Smm err = spa_open(target, spa, tag); 201236884Smm zfeature_checks_disable = B_FALSE; 202236884Smm 203236884Smm if (err != 0) 204236884Smm fatal("cannot open '%s': %s", target, strerror(err)); 205236884Smm if (spa_version(*spa) < SPA_VERSION_FEATURES) { 206236884Smm fatal("'%s' has version %d, features not enabled", target, 207236884Smm (int)spa_version(*spa)); 208236884Smm } 209236884Smm} 210236884Smm 211236884Smmstatic void 212236884Smmdump_obj(objset_t *os, uint64_t obj, const char *name) 213236884Smm{ 214236884Smm zap_cursor_t zc; 215236884Smm zap_attribute_t za; 216236884Smm 217236884Smm (void) printf("%s_obj:\n", name); 218236884Smm 219236884Smm for (zap_cursor_init(&zc, os, obj); 220236884Smm zap_cursor_retrieve(&zc, &za) == 0; 221236884Smm zap_cursor_advance(&zc)) { 222236884Smm if (za.za_integer_length == 8) { 223236884Smm ASSERT(za.za_num_integers == 1); 224236884Smm (void) printf("\t%s = %llu\n", 225236884Smm za.za_name, (u_longlong_t)za.za_first_integer); 226236884Smm } else { 227236884Smm ASSERT(za.za_integer_length == 1); 228236884Smm char val[1024]; 229236884Smm VERIFY(zap_lookup(os, obj, za.za_name, 230236884Smm 1, sizeof (val), val) == 0); 231236884Smm (void) printf("\t%s = %s\n", za.za_name, val); 232236884Smm } 233236884Smm } 234236884Smm zap_cursor_fini(&zc); 235236884Smm} 236236884Smm 237236884Smmstatic void 238236884Smmdump_mos(spa_t *spa) 239236884Smm{ 240236884Smm nvlist_t *nv = spa->spa_label_features; 241236884Smm 242236884Smm (void) printf("label config:\n"); 243236884Smm for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); 244236884Smm pair != NULL; 245236884Smm pair = nvlist_next_nvpair(nv, pair)) { 246236884Smm (void) printf("\t%s\n", nvpair_name(pair)); 247236884Smm } 248236884Smm} 249236884Smm 250236884Smmstatic void 251236884Smmzhack_do_feature_stat(int argc, char **argv) 252236884Smm{ 253236884Smm spa_t *spa; 254236884Smm objset_t *os; 255236884Smm char *target; 256236884Smm 257236884Smm argc--; 258236884Smm argv++; 259236884Smm 260236884Smm if (argc < 1) { 261236884Smm (void) fprintf(stderr, "error: missing pool name\n"); 262236884Smm usage(); 263236884Smm } 264236884Smm target = argv[0]; 265236884Smm 266236884Smm zhack_spa_open(target, B_TRUE, FTAG, &spa); 267236884Smm os = spa->spa_meta_objset; 268236884Smm 269236884Smm dump_obj(os, spa->spa_feat_for_read_obj, "for_read"); 270236884Smm dump_obj(os, spa->spa_feat_for_write_obj, "for_write"); 271236884Smm dump_obj(os, spa->spa_feat_desc_obj, "descriptions"); 272236884Smm dump_mos(spa); 273236884Smm 274236884Smm spa_close(spa, FTAG); 275236884Smm} 276236884Smm 277236884Smmstatic void 278248571Smmfeature_enable_sync(void *arg, dmu_tx_t *tx) 279236884Smm{ 280248571Smm spa_t *spa = dmu_tx_pool(tx)->dp_spa; 281248571Smm zfeature_info_t *feature = arg; 282236884Smm 283236884Smm spa_feature_enable(spa, feature, tx); 284248571Smm spa_history_log_internal(spa, "zhack enable feature", tx, 285248571Smm "name=%s can_readonly=%u", 286248571Smm feature->fi_guid, feature->fi_can_readonly); 287236884Smm} 288236884Smm 289236884Smmstatic void 290236884Smmzhack_do_feature_enable(int argc, char **argv) 291236884Smm{ 292236884Smm char c; 293236884Smm char *desc, *target; 294236884Smm spa_t *spa; 295236884Smm objset_t *mos; 296236884Smm zfeature_info_t feature; 297236884Smm zfeature_info_t *nodeps[] = { NULL }; 298236884Smm 299236884Smm /* 300236884Smm * Features are not added to the pool's label until their refcounts 301236884Smm * are incremented, so fi_mos can just be left as false for now. 302236884Smm */ 303236884Smm desc = NULL; 304236884Smm feature.fi_uname = "zhack"; 305236884Smm feature.fi_mos = B_FALSE; 306236884Smm feature.fi_can_readonly = B_FALSE; 307236884Smm feature.fi_depends = nodeps; 308236884Smm 309236884Smm optind = 1; 310236884Smm while ((c = getopt(argc, argv, "rmd:")) != -1) { 311236884Smm switch (c) { 312236884Smm case 'r': 313236884Smm feature.fi_can_readonly = B_TRUE; 314236884Smm break; 315236884Smm case 'd': 316236884Smm desc = strdup(optarg); 317236884Smm break; 318236884Smm default: 319236884Smm usage(); 320236884Smm break; 321236884Smm } 322236884Smm } 323236884Smm 324236884Smm if (desc == NULL) 325236884Smm desc = strdup("zhack injected"); 326236884Smm feature.fi_desc = desc; 327236884Smm 328236884Smm argc -= optind; 329236884Smm argv += optind; 330236884Smm 331236884Smm if (argc < 2) { 332236884Smm (void) fprintf(stderr, "error: missing feature or pool name\n"); 333236884Smm usage(); 334236884Smm } 335236884Smm target = argv[0]; 336236884Smm feature.fi_guid = argv[1]; 337236884Smm 338236884Smm if (!zfeature_is_valid_guid(feature.fi_guid)) 339236884Smm fatal("invalid feature guid: %s", feature.fi_guid); 340236884Smm 341236884Smm zhack_spa_open(target, B_FALSE, FTAG, &spa); 342236884Smm mos = spa->spa_meta_objset; 343236884Smm 344236884Smm if (0 == zfeature_lookup_guid(feature.fi_guid, NULL)) 345236884Smm fatal("'%s' is a real feature, will not enable"); 346236884Smm if (0 == zap_contains(mos, spa->spa_feat_desc_obj, feature.fi_guid)) 347236884Smm fatal("feature already enabled: %s", feature.fi_guid); 348236884Smm 349248571Smm VERIFY0(dsl_sync_task(spa_name(spa), NULL, 350248571Smm feature_enable_sync, &feature, 5)); 351236884Smm 352236884Smm spa_close(spa, FTAG); 353236884Smm 354236884Smm free(desc); 355236884Smm} 356236884Smm 357236884Smmstatic void 358248571Smmfeature_incr_sync(void *arg, dmu_tx_t *tx) 359236884Smm{ 360248571Smm spa_t *spa = dmu_tx_pool(tx)->dp_spa; 361248571Smm zfeature_info_t *feature = arg; 362236884Smm 363236884Smm spa_feature_incr(spa, feature, tx); 364248571Smm spa_history_log_internal(spa, "zhack feature incr", tx, 365248571Smm "name=%s", feature->fi_guid); 366236884Smm} 367236884Smm 368236884Smmstatic void 369248571Smmfeature_decr_sync(void *arg, dmu_tx_t *tx) 370236884Smm{ 371248571Smm spa_t *spa = dmu_tx_pool(tx)->dp_spa; 372248571Smm zfeature_info_t *feature = arg; 373236884Smm 374236884Smm spa_feature_decr(spa, feature, tx); 375248571Smm spa_history_log_internal(spa, "zhack feature decr", tx, 376248571Smm "name=%s", feature->fi_guid); 377236884Smm} 378236884Smm 379236884Smmstatic void 380236884Smmzhack_do_feature_ref(int argc, char **argv) 381236884Smm{ 382236884Smm char c; 383236884Smm char *target; 384236884Smm boolean_t decr = B_FALSE; 385236884Smm spa_t *spa; 386236884Smm objset_t *mos; 387236884Smm zfeature_info_t feature; 388236884Smm zfeature_info_t *nodeps[] = { NULL }; 389236884Smm 390236884Smm /* 391236884Smm * fi_desc does not matter here because it was written to disk 392236884Smm * when the feature was enabled, but we need to properly set the 393236884Smm * feature for read or write based on the information we read off 394236884Smm * disk later. 395236884Smm */ 396236884Smm feature.fi_uname = "zhack"; 397236884Smm feature.fi_mos = B_FALSE; 398236884Smm feature.fi_desc = NULL; 399236884Smm feature.fi_depends = nodeps; 400236884Smm 401236884Smm optind = 1; 402236884Smm while ((c = getopt(argc, argv, "md")) != -1) { 403236884Smm switch (c) { 404236884Smm case 'm': 405236884Smm feature.fi_mos = B_TRUE; 406236884Smm break; 407236884Smm case 'd': 408236884Smm decr = B_TRUE; 409236884Smm break; 410236884Smm default: 411236884Smm usage(); 412236884Smm break; 413236884Smm } 414236884Smm } 415236884Smm argc -= optind; 416236884Smm argv += optind; 417236884Smm 418236884Smm if (argc < 2) { 419236884Smm (void) fprintf(stderr, "error: missing feature or pool name\n"); 420236884Smm usage(); 421236884Smm } 422236884Smm target = argv[0]; 423236884Smm feature.fi_guid = argv[1]; 424236884Smm 425236884Smm if (!zfeature_is_valid_guid(feature.fi_guid)) 426236884Smm fatal("invalid feature guid: %s", feature.fi_guid); 427236884Smm 428236884Smm zhack_spa_open(target, B_FALSE, FTAG, &spa); 429236884Smm mos = spa->spa_meta_objset; 430236884Smm 431236884Smm if (0 == zfeature_lookup_guid(feature.fi_guid, NULL)) 432236884Smm fatal("'%s' is a real feature, will not change refcount"); 433236884Smm 434236884Smm if (0 == zap_contains(mos, spa->spa_feat_for_read_obj, 435236884Smm feature.fi_guid)) { 436236884Smm feature.fi_can_readonly = B_FALSE; 437236884Smm } else if (0 == zap_contains(mos, spa->spa_feat_for_write_obj, 438236884Smm feature.fi_guid)) { 439236884Smm feature.fi_can_readonly = B_TRUE; 440236884Smm } else { 441236884Smm fatal("feature is not enabled: %s", feature.fi_guid); 442236884Smm } 443236884Smm 444236884Smm if (decr && !spa_feature_is_active(spa, &feature)) 445236884Smm fatal("feature refcount already 0: %s", feature.fi_guid); 446236884Smm 447248571Smm VERIFY0(dsl_sync_task(spa_name(spa), NULL, 448248571Smm decr ? feature_decr_sync : feature_incr_sync, &feature, 5)); 449236884Smm 450236884Smm spa_close(spa, FTAG); 451236884Smm} 452236884Smm 453236884Smmstatic int 454236884Smmzhack_do_feature(int argc, char **argv) 455236884Smm{ 456236884Smm char *subcommand; 457236884Smm 458236884Smm argc--; 459236884Smm argv++; 460236884Smm if (argc == 0) { 461236884Smm (void) fprintf(stderr, 462236884Smm "error: no feature operation specified\n"); 463236884Smm usage(); 464236884Smm } 465236884Smm 466236884Smm subcommand = argv[0]; 467236884Smm if (strcmp(subcommand, "stat") == 0) { 468236884Smm zhack_do_feature_stat(argc, argv); 469236884Smm } else if (strcmp(subcommand, "enable") == 0) { 470236884Smm zhack_do_feature_enable(argc, argv); 471236884Smm } else if (strcmp(subcommand, "ref") == 0) { 472236884Smm zhack_do_feature_ref(argc, argv); 473236884Smm } else { 474236884Smm (void) fprintf(stderr, "error: unknown subcommand: %s\n", 475236884Smm subcommand); 476236884Smm usage(); 477236884Smm } 478236884Smm 479236884Smm return (0); 480236884Smm} 481236884Smm 482236884Smm#define MAX_NUM_PATHS 1024 483236884Smm 484236884Smmint 485236884Smmmain(int argc, char **argv) 486236884Smm{ 487236884Smm extern void zfs_prop_init(void); 488236884Smm 489236884Smm char *path[MAX_NUM_PATHS]; 490236884Smm const char *subcommand; 491236884Smm int rv = 0; 492236884Smm char c; 493236884Smm 494236884Smm g_importargs.path = path; 495236884Smm 496236884Smm dprintf_setup(&argc, argv); 497236884Smm zfs_prop_init(); 498236884Smm 499236884Smm while ((c = getopt(argc, argv, "c:d:")) != -1) { 500236884Smm switch (c) { 501236884Smm case 'c': 502236884Smm g_importargs.cachefile = optarg; 503236884Smm break; 504236884Smm case 'd': 505236884Smm assert(g_importargs.paths < MAX_NUM_PATHS); 506236884Smm g_importargs.path[g_importargs.paths++] = optarg; 507236884Smm break; 508236884Smm default: 509236884Smm usage(); 510236884Smm break; 511236884Smm } 512236884Smm } 513236884Smm 514236884Smm argc -= optind; 515236884Smm argv += optind; 516236884Smm optind = 1; 517236884Smm 518236884Smm if (argc == 0) { 519236884Smm (void) fprintf(stderr, "error: no command specified\n"); 520236884Smm usage(); 521236884Smm } 522236884Smm 523236884Smm subcommand = argv[0]; 524236884Smm 525236884Smm if (strcmp(subcommand, "feature") == 0) { 526236884Smm rv = zhack_do_feature(argc, argv); 527236884Smm } else { 528236884Smm (void) fprintf(stderr, "error: unknown subcommand: %s\n", 529236884Smm subcommand); 530236884Smm usage(); 531236884Smm } 532236884Smm 533236884Smm if (!g_readonly && spa_export(g_pool, NULL, B_TRUE, B_TRUE) != 0) { 534236884Smm fatal("pool export failed; " 535236884Smm "changes may not be committed to disk\n"); 536236884Smm } 537236884Smm 538236884Smm libzfs_fini(g_zfs); 539236884Smm kernel_fini(); 540236884Smm 541236884Smm return (rv); 542236884Smm} 543