1185029Spjd/* 2185029Spjd * CDDL HEADER START 3185029Spjd * 4185029Spjd * The contents of this file are subject to the terms of the 5185029Spjd * Common Development and Distribution License (the "License"). 6185029Spjd * You may not use this file except in compliance with the License. 7185029Spjd * 8185029Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9185029Spjd * or http://www.opensolaris.org/os/licensing. 10185029Spjd * See the License for the specific language governing permissions 11185029Spjd * and limitations under the License. 12185029Spjd * 13185029Spjd * When distributing Covered Code, include this CDDL HEADER in each 14185029Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15185029Spjd * If applicable, add the following below this CDDL HEADER, with the 16185029Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17185029Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18185029Spjd * 19185029Spjd * CDDL HEADER END 20185029Spjd */ 21185029Spjd/* 22219089Spjd * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23249643Smm * Copyright (c) 2012 by Delphix. All rights reserved. 24185029Spjd */ 25185029Spjd 26185029Spjd/* 27185029Spjd * This file is intended for functions that ought to be common between user 28185029Spjd * land (libzfs) and the kernel. When many common routines need to be shared 29185029Spjd * then a separate file should to be created. 30185029Spjd */ 31185029Spjd 32185029Spjd#if defined(_KERNEL) 33185029Spjd#include <sys/systm.h> 34219089Spjd#else 35219089Spjd#include <string.h> 36185029Spjd#endif 37185029Spjd 38185029Spjd#include <sys/types.h> 39185029Spjd#include <sys/fs/zfs.h> 40185029Spjd#include <sys/nvpair.h> 41219089Spjd#include "zfs_comutil.h" 42185029Spjd 43185029Spjd/* 44185029Spjd * Are there allocatable vdevs? 45185029Spjd */ 46185029Spjdboolean_t 47185029Spjdzfs_allocatable_devs(nvlist_t *nv) 48185029Spjd{ 49185029Spjd uint64_t is_log; 50185029Spjd uint_t c; 51185029Spjd nvlist_t **child; 52185029Spjd uint_t children; 53185029Spjd 54185029Spjd if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 55185029Spjd &child, &children) != 0) { 56185029Spjd return (B_FALSE); 57185029Spjd } 58185029Spjd for (c = 0; c < children; c++) { 59185029Spjd is_log = 0; 60185029Spjd (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 61185029Spjd &is_log); 62185029Spjd if (!is_log) 63185029Spjd return (B_TRUE); 64185029Spjd } 65185029Spjd return (B_FALSE); 66185029Spjd} 67219089Spjd 68219089Spjdvoid 69219089Spjdzpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp) 70219089Spjd{ 71219089Spjd nvlist_t *policy; 72219089Spjd nvpair_t *elem; 73219089Spjd char *nm; 74219089Spjd 75219089Spjd /* Defaults */ 76219089Spjd zrpp->zrp_request = ZPOOL_NO_REWIND; 77219089Spjd zrpp->zrp_maxmeta = 0; 78219089Spjd zrpp->zrp_maxdata = UINT64_MAX; 79219089Spjd zrpp->zrp_txg = UINT64_MAX; 80219089Spjd 81219089Spjd if (nvl == NULL) 82219089Spjd return; 83219089Spjd 84219089Spjd elem = NULL; 85219089Spjd while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 86219089Spjd nm = nvpair_name(elem); 87219089Spjd if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) { 88219089Spjd if (nvpair_value_nvlist(elem, &policy) == 0) 89219089Spjd zpool_get_rewind_policy(policy, zrpp); 90219089Spjd return; 91219089Spjd } else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) { 92219089Spjd if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0) 93219089Spjd if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES) 94219089Spjd zrpp->zrp_request = ZPOOL_NO_REWIND; 95219089Spjd } else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) { 96219089Spjd (void) nvpair_value_uint64(elem, &zrpp->zrp_txg); 97219089Spjd } else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) { 98219089Spjd (void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta); 99219089Spjd } else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) { 100219089Spjd (void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata); 101219089Spjd } 102219089Spjd } 103219089Spjd if (zrpp->zrp_request == 0) 104219089Spjd zrpp->zrp_request = ZPOOL_NO_REWIND; 105219089Spjd} 106219089Spjd 107219089Spjdtypedef struct zfs_version_spa_map { 108219089Spjd int version_zpl; 109219089Spjd int version_spa; 110219089Spjd} zfs_version_spa_map_t; 111219089Spjd 112219089Spjd/* 113219089Spjd * Keep this table in monotonically increasing version number order. 114219089Spjd */ 115219089Spjdstatic zfs_version_spa_map_t zfs_version_table[] = { 116219089Spjd {ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL}, 117219089Spjd {ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL}, 118219089Spjd {ZPL_VERSION_FUID, SPA_VERSION_FUID}, 119219089Spjd {ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE}, 120219089Spjd {ZPL_VERSION_SA, SPA_VERSION_SA}, 121219089Spjd {0, 0} 122219089Spjd}; 123219089Spjd 124219089Spjd/* 125219089Spjd * Return the max zpl version for a corresponding spa version 126219089Spjd * -1 is returned if no mapping exists. 127219089Spjd */ 128219089Spjdint 129219089Spjdzfs_zpl_version_map(int spa_version) 130219089Spjd{ 131219089Spjd int i; 132219089Spjd int version = -1; 133219089Spjd 134219089Spjd for (i = 0; zfs_version_table[i].version_spa; i++) { 135219089Spjd if (spa_version >= zfs_version_table[i].version_spa) 136219089Spjd version = zfs_version_table[i].version_zpl; 137219089Spjd } 138219089Spjd 139219089Spjd return (version); 140219089Spjd} 141219089Spjd 142219089Spjd/* 143219089Spjd * Return the min spa version for a corresponding spa version 144219089Spjd * -1 is returned if no mapping exists. 145219089Spjd */ 146219089Spjdint 147219089Spjdzfs_spa_version_map(int zpl_version) 148219089Spjd{ 149219089Spjd int i; 150219089Spjd int version = -1; 151219089Spjd 152219089Spjd for (i = 0; zfs_version_table[i].version_zpl; i++) { 153219089Spjd if (zfs_version_table[i].version_zpl >= zpl_version) 154219089Spjd return (zfs_version_table[i].version_spa); 155219089Spjd } 156219089Spjd 157219089Spjd return (version); 158219089Spjd} 159219089Spjd 160249643Smm/* 161249643Smm * This is the table of legacy internal event names; it should not be modified. 162249643Smm * The internal events are now stored in the history log as strings. 163249643Smm */ 164249643Smmconst char *zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = { 165219089Spjd "invalid event", 166219089Spjd "pool create", 167219089Spjd "vdev add", 168219089Spjd "pool remove", 169219089Spjd "pool destroy", 170219089Spjd "pool export", 171219089Spjd "pool import", 172219089Spjd "vdev attach", 173219089Spjd "vdev replace", 174219089Spjd "vdev detach", 175219089Spjd "vdev online", 176219089Spjd "vdev offline", 177219089Spjd "vdev upgrade", 178219089Spjd "pool clear", 179219089Spjd "pool scrub", 180219089Spjd "pool property set", 181219089Spjd "create", 182219089Spjd "clone", 183219089Spjd "destroy", 184219089Spjd "destroy_begin_sync", 185219089Spjd "inherit", 186219089Spjd "property set", 187219089Spjd "quota set", 188219089Spjd "permission update", 189219089Spjd "permission remove", 190219089Spjd "permission who remove", 191219089Spjd "promote", 192219089Spjd "receive", 193219089Spjd "rename", 194219089Spjd "reservation set", 195219089Spjd "replay_inc_sync", 196219089Spjd "replay_full_sync", 197219089Spjd "rollback", 198219089Spjd "snapshot", 199219089Spjd "filesystem version upgrade", 200219089Spjd "refquota set", 201219089Spjd "refreservation set", 202219089Spjd "pool scrub done", 203219089Spjd "user hold", 204219089Spjd "user release", 205219089Spjd "pool split", 206219089Spjd}; 207