1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy 22*eda14cbcSMatt Macy /* 23*eda14cbcSMatt Macy * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24*eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25*eda14cbcSMatt Macy * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 26*eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27*eda14cbcSMatt Macy * Copyright (c) 2018 Datto Inc. 28*eda14cbcSMatt Macy * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. 29*eda14cbcSMatt Macy * Copyright (c) 2017, Intel Corporation. 30*eda14cbcSMatt Macy * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com> 31*eda14cbcSMatt Macy */ 32*eda14cbcSMatt Macy 33*eda14cbcSMatt Macy #include <errno.h> 34*eda14cbcSMatt Macy #include <libintl.h> 35*eda14cbcSMatt Macy #include <stdio.h> 36*eda14cbcSMatt Macy #include <stdlib.h> 37*eda14cbcSMatt Macy #include <strings.h> 38*eda14cbcSMatt Macy #include <unistd.h> 39*eda14cbcSMatt Macy #include <libgen.h> 40*eda14cbcSMatt Macy #include <zone.h> 41*eda14cbcSMatt Macy #include <sys/stat.h> 42*eda14cbcSMatt Macy #include <sys/efi_partition.h> 43*eda14cbcSMatt Macy #include <sys/systeminfo.h> 44*eda14cbcSMatt Macy #include <sys/zfs_ioctl.h> 45*eda14cbcSMatt Macy #include <sys/vdev_disk.h> 46*eda14cbcSMatt Macy #include <dlfcn.h> 47*eda14cbcSMatt Macy #include <libzutil.h> 48*eda14cbcSMatt Macy 49*eda14cbcSMatt Macy #include "zfs_namecheck.h" 50*eda14cbcSMatt Macy #include "zfs_prop.h" 51*eda14cbcSMatt Macy #include "libzfs_impl.h" 52*eda14cbcSMatt Macy #include "zfs_comutil.h" 53*eda14cbcSMatt Macy #include "zfeature_common.h" 54*eda14cbcSMatt Macy 55*eda14cbcSMatt Macy static boolean_t zpool_vdev_is_interior(const char *name); 56*eda14cbcSMatt Macy 57*eda14cbcSMatt Macy typedef struct prop_flags { 58*eda14cbcSMatt Macy int create:1; /* Validate property on creation */ 59*eda14cbcSMatt Macy int import:1; /* Validate property on import */ 60*eda14cbcSMatt Macy } prop_flags_t; 61*eda14cbcSMatt Macy 62*eda14cbcSMatt Macy /* 63*eda14cbcSMatt Macy * ==================================================================== 64*eda14cbcSMatt Macy * zpool property functions 65*eda14cbcSMatt Macy * ==================================================================== 66*eda14cbcSMatt Macy */ 67*eda14cbcSMatt Macy 68*eda14cbcSMatt Macy static int 69*eda14cbcSMatt Macy zpool_get_all_props(zpool_handle_t *zhp) 70*eda14cbcSMatt Macy { 71*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 72*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 73*eda14cbcSMatt Macy 74*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 75*eda14cbcSMatt Macy 76*eda14cbcSMatt Macy if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 77*eda14cbcSMatt Macy return (-1); 78*eda14cbcSMatt Macy 79*eda14cbcSMatt Macy while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 80*eda14cbcSMatt Macy if (errno == ENOMEM) { 81*eda14cbcSMatt Macy if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 82*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 83*eda14cbcSMatt Macy return (-1); 84*eda14cbcSMatt Macy } 85*eda14cbcSMatt Macy } else { 86*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 87*eda14cbcSMatt Macy return (-1); 88*eda14cbcSMatt Macy } 89*eda14cbcSMatt Macy } 90*eda14cbcSMatt Macy 91*eda14cbcSMatt Macy if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 92*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 93*eda14cbcSMatt Macy return (-1); 94*eda14cbcSMatt Macy } 95*eda14cbcSMatt Macy 96*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 97*eda14cbcSMatt Macy 98*eda14cbcSMatt Macy return (0); 99*eda14cbcSMatt Macy } 100*eda14cbcSMatt Macy 101*eda14cbcSMatt Macy int 102*eda14cbcSMatt Macy zpool_props_refresh(zpool_handle_t *zhp) 103*eda14cbcSMatt Macy { 104*eda14cbcSMatt Macy nvlist_t *old_props; 105*eda14cbcSMatt Macy 106*eda14cbcSMatt Macy old_props = zhp->zpool_props; 107*eda14cbcSMatt Macy 108*eda14cbcSMatt Macy if (zpool_get_all_props(zhp) != 0) 109*eda14cbcSMatt Macy return (-1); 110*eda14cbcSMatt Macy 111*eda14cbcSMatt Macy nvlist_free(old_props); 112*eda14cbcSMatt Macy return (0); 113*eda14cbcSMatt Macy } 114*eda14cbcSMatt Macy 115*eda14cbcSMatt Macy static const char * 116*eda14cbcSMatt Macy zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 117*eda14cbcSMatt Macy zprop_source_t *src) 118*eda14cbcSMatt Macy { 119*eda14cbcSMatt Macy nvlist_t *nv, *nvl; 120*eda14cbcSMatt Macy uint64_t ival; 121*eda14cbcSMatt Macy char *value; 122*eda14cbcSMatt Macy zprop_source_t source; 123*eda14cbcSMatt Macy 124*eda14cbcSMatt Macy nvl = zhp->zpool_props; 125*eda14cbcSMatt Macy if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 126*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 127*eda14cbcSMatt Macy source = ival; 128*eda14cbcSMatt Macy verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 129*eda14cbcSMatt Macy } else { 130*eda14cbcSMatt Macy source = ZPROP_SRC_DEFAULT; 131*eda14cbcSMatt Macy if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 132*eda14cbcSMatt Macy value = "-"; 133*eda14cbcSMatt Macy } 134*eda14cbcSMatt Macy 135*eda14cbcSMatt Macy if (src) 136*eda14cbcSMatt Macy *src = source; 137*eda14cbcSMatt Macy 138*eda14cbcSMatt Macy return (value); 139*eda14cbcSMatt Macy } 140*eda14cbcSMatt Macy 141*eda14cbcSMatt Macy uint64_t 142*eda14cbcSMatt Macy zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 143*eda14cbcSMatt Macy { 144*eda14cbcSMatt Macy nvlist_t *nv, *nvl; 145*eda14cbcSMatt Macy uint64_t value; 146*eda14cbcSMatt Macy zprop_source_t source; 147*eda14cbcSMatt Macy 148*eda14cbcSMatt Macy if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 149*eda14cbcSMatt Macy /* 150*eda14cbcSMatt Macy * zpool_get_all_props() has most likely failed because 151*eda14cbcSMatt Macy * the pool is faulted, but if all we need is the top level 152*eda14cbcSMatt Macy * vdev's guid then get it from the zhp config nvlist. 153*eda14cbcSMatt Macy */ 154*eda14cbcSMatt Macy if ((prop == ZPOOL_PROP_GUID) && 155*eda14cbcSMatt Macy (nvlist_lookup_nvlist(zhp->zpool_config, 156*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 157*eda14cbcSMatt Macy (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 158*eda14cbcSMatt Macy == 0)) { 159*eda14cbcSMatt Macy return (value); 160*eda14cbcSMatt Macy } 161*eda14cbcSMatt Macy return (zpool_prop_default_numeric(prop)); 162*eda14cbcSMatt Macy } 163*eda14cbcSMatt Macy 164*eda14cbcSMatt Macy nvl = zhp->zpool_props; 165*eda14cbcSMatt Macy if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 166*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 167*eda14cbcSMatt Macy source = value; 168*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 169*eda14cbcSMatt Macy } else { 170*eda14cbcSMatt Macy source = ZPROP_SRC_DEFAULT; 171*eda14cbcSMatt Macy value = zpool_prop_default_numeric(prop); 172*eda14cbcSMatt Macy } 173*eda14cbcSMatt Macy 174*eda14cbcSMatt Macy if (src) 175*eda14cbcSMatt Macy *src = source; 176*eda14cbcSMatt Macy 177*eda14cbcSMatt Macy return (value); 178*eda14cbcSMatt Macy } 179*eda14cbcSMatt Macy 180*eda14cbcSMatt Macy /* 181*eda14cbcSMatt Macy * Map VDEV STATE to printed strings. 182*eda14cbcSMatt Macy */ 183*eda14cbcSMatt Macy const char * 184*eda14cbcSMatt Macy zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 185*eda14cbcSMatt Macy { 186*eda14cbcSMatt Macy switch (state) { 187*eda14cbcSMatt Macy case VDEV_STATE_CLOSED: 188*eda14cbcSMatt Macy case VDEV_STATE_OFFLINE: 189*eda14cbcSMatt Macy return (gettext("OFFLINE")); 190*eda14cbcSMatt Macy case VDEV_STATE_REMOVED: 191*eda14cbcSMatt Macy return (gettext("REMOVED")); 192*eda14cbcSMatt Macy case VDEV_STATE_CANT_OPEN: 193*eda14cbcSMatt Macy if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 194*eda14cbcSMatt Macy return (gettext("FAULTED")); 195*eda14cbcSMatt Macy else if (aux == VDEV_AUX_SPLIT_POOL) 196*eda14cbcSMatt Macy return (gettext("SPLIT")); 197*eda14cbcSMatt Macy else 198*eda14cbcSMatt Macy return (gettext("UNAVAIL")); 199*eda14cbcSMatt Macy case VDEV_STATE_FAULTED: 200*eda14cbcSMatt Macy return (gettext("FAULTED")); 201*eda14cbcSMatt Macy case VDEV_STATE_DEGRADED: 202*eda14cbcSMatt Macy return (gettext("DEGRADED")); 203*eda14cbcSMatt Macy case VDEV_STATE_HEALTHY: 204*eda14cbcSMatt Macy return (gettext("ONLINE")); 205*eda14cbcSMatt Macy 206*eda14cbcSMatt Macy default: 207*eda14cbcSMatt Macy break; 208*eda14cbcSMatt Macy } 209*eda14cbcSMatt Macy 210*eda14cbcSMatt Macy return (gettext("UNKNOWN")); 211*eda14cbcSMatt Macy } 212*eda14cbcSMatt Macy 213*eda14cbcSMatt Macy /* 214*eda14cbcSMatt Macy * Map POOL STATE to printed strings. 215*eda14cbcSMatt Macy */ 216*eda14cbcSMatt Macy const char * 217*eda14cbcSMatt Macy zpool_pool_state_to_name(pool_state_t state) 218*eda14cbcSMatt Macy { 219*eda14cbcSMatt Macy switch (state) { 220*eda14cbcSMatt Macy default: 221*eda14cbcSMatt Macy break; 222*eda14cbcSMatt Macy case POOL_STATE_ACTIVE: 223*eda14cbcSMatt Macy return (gettext("ACTIVE")); 224*eda14cbcSMatt Macy case POOL_STATE_EXPORTED: 225*eda14cbcSMatt Macy return (gettext("EXPORTED")); 226*eda14cbcSMatt Macy case POOL_STATE_DESTROYED: 227*eda14cbcSMatt Macy return (gettext("DESTROYED")); 228*eda14cbcSMatt Macy case POOL_STATE_SPARE: 229*eda14cbcSMatt Macy return (gettext("SPARE")); 230*eda14cbcSMatt Macy case POOL_STATE_L2CACHE: 231*eda14cbcSMatt Macy return (gettext("L2CACHE")); 232*eda14cbcSMatt Macy case POOL_STATE_UNINITIALIZED: 233*eda14cbcSMatt Macy return (gettext("UNINITIALIZED")); 234*eda14cbcSMatt Macy case POOL_STATE_UNAVAIL: 235*eda14cbcSMatt Macy return (gettext("UNAVAIL")); 236*eda14cbcSMatt Macy case POOL_STATE_POTENTIALLY_ACTIVE: 237*eda14cbcSMatt Macy return (gettext("POTENTIALLY_ACTIVE")); 238*eda14cbcSMatt Macy } 239*eda14cbcSMatt Macy 240*eda14cbcSMatt Macy return (gettext("UNKNOWN")); 241*eda14cbcSMatt Macy } 242*eda14cbcSMatt Macy 243*eda14cbcSMatt Macy /* 244*eda14cbcSMatt Macy * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED", 245*eda14cbcSMatt Macy * "SUSPENDED", etc). 246*eda14cbcSMatt Macy */ 247*eda14cbcSMatt Macy const char * 248*eda14cbcSMatt Macy zpool_get_state_str(zpool_handle_t *zhp) 249*eda14cbcSMatt Macy { 250*eda14cbcSMatt Macy zpool_errata_t errata; 251*eda14cbcSMatt Macy zpool_status_t status; 252*eda14cbcSMatt Macy nvlist_t *nvroot; 253*eda14cbcSMatt Macy vdev_stat_t *vs; 254*eda14cbcSMatt Macy uint_t vsc; 255*eda14cbcSMatt Macy const char *str; 256*eda14cbcSMatt Macy 257*eda14cbcSMatt Macy status = zpool_get_status(zhp, NULL, &errata); 258*eda14cbcSMatt Macy 259*eda14cbcSMatt Macy if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 260*eda14cbcSMatt Macy str = gettext("FAULTED"); 261*eda14cbcSMatt Macy } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT || 262*eda14cbcSMatt Macy status == ZPOOL_STATUS_IO_FAILURE_MMP) { 263*eda14cbcSMatt Macy str = gettext("SUSPENDED"); 264*eda14cbcSMatt Macy } else { 265*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 266*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 267*eda14cbcSMatt Macy verify(nvlist_lookup_uint64_array(nvroot, 268*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 269*eda14cbcSMatt Macy == 0); 270*eda14cbcSMatt Macy str = zpool_state_to_name(vs->vs_state, vs->vs_aux); 271*eda14cbcSMatt Macy } 272*eda14cbcSMatt Macy return (str); 273*eda14cbcSMatt Macy } 274*eda14cbcSMatt Macy 275*eda14cbcSMatt Macy /* 276*eda14cbcSMatt Macy * Get a zpool property value for 'prop' and return the value in 277*eda14cbcSMatt Macy * a pre-allocated buffer. 278*eda14cbcSMatt Macy */ 279*eda14cbcSMatt Macy int 280*eda14cbcSMatt Macy zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, 281*eda14cbcSMatt Macy size_t len, zprop_source_t *srctype, boolean_t literal) 282*eda14cbcSMatt Macy { 283*eda14cbcSMatt Macy uint64_t intval; 284*eda14cbcSMatt Macy const char *strval; 285*eda14cbcSMatt Macy zprop_source_t src = ZPROP_SRC_NONE; 286*eda14cbcSMatt Macy 287*eda14cbcSMatt Macy if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 288*eda14cbcSMatt Macy switch (prop) { 289*eda14cbcSMatt Macy case ZPOOL_PROP_NAME: 290*eda14cbcSMatt Macy (void) strlcpy(buf, zpool_get_name(zhp), len); 291*eda14cbcSMatt Macy break; 292*eda14cbcSMatt Macy 293*eda14cbcSMatt Macy case ZPOOL_PROP_HEALTH: 294*eda14cbcSMatt Macy (void) strlcpy(buf, zpool_get_state_str(zhp), len); 295*eda14cbcSMatt Macy break; 296*eda14cbcSMatt Macy 297*eda14cbcSMatt Macy case ZPOOL_PROP_GUID: 298*eda14cbcSMatt Macy intval = zpool_get_prop_int(zhp, prop, &src); 299*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", (u_longlong_t)intval); 300*eda14cbcSMatt Macy break; 301*eda14cbcSMatt Macy 302*eda14cbcSMatt Macy case ZPOOL_PROP_ALTROOT: 303*eda14cbcSMatt Macy case ZPOOL_PROP_CACHEFILE: 304*eda14cbcSMatt Macy case ZPOOL_PROP_COMMENT: 305*eda14cbcSMatt Macy if (zhp->zpool_props != NULL || 306*eda14cbcSMatt Macy zpool_get_all_props(zhp) == 0) { 307*eda14cbcSMatt Macy (void) strlcpy(buf, 308*eda14cbcSMatt Macy zpool_get_prop_string(zhp, prop, &src), 309*eda14cbcSMatt Macy len); 310*eda14cbcSMatt Macy break; 311*eda14cbcSMatt Macy } 312*eda14cbcSMatt Macy /* FALLTHROUGH */ 313*eda14cbcSMatt Macy default: 314*eda14cbcSMatt Macy (void) strlcpy(buf, "-", len); 315*eda14cbcSMatt Macy break; 316*eda14cbcSMatt Macy } 317*eda14cbcSMatt Macy 318*eda14cbcSMatt Macy if (srctype != NULL) 319*eda14cbcSMatt Macy *srctype = src; 320*eda14cbcSMatt Macy return (0); 321*eda14cbcSMatt Macy } 322*eda14cbcSMatt Macy 323*eda14cbcSMatt Macy if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 324*eda14cbcSMatt Macy prop != ZPOOL_PROP_NAME) 325*eda14cbcSMatt Macy return (-1); 326*eda14cbcSMatt Macy 327*eda14cbcSMatt Macy switch (zpool_prop_get_type(prop)) { 328*eda14cbcSMatt Macy case PROP_TYPE_STRING: 329*eda14cbcSMatt Macy (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 330*eda14cbcSMatt Macy len); 331*eda14cbcSMatt Macy break; 332*eda14cbcSMatt Macy 333*eda14cbcSMatt Macy case PROP_TYPE_NUMBER: 334*eda14cbcSMatt Macy intval = zpool_get_prop_int(zhp, prop, &src); 335*eda14cbcSMatt Macy 336*eda14cbcSMatt Macy switch (prop) { 337*eda14cbcSMatt Macy case ZPOOL_PROP_SIZE: 338*eda14cbcSMatt Macy case ZPOOL_PROP_ALLOCATED: 339*eda14cbcSMatt Macy case ZPOOL_PROP_FREE: 340*eda14cbcSMatt Macy case ZPOOL_PROP_FREEING: 341*eda14cbcSMatt Macy case ZPOOL_PROP_LEAKED: 342*eda14cbcSMatt Macy case ZPOOL_PROP_ASHIFT: 343*eda14cbcSMatt Macy if (literal) 344*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", 345*eda14cbcSMatt Macy (u_longlong_t)intval); 346*eda14cbcSMatt Macy else 347*eda14cbcSMatt Macy (void) zfs_nicenum(intval, buf, len); 348*eda14cbcSMatt Macy break; 349*eda14cbcSMatt Macy 350*eda14cbcSMatt Macy case ZPOOL_PROP_EXPANDSZ: 351*eda14cbcSMatt Macy case ZPOOL_PROP_CHECKPOINT: 352*eda14cbcSMatt Macy if (intval == 0) { 353*eda14cbcSMatt Macy (void) strlcpy(buf, "-", len); 354*eda14cbcSMatt Macy } else if (literal) { 355*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", 356*eda14cbcSMatt Macy (u_longlong_t)intval); 357*eda14cbcSMatt Macy } else { 358*eda14cbcSMatt Macy (void) zfs_nicebytes(intval, buf, len); 359*eda14cbcSMatt Macy } 360*eda14cbcSMatt Macy break; 361*eda14cbcSMatt Macy 362*eda14cbcSMatt Macy case ZPOOL_PROP_CAPACITY: 363*eda14cbcSMatt Macy if (literal) { 364*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", 365*eda14cbcSMatt Macy (u_longlong_t)intval); 366*eda14cbcSMatt Macy } else { 367*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu%%", 368*eda14cbcSMatt Macy (u_longlong_t)intval); 369*eda14cbcSMatt Macy } 370*eda14cbcSMatt Macy break; 371*eda14cbcSMatt Macy 372*eda14cbcSMatt Macy case ZPOOL_PROP_FRAGMENTATION: 373*eda14cbcSMatt Macy if (intval == UINT64_MAX) { 374*eda14cbcSMatt Macy (void) strlcpy(buf, "-", len); 375*eda14cbcSMatt Macy } else if (literal) { 376*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", 377*eda14cbcSMatt Macy (u_longlong_t)intval); 378*eda14cbcSMatt Macy } else { 379*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu%%", 380*eda14cbcSMatt Macy (u_longlong_t)intval); 381*eda14cbcSMatt Macy } 382*eda14cbcSMatt Macy break; 383*eda14cbcSMatt Macy 384*eda14cbcSMatt Macy case ZPOOL_PROP_DEDUPRATIO: 385*eda14cbcSMatt Macy if (literal) 386*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu.%02llu", 387*eda14cbcSMatt Macy (u_longlong_t)(intval / 100), 388*eda14cbcSMatt Macy (u_longlong_t)(intval % 100)); 389*eda14cbcSMatt Macy else 390*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu.%02llux", 391*eda14cbcSMatt Macy (u_longlong_t)(intval / 100), 392*eda14cbcSMatt Macy (u_longlong_t)(intval % 100)); 393*eda14cbcSMatt Macy break; 394*eda14cbcSMatt Macy 395*eda14cbcSMatt Macy case ZPOOL_PROP_HEALTH: 396*eda14cbcSMatt Macy (void) strlcpy(buf, zpool_get_state_str(zhp), len); 397*eda14cbcSMatt Macy break; 398*eda14cbcSMatt Macy case ZPOOL_PROP_VERSION: 399*eda14cbcSMatt Macy if (intval >= SPA_VERSION_FEATURES) { 400*eda14cbcSMatt Macy (void) snprintf(buf, len, "-"); 401*eda14cbcSMatt Macy break; 402*eda14cbcSMatt Macy } 403*eda14cbcSMatt Macy /* FALLTHROUGH */ 404*eda14cbcSMatt Macy default: 405*eda14cbcSMatt Macy (void) snprintf(buf, len, "%llu", (u_longlong_t)intval); 406*eda14cbcSMatt Macy } 407*eda14cbcSMatt Macy break; 408*eda14cbcSMatt Macy 409*eda14cbcSMatt Macy case PROP_TYPE_INDEX: 410*eda14cbcSMatt Macy intval = zpool_get_prop_int(zhp, prop, &src); 411*eda14cbcSMatt Macy if (zpool_prop_index_to_string(prop, intval, &strval) 412*eda14cbcSMatt Macy != 0) 413*eda14cbcSMatt Macy return (-1); 414*eda14cbcSMatt Macy (void) strlcpy(buf, strval, len); 415*eda14cbcSMatt Macy break; 416*eda14cbcSMatt Macy 417*eda14cbcSMatt Macy default: 418*eda14cbcSMatt Macy abort(); 419*eda14cbcSMatt Macy } 420*eda14cbcSMatt Macy 421*eda14cbcSMatt Macy if (srctype) 422*eda14cbcSMatt Macy *srctype = src; 423*eda14cbcSMatt Macy 424*eda14cbcSMatt Macy return (0); 425*eda14cbcSMatt Macy } 426*eda14cbcSMatt Macy 427*eda14cbcSMatt Macy /* 428*eda14cbcSMatt Macy * Check if the bootfs name has the same pool name as it is set to. 429*eda14cbcSMatt Macy * Assuming bootfs is a valid dataset name. 430*eda14cbcSMatt Macy */ 431*eda14cbcSMatt Macy static boolean_t 432*eda14cbcSMatt Macy bootfs_name_valid(const char *pool, const char *bootfs) 433*eda14cbcSMatt Macy { 434*eda14cbcSMatt Macy int len = strlen(pool); 435*eda14cbcSMatt Macy if (bootfs[0] == '\0') 436*eda14cbcSMatt Macy return (B_TRUE); 437*eda14cbcSMatt Macy 438*eda14cbcSMatt Macy if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 439*eda14cbcSMatt Macy return (B_FALSE); 440*eda14cbcSMatt Macy 441*eda14cbcSMatt Macy if (strncmp(pool, bootfs, len) == 0 && 442*eda14cbcSMatt Macy (bootfs[len] == '/' || bootfs[len] == '\0')) 443*eda14cbcSMatt Macy return (B_TRUE); 444*eda14cbcSMatt Macy 445*eda14cbcSMatt Macy return (B_FALSE); 446*eda14cbcSMatt Macy } 447*eda14cbcSMatt Macy 448*eda14cbcSMatt Macy /* 449*eda14cbcSMatt Macy * Given an nvlist of zpool properties to be set, validate that they are 450*eda14cbcSMatt Macy * correct, and parse any numeric properties (index, boolean, etc) if they are 451*eda14cbcSMatt Macy * specified as strings. 452*eda14cbcSMatt Macy */ 453*eda14cbcSMatt Macy static nvlist_t * 454*eda14cbcSMatt Macy zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 455*eda14cbcSMatt Macy nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 456*eda14cbcSMatt Macy { 457*eda14cbcSMatt Macy nvpair_t *elem; 458*eda14cbcSMatt Macy nvlist_t *retprops; 459*eda14cbcSMatt Macy zpool_prop_t prop; 460*eda14cbcSMatt Macy char *strval; 461*eda14cbcSMatt Macy uint64_t intval; 462*eda14cbcSMatt Macy char *slash, *check; 463*eda14cbcSMatt Macy struct stat64 statbuf; 464*eda14cbcSMatt Macy zpool_handle_t *zhp; 465*eda14cbcSMatt Macy 466*eda14cbcSMatt Macy if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 467*eda14cbcSMatt Macy (void) no_memory(hdl); 468*eda14cbcSMatt Macy return (NULL); 469*eda14cbcSMatt Macy } 470*eda14cbcSMatt Macy 471*eda14cbcSMatt Macy elem = NULL; 472*eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 473*eda14cbcSMatt Macy const char *propname = nvpair_name(elem); 474*eda14cbcSMatt Macy 475*eda14cbcSMatt Macy prop = zpool_name_to_prop(propname); 476*eda14cbcSMatt Macy if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) { 477*eda14cbcSMatt Macy int err; 478*eda14cbcSMatt Macy char *fname = strchr(propname, '@') + 1; 479*eda14cbcSMatt Macy 480*eda14cbcSMatt Macy err = zfeature_lookup_name(fname, NULL); 481*eda14cbcSMatt Macy if (err != 0) { 482*eda14cbcSMatt Macy ASSERT3U(err, ==, ENOENT); 483*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 484*eda14cbcSMatt Macy "invalid feature '%s'"), fname); 485*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 486*eda14cbcSMatt Macy goto error; 487*eda14cbcSMatt Macy } 488*eda14cbcSMatt Macy 489*eda14cbcSMatt Macy if (nvpair_type(elem) != DATA_TYPE_STRING) { 490*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 491*eda14cbcSMatt Macy "'%s' must be a string"), propname); 492*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 493*eda14cbcSMatt Macy goto error; 494*eda14cbcSMatt Macy } 495*eda14cbcSMatt Macy 496*eda14cbcSMatt Macy (void) nvpair_value_string(elem, &strval); 497*eda14cbcSMatt Macy if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 && 498*eda14cbcSMatt Macy strcmp(strval, ZFS_FEATURE_DISABLED) != 0) { 499*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 500*eda14cbcSMatt Macy "property '%s' can only be set to " 501*eda14cbcSMatt Macy "'enabled' or 'disabled'"), propname); 502*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 503*eda14cbcSMatt Macy goto error; 504*eda14cbcSMatt Macy } 505*eda14cbcSMatt Macy 506*eda14cbcSMatt Macy if (!flags.create && 507*eda14cbcSMatt Macy strcmp(strval, ZFS_FEATURE_DISABLED) == 0) { 508*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 509*eda14cbcSMatt Macy "property '%s' can only be set to " 510*eda14cbcSMatt Macy "'disabled' at creation time"), propname); 511*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 512*eda14cbcSMatt Macy goto error; 513*eda14cbcSMatt Macy } 514*eda14cbcSMatt Macy 515*eda14cbcSMatt Macy if (nvlist_add_uint64(retprops, propname, 0) != 0) { 516*eda14cbcSMatt Macy (void) no_memory(hdl); 517*eda14cbcSMatt Macy goto error; 518*eda14cbcSMatt Macy } 519*eda14cbcSMatt Macy continue; 520*eda14cbcSMatt Macy } 521*eda14cbcSMatt Macy 522*eda14cbcSMatt Macy /* 523*eda14cbcSMatt Macy * Make sure this property is valid and applies to this type. 524*eda14cbcSMatt Macy */ 525*eda14cbcSMatt Macy if (prop == ZPOOL_PROP_INVAL) { 526*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 527*eda14cbcSMatt Macy "invalid property '%s'"), propname); 528*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 529*eda14cbcSMatt Macy goto error; 530*eda14cbcSMatt Macy } 531*eda14cbcSMatt Macy 532*eda14cbcSMatt Macy if (zpool_prop_readonly(prop)) { 533*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 534*eda14cbcSMatt Macy "is readonly"), propname); 535*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 536*eda14cbcSMatt Macy goto error; 537*eda14cbcSMatt Macy } 538*eda14cbcSMatt Macy 539*eda14cbcSMatt Macy if (!flags.create && zpool_prop_setonce(prop)) { 540*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 541*eda14cbcSMatt Macy "property '%s' can only be set at " 542*eda14cbcSMatt Macy "creation time"), propname); 543*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 544*eda14cbcSMatt Macy goto error; 545*eda14cbcSMatt Macy } 546*eda14cbcSMatt Macy 547*eda14cbcSMatt Macy if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 548*eda14cbcSMatt Macy &strval, &intval, errbuf) != 0) 549*eda14cbcSMatt Macy goto error; 550*eda14cbcSMatt Macy 551*eda14cbcSMatt Macy /* 552*eda14cbcSMatt Macy * Perform additional checking for specific properties. 553*eda14cbcSMatt Macy */ 554*eda14cbcSMatt Macy switch (prop) { 555*eda14cbcSMatt Macy case ZPOOL_PROP_VERSION: 556*eda14cbcSMatt Macy if (intval < version || 557*eda14cbcSMatt Macy !SPA_VERSION_IS_SUPPORTED(intval)) { 558*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 559*eda14cbcSMatt Macy "property '%s' number %d is invalid."), 560*eda14cbcSMatt Macy propname, intval); 561*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 562*eda14cbcSMatt Macy goto error; 563*eda14cbcSMatt Macy } 564*eda14cbcSMatt Macy break; 565*eda14cbcSMatt Macy 566*eda14cbcSMatt Macy case ZPOOL_PROP_ASHIFT: 567*eda14cbcSMatt Macy if (intval != 0 && 568*eda14cbcSMatt Macy (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) { 569*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 570*eda14cbcSMatt Macy "property '%s' number %d is invalid, only " 571*eda14cbcSMatt Macy "values between %" PRId32 " and " 572*eda14cbcSMatt Macy "%" PRId32 " are allowed."), 573*eda14cbcSMatt Macy propname, intval, ASHIFT_MIN, ASHIFT_MAX); 574*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 575*eda14cbcSMatt Macy goto error; 576*eda14cbcSMatt Macy } 577*eda14cbcSMatt Macy break; 578*eda14cbcSMatt Macy 579*eda14cbcSMatt Macy case ZPOOL_PROP_BOOTFS: 580*eda14cbcSMatt Macy if (flags.create || flags.import) { 581*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 582*eda14cbcSMatt Macy "property '%s' cannot be set at creation " 583*eda14cbcSMatt Macy "or import time"), propname); 584*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 585*eda14cbcSMatt Macy goto error; 586*eda14cbcSMatt Macy } 587*eda14cbcSMatt Macy 588*eda14cbcSMatt Macy if (version < SPA_VERSION_BOOTFS) { 589*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 590*eda14cbcSMatt Macy "pool must be upgraded to support " 591*eda14cbcSMatt Macy "'%s' property"), propname); 592*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 593*eda14cbcSMatt Macy goto error; 594*eda14cbcSMatt Macy } 595*eda14cbcSMatt Macy 596*eda14cbcSMatt Macy /* 597*eda14cbcSMatt Macy * bootfs property value has to be a dataset name and 598*eda14cbcSMatt Macy * the dataset has to be in the same pool as it sets to. 599*eda14cbcSMatt Macy */ 600*eda14cbcSMatt Macy if (!bootfs_name_valid(poolname, strval)) { 601*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 602*eda14cbcSMatt Macy "is an invalid name"), strval); 603*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 604*eda14cbcSMatt Macy goto error; 605*eda14cbcSMatt Macy } 606*eda14cbcSMatt Macy 607*eda14cbcSMatt Macy if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 608*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 609*eda14cbcSMatt Macy "could not open pool '%s'"), poolname); 610*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 611*eda14cbcSMatt Macy goto error; 612*eda14cbcSMatt Macy } 613*eda14cbcSMatt Macy zpool_close(zhp); 614*eda14cbcSMatt Macy break; 615*eda14cbcSMatt Macy 616*eda14cbcSMatt Macy case ZPOOL_PROP_ALTROOT: 617*eda14cbcSMatt Macy if (!flags.create && !flags.import) { 618*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 619*eda14cbcSMatt Macy "property '%s' can only be set during pool " 620*eda14cbcSMatt Macy "creation or import"), propname); 621*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 622*eda14cbcSMatt Macy goto error; 623*eda14cbcSMatt Macy } 624*eda14cbcSMatt Macy 625*eda14cbcSMatt Macy if (strval[0] != '/') { 626*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 627*eda14cbcSMatt Macy "bad alternate root '%s'"), strval); 628*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 629*eda14cbcSMatt Macy goto error; 630*eda14cbcSMatt Macy } 631*eda14cbcSMatt Macy break; 632*eda14cbcSMatt Macy 633*eda14cbcSMatt Macy case ZPOOL_PROP_CACHEFILE: 634*eda14cbcSMatt Macy if (strval[0] == '\0') 635*eda14cbcSMatt Macy break; 636*eda14cbcSMatt Macy 637*eda14cbcSMatt Macy if (strcmp(strval, "none") == 0) 638*eda14cbcSMatt Macy break; 639*eda14cbcSMatt Macy 640*eda14cbcSMatt Macy if (strval[0] != '/') { 641*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 642*eda14cbcSMatt Macy "property '%s' must be empty, an " 643*eda14cbcSMatt Macy "absolute path, or 'none'"), propname); 644*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 645*eda14cbcSMatt Macy goto error; 646*eda14cbcSMatt Macy } 647*eda14cbcSMatt Macy 648*eda14cbcSMatt Macy slash = strrchr(strval, '/'); 649*eda14cbcSMatt Macy 650*eda14cbcSMatt Macy if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 651*eda14cbcSMatt Macy strcmp(slash, "/..") == 0) { 652*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 653*eda14cbcSMatt Macy "'%s' is not a valid file"), strval); 654*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 655*eda14cbcSMatt Macy goto error; 656*eda14cbcSMatt Macy } 657*eda14cbcSMatt Macy 658*eda14cbcSMatt Macy *slash = '\0'; 659*eda14cbcSMatt Macy 660*eda14cbcSMatt Macy if (strval[0] != '\0' && 661*eda14cbcSMatt Macy (stat64(strval, &statbuf) != 0 || 662*eda14cbcSMatt Macy !S_ISDIR(statbuf.st_mode))) { 663*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 664*eda14cbcSMatt Macy "'%s' is not a valid directory"), 665*eda14cbcSMatt Macy strval); 666*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 667*eda14cbcSMatt Macy goto error; 668*eda14cbcSMatt Macy } 669*eda14cbcSMatt Macy 670*eda14cbcSMatt Macy *slash = '/'; 671*eda14cbcSMatt Macy break; 672*eda14cbcSMatt Macy 673*eda14cbcSMatt Macy case ZPOOL_PROP_COMMENT: 674*eda14cbcSMatt Macy for (check = strval; *check != '\0'; check++) { 675*eda14cbcSMatt Macy if (!isprint(*check)) { 676*eda14cbcSMatt Macy zfs_error_aux(hdl, 677*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 678*eda14cbcSMatt Macy "comment may only have printable " 679*eda14cbcSMatt Macy "characters")); 680*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, 681*eda14cbcSMatt Macy errbuf); 682*eda14cbcSMatt Macy goto error; 683*eda14cbcSMatt Macy } 684*eda14cbcSMatt Macy } 685*eda14cbcSMatt Macy if (strlen(strval) > ZPROP_MAX_COMMENT) { 686*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 687*eda14cbcSMatt Macy "comment must not exceed %d characters"), 688*eda14cbcSMatt Macy ZPROP_MAX_COMMENT); 689*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 690*eda14cbcSMatt Macy goto error; 691*eda14cbcSMatt Macy } 692*eda14cbcSMatt Macy break; 693*eda14cbcSMatt Macy case ZPOOL_PROP_READONLY: 694*eda14cbcSMatt Macy if (!flags.import) { 695*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 696*eda14cbcSMatt Macy "property '%s' can only be set at " 697*eda14cbcSMatt Macy "import time"), propname); 698*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 699*eda14cbcSMatt Macy goto error; 700*eda14cbcSMatt Macy } 701*eda14cbcSMatt Macy break; 702*eda14cbcSMatt Macy case ZPOOL_PROP_MULTIHOST: 703*eda14cbcSMatt Macy if (get_system_hostid() == 0) { 704*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 705*eda14cbcSMatt Macy "requires a non-zero system hostid")); 706*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 707*eda14cbcSMatt Macy goto error; 708*eda14cbcSMatt Macy } 709*eda14cbcSMatt Macy break; 710*eda14cbcSMatt Macy case ZPOOL_PROP_DEDUPDITTO: 711*eda14cbcSMatt Macy printf("Note: property '%s' no longer has " 712*eda14cbcSMatt Macy "any effect\n", propname); 713*eda14cbcSMatt Macy break; 714*eda14cbcSMatt Macy 715*eda14cbcSMatt Macy default: 716*eda14cbcSMatt Macy break; 717*eda14cbcSMatt Macy } 718*eda14cbcSMatt Macy } 719*eda14cbcSMatt Macy 720*eda14cbcSMatt Macy return (retprops); 721*eda14cbcSMatt Macy error: 722*eda14cbcSMatt Macy nvlist_free(retprops); 723*eda14cbcSMatt Macy return (NULL); 724*eda14cbcSMatt Macy } 725*eda14cbcSMatt Macy 726*eda14cbcSMatt Macy /* 727*eda14cbcSMatt Macy * Set zpool property : propname=propval. 728*eda14cbcSMatt Macy */ 729*eda14cbcSMatt Macy int 730*eda14cbcSMatt Macy zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 731*eda14cbcSMatt Macy { 732*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 733*eda14cbcSMatt Macy int ret = -1; 734*eda14cbcSMatt Macy char errbuf[1024]; 735*eda14cbcSMatt Macy nvlist_t *nvl = NULL; 736*eda14cbcSMatt Macy nvlist_t *realprops; 737*eda14cbcSMatt Macy uint64_t version; 738*eda14cbcSMatt Macy prop_flags_t flags = { 0 }; 739*eda14cbcSMatt Macy 740*eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), 741*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 742*eda14cbcSMatt Macy zhp->zpool_name); 743*eda14cbcSMatt Macy 744*eda14cbcSMatt Macy if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 745*eda14cbcSMatt Macy return (no_memory(zhp->zpool_hdl)); 746*eda14cbcSMatt Macy 747*eda14cbcSMatt Macy if (nvlist_add_string(nvl, propname, propval) != 0) { 748*eda14cbcSMatt Macy nvlist_free(nvl); 749*eda14cbcSMatt Macy return (no_memory(zhp->zpool_hdl)); 750*eda14cbcSMatt Macy } 751*eda14cbcSMatt Macy 752*eda14cbcSMatt Macy version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 753*eda14cbcSMatt Macy if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 754*eda14cbcSMatt Macy zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 755*eda14cbcSMatt Macy nvlist_free(nvl); 756*eda14cbcSMatt Macy return (-1); 757*eda14cbcSMatt Macy } 758*eda14cbcSMatt Macy 759*eda14cbcSMatt Macy nvlist_free(nvl); 760*eda14cbcSMatt Macy nvl = realprops; 761*eda14cbcSMatt Macy 762*eda14cbcSMatt Macy /* 763*eda14cbcSMatt Macy * Execute the corresponding ioctl() to set this property. 764*eda14cbcSMatt Macy */ 765*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 766*eda14cbcSMatt Macy 767*eda14cbcSMatt Macy if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 768*eda14cbcSMatt Macy nvlist_free(nvl); 769*eda14cbcSMatt Macy return (-1); 770*eda14cbcSMatt Macy } 771*eda14cbcSMatt Macy 772*eda14cbcSMatt Macy ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 773*eda14cbcSMatt Macy 774*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 775*eda14cbcSMatt Macy nvlist_free(nvl); 776*eda14cbcSMatt Macy 777*eda14cbcSMatt Macy if (ret) 778*eda14cbcSMatt Macy (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 779*eda14cbcSMatt Macy else 780*eda14cbcSMatt Macy (void) zpool_props_refresh(zhp); 781*eda14cbcSMatt Macy 782*eda14cbcSMatt Macy return (ret); 783*eda14cbcSMatt Macy } 784*eda14cbcSMatt Macy 785*eda14cbcSMatt Macy int 786*eda14cbcSMatt Macy zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 787*eda14cbcSMatt Macy { 788*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 789*eda14cbcSMatt Macy zprop_list_t *entry; 790*eda14cbcSMatt Macy char buf[ZFS_MAXPROPLEN]; 791*eda14cbcSMatt Macy nvlist_t *features = NULL; 792*eda14cbcSMatt Macy nvpair_t *nvp; 793*eda14cbcSMatt Macy zprop_list_t **last; 794*eda14cbcSMatt Macy boolean_t firstexpand = (NULL == *plp); 795*eda14cbcSMatt Macy int i; 796*eda14cbcSMatt Macy 797*eda14cbcSMatt Macy if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 798*eda14cbcSMatt Macy return (-1); 799*eda14cbcSMatt Macy 800*eda14cbcSMatt Macy last = plp; 801*eda14cbcSMatt Macy while (*last != NULL) 802*eda14cbcSMatt Macy last = &(*last)->pl_next; 803*eda14cbcSMatt Macy 804*eda14cbcSMatt Macy if ((*plp)->pl_all) 805*eda14cbcSMatt Macy features = zpool_get_features(zhp); 806*eda14cbcSMatt Macy 807*eda14cbcSMatt Macy if ((*plp)->pl_all && firstexpand) { 808*eda14cbcSMatt Macy for (i = 0; i < SPA_FEATURES; i++) { 809*eda14cbcSMatt Macy zprop_list_t *entry = zfs_alloc(hdl, 810*eda14cbcSMatt Macy sizeof (zprop_list_t)); 811*eda14cbcSMatt Macy entry->pl_prop = ZPROP_INVAL; 812*eda14cbcSMatt Macy entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 813*eda14cbcSMatt Macy spa_feature_table[i].fi_uname); 814*eda14cbcSMatt Macy entry->pl_width = strlen(entry->pl_user_prop); 815*eda14cbcSMatt Macy entry->pl_all = B_TRUE; 816*eda14cbcSMatt Macy 817*eda14cbcSMatt Macy *last = entry; 818*eda14cbcSMatt Macy last = &entry->pl_next; 819*eda14cbcSMatt Macy } 820*eda14cbcSMatt Macy } 821*eda14cbcSMatt Macy 822*eda14cbcSMatt Macy /* add any unsupported features */ 823*eda14cbcSMatt Macy for (nvp = nvlist_next_nvpair(features, NULL); 824*eda14cbcSMatt Macy nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 825*eda14cbcSMatt Macy char *propname; 826*eda14cbcSMatt Macy boolean_t found; 827*eda14cbcSMatt Macy zprop_list_t *entry; 828*eda14cbcSMatt Macy 829*eda14cbcSMatt Macy if (zfeature_is_supported(nvpair_name(nvp))) 830*eda14cbcSMatt Macy continue; 831*eda14cbcSMatt Macy 832*eda14cbcSMatt Macy propname = zfs_asprintf(hdl, "unsupported@%s", 833*eda14cbcSMatt Macy nvpair_name(nvp)); 834*eda14cbcSMatt Macy 835*eda14cbcSMatt Macy /* 836*eda14cbcSMatt Macy * Before adding the property to the list make sure that no 837*eda14cbcSMatt Macy * other pool already added the same property. 838*eda14cbcSMatt Macy */ 839*eda14cbcSMatt Macy found = B_FALSE; 840*eda14cbcSMatt Macy entry = *plp; 841*eda14cbcSMatt Macy while (entry != NULL) { 842*eda14cbcSMatt Macy if (entry->pl_user_prop != NULL && 843*eda14cbcSMatt Macy strcmp(propname, entry->pl_user_prop) == 0) { 844*eda14cbcSMatt Macy found = B_TRUE; 845*eda14cbcSMatt Macy break; 846*eda14cbcSMatt Macy } 847*eda14cbcSMatt Macy entry = entry->pl_next; 848*eda14cbcSMatt Macy } 849*eda14cbcSMatt Macy if (found) { 850*eda14cbcSMatt Macy free(propname); 851*eda14cbcSMatt Macy continue; 852*eda14cbcSMatt Macy } 853*eda14cbcSMatt Macy 854*eda14cbcSMatt Macy entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 855*eda14cbcSMatt Macy entry->pl_prop = ZPROP_INVAL; 856*eda14cbcSMatt Macy entry->pl_user_prop = propname; 857*eda14cbcSMatt Macy entry->pl_width = strlen(entry->pl_user_prop); 858*eda14cbcSMatt Macy entry->pl_all = B_TRUE; 859*eda14cbcSMatt Macy 860*eda14cbcSMatt Macy *last = entry; 861*eda14cbcSMatt Macy last = &entry->pl_next; 862*eda14cbcSMatt Macy } 863*eda14cbcSMatt Macy 864*eda14cbcSMatt Macy for (entry = *plp; entry != NULL; entry = entry->pl_next) { 865*eda14cbcSMatt Macy 866*eda14cbcSMatt Macy if (entry->pl_fixed) 867*eda14cbcSMatt Macy continue; 868*eda14cbcSMatt Macy 869*eda14cbcSMatt Macy if (entry->pl_prop != ZPROP_INVAL && 870*eda14cbcSMatt Macy zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 871*eda14cbcSMatt Macy NULL, B_FALSE) == 0) { 872*eda14cbcSMatt Macy if (strlen(buf) > entry->pl_width) 873*eda14cbcSMatt Macy entry->pl_width = strlen(buf); 874*eda14cbcSMatt Macy } 875*eda14cbcSMatt Macy } 876*eda14cbcSMatt Macy 877*eda14cbcSMatt Macy return (0); 878*eda14cbcSMatt Macy } 879*eda14cbcSMatt Macy 880*eda14cbcSMatt Macy /* 881*eda14cbcSMatt Macy * Get the state for the given feature on the given ZFS pool. 882*eda14cbcSMatt Macy */ 883*eda14cbcSMatt Macy int 884*eda14cbcSMatt Macy zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 885*eda14cbcSMatt Macy size_t len) 886*eda14cbcSMatt Macy { 887*eda14cbcSMatt Macy uint64_t refcount; 888*eda14cbcSMatt Macy boolean_t found = B_FALSE; 889*eda14cbcSMatt Macy nvlist_t *features = zpool_get_features(zhp); 890*eda14cbcSMatt Macy boolean_t supported; 891*eda14cbcSMatt Macy const char *feature = strchr(propname, '@') + 1; 892*eda14cbcSMatt Macy 893*eda14cbcSMatt Macy supported = zpool_prop_feature(propname); 894*eda14cbcSMatt Macy ASSERT(supported || zpool_prop_unsupported(propname)); 895*eda14cbcSMatt Macy 896*eda14cbcSMatt Macy /* 897*eda14cbcSMatt Macy * Convert from feature name to feature guid. This conversion is 898*eda14cbcSMatt Macy * unnecessary for unsupported@... properties because they already 899*eda14cbcSMatt Macy * use guids. 900*eda14cbcSMatt Macy */ 901*eda14cbcSMatt Macy if (supported) { 902*eda14cbcSMatt Macy int ret; 903*eda14cbcSMatt Macy spa_feature_t fid; 904*eda14cbcSMatt Macy 905*eda14cbcSMatt Macy ret = zfeature_lookup_name(feature, &fid); 906*eda14cbcSMatt Macy if (ret != 0) { 907*eda14cbcSMatt Macy (void) strlcpy(buf, "-", len); 908*eda14cbcSMatt Macy return (ENOTSUP); 909*eda14cbcSMatt Macy } 910*eda14cbcSMatt Macy feature = spa_feature_table[fid].fi_guid; 911*eda14cbcSMatt Macy } 912*eda14cbcSMatt Macy 913*eda14cbcSMatt Macy if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 914*eda14cbcSMatt Macy found = B_TRUE; 915*eda14cbcSMatt Macy 916*eda14cbcSMatt Macy if (supported) { 917*eda14cbcSMatt Macy if (!found) { 918*eda14cbcSMatt Macy (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 919*eda14cbcSMatt Macy } else { 920*eda14cbcSMatt Macy if (refcount == 0) 921*eda14cbcSMatt Macy (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 922*eda14cbcSMatt Macy else 923*eda14cbcSMatt Macy (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 924*eda14cbcSMatt Macy } 925*eda14cbcSMatt Macy } else { 926*eda14cbcSMatt Macy if (found) { 927*eda14cbcSMatt Macy if (refcount == 0) { 928*eda14cbcSMatt Macy (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 929*eda14cbcSMatt Macy } else { 930*eda14cbcSMatt Macy (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 931*eda14cbcSMatt Macy } 932*eda14cbcSMatt Macy } else { 933*eda14cbcSMatt Macy (void) strlcpy(buf, "-", len); 934*eda14cbcSMatt Macy return (ENOTSUP); 935*eda14cbcSMatt Macy } 936*eda14cbcSMatt Macy } 937*eda14cbcSMatt Macy 938*eda14cbcSMatt Macy return (0); 939*eda14cbcSMatt Macy } 940*eda14cbcSMatt Macy 941*eda14cbcSMatt Macy /* 942*eda14cbcSMatt Macy * Validate the given pool name, optionally putting an extended error message in 943*eda14cbcSMatt Macy * 'buf'. 944*eda14cbcSMatt Macy */ 945*eda14cbcSMatt Macy boolean_t 946*eda14cbcSMatt Macy zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 947*eda14cbcSMatt Macy { 948*eda14cbcSMatt Macy namecheck_err_t why; 949*eda14cbcSMatt Macy char what; 950*eda14cbcSMatt Macy int ret; 951*eda14cbcSMatt Macy 952*eda14cbcSMatt Macy ret = pool_namecheck(pool, &why, &what); 953*eda14cbcSMatt Macy 954*eda14cbcSMatt Macy /* 955*eda14cbcSMatt Macy * The rules for reserved pool names were extended at a later point. 956*eda14cbcSMatt Macy * But we need to support users with existing pools that may now be 957*eda14cbcSMatt Macy * invalid. So we only check for this expanded set of names during a 958*eda14cbcSMatt Macy * create (or import), and only in userland. 959*eda14cbcSMatt Macy */ 960*eda14cbcSMatt Macy if (ret == 0 && !isopen && 961*eda14cbcSMatt Macy (strncmp(pool, "mirror", 6) == 0 || 962*eda14cbcSMatt Macy strncmp(pool, "raidz", 5) == 0 || 963*eda14cbcSMatt Macy strncmp(pool, "spare", 5) == 0 || 964*eda14cbcSMatt Macy strcmp(pool, "log") == 0)) { 965*eda14cbcSMatt Macy if (hdl != NULL) 966*eda14cbcSMatt Macy zfs_error_aux(hdl, 967*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "name is reserved")); 968*eda14cbcSMatt Macy return (B_FALSE); 969*eda14cbcSMatt Macy } 970*eda14cbcSMatt Macy 971*eda14cbcSMatt Macy 972*eda14cbcSMatt Macy if (ret != 0) { 973*eda14cbcSMatt Macy if (hdl != NULL) { 974*eda14cbcSMatt Macy switch (why) { 975*eda14cbcSMatt Macy case NAME_ERR_TOOLONG: 976*eda14cbcSMatt Macy zfs_error_aux(hdl, 977*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "name is too long")); 978*eda14cbcSMatt Macy break; 979*eda14cbcSMatt Macy 980*eda14cbcSMatt Macy case NAME_ERR_INVALCHAR: 981*eda14cbcSMatt Macy zfs_error_aux(hdl, 982*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "invalid character " 983*eda14cbcSMatt Macy "'%c' in pool name"), what); 984*eda14cbcSMatt Macy break; 985*eda14cbcSMatt Macy 986*eda14cbcSMatt Macy case NAME_ERR_NOLETTER: 987*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 988*eda14cbcSMatt Macy "name must begin with a letter")); 989*eda14cbcSMatt Macy break; 990*eda14cbcSMatt Macy 991*eda14cbcSMatt Macy case NAME_ERR_RESERVED: 992*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 993*eda14cbcSMatt Macy "name is reserved")); 994*eda14cbcSMatt Macy break; 995*eda14cbcSMatt Macy 996*eda14cbcSMatt Macy case NAME_ERR_DISKLIKE: 997*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 998*eda14cbcSMatt Macy "pool name is reserved")); 999*eda14cbcSMatt Macy break; 1000*eda14cbcSMatt Macy 1001*eda14cbcSMatt Macy case NAME_ERR_LEADING_SLASH: 1002*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1003*eda14cbcSMatt Macy "leading slash in name")); 1004*eda14cbcSMatt Macy break; 1005*eda14cbcSMatt Macy 1006*eda14cbcSMatt Macy case NAME_ERR_EMPTY_COMPONENT: 1007*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1008*eda14cbcSMatt Macy "empty component in name")); 1009*eda14cbcSMatt Macy break; 1010*eda14cbcSMatt Macy 1011*eda14cbcSMatt Macy case NAME_ERR_TRAILING_SLASH: 1012*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1013*eda14cbcSMatt Macy "trailing slash in name")); 1014*eda14cbcSMatt Macy break; 1015*eda14cbcSMatt Macy 1016*eda14cbcSMatt Macy case NAME_ERR_MULTIPLE_DELIMITERS: 1017*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1018*eda14cbcSMatt Macy "multiple '@' and/or '#' delimiters in " 1019*eda14cbcSMatt Macy "name")); 1020*eda14cbcSMatt Macy break; 1021*eda14cbcSMatt Macy 1022*eda14cbcSMatt Macy case NAME_ERR_NO_AT: 1023*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1024*eda14cbcSMatt Macy "permission set is missing '@'")); 1025*eda14cbcSMatt Macy break; 1026*eda14cbcSMatt Macy 1027*eda14cbcSMatt Macy default: 1028*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1029*eda14cbcSMatt Macy "(%d) not defined"), why); 1030*eda14cbcSMatt Macy break; 1031*eda14cbcSMatt Macy } 1032*eda14cbcSMatt Macy } 1033*eda14cbcSMatt Macy return (B_FALSE); 1034*eda14cbcSMatt Macy } 1035*eda14cbcSMatt Macy 1036*eda14cbcSMatt Macy return (B_TRUE); 1037*eda14cbcSMatt Macy } 1038*eda14cbcSMatt Macy 1039*eda14cbcSMatt Macy /* 1040*eda14cbcSMatt Macy * Open a handle to the given pool, even if the pool is currently in the FAULTED 1041*eda14cbcSMatt Macy * state. 1042*eda14cbcSMatt Macy */ 1043*eda14cbcSMatt Macy zpool_handle_t * 1044*eda14cbcSMatt Macy zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 1045*eda14cbcSMatt Macy { 1046*eda14cbcSMatt Macy zpool_handle_t *zhp; 1047*eda14cbcSMatt Macy boolean_t missing; 1048*eda14cbcSMatt Macy 1049*eda14cbcSMatt Macy /* 1050*eda14cbcSMatt Macy * Make sure the pool name is valid. 1051*eda14cbcSMatt Macy */ 1052*eda14cbcSMatt Macy if (!zpool_name_valid(hdl, B_TRUE, pool)) { 1053*eda14cbcSMatt Macy (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 1054*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot open '%s'"), 1055*eda14cbcSMatt Macy pool); 1056*eda14cbcSMatt Macy return (NULL); 1057*eda14cbcSMatt Macy } 1058*eda14cbcSMatt Macy 1059*eda14cbcSMatt Macy if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 1060*eda14cbcSMatt Macy return (NULL); 1061*eda14cbcSMatt Macy 1062*eda14cbcSMatt Macy zhp->zpool_hdl = hdl; 1063*eda14cbcSMatt Macy (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1064*eda14cbcSMatt Macy 1065*eda14cbcSMatt Macy if (zpool_refresh_stats(zhp, &missing) != 0) { 1066*eda14cbcSMatt Macy zpool_close(zhp); 1067*eda14cbcSMatt Macy return (NULL); 1068*eda14cbcSMatt Macy } 1069*eda14cbcSMatt Macy 1070*eda14cbcSMatt Macy if (missing) { 1071*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 1072*eda14cbcSMatt Macy (void) zfs_error_fmt(hdl, EZFS_NOENT, 1073*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 1074*eda14cbcSMatt Macy zpool_close(zhp); 1075*eda14cbcSMatt Macy return (NULL); 1076*eda14cbcSMatt Macy } 1077*eda14cbcSMatt Macy 1078*eda14cbcSMatt Macy return (zhp); 1079*eda14cbcSMatt Macy } 1080*eda14cbcSMatt Macy 1081*eda14cbcSMatt Macy /* 1082*eda14cbcSMatt Macy * Like the above, but silent on error. Used when iterating over pools (because 1083*eda14cbcSMatt Macy * the configuration cache may be out of date). 1084*eda14cbcSMatt Macy */ 1085*eda14cbcSMatt Macy int 1086*eda14cbcSMatt Macy zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1087*eda14cbcSMatt Macy { 1088*eda14cbcSMatt Macy zpool_handle_t *zhp; 1089*eda14cbcSMatt Macy boolean_t missing; 1090*eda14cbcSMatt Macy 1091*eda14cbcSMatt Macy if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 1092*eda14cbcSMatt Macy return (-1); 1093*eda14cbcSMatt Macy 1094*eda14cbcSMatt Macy zhp->zpool_hdl = hdl; 1095*eda14cbcSMatt Macy (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1096*eda14cbcSMatt Macy 1097*eda14cbcSMatt Macy if (zpool_refresh_stats(zhp, &missing) != 0) { 1098*eda14cbcSMatt Macy zpool_close(zhp); 1099*eda14cbcSMatt Macy return (-1); 1100*eda14cbcSMatt Macy } 1101*eda14cbcSMatt Macy 1102*eda14cbcSMatt Macy if (missing) { 1103*eda14cbcSMatt Macy zpool_close(zhp); 1104*eda14cbcSMatt Macy *ret = NULL; 1105*eda14cbcSMatt Macy return (0); 1106*eda14cbcSMatt Macy } 1107*eda14cbcSMatt Macy 1108*eda14cbcSMatt Macy *ret = zhp; 1109*eda14cbcSMatt Macy return (0); 1110*eda14cbcSMatt Macy } 1111*eda14cbcSMatt Macy 1112*eda14cbcSMatt Macy /* 1113*eda14cbcSMatt Macy * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1114*eda14cbcSMatt Macy * state. 1115*eda14cbcSMatt Macy */ 1116*eda14cbcSMatt Macy zpool_handle_t * 1117*eda14cbcSMatt Macy zpool_open(libzfs_handle_t *hdl, const char *pool) 1118*eda14cbcSMatt Macy { 1119*eda14cbcSMatt Macy zpool_handle_t *zhp; 1120*eda14cbcSMatt Macy 1121*eda14cbcSMatt Macy if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1122*eda14cbcSMatt Macy return (NULL); 1123*eda14cbcSMatt Macy 1124*eda14cbcSMatt Macy if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1125*eda14cbcSMatt Macy (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 1126*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1127*eda14cbcSMatt Macy zpool_close(zhp); 1128*eda14cbcSMatt Macy return (NULL); 1129*eda14cbcSMatt Macy } 1130*eda14cbcSMatt Macy 1131*eda14cbcSMatt Macy return (zhp); 1132*eda14cbcSMatt Macy } 1133*eda14cbcSMatt Macy 1134*eda14cbcSMatt Macy /* 1135*eda14cbcSMatt Macy * Close the handle. Simply frees the memory associated with the handle. 1136*eda14cbcSMatt Macy */ 1137*eda14cbcSMatt Macy void 1138*eda14cbcSMatt Macy zpool_close(zpool_handle_t *zhp) 1139*eda14cbcSMatt Macy { 1140*eda14cbcSMatt Macy nvlist_free(zhp->zpool_config); 1141*eda14cbcSMatt Macy nvlist_free(zhp->zpool_old_config); 1142*eda14cbcSMatt Macy nvlist_free(zhp->zpool_props); 1143*eda14cbcSMatt Macy free(zhp); 1144*eda14cbcSMatt Macy } 1145*eda14cbcSMatt Macy 1146*eda14cbcSMatt Macy /* 1147*eda14cbcSMatt Macy * Return the name of the pool. 1148*eda14cbcSMatt Macy */ 1149*eda14cbcSMatt Macy const char * 1150*eda14cbcSMatt Macy zpool_get_name(zpool_handle_t *zhp) 1151*eda14cbcSMatt Macy { 1152*eda14cbcSMatt Macy return (zhp->zpool_name); 1153*eda14cbcSMatt Macy } 1154*eda14cbcSMatt Macy 1155*eda14cbcSMatt Macy 1156*eda14cbcSMatt Macy /* 1157*eda14cbcSMatt Macy * Return the state of the pool (ACTIVE or UNAVAILABLE) 1158*eda14cbcSMatt Macy */ 1159*eda14cbcSMatt Macy int 1160*eda14cbcSMatt Macy zpool_get_state(zpool_handle_t *zhp) 1161*eda14cbcSMatt Macy { 1162*eda14cbcSMatt Macy return (zhp->zpool_state); 1163*eda14cbcSMatt Macy } 1164*eda14cbcSMatt Macy 1165*eda14cbcSMatt Macy /* 1166*eda14cbcSMatt Macy * Check if vdev list contains a special vdev 1167*eda14cbcSMatt Macy */ 1168*eda14cbcSMatt Macy static boolean_t 1169*eda14cbcSMatt Macy zpool_has_special_vdev(nvlist_t *nvroot) 1170*eda14cbcSMatt Macy { 1171*eda14cbcSMatt Macy nvlist_t **child; 1172*eda14cbcSMatt Macy uint_t children; 1173*eda14cbcSMatt Macy 1174*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child, 1175*eda14cbcSMatt Macy &children) == 0) { 1176*eda14cbcSMatt Macy for (uint_t c = 0; c < children; c++) { 1177*eda14cbcSMatt Macy char *bias; 1178*eda14cbcSMatt Macy 1179*eda14cbcSMatt Macy if (nvlist_lookup_string(child[c], 1180*eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 && 1181*eda14cbcSMatt Macy strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) { 1182*eda14cbcSMatt Macy return (B_TRUE); 1183*eda14cbcSMatt Macy } 1184*eda14cbcSMatt Macy } 1185*eda14cbcSMatt Macy } 1186*eda14cbcSMatt Macy return (B_FALSE); 1187*eda14cbcSMatt Macy } 1188*eda14cbcSMatt Macy 1189*eda14cbcSMatt Macy /* 1190*eda14cbcSMatt Macy * Create the named pool, using the provided vdev list. It is assumed 1191*eda14cbcSMatt Macy * that the consumer has already validated the contents of the nvlist, so we 1192*eda14cbcSMatt Macy * don't have to worry about error semantics. 1193*eda14cbcSMatt Macy */ 1194*eda14cbcSMatt Macy int 1195*eda14cbcSMatt Macy zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 1196*eda14cbcSMatt Macy nvlist_t *props, nvlist_t *fsprops) 1197*eda14cbcSMatt Macy { 1198*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1199*eda14cbcSMatt Macy nvlist_t *zc_fsprops = NULL; 1200*eda14cbcSMatt Macy nvlist_t *zc_props = NULL; 1201*eda14cbcSMatt Macy nvlist_t *hidden_args = NULL; 1202*eda14cbcSMatt Macy uint8_t *wkeydata = NULL; 1203*eda14cbcSMatt Macy uint_t wkeylen = 0; 1204*eda14cbcSMatt Macy char msg[1024]; 1205*eda14cbcSMatt Macy int ret = -1; 1206*eda14cbcSMatt Macy 1207*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1208*eda14cbcSMatt Macy "cannot create '%s'"), pool); 1209*eda14cbcSMatt Macy 1210*eda14cbcSMatt Macy if (!zpool_name_valid(hdl, B_FALSE, pool)) 1211*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1212*eda14cbcSMatt Macy 1213*eda14cbcSMatt Macy if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1214*eda14cbcSMatt Macy return (-1); 1215*eda14cbcSMatt Macy 1216*eda14cbcSMatt Macy if (props) { 1217*eda14cbcSMatt Macy prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1218*eda14cbcSMatt Macy 1219*eda14cbcSMatt Macy if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1220*eda14cbcSMatt Macy SPA_VERSION_1, flags, msg)) == NULL) { 1221*eda14cbcSMatt Macy goto create_failed; 1222*eda14cbcSMatt Macy } 1223*eda14cbcSMatt Macy } 1224*eda14cbcSMatt Macy 1225*eda14cbcSMatt Macy if (fsprops) { 1226*eda14cbcSMatt Macy uint64_t zoned; 1227*eda14cbcSMatt Macy char *zonestr; 1228*eda14cbcSMatt Macy 1229*eda14cbcSMatt Macy zoned = ((nvlist_lookup_string(fsprops, 1230*eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 1231*eda14cbcSMatt Macy strcmp(zonestr, "on") == 0); 1232*eda14cbcSMatt Macy 1233*eda14cbcSMatt Macy if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1234*eda14cbcSMatt Macy fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) { 1235*eda14cbcSMatt Macy goto create_failed; 1236*eda14cbcSMatt Macy } 1237*eda14cbcSMatt Macy 1238*eda14cbcSMatt Macy if (nvlist_exists(zc_fsprops, 1239*eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) && 1240*eda14cbcSMatt Macy !zpool_has_special_vdev(nvroot)) { 1241*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1242*eda14cbcSMatt Macy "%s property requires a special vdev"), 1243*eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)); 1244*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADPROP, msg); 1245*eda14cbcSMatt Macy goto create_failed; 1246*eda14cbcSMatt Macy } 1247*eda14cbcSMatt Macy 1248*eda14cbcSMatt Macy if (!zc_props && 1249*eda14cbcSMatt Macy (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 1250*eda14cbcSMatt Macy goto create_failed; 1251*eda14cbcSMatt Macy } 1252*eda14cbcSMatt Macy if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE, 1253*eda14cbcSMatt Macy &wkeydata, &wkeylen) != 0) { 1254*eda14cbcSMatt Macy zfs_error(hdl, EZFS_CRYPTOFAILED, msg); 1255*eda14cbcSMatt Macy goto create_failed; 1256*eda14cbcSMatt Macy } 1257*eda14cbcSMatt Macy if (nvlist_add_nvlist(zc_props, 1258*eda14cbcSMatt Macy ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 1259*eda14cbcSMatt Macy goto create_failed; 1260*eda14cbcSMatt Macy } 1261*eda14cbcSMatt Macy if (wkeydata != NULL) { 1262*eda14cbcSMatt Macy if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0) 1263*eda14cbcSMatt Macy goto create_failed; 1264*eda14cbcSMatt Macy 1265*eda14cbcSMatt Macy if (nvlist_add_uint8_array(hidden_args, "wkeydata", 1266*eda14cbcSMatt Macy wkeydata, wkeylen) != 0) 1267*eda14cbcSMatt Macy goto create_failed; 1268*eda14cbcSMatt Macy 1269*eda14cbcSMatt Macy if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS, 1270*eda14cbcSMatt Macy hidden_args) != 0) 1271*eda14cbcSMatt Macy goto create_failed; 1272*eda14cbcSMatt Macy } 1273*eda14cbcSMatt Macy } 1274*eda14cbcSMatt Macy 1275*eda14cbcSMatt Macy if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 1276*eda14cbcSMatt Macy goto create_failed; 1277*eda14cbcSMatt Macy 1278*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1279*eda14cbcSMatt Macy 1280*eda14cbcSMatt Macy if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1281*eda14cbcSMatt Macy 1282*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1283*eda14cbcSMatt Macy nvlist_free(zc_props); 1284*eda14cbcSMatt Macy nvlist_free(zc_fsprops); 1285*eda14cbcSMatt Macy nvlist_free(hidden_args); 1286*eda14cbcSMatt Macy if (wkeydata != NULL) 1287*eda14cbcSMatt Macy free(wkeydata); 1288*eda14cbcSMatt Macy 1289*eda14cbcSMatt Macy switch (errno) { 1290*eda14cbcSMatt Macy case EBUSY: 1291*eda14cbcSMatt Macy /* 1292*eda14cbcSMatt Macy * This can happen if the user has specified the same 1293*eda14cbcSMatt Macy * device multiple times. We can't reliably detect this 1294*eda14cbcSMatt Macy * until we try to add it and see we already have a 1295*eda14cbcSMatt Macy * label. This can also happen under if the device is 1296*eda14cbcSMatt Macy * part of an active md or lvm device. 1297*eda14cbcSMatt Macy */ 1298*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1299*eda14cbcSMatt Macy "one or more vdevs refer to the same device, or " 1300*eda14cbcSMatt Macy "one of\nthe devices is part of an active md or " 1301*eda14cbcSMatt Macy "lvm device")); 1302*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADDEV, msg)); 1303*eda14cbcSMatt Macy 1304*eda14cbcSMatt Macy case ERANGE: 1305*eda14cbcSMatt Macy /* 1306*eda14cbcSMatt Macy * This happens if the record size is smaller or larger 1307*eda14cbcSMatt Macy * than the allowed size range, or not a power of 2. 1308*eda14cbcSMatt Macy * 1309*eda14cbcSMatt Macy * NOTE: although zfs_valid_proplist is called earlier, 1310*eda14cbcSMatt Macy * this case may have slipped through since the 1311*eda14cbcSMatt Macy * pool does not exist yet and it is therefore 1312*eda14cbcSMatt Macy * impossible to read properties e.g. max blocksize 1313*eda14cbcSMatt Macy * from the pool. 1314*eda14cbcSMatt Macy */ 1315*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1316*eda14cbcSMatt Macy "record size invalid")); 1317*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, msg)); 1318*eda14cbcSMatt Macy 1319*eda14cbcSMatt Macy case EOVERFLOW: 1320*eda14cbcSMatt Macy /* 1321*eda14cbcSMatt Macy * This occurs when one of the devices is below 1322*eda14cbcSMatt Macy * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1323*eda14cbcSMatt Macy * device was the problem device since there's no 1324*eda14cbcSMatt Macy * reliable way to determine device size from userland. 1325*eda14cbcSMatt Macy */ 1326*eda14cbcSMatt Macy { 1327*eda14cbcSMatt Macy char buf[64]; 1328*eda14cbcSMatt Macy 1329*eda14cbcSMatt Macy zfs_nicebytes(SPA_MINDEVSIZE, buf, 1330*eda14cbcSMatt Macy sizeof (buf)); 1331*eda14cbcSMatt Macy 1332*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1333*eda14cbcSMatt Macy "one or more devices is less than the " 1334*eda14cbcSMatt Macy "minimum size (%s)"), buf); 1335*eda14cbcSMatt Macy } 1336*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADDEV, msg)); 1337*eda14cbcSMatt Macy 1338*eda14cbcSMatt Macy case ENOSPC: 1339*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1340*eda14cbcSMatt Macy "one or more devices is out of space")); 1341*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADDEV, msg)); 1342*eda14cbcSMatt Macy 1343*eda14cbcSMatt Macy default: 1344*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 1345*eda14cbcSMatt Macy } 1346*eda14cbcSMatt Macy } 1347*eda14cbcSMatt Macy 1348*eda14cbcSMatt Macy create_failed: 1349*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1350*eda14cbcSMatt Macy nvlist_free(zc_props); 1351*eda14cbcSMatt Macy nvlist_free(zc_fsprops); 1352*eda14cbcSMatt Macy nvlist_free(hidden_args); 1353*eda14cbcSMatt Macy if (wkeydata != NULL) 1354*eda14cbcSMatt Macy free(wkeydata); 1355*eda14cbcSMatt Macy return (ret); 1356*eda14cbcSMatt Macy } 1357*eda14cbcSMatt Macy 1358*eda14cbcSMatt Macy /* 1359*eda14cbcSMatt Macy * Destroy the given pool. It is up to the caller to ensure that there are no 1360*eda14cbcSMatt Macy * datasets left in the pool. 1361*eda14cbcSMatt Macy */ 1362*eda14cbcSMatt Macy int 1363*eda14cbcSMatt Macy zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1364*eda14cbcSMatt Macy { 1365*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1366*eda14cbcSMatt Macy zfs_handle_t *zfp = NULL; 1367*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 1368*eda14cbcSMatt Macy char msg[1024]; 1369*eda14cbcSMatt Macy 1370*eda14cbcSMatt Macy if (zhp->zpool_state == POOL_STATE_ACTIVE && 1371*eda14cbcSMatt Macy (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1372*eda14cbcSMatt Macy return (-1); 1373*eda14cbcSMatt Macy 1374*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1375*eda14cbcSMatt Macy zc.zc_history = (uint64_t)(uintptr_t)log_str; 1376*eda14cbcSMatt Macy 1377*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 1378*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1379*eda14cbcSMatt Macy "cannot destroy '%s'"), zhp->zpool_name); 1380*eda14cbcSMatt Macy 1381*eda14cbcSMatt Macy if (errno == EROFS) { 1382*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1383*eda14cbcSMatt Macy "one or more devices is read only")); 1384*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 1385*eda14cbcSMatt Macy } else { 1386*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 1387*eda14cbcSMatt Macy } 1388*eda14cbcSMatt Macy 1389*eda14cbcSMatt Macy if (zfp) 1390*eda14cbcSMatt Macy zfs_close(zfp); 1391*eda14cbcSMatt Macy return (-1); 1392*eda14cbcSMatt Macy } 1393*eda14cbcSMatt Macy 1394*eda14cbcSMatt Macy if (zfp) { 1395*eda14cbcSMatt Macy remove_mountpoint(zfp); 1396*eda14cbcSMatt Macy zfs_close(zfp); 1397*eda14cbcSMatt Macy } 1398*eda14cbcSMatt Macy 1399*eda14cbcSMatt Macy return (0); 1400*eda14cbcSMatt Macy } 1401*eda14cbcSMatt Macy 1402*eda14cbcSMatt Macy /* 1403*eda14cbcSMatt Macy * Create a checkpoint in the given pool. 1404*eda14cbcSMatt Macy */ 1405*eda14cbcSMatt Macy int 1406*eda14cbcSMatt Macy zpool_checkpoint(zpool_handle_t *zhp) 1407*eda14cbcSMatt Macy { 1408*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 1409*eda14cbcSMatt Macy char msg[1024]; 1410*eda14cbcSMatt Macy int error; 1411*eda14cbcSMatt Macy 1412*eda14cbcSMatt Macy error = lzc_pool_checkpoint(zhp->zpool_name); 1413*eda14cbcSMatt Macy if (error != 0) { 1414*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1415*eda14cbcSMatt Macy "cannot checkpoint '%s'"), zhp->zpool_name); 1416*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, error, msg); 1417*eda14cbcSMatt Macy return (-1); 1418*eda14cbcSMatt Macy } 1419*eda14cbcSMatt Macy 1420*eda14cbcSMatt Macy return (0); 1421*eda14cbcSMatt Macy } 1422*eda14cbcSMatt Macy 1423*eda14cbcSMatt Macy /* 1424*eda14cbcSMatt Macy * Discard the checkpoint from the given pool. 1425*eda14cbcSMatt Macy */ 1426*eda14cbcSMatt Macy int 1427*eda14cbcSMatt Macy zpool_discard_checkpoint(zpool_handle_t *zhp) 1428*eda14cbcSMatt Macy { 1429*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 1430*eda14cbcSMatt Macy char msg[1024]; 1431*eda14cbcSMatt Macy int error; 1432*eda14cbcSMatt Macy 1433*eda14cbcSMatt Macy error = lzc_pool_checkpoint_discard(zhp->zpool_name); 1434*eda14cbcSMatt Macy if (error != 0) { 1435*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1436*eda14cbcSMatt Macy "cannot discard checkpoint in '%s'"), zhp->zpool_name); 1437*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, error, msg); 1438*eda14cbcSMatt Macy return (-1); 1439*eda14cbcSMatt Macy } 1440*eda14cbcSMatt Macy 1441*eda14cbcSMatt Macy return (0); 1442*eda14cbcSMatt Macy } 1443*eda14cbcSMatt Macy 1444*eda14cbcSMatt Macy /* 1445*eda14cbcSMatt Macy * Add the given vdevs to the pool. The caller must have already performed the 1446*eda14cbcSMatt Macy * necessary verification to ensure that the vdev specification is well-formed. 1447*eda14cbcSMatt Macy */ 1448*eda14cbcSMatt Macy int 1449*eda14cbcSMatt Macy zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1450*eda14cbcSMatt Macy { 1451*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1452*eda14cbcSMatt Macy int ret; 1453*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 1454*eda14cbcSMatt Macy char msg[1024]; 1455*eda14cbcSMatt Macy nvlist_t **spares, **l2cache; 1456*eda14cbcSMatt Macy uint_t nspares, nl2cache; 1457*eda14cbcSMatt Macy 1458*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1459*eda14cbcSMatt Macy "cannot add to '%s'"), zhp->zpool_name); 1460*eda14cbcSMatt Macy 1461*eda14cbcSMatt Macy if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1462*eda14cbcSMatt Macy SPA_VERSION_SPARES && 1463*eda14cbcSMatt Macy nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 1464*eda14cbcSMatt Macy &spares, &nspares) == 0) { 1465*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1466*eda14cbcSMatt Macy "upgraded to add hot spares")); 1467*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1468*eda14cbcSMatt Macy } 1469*eda14cbcSMatt Macy 1470*eda14cbcSMatt Macy if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1471*eda14cbcSMatt Macy SPA_VERSION_L2CACHE && 1472*eda14cbcSMatt Macy nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1473*eda14cbcSMatt Macy &l2cache, &nl2cache) == 0) { 1474*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1475*eda14cbcSMatt Macy "upgraded to add cache devices")); 1476*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1477*eda14cbcSMatt Macy } 1478*eda14cbcSMatt Macy 1479*eda14cbcSMatt Macy if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1480*eda14cbcSMatt Macy return (-1); 1481*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1482*eda14cbcSMatt Macy 1483*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1484*eda14cbcSMatt Macy switch (errno) { 1485*eda14cbcSMatt Macy case EBUSY: 1486*eda14cbcSMatt Macy /* 1487*eda14cbcSMatt Macy * This can happen if the user has specified the same 1488*eda14cbcSMatt Macy * device multiple times. We can't reliably detect this 1489*eda14cbcSMatt Macy * until we try to add it and see we already have a 1490*eda14cbcSMatt Macy * label. 1491*eda14cbcSMatt Macy */ 1492*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1493*eda14cbcSMatt Macy "one or more vdevs refer to the same device")); 1494*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 1495*eda14cbcSMatt Macy break; 1496*eda14cbcSMatt Macy 1497*eda14cbcSMatt Macy case EINVAL: 1498*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1499*eda14cbcSMatt Macy "invalid config; a pool with removing/removed " 1500*eda14cbcSMatt Macy "vdevs does not support adding raidz vdevs")); 1501*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 1502*eda14cbcSMatt Macy break; 1503*eda14cbcSMatt Macy 1504*eda14cbcSMatt Macy case EOVERFLOW: 1505*eda14cbcSMatt Macy /* 1506*eda14cbcSMatt Macy * This occurs when one of the devices is below 1507*eda14cbcSMatt Macy * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1508*eda14cbcSMatt Macy * device was the problem device since there's no 1509*eda14cbcSMatt Macy * reliable way to determine device size from userland. 1510*eda14cbcSMatt Macy */ 1511*eda14cbcSMatt Macy { 1512*eda14cbcSMatt Macy char buf[64]; 1513*eda14cbcSMatt Macy 1514*eda14cbcSMatt Macy zfs_nicebytes(SPA_MINDEVSIZE, buf, 1515*eda14cbcSMatt Macy sizeof (buf)); 1516*eda14cbcSMatt Macy 1517*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1518*eda14cbcSMatt Macy "device is less than the minimum " 1519*eda14cbcSMatt Macy "size (%s)"), buf); 1520*eda14cbcSMatt Macy } 1521*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 1522*eda14cbcSMatt Macy break; 1523*eda14cbcSMatt Macy 1524*eda14cbcSMatt Macy case ENOTSUP: 1525*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1526*eda14cbcSMatt Macy "pool must be upgraded to add these vdevs")); 1527*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1528*eda14cbcSMatt Macy break; 1529*eda14cbcSMatt Macy 1530*eda14cbcSMatt Macy default: 1531*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 1532*eda14cbcSMatt Macy } 1533*eda14cbcSMatt Macy 1534*eda14cbcSMatt Macy ret = -1; 1535*eda14cbcSMatt Macy } else { 1536*eda14cbcSMatt Macy ret = 0; 1537*eda14cbcSMatt Macy } 1538*eda14cbcSMatt Macy 1539*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1540*eda14cbcSMatt Macy 1541*eda14cbcSMatt Macy return (ret); 1542*eda14cbcSMatt Macy } 1543*eda14cbcSMatt Macy 1544*eda14cbcSMatt Macy /* 1545*eda14cbcSMatt Macy * Exports the pool from the system. The caller must ensure that there are no 1546*eda14cbcSMatt Macy * mounted datasets in the pool. 1547*eda14cbcSMatt Macy */ 1548*eda14cbcSMatt Macy static int 1549*eda14cbcSMatt Macy zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 1550*eda14cbcSMatt Macy const char *log_str) 1551*eda14cbcSMatt Macy { 1552*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1553*eda14cbcSMatt Macy char msg[1024]; 1554*eda14cbcSMatt Macy 1555*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1556*eda14cbcSMatt Macy "cannot export '%s'"), zhp->zpool_name); 1557*eda14cbcSMatt Macy 1558*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1559*eda14cbcSMatt Macy zc.zc_cookie = force; 1560*eda14cbcSMatt Macy zc.zc_guid = hardforce; 1561*eda14cbcSMatt Macy zc.zc_history = (uint64_t)(uintptr_t)log_str; 1562*eda14cbcSMatt Macy 1563*eda14cbcSMatt Macy if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 1564*eda14cbcSMatt Macy switch (errno) { 1565*eda14cbcSMatt Macy case EXDEV: 1566*eda14cbcSMatt Macy zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 1567*eda14cbcSMatt Macy "use '-f' to override the following errors:\n" 1568*eda14cbcSMatt Macy "'%s' has an active shared spare which could be" 1569*eda14cbcSMatt Macy " used by other pools once '%s' is exported."), 1570*eda14cbcSMatt Macy zhp->zpool_name, zhp->zpool_name); 1571*eda14cbcSMatt Macy return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 1572*eda14cbcSMatt Macy msg)); 1573*eda14cbcSMatt Macy default: 1574*eda14cbcSMatt Macy return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 1575*eda14cbcSMatt Macy msg)); 1576*eda14cbcSMatt Macy } 1577*eda14cbcSMatt Macy } 1578*eda14cbcSMatt Macy 1579*eda14cbcSMatt Macy return (0); 1580*eda14cbcSMatt Macy } 1581*eda14cbcSMatt Macy 1582*eda14cbcSMatt Macy int 1583*eda14cbcSMatt Macy zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1584*eda14cbcSMatt Macy { 1585*eda14cbcSMatt Macy return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1586*eda14cbcSMatt Macy } 1587*eda14cbcSMatt Macy 1588*eda14cbcSMatt Macy int 1589*eda14cbcSMatt Macy zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1590*eda14cbcSMatt Macy { 1591*eda14cbcSMatt Macy return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1592*eda14cbcSMatt Macy } 1593*eda14cbcSMatt Macy 1594*eda14cbcSMatt Macy static void 1595*eda14cbcSMatt Macy zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 1596*eda14cbcSMatt Macy nvlist_t *config) 1597*eda14cbcSMatt Macy { 1598*eda14cbcSMatt Macy nvlist_t *nv = NULL; 1599*eda14cbcSMatt Macy uint64_t rewindto; 1600*eda14cbcSMatt Macy int64_t loss = -1; 1601*eda14cbcSMatt Macy struct tm t; 1602*eda14cbcSMatt Macy char timestr[128]; 1603*eda14cbcSMatt Macy 1604*eda14cbcSMatt Macy if (!hdl->libzfs_printerr || config == NULL) 1605*eda14cbcSMatt Macy return; 1606*eda14cbcSMatt Macy 1607*eda14cbcSMatt Macy if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1608*eda14cbcSMatt Macy nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1609*eda14cbcSMatt Macy return; 1610*eda14cbcSMatt Macy } 1611*eda14cbcSMatt Macy 1612*eda14cbcSMatt Macy if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1613*eda14cbcSMatt Macy return; 1614*eda14cbcSMatt Macy (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1615*eda14cbcSMatt Macy 1616*eda14cbcSMatt Macy if (localtime_r((time_t *)&rewindto, &t) != NULL && 1617*eda14cbcSMatt Macy strftime(timestr, 128, "%c", &t) != 0) { 1618*eda14cbcSMatt Macy if (dryrun) { 1619*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1620*eda14cbcSMatt Macy "Would be able to return %s " 1621*eda14cbcSMatt Macy "to its state as of %s.\n"), 1622*eda14cbcSMatt Macy name, timestr); 1623*eda14cbcSMatt Macy } else { 1624*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1625*eda14cbcSMatt Macy "Pool %s returned to its state as of %s.\n"), 1626*eda14cbcSMatt Macy name, timestr); 1627*eda14cbcSMatt Macy } 1628*eda14cbcSMatt Macy if (loss > 120) { 1629*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1630*eda14cbcSMatt Macy "%s approximately %lld "), 1631*eda14cbcSMatt Macy dryrun ? "Would discard" : "Discarded", 1632*eda14cbcSMatt Macy ((longlong_t)loss + 30) / 60); 1633*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1634*eda14cbcSMatt Macy "minutes of transactions.\n")); 1635*eda14cbcSMatt Macy } else if (loss > 0) { 1636*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1637*eda14cbcSMatt Macy "%s approximately %lld "), 1638*eda14cbcSMatt Macy dryrun ? "Would discard" : "Discarded", 1639*eda14cbcSMatt Macy (longlong_t)loss); 1640*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1641*eda14cbcSMatt Macy "seconds of transactions.\n")); 1642*eda14cbcSMatt Macy } 1643*eda14cbcSMatt Macy } 1644*eda14cbcSMatt Macy } 1645*eda14cbcSMatt Macy 1646*eda14cbcSMatt Macy void 1647*eda14cbcSMatt Macy zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1648*eda14cbcSMatt Macy nvlist_t *config) 1649*eda14cbcSMatt Macy { 1650*eda14cbcSMatt Macy nvlist_t *nv = NULL; 1651*eda14cbcSMatt Macy int64_t loss = -1; 1652*eda14cbcSMatt Macy uint64_t edata = UINT64_MAX; 1653*eda14cbcSMatt Macy uint64_t rewindto; 1654*eda14cbcSMatt Macy struct tm t; 1655*eda14cbcSMatt Macy char timestr[128]; 1656*eda14cbcSMatt Macy 1657*eda14cbcSMatt Macy if (!hdl->libzfs_printerr) 1658*eda14cbcSMatt Macy return; 1659*eda14cbcSMatt Macy 1660*eda14cbcSMatt Macy if (reason >= 0) 1661*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1662*eda14cbcSMatt Macy else 1663*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1664*eda14cbcSMatt Macy 1665*eda14cbcSMatt Macy /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 1666*eda14cbcSMatt Macy if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1667*eda14cbcSMatt Macy nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 1668*eda14cbcSMatt Macy nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1669*eda14cbcSMatt Macy goto no_info; 1670*eda14cbcSMatt Macy 1671*eda14cbcSMatt Macy (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1672*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1673*eda14cbcSMatt Macy &edata); 1674*eda14cbcSMatt Macy 1675*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1676*eda14cbcSMatt Macy "Recovery is possible, but will result in some data loss.\n")); 1677*eda14cbcSMatt Macy 1678*eda14cbcSMatt Macy if (localtime_r((time_t *)&rewindto, &t) != NULL && 1679*eda14cbcSMatt Macy strftime(timestr, 128, "%c", &t) != 0) { 1680*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1681*eda14cbcSMatt Macy "\tReturning the pool to its state as of %s\n" 1682*eda14cbcSMatt Macy "\tshould correct the problem. "), 1683*eda14cbcSMatt Macy timestr); 1684*eda14cbcSMatt Macy } else { 1685*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1686*eda14cbcSMatt Macy "\tReverting the pool to an earlier state " 1687*eda14cbcSMatt Macy "should correct the problem.\n\t")); 1688*eda14cbcSMatt Macy } 1689*eda14cbcSMatt Macy 1690*eda14cbcSMatt Macy if (loss > 120) { 1691*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1692*eda14cbcSMatt Macy "Approximately %lld minutes of data\n" 1693*eda14cbcSMatt Macy "\tmust be discarded, irreversibly. "), 1694*eda14cbcSMatt Macy ((longlong_t)loss + 30) / 60); 1695*eda14cbcSMatt Macy } else if (loss > 0) { 1696*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1697*eda14cbcSMatt Macy "Approximately %lld seconds of data\n" 1698*eda14cbcSMatt Macy "\tmust be discarded, irreversibly. "), 1699*eda14cbcSMatt Macy (longlong_t)loss); 1700*eda14cbcSMatt Macy } 1701*eda14cbcSMatt Macy if (edata != 0 && edata != UINT64_MAX) { 1702*eda14cbcSMatt Macy if (edata == 1) { 1703*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1704*eda14cbcSMatt Macy "After rewind, at least\n" 1705*eda14cbcSMatt Macy "\tone persistent user-data error will remain. ")); 1706*eda14cbcSMatt Macy } else { 1707*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1708*eda14cbcSMatt Macy "After rewind, several\n" 1709*eda14cbcSMatt Macy "\tpersistent user-data errors will remain. ")); 1710*eda14cbcSMatt Macy } 1711*eda14cbcSMatt Macy } 1712*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1713*eda14cbcSMatt Macy "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1714*eda14cbcSMatt Macy reason >= 0 ? "clear" : "import", name); 1715*eda14cbcSMatt Macy 1716*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1717*eda14cbcSMatt Macy "A scrub of the pool\n" 1718*eda14cbcSMatt Macy "\tis strongly recommended after recovery.\n")); 1719*eda14cbcSMatt Macy return; 1720*eda14cbcSMatt Macy 1721*eda14cbcSMatt Macy no_info: 1722*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1723*eda14cbcSMatt Macy "Destroy and re-create the pool from\n\ta backup source.\n")); 1724*eda14cbcSMatt Macy } 1725*eda14cbcSMatt Macy 1726*eda14cbcSMatt Macy /* 1727*eda14cbcSMatt Macy * zpool_import() is a contracted interface. Should be kept the same 1728*eda14cbcSMatt Macy * if possible. 1729*eda14cbcSMatt Macy * 1730*eda14cbcSMatt Macy * Applications should use zpool_import_props() to import a pool with 1731*eda14cbcSMatt Macy * new properties value to be set. 1732*eda14cbcSMatt Macy */ 1733*eda14cbcSMatt Macy int 1734*eda14cbcSMatt Macy zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1735*eda14cbcSMatt Macy char *altroot) 1736*eda14cbcSMatt Macy { 1737*eda14cbcSMatt Macy nvlist_t *props = NULL; 1738*eda14cbcSMatt Macy int ret; 1739*eda14cbcSMatt Macy 1740*eda14cbcSMatt Macy if (altroot != NULL) { 1741*eda14cbcSMatt Macy if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1742*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_NOMEM, 1743*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1744*eda14cbcSMatt Macy newname)); 1745*eda14cbcSMatt Macy } 1746*eda14cbcSMatt Macy 1747*eda14cbcSMatt Macy if (nvlist_add_string(props, 1748*eda14cbcSMatt Macy zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1749*eda14cbcSMatt Macy nvlist_add_string(props, 1750*eda14cbcSMatt Macy zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1751*eda14cbcSMatt Macy nvlist_free(props); 1752*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_NOMEM, 1753*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1754*eda14cbcSMatt Macy newname)); 1755*eda14cbcSMatt Macy } 1756*eda14cbcSMatt Macy } 1757*eda14cbcSMatt Macy 1758*eda14cbcSMatt Macy ret = zpool_import_props(hdl, config, newname, props, 1759*eda14cbcSMatt Macy ZFS_IMPORT_NORMAL); 1760*eda14cbcSMatt Macy nvlist_free(props); 1761*eda14cbcSMatt Macy return (ret); 1762*eda14cbcSMatt Macy } 1763*eda14cbcSMatt Macy 1764*eda14cbcSMatt Macy static void 1765*eda14cbcSMatt Macy print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 1766*eda14cbcSMatt Macy int indent) 1767*eda14cbcSMatt Macy { 1768*eda14cbcSMatt Macy nvlist_t **child; 1769*eda14cbcSMatt Macy uint_t c, children; 1770*eda14cbcSMatt Macy char *vname; 1771*eda14cbcSMatt Macy uint64_t is_log = 0; 1772*eda14cbcSMatt Macy 1773*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 1774*eda14cbcSMatt Macy &is_log); 1775*eda14cbcSMatt Macy 1776*eda14cbcSMatt Macy if (name != NULL) 1777*eda14cbcSMatt Macy (void) printf("\t%*s%s%s\n", indent, "", name, 1778*eda14cbcSMatt Macy is_log ? " [log]" : ""); 1779*eda14cbcSMatt Macy 1780*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 1781*eda14cbcSMatt Macy &child, &children) != 0) 1782*eda14cbcSMatt Macy return; 1783*eda14cbcSMatt Macy 1784*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 1785*eda14cbcSMatt Macy vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID); 1786*eda14cbcSMatt Macy print_vdev_tree(hdl, vname, child[c], indent + 2); 1787*eda14cbcSMatt Macy free(vname); 1788*eda14cbcSMatt Macy } 1789*eda14cbcSMatt Macy } 1790*eda14cbcSMatt Macy 1791*eda14cbcSMatt Macy void 1792*eda14cbcSMatt Macy zpool_print_unsup_feat(nvlist_t *config) 1793*eda14cbcSMatt Macy { 1794*eda14cbcSMatt Macy nvlist_t *nvinfo, *unsup_feat; 1795*eda14cbcSMatt Macy nvpair_t *nvp; 1796*eda14cbcSMatt Macy 1797*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1798*eda14cbcSMatt Macy 0); 1799*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1800*eda14cbcSMatt Macy &unsup_feat) == 0); 1801*eda14cbcSMatt Macy 1802*eda14cbcSMatt Macy for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1803*eda14cbcSMatt Macy nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1804*eda14cbcSMatt Macy char *desc; 1805*eda14cbcSMatt Macy 1806*eda14cbcSMatt Macy verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1807*eda14cbcSMatt Macy verify(nvpair_value_string(nvp, &desc) == 0); 1808*eda14cbcSMatt Macy 1809*eda14cbcSMatt Macy if (strlen(desc) > 0) 1810*eda14cbcSMatt Macy (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1811*eda14cbcSMatt Macy else 1812*eda14cbcSMatt Macy (void) printf("\t%s\n", nvpair_name(nvp)); 1813*eda14cbcSMatt Macy } 1814*eda14cbcSMatt Macy } 1815*eda14cbcSMatt Macy 1816*eda14cbcSMatt Macy /* 1817*eda14cbcSMatt Macy * Import the given pool using the known configuration and a list of 1818*eda14cbcSMatt Macy * properties to be set. The configuration should have come from 1819*eda14cbcSMatt Macy * zpool_find_import(). The 'newname' parameters control whether the pool 1820*eda14cbcSMatt Macy * is imported with a different name. 1821*eda14cbcSMatt Macy */ 1822*eda14cbcSMatt Macy int 1823*eda14cbcSMatt Macy zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1824*eda14cbcSMatt Macy nvlist_t *props, int flags) 1825*eda14cbcSMatt Macy { 1826*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1827*eda14cbcSMatt Macy zpool_load_policy_t policy; 1828*eda14cbcSMatt Macy nvlist_t *nv = NULL; 1829*eda14cbcSMatt Macy nvlist_t *nvinfo = NULL; 1830*eda14cbcSMatt Macy nvlist_t *missing = NULL; 1831*eda14cbcSMatt Macy char *thename; 1832*eda14cbcSMatt Macy char *origname; 1833*eda14cbcSMatt Macy int ret; 1834*eda14cbcSMatt Macy int error = 0; 1835*eda14cbcSMatt Macy char errbuf[1024]; 1836*eda14cbcSMatt Macy 1837*eda14cbcSMatt Macy verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1838*eda14cbcSMatt Macy &origname) == 0); 1839*eda14cbcSMatt Macy 1840*eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1841*eda14cbcSMatt Macy "cannot import pool '%s'"), origname); 1842*eda14cbcSMatt Macy 1843*eda14cbcSMatt Macy if (newname != NULL) { 1844*eda14cbcSMatt Macy if (!zpool_name_valid(hdl, B_FALSE, newname)) 1845*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 1846*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1847*eda14cbcSMatt Macy newname)); 1848*eda14cbcSMatt Macy thename = (char *)newname; 1849*eda14cbcSMatt Macy } else { 1850*eda14cbcSMatt Macy thename = origname; 1851*eda14cbcSMatt Macy } 1852*eda14cbcSMatt Macy 1853*eda14cbcSMatt Macy if (props != NULL) { 1854*eda14cbcSMatt Macy uint64_t version; 1855*eda14cbcSMatt Macy prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1856*eda14cbcSMatt Macy 1857*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1858*eda14cbcSMatt Macy &version) == 0); 1859*eda14cbcSMatt Macy 1860*eda14cbcSMatt Macy if ((props = zpool_valid_proplist(hdl, origname, 1861*eda14cbcSMatt Macy props, version, flags, errbuf)) == NULL) 1862*eda14cbcSMatt Macy return (-1); 1863*eda14cbcSMatt Macy if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1864*eda14cbcSMatt Macy nvlist_free(props); 1865*eda14cbcSMatt Macy return (-1); 1866*eda14cbcSMatt Macy } 1867*eda14cbcSMatt Macy nvlist_free(props); 1868*eda14cbcSMatt Macy } 1869*eda14cbcSMatt Macy 1870*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1871*eda14cbcSMatt Macy 1872*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1873*eda14cbcSMatt Macy &zc.zc_guid) == 0); 1874*eda14cbcSMatt Macy 1875*eda14cbcSMatt Macy if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1876*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1877*eda14cbcSMatt Macy return (-1); 1878*eda14cbcSMatt Macy } 1879*eda14cbcSMatt Macy if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1880*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1881*eda14cbcSMatt Macy return (-1); 1882*eda14cbcSMatt Macy } 1883*eda14cbcSMatt Macy 1884*eda14cbcSMatt Macy zc.zc_cookie = flags; 1885*eda14cbcSMatt Macy while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 1886*eda14cbcSMatt Macy errno == ENOMEM) { 1887*eda14cbcSMatt Macy if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 1888*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1889*eda14cbcSMatt Macy return (-1); 1890*eda14cbcSMatt Macy } 1891*eda14cbcSMatt Macy } 1892*eda14cbcSMatt Macy if (ret != 0) 1893*eda14cbcSMatt Macy error = errno; 1894*eda14cbcSMatt Macy 1895*eda14cbcSMatt Macy (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1896*eda14cbcSMatt Macy 1897*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 1898*eda14cbcSMatt Macy 1899*eda14cbcSMatt Macy zpool_get_load_policy(config, &policy); 1900*eda14cbcSMatt Macy 1901*eda14cbcSMatt Macy if (error) { 1902*eda14cbcSMatt Macy char desc[1024]; 1903*eda14cbcSMatt Macy char aux[256]; 1904*eda14cbcSMatt Macy 1905*eda14cbcSMatt Macy /* 1906*eda14cbcSMatt Macy * Dry-run failed, but we print out what success 1907*eda14cbcSMatt Macy * looks like if we found a best txg 1908*eda14cbcSMatt Macy */ 1909*eda14cbcSMatt Macy if (policy.zlp_rewind & ZPOOL_TRY_REWIND) { 1910*eda14cbcSMatt Macy zpool_rewind_exclaim(hdl, newname ? origname : thename, 1911*eda14cbcSMatt Macy B_TRUE, nv); 1912*eda14cbcSMatt Macy nvlist_free(nv); 1913*eda14cbcSMatt Macy return (-1); 1914*eda14cbcSMatt Macy } 1915*eda14cbcSMatt Macy 1916*eda14cbcSMatt Macy if (newname == NULL) 1917*eda14cbcSMatt Macy (void) snprintf(desc, sizeof (desc), 1918*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1919*eda14cbcSMatt Macy thename); 1920*eda14cbcSMatt Macy else 1921*eda14cbcSMatt Macy (void) snprintf(desc, sizeof (desc), 1922*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1923*eda14cbcSMatt Macy origname, thename); 1924*eda14cbcSMatt Macy 1925*eda14cbcSMatt Macy switch (error) { 1926*eda14cbcSMatt Macy case ENOTSUP: 1927*eda14cbcSMatt Macy if (nv != NULL && nvlist_lookup_nvlist(nv, 1928*eda14cbcSMatt Macy ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1929*eda14cbcSMatt Macy nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1930*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, "This " 1931*eda14cbcSMatt Macy "pool uses the following feature(s) not " 1932*eda14cbcSMatt Macy "supported by this system:\n")); 1933*eda14cbcSMatt Macy zpool_print_unsup_feat(nv); 1934*eda14cbcSMatt Macy if (nvlist_exists(nvinfo, 1935*eda14cbcSMatt Macy ZPOOL_CONFIG_CAN_RDONLY)) { 1936*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 1937*eda14cbcSMatt Macy "All unsupported features are only " 1938*eda14cbcSMatt Macy "required for writing to the pool." 1939*eda14cbcSMatt Macy "\nThe pool can be imported using " 1940*eda14cbcSMatt Macy "'-o readonly=on'.\n")); 1941*eda14cbcSMatt Macy } 1942*eda14cbcSMatt Macy } 1943*eda14cbcSMatt Macy /* 1944*eda14cbcSMatt Macy * Unsupported version. 1945*eda14cbcSMatt Macy */ 1946*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1947*eda14cbcSMatt Macy break; 1948*eda14cbcSMatt Macy 1949*eda14cbcSMatt Macy case EREMOTEIO: 1950*eda14cbcSMatt Macy if (nv != NULL && nvlist_lookup_nvlist(nv, 1951*eda14cbcSMatt Macy ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) { 1952*eda14cbcSMatt Macy char *hostname = "<unknown>"; 1953*eda14cbcSMatt Macy uint64_t hostid = 0; 1954*eda14cbcSMatt Macy mmp_state_t mmp_state; 1955*eda14cbcSMatt Macy 1956*eda14cbcSMatt Macy mmp_state = fnvlist_lookup_uint64(nvinfo, 1957*eda14cbcSMatt Macy ZPOOL_CONFIG_MMP_STATE); 1958*eda14cbcSMatt Macy 1959*eda14cbcSMatt Macy if (nvlist_exists(nvinfo, 1960*eda14cbcSMatt Macy ZPOOL_CONFIG_MMP_HOSTNAME)) 1961*eda14cbcSMatt Macy hostname = fnvlist_lookup_string(nvinfo, 1962*eda14cbcSMatt Macy ZPOOL_CONFIG_MMP_HOSTNAME); 1963*eda14cbcSMatt Macy 1964*eda14cbcSMatt Macy if (nvlist_exists(nvinfo, 1965*eda14cbcSMatt Macy ZPOOL_CONFIG_MMP_HOSTID)) 1966*eda14cbcSMatt Macy hostid = fnvlist_lookup_uint64(nvinfo, 1967*eda14cbcSMatt Macy ZPOOL_CONFIG_MMP_HOSTID); 1968*eda14cbcSMatt Macy 1969*eda14cbcSMatt Macy if (mmp_state == MMP_STATE_ACTIVE) { 1970*eda14cbcSMatt Macy (void) snprintf(aux, sizeof (aux), 1971*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "pool is imp" 1972*eda14cbcSMatt Macy "orted on host '%s' (hostid=%lx).\n" 1973*eda14cbcSMatt Macy "Export the pool on the other " 1974*eda14cbcSMatt Macy "system, then run 'zpool import'."), 1975*eda14cbcSMatt Macy hostname, (unsigned long) hostid); 1976*eda14cbcSMatt Macy } else if (mmp_state == MMP_STATE_NO_HOSTID) { 1977*eda14cbcSMatt Macy (void) snprintf(aux, sizeof (aux), 1978*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "pool has " 1979*eda14cbcSMatt Macy "the multihost property on and " 1980*eda14cbcSMatt Macy "the\nsystem's hostid is not set. " 1981*eda14cbcSMatt Macy "Set a unique system hostid with " 1982*eda14cbcSMatt Macy "the zgenhostid(8) command.\n")); 1983*eda14cbcSMatt Macy } 1984*eda14cbcSMatt Macy 1985*eda14cbcSMatt Macy (void) zfs_error_aux(hdl, aux); 1986*eda14cbcSMatt Macy } 1987*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc); 1988*eda14cbcSMatt Macy break; 1989*eda14cbcSMatt Macy 1990*eda14cbcSMatt Macy case EINVAL: 1991*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1992*eda14cbcSMatt Macy break; 1993*eda14cbcSMatt Macy 1994*eda14cbcSMatt Macy case EROFS: 1995*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1996*eda14cbcSMatt Macy "one or more devices is read only")); 1997*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, desc); 1998*eda14cbcSMatt Macy break; 1999*eda14cbcSMatt Macy 2000*eda14cbcSMatt Macy case ENXIO: 2001*eda14cbcSMatt Macy if (nv && nvlist_lookup_nvlist(nv, 2002*eda14cbcSMatt Macy ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 2003*eda14cbcSMatt Macy nvlist_lookup_nvlist(nvinfo, 2004*eda14cbcSMatt Macy ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 2005*eda14cbcSMatt Macy (void) printf(dgettext(TEXT_DOMAIN, 2006*eda14cbcSMatt Macy "The devices below are missing or " 2007*eda14cbcSMatt Macy "corrupted, use '-m' to import the pool " 2008*eda14cbcSMatt Macy "anyway:\n")); 2009*eda14cbcSMatt Macy print_vdev_tree(hdl, NULL, missing, 2); 2010*eda14cbcSMatt Macy (void) printf("\n"); 2011*eda14cbcSMatt Macy } 2012*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, error, desc); 2013*eda14cbcSMatt Macy break; 2014*eda14cbcSMatt Macy 2015*eda14cbcSMatt Macy case EEXIST: 2016*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, error, desc); 2017*eda14cbcSMatt Macy break; 2018*eda14cbcSMatt Macy 2019*eda14cbcSMatt Macy case EBUSY: 2020*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2021*eda14cbcSMatt Macy "one or more devices are already in use\n")); 2022*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, desc); 2023*eda14cbcSMatt Macy break; 2024*eda14cbcSMatt Macy case ENAMETOOLONG: 2025*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2026*eda14cbcSMatt Macy "new name of at least one dataset is longer than " 2027*eda14cbcSMatt Macy "the maximum allowable length")); 2028*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 2029*eda14cbcSMatt Macy break; 2030*eda14cbcSMatt Macy default: 2031*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, error, desc); 2032*eda14cbcSMatt Macy zpool_explain_recover(hdl, 2033*eda14cbcSMatt Macy newname ? origname : thename, -error, nv); 2034*eda14cbcSMatt Macy break; 2035*eda14cbcSMatt Macy } 2036*eda14cbcSMatt Macy 2037*eda14cbcSMatt Macy nvlist_free(nv); 2038*eda14cbcSMatt Macy ret = -1; 2039*eda14cbcSMatt Macy } else { 2040*eda14cbcSMatt Macy zpool_handle_t *zhp; 2041*eda14cbcSMatt Macy 2042*eda14cbcSMatt Macy /* 2043*eda14cbcSMatt Macy * This should never fail, but play it safe anyway. 2044*eda14cbcSMatt Macy */ 2045*eda14cbcSMatt Macy if (zpool_open_silent(hdl, thename, &zhp) != 0) 2046*eda14cbcSMatt Macy ret = -1; 2047*eda14cbcSMatt Macy else if (zhp != NULL) 2048*eda14cbcSMatt Macy zpool_close(zhp); 2049*eda14cbcSMatt Macy if (policy.zlp_rewind & 2050*eda14cbcSMatt Macy (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 2051*eda14cbcSMatt Macy zpool_rewind_exclaim(hdl, newname ? origname : thename, 2052*eda14cbcSMatt Macy ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv); 2053*eda14cbcSMatt Macy } 2054*eda14cbcSMatt Macy nvlist_free(nv); 2055*eda14cbcSMatt Macy return (0); 2056*eda14cbcSMatt Macy } 2057*eda14cbcSMatt Macy 2058*eda14cbcSMatt Macy return (ret); 2059*eda14cbcSMatt Macy } 2060*eda14cbcSMatt Macy 2061*eda14cbcSMatt Macy /* 2062*eda14cbcSMatt Macy * Translate vdev names to guids. If a vdev_path is determined to be 2063*eda14cbcSMatt Macy * unsuitable then a vd_errlist is allocated and the vdev path and errno 2064*eda14cbcSMatt Macy * are added to it. 2065*eda14cbcSMatt Macy */ 2066*eda14cbcSMatt Macy static int 2067*eda14cbcSMatt Macy zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds, 2068*eda14cbcSMatt Macy nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist) 2069*eda14cbcSMatt Macy { 2070*eda14cbcSMatt Macy nvlist_t *errlist = NULL; 2071*eda14cbcSMatt Macy int error = 0; 2072*eda14cbcSMatt Macy 2073*eda14cbcSMatt Macy for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL; 2074*eda14cbcSMatt Macy elem = nvlist_next_nvpair(vds, elem)) { 2075*eda14cbcSMatt Macy boolean_t spare, cache; 2076*eda14cbcSMatt Macy 2077*eda14cbcSMatt Macy char *vd_path = nvpair_name(elem); 2078*eda14cbcSMatt Macy nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache, 2079*eda14cbcSMatt Macy NULL); 2080*eda14cbcSMatt Macy 2081*eda14cbcSMatt Macy if ((tgt == NULL) || cache || spare) { 2082*eda14cbcSMatt Macy if (errlist == NULL) { 2083*eda14cbcSMatt Macy errlist = fnvlist_alloc(); 2084*eda14cbcSMatt Macy error = EINVAL; 2085*eda14cbcSMatt Macy } 2086*eda14cbcSMatt Macy 2087*eda14cbcSMatt Macy uint64_t err = (tgt == NULL) ? EZFS_NODEVICE : 2088*eda14cbcSMatt Macy (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE); 2089*eda14cbcSMatt Macy fnvlist_add_int64(errlist, vd_path, err); 2090*eda14cbcSMatt Macy continue; 2091*eda14cbcSMatt Macy } 2092*eda14cbcSMatt Macy 2093*eda14cbcSMatt Macy uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 2094*eda14cbcSMatt Macy fnvlist_add_uint64(vdev_guids, vd_path, guid); 2095*eda14cbcSMatt Macy 2096*eda14cbcSMatt Macy char msg[MAXNAMELEN]; 2097*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid); 2098*eda14cbcSMatt Macy fnvlist_add_string(guids_to_paths, msg, vd_path); 2099*eda14cbcSMatt Macy } 2100*eda14cbcSMatt Macy 2101*eda14cbcSMatt Macy if (error != 0) { 2102*eda14cbcSMatt Macy verify(errlist != NULL); 2103*eda14cbcSMatt Macy if (vd_errlist != NULL) 2104*eda14cbcSMatt Macy *vd_errlist = errlist; 2105*eda14cbcSMatt Macy else 2106*eda14cbcSMatt Macy fnvlist_free(errlist); 2107*eda14cbcSMatt Macy } 2108*eda14cbcSMatt Macy 2109*eda14cbcSMatt Macy return (error); 2110*eda14cbcSMatt Macy } 2111*eda14cbcSMatt Macy 2112*eda14cbcSMatt Macy static int 2113*eda14cbcSMatt Macy xlate_init_err(int err) 2114*eda14cbcSMatt Macy { 2115*eda14cbcSMatt Macy switch (err) { 2116*eda14cbcSMatt Macy case ENODEV: 2117*eda14cbcSMatt Macy return (EZFS_NODEVICE); 2118*eda14cbcSMatt Macy case EINVAL: 2119*eda14cbcSMatt Macy case EROFS: 2120*eda14cbcSMatt Macy return (EZFS_BADDEV); 2121*eda14cbcSMatt Macy case EBUSY: 2122*eda14cbcSMatt Macy return (EZFS_INITIALIZING); 2123*eda14cbcSMatt Macy case ESRCH: 2124*eda14cbcSMatt Macy return (EZFS_NO_INITIALIZE); 2125*eda14cbcSMatt Macy } 2126*eda14cbcSMatt Macy return (err); 2127*eda14cbcSMatt Macy } 2128*eda14cbcSMatt Macy 2129*eda14cbcSMatt Macy /* 2130*eda14cbcSMatt Macy * Begin, suspend, or cancel the initialization (initializing of all free 2131*eda14cbcSMatt Macy * blocks) for the given vdevs in the given pool. 2132*eda14cbcSMatt Macy */ 2133*eda14cbcSMatt Macy static int 2134*eda14cbcSMatt Macy zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2135*eda14cbcSMatt Macy nvlist_t *vds, boolean_t wait) 2136*eda14cbcSMatt Macy { 2137*eda14cbcSMatt Macy int err; 2138*eda14cbcSMatt Macy 2139*eda14cbcSMatt Macy nvlist_t *vdev_guids = fnvlist_alloc(); 2140*eda14cbcSMatt Macy nvlist_t *guids_to_paths = fnvlist_alloc(); 2141*eda14cbcSMatt Macy nvlist_t *vd_errlist = NULL; 2142*eda14cbcSMatt Macy nvlist_t *errlist; 2143*eda14cbcSMatt Macy nvpair_t *elem; 2144*eda14cbcSMatt Macy 2145*eda14cbcSMatt Macy err = zpool_translate_vdev_guids(zhp, vds, vdev_guids, 2146*eda14cbcSMatt Macy guids_to_paths, &vd_errlist); 2147*eda14cbcSMatt Macy 2148*eda14cbcSMatt Macy if (err != 0) { 2149*eda14cbcSMatt Macy verify(vd_errlist != NULL); 2150*eda14cbcSMatt Macy goto list_errors; 2151*eda14cbcSMatt Macy } 2152*eda14cbcSMatt Macy 2153*eda14cbcSMatt Macy err = lzc_initialize(zhp->zpool_name, cmd_type, 2154*eda14cbcSMatt Macy vdev_guids, &errlist); 2155*eda14cbcSMatt Macy 2156*eda14cbcSMatt Macy if (err != 0) { 2157*eda14cbcSMatt Macy if (errlist != NULL) { 2158*eda14cbcSMatt Macy vd_errlist = fnvlist_lookup_nvlist(errlist, 2159*eda14cbcSMatt Macy ZPOOL_INITIALIZE_VDEVS); 2160*eda14cbcSMatt Macy goto list_errors; 2161*eda14cbcSMatt Macy } 2162*eda14cbcSMatt Macy (void) zpool_standard_error(zhp->zpool_hdl, err, 2163*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "operation failed")); 2164*eda14cbcSMatt Macy goto out; 2165*eda14cbcSMatt Macy } 2166*eda14cbcSMatt Macy 2167*eda14cbcSMatt Macy if (wait) { 2168*eda14cbcSMatt Macy for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL; 2169*eda14cbcSMatt Macy elem = nvlist_next_nvpair(vdev_guids, elem)) { 2170*eda14cbcSMatt Macy 2171*eda14cbcSMatt Macy uint64_t guid = fnvpair_value_uint64(elem); 2172*eda14cbcSMatt Macy 2173*eda14cbcSMatt Macy err = lzc_wait_tag(zhp->zpool_name, 2174*eda14cbcSMatt Macy ZPOOL_WAIT_INITIALIZE, guid, NULL); 2175*eda14cbcSMatt Macy if (err != 0) { 2176*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, 2177*eda14cbcSMatt Macy err, dgettext(TEXT_DOMAIN, "error " 2178*eda14cbcSMatt Macy "waiting for '%s' to initialize"), 2179*eda14cbcSMatt Macy nvpair_name(elem)); 2180*eda14cbcSMatt Macy 2181*eda14cbcSMatt Macy goto out; 2182*eda14cbcSMatt Macy } 2183*eda14cbcSMatt Macy } 2184*eda14cbcSMatt Macy } 2185*eda14cbcSMatt Macy goto out; 2186*eda14cbcSMatt Macy 2187*eda14cbcSMatt Macy list_errors: 2188*eda14cbcSMatt Macy for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL; 2189*eda14cbcSMatt Macy elem = nvlist_next_nvpair(vd_errlist, elem)) { 2190*eda14cbcSMatt Macy int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem)); 2191*eda14cbcSMatt Macy char *path; 2192*eda14cbcSMatt Macy 2193*eda14cbcSMatt Macy if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem), 2194*eda14cbcSMatt Macy &path) != 0) 2195*eda14cbcSMatt Macy path = nvpair_name(elem); 2196*eda14cbcSMatt Macy 2197*eda14cbcSMatt Macy (void) zfs_error_fmt(zhp->zpool_hdl, vd_error, 2198*eda14cbcSMatt Macy "cannot initialize '%s'", path); 2199*eda14cbcSMatt Macy } 2200*eda14cbcSMatt Macy 2201*eda14cbcSMatt Macy out: 2202*eda14cbcSMatt Macy fnvlist_free(vdev_guids); 2203*eda14cbcSMatt Macy fnvlist_free(guids_to_paths); 2204*eda14cbcSMatt Macy 2205*eda14cbcSMatt Macy if (vd_errlist != NULL) 2206*eda14cbcSMatt Macy fnvlist_free(vd_errlist); 2207*eda14cbcSMatt Macy 2208*eda14cbcSMatt Macy return (err == 0 ? 0 : -1); 2209*eda14cbcSMatt Macy } 2210*eda14cbcSMatt Macy 2211*eda14cbcSMatt Macy int 2212*eda14cbcSMatt Macy zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2213*eda14cbcSMatt Macy nvlist_t *vds) 2214*eda14cbcSMatt Macy { 2215*eda14cbcSMatt Macy return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE)); 2216*eda14cbcSMatt Macy } 2217*eda14cbcSMatt Macy 2218*eda14cbcSMatt Macy int 2219*eda14cbcSMatt Macy zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2220*eda14cbcSMatt Macy nvlist_t *vds) 2221*eda14cbcSMatt Macy { 2222*eda14cbcSMatt Macy return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE)); 2223*eda14cbcSMatt Macy } 2224*eda14cbcSMatt Macy 2225*eda14cbcSMatt Macy static int 2226*eda14cbcSMatt Macy xlate_trim_err(int err) 2227*eda14cbcSMatt Macy { 2228*eda14cbcSMatt Macy switch (err) { 2229*eda14cbcSMatt Macy case ENODEV: 2230*eda14cbcSMatt Macy return (EZFS_NODEVICE); 2231*eda14cbcSMatt Macy case EINVAL: 2232*eda14cbcSMatt Macy case EROFS: 2233*eda14cbcSMatt Macy return (EZFS_BADDEV); 2234*eda14cbcSMatt Macy case EBUSY: 2235*eda14cbcSMatt Macy return (EZFS_TRIMMING); 2236*eda14cbcSMatt Macy case ESRCH: 2237*eda14cbcSMatt Macy return (EZFS_NO_TRIM); 2238*eda14cbcSMatt Macy case EOPNOTSUPP: 2239*eda14cbcSMatt Macy return (EZFS_TRIM_NOTSUP); 2240*eda14cbcSMatt Macy } 2241*eda14cbcSMatt Macy return (err); 2242*eda14cbcSMatt Macy } 2243*eda14cbcSMatt Macy 2244*eda14cbcSMatt Macy static int 2245*eda14cbcSMatt Macy zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids) 2246*eda14cbcSMatt Macy { 2247*eda14cbcSMatt Macy int err; 2248*eda14cbcSMatt Macy nvpair_t *elem; 2249*eda14cbcSMatt Macy 2250*eda14cbcSMatt Macy for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL; 2251*eda14cbcSMatt Macy elem = nvlist_next_nvpair(vdev_guids, elem)) { 2252*eda14cbcSMatt Macy 2253*eda14cbcSMatt Macy uint64_t guid = fnvpair_value_uint64(elem); 2254*eda14cbcSMatt Macy 2255*eda14cbcSMatt Macy err = lzc_wait_tag(zhp->zpool_name, 2256*eda14cbcSMatt Macy ZPOOL_WAIT_TRIM, guid, NULL); 2257*eda14cbcSMatt Macy if (err != 0) { 2258*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, 2259*eda14cbcSMatt Macy err, dgettext(TEXT_DOMAIN, "error " 2260*eda14cbcSMatt Macy "waiting to trim '%s'"), nvpair_name(elem)); 2261*eda14cbcSMatt Macy 2262*eda14cbcSMatt Macy return (err); 2263*eda14cbcSMatt Macy } 2264*eda14cbcSMatt Macy } 2265*eda14cbcSMatt Macy return (0); 2266*eda14cbcSMatt Macy } 2267*eda14cbcSMatt Macy 2268*eda14cbcSMatt Macy /* 2269*eda14cbcSMatt Macy * Check errlist and report any errors, omitting ones which should be 2270*eda14cbcSMatt Macy * suppressed. Returns B_TRUE if any errors were reported. 2271*eda14cbcSMatt Macy */ 2272*eda14cbcSMatt Macy static boolean_t 2273*eda14cbcSMatt Macy check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags, 2274*eda14cbcSMatt Macy nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist) 2275*eda14cbcSMatt Macy { 2276*eda14cbcSMatt Macy nvpair_t *elem; 2277*eda14cbcSMatt Macy boolean_t reported_errs = B_FALSE; 2278*eda14cbcSMatt Macy int num_vds = 0; 2279*eda14cbcSMatt Macy int num_suppressed_errs = 0; 2280*eda14cbcSMatt Macy 2281*eda14cbcSMatt Macy for (elem = nvlist_next_nvpair(vds, NULL); 2282*eda14cbcSMatt Macy elem != NULL; elem = nvlist_next_nvpair(vds, elem)) { 2283*eda14cbcSMatt Macy num_vds++; 2284*eda14cbcSMatt Macy } 2285*eda14cbcSMatt Macy 2286*eda14cbcSMatt Macy for (elem = nvlist_next_nvpair(errlist, NULL); 2287*eda14cbcSMatt Macy elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) { 2288*eda14cbcSMatt Macy int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem)); 2289*eda14cbcSMatt Macy char *path; 2290*eda14cbcSMatt Macy 2291*eda14cbcSMatt Macy /* 2292*eda14cbcSMatt Macy * If only the pool was specified, and it was not a secure 2293*eda14cbcSMatt Macy * trim then suppress warnings for individual vdevs which 2294*eda14cbcSMatt Macy * do not support trimming. 2295*eda14cbcSMatt Macy */ 2296*eda14cbcSMatt Macy if (vd_error == EZFS_TRIM_NOTSUP && 2297*eda14cbcSMatt Macy trim_flags->fullpool && 2298*eda14cbcSMatt Macy !trim_flags->secure) { 2299*eda14cbcSMatt Macy num_suppressed_errs++; 2300*eda14cbcSMatt Macy continue; 2301*eda14cbcSMatt Macy } 2302*eda14cbcSMatt Macy 2303*eda14cbcSMatt Macy reported_errs = B_TRUE; 2304*eda14cbcSMatt Macy if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem), 2305*eda14cbcSMatt Macy &path) != 0) 2306*eda14cbcSMatt Macy path = nvpair_name(elem); 2307*eda14cbcSMatt Macy 2308*eda14cbcSMatt Macy (void) zfs_error_fmt(zhp->zpool_hdl, vd_error, 2309*eda14cbcSMatt Macy "cannot trim '%s'", path); 2310*eda14cbcSMatt Macy } 2311*eda14cbcSMatt Macy 2312*eda14cbcSMatt Macy if (num_suppressed_errs == num_vds) { 2313*eda14cbcSMatt Macy (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 2314*eda14cbcSMatt Macy "no devices in pool support trim operations")); 2315*eda14cbcSMatt Macy (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP, 2316*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot trim"))); 2317*eda14cbcSMatt Macy reported_errs = B_TRUE; 2318*eda14cbcSMatt Macy } 2319*eda14cbcSMatt Macy 2320*eda14cbcSMatt Macy return (reported_errs); 2321*eda14cbcSMatt Macy } 2322*eda14cbcSMatt Macy 2323*eda14cbcSMatt Macy /* 2324*eda14cbcSMatt Macy * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for 2325*eda14cbcSMatt Macy * the given vdevs in the given pool. 2326*eda14cbcSMatt Macy */ 2327*eda14cbcSMatt Macy int 2328*eda14cbcSMatt Macy zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds, 2329*eda14cbcSMatt Macy trimflags_t *trim_flags) 2330*eda14cbcSMatt Macy { 2331*eda14cbcSMatt Macy int err; 2332*eda14cbcSMatt Macy int retval = 0; 2333*eda14cbcSMatt Macy 2334*eda14cbcSMatt Macy nvlist_t *vdev_guids = fnvlist_alloc(); 2335*eda14cbcSMatt Macy nvlist_t *guids_to_paths = fnvlist_alloc(); 2336*eda14cbcSMatt Macy nvlist_t *errlist = NULL; 2337*eda14cbcSMatt Macy 2338*eda14cbcSMatt Macy err = zpool_translate_vdev_guids(zhp, vds, vdev_guids, 2339*eda14cbcSMatt Macy guids_to_paths, &errlist); 2340*eda14cbcSMatt Macy if (err != 0) { 2341*eda14cbcSMatt Macy check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist); 2342*eda14cbcSMatt Macy retval = -1; 2343*eda14cbcSMatt Macy goto out; 2344*eda14cbcSMatt Macy } 2345*eda14cbcSMatt Macy 2346*eda14cbcSMatt Macy err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate, 2347*eda14cbcSMatt Macy trim_flags->secure, vdev_guids, &errlist); 2348*eda14cbcSMatt Macy if (err != 0) { 2349*eda14cbcSMatt Macy nvlist_t *vd_errlist; 2350*eda14cbcSMatt Macy if (errlist != NULL && nvlist_lookup_nvlist(errlist, 2351*eda14cbcSMatt Macy ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) { 2352*eda14cbcSMatt Macy if (check_trim_errs(zhp, trim_flags, guids_to_paths, 2353*eda14cbcSMatt Macy vds, vd_errlist)) { 2354*eda14cbcSMatt Macy retval = -1; 2355*eda14cbcSMatt Macy goto out; 2356*eda14cbcSMatt Macy } 2357*eda14cbcSMatt Macy } else { 2358*eda14cbcSMatt Macy char msg[1024]; 2359*eda14cbcSMatt Macy 2360*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 2361*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "operation failed")); 2362*eda14cbcSMatt Macy zpool_standard_error(zhp->zpool_hdl, err, msg); 2363*eda14cbcSMatt Macy retval = -1; 2364*eda14cbcSMatt Macy goto out; 2365*eda14cbcSMatt Macy } 2366*eda14cbcSMatt Macy } 2367*eda14cbcSMatt Macy 2368*eda14cbcSMatt Macy 2369*eda14cbcSMatt Macy if (trim_flags->wait) 2370*eda14cbcSMatt Macy retval = zpool_trim_wait(zhp, vdev_guids); 2371*eda14cbcSMatt Macy 2372*eda14cbcSMatt Macy out: 2373*eda14cbcSMatt Macy if (errlist != NULL) 2374*eda14cbcSMatt Macy fnvlist_free(errlist); 2375*eda14cbcSMatt Macy fnvlist_free(vdev_guids); 2376*eda14cbcSMatt Macy fnvlist_free(guids_to_paths); 2377*eda14cbcSMatt Macy return (retval); 2378*eda14cbcSMatt Macy } 2379*eda14cbcSMatt Macy 2380*eda14cbcSMatt Macy /* 2381*eda14cbcSMatt Macy * Scan the pool. 2382*eda14cbcSMatt Macy */ 2383*eda14cbcSMatt Macy int 2384*eda14cbcSMatt Macy zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) 2385*eda14cbcSMatt Macy { 2386*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 2387*eda14cbcSMatt Macy char msg[1024]; 2388*eda14cbcSMatt Macy int err; 2389*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 2390*eda14cbcSMatt Macy 2391*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2392*eda14cbcSMatt Macy zc.zc_cookie = func; 2393*eda14cbcSMatt Macy zc.zc_flags = cmd; 2394*eda14cbcSMatt Macy 2395*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0) 2396*eda14cbcSMatt Macy return (0); 2397*eda14cbcSMatt Macy 2398*eda14cbcSMatt Macy err = errno; 2399*eda14cbcSMatt Macy 2400*eda14cbcSMatt Macy /* ECANCELED on a scrub means we resumed a paused scrub */ 2401*eda14cbcSMatt Macy if (err == ECANCELED && func == POOL_SCAN_SCRUB && 2402*eda14cbcSMatt Macy cmd == POOL_SCRUB_NORMAL) 2403*eda14cbcSMatt Macy return (0); 2404*eda14cbcSMatt Macy 2405*eda14cbcSMatt Macy if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL) 2406*eda14cbcSMatt Macy return (0); 2407*eda14cbcSMatt Macy 2408*eda14cbcSMatt Macy if (func == POOL_SCAN_SCRUB) { 2409*eda14cbcSMatt Macy if (cmd == POOL_SCRUB_PAUSE) { 2410*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2411*eda14cbcSMatt Macy "cannot pause scrubbing %s"), zc.zc_name); 2412*eda14cbcSMatt Macy } else { 2413*eda14cbcSMatt Macy assert(cmd == POOL_SCRUB_NORMAL); 2414*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2415*eda14cbcSMatt Macy "cannot scrub %s"), zc.zc_name); 2416*eda14cbcSMatt Macy } 2417*eda14cbcSMatt Macy } else if (func == POOL_SCAN_RESILVER) { 2418*eda14cbcSMatt Macy assert(cmd == POOL_SCRUB_NORMAL); 2419*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2420*eda14cbcSMatt Macy "cannot restart resilver on %s"), zc.zc_name); 2421*eda14cbcSMatt Macy } else if (func == POOL_SCAN_NONE) { 2422*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 2423*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 2424*eda14cbcSMatt Macy zc.zc_name); 2425*eda14cbcSMatt Macy } else { 2426*eda14cbcSMatt Macy assert(!"unexpected result"); 2427*eda14cbcSMatt Macy } 2428*eda14cbcSMatt Macy 2429*eda14cbcSMatt Macy if (err == EBUSY) { 2430*eda14cbcSMatt Macy nvlist_t *nvroot; 2431*eda14cbcSMatt Macy pool_scan_stat_t *ps = NULL; 2432*eda14cbcSMatt Macy uint_t psc; 2433*eda14cbcSMatt Macy 2434*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(zhp->zpool_config, 2435*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 2436*eda14cbcSMatt Macy (void) nvlist_lookup_uint64_array(nvroot, 2437*eda14cbcSMatt Macy ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 2438*eda14cbcSMatt Macy if (ps && ps->pss_func == POOL_SCAN_SCRUB && 2439*eda14cbcSMatt Macy ps->pss_state == DSS_SCANNING) { 2440*eda14cbcSMatt Macy if (cmd == POOL_SCRUB_PAUSE) 2441*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg)); 2442*eda14cbcSMatt Macy else 2443*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 2444*eda14cbcSMatt Macy } else { 2445*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_RESILVERING, msg)); 2446*eda14cbcSMatt Macy } 2447*eda14cbcSMatt Macy } else if (err == ENOENT) { 2448*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 2449*eda14cbcSMatt Macy } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) { 2450*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg)); 2451*eda14cbcSMatt Macy } else { 2452*eda14cbcSMatt Macy return (zpool_standard_error(hdl, err, msg)); 2453*eda14cbcSMatt Macy } 2454*eda14cbcSMatt Macy } 2455*eda14cbcSMatt Macy 2456*eda14cbcSMatt Macy /* 2457*eda14cbcSMatt Macy * Find a vdev that matches the search criteria specified. We use the 2458*eda14cbcSMatt Macy * the nvpair name to determine how we should look for the device. 2459*eda14cbcSMatt Macy * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 2460*eda14cbcSMatt Macy * spare; but FALSE if its an INUSE spare. 2461*eda14cbcSMatt Macy */ 2462*eda14cbcSMatt Macy static nvlist_t * 2463*eda14cbcSMatt Macy vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 2464*eda14cbcSMatt Macy boolean_t *l2cache, boolean_t *log) 2465*eda14cbcSMatt Macy { 2466*eda14cbcSMatt Macy uint_t c, children; 2467*eda14cbcSMatt Macy nvlist_t **child; 2468*eda14cbcSMatt Macy nvlist_t *ret; 2469*eda14cbcSMatt Macy uint64_t is_log; 2470*eda14cbcSMatt Macy char *srchkey; 2471*eda14cbcSMatt Macy nvpair_t *pair = nvlist_next_nvpair(search, NULL); 2472*eda14cbcSMatt Macy 2473*eda14cbcSMatt Macy /* Nothing to look for */ 2474*eda14cbcSMatt Macy if (search == NULL || pair == NULL) 2475*eda14cbcSMatt Macy return (NULL); 2476*eda14cbcSMatt Macy 2477*eda14cbcSMatt Macy /* Obtain the key we will use to search */ 2478*eda14cbcSMatt Macy srchkey = nvpair_name(pair); 2479*eda14cbcSMatt Macy 2480*eda14cbcSMatt Macy switch (nvpair_type(pair)) { 2481*eda14cbcSMatt Macy case DATA_TYPE_UINT64: 2482*eda14cbcSMatt Macy if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 2483*eda14cbcSMatt Macy uint64_t srchval, theguid; 2484*eda14cbcSMatt Macy 2485*eda14cbcSMatt Macy verify(nvpair_value_uint64(pair, &srchval) == 0); 2486*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2487*eda14cbcSMatt Macy &theguid) == 0); 2488*eda14cbcSMatt Macy if (theguid == srchval) 2489*eda14cbcSMatt Macy return (nv); 2490*eda14cbcSMatt Macy } 2491*eda14cbcSMatt Macy break; 2492*eda14cbcSMatt Macy 2493*eda14cbcSMatt Macy case DATA_TYPE_STRING: { 2494*eda14cbcSMatt Macy char *srchval, *val; 2495*eda14cbcSMatt Macy 2496*eda14cbcSMatt Macy verify(nvpair_value_string(pair, &srchval) == 0); 2497*eda14cbcSMatt Macy if (nvlist_lookup_string(nv, srchkey, &val) != 0) 2498*eda14cbcSMatt Macy break; 2499*eda14cbcSMatt Macy 2500*eda14cbcSMatt Macy /* 2501*eda14cbcSMatt Macy * Search for the requested value. Special cases: 2502*eda14cbcSMatt Macy * 2503*eda14cbcSMatt Macy * - ZPOOL_CONFIG_PATH for whole disk entries. These end in 2504*eda14cbcSMatt Macy * "-part1", or "p1". The suffix is hidden from the user, 2505*eda14cbcSMatt Macy * but included in the string, so this matches around it. 2506*eda14cbcSMatt Macy * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname() 2507*eda14cbcSMatt Macy * is used to check all possible expanded paths. 2508*eda14cbcSMatt Macy * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 2509*eda14cbcSMatt Macy * 2510*eda14cbcSMatt Macy * Otherwise, all other searches are simple string compares. 2511*eda14cbcSMatt Macy */ 2512*eda14cbcSMatt Macy if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) { 2513*eda14cbcSMatt Macy uint64_t wholedisk = 0; 2514*eda14cbcSMatt Macy 2515*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2516*eda14cbcSMatt Macy &wholedisk); 2517*eda14cbcSMatt Macy if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0) 2518*eda14cbcSMatt Macy return (nv); 2519*eda14cbcSMatt Macy 2520*eda14cbcSMatt Macy } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 2521*eda14cbcSMatt Macy char *type, *idx, *end, *p; 2522*eda14cbcSMatt Macy uint64_t id, vdev_id; 2523*eda14cbcSMatt Macy 2524*eda14cbcSMatt Macy /* 2525*eda14cbcSMatt Macy * Determine our vdev type, keeping in mind 2526*eda14cbcSMatt Macy * that the srchval is composed of a type and 2527*eda14cbcSMatt Macy * vdev id pair (i.e. mirror-4). 2528*eda14cbcSMatt Macy */ 2529*eda14cbcSMatt Macy if ((type = strdup(srchval)) == NULL) 2530*eda14cbcSMatt Macy return (NULL); 2531*eda14cbcSMatt Macy 2532*eda14cbcSMatt Macy if ((p = strrchr(type, '-')) == NULL) { 2533*eda14cbcSMatt Macy free(type); 2534*eda14cbcSMatt Macy break; 2535*eda14cbcSMatt Macy } 2536*eda14cbcSMatt Macy idx = p + 1; 2537*eda14cbcSMatt Macy *p = '\0'; 2538*eda14cbcSMatt Macy 2539*eda14cbcSMatt Macy /* 2540*eda14cbcSMatt Macy * If the types don't match then keep looking. 2541*eda14cbcSMatt Macy */ 2542*eda14cbcSMatt Macy if (strncmp(val, type, strlen(val)) != 0) { 2543*eda14cbcSMatt Macy free(type); 2544*eda14cbcSMatt Macy break; 2545*eda14cbcSMatt Macy } 2546*eda14cbcSMatt Macy 2547*eda14cbcSMatt Macy verify(zpool_vdev_is_interior(type)); 2548*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 2549*eda14cbcSMatt Macy &id) == 0); 2550*eda14cbcSMatt Macy 2551*eda14cbcSMatt Macy errno = 0; 2552*eda14cbcSMatt Macy vdev_id = strtoull(idx, &end, 10); 2553*eda14cbcSMatt Macy 2554*eda14cbcSMatt Macy free(type); 2555*eda14cbcSMatt Macy if (errno != 0) 2556*eda14cbcSMatt Macy return (NULL); 2557*eda14cbcSMatt Macy 2558*eda14cbcSMatt Macy /* 2559*eda14cbcSMatt Macy * Now verify that we have the correct vdev id. 2560*eda14cbcSMatt Macy */ 2561*eda14cbcSMatt Macy if (vdev_id == id) 2562*eda14cbcSMatt Macy return (nv); 2563*eda14cbcSMatt Macy } 2564*eda14cbcSMatt Macy 2565*eda14cbcSMatt Macy /* 2566*eda14cbcSMatt Macy * Common case 2567*eda14cbcSMatt Macy */ 2568*eda14cbcSMatt Macy if (strcmp(srchval, val) == 0) 2569*eda14cbcSMatt Macy return (nv); 2570*eda14cbcSMatt Macy break; 2571*eda14cbcSMatt Macy } 2572*eda14cbcSMatt Macy 2573*eda14cbcSMatt Macy default: 2574*eda14cbcSMatt Macy break; 2575*eda14cbcSMatt Macy } 2576*eda14cbcSMatt Macy 2577*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2578*eda14cbcSMatt Macy &child, &children) != 0) 2579*eda14cbcSMatt Macy return (NULL); 2580*eda14cbcSMatt Macy 2581*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 2582*eda14cbcSMatt Macy if ((ret = vdev_to_nvlist_iter(child[c], search, 2583*eda14cbcSMatt Macy avail_spare, l2cache, NULL)) != NULL) { 2584*eda14cbcSMatt Macy /* 2585*eda14cbcSMatt Macy * The 'is_log' value is only set for the toplevel 2586*eda14cbcSMatt Macy * vdev, not the leaf vdevs. So we always lookup the 2587*eda14cbcSMatt Macy * log device from the root of the vdev tree (where 2588*eda14cbcSMatt Macy * 'log' is non-NULL). 2589*eda14cbcSMatt Macy */ 2590*eda14cbcSMatt Macy if (log != NULL && 2591*eda14cbcSMatt Macy nvlist_lookup_uint64(child[c], 2592*eda14cbcSMatt Macy ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2593*eda14cbcSMatt Macy is_log) { 2594*eda14cbcSMatt Macy *log = B_TRUE; 2595*eda14cbcSMatt Macy } 2596*eda14cbcSMatt Macy return (ret); 2597*eda14cbcSMatt Macy } 2598*eda14cbcSMatt Macy } 2599*eda14cbcSMatt Macy 2600*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 2601*eda14cbcSMatt Macy &child, &children) == 0) { 2602*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 2603*eda14cbcSMatt Macy if ((ret = vdev_to_nvlist_iter(child[c], search, 2604*eda14cbcSMatt Macy avail_spare, l2cache, NULL)) != NULL) { 2605*eda14cbcSMatt Macy *avail_spare = B_TRUE; 2606*eda14cbcSMatt Macy return (ret); 2607*eda14cbcSMatt Macy } 2608*eda14cbcSMatt Macy } 2609*eda14cbcSMatt Macy } 2610*eda14cbcSMatt Macy 2611*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2612*eda14cbcSMatt Macy &child, &children) == 0) { 2613*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 2614*eda14cbcSMatt Macy if ((ret = vdev_to_nvlist_iter(child[c], search, 2615*eda14cbcSMatt Macy avail_spare, l2cache, NULL)) != NULL) { 2616*eda14cbcSMatt Macy *l2cache = B_TRUE; 2617*eda14cbcSMatt Macy return (ret); 2618*eda14cbcSMatt Macy } 2619*eda14cbcSMatt Macy } 2620*eda14cbcSMatt Macy } 2621*eda14cbcSMatt Macy 2622*eda14cbcSMatt Macy return (NULL); 2623*eda14cbcSMatt Macy } 2624*eda14cbcSMatt Macy 2625*eda14cbcSMatt Macy /* 2626*eda14cbcSMatt Macy * Given a physical path or guid, find the associated vdev. 2627*eda14cbcSMatt Macy */ 2628*eda14cbcSMatt Macy nvlist_t * 2629*eda14cbcSMatt Macy zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2630*eda14cbcSMatt Macy boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2631*eda14cbcSMatt Macy { 2632*eda14cbcSMatt Macy nvlist_t *search, *nvroot, *ret; 2633*eda14cbcSMatt Macy uint64_t guid; 2634*eda14cbcSMatt Macy char *end; 2635*eda14cbcSMatt Macy 2636*eda14cbcSMatt Macy verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2637*eda14cbcSMatt Macy 2638*eda14cbcSMatt Macy guid = strtoull(ppath, &end, 0); 2639*eda14cbcSMatt Macy if (guid != 0 && *end == '\0') { 2640*eda14cbcSMatt Macy verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 2641*eda14cbcSMatt Macy } else { 2642*eda14cbcSMatt Macy verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, 2643*eda14cbcSMatt Macy ppath) == 0); 2644*eda14cbcSMatt Macy } 2645*eda14cbcSMatt Macy 2646*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2647*eda14cbcSMatt Macy &nvroot) == 0); 2648*eda14cbcSMatt Macy 2649*eda14cbcSMatt Macy *avail_spare = B_FALSE; 2650*eda14cbcSMatt Macy *l2cache = B_FALSE; 2651*eda14cbcSMatt Macy if (log != NULL) 2652*eda14cbcSMatt Macy *log = B_FALSE; 2653*eda14cbcSMatt Macy ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2654*eda14cbcSMatt Macy nvlist_free(search); 2655*eda14cbcSMatt Macy 2656*eda14cbcSMatt Macy return (ret); 2657*eda14cbcSMatt Macy } 2658*eda14cbcSMatt Macy 2659*eda14cbcSMatt Macy /* 2660*eda14cbcSMatt Macy * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 2661*eda14cbcSMatt Macy */ 2662*eda14cbcSMatt Macy static boolean_t 2663*eda14cbcSMatt Macy zpool_vdev_is_interior(const char *name) 2664*eda14cbcSMatt Macy { 2665*eda14cbcSMatt Macy if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 2666*eda14cbcSMatt Macy strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 || 2667*eda14cbcSMatt Macy strncmp(name, 2668*eda14cbcSMatt Macy VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 || 2669*eda14cbcSMatt Macy strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 2670*eda14cbcSMatt Macy return (B_TRUE); 2671*eda14cbcSMatt Macy return (B_FALSE); 2672*eda14cbcSMatt Macy } 2673*eda14cbcSMatt Macy 2674*eda14cbcSMatt Macy nvlist_t * 2675*eda14cbcSMatt Macy zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2676*eda14cbcSMatt Macy boolean_t *l2cache, boolean_t *log) 2677*eda14cbcSMatt Macy { 2678*eda14cbcSMatt Macy char *end; 2679*eda14cbcSMatt Macy nvlist_t *nvroot, *search, *ret; 2680*eda14cbcSMatt Macy uint64_t guid; 2681*eda14cbcSMatt Macy 2682*eda14cbcSMatt Macy verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2683*eda14cbcSMatt Macy 2684*eda14cbcSMatt Macy guid = strtoull(path, &end, 0); 2685*eda14cbcSMatt Macy if (guid != 0 && *end == '\0') { 2686*eda14cbcSMatt Macy verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 2687*eda14cbcSMatt Macy } else if (zpool_vdev_is_interior(path)) { 2688*eda14cbcSMatt Macy verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2689*eda14cbcSMatt Macy } else { 2690*eda14cbcSMatt Macy verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2691*eda14cbcSMatt Macy } 2692*eda14cbcSMatt Macy 2693*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2694*eda14cbcSMatt Macy &nvroot) == 0); 2695*eda14cbcSMatt Macy 2696*eda14cbcSMatt Macy *avail_spare = B_FALSE; 2697*eda14cbcSMatt Macy *l2cache = B_FALSE; 2698*eda14cbcSMatt Macy if (log != NULL) 2699*eda14cbcSMatt Macy *log = B_FALSE; 2700*eda14cbcSMatt Macy ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2701*eda14cbcSMatt Macy nvlist_free(search); 2702*eda14cbcSMatt Macy 2703*eda14cbcSMatt Macy return (ret); 2704*eda14cbcSMatt Macy } 2705*eda14cbcSMatt Macy 2706*eda14cbcSMatt Macy static int 2707*eda14cbcSMatt Macy vdev_is_online(nvlist_t *nv) 2708*eda14cbcSMatt Macy { 2709*eda14cbcSMatt Macy uint64_t ival; 2710*eda14cbcSMatt Macy 2711*eda14cbcSMatt Macy if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 2712*eda14cbcSMatt Macy nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 2713*eda14cbcSMatt Macy nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 2714*eda14cbcSMatt Macy return (0); 2715*eda14cbcSMatt Macy 2716*eda14cbcSMatt Macy return (1); 2717*eda14cbcSMatt Macy } 2718*eda14cbcSMatt Macy 2719*eda14cbcSMatt Macy /* 2720*eda14cbcSMatt Macy * Helper function for zpool_get_physpaths(). 2721*eda14cbcSMatt Macy */ 2722*eda14cbcSMatt Macy static int 2723*eda14cbcSMatt Macy vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2724*eda14cbcSMatt Macy size_t *bytes_written) 2725*eda14cbcSMatt Macy { 2726*eda14cbcSMatt Macy size_t bytes_left, pos, rsz; 2727*eda14cbcSMatt Macy char *tmppath; 2728*eda14cbcSMatt Macy const char *format; 2729*eda14cbcSMatt Macy 2730*eda14cbcSMatt Macy if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2731*eda14cbcSMatt Macy &tmppath) != 0) 2732*eda14cbcSMatt Macy return (EZFS_NODEVICE); 2733*eda14cbcSMatt Macy 2734*eda14cbcSMatt Macy pos = *bytes_written; 2735*eda14cbcSMatt Macy bytes_left = physpath_size - pos; 2736*eda14cbcSMatt Macy format = (pos == 0) ? "%s" : " %s"; 2737*eda14cbcSMatt Macy 2738*eda14cbcSMatt Macy rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2739*eda14cbcSMatt Macy *bytes_written += rsz; 2740*eda14cbcSMatt Macy 2741*eda14cbcSMatt Macy if (rsz >= bytes_left) { 2742*eda14cbcSMatt Macy /* if physpath was not copied properly, clear it */ 2743*eda14cbcSMatt Macy if (bytes_left != 0) { 2744*eda14cbcSMatt Macy physpath[pos] = 0; 2745*eda14cbcSMatt Macy } 2746*eda14cbcSMatt Macy return (EZFS_NOSPC); 2747*eda14cbcSMatt Macy } 2748*eda14cbcSMatt Macy return (0); 2749*eda14cbcSMatt Macy } 2750*eda14cbcSMatt Macy 2751*eda14cbcSMatt Macy static int 2752*eda14cbcSMatt Macy vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 2753*eda14cbcSMatt Macy size_t *rsz, boolean_t is_spare) 2754*eda14cbcSMatt Macy { 2755*eda14cbcSMatt Macy char *type; 2756*eda14cbcSMatt Macy int ret; 2757*eda14cbcSMatt Macy 2758*eda14cbcSMatt Macy if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 2759*eda14cbcSMatt Macy return (EZFS_INVALCONFIG); 2760*eda14cbcSMatt Macy 2761*eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_DISK) == 0) { 2762*eda14cbcSMatt Macy /* 2763*eda14cbcSMatt Macy * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 2764*eda14cbcSMatt Macy * For a spare vdev, we only want to boot from the active 2765*eda14cbcSMatt Macy * spare device. 2766*eda14cbcSMatt Macy */ 2767*eda14cbcSMatt Macy if (is_spare) { 2768*eda14cbcSMatt Macy uint64_t spare = 0; 2769*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 2770*eda14cbcSMatt Macy &spare); 2771*eda14cbcSMatt Macy if (!spare) 2772*eda14cbcSMatt Macy return (EZFS_INVALCONFIG); 2773*eda14cbcSMatt Macy } 2774*eda14cbcSMatt Macy 2775*eda14cbcSMatt Macy if (vdev_is_online(nv)) { 2776*eda14cbcSMatt Macy if ((ret = vdev_get_one_physpath(nv, physpath, 2777*eda14cbcSMatt Macy phypath_size, rsz)) != 0) 2778*eda14cbcSMatt Macy return (ret); 2779*eda14cbcSMatt Macy } 2780*eda14cbcSMatt Macy } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2781*eda14cbcSMatt Macy strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 2782*eda14cbcSMatt Macy strcmp(type, VDEV_TYPE_REPLACING) == 0 || 2783*eda14cbcSMatt Macy (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 2784*eda14cbcSMatt Macy nvlist_t **child; 2785*eda14cbcSMatt Macy uint_t count; 2786*eda14cbcSMatt Macy int i, ret; 2787*eda14cbcSMatt Macy 2788*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, 2789*eda14cbcSMatt Macy ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 2790*eda14cbcSMatt Macy return (EZFS_INVALCONFIG); 2791*eda14cbcSMatt Macy 2792*eda14cbcSMatt Macy for (i = 0; i < count; i++) { 2793*eda14cbcSMatt Macy ret = vdev_get_physpaths(child[i], physpath, 2794*eda14cbcSMatt Macy phypath_size, rsz, is_spare); 2795*eda14cbcSMatt Macy if (ret == EZFS_NOSPC) 2796*eda14cbcSMatt Macy return (ret); 2797*eda14cbcSMatt Macy } 2798*eda14cbcSMatt Macy } 2799*eda14cbcSMatt Macy 2800*eda14cbcSMatt Macy return (EZFS_POOL_INVALARG); 2801*eda14cbcSMatt Macy } 2802*eda14cbcSMatt Macy 2803*eda14cbcSMatt Macy /* 2804*eda14cbcSMatt Macy * Get phys_path for a root pool config. 2805*eda14cbcSMatt Macy * Return 0 on success; non-zero on failure. 2806*eda14cbcSMatt Macy */ 2807*eda14cbcSMatt Macy static int 2808*eda14cbcSMatt Macy zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2809*eda14cbcSMatt Macy { 2810*eda14cbcSMatt Macy size_t rsz; 2811*eda14cbcSMatt Macy nvlist_t *vdev_root; 2812*eda14cbcSMatt Macy nvlist_t **child; 2813*eda14cbcSMatt Macy uint_t count; 2814*eda14cbcSMatt Macy char *type; 2815*eda14cbcSMatt Macy 2816*eda14cbcSMatt Macy rsz = 0; 2817*eda14cbcSMatt Macy 2818*eda14cbcSMatt Macy if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2819*eda14cbcSMatt Macy &vdev_root) != 0) 2820*eda14cbcSMatt Macy return (EZFS_INVALCONFIG); 2821*eda14cbcSMatt Macy 2822*eda14cbcSMatt Macy if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2823*eda14cbcSMatt Macy nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2824*eda14cbcSMatt Macy &child, &count) != 0) 2825*eda14cbcSMatt Macy return (EZFS_INVALCONFIG); 2826*eda14cbcSMatt Macy 2827*eda14cbcSMatt Macy /* 2828*eda14cbcSMatt Macy * root pool can only have a single top-level vdev. 2829*eda14cbcSMatt Macy */ 2830*eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2831*eda14cbcSMatt Macy return (EZFS_POOL_INVALARG); 2832*eda14cbcSMatt Macy 2833*eda14cbcSMatt Macy (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 2834*eda14cbcSMatt Macy B_FALSE); 2835*eda14cbcSMatt Macy 2836*eda14cbcSMatt Macy /* No online devices */ 2837*eda14cbcSMatt Macy if (rsz == 0) 2838*eda14cbcSMatt Macy return (EZFS_NODEVICE); 2839*eda14cbcSMatt Macy 2840*eda14cbcSMatt Macy return (0); 2841*eda14cbcSMatt Macy } 2842*eda14cbcSMatt Macy 2843*eda14cbcSMatt Macy /* 2844*eda14cbcSMatt Macy * Get phys_path for a root pool 2845*eda14cbcSMatt Macy * Return 0 on success; non-zero on failure. 2846*eda14cbcSMatt Macy */ 2847*eda14cbcSMatt Macy int 2848*eda14cbcSMatt Macy zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2849*eda14cbcSMatt Macy { 2850*eda14cbcSMatt Macy return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2851*eda14cbcSMatt Macy phypath_size)); 2852*eda14cbcSMatt Macy } 2853*eda14cbcSMatt Macy 2854*eda14cbcSMatt Macy /* 2855*eda14cbcSMatt Macy * Convert a vdev path to a GUID. Returns GUID or 0 on error. 2856*eda14cbcSMatt Macy * 2857*eda14cbcSMatt Macy * If is_spare, is_l2cache, or is_log is non-NULL, then store within it 2858*eda14cbcSMatt Macy * if the VDEV is a spare, l2cache, or log device. If they're NULL then 2859*eda14cbcSMatt Macy * ignore them. 2860*eda14cbcSMatt Macy */ 2861*eda14cbcSMatt Macy static uint64_t 2862*eda14cbcSMatt Macy zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path, 2863*eda14cbcSMatt Macy boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log) 2864*eda14cbcSMatt Macy { 2865*eda14cbcSMatt Macy uint64_t guid; 2866*eda14cbcSMatt Macy boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE; 2867*eda14cbcSMatt Macy nvlist_t *tgt; 2868*eda14cbcSMatt Macy 2869*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache, 2870*eda14cbcSMatt Macy &log)) == NULL) 2871*eda14cbcSMatt Macy return (0); 2872*eda14cbcSMatt Macy 2873*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0); 2874*eda14cbcSMatt Macy if (is_spare != NULL) 2875*eda14cbcSMatt Macy *is_spare = spare; 2876*eda14cbcSMatt Macy if (is_l2cache != NULL) 2877*eda14cbcSMatt Macy *is_l2cache = l2cache; 2878*eda14cbcSMatt Macy if (is_log != NULL) 2879*eda14cbcSMatt Macy *is_log = log; 2880*eda14cbcSMatt Macy 2881*eda14cbcSMatt Macy return (guid); 2882*eda14cbcSMatt Macy } 2883*eda14cbcSMatt Macy 2884*eda14cbcSMatt Macy /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */ 2885*eda14cbcSMatt Macy uint64_t 2886*eda14cbcSMatt Macy zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path) 2887*eda14cbcSMatt Macy { 2888*eda14cbcSMatt Macy return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL)); 2889*eda14cbcSMatt Macy } 2890*eda14cbcSMatt Macy 2891*eda14cbcSMatt Macy /* 2892*eda14cbcSMatt Macy * Bring the specified vdev online. The 'flags' parameter is a set of the 2893*eda14cbcSMatt Macy * ZFS_ONLINE_* flags. 2894*eda14cbcSMatt Macy */ 2895*eda14cbcSMatt Macy int 2896*eda14cbcSMatt Macy zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 2897*eda14cbcSMatt Macy vdev_state_t *newstate) 2898*eda14cbcSMatt Macy { 2899*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 2900*eda14cbcSMatt Macy char msg[1024]; 2901*eda14cbcSMatt Macy char *pathname; 2902*eda14cbcSMatt Macy nvlist_t *tgt; 2903*eda14cbcSMatt Macy boolean_t avail_spare, l2cache, islog; 2904*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 2905*eda14cbcSMatt Macy int error; 2906*eda14cbcSMatt Macy 2907*eda14cbcSMatt Macy if (flags & ZFS_ONLINE_EXPAND) { 2908*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 2909*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2910*eda14cbcSMatt Macy } else { 2911*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 2912*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2913*eda14cbcSMatt Macy } 2914*eda14cbcSMatt Macy 2915*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2916*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2917*eda14cbcSMatt Macy &islog)) == NULL) 2918*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2919*eda14cbcSMatt Macy 2920*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2921*eda14cbcSMatt Macy 2922*eda14cbcSMatt Macy if (avail_spare) 2923*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2924*eda14cbcSMatt Macy 2925*eda14cbcSMatt Macy if ((flags & ZFS_ONLINE_EXPAND || 2926*eda14cbcSMatt Macy zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) && 2927*eda14cbcSMatt Macy nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) { 2928*eda14cbcSMatt Macy uint64_t wholedisk = 0; 2929*eda14cbcSMatt Macy 2930*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2931*eda14cbcSMatt Macy &wholedisk); 2932*eda14cbcSMatt Macy 2933*eda14cbcSMatt Macy /* 2934*eda14cbcSMatt Macy * XXX - L2ARC 1.0 devices can't support expansion. 2935*eda14cbcSMatt Macy */ 2936*eda14cbcSMatt Macy if (l2cache) { 2937*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2938*eda14cbcSMatt Macy "cannot expand cache devices")); 2939*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2940*eda14cbcSMatt Macy } 2941*eda14cbcSMatt Macy 2942*eda14cbcSMatt Macy if (wholedisk) { 2943*eda14cbcSMatt Macy const char *fullpath = path; 2944*eda14cbcSMatt Macy char buf[MAXPATHLEN]; 2945*eda14cbcSMatt Macy 2946*eda14cbcSMatt Macy if (path[0] != '/') { 2947*eda14cbcSMatt Macy error = zfs_resolve_shortname(path, buf, 2948*eda14cbcSMatt Macy sizeof (buf)); 2949*eda14cbcSMatt Macy if (error != 0) 2950*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, 2951*eda14cbcSMatt Macy msg)); 2952*eda14cbcSMatt Macy 2953*eda14cbcSMatt Macy fullpath = buf; 2954*eda14cbcSMatt Macy } 2955*eda14cbcSMatt Macy 2956*eda14cbcSMatt Macy error = zpool_relabel_disk(hdl, fullpath, msg); 2957*eda14cbcSMatt Macy if (error != 0) 2958*eda14cbcSMatt Macy return (error); 2959*eda14cbcSMatt Macy } 2960*eda14cbcSMatt Macy } 2961*eda14cbcSMatt Macy 2962*eda14cbcSMatt Macy zc.zc_cookie = VDEV_STATE_ONLINE; 2963*eda14cbcSMatt Macy zc.zc_obj = flags; 2964*eda14cbcSMatt Macy 2965*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 2966*eda14cbcSMatt Macy if (errno == EINVAL) { 2967*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 2968*eda14cbcSMatt Macy "from this pool into a new one. Use '%s' " 2969*eda14cbcSMatt Macy "instead"), "zpool detach"); 2970*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 2971*eda14cbcSMatt Macy } 2972*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 2973*eda14cbcSMatt Macy } 2974*eda14cbcSMatt Macy 2975*eda14cbcSMatt Macy *newstate = zc.zc_cookie; 2976*eda14cbcSMatt Macy return (0); 2977*eda14cbcSMatt Macy } 2978*eda14cbcSMatt Macy 2979*eda14cbcSMatt Macy /* 2980*eda14cbcSMatt Macy * Take the specified vdev offline 2981*eda14cbcSMatt Macy */ 2982*eda14cbcSMatt Macy int 2983*eda14cbcSMatt Macy zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2984*eda14cbcSMatt Macy { 2985*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 2986*eda14cbcSMatt Macy char msg[1024]; 2987*eda14cbcSMatt Macy nvlist_t *tgt; 2988*eda14cbcSMatt Macy boolean_t avail_spare, l2cache; 2989*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 2990*eda14cbcSMatt Macy 2991*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 2992*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2993*eda14cbcSMatt Macy 2994*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2995*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2996*eda14cbcSMatt Macy NULL)) == NULL) 2997*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2998*eda14cbcSMatt Macy 2999*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 3000*eda14cbcSMatt Macy 3001*eda14cbcSMatt Macy if (avail_spare) 3002*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3003*eda14cbcSMatt Macy 3004*eda14cbcSMatt Macy zc.zc_cookie = VDEV_STATE_OFFLINE; 3005*eda14cbcSMatt Macy zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 3006*eda14cbcSMatt Macy 3007*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3008*eda14cbcSMatt Macy return (0); 3009*eda14cbcSMatt Macy 3010*eda14cbcSMatt Macy switch (errno) { 3011*eda14cbcSMatt Macy case EBUSY: 3012*eda14cbcSMatt Macy 3013*eda14cbcSMatt Macy /* 3014*eda14cbcSMatt Macy * There are no other replicas of this device. 3015*eda14cbcSMatt Macy */ 3016*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 3017*eda14cbcSMatt Macy 3018*eda14cbcSMatt Macy case EEXIST: 3019*eda14cbcSMatt Macy /* 3020*eda14cbcSMatt Macy * The log device has unplayed logs 3021*eda14cbcSMatt Macy */ 3022*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 3023*eda14cbcSMatt Macy 3024*eda14cbcSMatt Macy default: 3025*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3026*eda14cbcSMatt Macy } 3027*eda14cbcSMatt Macy } 3028*eda14cbcSMatt Macy 3029*eda14cbcSMatt Macy /* 3030*eda14cbcSMatt Macy * Mark the given vdev faulted. 3031*eda14cbcSMatt Macy */ 3032*eda14cbcSMatt Macy int 3033*eda14cbcSMatt Macy zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 3034*eda14cbcSMatt Macy { 3035*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3036*eda14cbcSMatt Macy char msg[1024]; 3037*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3038*eda14cbcSMatt Macy 3039*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3040*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid); 3041*eda14cbcSMatt Macy 3042*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3043*eda14cbcSMatt Macy zc.zc_guid = guid; 3044*eda14cbcSMatt Macy zc.zc_cookie = VDEV_STATE_FAULTED; 3045*eda14cbcSMatt Macy zc.zc_obj = aux; 3046*eda14cbcSMatt Macy 3047*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3048*eda14cbcSMatt Macy return (0); 3049*eda14cbcSMatt Macy 3050*eda14cbcSMatt Macy switch (errno) { 3051*eda14cbcSMatt Macy case EBUSY: 3052*eda14cbcSMatt Macy 3053*eda14cbcSMatt Macy /* 3054*eda14cbcSMatt Macy * There are no other replicas of this device. 3055*eda14cbcSMatt Macy */ 3056*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 3057*eda14cbcSMatt Macy 3058*eda14cbcSMatt Macy default: 3059*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3060*eda14cbcSMatt Macy } 3061*eda14cbcSMatt Macy 3062*eda14cbcSMatt Macy } 3063*eda14cbcSMatt Macy 3064*eda14cbcSMatt Macy /* 3065*eda14cbcSMatt Macy * Mark the given vdev degraded. 3066*eda14cbcSMatt Macy */ 3067*eda14cbcSMatt Macy int 3068*eda14cbcSMatt Macy zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 3069*eda14cbcSMatt Macy { 3070*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3071*eda14cbcSMatt Macy char msg[1024]; 3072*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3073*eda14cbcSMatt Macy 3074*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3075*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid); 3076*eda14cbcSMatt Macy 3077*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3078*eda14cbcSMatt Macy zc.zc_guid = guid; 3079*eda14cbcSMatt Macy zc.zc_cookie = VDEV_STATE_DEGRADED; 3080*eda14cbcSMatt Macy zc.zc_obj = aux; 3081*eda14cbcSMatt Macy 3082*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 3083*eda14cbcSMatt Macy return (0); 3084*eda14cbcSMatt Macy 3085*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3086*eda14cbcSMatt Macy } 3087*eda14cbcSMatt Macy 3088*eda14cbcSMatt Macy /* 3089*eda14cbcSMatt Macy * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 3090*eda14cbcSMatt Macy * a hot spare. 3091*eda14cbcSMatt Macy */ 3092*eda14cbcSMatt Macy static boolean_t 3093*eda14cbcSMatt Macy is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 3094*eda14cbcSMatt Macy { 3095*eda14cbcSMatt Macy nvlist_t **child; 3096*eda14cbcSMatt Macy uint_t c, children; 3097*eda14cbcSMatt Macy char *type; 3098*eda14cbcSMatt Macy 3099*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 3100*eda14cbcSMatt Macy &children) == 0) { 3101*eda14cbcSMatt Macy verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 3102*eda14cbcSMatt Macy &type) == 0); 3103*eda14cbcSMatt Macy 3104*eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 3105*eda14cbcSMatt Macy children == 2 && child[which] == tgt) 3106*eda14cbcSMatt Macy return (B_TRUE); 3107*eda14cbcSMatt Macy 3108*eda14cbcSMatt Macy for (c = 0; c < children; c++) 3109*eda14cbcSMatt Macy if (is_replacing_spare(child[c], tgt, which)) 3110*eda14cbcSMatt Macy return (B_TRUE); 3111*eda14cbcSMatt Macy } 3112*eda14cbcSMatt Macy 3113*eda14cbcSMatt Macy return (B_FALSE); 3114*eda14cbcSMatt Macy } 3115*eda14cbcSMatt Macy 3116*eda14cbcSMatt Macy /* 3117*eda14cbcSMatt Macy * Attach new_disk (fully described by nvroot) to old_disk. 3118*eda14cbcSMatt Macy * If 'replacing' is specified, the new disk will replace the old one. 3119*eda14cbcSMatt Macy */ 3120*eda14cbcSMatt Macy int 3121*eda14cbcSMatt Macy zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk, 3122*eda14cbcSMatt Macy const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild) 3123*eda14cbcSMatt Macy { 3124*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3125*eda14cbcSMatt Macy char msg[1024]; 3126*eda14cbcSMatt Macy int ret; 3127*eda14cbcSMatt Macy nvlist_t *tgt; 3128*eda14cbcSMatt Macy boolean_t avail_spare, l2cache, islog; 3129*eda14cbcSMatt Macy uint64_t val; 3130*eda14cbcSMatt Macy char *newname; 3131*eda14cbcSMatt Macy nvlist_t **child; 3132*eda14cbcSMatt Macy uint_t children; 3133*eda14cbcSMatt Macy nvlist_t *config_root; 3134*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3135*eda14cbcSMatt Macy 3136*eda14cbcSMatt Macy if (replacing) 3137*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 3138*eda14cbcSMatt Macy "cannot replace %s with %s"), old_disk, new_disk); 3139*eda14cbcSMatt Macy else 3140*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 3141*eda14cbcSMatt Macy "cannot attach %s to %s"), new_disk, old_disk); 3142*eda14cbcSMatt Macy 3143*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3144*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 3145*eda14cbcSMatt Macy &islog)) == NULL) 3146*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3147*eda14cbcSMatt Macy 3148*eda14cbcSMatt Macy if (avail_spare) 3149*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3150*eda14cbcSMatt Macy 3151*eda14cbcSMatt Macy if (l2cache) 3152*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 3153*eda14cbcSMatt Macy 3154*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 3155*eda14cbcSMatt Macy zc.zc_cookie = replacing; 3156*eda14cbcSMatt Macy zc.zc_simple = rebuild; 3157*eda14cbcSMatt Macy 3158*eda14cbcSMatt Macy if (rebuild && 3159*eda14cbcSMatt Macy zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) { 3160*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3161*eda14cbcSMatt Macy "the loaded zfs module doesn't support device rebuilds")); 3162*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg)); 3163*eda14cbcSMatt Macy } 3164*eda14cbcSMatt Macy 3165*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 3166*eda14cbcSMatt Macy &child, &children) != 0 || children != 1) { 3167*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3168*eda14cbcSMatt Macy "new device must be a single disk")); 3169*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 3170*eda14cbcSMatt Macy } 3171*eda14cbcSMatt Macy 3172*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 3173*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 3174*eda14cbcSMatt Macy 3175*eda14cbcSMatt Macy if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL) 3176*eda14cbcSMatt Macy return (-1); 3177*eda14cbcSMatt Macy 3178*eda14cbcSMatt Macy /* 3179*eda14cbcSMatt Macy * If the target is a hot spare that has been swapped in, we can only 3180*eda14cbcSMatt Macy * replace it with another hot spare. 3181*eda14cbcSMatt Macy */ 3182*eda14cbcSMatt Macy if (replacing && 3183*eda14cbcSMatt Macy nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 3184*eda14cbcSMatt Macy (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 3185*eda14cbcSMatt Macy NULL) == NULL || !avail_spare) && 3186*eda14cbcSMatt Macy is_replacing_spare(config_root, tgt, 1)) { 3187*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3188*eda14cbcSMatt Macy "can only be replaced by another hot spare")); 3189*eda14cbcSMatt Macy free(newname); 3190*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADTARGET, msg)); 3191*eda14cbcSMatt Macy } 3192*eda14cbcSMatt Macy 3193*eda14cbcSMatt Macy free(newname); 3194*eda14cbcSMatt Macy 3195*eda14cbcSMatt Macy if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 3196*eda14cbcSMatt Macy return (-1); 3197*eda14cbcSMatt Macy 3198*eda14cbcSMatt Macy ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 3199*eda14cbcSMatt Macy 3200*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3201*eda14cbcSMatt Macy 3202*eda14cbcSMatt Macy if (ret == 0) 3203*eda14cbcSMatt Macy return (0); 3204*eda14cbcSMatt Macy 3205*eda14cbcSMatt Macy switch (errno) { 3206*eda14cbcSMatt Macy case ENOTSUP: 3207*eda14cbcSMatt Macy /* 3208*eda14cbcSMatt Macy * Can't attach to or replace this type of vdev. 3209*eda14cbcSMatt Macy */ 3210*eda14cbcSMatt Macy if (replacing) { 3211*eda14cbcSMatt Macy uint64_t version = zpool_get_prop_int(zhp, 3212*eda14cbcSMatt Macy ZPOOL_PROP_VERSION, NULL); 3213*eda14cbcSMatt Macy 3214*eda14cbcSMatt Macy if (islog) { 3215*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3216*eda14cbcSMatt Macy "cannot replace a log with a spare")); 3217*eda14cbcSMatt Macy } else if (rebuild) { 3218*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3219*eda14cbcSMatt Macy "only mirror vdevs support sequential " 3220*eda14cbcSMatt Macy "reconstruction")); 3221*eda14cbcSMatt Macy } else if (version >= SPA_VERSION_MULTI_REPLACE) { 3222*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3223*eda14cbcSMatt Macy "already in replacing/spare config; wait " 3224*eda14cbcSMatt Macy "for completion or use 'zpool detach'")); 3225*eda14cbcSMatt Macy } else { 3226*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3227*eda14cbcSMatt Macy "cannot replace a replacing device")); 3228*eda14cbcSMatt Macy } 3229*eda14cbcSMatt Macy } else { 3230*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3231*eda14cbcSMatt Macy "can only attach to mirrors and top-level " 3232*eda14cbcSMatt Macy "disks")); 3233*eda14cbcSMatt Macy } 3234*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADTARGET, msg); 3235*eda14cbcSMatt Macy break; 3236*eda14cbcSMatt Macy 3237*eda14cbcSMatt Macy case EINVAL: 3238*eda14cbcSMatt Macy /* 3239*eda14cbcSMatt Macy * The new device must be a single disk. 3240*eda14cbcSMatt Macy */ 3241*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3242*eda14cbcSMatt Macy "new device must be a single disk")); 3243*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 3244*eda14cbcSMatt Macy break; 3245*eda14cbcSMatt Macy 3246*eda14cbcSMatt Macy case EBUSY: 3247*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, " 3248*eda14cbcSMatt Macy "or device removal is in progress"), 3249*eda14cbcSMatt Macy new_disk); 3250*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 3251*eda14cbcSMatt Macy break; 3252*eda14cbcSMatt Macy 3253*eda14cbcSMatt Macy case EOVERFLOW: 3254*eda14cbcSMatt Macy /* 3255*eda14cbcSMatt Macy * The new device is too small. 3256*eda14cbcSMatt Macy */ 3257*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3258*eda14cbcSMatt Macy "device is too small")); 3259*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 3260*eda14cbcSMatt Macy break; 3261*eda14cbcSMatt Macy 3262*eda14cbcSMatt Macy case EDOM: 3263*eda14cbcSMatt Macy /* 3264*eda14cbcSMatt Macy * The new device has a different optimal sector size. 3265*eda14cbcSMatt Macy */ 3266*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3267*eda14cbcSMatt Macy "new device has a different optimal sector size; use the " 3268*eda14cbcSMatt Macy "option '-o ashift=N' to override the optimal size")); 3269*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADDEV, msg); 3270*eda14cbcSMatt Macy break; 3271*eda14cbcSMatt Macy 3272*eda14cbcSMatt Macy case ENAMETOOLONG: 3273*eda14cbcSMatt Macy /* 3274*eda14cbcSMatt Macy * The resulting top-level vdev spec won't fit in the label. 3275*eda14cbcSMatt Macy */ 3276*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 3277*eda14cbcSMatt Macy break; 3278*eda14cbcSMatt Macy 3279*eda14cbcSMatt Macy default: 3280*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 3281*eda14cbcSMatt Macy } 3282*eda14cbcSMatt Macy 3283*eda14cbcSMatt Macy return (-1); 3284*eda14cbcSMatt Macy } 3285*eda14cbcSMatt Macy 3286*eda14cbcSMatt Macy /* 3287*eda14cbcSMatt Macy * Detach the specified device. 3288*eda14cbcSMatt Macy */ 3289*eda14cbcSMatt Macy int 3290*eda14cbcSMatt Macy zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 3291*eda14cbcSMatt Macy { 3292*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3293*eda14cbcSMatt Macy char msg[1024]; 3294*eda14cbcSMatt Macy nvlist_t *tgt; 3295*eda14cbcSMatt Macy boolean_t avail_spare, l2cache; 3296*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3297*eda14cbcSMatt Macy 3298*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3299*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 3300*eda14cbcSMatt Macy 3301*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3302*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3303*eda14cbcSMatt Macy NULL)) == NULL) 3304*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3305*eda14cbcSMatt Macy 3306*eda14cbcSMatt Macy if (avail_spare) 3307*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3308*eda14cbcSMatt Macy 3309*eda14cbcSMatt Macy if (l2cache) 3310*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 3311*eda14cbcSMatt Macy 3312*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 3313*eda14cbcSMatt Macy 3314*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 3315*eda14cbcSMatt Macy return (0); 3316*eda14cbcSMatt Macy 3317*eda14cbcSMatt Macy switch (errno) { 3318*eda14cbcSMatt Macy 3319*eda14cbcSMatt Macy case ENOTSUP: 3320*eda14cbcSMatt Macy /* 3321*eda14cbcSMatt Macy * Can't detach from this type of vdev. 3322*eda14cbcSMatt Macy */ 3323*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 3324*eda14cbcSMatt Macy "applicable to mirror and replacing vdevs")); 3325*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADTARGET, msg); 3326*eda14cbcSMatt Macy break; 3327*eda14cbcSMatt Macy 3328*eda14cbcSMatt Macy case EBUSY: 3329*eda14cbcSMatt Macy /* 3330*eda14cbcSMatt Macy * There are no other replicas of this device. 3331*eda14cbcSMatt Macy */ 3332*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 3333*eda14cbcSMatt Macy break; 3334*eda14cbcSMatt Macy 3335*eda14cbcSMatt Macy default: 3336*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 3337*eda14cbcSMatt Macy } 3338*eda14cbcSMatt Macy 3339*eda14cbcSMatt Macy return (-1); 3340*eda14cbcSMatt Macy } 3341*eda14cbcSMatt Macy 3342*eda14cbcSMatt Macy /* 3343*eda14cbcSMatt Macy * Find a mirror vdev in the source nvlist. 3344*eda14cbcSMatt Macy * 3345*eda14cbcSMatt Macy * The mchild array contains a list of disks in one of the top-level mirrors 3346*eda14cbcSMatt Macy * of the source pool. The schild array contains a list of disks that the 3347*eda14cbcSMatt Macy * user specified on the command line. We loop over the mchild array to 3348*eda14cbcSMatt Macy * see if any entry in the schild array matches. 3349*eda14cbcSMatt Macy * 3350*eda14cbcSMatt Macy * If a disk in the mchild array is found in the schild array, we return 3351*eda14cbcSMatt Macy * the index of that entry. Otherwise we return -1. 3352*eda14cbcSMatt Macy */ 3353*eda14cbcSMatt Macy static int 3354*eda14cbcSMatt Macy find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 3355*eda14cbcSMatt Macy nvlist_t **schild, uint_t schildren) 3356*eda14cbcSMatt Macy { 3357*eda14cbcSMatt Macy uint_t mc; 3358*eda14cbcSMatt Macy 3359*eda14cbcSMatt Macy for (mc = 0; mc < mchildren; mc++) { 3360*eda14cbcSMatt Macy uint_t sc; 3361*eda14cbcSMatt Macy char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3362*eda14cbcSMatt Macy mchild[mc], 0); 3363*eda14cbcSMatt Macy 3364*eda14cbcSMatt Macy for (sc = 0; sc < schildren; sc++) { 3365*eda14cbcSMatt Macy char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3366*eda14cbcSMatt Macy schild[sc], 0); 3367*eda14cbcSMatt Macy boolean_t result = (strcmp(mpath, spath) == 0); 3368*eda14cbcSMatt Macy 3369*eda14cbcSMatt Macy free(spath); 3370*eda14cbcSMatt Macy if (result) { 3371*eda14cbcSMatt Macy free(mpath); 3372*eda14cbcSMatt Macy return (mc); 3373*eda14cbcSMatt Macy } 3374*eda14cbcSMatt Macy } 3375*eda14cbcSMatt Macy 3376*eda14cbcSMatt Macy free(mpath); 3377*eda14cbcSMatt Macy } 3378*eda14cbcSMatt Macy 3379*eda14cbcSMatt Macy return (-1); 3380*eda14cbcSMatt Macy } 3381*eda14cbcSMatt Macy 3382*eda14cbcSMatt Macy /* 3383*eda14cbcSMatt Macy * Split a mirror pool. If newroot points to null, then a new nvlist 3384*eda14cbcSMatt Macy * is generated and it is the responsibility of the caller to free it. 3385*eda14cbcSMatt Macy */ 3386*eda14cbcSMatt Macy int 3387*eda14cbcSMatt Macy zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 3388*eda14cbcSMatt Macy nvlist_t *props, splitflags_t flags) 3389*eda14cbcSMatt Macy { 3390*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3391*eda14cbcSMatt Macy char msg[1024]; 3392*eda14cbcSMatt Macy nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 3393*eda14cbcSMatt Macy nvlist_t **varray = NULL, *zc_props = NULL; 3394*eda14cbcSMatt Macy uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 3395*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3396*eda14cbcSMatt Macy uint64_t vers, readonly = B_FALSE; 3397*eda14cbcSMatt Macy boolean_t freelist = B_FALSE, memory_err = B_TRUE; 3398*eda14cbcSMatt Macy int retval = 0; 3399*eda14cbcSMatt Macy 3400*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3401*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 3402*eda14cbcSMatt Macy 3403*eda14cbcSMatt Macy if (!zpool_name_valid(hdl, B_FALSE, newname)) 3404*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 3405*eda14cbcSMatt Macy 3406*eda14cbcSMatt Macy if ((config = zpool_get_config(zhp, NULL)) == NULL) { 3407*eda14cbcSMatt Macy (void) fprintf(stderr, gettext("Internal error: unable to " 3408*eda14cbcSMatt Macy "retrieve pool configuration\n")); 3409*eda14cbcSMatt Macy return (-1); 3410*eda14cbcSMatt Macy } 3411*eda14cbcSMatt Macy 3412*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 3413*eda14cbcSMatt Macy == 0); 3414*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 3415*eda14cbcSMatt Macy 3416*eda14cbcSMatt Macy if (props) { 3417*eda14cbcSMatt Macy prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 3418*eda14cbcSMatt Macy if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 3419*eda14cbcSMatt Macy props, vers, flags, msg)) == NULL) 3420*eda14cbcSMatt Macy return (-1); 3421*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(zc_props, 3422*eda14cbcSMatt Macy zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); 3423*eda14cbcSMatt Macy if (readonly) { 3424*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3425*eda14cbcSMatt Macy "property %s can only be set at import time"), 3426*eda14cbcSMatt Macy zpool_prop_to_name(ZPOOL_PROP_READONLY)); 3427*eda14cbcSMatt Macy return (-1); 3428*eda14cbcSMatt Macy } 3429*eda14cbcSMatt Macy } 3430*eda14cbcSMatt Macy 3431*eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 3432*eda14cbcSMatt Macy &children) != 0) { 3433*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3434*eda14cbcSMatt Macy "Source pool is missing vdev tree")); 3435*eda14cbcSMatt Macy nvlist_free(zc_props); 3436*eda14cbcSMatt Macy return (-1); 3437*eda14cbcSMatt Macy } 3438*eda14cbcSMatt Macy 3439*eda14cbcSMatt Macy varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 3440*eda14cbcSMatt Macy vcount = 0; 3441*eda14cbcSMatt Macy 3442*eda14cbcSMatt Macy if (*newroot == NULL || 3443*eda14cbcSMatt Macy nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 3444*eda14cbcSMatt Macy &newchild, &newchildren) != 0) 3445*eda14cbcSMatt Macy newchildren = 0; 3446*eda14cbcSMatt Macy 3447*eda14cbcSMatt Macy for (c = 0; c < children; c++) { 3448*eda14cbcSMatt Macy uint64_t is_log = B_FALSE, is_hole = B_FALSE; 3449*eda14cbcSMatt Macy char *type; 3450*eda14cbcSMatt Macy nvlist_t **mchild, *vdev; 3451*eda14cbcSMatt Macy uint_t mchildren; 3452*eda14cbcSMatt Macy int entry; 3453*eda14cbcSMatt Macy 3454*eda14cbcSMatt Macy /* 3455*eda14cbcSMatt Macy * Unlike cache & spares, slogs are stored in the 3456*eda14cbcSMatt Macy * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 3457*eda14cbcSMatt Macy */ 3458*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 3459*eda14cbcSMatt Macy &is_log); 3460*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 3461*eda14cbcSMatt Macy &is_hole); 3462*eda14cbcSMatt Macy if (is_log || is_hole) { 3463*eda14cbcSMatt Macy /* 3464*eda14cbcSMatt Macy * Create a hole vdev and put it in the config. 3465*eda14cbcSMatt Macy */ 3466*eda14cbcSMatt Macy if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 3467*eda14cbcSMatt Macy goto out; 3468*eda14cbcSMatt Macy if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 3469*eda14cbcSMatt Macy VDEV_TYPE_HOLE) != 0) 3470*eda14cbcSMatt Macy goto out; 3471*eda14cbcSMatt Macy if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 3472*eda14cbcSMatt Macy 1) != 0) 3473*eda14cbcSMatt Macy goto out; 3474*eda14cbcSMatt Macy if (lastlog == 0) 3475*eda14cbcSMatt Macy lastlog = vcount; 3476*eda14cbcSMatt Macy varray[vcount++] = vdev; 3477*eda14cbcSMatt Macy continue; 3478*eda14cbcSMatt Macy } 3479*eda14cbcSMatt Macy lastlog = 0; 3480*eda14cbcSMatt Macy verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 3481*eda14cbcSMatt Macy == 0); 3482*eda14cbcSMatt Macy 3483*eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) { 3484*eda14cbcSMatt Macy vdev = child[c]; 3485*eda14cbcSMatt Macy if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 3486*eda14cbcSMatt Macy goto out; 3487*eda14cbcSMatt Macy continue; 3488*eda14cbcSMatt Macy } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 3489*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3490*eda14cbcSMatt Macy "Source pool must be composed only of mirrors\n")); 3491*eda14cbcSMatt Macy retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 3492*eda14cbcSMatt Macy goto out; 3493*eda14cbcSMatt Macy } 3494*eda14cbcSMatt Macy 3495*eda14cbcSMatt Macy verify(nvlist_lookup_nvlist_array(child[c], 3496*eda14cbcSMatt Macy ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 3497*eda14cbcSMatt Macy 3498*eda14cbcSMatt Macy /* find or add an entry for this top-level vdev */ 3499*eda14cbcSMatt Macy if (newchildren > 0 && 3500*eda14cbcSMatt Macy (entry = find_vdev_entry(zhp, mchild, mchildren, 3501*eda14cbcSMatt Macy newchild, newchildren)) >= 0) { 3502*eda14cbcSMatt Macy /* We found a disk that the user specified. */ 3503*eda14cbcSMatt Macy vdev = mchild[entry]; 3504*eda14cbcSMatt Macy ++found; 3505*eda14cbcSMatt Macy } else { 3506*eda14cbcSMatt Macy /* User didn't specify a disk for this vdev. */ 3507*eda14cbcSMatt Macy vdev = mchild[mchildren - 1]; 3508*eda14cbcSMatt Macy } 3509*eda14cbcSMatt Macy 3510*eda14cbcSMatt Macy if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 3511*eda14cbcSMatt Macy goto out; 3512*eda14cbcSMatt Macy } 3513*eda14cbcSMatt Macy 3514*eda14cbcSMatt Macy /* did we find every disk the user specified? */ 3515*eda14cbcSMatt Macy if (found != newchildren) { 3516*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 3517*eda14cbcSMatt Macy "include at most one disk from each mirror")); 3518*eda14cbcSMatt Macy retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 3519*eda14cbcSMatt Macy goto out; 3520*eda14cbcSMatt Macy } 3521*eda14cbcSMatt Macy 3522*eda14cbcSMatt Macy /* Prepare the nvlist for populating. */ 3523*eda14cbcSMatt Macy if (*newroot == NULL) { 3524*eda14cbcSMatt Macy if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 3525*eda14cbcSMatt Macy goto out; 3526*eda14cbcSMatt Macy freelist = B_TRUE; 3527*eda14cbcSMatt Macy if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 3528*eda14cbcSMatt Macy VDEV_TYPE_ROOT) != 0) 3529*eda14cbcSMatt Macy goto out; 3530*eda14cbcSMatt Macy } else { 3531*eda14cbcSMatt Macy verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 3532*eda14cbcSMatt Macy } 3533*eda14cbcSMatt Macy 3534*eda14cbcSMatt Macy /* Add all the children we found */ 3535*eda14cbcSMatt Macy if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 3536*eda14cbcSMatt Macy lastlog == 0 ? vcount : lastlog) != 0) 3537*eda14cbcSMatt Macy goto out; 3538*eda14cbcSMatt Macy 3539*eda14cbcSMatt Macy /* 3540*eda14cbcSMatt Macy * If we're just doing a dry run, exit now with success. 3541*eda14cbcSMatt Macy */ 3542*eda14cbcSMatt Macy if (flags.dryrun) { 3543*eda14cbcSMatt Macy memory_err = B_FALSE; 3544*eda14cbcSMatt Macy freelist = B_FALSE; 3545*eda14cbcSMatt Macy goto out; 3546*eda14cbcSMatt Macy } 3547*eda14cbcSMatt Macy 3548*eda14cbcSMatt Macy /* now build up the config list & call the ioctl */ 3549*eda14cbcSMatt Macy if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 3550*eda14cbcSMatt Macy goto out; 3551*eda14cbcSMatt Macy 3552*eda14cbcSMatt Macy if (nvlist_add_nvlist(newconfig, 3553*eda14cbcSMatt Macy ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 3554*eda14cbcSMatt Macy nvlist_add_string(newconfig, 3555*eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 3556*eda14cbcSMatt Macy nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 3557*eda14cbcSMatt Macy goto out; 3558*eda14cbcSMatt Macy 3559*eda14cbcSMatt Macy /* 3560*eda14cbcSMatt Macy * The new pool is automatically part of the namespace unless we 3561*eda14cbcSMatt Macy * explicitly export it. 3562*eda14cbcSMatt Macy */ 3563*eda14cbcSMatt Macy if (!flags.import) 3564*eda14cbcSMatt Macy zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 3565*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3566*eda14cbcSMatt Macy (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 3567*eda14cbcSMatt Macy if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 3568*eda14cbcSMatt Macy goto out; 3569*eda14cbcSMatt Macy if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 3570*eda14cbcSMatt Macy goto out; 3571*eda14cbcSMatt Macy 3572*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 3573*eda14cbcSMatt Macy retval = zpool_standard_error(hdl, errno, msg); 3574*eda14cbcSMatt Macy goto out; 3575*eda14cbcSMatt Macy } 3576*eda14cbcSMatt Macy 3577*eda14cbcSMatt Macy freelist = B_FALSE; 3578*eda14cbcSMatt Macy memory_err = B_FALSE; 3579*eda14cbcSMatt Macy 3580*eda14cbcSMatt Macy out: 3581*eda14cbcSMatt Macy if (varray != NULL) { 3582*eda14cbcSMatt Macy int v; 3583*eda14cbcSMatt Macy 3584*eda14cbcSMatt Macy for (v = 0; v < vcount; v++) 3585*eda14cbcSMatt Macy nvlist_free(varray[v]); 3586*eda14cbcSMatt Macy free(varray); 3587*eda14cbcSMatt Macy } 3588*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3589*eda14cbcSMatt Macy nvlist_free(zc_props); 3590*eda14cbcSMatt Macy nvlist_free(newconfig); 3591*eda14cbcSMatt Macy if (freelist) { 3592*eda14cbcSMatt Macy nvlist_free(*newroot); 3593*eda14cbcSMatt Macy *newroot = NULL; 3594*eda14cbcSMatt Macy } 3595*eda14cbcSMatt Macy 3596*eda14cbcSMatt Macy if (retval != 0) 3597*eda14cbcSMatt Macy return (retval); 3598*eda14cbcSMatt Macy 3599*eda14cbcSMatt Macy if (memory_err) 3600*eda14cbcSMatt Macy return (no_memory(hdl)); 3601*eda14cbcSMatt Macy 3602*eda14cbcSMatt Macy return (0); 3603*eda14cbcSMatt Macy } 3604*eda14cbcSMatt Macy 3605*eda14cbcSMatt Macy /* 3606*eda14cbcSMatt Macy * Remove the given device. 3607*eda14cbcSMatt Macy */ 3608*eda14cbcSMatt Macy int 3609*eda14cbcSMatt Macy zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 3610*eda14cbcSMatt Macy { 3611*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3612*eda14cbcSMatt Macy char msg[1024]; 3613*eda14cbcSMatt Macy nvlist_t *tgt; 3614*eda14cbcSMatt Macy boolean_t avail_spare, l2cache, islog; 3615*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3616*eda14cbcSMatt Macy uint64_t version; 3617*eda14cbcSMatt Macy 3618*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3619*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 3620*eda14cbcSMatt Macy 3621*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3622*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3623*eda14cbcSMatt Macy &islog)) == NULL) 3624*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3625*eda14cbcSMatt Macy 3626*eda14cbcSMatt Macy version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 3627*eda14cbcSMatt Macy if (islog && version < SPA_VERSION_HOLES) { 3628*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3629*eda14cbcSMatt Macy "pool must be upgraded to support log removal")); 3630*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADVERSION, msg)); 3631*eda14cbcSMatt Macy } 3632*eda14cbcSMatt Macy 3633*eda14cbcSMatt Macy zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 3634*eda14cbcSMatt Macy 3635*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 3636*eda14cbcSMatt Macy return (0); 3637*eda14cbcSMatt Macy 3638*eda14cbcSMatt Macy switch (errno) { 3639*eda14cbcSMatt Macy 3640*eda14cbcSMatt Macy case EINVAL: 3641*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3642*eda14cbcSMatt Macy "invalid config; all top-level vdevs must " 3643*eda14cbcSMatt Macy "have the same sector size and not be raidz.")); 3644*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 3645*eda14cbcSMatt Macy break; 3646*eda14cbcSMatt Macy 3647*eda14cbcSMatt Macy case EBUSY: 3648*eda14cbcSMatt Macy if (islog) { 3649*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3650*eda14cbcSMatt Macy "Mount encrypted datasets to replay logs.")); 3651*eda14cbcSMatt Macy } else { 3652*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3653*eda14cbcSMatt Macy "Pool busy; removal may already be in progress")); 3654*eda14cbcSMatt Macy } 3655*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BUSY, msg); 3656*eda14cbcSMatt Macy break; 3657*eda14cbcSMatt Macy 3658*eda14cbcSMatt Macy case EACCES: 3659*eda14cbcSMatt Macy if (islog) { 3660*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3661*eda14cbcSMatt Macy "Mount encrypted datasets to replay logs.")); 3662*eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BUSY, msg); 3663*eda14cbcSMatt Macy } else { 3664*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 3665*eda14cbcSMatt Macy } 3666*eda14cbcSMatt Macy break; 3667*eda14cbcSMatt Macy 3668*eda14cbcSMatt Macy default: 3669*eda14cbcSMatt Macy (void) zpool_standard_error(hdl, errno, msg); 3670*eda14cbcSMatt Macy } 3671*eda14cbcSMatt Macy return (-1); 3672*eda14cbcSMatt Macy } 3673*eda14cbcSMatt Macy 3674*eda14cbcSMatt Macy int 3675*eda14cbcSMatt Macy zpool_vdev_remove_cancel(zpool_handle_t *zhp) 3676*eda14cbcSMatt Macy { 3677*eda14cbcSMatt Macy zfs_cmd_t zc; 3678*eda14cbcSMatt Macy char msg[1024]; 3679*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3680*eda14cbcSMatt Macy 3681*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3682*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot cancel removal")); 3683*eda14cbcSMatt Macy 3684*eda14cbcSMatt Macy bzero(&zc, sizeof (zc)); 3685*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3686*eda14cbcSMatt Macy zc.zc_cookie = 1; 3687*eda14cbcSMatt Macy 3688*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 3689*eda14cbcSMatt Macy return (0); 3690*eda14cbcSMatt Macy 3691*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3692*eda14cbcSMatt Macy } 3693*eda14cbcSMatt Macy 3694*eda14cbcSMatt Macy int 3695*eda14cbcSMatt Macy zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path, 3696*eda14cbcSMatt Macy uint64_t *sizep) 3697*eda14cbcSMatt Macy { 3698*eda14cbcSMatt Macy char msg[1024]; 3699*eda14cbcSMatt Macy nvlist_t *tgt; 3700*eda14cbcSMatt Macy boolean_t avail_spare, l2cache, islog; 3701*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3702*eda14cbcSMatt Macy 3703*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3704*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"), 3705*eda14cbcSMatt Macy path); 3706*eda14cbcSMatt Macy 3707*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 3708*eda14cbcSMatt Macy &islog)) == NULL) 3709*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3710*eda14cbcSMatt Macy 3711*eda14cbcSMatt Macy if (avail_spare || l2cache || islog) { 3712*eda14cbcSMatt Macy *sizep = 0; 3713*eda14cbcSMatt Macy return (0); 3714*eda14cbcSMatt Macy } 3715*eda14cbcSMatt Macy 3716*eda14cbcSMatt Macy if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) { 3717*eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3718*eda14cbcSMatt Macy "indirect size not available")); 3719*eda14cbcSMatt Macy return (zfs_error(hdl, EINVAL, msg)); 3720*eda14cbcSMatt Macy } 3721*eda14cbcSMatt Macy return (0); 3722*eda14cbcSMatt Macy } 3723*eda14cbcSMatt Macy 3724*eda14cbcSMatt Macy /* 3725*eda14cbcSMatt Macy * Clear the errors for the pool, or the particular device if specified. 3726*eda14cbcSMatt Macy */ 3727*eda14cbcSMatt Macy int 3728*eda14cbcSMatt Macy zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3729*eda14cbcSMatt Macy { 3730*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3731*eda14cbcSMatt Macy char msg[1024]; 3732*eda14cbcSMatt Macy nvlist_t *tgt; 3733*eda14cbcSMatt Macy zpool_load_policy_t policy; 3734*eda14cbcSMatt Macy boolean_t avail_spare, l2cache; 3735*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3736*eda14cbcSMatt Macy nvlist_t *nvi = NULL; 3737*eda14cbcSMatt Macy int error; 3738*eda14cbcSMatt Macy 3739*eda14cbcSMatt Macy if (path) 3740*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3741*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3742*eda14cbcSMatt Macy path); 3743*eda14cbcSMatt Macy else 3744*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3745*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3746*eda14cbcSMatt Macy zhp->zpool_name); 3747*eda14cbcSMatt Macy 3748*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3749*eda14cbcSMatt Macy if (path) { 3750*eda14cbcSMatt Macy if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 3751*eda14cbcSMatt Macy &l2cache, NULL)) == NULL) 3752*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3753*eda14cbcSMatt Macy 3754*eda14cbcSMatt Macy /* 3755*eda14cbcSMatt Macy * Don't allow error clearing for hot spares. Do allow 3756*eda14cbcSMatt Macy * error clearing for l2cache devices. 3757*eda14cbcSMatt Macy */ 3758*eda14cbcSMatt Macy if (avail_spare) 3759*eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3760*eda14cbcSMatt Macy 3761*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 3762*eda14cbcSMatt Macy &zc.zc_guid) == 0); 3763*eda14cbcSMatt Macy } 3764*eda14cbcSMatt Macy 3765*eda14cbcSMatt Macy zpool_get_load_policy(rewindnvl, &policy); 3766*eda14cbcSMatt Macy zc.zc_cookie = policy.zlp_rewind; 3767*eda14cbcSMatt Macy 3768*eda14cbcSMatt Macy if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3769*eda14cbcSMatt Macy return (-1); 3770*eda14cbcSMatt Macy 3771*eda14cbcSMatt Macy if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3772*eda14cbcSMatt Macy return (-1); 3773*eda14cbcSMatt Macy 3774*eda14cbcSMatt Macy while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 3775*eda14cbcSMatt Macy errno == ENOMEM) { 3776*eda14cbcSMatt Macy if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 3777*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3778*eda14cbcSMatt Macy return (-1); 3779*eda14cbcSMatt Macy } 3780*eda14cbcSMatt Macy } 3781*eda14cbcSMatt Macy 3782*eda14cbcSMatt Macy if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) && 3783*eda14cbcSMatt Macy errno != EPERM && errno != EACCES)) { 3784*eda14cbcSMatt Macy if (policy.zlp_rewind & 3785*eda14cbcSMatt Macy (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3786*eda14cbcSMatt Macy (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3787*eda14cbcSMatt Macy zpool_rewind_exclaim(hdl, zc.zc_name, 3788*eda14cbcSMatt Macy ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), 3789*eda14cbcSMatt Macy nvi); 3790*eda14cbcSMatt Macy nvlist_free(nvi); 3791*eda14cbcSMatt Macy } 3792*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3793*eda14cbcSMatt Macy return (0); 3794*eda14cbcSMatt Macy } 3795*eda14cbcSMatt Macy 3796*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3797*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3798*eda14cbcSMatt Macy } 3799*eda14cbcSMatt Macy 3800*eda14cbcSMatt Macy /* 3801*eda14cbcSMatt Macy * Similar to zpool_clear(), but takes a GUID (used by fmd). 3802*eda14cbcSMatt Macy */ 3803*eda14cbcSMatt Macy int 3804*eda14cbcSMatt Macy zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 3805*eda14cbcSMatt Macy { 3806*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3807*eda14cbcSMatt Macy char msg[1024]; 3808*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3809*eda14cbcSMatt Macy 3810*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3811*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 3812*eda14cbcSMatt Macy (u_longlong_t)guid); 3813*eda14cbcSMatt Macy 3814*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3815*eda14cbcSMatt Macy zc.zc_guid = guid; 3816*eda14cbcSMatt Macy zc.zc_cookie = ZPOOL_NO_REWIND; 3817*eda14cbcSMatt Macy 3818*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0) 3819*eda14cbcSMatt Macy return (0); 3820*eda14cbcSMatt Macy 3821*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3822*eda14cbcSMatt Macy } 3823*eda14cbcSMatt Macy 3824*eda14cbcSMatt Macy /* 3825*eda14cbcSMatt Macy * Change the GUID for a pool. 3826*eda14cbcSMatt Macy */ 3827*eda14cbcSMatt Macy int 3828*eda14cbcSMatt Macy zpool_reguid(zpool_handle_t *zhp) 3829*eda14cbcSMatt Macy { 3830*eda14cbcSMatt Macy char msg[1024]; 3831*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 3832*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3833*eda14cbcSMatt Macy 3834*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), 3835*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3836*eda14cbcSMatt Macy 3837*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3838*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3839*eda14cbcSMatt Macy return (0); 3840*eda14cbcSMatt Macy 3841*eda14cbcSMatt Macy return (zpool_standard_error(hdl, errno, msg)); 3842*eda14cbcSMatt Macy } 3843*eda14cbcSMatt Macy 3844*eda14cbcSMatt Macy /* 3845*eda14cbcSMatt Macy * Reopen the pool. 3846*eda14cbcSMatt Macy */ 3847*eda14cbcSMatt Macy int 3848*eda14cbcSMatt Macy zpool_reopen_one(zpool_handle_t *zhp, void *data) 3849*eda14cbcSMatt Macy { 3850*eda14cbcSMatt Macy libzfs_handle_t *hdl = zpool_get_handle(zhp); 3851*eda14cbcSMatt Macy const char *pool_name = zpool_get_name(zhp); 3852*eda14cbcSMatt Macy boolean_t *scrub_restart = data; 3853*eda14cbcSMatt Macy int error; 3854*eda14cbcSMatt Macy 3855*eda14cbcSMatt Macy error = lzc_reopen(pool_name, *scrub_restart); 3856*eda14cbcSMatt Macy if (error) { 3857*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, error, 3858*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name)); 3859*eda14cbcSMatt Macy } 3860*eda14cbcSMatt Macy 3861*eda14cbcSMatt Macy return (0); 3862*eda14cbcSMatt Macy } 3863*eda14cbcSMatt Macy 3864*eda14cbcSMatt Macy /* call into libzfs_core to execute the sync IOCTL per pool */ 3865*eda14cbcSMatt Macy int 3866*eda14cbcSMatt Macy zpool_sync_one(zpool_handle_t *zhp, void *data) 3867*eda14cbcSMatt Macy { 3868*eda14cbcSMatt Macy int ret; 3869*eda14cbcSMatt Macy libzfs_handle_t *hdl = zpool_get_handle(zhp); 3870*eda14cbcSMatt Macy const char *pool_name = zpool_get_name(zhp); 3871*eda14cbcSMatt Macy boolean_t *force = data; 3872*eda14cbcSMatt Macy nvlist_t *innvl = fnvlist_alloc(); 3873*eda14cbcSMatt Macy 3874*eda14cbcSMatt Macy fnvlist_add_boolean_value(innvl, "force", *force); 3875*eda14cbcSMatt Macy if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) { 3876*eda14cbcSMatt Macy nvlist_free(innvl); 3877*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, ret, 3878*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name)); 3879*eda14cbcSMatt Macy } 3880*eda14cbcSMatt Macy nvlist_free(innvl); 3881*eda14cbcSMatt Macy 3882*eda14cbcSMatt Macy return (0); 3883*eda14cbcSMatt Macy } 3884*eda14cbcSMatt Macy 3885*eda14cbcSMatt Macy #define PATH_BUF_LEN 64 3886*eda14cbcSMatt Macy 3887*eda14cbcSMatt Macy /* 3888*eda14cbcSMatt Macy * Given a vdev, return the name to display in iostat. If the vdev has a path, 3889*eda14cbcSMatt Macy * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3890*eda14cbcSMatt Macy * We also check if this is a whole disk, in which case we strip off the 3891*eda14cbcSMatt Macy * trailing 's0' slice name. 3892*eda14cbcSMatt Macy * 3893*eda14cbcSMatt Macy * This routine is also responsible for identifying when disks have been 3894*eda14cbcSMatt Macy * reconfigured in a new location. The kernel will have opened the device by 3895*eda14cbcSMatt Macy * devid, but the path will still refer to the old location. To catch this, we 3896*eda14cbcSMatt Macy * first do a path -> devid translation (which is fast for the common case). If 3897*eda14cbcSMatt Macy * the devid matches, we're done. If not, we do a reverse devid -> path 3898*eda14cbcSMatt Macy * translation and issue the appropriate ioctl() to update the path of the vdev. 3899*eda14cbcSMatt Macy * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3900*eda14cbcSMatt Macy * of these checks. 3901*eda14cbcSMatt Macy */ 3902*eda14cbcSMatt Macy char * 3903*eda14cbcSMatt Macy zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 3904*eda14cbcSMatt Macy int name_flags) 3905*eda14cbcSMatt Macy { 3906*eda14cbcSMatt Macy char *path, *type, *env; 3907*eda14cbcSMatt Macy uint64_t value; 3908*eda14cbcSMatt Macy char buf[PATH_BUF_LEN]; 3909*eda14cbcSMatt Macy char tmpbuf[PATH_BUF_LEN]; 3910*eda14cbcSMatt Macy 3911*eda14cbcSMatt Macy /* 3912*eda14cbcSMatt Macy * vdev_name will be "root"/"root-0" for the root vdev, but it is the 3913*eda14cbcSMatt Macy * zpool name that will be displayed to the user. 3914*eda14cbcSMatt Macy */ 3915*eda14cbcSMatt Macy verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0); 3916*eda14cbcSMatt Macy if (zhp != NULL && strcmp(type, "root") == 0) 3917*eda14cbcSMatt Macy return (zfs_strdup(hdl, zpool_get_name(zhp))); 3918*eda14cbcSMatt Macy 3919*eda14cbcSMatt Macy env = getenv("ZPOOL_VDEV_NAME_PATH"); 3920*eda14cbcSMatt Macy if (env && (strtoul(env, NULL, 0) > 0 || 3921*eda14cbcSMatt Macy !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3922*eda14cbcSMatt Macy name_flags |= VDEV_NAME_PATH; 3923*eda14cbcSMatt Macy 3924*eda14cbcSMatt Macy env = getenv("ZPOOL_VDEV_NAME_GUID"); 3925*eda14cbcSMatt Macy if (env && (strtoul(env, NULL, 0) > 0 || 3926*eda14cbcSMatt Macy !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3927*eda14cbcSMatt Macy name_flags |= VDEV_NAME_GUID; 3928*eda14cbcSMatt Macy 3929*eda14cbcSMatt Macy env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS"); 3930*eda14cbcSMatt Macy if (env && (strtoul(env, NULL, 0) > 0 || 3931*eda14cbcSMatt Macy !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3932*eda14cbcSMatt Macy name_flags |= VDEV_NAME_FOLLOW_LINKS; 3933*eda14cbcSMatt Macy 3934*eda14cbcSMatt Macy if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 || 3935*eda14cbcSMatt Macy name_flags & VDEV_NAME_GUID) { 3936*eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value); 3937*eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value); 3938*eda14cbcSMatt Macy path = buf; 3939*eda14cbcSMatt Macy } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3940*eda14cbcSMatt Macy if (name_flags & VDEV_NAME_FOLLOW_LINKS) { 3941*eda14cbcSMatt Macy char *rp = realpath(path, NULL); 3942*eda14cbcSMatt Macy if (rp) { 3943*eda14cbcSMatt Macy strlcpy(buf, rp, sizeof (buf)); 3944*eda14cbcSMatt Macy path = buf; 3945*eda14cbcSMatt Macy free(rp); 3946*eda14cbcSMatt Macy } 3947*eda14cbcSMatt Macy } 3948*eda14cbcSMatt Macy 3949*eda14cbcSMatt Macy /* 3950*eda14cbcSMatt Macy * For a block device only use the name. 3951*eda14cbcSMatt Macy */ 3952*eda14cbcSMatt Macy if ((strcmp(type, VDEV_TYPE_DISK) == 0) && 3953*eda14cbcSMatt Macy !(name_flags & VDEV_NAME_PATH)) { 3954*eda14cbcSMatt Macy path = zfs_strip_path(path); 3955*eda14cbcSMatt Macy } 3956*eda14cbcSMatt Macy 3957*eda14cbcSMatt Macy /* 3958*eda14cbcSMatt Macy * Remove the partition from the path it this is a whole disk. 3959*eda14cbcSMatt Macy */ 3960*eda14cbcSMatt Macy if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value) 3961*eda14cbcSMatt Macy == 0 && value && !(name_flags & VDEV_NAME_PATH)) { 3962*eda14cbcSMatt Macy return (zfs_strip_partition(path)); 3963*eda14cbcSMatt Macy } 3964*eda14cbcSMatt Macy } else { 3965*eda14cbcSMatt Macy path = type; 3966*eda14cbcSMatt Macy 3967*eda14cbcSMatt Macy /* 3968*eda14cbcSMatt Macy * If it's a raidz device, we need to stick in the parity level. 3969*eda14cbcSMatt Macy */ 3970*eda14cbcSMatt Macy if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 3971*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 3972*eda14cbcSMatt Macy &value) == 0); 3973*eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), "%s%llu", path, 3974*eda14cbcSMatt Macy (u_longlong_t)value); 3975*eda14cbcSMatt Macy path = buf; 3976*eda14cbcSMatt Macy } 3977*eda14cbcSMatt Macy 3978*eda14cbcSMatt Macy /* 3979*eda14cbcSMatt Macy * We identify each top-level vdev by using a <type-id> 3980*eda14cbcSMatt Macy * naming convention. 3981*eda14cbcSMatt Macy */ 3982*eda14cbcSMatt Macy if (name_flags & VDEV_NAME_TYPE_ID) { 3983*eda14cbcSMatt Macy uint64_t id; 3984*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 3985*eda14cbcSMatt Macy &id) == 0); 3986*eda14cbcSMatt Macy (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu", 3987*eda14cbcSMatt Macy path, (u_longlong_t)id); 3988*eda14cbcSMatt Macy path = tmpbuf; 3989*eda14cbcSMatt Macy } 3990*eda14cbcSMatt Macy } 3991*eda14cbcSMatt Macy 3992*eda14cbcSMatt Macy return (zfs_strdup(hdl, path)); 3993*eda14cbcSMatt Macy } 3994*eda14cbcSMatt Macy 3995*eda14cbcSMatt Macy static int 3996*eda14cbcSMatt Macy zbookmark_mem_compare(const void *a, const void *b) 3997*eda14cbcSMatt Macy { 3998*eda14cbcSMatt Macy return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3999*eda14cbcSMatt Macy } 4000*eda14cbcSMatt Macy 4001*eda14cbcSMatt Macy /* 4002*eda14cbcSMatt Macy * Retrieve the persistent error log, uniquify the members, and return to the 4003*eda14cbcSMatt Macy * caller. 4004*eda14cbcSMatt Macy */ 4005*eda14cbcSMatt Macy int 4006*eda14cbcSMatt Macy zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 4007*eda14cbcSMatt Macy { 4008*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4009*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 4010*eda14cbcSMatt Macy uint64_t count; 4011*eda14cbcSMatt Macy zbookmark_phys_t *zb = NULL; 4012*eda14cbcSMatt Macy int i; 4013*eda14cbcSMatt Macy 4014*eda14cbcSMatt Macy /* 4015*eda14cbcSMatt Macy * Retrieve the raw error list from the kernel. If the number of errors 4016*eda14cbcSMatt Macy * has increased, allocate more space and continue until we get the 4017*eda14cbcSMatt Macy * entire list. 4018*eda14cbcSMatt Macy */ 4019*eda14cbcSMatt Macy verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 4020*eda14cbcSMatt Macy &count) == 0); 4021*eda14cbcSMatt Macy if (count == 0) 4022*eda14cbcSMatt Macy return (0); 4023*eda14cbcSMatt Macy zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 4024*eda14cbcSMatt Macy count * sizeof (zbookmark_phys_t)); 4025*eda14cbcSMatt Macy zc.zc_nvlist_dst_size = count; 4026*eda14cbcSMatt Macy (void) strcpy(zc.zc_name, zhp->zpool_name); 4027*eda14cbcSMatt Macy for (;;) { 4028*eda14cbcSMatt Macy if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG, 4029*eda14cbcSMatt Macy &zc) != 0) { 4030*eda14cbcSMatt Macy free((void *)(uintptr_t)zc.zc_nvlist_dst); 4031*eda14cbcSMatt Macy if (errno == ENOMEM) { 4032*eda14cbcSMatt Macy void *dst; 4033*eda14cbcSMatt Macy 4034*eda14cbcSMatt Macy count = zc.zc_nvlist_dst_size; 4035*eda14cbcSMatt Macy dst = zfs_alloc(zhp->zpool_hdl, count * 4036*eda14cbcSMatt Macy sizeof (zbookmark_phys_t)); 4037*eda14cbcSMatt Macy zc.zc_nvlist_dst = (uintptr_t)dst; 4038*eda14cbcSMatt Macy } else { 4039*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, errno, 4040*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "errors: List of " 4041*eda14cbcSMatt Macy "errors unavailable"))); 4042*eda14cbcSMatt Macy } 4043*eda14cbcSMatt Macy } else { 4044*eda14cbcSMatt Macy break; 4045*eda14cbcSMatt Macy } 4046*eda14cbcSMatt Macy } 4047*eda14cbcSMatt Macy 4048*eda14cbcSMatt Macy /* 4049*eda14cbcSMatt Macy * Sort the resulting bookmarks. This is a little confusing due to the 4050*eda14cbcSMatt Macy * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 4051*eda14cbcSMatt Macy * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks 4052*eda14cbcSMatt Macy * _not_ copied as part of the process. So we point the start of our 4053*eda14cbcSMatt Macy * array appropriate and decrement the total number of elements. 4054*eda14cbcSMatt Macy */ 4055*eda14cbcSMatt Macy zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 4056*eda14cbcSMatt Macy zc.zc_nvlist_dst_size; 4057*eda14cbcSMatt Macy count -= zc.zc_nvlist_dst_size; 4058*eda14cbcSMatt Macy 4059*eda14cbcSMatt Macy qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 4060*eda14cbcSMatt Macy 4061*eda14cbcSMatt Macy verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 4062*eda14cbcSMatt Macy 4063*eda14cbcSMatt Macy /* 4064*eda14cbcSMatt Macy * Fill in the nverrlistp with nvlist's of dataset and object numbers. 4065*eda14cbcSMatt Macy */ 4066*eda14cbcSMatt Macy for (i = 0; i < count; i++) { 4067*eda14cbcSMatt Macy nvlist_t *nv; 4068*eda14cbcSMatt Macy 4069*eda14cbcSMatt Macy /* ignoring zb_blkid and zb_level for now */ 4070*eda14cbcSMatt Macy if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 4071*eda14cbcSMatt Macy zb[i-1].zb_object == zb[i].zb_object) 4072*eda14cbcSMatt Macy continue; 4073*eda14cbcSMatt Macy 4074*eda14cbcSMatt Macy if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 4075*eda14cbcSMatt Macy goto nomem; 4076*eda14cbcSMatt Macy if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 4077*eda14cbcSMatt Macy zb[i].zb_objset) != 0) { 4078*eda14cbcSMatt Macy nvlist_free(nv); 4079*eda14cbcSMatt Macy goto nomem; 4080*eda14cbcSMatt Macy } 4081*eda14cbcSMatt Macy if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 4082*eda14cbcSMatt Macy zb[i].zb_object) != 0) { 4083*eda14cbcSMatt Macy nvlist_free(nv); 4084*eda14cbcSMatt Macy goto nomem; 4085*eda14cbcSMatt Macy } 4086*eda14cbcSMatt Macy if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 4087*eda14cbcSMatt Macy nvlist_free(nv); 4088*eda14cbcSMatt Macy goto nomem; 4089*eda14cbcSMatt Macy } 4090*eda14cbcSMatt Macy nvlist_free(nv); 4091*eda14cbcSMatt Macy } 4092*eda14cbcSMatt Macy 4093*eda14cbcSMatt Macy free((void *)(uintptr_t)zc.zc_nvlist_dst); 4094*eda14cbcSMatt Macy return (0); 4095*eda14cbcSMatt Macy 4096*eda14cbcSMatt Macy nomem: 4097*eda14cbcSMatt Macy free((void *)(uintptr_t)zc.zc_nvlist_dst); 4098*eda14cbcSMatt Macy return (no_memory(zhp->zpool_hdl)); 4099*eda14cbcSMatt Macy } 4100*eda14cbcSMatt Macy 4101*eda14cbcSMatt Macy /* 4102*eda14cbcSMatt Macy * Upgrade a ZFS pool to the latest on-disk version. 4103*eda14cbcSMatt Macy */ 4104*eda14cbcSMatt Macy int 4105*eda14cbcSMatt Macy zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 4106*eda14cbcSMatt Macy { 4107*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4108*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 4109*eda14cbcSMatt Macy 4110*eda14cbcSMatt Macy (void) strcpy(zc.zc_name, zhp->zpool_name); 4111*eda14cbcSMatt Macy zc.zc_cookie = new_version; 4112*eda14cbcSMatt Macy 4113*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 4114*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, errno, 4115*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 4116*eda14cbcSMatt Macy zhp->zpool_name)); 4117*eda14cbcSMatt Macy return (0); 4118*eda14cbcSMatt Macy } 4119*eda14cbcSMatt Macy 4120*eda14cbcSMatt Macy void 4121*eda14cbcSMatt Macy zfs_save_arguments(int argc, char **argv, char *string, int len) 4122*eda14cbcSMatt Macy { 4123*eda14cbcSMatt Macy int i; 4124*eda14cbcSMatt Macy 4125*eda14cbcSMatt Macy (void) strlcpy(string, basename(argv[0]), len); 4126*eda14cbcSMatt Macy for (i = 1; i < argc; i++) { 4127*eda14cbcSMatt Macy (void) strlcat(string, " ", len); 4128*eda14cbcSMatt Macy (void) strlcat(string, argv[i], len); 4129*eda14cbcSMatt Macy } 4130*eda14cbcSMatt Macy } 4131*eda14cbcSMatt Macy 4132*eda14cbcSMatt Macy int 4133*eda14cbcSMatt Macy zpool_log_history(libzfs_handle_t *hdl, const char *message) 4134*eda14cbcSMatt Macy { 4135*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4136*eda14cbcSMatt Macy nvlist_t *args; 4137*eda14cbcSMatt Macy int err; 4138*eda14cbcSMatt Macy 4139*eda14cbcSMatt Macy args = fnvlist_alloc(); 4140*eda14cbcSMatt Macy fnvlist_add_string(args, "message", message); 4141*eda14cbcSMatt Macy err = zcmd_write_src_nvlist(hdl, &zc, args); 4142*eda14cbcSMatt Macy if (err == 0) 4143*eda14cbcSMatt Macy err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc); 4144*eda14cbcSMatt Macy nvlist_free(args); 4145*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 4146*eda14cbcSMatt Macy return (err); 4147*eda14cbcSMatt Macy } 4148*eda14cbcSMatt Macy 4149*eda14cbcSMatt Macy /* 4150*eda14cbcSMatt Macy * Perform ioctl to get some command history of a pool. 4151*eda14cbcSMatt Macy * 4152*eda14cbcSMatt Macy * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 4153*eda14cbcSMatt Macy * logical offset of the history buffer to start reading from. 4154*eda14cbcSMatt Macy * 4155*eda14cbcSMatt Macy * Upon return, 'off' is the next logical offset to read from and 4156*eda14cbcSMatt Macy * 'len' is the actual amount of bytes read into 'buf'. 4157*eda14cbcSMatt Macy */ 4158*eda14cbcSMatt Macy static int 4159*eda14cbcSMatt Macy get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 4160*eda14cbcSMatt Macy { 4161*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4162*eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zpool_hdl; 4163*eda14cbcSMatt Macy 4164*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4165*eda14cbcSMatt Macy 4166*eda14cbcSMatt Macy zc.zc_history = (uint64_t)(uintptr_t)buf; 4167*eda14cbcSMatt Macy zc.zc_history_len = *len; 4168*eda14cbcSMatt Macy zc.zc_history_offset = *off; 4169*eda14cbcSMatt Macy 4170*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 4171*eda14cbcSMatt Macy switch (errno) { 4172*eda14cbcSMatt Macy case EPERM: 4173*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_PERM, 4174*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4175*eda14cbcSMatt Macy "cannot show history for pool '%s'"), 4176*eda14cbcSMatt Macy zhp->zpool_name)); 4177*eda14cbcSMatt Macy case ENOENT: 4178*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 4179*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get history for pool " 4180*eda14cbcSMatt Macy "'%s'"), zhp->zpool_name)); 4181*eda14cbcSMatt Macy case ENOTSUP: 4182*eda14cbcSMatt Macy return (zfs_error_fmt(hdl, EZFS_BADVERSION, 4183*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get history for pool " 4184*eda14cbcSMatt Macy "'%s', pool must be upgraded"), zhp->zpool_name)); 4185*eda14cbcSMatt Macy default: 4186*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, errno, 4187*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4188*eda14cbcSMatt Macy "cannot get history for '%s'"), zhp->zpool_name)); 4189*eda14cbcSMatt Macy } 4190*eda14cbcSMatt Macy } 4191*eda14cbcSMatt Macy 4192*eda14cbcSMatt Macy *len = zc.zc_history_len; 4193*eda14cbcSMatt Macy *off = zc.zc_history_offset; 4194*eda14cbcSMatt Macy 4195*eda14cbcSMatt Macy return (0); 4196*eda14cbcSMatt Macy } 4197*eda14cbcSMatt Macy 4198*eda14cbcSMatt Macy /* 4199*eda14cbcSMatt Macy * Retrieve the command history of a pool. 4200*eda14cbcSMatt Macy */ 4201*eda14cbcSMatt Macy int 4202*eda14cbcSMatt Macy zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off, 4203*eda14cbcSMatt Macy boolean_t *eof) 4204*eda14cbcSMatt Macy { 4205*eda14cbcSMatt Macy char *buf; 4206*eda14cbcSMatt Macy int buflen = 128 * 1024; 4207*eda14cbcSMatt Macy nvlist_t **records = NULL; 4208*eda14cbcSMatt Macy uint_t numrecords = 0; 4209*eda14cbcSMatt Macy int err, i; 4210*eda14cbcSMatt Macy uint64_t start = *off; 4211*eda14cbcSMatt Macy 4212*eda14cbcSMatt Macy buf = malloc(buflen); 4213*eda14cbcSMatt Macy if (buf == NULL) 4214*eda14cbcSMatt Macy return (ENOMEM); 4215*eda14cbcSMatt Macy /* process about 1MB a time */ 4216*eda14cbcSMatt Macy while (*off - start < 1024 * 1024) { 4217*eda14cbcSMatt Macy uint64_t bytes_read = buflen; 4218*eda14cbcSMatt Macy uint64_t leftover; 4219*eda14cbcSMatt Macy 4220*eda14cbcSMatt Macy if ((err = get_history(zhp, buf, off, &bytes_read)) != 0) 4221*eda14cbcSMatt Macy break; 4222*eda14cbcSMatt Macy 4223*eda14cbcSMatt Macy /* if nothing else was read in, we're at EOF, just return */ 4224*eda14cbcSMatt Macy if (!bytes_read) { 4225*eda14cbcSMatt Macy *eof = B_TRUE; 4226*eda14cbcSMatt Macy break; 4227*eda14cbcSMatt Macy } 4228*eda14cbcSMatt Macy 4229*eda14cbcSMatt Macy if ((err = zpool_history_unpack(buf, bytes_read, 4230*eda14cbcSMatt Macy &leftover, &records, &numrecords)) != 0) 4231*eda14cbcSMatt Macy break; 4232*eda14cbcSMatt Macy *off -= leftover; 4233*eda14cbcSMatt Macy if (leftover == bytes_read) { 4234*eda14cbcSMatt Macy /* 4235*eda14cbcSMatt Macy * no progress made, because buffer is not big enough 4236*eda14cbcSMatt Macy * to hold this record; resize and retry. 4237*eda14cbcSMatt Macy */ 4238*eda14cbcSMatt Macy buflen *= 2; 4239*eda14cbcSMatt Macy free(buf); 4240*eda14cbcSMatt Macy buf = malloc(buflen); 4241*eda14cbcSMatt Macy if (buf == NULL) 4242*eda14cbcSMatt Macy return (ENOMEM); 4243*eda14cbcSMatt Macy } 4244*eda14cbcSMatt Macy } 4245*eda14cbcSMatt Macy 4246*eda14cbcSMatt Macy free(buf); 4247*eda14cbcSMatt Macy 4248*eda14cbcSMatt Macy if (!err) { 4249*eda14cbcSMatt Macy verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 4250*eda14cbcSMatt Macy verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 4251*eda14cbcSMatt Macy records, numrecords) == 0); 4252*eda14cbcSMatt Macy } 4253*eda14cbcSMatt Macy for (i = 0; i < numrecords; i++) 4254*eda14cbcSMatt Macy nvlist_free(records[i]); 4255*eda14cbcSMatt Macy free(records); 4256*eda14cbcSMatt Macy 4257*eda14cbcSMatt Macy return (err); 4258*eda14cbcSMatt Macy } 4259*eda14cbcSMatt Macy 4260*eda14cbcSMatt Macy /* 4261*eda14cbcSMatt Macy * Retrieve the next event given the passed 'zevent_fd' file descriptor. 4262*eda14cbcSMatt Macy * If there is a new event available 'nvp' will contain a newly allocated 4263*eda14cbcSMatt Macy * nvlist and 'dropped' will be set to the number of missed events since 4264*eda14cbcSMatt Macy * the last call to this function. When 'nvp' is set to NULL it indicates 4265*eda14cbcSMatt Macy * no new events are available. In either case the function returns 0 and 4266*eda14cbcSMatt Macy * it is up to the caller to free 'nvp'. In the case of a fatal error the 4267*eda14cbcSMatt Macy * function will return a non-zero value. When the function is called in 4268*eda14cbcSMatt Macy * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed), 4269*eda14cbcSMatt Macy * it will not return until a new event is available. 4270*eda14cbcSMatt Macy */ 4271*eda14cbcSMatt Macy int 4272*eda14cbcSMatt Macy zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp, 4273*eda14cbcSMatt Macy int *dropped, unsigned flags, int zevent_fd) 4274*eda14cbcSMatt Macy { 4275*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4276*eda14cbcSMatt Macy int error = 0; 4277*eda14cbcSMatt Macy 4278*eda14cbcSMatt Macy *nvp = NULL; 4279*eda14cbcSMatt Macy *dropped = 0; 4280*eda14cbcSMatt Macy zc.zc_cleanup_fd = zevent_fd; 4281*eda14cbcSMatt Macy 4282*eda14cbcSMatt Macy if (flags & ZEVENT_NONBLOCK) 4283*eda14cbcSMatt Macy zc.zc_guid = ZEVENT_NONBLOCK; 4284*eda14cbcSMatt Macy 4285*eda14cbcSMatt Macy if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0) 4286*eda14cbcSMatt Macy return (-1); 4287*eda14cbcSMatt Macy 4288*eda14cbcSMatt Macy retry: 4289*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) { 4290*eda14cbcSMatt Macy switch (errno) { 4291*eda14cbcSMatt Macy case ESHUTDOWN: 4292*eda14cbcSMatt Macy error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 4293*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "zfs shutdown")); 4294*eda14cbcSMatt Macy goto out; 4295*eda14cbcSMatt Macy case ENOENT: 4296*eda14cbcSMatt Macy /* Blocking error case should not occur */ 4297*eda14cbcSMatt Macy if (!(flags & ZEVENT_NONBLOCK)) 4298*eda14cbcSMatt Macy error = zpool_standard_error_fmt(hdl, errno, 4299*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4300*eda14cbcSMatt Macy 4301*eda14cbcSMatt Macy goto out; 4302*eda14cbcSMatt Macy case ENOMEM: 4303*eda14cbcSMatt Macy if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 4304*eda14cbcSMatt Macy error = zfs_error_fmt(hdl, EZFS_NOMEM, 4305*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4306*eda14cbcSMatt Macy goto out; 4307*eda14cbcSMatt Macy } else { 4308*eda14cbcSMatt Macy goto retry; 4309*eda14cbcSMatt Macy } 4310*eda14cbcSMatt Macy default: 4311*eda14cbcSMatt Macy error = zpool_standard_error_fmt(hdl, errno, 4312*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4313*eda14cbcSMatt Macy goto out; 4314*eda14cbcSMatt Macy } 4315*eda14cbcSMatt Macy } 4316*eda14cbcSMatt Macy 4317*eda14cbcSMatt Macy error = zcmd_read_dst_nvlist(hdl, &zc, nvp); 4318*eda14cbcSMatt Macy if (error != 0) 4319*eda14cbcSMatt Macy goto out; 4320*eda14cbcSMatt Macy 4321*eda14cbcSMatt Macy *dropped = (int)zc.zc_cookie; 4322*eda14cbcSMatt Macy out: 4323*eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 4324*eda14cbcSMatt Macy 4325*eda14cbcSMatt Macy return (error); 4326*eda14cbcSMatt Macy } 4327*eda14cbcSMatt Macy 4328*eda14cbcSMatt Macy /* 4329*eda14cbcSMatt Macy * Clear all events. 4330*eda14cbcSMatt Macy */ 4331*eda14cbcSMatt Macy int 4332*eda14cbcSMatt Macy zpool_events_clear(libzfs_handle_t *hdl, int *count) 4333*eda14cbcSMatt Macy { 4334*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4335*eda14cbcSMatt Macy char msg[1024]; 4336*eda14cbcSMatt Macy 4337*eda14cbcSMatt Macy (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 4338*eda14cbcSMatt Macy "cannot clear events")); 4339*eda14cbcSMatt Macy 4340*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0) 4341*eda14cbcSMatt Macy return (zpool_standard_error_fmt(hdl, errno, msg)); 4342*eda14cbcSMatt Macy 4343*eda14cbcSMatt Macy if (count != NULL) 4344*eda14cbcSMatt Macy *count = (int)zc.zc_cookie; /* # of events cleared */ 4345*eda14cbcSMatt Macy 4346*eda14cbcSMatt Macy return (0); 4347*eda14cbcSMatt Macy } 4348*eda14cbcSMatt Macy 4349*eda14cbcSMatt Macy /* 4350*eda14cbcSMatt Macy * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for 4351*eda14cbcSMatt Macy * the passed zevent_fd file handle. On success zero is returned, 4352*eda14cbcSMatt Macy * otherwise -1 is returned and hdl->libzfs_error is set to the errno. 4353*eda14cbcSMatt Macy */ 4354*eda14cbcSMatt Macy int 4355*eda14cbcSMatt Macy zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd) 4356*eda14cbcSMatt Macy { 4357*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4358*eda14cbcSMatt Macy int error = 0; 4359*eda14cbcSMatt Macy 4360*eda14cbcSMatt Macy zc.zc_guid = eid; 4361*eda14cbcSMatt Macy zc.zc_cleanup_fd = zevent_fd; 4362*eda14cbcSMatt Macy 4363*eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) { 4364*eda14cbcSMatt Macy switch (errno) { 4365*eda14cbcSMatt Macy case ENOENT: 4366*eda14cbcSMatt Macy error = zfs_error_fmt(hdl, EZFS_NOENT, 4367*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4368*eda14cbcSMatt Macy break; 4369*eda14cbcSMatt Macy 4370*eda14cbcSMatt Macy case ENOMEM: 4371*eda14cbcSMatt Macy error = zfs_error_fmt(hdl, EZFS_NOMEM, 4372*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4373*eda14cbcSMatt Macy break; 4374*eda14cbcSMatt Macy 4375*eda14cbcSMatt Macy default: 4376*eda14cbcSMatt Macy error = zpool_standard_error_fmt(hdl, errno, 4377*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot get event")); 4378*eda14cbcSMatt Macy break; 4379*eda14cbcSMatt Macy } 4380*eda14cbcSMatt Macy } 4381*eda14cbcSMatt Macy 4382*eda14cbcSMatt Macy return (error); 4383*eda14cbcSMatt Macy } 4384*eda14cbcSMatt Macy 4385*eda14cbcSMatt Macy static void 4386*eda14cbcSMatt Macy zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4387*eda14cbcSMatt Macy char *pathname, size_t len, boolean_t always_unmounted) 4388*eda14cbcSMatt Macy { 4389*eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4390*eda14cbcSMatt Macy boolean_t mounted = B_FALSE; 4391*eda14cbcSMatt Macy char *mntpnt = NULL; 4392*eda14cbcSMatt Macy char dsname[ZFS_MAX_DATASET_NAME_LEN]; 4393*eda14cbcSMatt Macy 4394*eda14cbcSMatt Macy if (dsobj == 0) { 4395*eda14cbcSMatt Macy /* special case for the MOS */ 4396*eda14cbcSMatt Macy (void) snprintf(pathname, len, "<metadata>:<0x%llx>", 4397*eda14cbcSMatt Macy (longlong_t)obj); 4398*eda14cbcSMatt Macy return; 4399*eda14cbcSMatt Macy } 4400*eda14cbcSMatt Macy 4401*eda14cbcSMatt Macy /* get the dataset's name */ 4402*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 4403*eda14cbcSMatt Macy zc.zc_obj = dsobj; 4404*eda14cbcSMatt Macy if (zfs_ioctl(zhp->zpool_hdl, 4405*eda14cbcSMatt Macy ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 4406*eda14cbcSMatt Macy /* just write out a path of two object numbers */ 4407*eda14cbcSMatt Macy (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 4408*eda14cbcSMatt Macy (longlong_t)dsobj, (longlong_t)obj); 4409*eda14cbcSMatt Macy return; 4410*eda14cbcSMatt Macy } 4411*eda14cbcSMatt Macy (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 4412*eda14cbcSMatt Macy 4413*eda14cbcSMatt Macy /* find out if the dataset is mounted */ 4414*eda14cbcSMatt Macy mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname, 4415*eda14cbcSMatt Macy &mntpnt); 4416*eda14cbcSMatt Macy 4417*eda14cbcSMatt Macy /* get the corrupted object's path */ 4418*eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 4419*eda14cbcSMatt Macy zc.zc_obj = obj; 4420*eda14cbcSMatt Macy if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH, 4421*eda14cbcSMatt Macy &zc) == 0) { 4422*eda14cbcSMatt Macy if (mounted) { 4423*eda14cbcSMatt Macy (void) snprintf(pathname, len, "%s%s", mntpnt, 4424*eda14cbcSMatt Macy zc.zc_value); 4425*eda14cbcSMatt Macy } else { 4426*eda14cbcSMatt Macy (void) snprintf(pathname, len, "%s:%s", 4427*eda14cbcSMatt Macy dsname, zc.zc_value); 4428*eda14cbcSMatt Macy } 4429*eda14cbcSMatt Macy } else { 4430*eda14cbcSMatt Macy (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, 4431*eda14cbcSMatt Macy (longlong_t)obj); 4432*eda14cbcSMatt Macy } 4433*eda14cbcSMatt Macy free(mntpnt); 4434*eda14cbcSMatt Macy } 4435*eda14cbcSMatt Macy 4436*eda14cbcSMatt Macy void 4437*eda14cbcSMatt Macy zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4438*eda14cbcSMatt Macy char *pathname, size_t len) 4439*eda14cbcSMatt Macy { 4440*eda14cbcSMatt Macy zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE); 4441*eda14cbcSMatt Macy } 4442*eda14cbcSMatt Macy 4443*eda14cbcSMatt Macy void 4444*eda14cbcSMatt Macy zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 4445*eda14cbcSMatt Macy char *pathname, size_t len) 4446*eda14cbcSMatt Macy { 4447*eda14cbcSMatt Macy zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE); 4448*eda14cbcSMatt Macy } 4449*eda14cbcSMatt Macy /* 4450*eda14cbcSMatt Macy * Wait while the specified activity is in progress in the pool. 4451*eda14cbcSMatt Macy */ 4452*eda14cbcSMatt Macy int 4453*eda14cbcSMatt Macy zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity) 4454*eda14cbcSMatt Macy { 4455*eda14cbcSMatt Macy boolean_t missing; 4456*eda14cbcSMatt Macy 4457*eda14cbcSMatt Macy int error = zpool_wait_status(zhp, activity, &missing, NULL); 4458*eda14cbcSMatt Macy 4459*eda14cbcSMatt Macy if (missing) { 4460*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT, 4461*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"), 4462*eda14cbcSMatt Macy zhp->zpool_name); 4463*eda14cbcSMatt Macy return (ENOENT); 4464*eda14cbcSMatt Macy } else { 4465*eda14cbcSMatt Macy return (error); 4466*eda14cbcSMatt Macy } 4467*eda14cbcSMatt Macy } 4468*eda14cbcSMatt Macy 4469*eda14cbcSMatt Macy /* 4470*eda14cbcSMatt Macy * Wait for the given activity and return the status of the wait (whether or not 4471*eda14cbcSMatt Macy * any waiting was done) in the 'waited' parameter. Non-existent pools are 4472*eda14cbcSMatt Macy * reported via the 'missing' parameter, rather than by printing an error 4473*eda14cbcSMatt Macy * message. This is convenient when this function is called in a loop over a 4474*eda14cbcSMatt Macy * long period of time (as it is, for example, by zpool's wait cmd). In that 4475*eda14cbcSMatt Macy * scenario, a pool being exported or destroyed should be considered a normal 4476*eda14cbcSMatt Macy * event, so we don't want to print an error when we find that the pool doesn't 4477*eda14cbcSMatt Macy * exist. 4478*eda14cbcSMatt Macy */ 4479*eda14cbcSMatt Macy int 4480*eda14cbcSMatt Macy zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity, 4481*eda14cbcSMatt Macy boolean_t *missing, boolean_t *waited) 4482*eda14cbcSMatt Macy { 4483*eda14cbcSMatt Macy int error = lzc_wait(zhp->zpool_name, activity, waited); 4484*eda14cbcSMatt Macy *missing = (error == ENOENT); 4485*eda14cbcSMatt Macy if (*missing) 4486*eda14cbcSMatt Macy return (0); 4487*eda14cbcSMatt Macy 4488*eda14cbcSMatt Macy if (error != 0) { 4489*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4490*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"), 4491*eda14cbcSMatt Macy zhp->zpool_name); 4492*eda14cbcSMatt Macy } 4493*eda14cbcSMatt Macy 4494*eda14cbcSMatt Macy return (error); 4495*eda14cbcSMatt Macy } 4496*eda14cbcSMatt Macy 4497*eda14cbcSMatt Macy int 4498*eda14cbcSMatt Macy zpool_set_bootenv(zpool_handle_t *zhp, const char *envmap) 4499*eda14cbcSMatt Macy { 4500*eda14cbcSMatt Macy int error = lzc_set_bootenv(zhp->zpool_name, envmap); 4501*eda14cbcSMatt Macy if (error != 0) { 4502*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4503*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4504*eda14cbcSMatt Macy "error setting bootenv in pool '%s'"), zhp->zpool_name); 4505*eda14cbcSMatt Macy } 4506*eda14cbcSMatt Macy 4507*eda14cbcSMatt Macy return (error); 4508*eda14cbcSMatt Macy } 4509*eda14cbcSMatt Macy 4510*eda14cbcSMatt Macy int 4511*eda14cbcSMatt Macy zpool_get_bootenv(zpool_handle_t *zhp, char *outbuf, size_t size, off_t offset) 4512*eda14cbcSMatt Macy { 4513*eda14cbcSMatt Macy nvlist_t *nvl = NULL; 4514*eda14cbcSMatt Macy int error = lzc_get_bootenv(zhp->zpool_name, &nvl); 4515*eda14cbcSMatt Macy if (error != 0) { 4516*eda14cbcSMatt Macy (void) zpool_standard_error_fmt(zhp->zpool_hdl, error, 4517*eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4518*eda14cbcSMatt Macy "error getting bootenv in pool '%s'"), zhp->zpool_name); 4519*eda14cbcSMatt Macy return (-1); 4520*eda14cbcSMatt Macy } 4521*eda14cbcSMatt Macy char *envmap = fnvlist_lookup_string(nvl, "envmap"); 4522*eda14cbcSMatt Macy if (offset >= strlen(envmap)) { 4523*eda14cbcSMatt Macy fnvlist_free(nvl); 4524*eda14cbcSMatt Macy return (0); 4525*eda14cbcSMatt Macy } 4526*eda14cbcSMatt Macy 4527*eda14cbcSMatt Macy strncpy(outbuf, envmap + offset, size); 4528*eda14cbcSMatt Macy int bytes = MIN(strlen(envmap + offset), size); 4529*eda14cbcSMatt Macy fnvlist_free(nvl); 4530*eda14cbcSMatt Macy return (bytes); 4531*eda14cbcSMatt Macy } 4532