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