Print this page
Style cleanups, use nvlist_empty, variable renames, early variable initialisation.


   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) 2012 by Delphix. All rights reserved.

  24  */
  25 
  26 /*
  27  * zhack is a debugging tool that can write changes to ZFS pool using libzpool
  28  * for testing purposes. Altering pools with zhack is unsupported and may
  29  * result in corrupted pools.
  30  */
  31 
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <ctype.h>
  35 #include <sys/zfs_context.h>
  36 #include <sys/spa.h>
  37 #include <sys/spa_impl.h>
  38 #include <sys/dmu.h>
  39 #include <sys/zap.h>
  40 #include <sys/zfs_znode.h>
  41 #include <sys/dsl_synctask.h>
  42 #include <sys/vdev.h>
  43 #include <sys/fs/zfs.h>


 136         g_readonly = readonly;
 137 
 138         /*
 139          * If we only want readonly access, it's OK if we find
 140          * a potentially-active (ie, imported into the kernel) pool from the
 141          * default cachefile.
 142          */
 143         if (readonly && spa_open(target, &spa, FTAG) == 0) {
 144                 spa_close(spa, FTAG);
 145                 return;
 146         }
 147 
 148         g_importargs.unique = B_TRUE;
 149         g_importargs.can_be_active = readonly;
 150         g_pool = strdup(target);
 151         if ((sepp = strpbrk(g_pool, "/@")) != NULL)
 152                 *sepp = '\0';
 153         g_importargs.poolname = g_pool;
 154         pools = zpool_search_import(g_zfs, &g_importargs);
 155 
 156         if (pools == NULL || nvlist_next_nvpair(pools, NULL) == NULL) {
 157                 if (!g_importargs.can_be_active) {
 158                         g_importargs.can_be_active = B_TRUE;
 159                         if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
 160                             spa_open(target, &spa, FTAG) == 0) {
 161                                 fatal("cannot import '%s': pool is active; run "
 162                                     "\"zpool export %s\" first\n",
 163                                     g_pool, g_pool);
 164                         }
 165                 }
 166 
 167                 fatal("cannot import '%s': no such pool available\n", g_pool);
 168         }
 169 
 170         elem = nvlist_next_nvpair(pools, NULL);
 171         name = nvpair_name(elem);
 172         verify(nvpair_value_nvlist(elem, &config) == 0);
 173 
 174         props = NULL;
 175         if (readonly) {
 176                 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);




   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) 2012 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>


 137         g_readonly = readonly;
 138 
 139         /*
 140          * If we only want readonly access, it's OK if we find
 141          * a potentially-active (ie, imported into the kernel) pool from the
 142          * default cachefile.
 143          */
 144         if (readonly && spa_open(target, &spa, FTAG) == 0) {
 145                 spa_close(spa, FTAG);
 146                 return;
 147         }
 148 
 149         g_importargs.unique = B_TRUE;
 150         g_importargs.can_be_active = readonly;
 151         g_pool = strdup(target);
 152         if ((sepp = strpbrk(g_pool, "/@")) != NULL)
 153                 *sepp = '\0';
 154         g_importargs.poolname = g_pool;
 155         pools = zpool_search_import(g_zfs, &g_importargs);
 156 
 157         if (nvlist_empty(pools)) {
 158                 if (!g_importargs.can_be_active) {
 159                         g_importargs.can_be_active = B_TRUE;
 160                         if (zpool_search_import(g_zfs, &g_importargs) != NULL ||
 161                             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);
 165                         }
 166                 }
 167 
 168                 fatal("cannot import '%s': no such pool available\n", g_pool);
 169         }
 170 
 171         elem = nvlist_next_nvpair(pools, NULL);
 172         name = nvpair_name(elem);
 173         verify(nvpair_value_nvlist(elem, &config) == 0);
 174 
 175         props = NULL;
 176         if (readonly) {
 177                 verify(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);