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  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012 by Delphix. All rights reserved.
  24  */
  25 
  26 /*
  27  * This file is intended for functions that ought to be common between user
  28  * land (libzfs) and the kernel. When many common routines need to be shared
  29  * then a separate file should to be created.
  30  */
  31 
  32 #if defined(_KERNEL)
  33 #include <sys/systm.h>
  34 #else
  35 #include <string.h>
  36 #endif
  37 
  38 #include <sys/types.h>
  39 #include <sys/fs/zfs.h>
  40 #include <sys/int_limits.h>
  41 #include <sys/nvpair.h>
  42 #include "zfs_comutil.h"
  43 
  44 /*
  45  * Are there allocatable vdevs?
  46  * Any pool must have at least one normal (metaslab) class vdev, which means:
  47  * at least one that is simultaneously non-slog and non-special
  48  */
  49 boolean_t
  50 zfs_allocatable_devs(nvlist_t *nv)
  51 {
  52         uint64_t is_log, is_special;
  53         uint_t c;
  54         nvlist_t **child;
  55         uint_t children;
  56 
  57         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
  58             &child, &children) != 0) {
  59                 return (B_FALSE);
  60         }
  61         for (c = 0; c < children; c++) {
  62                 is_log = is_special = 0;
  63                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
  64                     &is_log);
  65                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_SPECIAL,
  66                     &is_special);
  67                 if (!is_log && !is_special)
  68                         return (B_TRUE);
  69         }
  70         return (B_FALSE);
  71 }
  72 
  73 void
  74 zpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp)
  75 {
  76         nvlist_t *policy;
  77         nvpair_t *elem;
  78         char *nm;
  79 
  80         /* Defaults */
  81         zrpp->zrp_request = ZPOOL_NO_REWIND;
  82         zrpp->zrp_maxmeta = 0;
  83         zrpp->zrp_maxdata = UINT64_MAX;
  84         zrpp->zrp_txg = UINT64_MAX;
  85 
  86         if (nvl == NULL)
  87                 return;
  88 
  89         elem = NULL;
  90         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
  91                 nm = nvpair_name(elem);
  92                 if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) {
  93                         if (nvpair_value_nvlist(elem, &policy) == 0)
  94                                 zpool_get_rewind_policy(policy, zrpp);
  95                         return;
  96                 } else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) {
  97                         if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
  98                                 if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES)
  99                                         zrpp->zrp_request = ZPOOL_NO_REWIND;
 100                 } else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) {
 101                         (void) nvpair_value_uint64(elem, &zrpp->zrp_txg);
 102                 } else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) {
 103                         (void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
 104                 } else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
 105                         (void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
 106                 }
 107         }
 108         if (zrpp->zrp_request == 0)
 109                 zrpp->zrp_request = ZPOOL_NO_REWIND;
 110 }
 111 
 112 typedef struct zfs_version_spa_map {
 113         int     version_zpl;
 114         int     version_spa;
 115 } zfs_version_spa_map_t;
 116 
 117 /*
 118  * Keep this table in monotonically increasing version number order.
 119  */
 120 static zfs_version_spa_map_t zfs_version_table[] = {
 121         {ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
 122         {ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
 123         {ZPL_VERSION_FUID, SPA_VERSION_FUID},
 124         {ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
 125         {ZPL_VERSION_SA, SPA_VERSION_SA},
 126         {0, 0}
 127 };
 128 
 129 /*
 130  * Return the max zpl version for a corresponding spa version
 131  * -1 is returned if no mapping exists.
 132  */
 133 int
 134 zfs_zpl_version_map(int spa_version)
 135 {
 136         int i;
 137         int version = -1;
 138 
 139         for (i = 0; zfs_version_table[i].version_spa; i++) {
 140                 if (spa_version >= zfs_version_table[i].version_spa)
 141                         version = zfs_version_table[i].version_zpl;
 142         }
 143 
 144         return (version);
 145 }
 146 
 147 /*
 148  * Return the min spa version for a corresponding spa version
 149  * -1 is returned if no mapping exists.
 150  */
 151 int
 152 zfs_spa_version_map(int zpl_version)
 153 {
 154         int i;
 155         int version = -1;
 156 
 157         for (i = 0; zfs_version_table[i].version_zpl; i++) {
 158                 if (zfs_version_table[i].version_zpl >= zpl_version)
 159                         return (zfs_version_table[i].version_spa);
 160         }
 161 
 162         return (version);
 163 }
 164 
 165 /*
 166  * This is the table of legacy internal event names; it should not be modified.
 167  * The internal events are now stored in the history log as strings.
 168  */
 169 const char *zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = {
 170         "invalid event",
 171         "pool create",
 172         "vdev add",
 173         "pool remove",
 174         "pool destroy",
 175         "pool export",
 176         "pool import",
 177         "vdev attach",
 178         "vdev replace",
 179         "vdev detach",
 180         "vdev online",
 181         "vdev offline",
 182         "vdev upgrade",
 183         "pool clear",
 184         "pool scrub",
 185         "pool property set",
 186         "create",
 187         "clone",
 188         "destroy",
 189         "destroy_begin_sync",
 190         "inherit",
 191         "property set",
 192         "quota set",
 193         "permission update",
 194         "permission remove",
 195         "permission who remove",
 196         "promote",
 197         "receive",
 198         "rename",
 199         "reservation set",
 200         "replay_inc_sync",
 201         "replay_full_sync",
 202         "rollback",
 203         "snapshot",
 204         "filesystem version upgrade",
 205         "refquota set",
 206         "refreservation set",
 207         "pool scrub done",
 208         "user hold",
 209         "user release",
 210         "pool split",
 211 };