1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5441d80aaSlling * Common Development and Distribution License (the "License"). 6441d80aaSlling * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 249a686fbcSPaul Dagnelie * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25810e43b2SBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved. 266401734dSWill Andrews * Copyright 2016 Nexenta Systems, Inc. 2788f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 28*1702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens #include <ctype.h> 32fa9e4066Sahrens #include <errno.h> 33fa9e4066Sahrens #include <devid.h> 34fa9e4066Sahrens #include <fcntl.h> 35fa9e4066Sahrens #include <libintl.h> 36fa9e4066Sahrens #include <stdio.h> 37fa9e4066Sahrens #include <stdlib.h> 38f3861e1aSahl #include <strings.h> 39fa9e4066Sahrens #include <unistd.h> 404445fffbSMatthew Ahrens #include <libgen.h> 418488aeb5Staylor #include <sys/efi_partition.h> 428488aeb5Staylor #include <sys/vtoc.h> 43fa9e4066Sahrens #include <sys/zfs_ioctl.h> 44573ca77eSGeorge Wilson #include <dlfcn.h> 45fa9e4066Sahrens 46fa9e4066Sahrens #include "zfs_namecheck.h" 47b1b8ab34Slling #include "zfs_prop.h" 48fa9e4066Sahrens #include "libzfs_impl.h" 49468c413aSTim Haley #include "zfs_comutil.h" 50ad135b5dSChristopher Siden #include "zfeature_common.h" 51fa9e4066Sahrens 527855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *); 53990b4856Slling 54573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 55573ca77eSGeorge Wilson 56f9af39baSGeorge Wilson typedef struct prop_flags { 57f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 58f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 59f9af39baSGeorge Wilson } prop_flags_t; 60f9af39baSGeorge Wilson 61990b4856Slling /* 62990b4856Slling * ==================================================================== 63990b4856Slling * zpool property functions 64990b4856Slling * ==================================================================== 65990b4856Slling */ 66990b4856Slling 67990b4856Slling static int 68990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 69990b4856Slling { 70990b4856Slling zfs_cmd_t zc = { 0 }; 71990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 72990b4856Slling 73990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 74990b4856Slling 75990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 76990b4856Slling return (-1); 77990b4856Slling 78990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 79990b4856Slling if (errno == ENOMEM) { 80990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 81990b4856Slling zcmd_free_nvlists(&zc); 82990b4856Slling return (-1); 83990b4856Slling } 84990b4856Slling } else { 85990b4856Slling zcmd_free_nvlists(&zc); 86990b4856Slling return (-1); 87990b4856Slling } 88990b4856Slling } 89990b4856Slling 90990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 91990b4856Slling zcmd_free_nvlists(&zc); 92990b4856Slling return (-1); 93990b4856Slling } 94990b4856Slling 95990b4856Slling zcmd_free_nvlists(&zc); 96990b4856Slling 97990b4856Slling return (0); 98990b4856Slling } 99990b4856Slling 100990b4856Slling static int 101990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 102990b4856Slling { 103990b4856Slling nvlist_t *old_props; 104990b4856Slling 105990b4856Slling old_props = zhp->zpool_props; 106990b4856Slling 107990b4856Slling if (zpool_get_all_props(zhp) != 0) 108990b4856Slling return (-1); 109990b4856Slling 110990b4856Slling nvlist_free(old_props); 111990b4856Slling return (0); 112990b4856Slling } 113990b4856Slling 114990b4856Slling static char * 115990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 116990b4856Slling zprop_source_t *src) 117990b4856Slling { 118990b4856Slling nvlist_t *nv, *nvl; 119990b4856Slling uint64_t ival; 120990b4856Slling char *value; 121990b4856Slling zprop_source_t source; 122990b4856Slling 123990b4856Slling nvl = zhp->zpool_props; 124990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 125990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 126990b4856Slling source = ival; 127990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 128990b4856Slling } else { 129990b4856Slling source = ZPROP_SRC_DEFAULT; 130990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 131990b4856Slling value = "-"; 132990b4856Slling } 133990b4856Slling 134990b4856Slling if (src) 135990b4856Slling *src = source; 136990b4856Slling 137990b4856Slling return (value); 138990b4856Slling } 139990b4856Slling 140990b4856Slling uint64_t 141990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 142990b4856Slling { 143990b4856Slling nvlist_t *nv, *nvl; 144990b4856Slling uint64_t value; 145990b4856Slling zprop_source_t source; 146990b4856Slling 147b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 148b87f3af3Sperrin /* 149b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 150b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 151b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 152b87f3af3Sperrin */ 153b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 154b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 155b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 156b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 157b87f3af3Sperrin == 0)) { 158b87f3af3Sperrin return (value); 159b87f3af3Sperrin } 160990b4856Slling return (zpool_prop_default_numeric(prop)); 161b87f3af3Sperrin } 162990b4856Slling 163990b4856Slling nvl = zhp->zpool_props; 164990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 165990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 166990b4856Slling source = value; 167990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 168990b4856Slling } else { 169990b4856Slling source = ZPROP_SRC_DEFAULT; 170990b4856Slling value = zpool_prop_default_numeric(prop); 171990b4856Slling } 172990b4856Slling 173990b4856Slling if (src) 174990b4856Slling *src = source; 175990b4856Slling 176990b4856Slling return (value); 177990b4856Slling } 178990b4856Slling 179990b4856Slling /* 180990b4856Slling * Map VDEV STATE to printed strings. 181990b4856Slling */ 1826401734dSWill Andrews const char * 183990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 184990b4856Slling { 185990b4856Slling switch (state) { 186990b4856Slling case VDEV_STATE_CLOSED: 187990b4856Slling case VDEV_STATE_OFFLINE: 188990b4856Slling return (gettext("OFFLINE")); 189990b4856Slling case VDEV_STATE_REMOVED: 190990b4856Slling return (gettext("REMOVED")); 191990b4856Slling case VDEV_STATE_CANT_OPEN: 192b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 193990b4856Slling return (gettext("FAULTED")); 1941195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1951195e687SMark J Musante return (gettext("SPLIT")); 196990b4856Slling else 197990b4856Slling return (gettext("UNAVAIL")); 198990b4856Slling case VDEV_STATE_FAULTED: 199990b4856Slling return (gettext("FAULTED")); 200990b4856Slling case VDEV_STATE_DEGRADED: 201990b4856Slling return (gettext("DEGRADED")); 202990b4856Slling case VDEV_STATE_HEALTHY: 203990b4856Slling return (gettext("ONLINE")); 20488f61deeSIgor Kozhukhov 20588f61deeSIgor Kozhukhov default: 20688f61deeSIgor Kozhukhov break; 207990b4856Slling } 208990b4856Slling 209990b4856Slling return (gettext("UNKNOWN")); 210990b4856Slling } 211990b4856Slling 2126401734dSWill Andrews /* 2136401734dSWill Andrews * Map POOL STATE to printed strings. 2146401734dSWill Andrews */ 2156401734dSWill Andrews const char * 2166401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state) 2176401734dSWill Andrews { 2186401734dSWill Andrews switch (state) { 2196401734dSWill Andrews case POOL_STATE_ACTIVE: 2206401734dSWill Andrews return (gettext("ACTIVE")); 2216401734dSWill Andrews case POOL_STATE_EXPORTED: 2226401734dSWill Andrews return (gettext("EXPORTED")); 2236401734dSWill Andrews case POOL_STATE_DESTROYED: 2246401734dSWill Andrews return (gettext("DESTROYED")); 2256401734dSWill Andrews case POOL_STATE_SPARE: 2266401734dSWill Andrews return (gettext("SPARE")); 2276401734dSWill Andrews case POOL_STATE_L2CACHE: 2286401734dSWill Andrews return (gettext("L2CACHE")); 2296401734dSWill Andrews case POOL_STATE_UNINITIALIZED: 2306401734dSWill Andrews return (gettext("UNINITIALIZED")); 2316401734dSWill Andrews case POOL_STATE_UNAVAIL: 2326401734dSWill Andrews return (gettext("UNAVAIL")); 2336401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE: 2346401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE")); 2356401734dSWill Andrews } 2366401734dSWill Andrews 2376401734dSWill Andrews return (gettext("UNKNOWN")); 2386401734dSWill Andrews } 2396401734dSWill Andrews 240990b4856Slling /* 241990b4856Slling * Get a zpool property value for 'prop' and return the value in 242990b4856Slling * a pre-allocated buffer. 243990b4856Slling */ 244990b4856Slling int 245990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 246c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 247990b4856Slling { 248990b4856Slling uint64_t intval; 249990b4856Slling const char *strval; 250990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 251990b4856Slling nvlist_t *nvroot; 252990b4856Slling vdev_stat_t *vs; 253990b4856Slling uint_t vsc; 254990b4856Slling 255990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 256379c004dSEric Schrock switch (prop) { 257379c004dSEric Schrock case ZPOOL_PROP_NAME: 258990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 259379c004dSEric Schrock break; 260379c004dSEric Schrock 261379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 262990b4856Slling (void) strlcpy(buf, "FAULTED", len); 263379c004dSEric Schrock break; 264379c004dSEric Schrock 265379c004dSEric Schrock case ZPOOL_PROP_GUID: 266379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 267379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 268379c004dSEric Schrock break; 269379c004dSEric Schrock 270379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 271379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2728704186eSDan McDonald case ZPOOL_PROP_COMMENT: 273379c004dSEric Schrock if (zhp->zpool_props != NULL || 274379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 275379c004dSEric Schrock (void) strlcpy(buf, 276379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 277379c004dSEric Schrock len); 278c58b3526SAdam Stevko break; 279379c004dSEric Schrock } 280379c004dSEric Schrock /* FALLTHROUGH */ 281379c004dSEric Schrock default: 282990b4856Slling (void) strlcpy(buf, "-", len); 283379c004dSEric Schrock break; 284379c004dSEric Schrock } 285379c004dSEric Schrock 286379c004dSEric Schrock if (srctype != NULL) 287379c004dSEric Schrock *srctype = src; 288990b4856Slling return (0); 289990b4856Slling } 290990b4856Slling 291990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 292990b4856Slling prop != ZPOOL_PROP_NAME) 293990b4856Slling return (-1); 294990b4856Slling 295990b4856Slling switch (zpool_prop_get_type(prop)) { 296990b4856Slling case PROP_TYPE_STRING: 297990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 298990b4856Slling len); 299990b4856Slling break; 300990b4856Slling 301990b4856Slling case PROP_TYPE_NUMBER: 302990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 303990b4856Slling 304990b4856Slling switch (prop) { 305990b4856Slling case ZPOOL_PROP_SIZE: 306485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 307485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 308ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 3097fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 310c58b3526SAdam Stevko if (literal) { 311c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 312c58b3526SAdam Stevko (u_longlong_t)intval); 313c58b3526SAdam Stevko } else { 314c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len); 315c58b3526SAdam Stevko } 316990b4856Slling break; 3177855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 3187a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 3197a09f97bSGeorge Wilson if (intval == 0) { 3207a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 3217a09f97bSGeorge Wilson } else if (literal) { 3227a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 3237a09f97bSGeorge Wilson (u_longlong_t)intval); 3247a09f97bSGeorge Wilson } else { 3257a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 3267a09f97bSGeorge Wilson } 3277a09f97bSGeorge Wilson break; 328990b4856Slling case ZPOOL_PROP_CAPACITY: 329c58b3526SAdam Stevko if (literal) { 330c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 331c58b3526SAdam Stevko (u_longlong_t)intval); 332c58b3526SAdam Stevko } else { 333c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%", 334c58b3526SAdam Stevko (u_longlong_t)intval); 335c58b3526SAdam Stevko } 336990b4856Slling break; 3372e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3382e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3392e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3402e4c9986SGeorge Wilson } else { 3412e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3422e4c9986SGeorge Wilson (u_longlong_t)intval); 3432e4c9986SGeorge Wilson } 3442e4c9986SGeorge Wilson break; 345b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 346b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 347b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 348b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 349b24ab676SJeff Bonwick break; 350990b4856Slling case ZPOOL_PROP_HEALTH: 351990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 352990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 353990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3543f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3553f9d6ad7SLin Ling == 0); 356990b4856Slling 357990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 358990b4856Slling vs->vs_aux), len); 359990b4856Slling break; 360ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 361ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 362ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 363ad135b5dSChristopher Siden break; 364ad135b5dSChristopher Siden } 365ad135b5dSChristopher Siden /* FALLTHROUGH */ 366990b4856Slling default: 367990b4856Slling (void) snprintf(buf, len, "%llu", intval); 368990b4856Slling } 369990b4856Slling break; 370990b4856Slling 371990b4856Slling case PROP_TYPE_INDEX: 372990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 373990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 374990b4856Slling != 0) 375990b4856Slling return (-1); 376990b4856Slling (void) strlcpy(buf, strval, len); 377990b4856Slling break; 378990b4856Slling 379990b4856Slling default: 380990b4856Slling abort(); 381990b4856Slling } 382990b4856Slling 383990b4856Slling if (srctype) 384990b4856Slling *srctype = src; 385990b4856Slling 386990b4856Slling return (0); 387990b4856Slling } 388990b4856Slling 389990b4856Slling /* 390990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 391990b4856Slling * Assuming bootfs is a valid dataset name. 392990b4856Slling */ 393990b4856Slling static boolean_t 394990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 395990b4856Slling { 396990b4856Slling int len = strlen(pool); 397990b4856Slling 398fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 399990b4856Slling return (B_FALSE); 400990b4856Slling 401990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 402990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 403990b4856Slling return (B_TRUE); 404990b4856Slling 405990b4856Slling return (B_FALSE); 406990b4856Slling } 407990b4856Slling 4084263d13fSGeorge Wilson boolean_t 4094263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 410b5b76fecSGeorge Wilson { 4119adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN]; 412b5b76fecSGeorge Wilson 413b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 414c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 415b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 416b5b76fecSGeorge Wilson } 417b5b76fecSGeorge Wilson 418b5b76fecSGeorge Wilson 419990b4856Slling /* 420990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 421990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 422990b4856Slling * specified as strings. 423990b4856Slling */ 424990b4856Slling static nvlist_t * 4250a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 426f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 427990b4856Slling { 428990b4856Slling nvpair_t *elem; 429990b4856Slling nvlist_t *retprops; 430990b4856Slling zpool_prop_t prop; 431990b4856Slling char *strval; 432990b4856Slling uint64_t intval; 4338704186eSDan McDonald char *slash, *check; 4342f8aaab3Seschrock struct stat64 statbuf; 43515e6edf1Sgw zpool_handle_t *zhp; 436990b4856Slling 437990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 438990b4856Slling (void) no_memory(hdl); 439990b4856Slling return (NULL); 440990b4856Slling } 441990b4856Slling 442990b4856Slling elem = NULL; 443990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 444990b4856Slling const char *propname = nvpair_name(elem); 445990b4856Slling 446ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 447ad135b5dSChristopher Siden if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) { 448ad135b5dSChristopher Siden int err; 449ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 450ad135b5dSChristopher Siden 4512acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 452ad135b5dSChristopher Siden if (err != 0) { 453ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 454ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 455ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 456ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 457ad135b5dSChristopher Siden goto error; 458ad135b5dSChristopher Siden } 459ad135b5dSChristopher Siden 460ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 461ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 462ad135b5dSChristopher Siden "'%s' must be a string"), propname); 463ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 464ad135b5dSChristopher Siden goto error; 465ad135b5dSChristopher Siden } 466ad135b5dSChristopher Siden 467ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 468ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 469ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 470ad135b5dSChristopher Siden "property '%s' can only be set to " 471ad135b5dSChristopher Siden "'enabled'"), propname); 472ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 473ad135b5dSChristopher Siden goto error; 474ad135b5dSChristopher Siden } 475ad135b5dSChristopher Siden 476ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 477ad135b5dSChristopher Siden (void) no_memory(hdl); 478ad135b5dSChristopher Siden goto error; 479ad135b5dSChristopher Siden } 480ad135b5dSChristopher Siden continue; 481ad135b5dSChristopher Siden } 482ad135b5dSChristopher Siden 483990b4856Slling /* 484990b4856Slling * Make sure this property is valid and applies to this type. 485990b4856Slling */ 486ad135b5dSChristopher Siden if (prop == ZPROP_INVAL) { 487990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 488990b4856Slling "invalid property '%s'"), propname); 489990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 490990b4856Slling goto error; 491990b4856Slling } 492990b4856Slling 493990b4856Slling if (zpool_prop_readonly(prop)) { 494990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 495990b4856Slling "is readonly"), propname); 496990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 497990b4856Slling goto error; 498990b4856Slling } 499990b4856Slling 500990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 501990b4856Slling &strval, &intval, errbuf) != 0) 502990b4856Slling goto error; 503990b4856Slling 504990b4856Slling /* 505990b4856Slling * Perform additional checking for specific properties. 506990b4856Slling */ 507990b4856Slling switch (prop) { 508990b4856Slling case ZPOOL_PROP_VERSION: 509ad135b5dSChristopher Siden if (intval < version || 510ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 511990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 512990b4856Slling "property '%s' number %d is invalid."), 513990b4856Slling propname, intval); 514990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 515990b4856Slling goto error; 516990b4856Slling } 517990b4856Slling break; 518990b4856Slling 5197855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 5207855d95bSToomas Soome if (!flags.create) { 5217855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5227855d95bSToomas Soome "property '%s' can only be set during pool " 5237855d95bSToomas Soome "creation"), propname); 5247855d95bSToomas Soome (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5257855d95bSToomas Soome goto error; 5267855d95bSToomas Soome } 5277855d95bSToomas Soome break; 5287855d95bSToomas Soome 529990b4856Slling case ZPOOL_PROP_BOOTFS: 530f9af39baSGeorge Wilson if (flags.create || flags.import) { 531990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 532990b4856Slling "property '%s' cannot be set at creation " 533990b4856Slling "or import time"), propname); 534990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 535990b4856Slling goto error; 536990b4856Slling } 537990b4856Slling 538990b4856Slling if (version < SPA_VERSION_BOOTFS) { 539990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 540990b4856Slling "pool must be upgraded to support " 541990b4856Slling "'%s' property"), propname); 542990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 543990b4856Slling goto error; 544990b4856Slling } 545990b4856Slling 546990b4856Slling /* 547990b4856Slling * bootfs property value has to be a dataset name and 548990b4856Slling * the dataset has to be in the same pool as it sets to. 549990b4856Slling */ 550990b4856Slling if (strval[0] != '\0' && !bootfs_name_valid(poolname, 551990b4856Slling strval)) { 552990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 553990b4856Slling "is an invalid name"), strval); 554990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 555990b4856Slling goto error; 556990b4856Slling } 55715e6edf1Sgw 55815e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 55915e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 56015e6edf1Sgw "could not open pool '%s'"), poolname); 56115e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 56215e6edf1Sgw goto error; 56315e6edf1Sgw } 56415e6edf1Sgw zpool_close(zhp); 565990b4856Slling break; 566990b4856Slling 5672f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 568f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 569990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 570990b4856Slling "property '%s' can only be set during pool " 571990b4856Slling "creation or import"), propname); 572990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 573990b4856Slling goto error; 574990b4856Slling } 575990b4856Slling 5762f8aaab3Seschrock if (strval[0] != '/') { 577990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5782f8aaab3Seschrock "bad alternate root '%s'"), strval); 5792f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 580990b4856Slling goto error; 581990b4856Slling } 5822f8aaab3Seschrock break; 5832f8aaab3Seschrock 5842f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5852f8aaab3Seschrock if (strval[0] == '\0') 5862f8aaab3Seschrock break; 5872f8aaab3Seschrock 5882f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5892f8aaab3Seschrock break; 590990b4856Slling 591990b4856Slling if (strval[0] != '/') { 592990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5932f8aaab3Seschrock "property '%s' must be empty, an " 5942f8aaab3Seschrock "absolute path, or 'none'"), propname); 595990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 596990b4856Slling goto error; 597990b4856Slling } 598990b4856Slling 5992f8aaab3Seschrock slash = strrchr(strval, '/'); 600990b4856Slling 6012f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6022f8aaab3Seschrock strcmp(slash, "/..") == 0) { 6032f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6042f8aaab3Seschrock "'%s' is not a valid file"), strval); 6052f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6062f8aaab3Seschrock goto error; 6072f8aaab3Seschrock } 608990b4856Slling 6092f8aaab3Seschrock *slash = '\0'; 6102f8aaab3Seschrock 6112c32020fSeschrock if (strval[0] != '\0' && 6122c32020fSeschrock (stat64(strval, &statbuf) != 0 || 6132c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 6142f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6152f8aaab3Seschrock "'%s' is not a valid directory"), 6162f8aaab3Seschrock strval); 6172f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6182f8aaab3Seschrock goto error; 6192f8aaab3Seschrock } 6202f8aaab3Seschrock 6212f8aaab3Seschrock *slash = '/'; 6222f8aaab3Seschrock break; 623f9af39baSGeorge Wilson 6248704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6258704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6268704186eSDan McDonald if (!isprint(*check)) { 6278704186eSDan McDonald zfs_error_aux(hdl, 6288704186eSDan McDonald dgettext(TEXT_DOMAIN, 6298704186eSDan McDonald "comment may only have printable " 6308704186eSDan McDonald "characters")); 6318704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 6328704186eSDan McDonald errbuf); 6338704186eSDan McDonald goto error; 6348704186eSDan McDonald } 6358704186eSDan McDonald } 6368704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 6378704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6388704186eSDan McDonald "comment must not exceed %d characters"), 6398704186eSDan McDonald ZPROP_MAX_COMMENT); 6408704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6418704186eSDan McDonald goto error; 6428704186eSDan McDonald } 6438704186eSDan McDonald break; 644f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 645f9af39baSGeorge Wilson if (!flags.import) { 646f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 647f9af39baSGeorge Wilson "property '%s' can only be set at " 648f9af39baSGeorge Wilson "import time"), propname); 649f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 650f9af39baSGeorge Wilson goto error; 651f9af39baSGeorge Wilson } 652f9af39baSGeorge Wilson break; 65388f61deeSIgor Kozhukhov 65488f61deeSIgor Kozhukhov default: 65588f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 65688f61deeSIgor Kozhukhov "property '%s'(%d) not defined"), propname, prop); 65788f61deeSIgor Kozhukhov break; 658990b4856Slling } 659990b4856Slling } 660990b4856Slling 661990b4856Slling return (retprops); 662990b4856Slling error: 663990b4856Slling nvlist_free(retprops); 664990b4856Slling return (NULL); 665990b4856Slling } 666990b4856Slling 667990b4856Slling /* 668990b4856Slling * Set zpool property : propname=propval. 669990b4856Slling */ 670990b4856Slling int 671990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 672990b4856Slling { 673990b4856Slling zfs_cmd_t zc = { 0 }; 674990b4856Slling int ret = -1; 675990b4856Slling char errbuf[1024]; 676990b4856Slling nvlist_t *nvl = NULL; 677990b4856Slling nvlist_t *realprops; 678990b4856Slling uint64_t version; 679f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 680990b4856Slling 681990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 682990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 683990b4856Slling zhp->zpool_name); 684990b4856Slling 685990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 686990b4856Slling return (no_memory(zhp->zpool_hdl)); 687990b4856Slling 688990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 689990b4856Slling nvlist_free(nvl); 690990b4856Slling return (no_memory(zhp->zpool_hdl)); 691990b4856Slling } 692990b4856Slling 693990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 6940a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 695f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 696990b4856Slling nvlist_free(nvl); 697990b4856Slling return (-1); 698990b4856Slling } 699990b4856Slling 700990b4856Slling nvlist_free(nvl); 701990b4856Slling nvl = realprops; 702990b4856Slling 703990b4856Slling /* 704990b4856Slling * Execute the corresponding ioctl() to set this property. 705990b4856Slling */ 706990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 707990b4856Slling 708990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 709990b4856Slling nvlist_free(nvl); 710990b4856Slling return (-1); 711990b4856Slling } 712990b4856Slling 713990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 714990b4856Slling 715990b4856Slling zcmd_free_nvlists(&zc); 716990b4856Slling nvlist_free(nvl); 717990b4856Slling 718990b4856Slling if (ret) 719990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 720990b4856Slling else 721990b4856Slling (void) zpool_props_refresh(zhp); 722990b4856Slling 723990b4856Slling return (ret); 724990b4856Slling } 725990b4856Slling 726990b4856Slling int 727990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 728990b4856Slling { 729990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 730990b4856Slling zprop_list_t *entry; 731990b4856Slling char buf[ZFS_MAXPROPLEN]; 732ad135b5dSChristopher Siden nvlist_t *features = NULL; 733ad135b5dSChristopher Siden zprop_list_t **last; 734ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 735990b4856Slling 736990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 737990b4856Slling return (-1); 738990b4856Slling 739ad135b5dSChristopher Siden last = plp; 740ad135b5dSChristopher Siden while (*last != NULL) 741ad135b5dSChristopher Siden last = &(*last)->pl_next; 742ad135b5dSChristopher Siden 743ad135b5dSChristopher Siden if ((*plp)->pl_all) 744ad135b5dSChristopher Siden features = zpool_get_features(zhp); 745ad135b5dSChristopher Siden 746ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 747ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 748ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 749ad135b5dSChristopher Siden sizeof (zprop_list_t)); 750ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 751ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 752ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 753ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 754ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 755ad135b5dSChristopher Siden 756ad135b5dSChristopher Siden *last = entry; 757ad135b5dSChristopher Siden last = &entry->pl_next; 758ad135b5dSChristopher Siden } 759ad135b5dSChristopher Siden } 760ad135b5dSChristopher Siden 761ad135b5dSChristopher Siden /* add any unsupported features */ 762ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 763ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 764ad135b5dSChristopher Siden char *propname; 765ad135b5dSChristopher Siden boolean_t found; 766ad135b5dSChristopher Siden zprop_list_t *entry; 767ad135b5dSChristopher Siden 768ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 769ad135b5dSChristopher Siden continue; 770ad135b5dSChristopher Siden 771ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 772ad135b5dSChristopher Siden nvpair_name(nvp)); 773ad135b5dSChristopher Siden 774ad135b5dSChristopher Siden /* 775ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 776ad135b5dSChristopher Siden * other pool already added the same property. 777ad135b5dSChristopher Siden */ 778ad135b5dSChristopher Siden found = B_FALSE; 779ad135b5dSChristopher Siden entry = *plp; 780ad135b5dSChristopher Siden while (entry != NULL) { 781ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 782ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 783ad135b5dSChristopher Siden found = B_TRUE; 784ad135b5dSChristopher Siden break; 785ad135b5dSChristopher Siden } 786ad135b5dSChristopher Siden entry = entry->pl_next; 787ad135b5dSChristopher Siden } 788ad135b5dSChristopher Siden if (found) { 789ad135b5dSChristopher Siden free(propname); 790ad135b5dSChristopher Siden continue; 791ad135b5dSChristopher Siden } 792ad135b5dSChristopher Siden 793ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 794ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 795ad135b5dSChristopher Siden entry->pl_user_prop = propname; 796ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 797ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 798ad135b5dSChristopher Siden 799ad135b5dSChristopher Siden *last = entry; 800ad135b5dSChristopher Siden last = &entry->pl_next; 801ad135b5dSChristopher Siden } 802ad135b5dSChristopher Siden 803990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 804990b4856Slling 805990b4856Slling if (entry->pl_fixed) 806990b4856Slling continue; 807990b4856Slling 808990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 809990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 810c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 811990b4856Slling if (strlen(buf) > entry->pl_width) 812990b4856Slling entry->pl_width = strlen(buf); 813990b4856Slling } 814990b4856Slling } 815990b4856Slling 816990b4856Slling return (0); 817990b4856Slling } 818990b4856Slling 819ad135b5dSChristopher Siden /* 820ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 821ad135b5dSChristopher Siden */ 822ad135b5dSChristopher Siden int 823ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 824ad135b5dSChristopher Siden size_t len) 825ad135b5dSChristopher Siden { 826ad135b5dSChristopher Siden uint64_t refcount; 827ad135b5dSChristopher Siden boolean_t found = B_FALSE; 828ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 829ad135b5dSChristopher Siden boolean_t supported; 830ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 831ad135b5dSChristopher Siden 832ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 8337c13517fSSerapheim Dimitropoulos ASSERT(supported || zpool_prop_unsupported(propname)); 834ad135b5dSChristopher Siden 835ad135b5dSChristopher Siden /* 836ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 837ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 838ad135b5dSChristopher Siden * use guids. 839ad135b5dSChristopher Siden */ 840ad135b5dSChristopher Siden if (supported) { 841ad135b5dSChristopher Siden int ret; 8422acef22dSMatthew Ahrens spa_feature_t fid; 843ad135b5dSChristopher Siden 8442acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 845ad135b5dSChristopher Siden if (ret != 0) { 846ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 847ad135b5dSChristopher Siden return (ENOTSUP); 848ad135b5dSChristopher Siden } 8492acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 850ad135b5dSChristopher Siden } 851ad135b5dSChristopher Siden 852ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 853ad135b5dSChristopher Siden found = B_TRUE; 854ad135b5dSChristopher Siden 855ad135b5dSChristopher Siden if (supported) { 856ad135b5dSChristopher Siden if (!found) { 857ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 858ad135b5dSChristopher Siden } else { 859ad135b5dSChristopher Siden if (refcount == 0) 860ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 861ad135b5dSChristopher Siden else 862ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 863ad135b5dSChristopher Siden } 864ad135b5dSChristopher Siden } else { 865ad135b5dSChristopher Siden if (found) { 866ad135b5dSChristopher Siden if (refcount == 0) { 867ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 868ad135b5dSChristopher Siden } else { 869ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 870ad135b5dSChristopher Siden } 871ad135b5dSChristopher Siden } else { 872ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 873ad135b5dSChristopher Siden return (ENOTSUP); 874ad135b5dSChristopher Siden } 875ad135b5dSChristopher Siden } 876ad135b5dSChristopher Siden 877ad135b5dSChristopher Siden return (0); 878ad135b5dSChristopher Siden } 879990b4856Slling 880573ca77eSGeorge Wilson /* 881573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 882573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 883573ca77eSGeorge Wilson */ 884573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 885573ca77eSGeorge Wilson 886fa9e4066Sahrens /* 887fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 888fa9e4066Sahrens * 'buf'. 889fa9e4066Sahrens */ 890e7cbe64fSgw boolean_t 89199653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 892fa9e4066Sahrens { 893fa9e4066Sahrens namecheck_err_t why; 894fa9e4066Sahrens char what; 895b468a217Seschrock int ret; 896b468a217Seschrock 897b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 898b468a217Seschrock 899b468a217Seschrock /* 900b468a217Seschrock * The rules for reserved pool names were extended at a later point. 901b468a217Seschrock * But we need to support users with existing pools that may now be 902b468a217Seschrock * invalid. So we only check for this expanded set of names during a 903b468a217Seschrock * create (or import), and only in userland. 904b468a217Seschrock */ 905b468a217Seschrock if (ret == 0 && !isopen && 906b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 907b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 9088654d025Sperrin strncmp(pool, "spare", 5) == 0 || 9098654d025Sperrin strcmp(pool, "log") == 0)) { 910e7cbe64fSgw if (hdl != NULL) 911e7cbe64fSgw zfs_error_aux(hdl, 912e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 91399653d4eSeschrock return (B_FALSE); 914b468a217Seschrock } 915b468a217Seschrock 916fa9e4066Sahrens 917b468a217Seschrock if (ret != 0) { 91899653d4eSeschrock if (hdl != NULL) { 919fa9e4066Sahrens switch (why) { 920b81d61a6Slling case NAME_ERR_TOOLONG: 92199653d4eSeschrock zfs_error_aux(hdl, 922b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 923b81d61a6Slling break; 924b81d61a6Slling 925fa9e4066Sahrens case NAME_ERR_INVALCHAR: 92699653d4eSeschrock zfs_error_aux(hdl, 927fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 928fa9e4066Sahrens "'%c' in pool name"), what); 929fa9e4066Sahrens break; 930fa9e4066Sahrens 931fa9e4066Sahrens case NAME_ERR_NOLETTER: 93299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93399653d4eSeschrock "name must begin with a letter")); 934fa9e4066Sahrens break; 935fa9e4066Sahrens 936fa9e4066Sahrens case NAME_ERR_RESERVED: 93799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93899653d4eSeschrock "name is reserved")); 939fa9e4066Sahrens break; 940fa9e4066Sahrens 941fa9e4066Sahrens case NAME_ERR_DISKLIKE: 94299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 94399653d4eSeschrock "pool name is reserved")); 944fa9e4066Sahrens break; 9455ad82045Snd 9465ad82045Snd case NAME_ERR_LEADING_SLASH: 9475ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9485ad82045Snd "leading slash in name")); 9495ad82045Snd break; 9505ad82045Snd 9515ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 9525ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9535ad82045Snd "empty component in name")); 9545ad82045Snd break; 9555ad82045Snd 9565ad82045Snd case NAME_ERR_TRAILING_SLASH: 9575ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9585ad82045Snd "trailing slash in name")); 9595ad82045Snd break; 9605ad82045Snd 961edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 9625ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 963edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 964edb901aaSMarcel Telka "name")); 9655ad82045Snd break; 9665ad82045Snd 96788f61deeSIgor Kozhukhov default: 96888f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96988f61deeSIgor Kozhukhov "(%d) not defined"), why); 97088f61deeSIgor Kozhukhov break; 971fa9e4066Sahrens } 972fa9e4066Sahrens } 97399653d4eSeschrock return (B_FALSE); 974fa9e4066Sahrens } 975fa9e4066Sahrens 97699653d4eSeschrock return (B_TRUE); 977fa9e4066Sahrens } 978fa9e4066Sahrens 979fa9e4066Sahrens /* 980fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 981fa9e4066Sahrens * state. 982fa9e4066Sahrens */ 983fa9e4066Sahrens zpool_handle_t * 98499653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 985fa9e4066Sahrens { 986fa9e4066Sahrens zpool_handle_t *zhp; 98794de1d4cSeschrock boolean_t missing; 988fa9e4066Sahrens 989fa9e4066Sahrens /* 990fa9e4066Sahrens * Make sure the pool name is valid. 991fa9e4066Sahrens */ 99299653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 993ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 99499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 99599653d4eSeschrock pool); 996fa9e4066Sahrens return (NULL); 997fa9e4066Sahrens } 998fa9e4066Sahrens 99999653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 100099653d4eSeschrock return (NULL); 1001fa9e4066Sahrens 100299653d4eSeschrock zhp->zpool_hdl = hdl; 1003fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1004fa9e4066Sahrens 100594de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 100694de1d4cSeschrock zpool_close(zhp); 100794de1d4cSeschrock return (NULL); 100894de1d4cSeschrock } 100994de1d4cSeschrock 101094de1d4cSeschrock if (missing) { 1011990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 1012ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 1013990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 101494de1d4cSeschrock zpool_close(zhp); 101594de1d4cSeschrock return (NULL); 1016fa9e4066Sahrens } 1017fa9e4066Sahrens 1018fa9e4066Sahrens return (zhp); 1019fa9e4066Sahrens } 1020fa9e4066Sahrens 1021fa9e4066Sahrens /* 1022fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 1023fa9e4066Sahrens * the configuration cache may be out of date). 1024fa9e4066Sahrens */ 102594de1d4cSeschrock int 102694de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1027fa9e4066Sahrens { 1028fa9e4066Sahrens zpool_handle_t *zhp; 102994de1d4cSeschrock boolean_t missing; 1030fa9e4066Sahrens 103194de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 103294de1d4cSeschrock return (-1); 1033fa9e4066Sahrens 103499653d4eSeschrock zhp->zpool_hdl = hdl; 1035fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1036fa9e4066Sahrens 103794de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 103894de1d4cSeschrock zpool_close(zhp); 103994de1d4cSeschrock return (-1); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 104294de1d4cSeschrock if (missing) { 104394de1d4cSeschrock zpool_close(zhp); 104494de1d4cSeschrock *ret = NULL; 104594de1d4cSeschrock return (0); 104694de1d4cSeschrock } 104794de1d4cSeschrock 104894de1d4cSeschrock *ret = zhp; 104994de1d4cSeschrock return (0); 1050fa9e4066Sahrens } 1051fa9e4066Sahrens 1052fa9e4066Sahrens /* 1053fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1054fa9e4066Sahrens * state. 1055fa9e4066Sahrens */ 1056fa9e4066Sahrens zpool_handle_t * 105799653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1058fa9e4066Sahrens { 1059fa9e4066Sahrens zpool_handle_t *zhp; 1060fa9e4066Sahrens 106199653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1062fa9e4066Sahrens return (NULL); 1063fa9e4066Sahrens 1064fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1065ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 106699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1067fa9e4066Sahrens zpool_close(zhp); 1068fa9e4066Sahrens return (NULL); 1069fa9e4066Sahrens } 1070fa9e4066Sahrens 1071fa9e4066Sahrens return (zhp); 1072fa9e4066Sahrens } 1073fa9e4066Sahrens 1074fa9e4066Sahrens /* 1075fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1076fa9e4066Sahrens */ 1077fa9e4066Sahrens void 1078fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1079fa9e4066Sahrens { 1080aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config); 1081aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config); 1082aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props); 1083fa9e4066Sahrens free(zhp); 1084fa9e4066Sahrens } 1085fa9e4066Sahrens 1086fa9e4066Sahrens /* 1087fa9e4066Sahrens * Return the name of the pool. 1088fa9e4066Sahrens */ 1089fa9e4066Sahrens const char * 1090fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1091fa9e4066Sahrens { 1092fa9e4066Sahrens return (zhp->zpool_name); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens 1095fa9e4066Sahrens 1096fa9e4066Sahrens /* 1097fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1098fa9e4066Sahrens */ 1099fa9e4066Sahrens int 1100fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1101fa9e4066Sahrens { 1102fa9e4066Sahrens return (zhp->zpool_state); 1103fa9e4066Sahrens } 1104fa9e4066Sahrens 1105fa9e4066Sahrens /* 1106fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1107fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1108fa9e4066Sahrens * don't have to worry about error semantics. 1109fa9e4066Sahrens */ 1110fa9e4066Sahrens int 111199653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 11120a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1113fa9e4066Sahrens { 1114fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 11150a48a24eStimh nvlist_t *zc_fsprops = NULL; 11160a48a24eStimh nvlist_t *zc_props = NULL; 111799653d4eSeschrock char msg[1024]; 11180a48a24eStimh int ret = -1; 1119fa9e4066Sahrens 112099653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 112199653d4eSeschrock "cannot create '%s'"), pool); 1122fa9e4066Sahrens 112399653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 112499653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1125fa9e4066Sahrens 1126351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1127990b4856Slling return (-1); 1128fa9e4066Sahrens 11290a48a24eStimh if (props) { 1130f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1131f9af39baSGeorge Wilson 11320a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1133f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 11340a48a24eStimh goto create_failed; 11350a48a24eStimh } 11360a48a24eStimh } 113799653d4eSeschrock 11380a48a24eStimh if (fsprops) { 11390a48a24eStimh uint64_t zoned; 11400a48a24eStimh char *zonestr; 11410a48a24eStimh 11420a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 11430a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 11440a48a24eStimh strcmp(zonestr, "on") == 0); 11450a48a24eStimh 1146e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1147e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 11480a48a24eStimh goto create_failed; 11490a48a24eStimh } 11500a48a24eStimh if (!zc_props && 11510a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 11520a48a24eStimh goto create_failed; 11530a48a24eStimh } 11540a48a24eStimh if (nvlist_add_nvlist(zc_props, 11550a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 11560a48a24eStimh goto create_failed; 11570a48a24eStimh } 1158351420b3Slling } 1159fa9e4066Sahrens 11600a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 11610a48a24eStimh goto create_failed; 11620a48a24eStimh 1163990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1164fa9e4066Sahrens 11650a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1166351420b3Slling 1167e9dbad6fSeschrock zcmd_free_nvlists(&zc); 11680a48a24eStimh nvlist_free(zc_props); 11690a48a24eStimh nvlist_free(zc_fsprops); 1170fa9e4066Sahrens 117199653d4eSeschrock switch (errno) { 1172fa9e4066Sahrens case EBUSY: 1173fa9e4066Sahrens /* 1174fa9e4066Sahrens * This can happen if the user has specified the same 1175fa9e4066Sahrens * device multiple times. We can't reliably detect this 1176fa9e4066Sahrens * until we try to add it and see we already have a 1177fa9e4066Sahrens * label. 1178fa9e4066Sahrens */ 117999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 118099653d4eSeschrock "one or more vdevs refer to the same device")); 118199653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1182fa9e4066Sahrens 1183e9316f76SJoe Stein case ERANGE: 1184e9316f76SJoe Stein /* 1185e9316f76SJoe Stein * This happens if the record size is smaller or larger 1186e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1187e9316f76SJoe Stein * 1188e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1189e9316f76SJoe Stein * this case may have slipped through since the 1190e9316f76SJoe Stein * pool does not exist yet and it is therefore 1191e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1192e9316f76SJoe Stein * from the pool. 1193e9316f76SJoe Stein */ 1194e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1195e9316f76SJoe Stein "record size invalid")); 1196e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1197e9316f76SJoe Stein 1198fa9e4066Sahrens case EOVERFLOW: 1199fa9e4066Sahrens /* 120099653d4eSeschrock * This occurs when one of the devices is below 1201fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1202fa9e4066Sahrens * device was the problem device since there's no 1203fa9e4066Sahrens * reliable way to determine device size from userland. 1204fa9e4066Sahrens */ 1205fa9e4066Sahrens { 1206fa9e4066Sahrens char buf[64]; 1207fa9e4066Sahrens 1208fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1209fa9e4066Sahrens 121099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121199653d4eSeschrock "one or more devices is less than the " 121299653d4eSeschrock "minimum size (%s)"), buf); 1213fa9e4066Sahrens } 121499653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1215fa9e4066Sahrens 1216fa9e4066Sahrens case ENOSPC: 121799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121899653d4eSeschrock "one or more devices is out of space")); 121999653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1220fa9e4066Sahrens 1221fa94a07fSbrendan case ENOTBLK: 1222fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1223fa94a07fSbrendan "cache device must be a disk or disk slice")); 1224fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1225fa94a07fSbrendan 1226fa9e4066Sahrens default: 122799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1228fa9e4066Sahrens } 1229fa9e4066Sahrens } 1230fa9e4066Sahrens 12310a48a24eStimh create_failed: 1232351420b3Slling zcmd_free_nvlists(&zc); 12330a48a24eStimh nvlist_free(zc_props); 12340a48a24eStimh nvlist_free(zc_fsprops); 12350a48a24eStimh return (ret); 1236fa9e4066Sahrens } 1237fa9e4066Sahrens 1238fa9e4066Sahrens /* 1239fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1240fa9e4066Sahrens * datasets left in the pool. 1241fa9e4066Sahrens */ 1242fa9e4066Sahrens int 12434445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1244fa9e4066Sahrens { 1245fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1246fa9e4066Sahrens zfs_handle_t *zfp = NULL; 124799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 124899653d4eSeschrock char msg[1024]; 1249fa9e4066Sahrens 1250fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1251cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1252fa9e4066Sahrens return (-1); 1253fa9e4066Sahrens 1254fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 12554445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1256fa9e4066Sahrens 1257cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 125899653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 125999653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1260fa9e4066Sahrens 126199653d4eSeschrock if (errno == EROFS) { 126299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 126399653d4eSeschrock "one or more devices is read only")); 126499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 126599653d4eSeschrock } else { 126699653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1267fa9e4066Sahrens } 1268fa9e4066Sahrens 1269fa9e4066Sahrens if (zfp) 1270fa9e4066Sahrens zfs_close(zfp); 1271fa9e4066Sahrens return (-1); 1272fa9e4066Sahrens } 1273fa9e4066Sahrens 1274fa9e4066Sahrens if (zfp) { 1275fa9e4066Sahrens remove_mountpoint(zfp); 1276fa9e4066Sahrens zfs_close(zfp); 1277fa9e4066Sahrens } 1278fa9e4066Sahrens 1279fa9e4066Sahrens return (0); 1280fa9e4066Sahrens } 1281fa9e4066Sahrens 1282fa9e4066Sahrens /* 1283fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1284fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1285fa9e4066Sahrens */ 1286fa9e4066Sahrens int 1287fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1288fa9e4066Sahrens { 1289e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 129099653d4eSeschrock int ret; 129199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 129299653d4eSeschrock char msg[1024]; 1293fa94a07fSbrendan nvlist_t **spares, **l2cache; 1294fa94a07fSbrendan uint_t nspares, nl2cache; 129599653d4eSeschrock 129699653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 129799653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 129899653d4eSeschrock 1299fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1300fa94a07fSbrendan SPA_VERSION_SPARES && 130199653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 130299653d4eSeschrock &spares, &nspares) == 0) { 130399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 130499653d4eSeschrock "upgraded to add hot spares")); 130599653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 130699653d4eSeschrock } 1307fa9e4066Sahrens 1308fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1309fa94a07fSbrendan SPA_VERSION_L2CACHE && 1310fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1311fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1312fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1313fa94a07fSbrendan "upgraded to add cache devices")); 1314fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1315fa94a07fSbrendan } 1316fa94a07fSbrendan 1317990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 131899653d4eSeschrock return (-1); 1319fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1320fa9e4066Sahrens 1321cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1322fa9e4066Sahrens switch (errno) { 1323fa9e4066Sahrens case EBUSY: 1324fa9e4066Sahrens /* 1325fa9e4066Sahrens * This can happen if the user has specified the same 1326fa9e4066Sahrens * device multiple times. We can't reliably detect this 1327fa9e4066Sahrens * until we try to add it and see we already have a 1328fa9e4066Sahrens * label. 1329fa9e4066Sahrens */ 133099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 133199653d4eSeschrock "one or more vdevs refer to the same device")); 133299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1333fa9e4066Sahrens break; 1334fa9e4066Sahrens 1335fa9e4066Sahrens case EOVERFLOW: 1336fa9e4066Sahrens /* 1337fa9e4066Sahrens * This occurrs when one of the devices is below 1338fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1339fa9e4066Sahrens * device was the problem device since there's no 1340fa9e4066Sahrens * reliable way to determine device size from userland. 1341fa9e4066Sahrens */ 1342fa9e4066Sahrens { 1343fa9e4066Sahrens char buf[64]; 1344fa9e4066Sahrens 1345fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1346fa9e4066Sahrens 134799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 134899653d4eSeschrock "device is less than the minimum " 134999653d4eSeschrock "size (%s)"), buf); 1350fa9e4066Sahrens } 135199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 135299653d4eSeschrock break; 135399653d4eSeschrock 135499653d4eSeschrock case ENOTSUP: 135599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13568654d025Sperrin "pool must be upgraded to add these vdevs")); 135799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1358fa9e4066Sahrens break; 1359fa9e4066Sahrens 1360b1b8ab34Slling case EDOM: 1361b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13628654d025Sperrin "root pool can not have multiple vdevs" 13638654d025Sperrin " or separate logs")); 1364b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1365b1b8ab34Slling break; 1366b1b8ab34Slling 1367fa94a07fSbrendan case ENOTBLK: 1368fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1369fa94a07fSbrendan "cache device must be a disk or disk slice")); 1370fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1371fa94a07fSbrendan break; 1372fa94a07fSbrendan 1373fa9e4066Sahrens default: 137499653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1375fa9e4066Sahrens } 1376fa9e4066Sahrens 137799653d4eSeschrock ret = -1; 137899653d4eSeschrock } else { 137999653d4eSeschrock ret = 0; 1380fa9e4066Sahrens } 1381fa9e4066Sahrens 1382e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1383fa9e4066Sahrens 138499653d4eSeschrock return (ret); 1385fa9e4066Sahrens } 1386fa9e4066Sahrens 1387fa9e4066Sahrens /* 1388fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1389fa9e4066Sahrens * mounted datasets in the pool. 1390fa9e4066Sahrens */ 13914445fffbSMatthew Ahrens static int 13924445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 13934445fffbSMatthew Ahrens const char *log_str) 1394fa9e4066Sahrens { 1395fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 139689a89ebfSlling char msg[1024]; 1397fa9e4066Sahrens 139889a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 139989a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 140089a89ebfSlling 1401fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 140289a89ebfSlling zc.zc_cookie = force; 1403394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 14044445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 140589a89ebfSlling 140689a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 140789a89ebfSlling switch (errno) { 140889a89ebfSlling case EXDEV: 140989a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 141089a89ebfSlling "use '-f' to override the following errors:\n" 141189a89ebfSlling "'%s' has an active shared spare which could be" 141289a89ebfSlling " used by other pools once '%s' is exported."), 141389a89ebfSlling zhp->zpool_name, zhp->zpool_name); 141489a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 141589a89ebfSlling msg)); 141689a89ebfSlling default: 141789a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 141889a89ebfSlling msg)); 141989a89ebfSlling } 142089a89ebfSlling } 1421fa9e4066Sahrens 1422fa9e4066Sahrens return (0); 1423fa9e4066Sahrens } 1424fa9e4066Sahrens 1425394ab0cbSGeorge Wilson int 14264445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1427394ab0cbSGeorge Wilson { 14284445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1429394ab0cbSGeorge Wilson } 1430394ab0cbSGeorge Wilson 1431394ab0cbSGeorge Wilson int 14324445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1433394ab0cbSGeorge Wilson { 14344445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1435394ab0cbSGeorge Wilson } 1436394ab0cbSGeorge Wilson 1437468c413aSTim Haley static void 1438468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 14394b964adaSGeorge Wilson nvlist_t *config) 1440468c413aSTim Haley { 14414b964adaSGeorge Wilson nvlist_t *nv = NULL; 1442468c413aSTim Haley uint64_t rewindto; 1443468c413aSTim Haley int64_t loss = -1; 1444468c413aSTim Haley struct tm t; 1445468c413aSTim Haley char timestr[128]; 1446468c413aSTim Haley 14474b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 14484b964adaSGeorge Wilson return; 14494b964adaSGeorge Wilson 1450ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1451ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1452468c413aSTim Haley return; 1453ad135b5dSChristopher Siden } 1454468c413aSTim Haley 14554b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1456468c413aSTim Haley return; 14574b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1458468c413aSTim Haley 1459468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1460468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1461468c413aSTim Haley if (dryrun) { 1462468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1463468c413aSTim Haley "Would be able to return %s " 1464468c413aSTim Haley "to its state as of %s.\n"), 1465468c413aSTim Haley name, timestr); 1466468c413aSTim Haley } else { 1467468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1468468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1469468c413aSTim Haley name, timestr); 1470468c413aSTim Haley } 1471468c413aSTim Haley if (loss > 120) { 1472468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1473468c413aSTim Haley "%s approximately %lld "), 1474468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1475468c413aSTim Haley (loss + 30) / 60); 1476468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1477468c413aSTim Haley "minutes of transactions.\n")); 1478468c413aSTim Haley } else if (loss > 0) { 1479468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1480468c413aSTim Haley "%s approximately %lld "), 1481468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1482468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1483468c413aSTim Haley "seconds of transactions.\n")); 1484468c413aSTim Haley } 1485468c413aSTim Haley } 1486468c413aSTim Haley } 1487468c413aSTim Haley 1488468c413aSTim Haley void 1489468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1490468c413aSTim Haley nvlist_t *config) 1491468c413aSTim Haley { 14924b964adaSGeorge Wilson nvlist_t *nv = NULL; 1493468c413aSTim Haley int64_t loss = -1; 1494468c413aSTim Haley uint64_t edata = UINT64_MAX; 1495468c413aSTim Haley uint64_t rewindto; 1496468c413aSTim Haley struct tm t; 1497468c413aSTim Haley char timestr[128]; 1498468c413aSTim Haley 1499468c413aSTim Haley if (!hdl->libzfs_printerr) 1500468c413aSTim Haley return; 1501468c413aSTim Haley 1502468c413aSTim Haley if (reason >= 0) 1503468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1504468c413aSTim Haley else 1505468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1506468c413aSTim Haley 1507468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 15084b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1509ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 15104b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1511468c413aSTim Haley goto no_info; 1512468c413aSTim Haley 15134b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 15144b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1515468c413aSTim Haley &edata); 1516468c413aSTim Haley 1517468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1518468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1519468c413aSTim Haley 1520468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1521468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1522468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1523468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1524468c413aSTim Haley "\tshould correct the problem. "), 1525468c413aSTim Haley timestr); 1526468c413aSTim Haley } else { 1527468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1528468c413aSTim Haley "\tReverting the pool to an earlier state " 1529468c413aSTim Haley "should correct the problem.\n\t")); 1530468c413aSTim Haley } 1531468c413aSTim Haley 1532468c413aSTim Haley if (loss > 120) { 1533468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1534468c413aSTim Haley "Approximately %lld minutes of data\n" 1535468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1536468c413aSTim Haley } else if (loss > 0) { 1537468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1538468c413aSTim Haley "Approximately %lld seconds of data\n" 1539468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1540468c413aSTim Haley } 1541468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1542468c413aSTim Haley if (edata == 1) { 1543468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1544468c413aSTim Haley "After rewind, at least\n" 1545468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1546468c413aSTim Haley } else { 1547468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1548468c413aSTim Haley "After rewind, several\n" 1549468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1550468c413aSTim Haley } 1551468c413aSTim Haley } 1552468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1553a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1554a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1555468c413aSTim Haley 1556468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1557468c413aSTim Haley "A scrub of the pool\n" 1558468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1559468c413aSTim Haley return; 1560468c413aSTim Haley 1561468c413aSTim Haley no_info: 1562468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1563468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1564468c413aSTim Haley } 1565468c413aSTim Haley 1566fa9e4066Sahrens /* 1567990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1568990b4856Slling * if possible. 1569990b4856Slling * 1570990b4856Slling * Applications should use zpool_import_props() to import a pool with 1571990b4856Slling * new properties value to be set. 1572fa9e4066Sahrens */ 1573fa9e4066Sahrens int 157499653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1575990b4856Slling char *altroot) 1576990b4856Slling { 1577990b4856Slling nvlist_t *props = NULL; 1578990b4856Slling int ret; 1579990b4856Slling 1580990b4856Slling if (altroot != NULL) { 1581990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1582990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1583990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1584990b4856Slling newname)); 1585990b4856Slling } 1586990b4856Slling 1587990b4856Slling if (nvlist_add_string(props, 1588352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1589352d8027SGeorge Wilson nvlist_add_string(props, 1590352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1591990b4856Slling nvlist_free(props); 1592990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1593990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1594990b4856Slling newname)); 1595990b4856Slling } 1596990b4856Slling } 1597990b4856Slling 15984b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 15994b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1600aab83bb8SJosef 'Jeff' Sipek nvlist_free(props); 1601990b4856Slling return (ret); 1602990b4856Slling } 1603990b4856Slling 16044b964adaSGeorge Wilson static void 16054b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 16064b964adaSGeorge Wilson int indent) 16074b964adaSGeorge Wilson { 16084b964adaSGeorge Wilson nvlist_t **child; 16094b964adaSGeorge Wilson uint_t c, children; 16104b964adaSGeorge Wilson char *vname; 16114b964adaSGeorge Wilson uint64_t is_log = 0; 16124b964adaSGeorge Wilson 16134b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 16144b964adaSGeorge Wilson &is_log); 16154b964adaSGeorge Wilson 16164b964adaSGeorge Wilson if (name != NULL) 16174b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 16184b964adaSGeorge Wilson is_log ? " [log]" : ""); 16194b964adaSGeorge Wilson 16204b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 16214b964adaSGeorge Wilson &child, &children) != 0) 16224b964adaSGeorge Wilson return; 16234b964adaSGeorge Wilson 16244b964adaSGeorge Wilson for (c = 0; c < children; c++) { 16254b964adaSGeorge Wilson vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE); 16264b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 16274b964adaSGeorge Wilson free(vname); 16284b964adaSGeorge Wilson } 16294b964adaSGeorge Wilson } 16304b964adaSGeorge Wilson 1631ad135b5dSChristopher Siden void 1632ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1633ad135b5dSChristopher Siden { 1634ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1635ad135b5dSChristopher Siden 1636ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1637ad135b5dSChristopher Siden 0); 1638ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1639ad135b5dSChristopher Siden &unsup_feat) == 0); 1640ad135b5dSChristopher Siden 1641ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1642ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1643ad135b5dSChristopher Siden char *desc; 1644ad135b5dSChristopher Siden 1645ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1646ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1647ad135b5dSChristopher Siden 1648ad135b5dSChristopher Siden if (strlen(desc) > 0) 1649ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1650ad135b5dSChristopher Siden else 1651ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1652ad135b5dSChristopher Siden } 1653ad135b5dSChristopher Siden } 1654ad135b5dSChristopher Siden 1655990b4856Slling /* 1656990b4856Slling * Import the given pool using the known configuration and a list of 1657990b4856Slling * properties to be set. The configuration should have come from 1658990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1659990b4856Slling * is imported with a different name. 1660990b4856Slling */ 1661990b4856Slling int 1662990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 16634b964adaSGeorge Wilson nvlist_t *props, int flags) 1664fa9e4066Sahrens { 1665e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1666468c413aSTim Haley zpool_rewind_policy_t policy; 16674b964adaSGeorge Wilson nvlist_t *nv = NULL; 16684b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 16694b964adaSGeorge Wilson nvlist_t *missing = NULL; 1670fa9e4066Sahrens char *thename; 1671fa9e4066Sahrens char *origname; 1672fa9e4066Sahrens int ret; 16734b964adaSGeorge Wilson int error = 0; 1674990b4856Slling char errbuf[1024]; 1675fa9e4066Sahrens 1676fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1677fa9e4066Sahrens &origname) == 0); 1678fa9e4066Sahrens 1679990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1680990b4856Slling "cannot import pool '%s'"), origname); 1681990b4856Slling 1682fa9e4066Sahrens if (newname != NULL) { 168399653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1684ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 168599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 168699653d4eSeschrock newname)); 1687fa9e4066Sahrens thename = (char *)newname; 1688fa9e4066Sahrens } else { 1689fa9e4066Sahrens thename = origname; 1690fa9e4066Sahrens } 1691fa9e4066Sahrens 1692078266a5SMarcel Telka if (props != NULL) { 1693990b4856Slling uint64_t version; 1694f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1695fa9e4066Sahrens 1696990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1697990b4856Slling &version) == 0); 1698fa9e4066Sahrens 16990a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1700078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1701990b4856Slling return (-1); 1702078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1703351420b3Slling nvlist_free(props); 1704990b4856Slling return (-1); 1705351420b3Slling } 1706078266a5SMarcel Telka nvlist_free(props); 1707990b4856Slling } 1708990b4856Slling 1709990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1710fa9e4066Sahrens 1711fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1712ea8dc4b6Seschrock &zc.zc_guid) == 0); 1713fa9e4066Sahrens 1714351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1715078266a5SMarcel Telka zcmd_free_nvlists(&zc); 171699653d4eSeschrock return (-1); 1717351420b3Slling } 171857f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1719078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1720468c413aSTim Haley return (-1); 1721468c413aSTim Haley } 1722fa9e4066Sahrens 17234b964adaSGeorge Wilson zc.zc_cookie = flags; 17244b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 17254b964adaSGeorge Wilson errno == ENOMEM) { 17264b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 17274b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 17284b964adaSGeorge Wilson return (-1); 17294b964adaSGeorge Wilson } 17304b964adaSGeorge Wilson } 17314b964adaSGeorge Wilson if (ret != 0) 17324b964adaSGeorge Wilson error = errno; 17334b964adaSGeorge Wilson 17344b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1735078266a5SMarcel Telka 1736078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1737078266a5SMarcel Telka 17384b964adaSGeorge Wilson zpool_get_rewind_policy(config, &policy); 17394b964adaSGeorge Wilson 17404b964adaSGeorge Wilson if (error) { 1741fa9e4066Sahrens char desc[1024]; 1742468c413aSTim Haley 1743468c413aSTim Haley /* 1744468c413aSTim Haley * Dry-run failed, but we print out what success 1745468c413aSTim Haley * looks like if we found a best txg 1746468c413aSTim Haley */ 17474b964adaSGeorge Wilson if (policy.zrp_request & ZPOOL_TRY_REWIND) { 1748468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 17494b964adaSGeorge Wilson B_TRUE, nv); 17504b964adaSGeorge Wilson nvlist_free(nv); 1751468c413aSTim Haley return (-1); 1752468c413aSTim Haley } 1753468c413aSTim Haley 1754fa9e4066Sahrens if (newname == NULL) 1755fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1756fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1757fa9e4066Sahrens thename); 1758fa9e4066Sahrens else 1759fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1760fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1761fa9e4066Sahrens origname, thename); 1762fa9e4066Sahrens 17634b964adaSGeorge Wilson switch (error) { 1764ea8dc4b6Seschrock case ENOTSUP: 1765ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1766ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1767ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1768ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1769ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1770ad135b5dSChristopher Siden "supported by this system:\n")); 1771ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1772ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1773ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1774ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1775ad135b5dSChristopher Siden "All unsupported features are only " 1776ad135b5dSChristopher Siden "required for writing to the pool." 1777ad135b5dSChristopher Siden "\nThe pool can be imported using " 1778ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1779ad135b5dSChristopher Siden } 1780ad135b5dSChristopher Siden } 1781ea8dc4b6Seschrock /* 1782ea8dc4b6Seschrock * Unsupported version. 1783ea8dc4b6Seschrock */ 178499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1785ea8dc4b6Seschrock break; 1786ea8dc4b6Seschrock 1787b5989ec7Seschrock case EINVAL: 1788b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1789b5989ec7Seschrock break; 1790b5989ec7Seschrock 179154a91118SChris Kirby case EROFS: 179254a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 179354a91118SChris Kirby "one or more devices is read only")); 179454a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 179554a91118SChris Kirby break; 179654a91118SChris Kirby 17974b964adaSGeorge Wilson case ENXIO: 17984b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 17994b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 18004b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 18014b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 18024b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 18034b964adaSGeorge Wilson "The devices below are missing, use " 18044b964adaSGeorge Wilson "'-m' to import the pool anyway:\n")); 18054b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 18064b964adaSGeorge Wilson (void) printf("\n"); 18074b964adaSGeorge Wilson } 18084b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18094b964adaSGeorge Wilson break; 18104b964adaSGeorge Wilson 18114b964adaSGeorge Wilson case EEXIST: 18124b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 18134b964adaSGeorge Wilson break; 1814c971037bSPaul Dagnelie case ENAMETOOLONG: 1815c971037bSPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1816c971037bSPaul Dagnelie "new name of at least one dataset is longer than " 1817c971037bSPaul Dagnelie "the maximum allowable length")); 1818c971037bSPaul Dagnelie (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 1819c971037bSPaul Dagnelie break; 1820fa9e4066Sahrens default: 18214b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1822468c413aSTim Haley zpool_explain_recover(hdl, 18234b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1824468c413aSTim Haley break; 1825fa9e4066Sahrens } 1826fa9e4066Sahrens 18274b964adaSGeorge Wilson nvlist_free(nv); 1828fa9e4066Sahrens ret = -1; 1829fa9e4066Sahrens } else { 1830fa9e4066Sahrens zpool_handle_t *zhp; 1831ecd6cf80Smarks 1832fa9e4066Sahrens /* 1833fa9e4066Sahrens * This should never fail, but play it safe anyway. 1834fa9e4066Sahrens */ 1835681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 183694de1d4cSeschrock ret = -1; 1837681d9761SEric Taylor else if (zhp != NULL) 1838fa9e4066Sahrens zpool_close(zhp); 1839468c413aSTim Haley if (policy.zrp_request & 1840468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1841468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18424b964adaSGeorge Wilson ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv); 1843468c413aSTim Haley } 18444b964adaSGeorge Wilson nvlist_free(nv); 1845468c413aSTim Haley return (0); 1846fa9e4066Sahrens } 1847fa9e4066Sahrens 1848fa9e4066Sahrens return (ret); 1849fa9e4066Sahrens } 1850fa9e4066Sahrens 1851fa9e4066Sahrens /* 18523f9d6ad7SLin Ling * Scan the pool. 1853fa9e4066Sahrens */ 1854fa9e4066Sahrens int 1855*1702cce7SAlek Pinchuk zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) 1856fa9e4066Sahrens { 1857fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1858fa9e4066Sahrens char msg[1024]; 1859*1702cce7SAlek Pinchuk int err; 186099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 1861fa9e4066Sahrens 1862fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 18633f9d6ad7SLin Ling zc.zc_cookie = func; 1864*1702cce7SAlek Pinchuk zc.zc_flags = cmd; 1865fa9e4066Sahrens 1866*1702cce7SAlek Pinchuk if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0) 1867*1702cce7SAlek Pinchuk return (0); 1868*1702cce7SAlek Pinchuk 1869*1702cce7SAlek Pinchuk err = errno; 1870*1702cce7SAlek Pinchuk 1871*1702cce7SAlek Pinchuk /* ECANCELED on a scrub means we resumed a paused scrub */ 1872*1702cce7SAlek Pinchuk if (err == ECANCELED && func == POOL_SCAN_SCRUB && 1873*1702cce7SAlek Pinchuk cmd == POOL_SCRUB_NORMAL) 1874*1702cce7SAlek Pinchuk return (0); 1875*1702cce7SAlek Pinchuk 1876*1702cce7SAlek Pinchuk if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL) 1877fa9e4066Sahrens return (0); 1878fa9e4066Sahrens 18793f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 1880*1702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) { 1881*1702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1882*1702cce7SAlek Pinchuk "cannot pause scrubbing %s"), zc.zc_name); 1883*1702cce7SAlek Pinchuk } else { 1884*1702cce7SAlek Pinchuk assert(cmd == POOL_SCRUB_NORMAL); 1885*1702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 1886*1702cce7SAlek Pinchuk "cannot scrub %s"), zc.zc_name); 1887*1702cce7SAlek Pinchuk } 18883f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 18893f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 18903f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 18913f9d6ad7SLin Ling zc.zc_name); 18923f9d6ad7SLin Ling } else { 18933f9d6ad7SLin Ling assert(!"unexpected result"); 18943f9d6ad7SLin Ling } 1895fa9e4066Sahrens 1896*1702cce7SAlek Pinchuk if (err == EBUSY) { 18973f9d6ad7SLin Ling nvlist_t *nvroot; 18983f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 18993f9d6ad7SLin Ling uint_t psc; 19003f9d6ad7SLin Ling 19013f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 19023f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 19033f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 19043f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 1905*1702cce7SAlek Pinchuk if (ps && ps->pss_func == POOL_SCAN_SCRUB) { 1906*1702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) 1907*1702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg)); 1908*1702cce7SAlek Pinchuk else 1909*1702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 1910*1702cce7SAlek Pinchuk } else { 19113f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 1912*1702cce7SAlek Pinchuk } 1913*1702cce7SAlek Pinchuk } else if (err == ENOENT) { 19143f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 19153f9d6ad7SLin Ling } else { 1916*1702cce7SAlek Pinchuk return (zpool_standard_error(hdl, err, msg)); 19173f9d6ad7SLin Ling } 1918fa9e4066Sahrens } 1919fa9e4066Sahrens 19203fdda499SJohn Harres /* 19213fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 19223fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 19233fdda499SJohn Harres * verification of the s# part. 19243fdda499SJohn Harres */ 19253fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 19263fdda499SJohn Harres 19273fdda499SJohn Harres /* 19283fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 19293fdda499SJohn Harres * and the like. 19303fdda499SJohn Harres */ 19313fdda499SJohn Harres static int 19329a686fbcSPaul Dagnelie ctd_check_path(char *str) 19339a686fbcSPaul Dagnelie { 19343fdda499SJohn Harres /* 19353fdda499SJohn Harres * If it starts with a slash, check the last component. 19363fdda499SJohn Harres */ 19373fdda499SJohn Harres if (str && str[0] == '/') { 19383fdda499SJohn Harres char *tmp = strrchr(str, '/'); 19393fdda499SJohn Harres 19403fdda499SJohn Harres /* 19413fdda499SJohn Harres * If it ends in "/old", check the second-to-last 19423fdda499SJohn Harres * component of the string instead. 19433fdda499SJohn Harres */ 19443fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 19453fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 19463fdda499SJohn Harres ; 19473fdda499SJohn Harres } 19483fdda499SJohn Harres str = tmp + 1; 19493fdda499SJohn Harres } 19503fdda499SJohn Harres return (CTD_CHECK(str)); 19513fdda499SJohn Harres } 19523fdda499SJohn Harres 1953a43d325bSek /* 1954573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 1955573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 1956a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 1957a43d325bSek * spare; but FALSE if its an INUSE spare. 1958a43d325bSek */ 195999653d4eSeschrock static nvlist_t * 1960573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 1961573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 1962ea8dc4b6Seschrock { 1963ea8dc4b6Seschrock uint_t c, children; 1964ea8dc4b6Seschrock nvlist_t **child; 196599653d4eSeschrock nvlist_t *ret; 1966ee0eb9f2SEric Schrock uint64_t is_log; 1967573ca77eSGeorge Wilson char *srchkey; 1968573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 1969573ca77eSGeorge Wilson 1970573ca77eSGeorge Wilson /* Nothing to look for */ 1971573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 1972573ca77eSGeorge Wilson return (NULL); 1973ea8dc4b6Seschrock 1974573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 1975573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 1976573ca77eSGeorge Wilson 1977573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 1978cb04b873SMark J Musante case DATA_TYPE_UINT64: 1979573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 1980cb04b873SMark J Musante uint64_t srchval, theguid; 1981cb04b873SMark J Musante 1982cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 1983cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 1984cb04b873SMark J Musante &theguid) == 0); 1985cb04b873SMark J Musante if (theguid == srchval) 1986cb04b873SMark J Musante return (nv); 1987573ca77eSGeorge Wilson } 1988573ca77eSGeorge Wilson break; 1989573ca77eSGeorge Wilson 1990573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 1991573ca77eSGeorge Wilson char *srchval, *val; 1992573ca77eSGeorge Wilson 1993573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 1994573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 1995573ca77eSGeorge Wilson break; 1996ea8dc4b6Seschrock 1997ea8dc4b6Seschrock /* 19983fdda499SJohn Harres * Search for the requested value. Special cases: 19993fdda499SJohn Harres * 20007855d95bSToomas Soome * - ZPOOL_CONFIG_PATH for whole disk entries. To support 20017855d95bSToomas Soome * UEFI boot, these end in "s0" or "s0/old" or "s1" or 20027855d95bSToomas Soome * "s1/old". The "s0" or "s1" part is hidden from the user, 20033fdda499SJohn Harres * but included in the string, so this matches around it. 20043fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 20053fdda499SJohn Harres * 200688ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 2007ea8dc4b6Seschrock */ 20083fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 20093fdda499SJohn Harres ctd_check_path(val)) { 2010573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2011573ca77eSGeorge Wilson 2012573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2013573ca77eSGeorge Wilson &wholedisk); 2014573ca77eSGeorge Wilson if (wholedisk) { 20153fdda499SJohn Harres int slen = strlen(srchval); 20163fdda499SJohn Harres int vlen = strlen(val); 20173fdda499SJohn Harres 20183fdda499SJohn Harres if (slen != vlen - 2) 20193fdda499SJohn Harres break; 20203fdda499SJohn Harres 20213fdda499SJohn Harres /* 20223fdda499SJohn Harres * make_leaf_vdev() should only set 20233fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 20243fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 20253fdda499SJohn Harres * room for the indices used next. 20263fdda499SJohn Harres */ 20273fdda499SJohn Harres ASSERT(vlen >= 6); 20283fdda499SJohn Harres 20293fdda499SJohn Harres /* 20303fdda499SJohn Harres * strings identical except trailing "s0" 20313fdda499SJohn Harres */ 20327855d95bSToomas Soome if ((strcmp(&val[vlen - 2], "s0") == 0 || 20337855d95bSToomas Soome strcmp(&val[vlen - 2], "s1") == 0) && 20343fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 20353fdda499SJohn Harres return (nv); 20363fdda499SJohn Harres 2037573ca77eSGeorge Wilson /* 20383fdda499SJohn Harres * strings identical except trailing "s0/old" 2039573ca77eSGeorge Wilson */ 20407855d95bSToomas Soome if ((strcmp(&val[vlen - 6], "s0/old") == 0 || 20417855d95bSToomas Soome strcmp(&val[vlen - 6], "s1/old") == 0) && 20423fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 20433fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 2044573ca77eSGeorge Wilson return (nv); 20453fdda499SJohn Harres 2046573ca77eSGeorge Wilson break; 2047573ca77eSGeorge Wilson } 204888ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 204988ecc943SGeorge Wilson char *type, *idx, *end, *p; 205088ecc943SGeorge Wilson uint64_t id, vdev_id; 205188ecc943SGeorge Wilson 205288ecc943SGeorge Wilson /* 205388ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 205488ecc943SGeorge Wilson * that the srchval is composed of a type and 205588ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 205688ecc943SGeorge Wilson */ 205788ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 205888ecc943SGeorge Wilson return (NULL); 205988ecc943SGeorge Wilson 206088ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 206188ecc943SGeorge Wilson free(type); 206288ecc943SGeorge Wilson break; 206388ecc943SGeorge Wilson } 206488ecc943SGeorge Wilson idx = p + 1; 206588ecc943SGeorge Wilson *p = '\0'; 206688ecc943SGeorge Wilson 206788ecc943SGeorge Wilson /* 206888ecc943SGeorge Wilson * If the types don't match then keep looking. 206988ecc943SGeorge Wilson */ 207088ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 207188ecc943SGeorge Wilson free(type); 207288ecc943SGeorge Wilson break; 207388ecc943SGeorge Wilson } 207488ecc943SGeorge Wilson 207588ecc943SGeorge Wilson verify(strncmp(type, VDEV_TYPE_RAIDZ, 207688ecc943SGeorge Wilson strlen(VDEV_TYPE_RAIDZ)) == 0 || 207788ecc943SGeorge Wilson strncmp(type, VDEV_TYPE_MIRROR, 207888ecc943SGeorge Wilson strlen(VDEV_TYPE_MIRROR)) == 0); 207988ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 208088ecc943SGeorge Wilson &id) == 0); 208188ecc943SGeorge Wilson 208288ecc943SGeorge Wilson errno = 0; 208388ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 208488ecc943SGeorge Wilson 208588ecc943SGeorge Wilson free(type); 208688ecc943SGeorge Wilson if (errno != 0) 208788ecc943SGeorge Wilson return (NULL); 208888ecc943SGeorge Wilson 208988ecc943SGeorge Wilson /* 209088ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 209188ecc943SGeorge Wilson */ 209288ecc943SGeorge Wilson if (vdev_id == id) 209388ecc943SGeorge Wilson return (nv); 2094ea8dc4b6Seschrock } 2095573ca77eSGeorge Wilson 2096573ca77eSGeorge Wilson /* 2097573ca77eSGeorge Wilson * Common case 2098573ca77eSGeorge Wilson */ 2099573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2100573ca77eSGeorge Wilson return (nv); 2101573ca77eSGeorge Wilson break; 2102573ca77eSGeorge Wilson } 2103573ca77eSGeorge Wilson 2104573ca77eSGeorge Wilson default: 2105573ca77eSGeorge Wilson break; 2106ea8dc4b6Seschrock } 2107ea8dc4b6Seschrock 2108ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2109ea8dc4b6Seschrock &child, &children) != 0) 211099653d4eSeschrock return (NULL); 2111ea8dc4b6Seschrock 2112ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2113573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2114ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2115ee0eb9f2SEric Schrock /* 2116ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2117ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2118ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2119ee0eb9f2SEric Schrock * 'log' is non-NULL). 2120ee0eb9f2SEric Schrock */ 2121ee0eb9f2SEric Schrock if (log != NULL && 2122ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2123ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2124ee0eb9f2SEric Schrock is_log) { 2125ee0eb9f2SEric Schrock *log = B_TRUE; 2126ee0eb9f2SEric Schrock } 2127ea8dc4b6Seschrock return (ret); 2128ee0eb9f2SEric Schrock } 2129ee0eb9f2SEric Schrock } 2130ea8dc4b6Seschrock 213199653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 213299653d4eSeschrock &child, &children) == 0) { 213399653d4eSeschrock for (c = 0; c < children; c++) { 2134573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2135ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2136a43d325bSek *avail_spare = B_TRUE; 213799653d4eSeschrock return (ret); 213899653d4eSeschrock } 213999653d4eSeschrock } 214099653d4eSeschrock } 214199653d4eSeschrock 2142fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2143fa94a07fSbrendan &child, &children) == 0) { 2144fa94a07fSbrendan for (c = 0; c < children; c++) { 2145573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2146ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2147fa94a07fSbrendan *l2cache = B_TRUE; 2148fa94a07fSbrendan return (ret); 2149fa94a07fSbrendan } 2150fa94a07fSbrendan } 2151fa94a07fSbrendan } 2152fa94a07fSbrendan 215399653d4eSeschrock return (NULL); 2154ea8dc4b6Seschrock } 2155ea8dc4b6Seschrock 2156573ca77eSGeorge Wilson /* 2157573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2158573ca77eSGeorge Wilson * associated vdev. 2159573ca77eSGeorge Wilson */ 2160573ca77eSGeorge Wilson nvlist_t * 2161573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2162573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2163573ca77eSGeorge Wilson { 2164573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2165573ca77eSGeorge Wilson 2166573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2167573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2168573ca77eSGeorge Wilson 2169573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2170573ca77eSGeorge Wilson &nvroot) == 0); 2171573ca77eSGeorge Wilson 2172573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2173cb04b873SMark J Musante *l2cache = B_FALSE; 2174daeb70e5SMark J Musante if (log != NULL) 2175daeb70e5SMark J Musante *log = B_FALSE; 2176573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2177573ca77eSGeorge Wilson nvlist_free(search); 2178573ca77eSGeorge Wilson 2179573ca77eSGeorge Wilson return (ret); 2180573ca77eSGeorge Wilson } 2181573ca77eSGeorge Wilson 218288ecc943SGeorge Wilson /* 218388ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 218488ecc943SGeorge Wilson */ 218588ecc943SGeorge Wilson boolean_t 218688ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 218788ecc943SGeorge Wilson { 218888ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 218988ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 219088ecc943SGeorge Wilson return (B_TRUE); 219188ecc943SGeorge Wilson return (B_FALSE); 219288ecc943SGeorge Wilson } 219388ecc943SGeorge Wilson 219499653d4eSeschrock nvlist_t * 2195fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2196ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2197ea8dc4b6Seschrock { 2198ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2199ea8dc4b6Seschrock char *end; 2200573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2201ea8dc4b6Seschrock uint64_t guid; 2202ea8dc4b6Seschrock 2203573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2204573ca77eSGeorge Wilson 22050917b783Seschrock guid = strtoull(path, &end, 10); 2206ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2207573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 220888ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 220988ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2210ea8dc4b6Seschrock } else if (path[0] != '/') { 22116401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT, 22126401734dSWill Andrews path); 2213573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2214ea8dc4b6Seschrock } else { 2215573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2216ea8dc4b6Seschrock } 2217ea8dc4b6Seschrock 2218ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2219ea8dc4b6Seschrock &nvroot) == 0); 2220ea8dc4b6Seschrock 2221a43d325bSek *avail_spare = B_FALSE; 2222fa94a07fSbrendan *l2cache = B_FALSE; 2223ee0eb9f2SEric Schrock if (log != NULL) 2224ee0eb9f2SEric Schrock *log = B_FALSE; 2225573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2226573ca77eSGeorge Wilson nvlist_free(search); 2227573ca77eSGeorge Wilson 2228573ca77eSGeorge Wilson return (ret); 2229a43d325bSek } 2230a43d325bSek 223119397407SSherry Moore static int 223219397407SSherry Moore vdev_online(nvlist_t *nv) 223319397407SSherry Moore { 223419397407SSherry Moore uint64_t ival; 223519397407SSherry Moore 223619397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 223719397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 223819397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 223919397407SSherry Moore return (0); 224019397407SSherry Moore 224119397407SSherry Moore return (1); 224219397407SSherry Moore } 224319397407SSherry Moore 224419397407SSherry Moore /* 224521ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 224619397407SSherry Moore */ 2247753a6d45SSherry Moore static int 224821ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2249753a6d45SSherry Moore size_t *bytes_written) 225019397407SSherry Moore { 2251753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2252753a6d45SSherry Moore char *tmppath; 2253753a6d45SSherry Moore const char *format; 2254753a6d45SSherry Moore 2255753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2256753a6d45SSherry Moore &tmppath) != 0) 2257753a6d45SSherry Moore return (EZFS_NODEVICE); 2258753a6d45SSherry Moore 2259753a6d45SSherry Moore pos = *bytes_written; 2260753a6d45SSherry Moore bytes_left = physpath_size - pos; 2261753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2262753a6d45SSherry Moore 2263753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2264753a6d45SSherry Moore *bytes_written += rsz; 2265753a6d45SSherry Moore 2266753a6d45SSherry Moore if (rsz >= bytes_left) { 2267753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2268753a6d45SSherry Moore if (bytes_left != 0) { 2269753a6d45SSherry Moore physpath[pos] = 0; 2270753a6d45SSherry Moore } 2271753a6d45SSherry Moore return (EZFS_NOSPC); 2272753a6d45SSherry Moore } 2273753a6d45SSherry Moore return (0); 2274753a6d45SSherry Moore } 2275753a6d45SSherry Moore 227621ecdf64SLin Ling static int 227721ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 227821ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 227921ecdf64SLin Ling { 228021ecdf64SLin Ling char *type; 228121ecdf64SLin Ling int ret; 228221ecdf64SLin Ling 228321ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 228421ecdf64SLin Ling return (EZFS_INVALCONFIG); 228521ecdf64SLin Ling 228621ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 228721ecdf64SLin Ling /* 228821ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 228921ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 229021ecdf64SLin Ling * spare device. 229121ecdf64SLin Ling */ 229221ecdf64SLin Ling if (is_spare) { 229321ecdf64SLin Ling uint64_t spare = 0; 229421ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 229521ecdf64SLin Ling &spare); 229621ecdf64SLin Ling if (!spare) 229721ecdf64SLin Ling return (EZFS_INVALCONFIG); 229821ecdf64SLin Ling } 229921ecdf64SLin Ling 230021ecdf64SLin Ling if (vdev_online(nv)) { 230121ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 230221ecdf64SLin Ling phypath_size, rsz)) != 0) 230321ecdf64SLin Ling return (ret); 230421ecdf64SLin Ling } 230521ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2306d5f26ad8SToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 230721ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 230821ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 230921ecdf64SLin Ling nvlist_t **child; 231021ecdf64SLin Ling uint_t count; 231121ecdf64SLin Ling int i, ret; 231221ecdf64SLin Ling 231321ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 231421ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 231521ecdf64SLin Ling return (EZFS_INVALCONFIG); 231621ecdf64SLin Ling 231721ecdf64SLin Ling for (i = 0; i < count; i++) { 231821ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 231921ecdf64SLin Ling phypath_size, rsz, is_spare); 232021ecdf64SLin Ling if (ret == EZFS_NOSPC) 232121ecdf64SLin Ling return (ret); 232221ecdf64SLin Ling } 232321ecdf64SLin Ling } 232421ecdf64SLin Ling 232521ecdf64SLin Ling return (EZFS_POOL_INVALARG); 232621ecdf64SLin Ling } 232721ecdf64SLin Ling 2328753a6d45SSherry Moore /* 2329753a6d45SSherry Moore * Get phys_path for a root pool config. 2330753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2331753a6d45SSherry Moore */ 2332753a6d45SSherry Moore static int 2333753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2334753a6d45SSherry Moore { 2335753a6d45SSherry Moore size_t rsz; 233619397407SSherry Moore nvlist_t *vdev_root; 233719397407SSherry Moore nvlist_t **child; 233819397407SSherry Moore uint_t count; 2339753a6d45SSherry Moore char *type; 2340753a6d45SSherry Moore 2341753a6d45SSherry Moore rsz = 0; 2342753a6d45SSherry Moore 2343753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2344753a6d45SSherry Moore &vdev_root) != 0) 2345753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2346753a6d45SSherry Moore 2347753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2348753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2349753a6d45SSherry Moore &child, &count) != 0) 2350753a6d45SSherry Moore return (EZFS_INVALCONFIG); 235119397407SSherry Moore 235219397407SSherry Moore /* 23531a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 235419397407SSherry Moore */ 23551a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2356753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 235719397407SSherry Moore 235821ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 235921ecdf64SLin Ling B_FALSE); 236019397407SSherry Moore 2361753a6d45SSherry Moore /* No online devices */ 2362753a6d45SSherry Moore if (rsz == 0) 2363753a6d45SSherry Moore return (EZFS_NODEVICE); 2364753a6d45SSherry Moore 236519397407SSherry Moore return (0); 236619397407SSherry Moore } 236719397407SSherry Moore 2368753a6d45SSherry Moore /* 2369753a6d45SSherry Moore * Get phys_path for a root pool 2370753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2371753a6d45SSherry Moore */ 2372753a6d45SSherry Moore int 2373753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2374753a6d45SSherry Moore { 2375753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2376753a6d45SSherry Moore phypath_size)); 2377753a6d45SSherry Moore } 2378753a6d45SSherry Moore 2379573ca77eSGeorge Wilson /* 2380573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2381573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2382573ca77eSGeorge Wilson */ 2383573ca77eSGeorge Wilson static int 2384573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2385573ca77eSGeorge Wilson { 2386573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2387573ca77eSGeorge Wilson char errbuf[1024]; 2388573ca77eSGeorge Wilson int fd, error; 2389573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2390573ca77eSGeorge Wilson 2391573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2392573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2393573ca77eSGeorge Wilson return (-1); 2394573ca77eSGeorge Wilson 23956401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name); 2396573ca77eSGeorge Wilson 2397573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2398573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2399573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2400573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2401573ca77eSGeorge Wilson } 2402573ca77eSGeorge Wilson 2403573ca77eSGeorge Wilson /* 2404573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2405573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2406573ca77eSGeorge Wilson * ignore that error and continue on. 2407573ca77eSGeorge Wilson */ 2408573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2409573ca77eSGeorge Wilson (void) close(fd); 2410573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2411573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2412573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2413573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2414573ca77eSGeorge Wilson } 2415573ca77eSGeorge Wilson return (0); 2416573ca77eSGeorge Wilson } 2417573ca77eSGeorge Wilson 2418fa9e4066Sahrens /* 24193d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 24203d7072f8Seschrock * ZFS_ONLINE_* flags. 2421fa9e4066Sahrens */ 2422fa9e4066Sahrens int 24233d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 24243d7072f8Seschrock vdev_state_t *newstate) 2425fa9e4066Sahrens { 2426fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2427fa9e4066Sahrens char msg[1024]; 242899653d4eSeschrock nvlist_t *tgt; 2429573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 243099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2431fa9e4066Sahrens 2432573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2433573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2434573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2435573ca77eSGeorge Wilson } else { 2436573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2437573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2438573ca77eSGeorge Wilson } 2439ea8dc4b6Seschrock 2440fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2441ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2442573ca77eSGeorge Wilson &islog)) == NULL) 244399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2444fa9e4066Sahrens 244599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2446fa9e4066Sahrens 2447069f55e2SEric Schrock if (avail_spare) 2448a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2449a43d325bSek 2450573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND || 2451573ca77eSGeorge Wilson zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 2452573ca77eSGeorge Wilson char *pathname = NULL; 2453573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2454573ca77eSGeorge Wilson 2455573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2456573ca77eSGeorge Wilson &wholedisk); 2457573ca77eSGeorge Wilson verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 2458573ca77eSGeorge Wilson &pathname) == 0); 2459573ca77eSGeorge Wilson 2460573ca77eSGeorge Wilson /* 2461573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2462573ca77eSGeorge Wilson */ 2463573ca77eSGeorge Wilson if (l2cache) { 2464573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2465573ca77eSGeorge Wilson "cannot expand cache devices")); 2466573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2467573ca77eSGeorge Wilson } 2468573ca77eSGeorge Wilson 2469573ca77eSGeorge Wilson if (wholedisk) { 24706401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1; 2471cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2472573ca77eSGeorge Wilson } 2473573ca77eSGeorge Wilson } 2474573ca77eSGeorge Wilson 24753d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 24763d7072f8Seschrock zc.zc_obj = flags; 2477fa9e4066Sahrens 2478cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 24791195e687SMark J Musante if (errno == EINVAL) { 24801195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 24811195e687SMark J Musante "from this pool into a new one. Use '%s' " 24821195e687SMark J Musante "instead"), "zpool detach"); 24831195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 24841195e687SMark J Musante } 24853d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 24861195e687SMark J Musante } 24873d7072f8Seschrock 24883d7072f8Seschrock *newstate = zc.zc_cookie; 24893d7072f8Seschrock return (0); 2490fa9e4066Sahrens } 2491fa9e4066Sahrens 2492fa9e4066Sahrens /* 2493fa9e4066Sahrens * Take the specified vdev offline 2494fa9e4066Sahrens */ 2495fa9e4066Sahrens int 24963d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2497fa9e4066Sahrens { 2498fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2499fa9e4066Sahrens char msg[1024]; 250099653d4eSeschrock nvlist_t *tgt; 2501fa94a07fSbrendan boolean_t avail_spare, l2cache; 250299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2503fa9e4066Sahrens 2504ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2505ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2506ea8dc4b6Seschrock 2507fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2508ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2509ee0eb9f2SEric Schrock NULL)) == NULL) 251099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 251199653d4eSeschrock 251299653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2513fa9e4066Sahrens 2514069f55e2SEric Schrock if (avail_spare) 2515a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2516a43d325bSek 25173d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 25183d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 25193d7072f8Seschrock 2520cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25213d7072f8Seschrock return (0); 25223d7072f8Seschrock 25233d7072f8Seschrock switch (errno) { 25243d7072f8Seschrock case EBUSY: 25253d7072f8Seschrock 25263d7072f8Seschrock /* 25273d7072f8Seschrock * There are no other replicas of this device. 25283d7072f8Seschrock */ 25293d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 25303d7072f8Seschrock 2531e6ca193dSGeorge Wilson case EEXIST: 2532e6ca193dSGeorge Wilson /* 2533e6ca193dSGeorge Wilson * The log device has unplayed logs 2534e6ca193dSGeorge Wilson */ 2535e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2536e6ca193dSGeorge Wilson 25373d7072f8Seschrock default: 25383d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 25393d7072f8Seschrock } 25403d7072f8Seschrock } 25413d7072f8Seschrock 25423d7072f8Seschrock /* 25433d7072f8Seschrock * Mark the given vdev faulted. 25443d7072f8Seschrock */ 25453d7072f8Seschrock int 2546069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25473d7072f8Seschrock { 25483d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25493d7072f8Seschrock char msg[1024]; 25503d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25513d7072f8Seschrock 25523d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25533d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2554441d80aaSlling 25553d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25563d7072f8Seschrock zc.zc_guid = guid; 25573d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2558069f55e2SEric Schrock zc.zc_obj = aux; 25593d7072f8Seschrock 2560cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2561fa9e4066Sahrens return (0); 2562fa9e4066Sahrens 2563fa9e4066Sahrens switch (errno) { 256499653d4eSeschrock case EBUSY: 2565fa9e4066Sahrens 2566fa9e4066Sahrens /* 2567fa9e4066Sahrens * There are no other replicas of this device. 2568fa9e4066Sahrens */ 256999653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2570fa9e4066Sahrens 257199653d4eSeschrock default: 257299653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2573fa9e4066Sahrens } 25743d7072f8Seschrock 25753d7072f8Seschrock } 25763d7072f8Seschrock 25773d7072f8Seschrock /* 25783d7072f8Seschrock * Mark the given vdev degraded. 25793d7072f8Seschrock */ 25803d7072f8Seschrock int 2581069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 25823d7072f8Seschrock { 25833d7072f8Seschrock zfs_cmd_t zc = { 0 }; 25843d7072f8Seschrock char msg[1024]; 25853d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 25863d7072f8Seschrock 25873d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 25883d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 25893d7072f8Seschrock 25903d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 25913d7072f8Seschrock zc.zc_guid = guid; 25923d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2593069f55e2SEric Schrock zc.zc_obj = aux; 25943d7072f8Seschrock 2595cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 25963d7072f8Seschrock return (0); 25973d7072f8Seschrock 25983d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 259999653d4eSeschrock } 260099653d4eSeschrock 260199653d4eSeschrock /* 260299653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 260399653d4eSeschrock * a hot spare. 260499653d4eSeschrock */ 260599653d4eSeschrock static boolean_t 260699653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 260799653d4eSeschrock { 260899653d4eSeschrock nvlist_t **child; 260999653d4eSeschrock uint_t c, children; 261099653d4eSeschrock char *type; 261199653d4eSeschrock 261299653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 261399653d4eSeschrock &children) == 0) { 261499653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 261599653d4eSeschrock &type) == 0); 261699653d4eSeschrock 261799653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 261899653d4eSeschrock children == 2 && child[which] == tgt) 261999653d4eSeschrock return (B_TRUE); 262099653d4eSeschrock 262199653d4eSeschrock for (c = 0; c < children; c++) 262299653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 262399653d4eSeschrock return (B_TRUE); 262499653d4eSeschrock } 262599653d4eSeschrock 262699653d4eSeschrock return (B_FALSE); 2627fa9e4066Sahrens } 2628fa9e4066Sahrens 2629fa9e4066Sahrens /* 2630fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 26318654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2632fa9e4066Sahrens */ 2633fa9e4066Sahrens int 2634fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2635fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2636fa9e4066Sahrens { 2637fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2638fa9e4066Sahrens char msg[1024]; 2639fa9e4066Sahrens int ret; 264099653d4eSeschrock nvlist_t *tgt; 2641ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2642ee0eb9f2SEric Schrock uint64_t val; 2643cb04b873SMark J Musante char *newname; 264499653d4eSeschrock nvlist_t **child; 264599653d4eSeschrock uint_t children; 264699653d4eSeschrock nvlist_t *config_root; 264799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 26484263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2649fa9e4066Sahrens 2650ea8dc4b6Seschrock if (replacing) 2651ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2652ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2653ea8dc4b6Seschrock else 2654ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2655ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2656ea8dc4b6Seschrock 2657fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2658ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 2659ee0eb9f2SEric Schrock &islog)) == 0) 266099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 266199653d4eSeschrock 2662a43d325bSek if (avail_spare) 266399653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 266499653d4eSeschrock 2665fa94a07fSbrendan if (l2cache) 2666fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2667fa94a07fSbrendan 266899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2669fa9e4066Sahrens zc.zc_cookie = replacing; 2670fa9e4066Sahrens 267199653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 267299653d4eSeschrock &child, &children) != 0 || children != 1) { 267399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 267499653d4eSeschrock "new device must be a single disk")); 267599653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 267699653d4eSeschrock } 267799653d4eSeschrock 267899653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 267999653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 268099653d4eSeschrock 268188ecc943SGeorge Wilson if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL) 26820430f8daSeschrock return (-1); 26830430f8daSeschrock 268499653d4eSeschrock /* 268599653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 268699653d4eSeschrock * replace it with another hot spare. 268799653d4eSeschrock */ 268899653d4eSeschrock if (replacing && 268999653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2690ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2691ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2692ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 269399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 269499653d4eSeschrock "can only be replaced by another hot spare")); 26950430f8daSeschrock free(newname); 269699653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 269799653d4eSeschrock } 269899653d4eSeschrock 26990430f8daSeschrock free(newname); 27000430f8daSeschrock 2701990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 270299653d4eSeschrock return (-1); 2703fa9e4066Sahrens 2704cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2705fa9e4066Sahrens 2706e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2707fa9e4066Sahrens 2708b5b76fecSGeorge Wilson if (ret == 0) { 2709b5b76fecSGeorge Wilson if (rootpool) { 271021ecdf64SLin Ling /* 271121ecdf64SLin Ling * XXX need a better way to prevent user from 271221ecdf64SLin Ling * booting up a half-baked vdev. 271321ecdf64SLin Ling */ 271421ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 271521ecdf64SLin Ling "sure to wait until resilver is done " 271621ecdf64SLin Ling "before rebooting.\n")); 2717b5b76fecSGeorge Wilson } 2718fa9e4066Sahrens return (0); 2719b5b76fecSGeorge Wilson } 2720fa9e4066Sahrens 2721fa9e4066Sahrens switch (errno) { 2722ea8dc4b6Seschrock case ENOTSUP: 2723fa9e4066Sahrens /* 2724fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2725fa9e4066Sahrens */ 27268654d025Sperrin if (replacing) { 2727cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2728cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2729cb04b873SMark J Musante 2730ee0eb9f2SEric Schrock if (islog) 27318654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27328654d025Sperrin "cannot replace a log with a spare")); 2733cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2734cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2735cb04b873SMark J Musante "already in replacing/spare config; wait " 2736cb04b873SMark J Musante "for completion or use 'zpool detach'")); 27378654d025Sperrin else 27388654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27398654d025Sperrin "cannot replace a replacing device")); 27408654d025Sperrin } else { 274199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274299653d4eSeschrock "can only attach to mirrors and top-level " 274399653d4eSeschrock "disks")); 27448654d025Sperrin } 274599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2746fa9e4066Sahrens break; 2747fa9e4066Sahrens 2748ea8dc4b6Seschrock case EINVAL: 2749fa9e4066Sahrens /* 2750fa9e4066Sahrens * The new device must be a single disk. 2751fa9e4066Sahrens */ 275299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 275399653d4eSeschrock "new device must be a single disk")); 275499653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2755fa9e4066Sahrens break; 2756fa9e4066Sahrens 2757ea8dc4b6Seschrock case EBUSY: 275899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), 275999653d4eSeschrock new_disk); 276099653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2761fa9e4066Sahrens break; 2762fa9e4066Sahrens 2763ea8dc4b6Seschrock case EOVERFLOW: 2764fa9e4066Sahrens /* 2765fa9e4066Sahrens * The new device is too small. 2766fa9e4066Sahrens */ 276799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 276899653d4eSeschrock "device is too small")); 276999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2770fa9e4066Sahrens break; 2771fa9e4066Sahrens 2772ea8dc4b6Seschrock case EDOM: 2773fa9e4066Sahrens /* 2774fa9e4066Sahrens * The new device has a different alignment requirement. 2775fa9e4066Sahrens */ 277699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 277799653d4eSeschrock "devices have different sector alignment")); 277899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 2779fa9e4066Sahrens break; 2780fa9e4066Sahrens 2781ea8dc4b6Seschrock case ENAMETOOLONG: 2782fa9e4066Sahrens /* 2783fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 2784fa9e4066Sahrens */ 278599653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 2786fa9e4066Sahrens break; 2787fa9e4066Sahrens 2788ea8dc4b6Seschrock default: 278999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2790fa9e4066Sahrens } 2791fa9e4066Sahrens 279299653d4eSeschrock return (-1); 2793fa9e4066Sahrens } 2794fa9e4066Sahrens 2795fa9e4066Sahrens /* 2796fa9e4066Sahrens * Detach the specified device. 2797fa9e4066Sahrens */ 2798fa9e4066Sahrens int 2799fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 2800fa9e4066Sahrens { 2801fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2802fa9e4066Sahrens char msg[1024]; 280399653d4eSeschrock nvlist_t *tgt; 2804fa94a07fSbrendan boolean_t avail_spare, l2cache; 280599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2806fa9e4066Sahrens 2807ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2808ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 2809ea8dc4b6Seschrock 2810fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2811ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2812ee0eb9f2SEric Schrock NULL)) == 0) 281399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2814fa9e4066Sahrens 2815a43d325bSek if (avail_spare) 281699653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 281799653d4eSeschrock 2818fa94a07fSbrendan if (l2cache) 2819fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2820fa94a07fSbrendan 282199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 282299653d4eSeschrock 2823ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 2824fa9e4066Sahrens return (0); 2825fa9e4066Sahrens 2826fa9e4066Sahrens switch (errno) { 2827fa9e4066Sahrens 2828ea8dc4b6Seschrock case ENOTSUP: 2829fa9e4066Sahrens /* 2830fa9e4066Sahrens * Can't detach from this type of vdev. 2831fa9e4066Sahrens */ 283299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 283399653d4eSeschrock "applicable to mirror and replacing vdevs")); 2834cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2835fa9e4066Sahrens break; 2836fa9e4066Sahrens 2837ea8dc4b6Seschrock case EBUSY: 2838fa9e4066Sahrens /* 2839fa9e4066Sahrens * There are no other replicas of this device. 2840fa9e4066Sahrens */ 284199653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 2842fa9e4066Sahrens break; 2843fa9e4066Sahrens 2844ea8dc4b6Seschrock default: 284599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 2846ea8dc4b6Seschrock } 2847ea8dc4b6Seschrock 284899653d4eSeschrock return (-1); 284999653d4eSeschrock } 285099653d4eSeschrock 28511195e687SMark J Musante /* 28521195e687SMark J Musante * Find a mirror vdev in the source nvlist. 28531195e687SMark J Musante * 28541195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 28551195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 28561195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 28571195e687SMark J Musante * see if any entry in the schild array matches. 28581195e687SMark J Musante * 28591195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 28601195e687SMark J Musante * the index of that entry. Otherwise we return -1. 28611195e687SMark J Musante */ 28621195e687SMark J Musante static int 28631195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 28641195e687SMark J Musante nvlist_t **schild, uint_t schildren) 28651195e687SMark J Musante { 28661195e687SMark J Musante uint_t mc; 28671195e687SMark J Musante 28681195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 28691195e687SMark J Musante uint_t sc; 28701195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28711195e687SMark J Musante mchild[mc], B_FALSE); 28721195e687SMark J Musante 28731195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 28741195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 28751195e687SMark J Musante schild[sc], B_FALSE); 28761195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 28771195e687SMark J Musante 28781195e687SMark J Musante free(spath); 28791195e687SMark J Musante if (result) { 28801195e687SMark J Musante free(mpath); 28811195e687SMark J Musante return (mc); 28821195e687SMark J Musante } 28831195e687SMark J Musante } 28841195e687SMark J Musante 28851195e687SMark J Musante free(mpath); 28861195e687SMark J Musante } 28871195e687SMark J Musante 28881195e687SMark J Musante return (-1); 28891195e687SMark J Musante } 28901195e687SMark J Musante 28911195e687SMark J Musante /* 28921195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 28931195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 28941195e687SMark J Musante */ 28951195e687SMark J Musante int 28961195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 28971195e687SMark J Musante nvlist_t *props, splitflags_t flags) 28981195e687SMark J Musante { 28991195e687SMark J Musante zfs_cmd_t zc = { 0 }; 29001195e687SMark J Musante char msg[1024]; 29011195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 29021195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 29031195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 29041195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 29051195e687SMark J Musante uint64_t vers; 29061195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 29071195e687SMark J Musante int retval = 0; 29081195e687SMark J Musante 29091195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 29101195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 29111195e687SMark J Musante 29121195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 29131195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 29141195e687SMark J Musante 29151195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 29161195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 29171195e687SMark J Musante "retrieve pool configuration\n")); 29181195e687SMark J Musante return (-1); 29191195e687SMark J Musante } 29201195e687SMark J Musante 29211195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 29221195e687SMark J Musante == 0); 29231195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 29241195e687SMark J Musante 29251195e687SMark J Musante if (props) { 2926f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 29271195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 2928f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 29291195e687SMark J Musante return (-1); 29301195e687SMark J Musante } 29311195e687SMark J Musante 29321195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 29331195e687SMark J Musante &children) != 0) { 29341195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29351195e687SMark J Musante "Source pool is missing vdev tree")); 2936aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 29371195e687SMark J Musante return (-1); 29381195e687SMark J Musante } 29391195e687SMark J Musante 29401195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 29411195e687SMark J Musante vcount = 0; 29421195e687SMark J Musante 29431195e687SMark J Musante if (*newroot == NULL || 29441195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 29451195e687SMark J Musante &newchild, &newchildren) != 0) 29461195e687SMark J Musante newchildren = 0; 29471195e687SMark J Musante 29481195e687SMark J Musante for (c = 0; c < children; c++) { 29491195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 29501195e687SMark J Musante char *type; 29511195e687SMark J Musante nvlist_t **mchild, *vdev; 29521195e687SMark J Musante uint_t mchildren; 29531195e687SMark J Musante int entry; 29541195e687SMark J Musante 29551195e687SMark J Musante /* 29561195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 29571195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 29581195e687SMark J Musante */ 29591195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 29601195e687SMark J Musante &is_log); 29611195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 29621195e687SMark J Musante &is_hole); 29631195e687SMark J Musante if (is_log || is_hole) { 29641195e687SMark J Musante /* 29651195e687SMark J Musante * Create a hole vdev and put it in the config. 29661195e687SMark J Musante */ 29671195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 29681195e687SMark J Musante goto out; 29691195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 29701195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 29711195e687SMark J Musante goto out; 29721195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 29731195e687SMark J Musante 1) != 0) 29741195e687SMark J Musante goto out; 29751195e687SMark J Musante if (lastlog == 0) 29761195e687SMark J Musante lastlog = vcount; 29771195e687SMark J Musante varray[vcount++] = vdev; 29781195e687SMark J Musante continue; 29791195e687SMark J Musante } 29801195e687SMark J Musante lastlog = 0; 29811195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 29821195e687SMark J Musante == 0); 29831195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 29841195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29851195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 29861195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 29871195e687SMark J Musante goto out; 29881195e687SMark J Musante } 29891195e687SMark J Musante 29901195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 29911195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 29921195e687SMark J Musante 29931195e687SMark J Musante /* find or add an entry for this top-level vdev */ 29941195e687SMark J Musante if (newchildren > 0 && 29951195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 29961195e687SMark J Musante newchild, newchildren)) >= 0) { 29971195e687SMark J Musante /* We found a disk that the user specified. */ 29981195e687SMark J Musante vdev = mchild[entry]; 29991195e687SMark J Musante ++found; 30001195e687SMark J Musante } else { 30011195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 30021195e687SMark J Musante vdev = mchild[mchildren - 1]; 30031195e687SMark J Musante } 30041195e687SMark J Musante 30051195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 30061195e687SMark J Musante goto out; 30071195e687SMark J Musante } 30081195e687SMark J Musante 30091195e687SMark J Musante /* did we find every disk the user specified? */ 30101195e687SMark J Musante if (found != newchildren) { 30111195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 30121195e687SMark J Musante "include at most one disk from each mirror")); 30131195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 30141195e687SMark J Musante goto out; 30151195e687SMark J Musante } 30161195e687SMark J Musante 30171195e687SMark J Musante /* Prepare the nvlist for populating. */ 30181195e687SMark J Musante if (*newroot == NULL) { 30191195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 30201195e687SMark J Musante goto out; 30211195e687SMark J Musante freelist = B_TRUE; 30221195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 30231195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 30241195e687SMark J Musante goto out; 30251195e687SMark J Musante } else { 30261195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 30271195e687SMark J Musante } 30281195e687SMark J Musante 30291195e687SMark J Musante /* Add all the children we found */ 30301195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 30311195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 30321195e687SMark J Musante goto out; 30331195e687SMark J Musante 30341195e687SMark J Musante /* 30351195e687SMark J Musante * If we're just doing a dry run, exit now with success. 30361195e687SMark J Musante */ 30371195e687SMark J Musante if (flags.dryrun) { 30381195e687SMark J Musante memory_err = B_FALSE; 30391195e687SMark J Musante freelist = B_FALSE; 30401195e687SMark J Musante goto out; 30411195e687SMark J Musante } 30421195e687SMark J Musante 30431195e687SMark J Musante /* now build up the config list & call the ioctl */ 30441195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 30451195e687SMark J Musante goto out; 30461195e687SMark J Musante 30471195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 30481195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 30491195e687SMark J Musante nvlist_add_string(newconfig, 30501195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 30511195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 30521195e687SMark J Musante goto out; 30531195e687SMark J Musante 30541195e687SMark J Musante /* 30551195e687SMark J Musante * The new pool is automatically part of the namespace unless we 30561195e687SMark J Musante * explicitly export it. 30571195e687SMark J Musante */ 30581195e687SMark J Musante if (!flags.import) 30591195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 30601195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 30611195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 30621195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 30631195e687SMark J Musante goto out; 30641195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 30651195e687SMark J Musante goto out; 30661195e687SMark J Musante 30671195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 30681195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 30691195e687SMark J Musante goto out; 30701195e687SMark J Musante } 30711195e687SMark J Musante 30721195e687SMark J Musante freelist = B_FALSE; 30731195e687SMark J Musante memory_err = B_FALSE; 30741195e687SMark J Musante 30751195e687SMark J Musante out: 30761195e687SMark J Musante if (varray != NULL) { 30771195e687SMark J Musante int v; 30781195e687SMark J Musante 30791195e687SMark J Musante for (v = 0; v < vcount; v++) 30801195e687SMark J Musante nvlist_free(varray[v]); 30811195e687SMark J Musante free(varray); 30821195e687SMark J Musante } 30831195e687SMark J Musante zcmd_free_nvlists(&zc); 3084aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 3085aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig); 30861195e687SMark J Musante if (freelist) { 30871195e687SMark J Musante nvlist_free(*newroot); 30881195e687SMark J Musante *newroot = NULL; 30891195e687SMark J Musante } 30901195e687SMark J Musante 30911195e687SMark J Musante if (retval != 0) 30921195e687SMark J Musante return (retval); 30931195e687SMark J Musante 30941195e687SMark J Musante if (memory_err) 30951195e687SMark J Musante return (no_memory(hdl)); 30961195e687SMark J Musante 30971195e687SMark J Musante return (0); 30981195e687SMark J Musante } 30991195e687SMark J Musante 310099653d4eSeschrock /* 3101fa94a07fSbrendan * Remove the given device. Currently, this is supported only for hot spares 3102fa94a07fSbrendan * and level 2 cache devices. 310399653d4eSeschrock */ 310499653d4eSeschrock int 310599653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 310699653d4eSeschrock { 310799653d4eSeschrock zfs_cmd_t zc = { 0 }; 310899653d4eSeschrock char msg[1024]; 310999653d4eSeschrock nvlist_t *tgt; 311088ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 311199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 311288ecc943SGeorge Wilson uint64_t version; 311399653d4eSeschrock 311499653d4eSeschrock (void) snprintf(msg, sizeof (msg), 311599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 311699653d4eSeschrock 311799653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3118ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 311988ecc943SGeorge Wilson &islog)) == 0) 312099653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 312188ecc943SGeorge Wilson /* 312288ecc943SGeorge Wilson * XXX - this should just go away. 312388ecc943SGeorge Wilson */ 312488ecc943SGeorge Wilson if (!avail_spare && !l2cache && !islog) { 312599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 312688ecc943SGeorge Wilson "only inactive hot spares, cache, top-level, " 312788ecc943SGeorge Wilson "or log devices can be removed")); 312899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 312999653d4eSeschrock } 313099653d4eSeschrock 313188ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 313288ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 313388ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 313488ecc943SGeorge Wilson "pool must be upgrade to support log removal")); 313588ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 313688ecc943SGeorge Wilson } 313788ecc943SGeorge Wilson 313899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 313999653d4eSeschrock 3140ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 314199653d4eSeschrock return (0); 314299653d4eSeschrock 314399653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3144ea8dc4b6Seschrock } 3145ea8dc4b6Seschrock 3146ea8dc4b6Seschrock /* 3147ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3148ea8dc4b6Seschrock */ 3149ea8dc4b6Seschrock int 3150468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3151ea8dc4b6Seschrock { 3152ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3153ea8dc4b6Seschrock char msg[1024]; 315499653d4eSeschrock nvlist_t *tgt; 3155468c413aSTim Haley zpool_rewind_policy_t policy; 3156fa94a07fSbrendan boolean_t avail_spare, l2cache; 315799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3158468c413aSTim Haley nvlist_t *nvi = NULL; 31594b964adaSGeorge Wilson int error; 3160ea8dc4b6Seschrock 3161ea8dc4b6Seschrock if (path) 3162ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3163ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3164e9dbad6fSeschrock path); 3165ea8dc4b6Seschrock else 3166ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3167ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3168ea8dc4b6Seschrock zhp->zpool_name); 3169ea8dc4b6Seschrock 3170ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 317199653d4eSeschrock if (path) { 3172fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 3173ee0eb9f2SEric Schrock &l2cache, NULL)) == 0) 317499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3175ea8dc4b6Seschrock 3176fa94a07fSbrendan /* 3177fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3178fa94a07fSbrendan * error clearing for l2cache devices. 3179fa94a07fSbrendan */ 3180a43d325bSek if (avail_spare) 318199653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3182ea8dc4b6Seschrock 318399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 318499653d4eSeschrock &zc.zc_guid) == 0); 3185fa9e4066Sahrens } 3186fa9e4066Sahrens 3187468c413aSTim Haley zpool_get_rewind_policy(rewindnvl, &policy); 3188468c413aSTim Haley zc.zc_cookie = policy.zrp_request; 3189468c413aSTim Haley 319057f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3191468c413aSTim Haley return (-1); 3192468c413aSTim Haley 3193cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3194468c413aSTim Haley return (-1); 3195468c413aSTim Haley 31964b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 31974b964adaSGeorge Wilson errno == ENOMEM) { 31984b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 31994b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 32004b964adaSGeorge Wilson return (-1); 32014b964adaSGeorge Wilson } 32024b964adaSGeorge Wilson } 32034b964adaSGeorge Wilson 32044b964adaSGeorge Wilson if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) && 3205468c413aSTim Haley errno != EPERM && errno != EACCES)) { 3206468c413aSTim Haley if (policy.zrp_request & 3207468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3208468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3209468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 3210468c413aSTim Haley ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), 3211468c413aSTim Haley nvi); 3212468c413aSTim Haley nvlist_free(nvi); 3213468c413aSTim Haley } 3214468c413aSTim Haley zcmd_free_nvlists(&zc); 321599653d4eSeschrock return (0); 3216468c413aSTim Haley } 321799653d4eSeschrock 3218468c413aSTim Haley zcmd_free_nvlists(&zc); 321999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3220fa9e4066Sahrens } 3221fa9e4066Sahrens 32223d7072f8Seschrock /* 32233d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 32243d7072f8Seschrock */ 32253d7072f8Seschrock int 32263d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 32273d7072f8Seschrock { 32283d7072f8Seschrock zfs_cmd_t zc = { 0 }; 32293d7072f8Seschrock char msg[1024]; 32303d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 32313d7072f8Seschrock 32323d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 32333d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 32343d7072f8Seschrock guid); 32353d7072f8Seschrock 32363d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32373d7072f8Seschrock zc.zc_guid = guid; 323814f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 32393d7072f8Seschrock 32403d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 32413d7072f8Seschrock return (0); 32423d7072f8Seschrock 32433d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 32443d7072f8Seschrock } 32453d7072f8Seschrock 3246e9103aaeSGarrett D'Amore /* 3247e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3248e9103aaeSGarrett D'Amore */ 3249e9103aaeSGarrett D'Amore int 3250e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3251e9103aaeSGarrett D'Amore { 3252e9103aaeSGarrett D'Amore char msg[1024]; 3253e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3254e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3255e9103aaeSGarrett D'Amore 3256e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3257e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3258e9103aaeSGarrett D'Amore 3259e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3260e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3261e9103aaeSGarrett D'Amore return (0); 3262e9103aaeSGarrett D'Amore 3263e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3264e9103aaeSGarrett D'Amore } 3265e9103aaeSGarrett D'Amore 32664263d13fSGeorge Wilson /* 32674263d13fSGeorge Wilson * Reopen the pool. 32684263d13fSGeorge Wilson */ 32694263d13fSGeorge Wilson int 32704263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 32714263d13fSGeorge Wilson { 32724263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 32734263d13fSGeorge Wilson char msg[1024]; 32744263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 32754263d13fSGeorge Wilson 32764263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 32774263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 32784263d13fSGeorge Wilson zhp->zpool_name); 32794263d13fSGeorge Wilson 32804263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 32814263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 32824263d13fSGeorge Wilson return (0); 32834263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 32844263d13fSGeorge Wilson } 32854263d13fSGeorge Wilson 3286c67d9675Seschrock /* 3287c67d9675Seschrock * Convert from a devid string to a path. 3288c67d9675Seschrock */ 3289c67d9675Seschrock static char * 3290c67d9675Seschrock devid_to_path(char *devid_str) 3291c67d9675Seschrock { 3292c67d9675Seschrock ddi_devid_t devid; 3293c67d9675Seschrock char *minor; 3294c67d9675Seschrock char *path; 3295c67d9675Seschrock devid_nmlist_t *list = NULL; 3296c67d9675Seschrock int ret; 3297c67d9675Seschrock 3298c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3299c67d9675Seschrock return (NULL); 3300c67d9675Seschrock 3301c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3302c67d9675Seschrock 3303c67d9675Seschrock devid_str_free(minor); 3304c67d9675Seschrock devid_free(devid); 3305c67d9675Seschrock 3306c67d9675Seschrock if (ret != 0) 3307c67d9675Seschrock return (NULL); 3308c67d9675Seschrock 3309078266a5SMarcel Telka /* 3310078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3311078266a5SMarcel Telka */ 3312078266a5SMarcel Telka path = strdup(list[0].devname); 331399653d4eSeschrock 3314c67d9675Seschrock devid_free_nmlist(list); 3315c67d9675Seschrock 3316c67d9675Seschrock return (path); 3317c67d9675Seschrock } 3318c67d9675Seschrock 3319c67d9675Seschrock /* 3320c67d9675Seschrock * Convert from a path to a devid string. 3321c67d9675Seschrock */ 3322c67d9675Seschrock static char * 3323c67d9675Seschrock path_to_devid(const char *path) 3324c67d9675Seschrock { 3325c67d9675Seschrock int fd; 3326c67d9675Seschrock ddi_devid_t devid; 3327c67d9675Seschrock char *minor, *ret; 3328c67d9675Seschrock 3329c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3330c67d9675Seschrock return (NULL); 3331c67d9675Seschrock 3332c67d9675Seschrock minor = NULL; 3333c67d9675Seschrock ret = NULL; 3334c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3335c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3336c67d9675Seschrock ret = devid_str_encode(devid, minor); 3337c67d9675Seschrock if (minor != NULL) 3338c67d9675Seschrock devid_str_free(minor); 3339c67d9675Seschrock devid_free(devid); 3340c67d9675Seschrock } 3341c67d9675Seschrock (void) close(fd); 3342c67d9675Seschrock 3343c67d9675Seschrock return (ret); 3344c67d9675Seschrock } 3345c67d9675Seschrock 3346c67d9675Seschrock /* 3347c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3348c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3349c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3350c67d9675Seschrock */ 3351c67d9675Seschrock static void 3352c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3353c67d9675Seschrock { 3354c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3355c67d9675Seschrock 3356c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3357e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3358c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3359ea8dc4b6Seschrock &zc.zc_guid) == 0); 3360c67d9675Seschrock 336199653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3362c67d9675Seschrock } 3363c67d9675Seschrock 3364c67d9675Seschrock /* 3365c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3366c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3367c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3368c67d9675Seschrock * trailing 's0' slice name. 3369c67d9675Seschrock * 3370c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3371c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3372c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3373c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3374c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3375c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3376c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3377c67d9675Seschrock * of these checks. 3378c67d9675Seschrock */ 3379c67d9675Seschrock char * 338088ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 338188ecc943SGeorge Wilson boolean_t verbose) 3382c67d9675Seschrock { 3383c67d9675Seschrock char *path, *devid; 3384ea8dc4b6Seschrock uint64_t value; 3385ea8dc4b6Seschrock char buf[64]; 33863d7072f8Seschrock vdev_stat_t *vs; 33873d7072f8Seschrock uint_t vsc; 3388c67d9675Seschrock 3389ea8dc4b6Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 3390ea8dc4b6Seschrock &value) == 0) { 3391ea8dc4b6Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3392ea8dc4b6Seschrock &value) == 0); 33935ad82045Snd (void) snprintf(buf, sizeof (buf), "%llu", 33945ad82045Snd (u_longlong_t)value); 3395ea8dc4b6Seschrock path = buf; 3396ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3397c67d9675Seschrock 33983d7072f8Seschrock /* 33993d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 34003d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 34013d7072f8Seschrock * open a misbehaving device, which can have undesirable 34023d7072f8Seschrock * effects. 34033d7072f8Seschrock */ 34043f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 34053d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 34063d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 34073d7072f8Seschrock zhp != NULL && 3408c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3409c67d9675Seschrock /* 3410c67d9675Seschrock * Determine if the current path is correct. 3411c67d9675Seschrock */ 3412c67d9675Seschrock char *newdevid = path_to_devid(path); 3413c67d9675Seschrock 3414c67d9675Seschrock if (newdevid == NULL || 3415c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3416c67d9675Seschrock char *newpath; 3417c67d9675Seschrock 3418c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3419c67d9675Seschrock /* 3420c67d9675Seschrock * Update the path appropriately. 3421c67d9675Seschrock */ 3422c67d9675Seschrock set_path(zhp, nv, newpath); 342399653d4eSeschrock if (nvlist_add_string(nv, 342499653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 342599653d4eSeschrock verify(nvlist_lookup_string(nv, 342699653d4eSeschrock ZPOOL_CONFIG_PATH, 342799653d4eSeschrock &path) == 0); 3428c67d9675Seschrock free(newpath); 3429c67d9675Seschrock } 3430c67d9675Seschrock } 3431c67d9675Seschrock 343299653d4eSeschrock if (newdevid) 343399653d4eSeschrock devid_str_free(newdevid); 3434c67d9675Seschrock } 3435c67d9675Seschrock 34366401734dSWill Andrews if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) 34376401734dSWill Andrews path += strlen(ZFS_DISK_ROOTD); 3438c67d9675Seschrock 3439c67d9675Seschrock if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 3440ea8dc4b6Seschrock &value) == 0 && value) { 34413fdda499SJohn Harres int pathlen = strlen(path); 344299653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 34433fdda499SJohn Harres 34443fdda499SJohn Harres /* 34457855d95bSToomas Soome * If it starts with c#, and ends with "s0" or "s1", 34467855d95bSToomas Soome * chop the slice off, or if it ends with "s0/old" or 34477855d95bSToomas Soome * "s1/old", remove the slice from the middle. 34483fdda499SJohn Harres */ 34493fdda499SJohn Harres if (CTD_CHECK(tmp)) { 34507855d95bSToomas Soome if (strcmp(&tmp[pathlen - 2], "s0") == 0 || 34517855d95bSToomas Soome strcmp(&tmp[pathlen - 2], "s1") == 0) { 34523fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 34533fdda499SJohn Harres } else if (pathlen > 6 && 34547855d95bSToomas Soome (strcmp(&tmp[pathlen - 6], "s0/old") == 0 || 34557855d95bSToomas Soome strcmp(&tmp[pathlen - 6], "s1/old") == 0)) { 34563fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 34573fdda499SJohn Harres "/old"); 34583fdda499SJohn Harres } 34593fdda499SJohn Harres } 3460c67d9675Seschrock return (tmp); 3461c67d9675Seschrock } 3462c67d9675Seschrock } else { 3463c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 346499653d4eSeschrock 346599653d4eSeschrock /* 346699653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 346799653d4eSeschrock */ 346899653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 346999653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 347099653d4eSeschrock &value) == 0); 347199653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 34725ad82045Snd (u_longlong_t)value); 347399653d4eSeschrock path = buf; 347499653d4eSeschrock } 347588ecc943SGeorge Wilson 347688ecc943SGeorge Wilson /* 347788ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 347888ecc943SGeorge Wilson * naming convention. 347988ecc943SGeorge Wilson */ 348088ecc943SGeorge Wilson if (verbose) { 348188ecc943SGeorge Wilson uint64_t id; 348288ecc943SGeorge Wilson 348388ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 348488ecc943SGeorge Wilson &id) == 0); 348588ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 348688ecc943SGeorge Wilson (u_longlong_t)id); 348788ecc943SGeorge Wilson path = buf; 348888ecc943SGeorge Wilson } 3489c67d9675Seschrock } 3490c67d9675Seschrock 349199653d4eSeschrock return (zfs_strdup(hdl, path)); 3492c67d9675Seschrock } 3493ea8dc4b6Seschrock 3494ea8dc4b6Seschrock static int 3495a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3496ea8dc4b6Seschrock { 34977802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3498ea8dc4b6Seschrock } 3499ea8dc4b6Seschrock 3500ea8dc4b6Seschrock /* 3501ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3502ea8dc4b6Seschrock * caller. 3503ea8dc4b6Seschrock */ 3504ea8dc4b6Seschrock int 350555434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3506ea8dc4b6Seschrock { 3507ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3508ea8dc4b6Seschrock uint64_t count; 35097802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 351055434c77Sek int i; 3511ea8dc4b6Seschrock 3512ea8dc4b6Seschrock /* 3513ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3514ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3515ea8dc4b6Seschrock * entire list. 3516ea8dc4b6Seschrock */ 3517ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3518ea8dc4b6Seschrock &count) == 0); 351975519f38Sek if (count == 0) 352075519f38Sek return (0); 3521e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 35227802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 352399653d4eSeschrock return (-1); 3524e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3525ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3526ea8dc4b6Seschrock for (;;) { 352799653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 352899653d4eSeschrock &zc) != 0) { 3529e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3530ea8dc4b6Seschrock if (errno == ENOMEM) { 35317802d7bfSMatthew Ahrens void *dst; 35327802d7bfSMatthew Ahrens 3533bf561db0Svb count = zc.zc_nvlist_dst_size; 35347802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 35357802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 35367802d7bfSMatthew Ahrens if (dst == NULL) 353799653d4eSeschrock return (-1); 35387802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3539ea8dc4b6Seschrock } else { 3540ea8dc4b6Seschrock return (-1); 3541ea8dc4b6Seschrock } 3542ea8dc4b6Seschrock } else { 3543ea8dc4b6Seschrock break; 3544ea8dc4b6Seschrock } 3545ea8dc4b6Seschrock } 3546ea8dc4b6Seschrock 3547ea8dc4b6Seschrock /* 3548ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3549ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3550e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3551ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3552ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3553ea8dc4b6Seschrock */ 35547802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3555e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3556e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3557ea8dc4b6Seschrock 3558a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3559ea8dc4b6Seschrock 356055434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3561ea8dc4b6Seschrock 3562ea8dc4b6Seschrock /* 356355434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3564ea8dc4b6Seschrock */ 3565ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3566ea8dc4b6Seschrock nvlist_t *nv; 3567ea8dc4b6Seschrock 3568c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3569c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3570c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3571ea8dc4b6Seschrock continue; 3572ea8dc4b6Seschrock 357355434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 357455434c77Sek goto nomem; 357555434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 357655434c77Sek zb[i].zb_objset) != 0) { 357755434c77Sek nvlist_free(nv); 357899653d4eSeschrock goto nomem; 3579ea8dc4b6Seschrock } 358055434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 358155434c77Sek zb[i].zb_object) != 0) { 358255434c77Sek nvlist_free(nv); 358355434c77Sek goto nomem; 358455434c77Sek } 358555434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 358655434c77Sek nvlist_free(nv); 358755434c77Sek goto nomem; 358855434c77Sek } 358955434c77Sek nvlist_free(nv); 3590ea8dc4b6Seschrock } 3591ea8dc4b6Seschrock 35923ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3593ea8dc4b6Seschrock return (0); 359499653d4eSeschrock 359599653d4eSeschrock nomem: 3596e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 359799653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3598ea8dc4b6Seschrock } 3599eaca9bbdSeschrock 3600eaca9bbdSeschrock /* 3601eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3602eaca9bbdSeschrock */ 3603eaca9bbdSeschrock int 3604990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3605eaca9bbdSeschrock { 3606eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 360799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3608eaca9bbdSeschrock 3609eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3610990b4856Slling zc.zc_cookie = new_version; 3611990b4856Slling 3612ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3613ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 361499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 361599653d4eSeschrock zhp->zpool_name)); 3616eaca9bbdSeschrock return (0); 3617eaca9bbdSeschrock } 361806eeb2adSek 361906eeb2adSek void 36204445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 362106eeb2adSek { 36224445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 36234445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 36244445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 36254445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 36262a6b87f0Sek } 36272a6b87f0Sek } 36282a6b87f0Sek 36292a6b87f0Sek int 36304445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 36312a6b87f0Sek { 36324445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 36334445fffbSMatthew Ahrens nvlist_t *args; 36344445fffbSMatthew Ahrens int err; 36354445fffbSMatthew Ahrens 36364445fffbSMatthew Ahrens args = fnvlist_alloc(); 36374445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 36384445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 36394445fffbSMatthew Ahrens if (err == 0) 36404445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 36414445fffbSMatthew Ahrens nvlist_free(args); 36424445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 36434445fffbSMatthew Ahrens return (err); 364406eeb2adSek } 364506eeb2adSek 364606eeb2adSek /* 364706eeb2adSek * Perform ioctl to get some command history of a pool. 364806eeb2adSek * 364906eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 365006eeb2adSek * logical offset of the history buffer to start reading from. 365106eeb2adSek * 365206eeb2adSek * Upon return, 'off' is the next logical offset to read from and 365306eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 365406eeb2adSek */ 365506eeb2adSek static int 365606eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 365706eeb2adSek { 365806eeb2adSek zfs_cmd_t zc = { 0 }; 365906eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 366006eeb2adSek 366106eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 366206eeb2adSek 366306eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 366406eeb2adSek zc.zc_history_len = *len; 366506eeb2adSek zc.zc_history_offset = *off; 366606eeb2adSek 366706eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 366806eeb2adSek switch (errno) { 366906eeb2adSek case EPERM: 3670ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 3671ece3d9b3Slling dgettext(TEXT_DOMAIN, 367206eeb2adSek "cannot show history for pool '%s'"), 367306eeb2adSek zhp->zpool_name)); 367406eeb2adSek case ENOENT: 3675ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 367606eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 367706eeb2adSek "'%s'"), zhp->zpool_name)); 3678d7306b64Sek case ENOTSUP: 3679d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 3680d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 3681d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 368206eeb2adSek default: 3683ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 368406eeb2adSek dgettext(TEXT_DOMAIN, 368506eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 368606eeb2adSek } 368706eeb2adSek } 368806eeb2adSek 368906eeb2adSek *len = zc.zc_history_len; 369006eeb2adSek *off = zc.zc_history_offset; 369106eeb2adSek 369206eeb2adSek return (0); 369306eeb2adSek } 369406eeb2adSek 369506eeb2adSek /* 369606eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 369706eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 369806eeb2adSek * processed as there wasn't a complete record. 369906eeb2adSek */ 37008f18d1faSGeorge Wilson int 370106eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 370206eeb2adSek nvlist_t ***records, uint_t *numrecords) 370306eeb2adSek { 370406eeb2adSek uint64_t reclen; 370506eeb2adSek nvlist_t *nv; 370606eeb2adSek int i; 370706eeb2adSek 370806eeb2adSek while (bytes_read > sizeof (reclen)) { 370906eeb2adSek 371006eeb2adSek /* get length of packed record (stored as little endian) */ 371106eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 371206eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 371306eeb2adSek 371406eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 371506eeb2adSek break; 371606eeb2adSek 371706eeb2adSek /* unpack record */ 371806eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 371906eeb2adSek return (ENOMEM); 372006eeb2adSek bytes_read -= sizeof (reclen) + reclen; 372106eeb2adSek buf += sizeof (reclen) + reclen; 372206eeb2adSek 372306eeb2adSek /* add record to nvlist array */ 372406eeb2adSek (*numrecords)++; 372506eeb2adSek if (ISP2(*numrecords + 1)) { 372606eeb2adSek *records = realloc(*records, 372706eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 372806eeb2adSek } 372906eeb2adSek (*records)[*numrecords - 1] = nv; 373006eeb2adSek } 373106eeb2adSek 373206eeb2adSek *leftover = bytes_read; 373306eeb2adSek return (0); 373406eeb2adSek } 373506eeb2adSek 373606eeb2adSek /* 373706eeb2adSek * Retrieve the command history of a pool. 373806eeb2adSek */ 373906eeb2adSek int 374006eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 374106eeb2adSek { 37423339867aSMatthew Ahrens char *buf; 37433339867aSMatthew Ahrens int buflen = 128 * 1024; 374406eeb2adSek uint64_t off = 0; 374506eeb2adSek nvlist_t **records = NULL; 374606eeb2adSek uint_t numrecords = 0; 374706eeb2adSek int err, i; 374806eeb2adSek 37493339867aSMatthew Ahrens buf = malloc(buflen); 37503339867aSMatthew Ahrens if (buf == NULL) 37513339867aSMatthew Ahrens return (ENOMEM); 375206eeb2adSek do { 37533339867aSMatthew Ahrens uint64_t bytes_read = buflen; 375406eeb2adSek uint64_t leftover; 375506eeb2adSek 375606eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 375706eeb2adSek break; 375806eeb2adSek 375906eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 376006eeb2adSek if (!bytes_read) 376106eeb2adSek break; 376206eeb2adSek 376306eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 376406eeb2adSek &leftover, &records, &numrecords)) != 0) 376506eeb2adSek break; 376606eeb2adSek off -= leftover; 37673339867aSMatthew Ahrens if (leftover == bytes_read) { 37683339867aSMatthew Ahrens /* 37693339867aSMatthew Ahrens * no progress made, because buffer is not big enough 37703339867aSMatthew Ahrens * to hold this record; resize and retry. 37713339867aSMatthew Ahrens */ 37723339867aSMatthew Ahrens buflen *= 2; 37733339867aSMatthew Ahrens free(buf); 37743339867aSMatthew Ahrens buf = malloc(buflen); 37753339867aSMatthew Ahrens if (buf == NULL) 37763339867aSMatthew Ahrens return (ENOMEM); 37773339867aSMatthew Ahrens } 377806eeb2adSek 377906eeb2adSek /* CONSTCOND */ 378006eeb2adSek } while (1); 378106eeb2adSek 37823339867aSMatthew Ahrens free(buf); 37833339867aSMatthew Ahrens 378406eeb2adSek if (!err) { 378506eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 378606eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 378706eeb2adSek records, numrecords) == 0); 378806eeb2adSek } 378906eeb2adSek for (i = 0; i < numrecords; i++) 379006eeb2adSek nvlist_free(records[i]); 379106eeb2adSek free(records); 379206eeb2adSek 379306eeb2adSek return (err); 379406eeb2adSek } 379555434c77Sek 379655434c77Sek void 379755434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 379855434c77Sek char *pathname, size_t len) 379955434c77Sek { 380055434c77Sek zfs_cmd_t zc = { 0 }; 380155434c77Sek boolean_t mounted = B_FALSE; 380255434c77Sek char *mntpnt = NULL; 38039adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 380455434c77Sek 380555434c77Sek if (dsobj == 0) { 380655434c77Sek /* special case for the MOS */ 380755434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 380855434c77Sek return; 380955434c77Sek } 381055434c77Sek 381155434c77Sek /* get the dataset's name */ 381255434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 381355434c77Sek zc.zc_obj = dsobj; 381455434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 381555434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 381655434c77Sek /* just write out a path of two object numbers */ 381755434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 381855434c77Sek dsobj, obj); 381955434c77Sek return; 382055434c77Sek } 382155434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 382255434c77Sek 382355434c77Sek /* find out if the dataset is mounted */ 382455434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 382555434c77Sek 382655434c77Sek /* get the corrupted object's path */ 382755434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 382855434c77Sek zc.zc_obj = obj; 382955434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 383055434c77Sek &zc) == 0) { 383155434c77Sek if (mounted) { 383255434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 383355434c77Sek zc.zc_value); 383455434c77Sek } else { 383555434c77Sek (void) snprintf(pathname, len, "%s:%s", 383655434c77Sek dsname, zc.zc_value); 383755434c77Sek } 383855434c77Sek } else { 383955434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 384055434c77Sek } 384155434c77Sek free(mntpnt); 384255434c77Sek } 3843b1b8ab34Slling 384415e6edf1Sgw /* 384515e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 384615e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 384715e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 38487855d95bSToomas Soome * partition. If the caller has passed a non-NULL boolean argument, then 38497855d95bSToomas Soome * we set it to indicate if the disk does have efi system partition. 385015e6edf1Sgw */ 385115e6edf1Sgw static int 38527855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system) 385315e6edf1Sgw { 385415e6edf1Sgw char *path; 385515e6edf1Sgw int fd; 385615e6edf1Sgw char diskname[MAXPATHLEN]; 38577855d95bSToomas Soome boolean_t boot = B_FALSE; 385815e6edf1Sgw int err = -1; 38597855d95bSToomas Soome int slice; 386015e6edf1Sgw 386115e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 386215e6edf1Sgw return (err); 386315e6edf1Sgw 38646401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT, 386515e6edf1Sgw strrchr(path, '/')); 386615e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 386715e6edf1Sgw struct dk_gpt *vtoc; 386815e6edf1Sgw 386915e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 38707855d95bSToomas Soome for (slice = 0; slice < vtoc->efi_nparts; slice++) { 38717855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_SYSTEM) 38727855d95bSToomas Soome boot = B_TRUE; 38737855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_USR) 38747855d95bSToomas Soome break; 38757855d95bSToomas Soome } 38767855d95bSToomas Soome if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR) 38777855d95bSToomas Soome *sb = vtoc->efi_parts[slice].p_start; 38787855d95bSToomas Soome if (system != NULL) 38797855d95bSToomas Soome *system = boot; 388015e6edf1Sgw efi_free(vtoc); 388115e6edf1Sgw } 388215e6edf1Sgw (void) close(fd); 388315e6edf1Sgw } 388415e6edf1Sgw return (err); 388515e6edf1Sgw } 388615e6edf1Sgw 38878488aeb5Staylor /* 38888488aeb5Staylor * determine where a partition starts on a disk in the current 38898488aeb5Staylor * configuration 38908488aeb5Staylor */ 38918488aeb5Staylor static diskaddr_t 38928488aeb5Staylor find_start_block(nvlist_t *config) 38938488aeb5Staylor { 38948488aeb5Staylor nvlist_t **child; 38958488aeb5Staylor uint_t c, children; 38968488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 38978488aeb5Staylor uint64_t wholedisk; 38988488aeb5Staylor 38998488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 39008488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 39018488aeb5Staylor if (nvlist_lookup_uint64(config, 39028488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 39038488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 39048488aeb5Staylor return (MAXOFFSET_T); 39058488aeb5Staylor } 39067855d95bSToomas Soome if (read_efi_label(config, &sb, NULL) < 0) 390715e6edf1Sgw sb = MAXOFFSET_T; 39088488aeb5Staylor return (sb); 39098488aeb5Staylor } 39108488aeb5Staylor 39118488aeb5Staylor for (c = 0; c < children; c++) { 39128488aeb5Staylor sb = find_start_block(child[c]); 39138488aeb5Staylor if (sb != MAXOFFSET_T) { 39148488aeb5Staylor return (sb); 39158488aeb5Staylor } 39168488aeb5Staylor } 39178488aeb5Staylor return (MAXOFFSET_T); 39188488aeb5Staylor } 39198488aeb5Staylor 39208488aeb5Staylor /* 39218488aeb5Staylor * Label an individual disk. The name provided is the short name, 39228488aeb5Staylor * stripped of any leading /dev path. 39238488aeb5Staylor */ 39248488aeb5Staylor int 39257855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name, 39267855d95bSToomas Soome zpool_boot_label_t boot_type, uint64_t boot_size, int *slice) 39278488aeb5Staylor { 39288488aeb5Staylor char path[MAXPATHLEN]; 39298488aeb5Staylor struct dk_gpt *vtoc; 39308488aeb5Staylor int fd; 39318488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 39328488aeb5Staylor uint64_t slice_size; 39338488aeb5Staylor diskaddr_t start_block; 39348488aeb5Staylor char errbuf[1024]; 39358488aeb5Staylor 3936c6ef114fSmmusante /* prepare an error message just in case */ 3937c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 3938c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 3939c6ef114fSmmusante 39408488aeb5Staylor if (zhp) { 39418488aeb5Staylor nvlist_t *nvroot; 39428488aeb5Staylor 39438488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 39448488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 39458488aeb5Staylor 39468488aeb5Staylor if (zhp->zpool_start_block == 0) 39478488aeb5Staylor start_block = find_start_block(nvroot); 39488488aeb5Staylor else 39498488aeb5Staylor start_block = zhp->zpool_start_block; 39508488aeb5Staylor zhp->zpool_start_block = start_block; 39518488aeb5Staylor } else { 39528488aeb5Staylor /* new pool */ 39538488aeb5Staylor start_block = NEW_START_BLOCK; 39548488aeb5Staylor } 39558488aeb5Staylor 39566401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name, 39578488aeb5Staylor BACKUP_SLICE); 39588488aeb5Staylor 39598488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 39608488aeb5Staylor /* 39618488aeb5Staylor * This shouldn't happen. We've long since verified that this 39628488aeb5Staylor * is a valid device. 39638488aeb5Staylor */ 3964c6ef114fSmmusante zfs_error_aux(hdl, 3965c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 39668488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 39678488aeb5Staylor } 39688488aeb5Staylor 39698488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 39708488aeb5Staylor /* 39718488aeb5Staylor * The only way this can fail is if we run out of memory, or we 39728488aeb5Staylor * were unable to read the disk's capacity 39738488aeb5Staylor */ 39748488aeb5Staylor if (errno == ENOMEM) 39758488aeb5Staylor (void) no_memory(hdl); 39768488aeb5Staylor 39778488aeb5Staylor (void) close(fd); 3978c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3979c6ef114fSmmusante "unable to read disk capacity"), name); 39808488aeb5Staylor 39818488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 39828488aeb5Staylor } 39838488aeb5Staylor 39848488aeb5Staylor /* 39858488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 39868488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 39878488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 39888488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 39898488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 39908488aeb5Staylor * can get, in the absence of V_OTHER. 39918488aeb5Staylor */ 39927855d95bSToomas Soome /* first fix the partition start block */ 39937855d95bSToomas Soome if (start_block == MAXOFFSET_T) 39947855d95bSToomas Soome start_block = NEW_START_BLOCK; 39958488aeb5Staylor 39967855d95bSToomas Soome /* 39977855d95bSToomas Soome * EFI System partition is using slice 0. 39987855d95bSToomas Soome * ZFS is on slice 1 and slice 8 is reserved. 39997855d95bSToomas Soome * We assume the GPT partition table without system 40007855d95bSToomas Soome * partition has zfs p_start == NEW_START_BLOCK. 40017855d95bSToomas Soome * If start_block != NEW_START_BLOCK, it means we have 40027855d95bSToomas Soome * system partition. Correct solution would be to query/cache vtoc 40037855d95bSToomas Soome * from existing vdev member. 40047855d95bSToomas Soome */ 40057855d95bSToomas Soome if (boot_type == ZPOOL_CREATE_BOOT_LABEL) { 40067855d95bSToomas Soome if (boot_size % vtoc->efi_lbasize != 0) { 40077855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40087855d95bSToomas Soome "boot partition size must be a multiple of %d"), 40097855d95bSToomas Soome vtoc->efi_lbasize); 40107855d95bSToomas Soome (void) close(fd); 40117855d95bSToomas Soome efi_free(vtoc); 40127855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 40137855d95bSToomas Soome } 40147855d95bSToomas Soome /* 40157855d95bSToomas Soome * System partition size checks. 40167855d95bSToomas Soome * Note the 1MB is quite arbitrary value, since we 40177855d95bSToomas Soome * are creating dedicated pool, it should be enough 40187855d95bSToomas Soome * to hold fat + efi bootloader. May need to be 40197855d95bSToomas Soome * adjusted if the bootloader size will grow. 40207855d95bSToomas Soome */ 40217855d95bSToomas Soome if (boot_size < 1024 * 1024) { 40227855d95bSToomas Soome char buf[64]; 40237855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 40247855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40257855d95bSToomas Soome "Specified size %s for EFI System partition is too " 40267855d95bSToomas Soome "small, the minimum size is 1MB."), buf); 40277855d95bSToomas Soome (void) close(fd); 40287855d95bSToomas Soome efi_free(vtoc); 40297855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 40307855d95bSToomas Soome } 40317855d95bSToomas Soome /* 33MB is tested with mkfs -F pcfs */ 40327855d95bSToomas Soome if (hdl->libzfs_printerr && 40337855d95bSToomas Soome ((vtoc->efi_lbasize == 512 && 40347855d95bSToomas Soome boot_size < 33 * 1024 * 1024) || 40357855d95bSToomas Soome (vtoc->efi_lbasize == 4096 && 40367855d95bSToomas Soome boot_size < 256 * 1024 * 1024))) { 40377855d95bSToomas Soome char buf[64]; 40387855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 40397855d95bSToomas Soome (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 40407855d95bSToomas Soome "Warning: EFI System partition size %s is " 40417855d95bSToomas Soome "not allowing to create FAT32 file\nsystem, which " 40427855d95bSToomas Soome "may result in unbootable system.\n"), buf); 40437855d95bSToomas Soome } 40447855d95bSToomas Soome /* Adjust zfs partition start by size of system partition. */ 40457855d95bSToomas Soome start_block += boot_size / vtoc->efi_lbasize; 40467855d95bSToomas Soome } 40477855d95bSToomas Soome 40487855d95bSToomas Soome if (start_block == NEW_START_BLOCK) { 40497855d95bSToomas Soome /* 40507855d95bSToomas Soome * Use default layout. 40517855d95bSToomas Soome * ZFS is on slice 0 and slice 8 is reserved. 40527855d95bSToomas Soome */ 40537855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1; 40547855d95bSToomas Soome slice_size -= EFI_MIN_RESV_SIZE; 40557855d95bSToomas Soome slice_size -= start_block; 40567855d95bSToomas Soome if (slice != NULL) 40577855d95bSToomas Soome *slice = 0; 40587855d95bSToomas Soome 40597855d95bSToomas Soome vtoc->efi_parts[0].p_start = start_block; 40607855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 40617855d95bSToomas Soome 40627855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_USR; 40637855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 40647855d95bSToomas Soome 40657855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 40667855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 40677855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 40687855d95bSToomas Soome } else { 40697855d95bSToomas Soome slice_size = start_block - NEW_START_BLOCK; 40707855d95bSToomas Soome vtoc->efi_parts[0].p_start = NEW_START_BLOCK; 40717855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 40727855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_SYSTEM; 40737855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "loader"); 40747855d95bSToomas Soome if (slice != NULL) 40757855d95bSToomas Soome *slice = 1; 40767855d95bSToomas Soome /* prepare slice 1 */ 40777855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1 - slice_size; 40787855d95bSToomas Soome slice_size -= resv; 40797855d95bSToomas Soome slice_size -= NEW_START_BLOCK; 40807855d95bSToomas Soome vtoc->efi_parts[1].p_start = start_block; 40817855d95bSToomas Soome vtoc->efi_parts[1].p_size = slice_size; 40827855d95bSToomas Soome vtoc->efi_parts[1].p_tag = V_USR; 40837855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[1].p_name, "zfs"); 40847855d95bSToomas Soome 40857855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 40867855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 40877855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 40887855d95bSToomas Soome } 40898488aeb5Staylor 40908488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 40918488aeb5Staylor /* 40928488aeb5Staylor * Some block drivers (like pcata) may not support EFI 40938488aeb5Staylor * GPT labels. Print out a helpful error message dir- 40948488aeb5Staylor * ecting the user to manually label the disk and give 40958488aeb5Staylor * a specific slice. 40968488aeb5Staylor */ 40978488aeb5Staylor (void) close(fd); 40988488aeb5Staylor efi_free(vtoc); 40998488aeb5Staylor 41008488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4101c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 41028488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 41038488aeb5Staylor } 41048488aeb5Staylor 41058488aeb5Staylor (void) close(fd); 41068488aeb5Staylor efi_free(vtoc); 41078488aeb5Staylor return (0); 41088488aeb5Staylor } 4109e7cbe64fSgw 4110e7cbe64fSgw static boolean_t 4111e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 4112e7cbe64fSgw { 4113e7cbe64fSgw char *type; 4114e7cbe64fSgw nvlist_t **child; 4115e7cbe64fSgw uint_t children, c; 4116e7cbe64fSgw 4117e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 4118810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 411988ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 4120e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 4121e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4122e7cbe64fSgw "vdev type '%s' is not supported"), type); 4123e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 4124e7cbe64fSgw return (B_FALSE); 4125e7cbe64fSgw } 4126e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 4127e7cbe64fSgw &child, &children) == 0) { 4128e7cbe64fSgw for (c = 0; c < children; c++) { 4129e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 4130e7cbe64fSgw return (B_FALSE); 4131e7cbe64fSgw } 4132e7cbe64fSgw } 4133e7cbe64fSgw return (B_TRUE); 4134e7cbe64fSgw } 4135e7cbe64fSgw 4136e7cbe64fSgw /* 4137810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 4138810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 4139810e43b2SBill Pijewski * 4140810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 4141810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 4142810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 4143e7cbe64fSgw */ 4144e7cbe64fSgw int 4145e7cbe64fSgw zvol_check_dump_config(char *arg) 4146e7cbe64fSgw { 4147e7cbe64fSgw zpool_handle_t *zhp = NULL; 4148e7cbe64fSgw nvlist_t *config, *nvroot; 4149e7cbe64fSgw char *p, *volname; 4150e7cbe64fSgw nvlist_t **top; 4151e7cbe64fSgw uint_t toplevels; 4152e7cbe64fSgw libzfs_handle_t *hdl; 4153e7cbe64fSgw char errbuf[1024]; 41549adfa60dSMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN]; 4155e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 4156e7cbe64fSgw int ret = 1; 4157e7cbe64fSgw 4158e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 4159e7cbe64fSgw return (-1); 4160e7cbe64fSgw } 4161e7cbe64fSgw 4162e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4163e7cbe64fSgw "dump is not supported on device '%s'"), arg); 4164e7cbe64fSgw 4165e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 4166e7cbe64fSgw return (1); 4167e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 4168e7cbe64fSgw 4169e7cbe64fSgw volname = arg + pathlen; 4170e7cbe64fSgw 4171e7cbe64fSgw /* check the configuration of the pool */ 4172e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 4173e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4174e7cbe64fSgw "malformed dataset name")); 4175e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4176e7cbe64fSgw return (1); 41779adfa60dSMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) { 4178e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4179e7cbe64fSgw "dataset name is too long")); 4180e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4181e7cbe64fSgw return (1); 4182e7cbe64fSgw } else { 4183e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 4184e7cbe64fSgw poolname[p - volname] = '\0'; 4185e7cbe64fSgw } 4186e7cbe64fSgw 4187e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4188e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4189e7cbe64fSgw "could not open pool '%s'"), poolname); 4190e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4191e7cbe64fSgw goto out; 4192e7cbe64fSgw } 4193e7cbe64fSgw config = zpool_get_config(zhp, NULL); 4194e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4195e7cbe64fSgw &nvroot) != 0) { 4196e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4197e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 4198e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4199e7cbe64fSgw goto out; 4200e7cbe64fSgw } 4201e7cbe64fSgw 4202e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4203e7cbe64fSgw &top, &toplevels) == 0); 4204e7cbe64fSgw 4205e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4206e7cbe64fSgw goto out; 4207e7cbe64fSgw } 4208e7cbe64fSgw ret = 0; 4209e7cbe64fSgw 4210e7cbe64fSgw out: 4211e7cbe64fSgw if (zhp) 4212e7cbe64fSgw zpool_close(zhp); 4213e7cbe64fSgw libzfs_fini(hdl); 4214e7cbe64fSgw return (ret); 4215e7cbe64fSgw } 4216