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. 245cabbc6bSPrashanth Sreenivasa * Copyright (c) 2011, 2017 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> 281702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc. 29*663207adSDon Brady * Copyright (c) 2017, Intel Corporation. 30fa9e4066Sahrens */ 31fa9e4066Sahrens 32fa9e4066Sahrens #include <ctype.h> 33fa9e4066Sahrens #include <errno.h> 34fa9e4066Sahrens #include <devid.h> 35fa9e4066Sahrens #include <fcntl.h> 36fa9e4066Sahrens #include <libintl.h> 37fa9e4066Sahrens #include <stdio.h> 38fa9e4066Sahrens #include <stdlib.h> 39f3861e1aSahl #include <strings.h> 40fa9e4066Sahrens #include <unistd.h> 414445fffbSMatthew Ahrens #include <libgen.h> 428488aeb5Staylor #include <sys/efi_partition.h> 438488aeb5Staylor #include <sys/vtoc.h> 44fa9e4066Sahrens #include <sys/zfs_ioctl.h> 45573ca77eSGeorge Wilson #include <dlfcn.h> 46fa9e4066Sahrens 47fa9e4066Sahrens #include "zfs_namecheck.h" 48b1b8ab34Slling #include "zfs_prop.h" 49fa9e4066Sahrens #include "libzfs_impl.h" 50468c413aSTim Haley #include "zfs_comutil.h" 51ad135b5dSChristopher Siden #include "zfeature_common.h" 52fa9e4066Sahrens 537855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *); 542ba5f978SAlan Somers static boolean_t zpool_vdev_is_interior(const char *name); 55990b4856Slling 56573ca77eSGeorge Wilson #define BACKUP_SLICE "s2" 57573ca77eSGeorge Wilson 58f9af39baSGeorge Wilson typedef struct prop_flags { 59f9af39baSGeorge Wilson int create:1; /* Validate property on creation */ 60f9af39baSGeorge Wilson int import:1; /* Validate property on import */ 61f9af39baSGeorge Wilson } prop_flags_t; 62f9af39baSGeorge Wilson 63990b4856Slling /* 64990b4856Slling * ==================================================================== 65990b4856Slling * zpool property functions 66990b4856Slling * ==================================================================== 67990b4856Slling */ 68990b4856Slling 69990b4856Slling static int 70990b4856Slling zpool_get_all_props(zpool_handle_t *zhp) 71990b4856Slling { 72990b4856Slling zfs_cmd_t zc = { 0 }; 73990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 74990b4856Slling 75990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 76990b4856Slling 77990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 78990b4856Slling return (-1); 79990b4856Slling 80990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) { 81990b4856Slling if (errno == ENOMEM) { 82990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 83990b4856Slling zcmd_free_nvlists(&zc); 84990b4856Slling return (-1); 85990b4856Slling } 86990b4856Slling } else { 87990b4856Slling zcmd_free_nvlists(&zc); 88990b4856Slling return (-1); 89990b4856Slling } 90990b4856Slling } 91990b4856Slling 92990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) { 93990b4856Slling zcmd_free_nvlists(&zc); 94990b4856Slling return (-1); 95990b4856Slling } 96990b4856Slling 97990b4856Slling zcmd_free_nvlists(&zc); 98990b4856Slling 99990b4856Slling return (0); 100990b4856Slling } 101990b4856Slling 102990b4856Slling static int 103990b4856Slling zpool_props_refresh(zpool_handle_t *zhp) 104990b4856Slling { 105990b4856Slling nvlist_t *old_props; 106990b4856Slling 107990b4856Slling old_props = zhp->zpool_props; 108990b4856Slling 109990b4856Slling if (zpool_get_all_props(zhp) != 0) 110990b4856Slling return (-1); 111990b4856Slling 112990b4856Slling nvlist_free(old_props); 113990b4856Slling return (0); 114990b4856Slling } 115990b4856Slling 116990b4856Slling static char * 117990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop, 118990b4856Slling zprop_source_t *src) 119990b4856Slling { 120990b4856Slling nvlist_t *nv, *nvl; 121990b4856Slling uint64_t ival; 122990b4856Slling char *value; 123990b4856Slling zprop_source_t source; 124990b4856Slling 125990b4856Slling nvl = zhp->zpool_props; 126990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 127990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0); 128990b4856Slling source = ival; 129990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 130990b4856Slling } else { 131990b4856Slling source = ZPROP_SRC_DEFAULT; 132990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL) 133990b4856Slling value = "-"; 134990b4856Slling } 135990b4856Slling 136990b4856Slling if (src) 137990b4856Slling *src = source; 138990b4856Slling 139990b4856Slling return (value); 140990b4856Slling } 141990b4856Slling 142990b4856Slling uint64_t 143990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src) 144990b4856Slling { 145990b4856Slling nvlist_t *nv, *nvl; 146990b4856Slling uint64_t value; 147990b4856Slling zprop_source_t source; 148990b4856Slling 149b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) { 150b87f3af3Sperrin /* 151b87f3af3Sperrin * zpool_get_all_props() has most likely failed because 152b87f3af3Sperrin * the pool is faulted, but if all we need is the top level 153b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist. 154b87f3af3Sperrin */ 155b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) && 156b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config, 157b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) && 158b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value) 159b87f3af3Sperrin == 0)) { 160b87f3af3Sperrin return (value); 161b87f3af3Sperrin } 162990b4856Slling return (zpool_prop_default_numeric(prop)); 163b87f3af3Sperrin } 164990b4856Slling 165990b4856Slling nvl = zhp->zpool_props; 166990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) { 167990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0); 168990b4856Slling source = value; 169990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 170990b4856Slling } else { 171990b4856Slling source = ZPROP_SRC_DEFAULT; 172990b4856Slling value = zpool_prop_default_numeric(prop); 173990b4856Slling } 174990b4856Slling 175990b4856Slling if (src) 176990b4856Slling *src = source; 177990b4856Slling 178990b4856Slling return (value); 179990b4856Slling } 180990b4856Slling 181990b4856Slling /* 182990b4856Slling * Map VDEV STATE to printed strings. 183990b4856Slling */ 1846401734dSWill Andrews const char * 185990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux) 186990b4856Slling { 187990b4856Slling switch (state) { 188990b4856Slling case VDEV_STATE_CLOSED: 189990b4856Slling case VDEV_STATE_OFFLINE: 190990b4856Slling return (gettext("OFFLINE")); 191990b4856Slling case VDEV_STATE_REMOVED: 192990b4856Slling return (gettext("REMOVED")); 193990b4856Slling case VDEV_STATE_CANT_OPEN: 194b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 195990b4856Slling return (gettext("FAULTED")); 1961195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL) 1971195e687SMark J Musante return (gettext("SPLIT")); 198990b4856Slling else 199990b4856Slling return (gettext("UNAVAIL")); 200990b4856Slling case VDEV_STATE_FAULTED: 201990b4856Slling return (gettext("FAULTED")); 202990b4856Slling case VDEV_STATE_DEGRADED: 203990b4856Slling return (gettext("DEGRADED")); 204990b4856Slling case VDEV_STATE_HEALTHY: 205990b4856Slling return (gettext("ONLINE")); 20688f61deeSIgor Kozhukhov 20788f61deeSIgor Kozhukhov default: 20888f61deeSIgor Kozhukhov break; 209990b4856Slling } 210990b4856Slling 211990b4856Slling return (gettext("UNKNOWN")); 212990b4856Slling } 213990b4856Slling 2146401734dSWill Andrews /* 2156401734dSWill Andrews * Map POOL STATE to printed strings. 2166401734dSWill Andrews */ 2176401734dSWill Andrews const char * 2186401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state) 2196401734dSWill Andrews { 2206401734dSWill Andrews switch (state) { 2216401734dSWill Andrews case POOL_STATE_ACTIVE: 2226401734dSWill Andrews return (gettext("ACTIVE")); 2236401734dSWill Andrews case POOL_STATE_EXPORTED: 2246401734dSWill Andrews return (gettext("EXPORTED")); 2256401734dSWill Andrews case POOL_STATE_DESTROYED: 2266401734dSWill Andrews return (gettext("DESTROYED")); 2276401734dSWill Andrews case POOL_STATE_SPARE: 2286401734dSWill Andrews return (gettext("SPARE")); 2296401734dSWill Andrews case POOL_STATE_L2CACHE: 2306401734dSWill Andrews return (gettext("L2CACHE")); 2316401734dSWill Andrews case POOL_STATE_UNINITIALIZED: 2326401734dSWill Andrews return (gettext("UNINITIALIZED")); 2336401734dSWill Andrews case POOL_STATE_UNAVAIL: 2346401734dSWill Andrews return (gettext("UNAVAIL")); 2356401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE: 2366401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE")); 2376401734dSWill Andrews } 2386401734dSWill Andrews 2396401734dSWill Andrews return (gettext("UNKNOWN")); 2406401734dSWill Andrews } 2416401734dSWill Andrews 242990b4856Slling /* 243990b4856Slling * Get a zpool property value for 'prop' and return the value in 244990b4856Slling * a pre-allocated buffer. 245990b4856Slling */ 246990b4856Slling int 247990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len, 248c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal) 249990b4856Slling { 250990b4856Slling uint64_t intval; 251990b4856Slling const char *strval; 252990b4856Slling zprop_source_t src = ZPROP_SRC_NONE; 253990b4856Slling nvlist_t *nvroot; 254990b4856Slling vdev_stat_t *vs; 255990b4856Slling uint_t vsc; 256990b4856Slling 257990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) { 258379c004dSEric Schrock switch (prop) { 259379c004dSEric Schrock case ZPOOL_PROP_NAME: 260990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len); 261379c004dSEric Schrock break; 262379c004dSEric Schrock 263379c004dSEric Schrock case ZPOOL_PROP_HEALTH: 264990b4856Slling (void) strlcpy(buf, "FAULTED", len); 265379c004dSEric Schrock break; 266379c004dSEric Schrock 267379c004dSEric Schrock case ZPOOL_PROP_GUID: 268379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src); 269379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval); 270379c004dSEric Schrock break; 271379c004dSEric Schrock 272379c004dSEric Schrock case ZPOOL_PROP_ALTROOT: 273379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE: 2748704186eSDan McDonald case ZPOOL_PROP_COMMENT: 275379c004dSEric Schrock if (zhp->zpool_props != NULL || 276379c004dSEric Schrock zpool_get_all_props(zhp) == 0) { 277379c004dSEric Schrock (void) strlcpy(buf, 278379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src), 279379c004dSEric Schrock len); 280c58b3526SAdam Stevko break; 281379c004dSEric Schrock } 282379c004dSEric Schrock /* FALLTHROUGH */ 283379c004dSEric Schrock default: 284990b4856Slling (void) strlcpy(buf, "-", len); 285379c004dSEric Schrock break; 286379c004dSEric Schrock } 287379c004dSEric Schrock 288379c004dSEric Schrock if (srctype != NULL) 289379c004dSEric Schrock *srctype = src; 290990b4856Slling return (0); 291990b4856Slling } 292990b4856Slling 293990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) && 294990b4856Slling prop != ZPOOL_PROP_NAME) 295990b4856Slling return (-1); 296990b4856Slling 297990b4856Slling switch (zpool_prop_get_type(prop)) { 298990b4856Slling case PROP_TYPE_STRING: 299990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src), 300990b4856Slling len); 301990b4856Slling break; 302990b4856Slling 303990b4856Slling case PROP_TYPE_NUMBER: 304990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 305990b4856Slling 306990b4856Slling switch (prop) { 307990b4856Slling case ZPOOL_PROP_SIZE: 308485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED: 309485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE: 310ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING: 3117fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED: 312c58b3526SAdam Stevko if (literal) { 313c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 314c58b3526SAdam Stevko (u_longlong_t)intval); 315c58b3526SAdam Stevko } else { 316c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len); 317c58b3526SAdam Stevko } 318990b4856Slling break; 3197855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 3207a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ: 32186714001SSerapheim Dimitropoulos case ZPOOL_PROP_CHECKPOINT: 3227a09f97bSGeorge Wilson if (intval == 0) { 3237a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len); 3247a09f97bSGeorge Wilson } else if (literal) { 3257a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu", 3267a09f97bSGeorge Wilson (u_longlong_t)intval); 3277a09f97bSGeorge Wilson } else { 3287a09f97bSGeorge Wilson (void) zfs_nicenum(intval, buf, len); 3297a09f97bSGeorge Wilson } 3307a09f97bSGeorge Wilson break; 331990b4856Slling case ZPOOL_PROP_CAPACITY: 332c58b3526SAdam Stevko if (literal) { 333c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu", 334c58b3526SAdam Stevko (u_longlong_t)intval); 335c58b3526SAdam Stevko } else { 336c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%", 337c58b3526SAdam Stevko (u_longlong_t)intval); 338c58b3526SAdam Stevko } 339990b4856Slling break; 3402e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION: 3412e4c9986SGeorge Wilson if (intval == UINT64_MAX) { 3422e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len); 3432e4c9986SGeorge Wilson } else { 3442e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%", 3452e4c9986SGeorge Wilson (u_longlong_t)intval); 3462e4c9986SGeorge Wilson } 3472e4c9986SGeorge Wilson break; 348b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO: 349b24ab676SJeff Bonwick (void) snprintf(buf, len, "%llu.%02llux", 350b24ab676SJeff Bonwick (u_longlong_t)(intval / 100), 351b24ab676SJeff Bonwick (u_longlong_t)(intval % 100)); 352b24ab676SJeff Bonwick break; 353990b4856Slling case ZPOOL_PROP_HEALTH: 354990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 355990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 356990b4856Slling verify(nvlist_lookup_uint64_array(nvroot, 3573f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) 3583f9d6ad7SLin Ling == 0); 359990b4856Slling 360990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval, 361990b4856Slling vs->vs_aux), len); 362990b4856Slling break; 363ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION: 364ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) { 365ad135b5dSChristopher Siden (void) snprintf(buf, len, "-"); 366ad135b5dSChristopher Siden break; 367ad135b5dSChristopher Siden } 368ad135b5dSChristopher Siden /* FALLTHROUGH */ 369990b4856Slling default: 370990b4856Slling (void) snprintf(buf, len, "%llu", intval); 371990b4856Slling } 372990b4856Slling break; 373990b4856Slling 374990b4856Slling case PROP_TYPE_INDEX: 375990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src); 376990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval) 377990b4856Slling != 0) 378990b4856Slling return (-1); 379990b4856Slling (void) strlcpy(buf, strval, len); 380990b4856Slling break; 381990b4856Slling 382990b4856Slling default: 383990b4856Slling abort(); 384990b4856Slling } 385990b4856Slling 386990b4856Slling if (srctype) 387990b4856Slling *srctype = src; 388990b4856Slling 389990b4856Slling return (0); 390990b4856Slling } 391990b4856Slling 392990b4856Slling /* 393990b4856Slling * Check if the bootfs name has the same pool name as it is set to. 394990b4856Slling * Assuming bootfs is a valid dataset name. 395990b4856Slling */ 396990b4856Slling static boolean_t 397990b4856Slling bootfs_name_valid(const char *pool, char *bootfs) 398990b4856Slling { 399990b4856Slling int len = strlen(pool); 400015f38bbSPaul Dagnelie if (bootfs[0] == '\0') 401015f38bbSPaul Dagnelie return (B_TRUE); 402990b4856Slling 403fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT)) 404990b4856Slling return (B_FALSE); 405990b4856Slling 406990b4856Slling if (strncmp(pool, bootfs, len) == 0 && 407990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0')) 408990b4856Slling return (B_TRUE); 409990b4856Slling 410990b4856Slling return (B_FALSE); 411990b4856Slling } 412990b4856Slling 4134263d13fSGeorge Wilson boolean_t 4144263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp) 415b5b76fecSGeorge Wilson { 4169adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN]; 417b5b76fecSGeorge Wilson 418b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs, 419c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-", 420b5b76fecSGeorge Wilson sizeof (bootfs)) != 0); 421b5b76fecSGeorge Wilson } 422b5b76fecSGeorge Wilson 423b5b76fecSGeorge Wilson 424990b4856Slling /* 425990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are 426990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are 427990b4856Slling * specified as strings. 428990b4856Slling */ 429990b4856Slling static nvlist_t * 4300a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname, 431f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf) 432990b4856Slling { 433990b4856Slling nvpair_t *elem; 434990b4856Slling nvlist_t *retprops; 435990b4856Slling zpool_prop_t prop; 436990b4856Slling char *strval; 437990b4856Slling uint64_t intval; 4388704186eSDan McDonald char *slash, *check; 4392f8aaab3Seschrock struct stat64 statbuf; 44015e6edf1Sgw zpool_handle_t *zhp; 441990b4856Slling 442990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) { 443990b4856Slling (void) no_memory(hdl); 444990b4856Slling return (NULL); 445990b4856Slling } 446990b4856Slling 447990b4856Slling elem = NULL; 448990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 449990b4856Slling const char *propname = nvpair_name(elem); 450990b4856Slling 451ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname); 4524ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) { 453ad135b5dSChristopher Siden int err; 454ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1; 455ad135b5dSChristopher Siden 4562acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL); 457ad135b5dSChristopher Siden if (err != 0) { 458ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT); 459ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 460ad135b5dSChristopher Siden "invalid feature '%s'"), fname); 461ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 462ad135b5dSChristopher Siden goto error; 463ad135b5dSChristopher Siden } 464ad135b5dSChristopher Siden 465ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) { 466ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 467ad135b5dSChristopher Siden "'%s' must be a string"), propname); 468ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 469ad135b5dSChristopher Siden goto error; 470ad135b5dSChristopher Siden } 471ad135b5dSChristopher Siden 472ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval); 473ad135b5dSChristopher Siden if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) { 474ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 475ad135b5dSChristopher Siden "property '%s' can only be set to " 476ad135b5dSChristopher Siden "'enabled'"), propname); 477ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 478ad135b5dSChristopher Siden goto error; 479ad135b5dSChristopher Siden } 480ad135b5dSChristopher Siden 481ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) { 482ad135b5dSChristopher Siden (void) no_memory(hdl); 483ad135b5dSChristopher Siden goto error; 484ad135b5dSChristopher Siden } 485ad135b5dSChristopher Siden continue; 486ad135b5dSChristopher Siden } 487ad135b5dSChristopher Siden 488990b4856Slling /* 489990b4856Slling * Make sure this property is valid and applies to this type. 490990b4856Slling */ 4914ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL) { 492990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 493990b4856Slling "invalid property '%s'"), propname); 494990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 495990b4856Slling goto error; 496990b4856Slling } 497990b4856Slling 498990b4856Slling if (zpool_prop_readonly(prop)) { 499990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 500990b4856Slling "is readonly"), propname); 501990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 502990b4856Slling goto error; 503990b4856Slling } 504990b4856Slling 505990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops, 506990b4856Slling &strval, &intval, errbuf) != 0) 507990b4856Slling goto error; 508990b4856Slling 509990b4856Slling /* 510990b4856Slling * Perform additional checking for specific properties. 511990b4856Slling */ 512990b4856Slling switch (prop) { 513990b4856Slling case ZPOOL_PROP_VERSION: 514ad135b5dSChristopher Siden if (intval < version || 515ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) { 516990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 517990b4856Slling "property '%s' number %d is invalid."), 518990b4856Slling propname, intval); 519990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 520990b4856Slling goto error; 521990b4856Slling } 522990b4856Slling break; 523990b4856Slling 5247855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE: 5257855d95bSToomas Soome if (!flags.create) { 5267855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5277855d95bSToomas Soome "property '%s' can only be set during pool " 5287855d95bSToomas Soome "creation"), propname); 5297855d95bSToomas Soome (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5307855d95bSToomas Soome goto error; 5317855d95bSToomas Soome } 5327855d95bSToomas Soome break; 5337855d95bSToomas Soome 534990b4856Slling case ZPOOL_PROP_BOOTFS: 535f9af39baSGeorge Wilson if (flags.create || flags.import) { 536990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 537990b4856Slling "property '%s' cannot be set at creation " 538990b4856Slling "or import time"), propname); 539990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 540990b4856Slling goto error; 541990b4856Slling } 542990b4856Slling 543990b4856Slling if (version < SPA_VERSION_BOOTFS) { 544990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 545990b4856Slling "pool must be upgraded to support " 546990b4856Slling "'%s' property"), propname); 547990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 548990b4856Slling goto error; 549990b4856Slling } 550990b4856Slling 551990b4856Slling /* 552990b4856Slling * bootfs property value has to be a dataset name and 553990b4856Slling * the dataset has to be in the same pool as it sets to. 554990b4856Slling */ 555015f38bbSPaul Dagnelie if (!bootfs_name_valid(poolname, strval)) { 556990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 557990b4856Slling "is an invalid name"), strval); 558990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 559990b4856Slling goto error; 560990b4856Slling } 56115e6edf1Sgw 56215e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) { 56315e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 56415e6edf1Sgw "could not open pool '%s'"), poolname); 56515e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 56615e6edf1Sgw goto error; 56715e6edf1Sgw } 56815e6edf1Sgw zpool_close(zhp); 569990b4856Slling break; 570990b4856Slling 5712f8aaab3Seschrock case ZPOOL_PROP_ALTROOT: 572f9af39baSGeorge Wilson if (!flags.create && !flags.import) { 573990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 574990b4856Slling "property '%s' can only be set during pool " 575990b4856Slling "creation or import"), propname); 576990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 577990b4856Slling goto error; 578990b4856Slling } 579990b4856Slling 5802f8aaab3Seschrock if (strval[0] != '/') { 581990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5822f8aaab3Seschrock "bad alternate root '%s'"), strval); 5832f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 584990b4856Slling goto error; 585990b4856Slling } 5862f8aaab3Seschrock break; 5872f8aaab3Seschrock 5882f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE: 5892f8aaab3Seschrock if (strval[0] == '\0') 5902f8aaab3Seschrock break; 5912f8aaab3Seschrock 5922f8aaab3Seschrock if (strcmp(strval, "none") == 0) 5932f8aaab3Seschrock break; 594990b4856Slling 595990b4856Slling if (strval[0] != '/') { 596990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5972f8aaab3Seschrock "property '%s' must be empty, an " 5982f8aaab3Seschrock "absolute path, or 'none'"), propname); 599990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 600990b4856Slling goto error; 601990b4856Slling } 602990b4856Slling 6032f8aaab3Seschrock slash = strrchr(strval, '/'); 604990b4856Slling 6052f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || 6062f8aaab3Seschrock strcmp(slash, "/..") == 0) { 6072f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6082f8aaab3Seschrock "'%s' is not a valid file"), strval); 6092f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6102f8aaab3Seschrock goto error; 6112f8aaab3Seschrock } 612990b4856Slling 6132f8aaab3Seschrock *slash = '\0'; 6142f8aaab3Seschrock 6152c32020fSeschrock if (strval[0] != '\0' && 6162c32020fSeschrock (stat64(strval, &statbuf) != 0 || 6172c32020fSeschrock !S_ISDIR(statbuf.st_mode))) { 6182f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6192f8aaab3Seschrock "'%s' is not a valid directory"), 6202f8aaab3Seschrock strval); 6212f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf); 6222f8aaab3Seschrock goto error; 6232f8aaab3Seschrock } 6242f8aaab3Seschrock 6252f8aaab3Seschrock *slash = '/'; 6262f8aaab3Seschrock break; 627f9af39baSGeorge Wilson 6288704186eSDan McDonald case ZPOOL_PROP_COMMENT: 6298704186eSDan McDonald for (check = strval; *check != '\0'; check++) { 6308704186eSDan McDonald if (!isprint(*check)) { 6318704186eSDan McDonald zfs_error_aux(hdl, 6328704186eSDan McDonald dgettext(TEXT_DOMAIN, 6338704186eSDan McDonald "comment may only have printable " 6348704186eSDan McDonald "characters")); 6358704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, 6368704186eSDan McDonald errbuf); 6378704186eSDan McDonald goto error; 6388704186eSDan McDonald } 6398704186eSDan McDonald } 6408704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) { 6418704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6428704186eSDan McDonald "comment must not exceed %d characters"), 6438704186eSDan McDonald ZPROP_MAX_COMMENT); 6448704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6458704186eSDan McDonald goto error; 6468704186eSDan McDonald } 6478704186eSDan McDonald break; 64804e56356SAndriy Gapon 649f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY: 650f9af39baSGeorge Wilson if (!flags.import) { 651f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 652f9af39baSGeorge Wilson "property '%s' can only be set at " 653f9af39baSGeorge Wilson "import time"), propname); 654f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 655f9af39baSGeorge Wilson goto error; 656f9af39baSGeorge Wilson } 657f9af39baSGeorge Wilson break; 65888f61deeSIgor Kozhukhov 65904e56356SAndriy Gapon case ZPOOL_PROP_TNAME: 66004e56356SAndriy Gapon if (!flags.create) { 66104e56356SAndriy Gapon zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 66204e56356SAndriy Gapon "property '%s' can only be set at " 66304e56356SAndriy Gapon "creation time"), propname); 66404e56356SAndriy Gapon (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 66504e56356SAndriy Gapon goto error; 66604e56356SAndriy Gapon } 66704e56356SAndriy Gapon break; 66804e56356SAndriy Gapon 669e0f1c0afSOlaf Faaland case ZPOOL_PROP_MULTIHOST: 670e0f1c0afSOlaf Faaland if (get_system_hostid() == 0) { 671e0f1c0afSOlaf Faaland zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 672e0f1c0afSOlaf Faaland "requires a non-zero system hostid")); 673e0f1c0afSOlaf Faaland (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 674e0f1c0afSOlaf Faaland goto error; 675e0f1c0afSOlaf Faaland } 676e0f1c0afSOlaf Faaland break; 677e0f1c0afSOlaf Faaland 67888f61deeSIgor Kozhukhov default: 67988f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 68088f61deeSIgor Kozhukhov "property '%s'(%d) not defined"), propname, prop); 68188f61deeSIgor Kozhukhov break; 682990b4856Slling } 683990b4856Slling } 684990b4856Slling 685990b4856Slling return (retprops); 686990b4856Slling error: 687990b4856Slling nvlist_free(retprops); 688990b4856Slling return (NULL); 689990b4856Slling } 690990b4856Slling 691990b4856Slling /* 692990b4856Slling * Set zpool property : propname=propval. 693990b4856Slling */ 694990b4856Slling int 695990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval) 696990b4856Slling { 697990b4856Slling zfs_cmd_t zc = { 0 }; 698990b4856Slling int ret = -1; 699990b4856Slling char errbuf[1024]; 700990b4856Slling nvlist_t *nvl = NULL; 701990b4856Slling nvlist_t *realprops; 702990b4856Slling uint64_t version; 703f9af39baSGeorge Wilson prop_flags_t flags = { 0 }; 704990b4856Slling 705990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), 706990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 707990b4856Slling zhp->zpool_name); 708990b4856Slling 709990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 710990b4856Slling return (no_memory(zhp->zpool_hdl)); 711990b4856Slling 712990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) { 713990b4856Slling nvlist_free(nvl); 714990b4856Slling return (no_memory(zhp->zpool_hdl)); 715990b4856Slling } 716990b4856Slling 717990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 7180a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl, 719f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) { 720990b4856Slling nvlist_free(nvl); 721990b4856Slling return (-1); 722990b4856Slling } 723990b4856Slling 724990b4856Slling nvlist_free(nvl); 725990b4856Slling nvl = realprops; 726990b4856Slling 727990b4856Slling /* 728990b4856Slling * Execute the corresponding ioctl() to set this property. 729990b4856Slling */ 730990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 731990b4856Slling 732990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) { 733990b4856Slling nvlist_free(nvl); 734990b4856Slling return (-1); 735990b4856Slling } 736990b4856Slling 737990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc); 738990b4856Slling 739990b4856Slling zcmd_free_nvlists(&zc); 740990b4856Slling nvlist_free(nvl); 741990b4856Slling 742990b4856Slling if (ret) 743990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf); 744990b4856Slling else 745990b4856Slling (void) zpool_props_refresh(zhp); 746990b4856Slling 747990b4856Slling return (ret); 748990b4856Slling } 749990b4856Slling 750990b4856Slling int 751990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp) 752990b4856Slling { 753990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl; 754990b4856Slling zprop_list_t *entry; 755990b4856Slling char buf[ZFS_MAXPROPLEN]; 756ad135b5dSChristopher Siden nvlist_t *features = NULL; 757ad135b5dSChristopher Siden zprop_list_t **last; 758ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp); 759990b4856Slling 760990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0) 761990b4856Slling return (-1); 762990b4856Slling 763ad135b5dSChristopher Siden last = plp; 764ad135b5dSChristopher Siden while (*last != NULL) 765ad135b5dSChristopher Siden last = &(*last)->pl_next; 766ad135b5dSChristopher Siden 767ad135b5dSChristopher Siden if ((*plp)->pl_all) 768ad135b5dSChristopher Siden features = zpool_get_features(zhp); 769ad135b5dSChristopher Siden 770ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) { 771ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) { 772ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl, 773ad135b5dSChristopher Siden sizeof (zprop_list_t)); 774ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 775ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s", 776ad135b5dSChristopher Siden spa_feature_table[i].fi_uname); 777ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 778ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 779ad135b5dSChristopher Siden 780ad135b5dSChristopher Siden *last = entry; 781ad135b5dSChristopher Siden last = &entry->pl_next; 782ad135b5dSChristopher Siden } 783ad135b5dSChristopher Siden } 784ad135b5dSChristopher Siden 785ad135b5dSChristopher Siden /* add any unsupported features */ 786ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL); 787ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) { 788ad135b5dSChristopher Siden char *propname; 789ad135b5dSChristopher Siden boolean_t found; 790ad135b5dSChristopher Siden zprop_list_t *entry; 791ad135b5dSChristopher Siden 792ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp))) 793ad135b5dSChristopher Siden continue; 794ad135b5dSChristopher Siden 795ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s", 796ad135b5dSChristopher Siden nvpair_name(nvp)); 797ad135b5dSChristopher Siden 798ad135b5dSChristopher Siden /* 799ad135b5dSChristopher Siden * Before adding the property to the list make sure that no 800ad135b5dSChristopher Siden * other pool already added the same property. 801ad135b5dSChristopher Siden */ 802ad135b5dSChristopher Siden found = B_FALSE; 803ad135b5dSChristopher Siden entry = *plp; 804ad135b5dSChristopher Siden while (entry != NULL) { 805ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL && 806ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) { 807ad135b5dSChristopher Siden found = B_TRUE; 808ad135b5dSChristopher Siden break; 809ad135b5dSChristopher Siden } 810ad135b5dSChristopher Siden entry = entry->pl_next; 811ad135b5dSChristopher Siden } 812ad135b5dSChristopher Siden if (found) { 813ad135b5dSChristopher Siden free(propname); 814ad135b5dSChristopher Siden continue; 815ad135b5dSChristopher Siden } 816ad135b5dSChristopher Siden 817ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 818ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL; 819ad135b5dSChristopher Siden entry->pl_user_prop = propname; 820ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop); 821ad135b5dSChristopher Siden entry->pl_all = B_TRUE; 822ad135b5dSChristopher Siden 823ad135b5dSChristopher Siden *last = entry; 824ad135b5dSChristopher Siden last = &entry->pl_next; 825ad135b5dSChristopher Siden } 826ad135b5dSChristopher Siden 827990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) { 828990b4856Slling 829990b4856Slling if (entry->pl_fixed) 830990b4856Slling continue; 831990b4856Slling 832990b4856Slling if (entry->pl_prop != ZPROP_INVAL && 833990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf), 834c58b3526SAdam Stevko NULL, B_FALSE) == 0) { 835990b4856Slling if (strlen(buf) > entry->pl_width) 836990b4856Slling entry->pl_width = strlen(buf); 837990b4856Slling } 838990b4856Slling } 839990b4856Slling 840990b4856Slling return (0); 841990b4856Slling } 842990b4856Slling 843ad135b5dSChristopher Siden /* 844ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool. 845ad135b5dSChristopher Siden */ 846ad135b5dSChristopher Siden int 847ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf, 848ad135b5dSChristopher Siden size_t len) 849ad135b5dSChristopher Siden { 850ad135b5dSChristopher Siden uint64_t refcount; 851ad135b5dSChristopher Siden boolean_t found = B_FALSE; 852ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp); 853ad135b5dSChristopher Siden boolean_t supported; 854ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1; 855ad135b5dSChristopher Siden 856ad135b5dSChristopher Siden supported = zpool_prop_feature(propname); 8577c13517fSSerapheim Dimitropoulos ASSERT(supported || zpool_prop_unsupported(propname)); 858ad135b5dSChristopher Siden 859ad135b5dSChristopher Siden /* 860ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is 861ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already 862ad135b5dSChristopher Siden * use guids. 863ad135b5dSChristopher Siden */ 864ad135b5dSChristopher Siden if (supported) { 865ad135b5dSChristopher Siden int ret; 8662acef22dSMatthew Ahrens spa_feature_t fid; 867ad135b5dSChristopher Siden 8682acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid); 869ad135b5dSChristopher Siden if (ret != 0) { 870ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 871ad135b5dSChristopher Siden return (ENOTSUP); 872ad135b5dSChristopher Siden } 8732acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid; 874ad135b5dSChristopher Siden } 875ad135b5dSChristopher Siden 876ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0) 877ad135b5dSChristopher Siden found = B_TRUE; 878ad135b5dSChristopher Siden 879ad135b5dSChristopher Siden if (supported) { 880ad135b5dSChristopher Siden if (!found) { 881ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len); 882ad135b5dSChristopher Siden } else { 883ad135b5dSChristopher Siden if (refcount == 0) 884ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len); 885ad135b5dSChristopher Siden else 886ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len); 887ad135b5dSChristopher Siden } 888ad135b5dSChristopher Siden } else { 889ad135b5dSChristopher Siden if (found) { 890ad135b5dSChristopher Siden if (refcount == 0) { 891ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE); 892ad135b5dSChristopher Siden } else { 893ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY); 894ad135b5dSChristopher Siden } 895ad135b5dSChristopher Siden } else { 896ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len); 897ad135b5dSChristopher Siden return (ENOTSUP); 898ad135b5dSChristopher Siden } 899ad135b5dSChristopher Siden } 900ad135b5dSChristopher Siden 901ad135b5dSChristopher Siden return (0); 902ad135b5dSChristopher Siden } 903990b4856Slling 904573ca77eSGeorge Wilson /* 905573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage 906573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead. 907573ca77eSGeorge Wilson */ 908573ca77eSGeorge Wilson #define NEW_START_BLOCK 256 909573ca77eSGeorge Wilson 910fa9e4066Sahrens /* 911fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in 912fa9e4066Sahrens * 'buf'. 913fa9e4066Sahrens */ 914e7cbe64fSgw boolean_t 91599653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) 916fa9e4066Sahrens { 917fa9e4066Sahrens namecheck_err_t why; 918fa9e4066Sahrens char what; 919b468a217Seschrock int ret; 920b468a217Seschrock 921b468a217Seschrock ret = pool_namecheck(pool, &why, &what); 922b468a217Seschrock 923b468a217Seschrock /* 924b468a217Seschrock * The rules for reserved pool names were extended at a later point. 925b468a217Seschrock * But we need to support users with existing pools that may now be 926b468a217Seschrock * invalid. So we only check for this expanded set of names during a 927b468a217Seschrock * create (or import), and only in userland. 928b468a217Seschrock */ 929b468a217Seschrock if (ret == 0 && !isopen && 930b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 || 931b468a217Seschrock strncmp(pool, "raidz", 5) == 0 || 9328654d025Sperrin strncmp(pool, "spare", 5) == 0 || 9338654d025Sperrin strcmp(pool, "log") == 0)) { 934e7cbe64fSgw if (hdl != NULL) 935e7cbe64fSgw zfs_error_aux(hdl, 936e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved")); 93799653d4eSeschrock return (B_FALSE); 938b468a217Seschrock } 939b468a217Seschrock 940fa9e4066Sahrens 941b468a217Seschrock if (ret != 0) { 94299653d4eSeschrock if (hdl != NULL) { 943fa9e4066Sahrens switch (why) { 944b81d61a6Slling case NAME_ERR_TOOLONG: 94599653d4eSeschrock zfs_error_aux(hdl, 946b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long")); 947b81d61a6Slling break; 948b81d61a6Slling 949fa9e4066Sahrens case NAME_ERR_INVALCHAR: 95099653d4eSeschrock zfs_error_aux(hdl, 951fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 952fa9e4066Sahrens "'%c' in pool name"), what); 953fa9e4066Sahrens break; 954fa9e4066Sahrens 955fa9e4066Sahrens case NAME_ERR_NOLETTER: 95699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 95799653d4eSeschrock "name must begin with a letter")); 958fa9e4066Sahrens break; 959fa9e4066Sahrens 960fa9e4066Sahrens case NAME_ERR_RESERVED: 96199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96299653d4eSeschrock "name is reserved")); 963fa9e4066Sahrens break; 964fa9e4066Sahrens 965fa9e4066Sahrens case NAME_ERR_DISKLIKE: 96699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96799653d4eSeschrock "pool name is reserved")); 968fa9e4066Sahrens break; 9695ad82045Snd 9705ad82045Snd case NAME_ERR_LEADING_SLASH: 9715ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9725ad82045Snd "leading slash in name")); 9735ad82045Snd break; 9745ad82045Snd 9755ad82045Snd case NAME_ERR_EMPTY_COMPONENT: 9765ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9775ad82045Snd "empty component in name")); 9785ad82045Snd break; 9795ad82045Snd 9805ad82045Snd case NAME_ERR_TRAILING_SLASH: 9815ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9825ad82045Snd "trailing slash in name")); 9835ad82045Snd break; 9845ad82045Snd 985edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS: 9865ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 987edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in " 988edb901aaSMarcel Telka "name")); 9895ad82045Snd break; 9905ad82045Snd 99188f61deeSIgor Kozhukhov default: 99288f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 99388f61deeSIgor Kozhukhov "(%d) not defined"), why); 99488f61deeSIgor Kozhukhov break; 995fa9e4066Sahrens } 996fa9e4066Sahrens } 99799653d4eSeschrock return (B_FALSE); 998fa9e4066Sahrens } 999fa9e4066Sahrens 100099653d4eSeschrock return (B_TRUE); 1001fa9e4066Sahrens } 1002fa9e4066Sahrens 1003fa9e4066Sahrens /* 1004fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED 1005fa9e4066Sahrens * state. 1006fa9e4066Sahrens */ 1007fa9e4066Sahrens zpool_handle_t * 100899653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) 1009fa9e4066Sahrens { 1010fa9e4066Sahrens zpool_handle_t *zhp; 101194de1d4cSeschrock boolean_t missing; 1012fa9e4066Sahrens 1013fa9e4066Sahrens /* 1014fa9e4066Sahrens * Make sure the pool name is valid. 1015fa9e4066Sahrens */ 101699653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) { 1017ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME, 101899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), 101999653d4eSeschrock pool); 1020fa9e4066Sahrens return (NULL); 1021fa9e4066Sahrens } 1022fa9e4066Sahrens 102399653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 102499653d4eSeschrock return (NULL); 1025fa9e4066Sahrens 102699653d4eSeschrock zhp->zpool_hdl = hdl; 1027fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1028fa9e4066Sahrens 102994de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 103094de1d4cSeschrock zpool_close(zhp); 103194de1d4cSeschrock return (NULL); 103294de1d4cSeschrock } 103394de1d4cSeschrock 103494de1d4cSeschrock if (missing) { 1035990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool")); 1036ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT, 1037990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool); 103894de1d4cSeschrock zpool_close(zhp); 103994de1d4cSeschrock return (NULL); 1040fa9e4066Sahrens } 1041fa9e4066Sahrens 1042fa9e4066Sahrens return (zhp); 1043fa9e4066Sahrens } 1044fa9e4066Sahrens 1045fa9e4066Sahrens /* 1046fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because 1047fa9e4066Sahrens * the configuration cache may be out of date). 1048fa9e4066Sahrens */ 104994de1d4cSeschrock int 105094de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) 1051fa9e4066Sahrens { 1052fa9e4066Sahrens zpool_handle_t *zhp; 105394de1d4cSeschrock boolean_t missing; 1054fa9e4066Sahrens 105594de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) 105694de1d4cSeschrock return (-1); 1057fa9e4066Sahrens 105899653d4eSeschrock zhp->zpool_hdl = hdl; 1059fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); 1060fa9e4066Sahrens 106194de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) { 106294de1d4cSeschrock zpool_close(zhp); 106394de1d4cSeschrock return (-1); 1064fa9e4066Sahrens } 1065fa9e4066Sahrens 106694de1d4cSeschrock if (missing) { 106794de1d4cSeschrock zpool_close(zhp); 106894de1d4cSeschrock *ret = NULL; 106994de1d4cSeschrock return (0); 107094de1d4cSeschrock } 107194de1d4cSeschrock 107294de1d4cSeschrock *ret = zhp; 107394de1d4cSeschrock return (0); 1074fa9e4066Sahrens } 1075fa9e4066Sahrens 1076fa9e4066Sahrens /* 1077fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted 1078fa9e4066Sahrens * state. 1079fa9e4066Sahrens */ 1080fa9e4066Sahrens zpool_handle_t * 108199653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool) 1082fa9e4066Sahrens { 1083fa9e4066Sahrens zpool_handle_t *zhp; 1084fa9e4066Sahrens 108599653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) 1086fa9e4066Sahrens return (NULL); 1087fa9e4066Sahrens 1088fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) { 1089ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL, 109099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); 1091fa9e4066Sahrens zpool_close(zhp); 1092fa9e4066Sahrens return (NULL); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens 1095fa9e4066Sahrens return (zhp); 1096fa9e4066Sahrens } 1097fa9e4066Sahrens 1098fa9e4066Sahrens /* 1099fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle. 1100fa9e4066Sahrens */ 1101fa9e4066Sahrens void 1102fa9e4066Sahrens zpool_close(zpool_handle_t *zhp) 1103fa9e4066Sahrens { 1104aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config); 1105aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config); 1106aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props); 1107fa9e4066Sahrens free(zhp); 1108fa9e4066Sahrens } 1109fa9e4066Sahrens 1110fa9e4066Sahrens /* 1111fa9e4066Sahrens * Return the name of the pool. 1112fa9e4066Sahrens */ 1113fa9e4066Sahrens const char * 1114fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp) 1115fa9e4066Sahrens { 1116fa9e4066Sahrens return (zhp->zpool_name); 1117fa9e4066Sahrens } 1118fa9e4066Sahrens 1119fa9e4066Sahrens 1120fa9e4066Sahrens /* 1121fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE) 1122fa9e4066Sahrens */ 1123fa9e4066Sahrens int 1124fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp) 1125fa9e4066Sahrens { 1126fa9e4066Sahrens return (zhp->zpool_state); 1127fa9e4066Sahrens } 1128fa9e4066Sahrens 1129*663207adSDon Brady /* 1130*663207adSDon Brady * Check if vdev list contains a special vdev 1131*663207adSDon Brady */ 1132*663207adSDon Brady static boolean_t 1133*663207adSDon Brady zpool_has_special_vdev(nvlist_t *nvroot) 1134*663207adSDon Brady { 1135*663207adSDon Brady nvlist_t **child; 1136*663207adSDon Brady uint_t children; 1137*663207adSDon Brady 1138*663207adSDon Brady if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child, 1139*663207adSDon Brady &children) == 0) { 1140*663207adSDon Brady for (uint_t c = 0; c < children; c++) { 1141*663207adSDon Brady char *bias; 1142*663207adSDon Brady 1143*663207adSDon Brady if (nvlist_lookup_string(child[c], 1144*663207adSDon Brady ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 && 1145*663207adSDon Brady strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) { 1146*663207adSDon Brady return (B_TRUE); 1147*663207adSDon Brady } 1148*663207adSDon Brady } 1149*663207adSDon Brady } 1150*663207adSDon Brady return (B_FALSE); 1151*663207adSDon Brady } 1152*663207adSDon Brady 1153fa9e4066Sahrens /* 1154fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed 1155fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we 1156fa9e4066Sahrens * don't have to worry about error semantics. 1157fa9e4066Sahrens */ 1158fa9e4066Sahrens int 115999653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, 11600a48a24eStimh nvlist_t *props, nvlist_t *fsprops) 1161fa9e4066Sahrens { 1162fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 11630a48a24eStimh nvlist_t *zc_fsprops = NULL; 11640a48a24eStimh nvlist_t *zc_props = NULL; 116599653d4eSeschrock char msg[1024]; 11660a48a24eStimh int ret = -1; 1167fa9e4066Sahrens 116899653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 116999653d4eSeschrock "cannot create '%s'"), pool); 1170fa9e4066Sahrens 117199653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool)) 117299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 1173fa9e4066Sahrens 1174351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 1175990b4856Slling return (-1); 1176fa9e4066Sahrens 11770a48a24eStimh if (props) { 1178f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE }; 1179f9af39baSGeorge Wilson 11800a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props, 1181f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) { 11820a48a24eStimh goto create_failed; 11830a48a24eStimh } 11840a48a24eStimh } 118599653d4eSeschrock 11860a48a24eStimh if (fsprops) { 11870a48a24eStimh uint64_t zoned; 11880a48a24eStimh char *zonestr; 11890a48a24eStimh 11900a48a24eStimh zoned = ((nvlist_lookup_string(fsprops, 11910a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) && 11920a48a24eStimh strcmp(zonestr, "on") == 0); 11930a48a24eStimh 1194e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM, 1195e9316f76SJoe Stein fsprops, zoned, NULL, NULL, msg)) == NULL) { 11960a48a24eStimh goto create_failed; 11970a48a24eStimh } 1198*663207adSDon Brady 1199*663207adSDon Brady if (nvlist_exists(zc_fsprops, 1200*663207adSDon Brady zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) && 1201*663207adSDon Brady !zpool_has_special_vdev(nvroot)) { 1202*663207adSDon Brady zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1203*663207adSDon Brady "%s property requires a special vdev"), 1204*663207adSDon Brady zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)); 1205*663207adSDon Brady (void) zfs_error(hdl, EZFS_BADPROP, msg); 1206*663207adSDon Brady goto create_failed; 1207*663207adSDon Brady } 1208*663207adSDon Brady 12090a48a24eStimh if (!zc_props && 12100a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) { 12110a48a24eStimh goto create_failed; 12120a48a24eStimh } 12130a48a24eStimh if (nvlist_add_nvlist(zc_props, 12140a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) { 12150a48a24eStimh goto create_failed; 12160a48a24eStimh } 1217351420b3Slling } 1218fa9e4066Sahrens 12190a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 12200a48a24eStimh goto create_failed; 12210a48a24eStimh 1222990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); 1223fa9e4066Sahrens 12240a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) { 1225351420b3Slling 1226e9dbad6fSeschrock zcmd_free_nvlists(&zc); 12270a48a24eStimh nvlist_free(zc_props); 12280a48a24eStimh nvlist_free(zc_fsprops); 1229fa9e4066Sahrens 123099653d4eSeschrock switch (errno) { 1231fa9e4066Sahrens case EBUSY: 1232fa9e4066Sahrens /* 1233fa9e4066Sahrens * This can happen if the user has specified the same 1234fa9e4066Sahrens * device multiple times. We can't reliably detect this 1235fa9e4066Sahrens * until we try to add it and see we already have a 1236fa9e4066Sahrens * label. 1237fa9e4066Sahrens */ 123899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 123999653d4eSeschrock "one or more vdevs refer to the same device")); 124099653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1241fa9e4066Sahrens 1242e9316f76SJoe Stein case ERANGE: 1243e9316f76SJoe Stein /* 1244e9316f76SJoe Stein * This happens if the record size is smaller or larger 1245e9316f76SJoe Stein * than the allowed size range, or not a power of 2. 1246e9316f76SJoe Stein * 1247e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier, 1248e9316f76SJoe Stein * this case may have slipped through since the 1249e9316f76SJoe Stein * pool does not exist yet and it is therefore 1250e9316f76SJoe Stein * impossible to read properties e.g. max blocksize 1251e9316f76SJoe Stein * from the pool. 1252e9316f76SJoe Stein */ 1253e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1254e9316f76SJoe Stein "record size invalid")); 1255e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg)); 1256e9316f76SJoe Stein 1257fa9e4066Sahrens case EOVERFLOW: 1258fa9e4066Sahrens /* 125999653d4eSeschrock * This occurs when one of the devices is below 1260fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1261fa9e4066Sahrens * device was the problem device since there's no 1262fa9e4066Sahrens * reliable way to determine device size from userland. 1263fa9e4066Sahrens */ 1264fa9e4066Sahrens { 1265fa9e4066Sahrens char buf[64]; 1266fa9e4066Sahrens 1267fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1268fa9e4066Sahrens 126999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 127099653d4eSeschrock "one or more devices is less than the " 127199653d4eSeschrock "minimum size (%s)"), buf); 1272fa9e4066Sahrens } 127399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1274fa9e4066Sahrens 1275fa9e4066Sahrens case ENOSPC: 127699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 127799653d4eSeschrock "one or more devices is out of space")); 127899653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg)); 1279fa9e4066Sahrens 1280fa94a07fSbrendan case ENOTBLK: 1281fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1282fa94a07fSbrendan "cache device must be a disk or disk slice")); 1283fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADDEV, msg)); 1284fa94a07fSbrendan 1285fa9e4066Sahrens default: 128699653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 1287fa9e4066Sahrens } 1288fa9e4066Sahrens } 1289fa9e4066Sahrens 12900a48a24eStimh create_failed: 1291351420b3Slling zcmd_free_nvlists(&zc); 12920a48a24eStimh nvlist_free(zc_props); 12930a48a24eStimh nvlist_free(zc_fsprops); 12940a48a24eStimh return (ret); 1295fa9e4066Sahrens } 1296fa9e4066Sahrens 1297fa9e4066Sahrens /* 1298fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no 1299fa9e4066Sahrens * datasets left in the pool. 1300fa9e4066Sahrens */ 1301fa9e4066Sahrens int 13024445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str) 1303fa9e4066Sahrens { 1304fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1305fa9e4066Sahrens zfs_handle_t *zfp = NULL; 130699653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 130799653d4eSeschrock char msg[1024]; 1308fa9e4066Sahrens 1309fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE && 1310cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL) 1311fa9e4066Sahrens return (-1); 1312fa9e4066Sahrens 1313fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 13144445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 1315fa9e4066Sahrens 1316cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) { 131799653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 131899653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name); 1319fa9e4066Sahrens 132099653d4eSeschrock if (errno == EROFS) { 132199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 132299653d4eSeschrock "one or more devices is read only")); 132399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 132499653d4eSeschrock } else { 132599653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1326fa9e4066Sahrens } 1327fa9e4066Sahrens 1328fa9e4066Sahrens if (zfp) 1329fa9e4066Sahrens zfs_close(zfp); 1330fa9e4066Sahrens return (-1); 1331fa9e4066Sahrens } 1332fa9e4066Sahrens 1333fa9e4066Sahrens if (zfp) { 1334fa9e4066Sahrens remove_mountpoint(zfp); 1335fa9e4066Sahrens zfs_close(zfp); 1336fa9e4066Sahrens } 1337fa9e4066Sahrens 1338fa9e4066Sahrens return (0); 1339fa9e4066Sahrens } 1340fa9e4066Sahrens 134186714001SSerapheim Dimitropoulos /* 134286714001SSerapheim Dimitropoulos * Create a checkpoint in the given pool. 134386714001SSerapheim Dimitropoulos */ 134486714001SSerapheim Dimitropoulos int 134586714001SSerapheim Dimitropoulos zpool_checkpoint(zpool_handle_t *zhp) 134686714001SSerapheim Dimitropoulos { 134786714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl; 134886714001SSerapheim Dimitropoulos char msg[1024]; 134986714001SSerapheim Dimitropoulos int error; 135086714001SSerapheim Dimitropoulos 135186714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint(zhp->zpool_name); 135286714001SSerapheim Dimitropoulos if (error != 0) { 135386714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 135486714001SSerapheim Dimitropoulos "cannot checkpoint '%s'"), zhp->zpool_name); 135586714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg); 135686714001SSerapheim Dimitropoulos return (-1); 135786714001SSerapheim Dimitropoulos } 135886714001SSerapheim Dimitropoulos 135986714001SSerapheim Dimitropoulos return (0); 136086714001SSerapheim Dimitropoulos } 136186714001SSerapheim Dimitropoulos 136286714001SSerapheim Dimitropoulos /* 136386714001SSerapheim Dimitropoulos * Discard the checkpoint from the given pool. 136486714001SSerapheim Dimitropoulos */ 136586714001SSerapheim Dimitropoulos int 136686714001SSerapheim Dimitropoulos zpool_discard_checkpoint(zpool_handle_t *zhp) 136786714001SSerapheim Dimitropoulos { 136886714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl; 136986714001SSerapheim Dimitropoulos char msg[1024]; 137086714001SSerapheim Dimitropoulos int error; 137186714001SSerapheim Dimitropoulos 137286714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint_discard(zhp->zpool_name); 137386714001SSerapheim Dimitropoulos if (error != 0) { 137486714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 137586714001SSerapheim Dimitropoulos "cannot discard checkpoint in '%s'"), zhp->zpool_name); 137686714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg); 137786714001SSerapheim Dimitropoulos return (-1); 137886714001SSerapheim Dimitropoulos } 137986714001SSerapheim Dimitropoulos 138086714001SSerapheim Dimitropoulos return (0); 138186714001SSerapheim Dimitropoulos } 138286714001SSerapheim Dimitropoulos 1383fa9e4066Sahrens /* 1384fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the 1385fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed. 1386fa9e4066Sahrens */ 1387fa9e4066Sahrens int 1388fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) 1389fa9e4066Sahrens { 1390e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 139199653d4eSeschrock int ret; 139299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 139399653d4eSeschrock char msg[1024]; 1394fa94a07fSbrendan nvlist_t **spares, **l2cache; 1395fa94a07fSbrendan uint_t nspares, nl2cache; 139699653d4eSeschrock 139799653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 139899653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name); 139999653d4eSeschrock 1400fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1401fa94a07fSbrendan SPA_VERSION_SPARES && 140299653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, 140399653d4eSeschrock &spares, &nspares) == 0) { 140499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 140599653d4eSeschrock "upgraded to add hot spares")); 140699653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg)); 140799653d4eSeschrock } 1408fa9e4066Sahrens 1409fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) < 1410fa94a07fSbrendan SPA_VERSION_L2CACHE && 1411fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, 1412fa94a07fSbrendan &l2cache, &nl2cache) == 0) { 1413fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " 1414fa94a07fSbrendan "upgraded to add cache devices")); 1415fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg)); 1416fa94a07fSbrendan } 1417fa94a07fSbrendan 1418990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 141999653d4eSeschrock return (-1); 1420fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 1421fa9e4066Sahrens 1422cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) { 1423fa9e4066Sahrens switch (errno) { 1424fa9e4066Sahrens case EBUSY: 1425fa9e4066Sahrens /* 1426fa9e4066Sahrens * This can happen if the user has specified the same 1427fa9e4066Sahrens * device multiple times. We can't reliably detect this 1428fa9e4066Sahrens * until we try to add it and see we already have a 1429fa9e4066Sahrens * label. 1430fa9e4066Sahrens */ 143199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 143299653d4eSeschrock "one or more vdevs refer to the same device")); 143399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 1434fa9e4066Sahrens break; 1435fa9e4066Sahrens 14365cabbc6bSPrashanth Sreenivasa case EINVAL: 14375cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14385cabbc6bSPrashanth Sreenivasa "invalid config; a pool with removing/removed " 14395cabbc6bSPrashanth Sreenivasa "vdevs does not support adding raidz vdevs")); 14405cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BADDEV, msg); 14415cabbc6bSPrashanth Sreenivasa break; 14425cabbc6bSPrashanth Sreenivasa 1443fa9e4066Sahrens case EOVERFLOW: 1444fa9e4066Sahrens /* 1445fa9e4066Sahrens * This occurrs when one of the devices is below 1446fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which 1447fa9e4066Sahrens * device was the problem device since there's no 1448fa9e4066Sahrens * reliable way to determine device size from userland. 1449fa9e4066Sahrens */ 1450fa9e4066Sahrens { 1451fa9e4066Sahrens char buf[64]; 1452fa9e4066Sahrens 1453fa9e4066Sahrens zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); 1454fa9e4066Sahrens 145599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 145699653d4eSeschrock "device is less than the minimum " 145799653d4eSeschrock "size (%s)"), buf); 1458fa9e4066Sahrens } 145999653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 146099653d4eSeschrock break; 146199653d4eSeschrock 146299653d4eSeschrock case ENOTSUP: 146399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14648654d025Sperrin "pool must be upgraded to add these vdevs")); 146599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg); 1466fa9e4066Sahrens break; 1467fa9e4066Sahrens 1468b1b8ab34Slling case EDOM: 1469b1b8ab34Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14708654d025Sperrin "root pool can not have multiple vdevs" 14718654d025Sperrin " or separate logs")); 1472b1b8ab34Slling (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg); 1473b1b8ab34Slling break; 1474b1b8ab34Slling 1475fa94a07fSbrendan case ENOTBLK: 1476fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1477fa94a07fSbrendan "cache device must be a disk or disk slice")); 1478fa94a07fSbrendan (void) zfs_error(hdl, EZFS_BADDEV, msg); 1479fa94a07fSbrendan break; 1480fa94a07fSbrendan 1481fa9e4066Sahrens default: 148299653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 1483fa9e4066Sahrens } 1484fa9e4066Sahrens 148599653d4eSeschrock ret = -1; 148699653d4eSeschrock } else { 148799653d4eSeschrock ret = 0; 1488fa9e4066Sahrens } 1489fa9e4066Sahrens 1490e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1491fa9e4066Sahrens 149299653d4eSeschrock return (ret); 1493fa9e4066Sahrens } 1494fa9e4066Sahrens 1495fa9e4066Sahrens /* 1496fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no 1497fa9e4066Sahrens * mounted datasets in the pool. 1498fa9e4066Sahrens */ 14994445fffbSMatthew Ahrens static int 15004445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce, 15014445fffbSMatthew Ahrens const char *log_str) 1502fa9e4066Sahrens { 1503fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 150489a89ebfSlling char msg[1024]; 1505fa9e4066Sahrens 150689a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 150789a89ebfSlling "cannot export '%s'"), zhp->zpool_name); 150889a89ebfSlling 1509fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 151089a89ebfSlling zc.zc_cookie = force; 1511394ab0cbSGeorge Wilson zc.zc_guid = hardforce; 15124445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str; 151389a89ebfSlling 151489a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) { 151589a89ebfSlling switch (errno) { 151689a89ebfSlling case EXDEV: 151789a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN, 151889a89ebfSlling "use '-f' to override the following errors:\n" 151989a89ebfSlling "'%s' has an active shared spare which could be" 152089a89ebfSlling " used by other pools once '%s' is exported."), 152189a89ebfSlling zhp->zpool_name, zhp->zpool_name); 152289a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE, 152389a89ebfSlling msg)); 152489a89ebfSlling default: 152589a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno, 152689a89ebfSlling msg)); 152789a89ebfSlling } 152889a89ebfSlling } 1529fa9e4066Sahrens 1530fa9e4066Sahrens return (0); 1531fa9e4066Sahrens } 1532fa9e4066Sahrens 1533394ab0cbSGeorge Wilson int 15344445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str) 1535394ab0cbSGeorge Wilson { 15364445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str)); 1537394ab0cbSGeorge Wilson } 1538394ab0cbSGeorge Wilson 1539394ab0cbSGeorge Wilson int 15404445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str) 1541394ab0cbSGeorge Wilson { 15424445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str)); 1543394ab0cbSGeorge Wilson } 1544394ab0cbSGeorge Wilson 1545468c413aSTim Haley static void 1546468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun, 15474b964adaSGeorge Wilson nvlist_t *config) 1548468c413aSTim Haley { 15494b964adaSGeorge Wilson nvlist_t *nv = NULL; 1550468c413aSTim Haley uint64_t rewindto; 1551468c413aSTim Haley int64_t loss = -1; 1552468c413aSTim Haley struct tm t; 1553468c413aSTim Haley char timestr[128]; 1554468c413aSTim Haley 15554b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL) 15564b964adaSGeorge Wilson return; 15574b964adaSGeorge Wilson 1558ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1559ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) { 1560468c413aSTim Haley return; 1561ad135b5dSChristopher Siden } 1562468c413aSTim Haley 15634b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1564468c413aSTim Haley return; 15654b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 1566468c413aSTim Haley 1567468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1568468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1569468c413aSTim Haley if (dryrun) { 1570468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1571468c413aSTim Haley "Would be able to return %s " 1572468c413aSTim Haley "to its state as of %s.\n"), 1573468c413aSTim Haley name, timestr); 1574468c413aSTim Haley } else { 1575468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1576468c413aSTim Haley "Pool %s returned to its state as of %s.\n"), 1577468c413aSTim Haley name, timestr); 1578468c413aSTim Haley } 1579468c413aSTim Haley if (loss > 120) { 1580468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1581468c413aSTim Haley "%s approximately %lld "), 1582468c413aSTim Haley dryrun ? "Would discard" : "Discarded", 1583468c413aSTim Haley (loss + 30) / 60); 1584468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1585468c413aSTim Haley "minutes of transactions.\n")); 1586468c413aSTim Haley } else if (loss > 0) { 1587468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1588468c413aSTim Haley "%s approximately %lld "), 1589468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss); 1590468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1591468c413aSTim Haley "seconds of transactions.\n")); 1592468c413aSTim Haley } 1593468c413aSTim Haley } 1594468c413aSTim Haley } 1595468c413aSTim Haley 1596468c413aSTim Haley void 1597468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason, 1598468c413aSTim Haley nvlist_t *config) 1599468c413aSTim Haley { 16004b964adaSGeorge Wilson nvlist_t *nv = NULL; 1601468c413aSTim Haley int64_t loss = -1; 1602468c413aSTim Haley uint64_t edata = UINT64_MAX; 1603468c413aSTim Haley uint64_t rewindto; 1604468c413aSTim Haley struct tm t; 1605468c413aSTim Haley char timestr[128]; 1606468c413aSTim Haley 1607468c413aSTim Haley if (!hdl->libzfs_printerr) 1608468c413aSTim Haley return; 1609468c413aSTim Haley 1610468c413aSTim Haley if (reason >= 0) 1611468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: ")); 1612468c413aSTim Haley else 1613468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t")); 1614468c413aSTim Haley 1615468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */ 16164b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 || 1617ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 || 16184b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0) 1619468c413aSTim Haley goto no_info; 1620468c413aSTim Haley 16214b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss); 16224b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 1623468c413aSTim Haley &edata); 1624468c413aSTim Haley 1625468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1626468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n")); 1627468c413aSTim Haley 1628468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL && 1629468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) { 1630468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1631468c413aSTim Haley "\tReturning the pool to its state as of %s\n" 1632468c413aSTim Haley "\tshould correct the problem. "), 1633468c413aSTim Haley timestr); 1634468c413aSTim Haley } else { 1635468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1636468c413aSTim Haley "\tReverting the pool to an earlier state " 1637468c413aSTim Haley "should correct the problem.\n\t")); 1638468c413aSTim Haley } 1639468c413aSTim Haley 1640468c413aSTim Haley if (loss > 120) { 1641468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1642468c413aSTim Haley "Approximately %lld minutes of data\n" 1643468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60); 1644468c413aSTim Haley } else if (loss > 0) { 1645468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1646468c413aSTim Haley "Approximately %lld seconds of data\n" 1647468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss); 1648468c413aSTim Haley } 1649468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) { 1650468c413aSTim Haley if (edata == 1) { 1651468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1652468c413aSTim Haley "After rewind, at least\n" 1653468c413aSTim Haley "\tone persistent user-data error will remain. ")); 1654468c413aSTim Haley } else { 1655468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1656468c413aSTim Haley "After rewind, several\n" 1657468c413aSTim Haley "\tpersistent user-data errors will remain. ")); 1658468c413aSTim Haley } 1659468c413aSTim Haley } 1660468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1661a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "), 1662a33cae98STim Haley reason >= 0 ? "clear" : "import", name); 1663468c413aSTim Haley 1664468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1665468c413aSTim Haley "A scrub of the pool\n" 1666468c413aSTim Haley "\tis strongly recommended after recovery.\n")); 1667468c413aSTim Haley return; 1668468c413aSTim Haley 1669468c413aSTim Haley no_info: 1670468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, 1671468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n")); 1672468c413aSTim Haley } 1673468c413aSTim Haley 1674fa9e4066Sahrens /* 1675990b4856Slling * zpool_import() is a contracted interface. Should be kept the same 1676990b4856Slling * if possible. 1677990b4856Slling * 1678990b4856Slling * Applications should use zpool_import_props() to import a pool with 1679990b4856Slling * new properties value to be set. 1680fa9e4066Sahrens */ 1681fa9e4066Sahrens int 168299653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 1683990b4856Slling char *altroot) 1684990b4856Slling { 1685990b4856Slling nvlist_t *props = NULL; 1686990b4856Slling int ret; 1687990b4856Slling 1688990b4856Slling if (altroot != NULL) { 1689990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 1690990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1691990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1692990b4856Slling newname)); 1693990b4856Slling } 1694990b4856Slling 1695990b4856Slling if (nvlist_add_string(props, 1696352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 || 1697352d8027SGeorge Wilson nvlist_add_string(props, 1698352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) { 1699990b4856Slling nvlist_free(props); 1700990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM, 1701990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1702990b4856Slling newname)); 1703990b4856Slling } 1704990b4856Slling } 1705990b4856Slling 17064b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props, 17074b964adaSGeorge Wilson ZFS_IMPORT_NORMAL); 1708aab83bb8SJosef 'Jeff' Sipek nvlist_free(props); 1709990b4856Slling return (ret); 1710990b4856Slling } 1711990b4856Slling 17124b964adaSGeorge Wilson static void 17134b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv, 17144b964adaSGeorge Wilson int indent) 17154b964adaSGeorge Wilson { 17164b964adaSGeorge Wilson nvlist_t **child; 17174b964adaSGeorge Wilson uint_t c, children; 17184b964adaSGeorge Wilson char *vname; 17194b964adaSGeorge Wilson uint64_t is_log = 0; 17204b964adaSGeorge Wilson 17214b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, 17224b964adaSGeorge Wilson &is_log); 17234b964adaSGeorge Wilson 17244b964adaSGeorge Wilson if (name != NULL) 17254b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name, 17264b964adaSGeorge Wilson is_log ? " [log]" : ""); 17274b964adaSGeorge Wilson 17284b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 17294b964adaSGeorge Wilson &child, &children) != 0) 17304b964adaSGeorge Wilson return; 17314b964adaSGeorge Wilson 17324b964adaSGeorge Wilson for (c = 0; c < children; c++) { 1733*663207adSDon Brady vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID); 17344b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2); 17354b964adaSGeorge Wilson free(vname); 17364b964adaSGeorge Wilson } 17374b964adaSGeorge Wilson } 17384b964adaSGeorge Wilson 1739ad135b5dSChristopher Siden void 1740ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config) 1741ad135b5dSChristopher Siden { 1742ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat; 1743ad135b5dSChristopher Siden 1744ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 1745ad135b5dSChristopher Siden 0); 1746ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT, 1747ad135b5dSChristopher Siden &unsup_feat) == 0); 1748ad135b5dSChristopher Siden 1749ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL; 1750ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) { 1751ad135b5dSChristopher Siden char *desc; 1752ad135b5dSChristopher Siden 1753ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING); 1754ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0); 1755ad135b5dSChristopher Siden 1756ad135b5dSChristopher Siden if (strlen(desc) > 0) 1757ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc); 1758ad135b5dSChristopher Siden else 1759ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp)); 1760ad135b5dSChristopher Siden } 1761ad135b5dSChristopher Siden } 1762ad135b5dSChristopher Siden 1763990b4856Slling /* 1764990b4856Slling * Import the given pool using the known configuration and a list of 1765990b4856Slling * properties to be set. The configuration should have come from 1766990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool 1767990b4856Slling * is imported with a different name. 1768990b4856Slling */ 1769990b4856Slling int 1770990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, 17714b964adaSGeorge Wilson nvlist_t *props, int flags) 1772fa9e4066Sahrens { 1773e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 17745dafeea3SPavel Zakharov zpool_load_policy_t policy; 17754b964adaSGeorge Wilson nvlist_t *nv = NULL; 17764b964adaSGeorge Wilson nvlist_t *nvinfo = NULL; 17774b964adaSGeorge Wilson nvlist_t *missing = NULL; 1778fa9e4066Sahrens char *thename; 1779fa9e4066Sahrens char *origname; 1780fa9e4066Sahrens int ret; 17814b964adaSGeorge Wilson int error = 0; 1782990b4856Slling char errbuf[1024]; 1783fa9e4066Sahrens 1784fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 1785fa9e4066Sahrens &origname) == 0); 1786fa9e4066Sahrens 1787990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1788990b4856Slling "cannot import pool '%s'"), origname); 1789990b4856Slling 1790fa9e4066Sahrens if (newname != NULL) { 179199653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname)) 1792ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME, 179399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"), 179499653d4eSeschrock newname)); 1795fa9e4066Sahrens thename = (char *)newname; 1796fa9e4066Sahrens } else { 1797fa9e4066Sahrens thename = origname; 1798fa9e4066Sahrens } 1799fa9e4066Sahrens 1800078266a5SMarcel Telka if (props != NULL) { 1801990b4856Slling uint64_t version; 1802f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 1803fa9e4066Sahrens 1804990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 1805990b4856Slling &version) == 0); 1806fa9e4066Sahrens 18070a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname, 1808078266a5SMarcel Telka props, version, flags, errbuf)) == NULL) 1809990b4856Slling return (-1); 1810078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 1811351420b3Slling nvlist_free(props); 1812990b4856Slling return (-1); 1813351420b3Slling } 1814078266a5SMarcel Telka nvlist_free(props); 1815990b4856Slling } 1816990b4856Slling 1817990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); 1818fa9e4066Sahrens 1819fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 1820ea8dc4b6Seschrock &zc.zc_guid) == 0); 1821fa9e4066Sahrens 1822351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) { 1823078266a5SMarcel Telka zcmd_free_nvlists(&zc); 182499653d4eSeschrock return (-1); 1825351420b3Slling } 182657f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) { 1827078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1828468c413aSTim Haley return (-1); 1829468c413aSTim Haley } 1830fa9e4066Sahrens 18314b964adaSGeorge Wilson zc.zc_cookie = flags; 18324b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 && 18334b964adaSGeorge Wilson errno == ENOMEM) { 18344b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 18354b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 18364b964adaSGeorge Wilson return (-1); 18374b964adaSGeorge Wilson } 18384b964adaSGeorge Wilson } 18394b964adaSGeorge Wilson if (ret != 0) 18404b964adaSGeorge Wilson error = errno; 18414b964adaSGeorge Wilson 18424b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv); 1843078266a5SMarcel Telka 1844078266a5SMarcel Telka zcmd_free_nvlists(&zc); 1845078266a5SMarcel Telka 18465dafeea3SPavel Zakharov zpool_get_load_policy(config, &policy); 18474b964adaSGeorge Wilson 18484b964adaSGeorge Wilson if (error) { 1849fa9e4066Sahrens char desc[1024]; 1850e0f1c0afSOlaf Faaland char aux[256]; 1851468c413aSTim Haley 1852468c413aSTim Haley /* 1853468c413aSTim Haley * Dry-run failed, but we print out what success 1854468c413aSTim Haley * looks like if we found a best txg 1855468c413aSTim Haley */ 18565dafeea3SPavel Zakharov if (policy.zlp_rewind & ZPOOL_TRY_REWIND) { 1857468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 18584b964adaSGeorge Wilson B_TRUE, nv); 18594b964adaSGeorge Wilson nvlist_free(nv); 1860468c413aSTim Haley return (-1); 1861468c413aSTim Haley } 1862468c413aSTim Haley 1863fa9e4066Sahrens if (newname == NULL) 1864fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1865fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"), 1866fa9e4066Sahrens thename); 1867fa9e4066Sahrens else 1868fa9e4066Sahrens (void) snprintf(desc, sizeof (desc), 1869fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), 1870fa9e4066Sahrens origname, thename); 1871fa9e4066Sahrens 18724b964adaSGeorge Wilson switch (error) { 1873ea8dc4b6Seschrock case ENOTSUP: 1874ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv, 1875ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 1876ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) { 1877ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This " 1878ad135b5dSChristopher Siden "pool uses the following feature(s) not " 1879ad135b5dSChristopher Siden "supported by this system:\n")); 1880ad135b5dSChristopher Siden zpool_print_unsup_feat(nv); 1881ad135b5dSChristopher Siden if (nvlist_exists(nvinfo, 1882ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) { 1883ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, 1884ad135b5dSChristopher Siden "All unsupported features are only " 1885ad135b5dSChristopher Siden "required for writing to the pool." 1886ad135b5dSChristopher Siden "\nThe pool can be imported using " 1887ad135b5dSChristopher Siden "'-o readonly=on'.\n")); 1888ad135b5dSChristopher Siden } 1889ad135b5dSChristopher Siden } 1890ea8dc4b6Seschrock /* 1891ea8dc4b6Seschrock * Unsupported version. 1892ea8dc4b6Seschrock */ 189399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc); 1894ea8dc4b6Seschrock break; 1895ea8dc4b6Seschrock 1896e0f1c0afSOlaf Faaland case EREMOTEIO: 1897e0f1c0afSOlaf Faaland if (nv != NULL && nvlist_lookup_nvlist(nv, 1898e0f1c0afSOlaf Faaland ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) { 1899e0f1c0afSOlaf Faaland char *hostname = "<unknown>"; 1900e0f1c0afSOlaf Faaland uint64_t hostid = 0; 1901e0f1c0afSOlaf Faaland mmp_state_t mmp_state; 1902e0f1c0afSOlaf Faaland 1903e0f1c0afSOlaf Faaland mmp_state = fnvlist_lookup_uint64(nvinfo, 1904e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_STATE); 1905e0f1c0afSOlaf Faaland 1906e0f1c0afSOlaf Faaland if (nvlist_exists(nvinfo, 1907e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTNAME)) 1908e0f1c0afSOlaf Faaland hostname = fnvlist_lookup_string(nvinfo, 1909e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTNAME); 1910e0f1c0afSOlaf Faaland 1911e0f1c0afSOlaf Faaland if (nvlist_exists(nvinfo, 1912e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTID)) 1913e0f1c0afSOlaf Faaland hostid = fnvlist_lookup_uint64(nvinfo, 1914e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTID); 1915e0f1c0afSOlaf Faaland 1916e0f1c0afSOlaf Faaland if (mmp_state == MMP_STATE_ACTIVE) { 1917e0f1c0afSOlaf Faaland (void) snprintf(aux, sizeof (aux), 1918e0f1c0afSOlaf Faaland dgettext(TEXT_DOMAIN, "pool is imp" 1919e0f1c0afSOlaf Faaland "orted on host '%s' (hostid=%lx).\n" 1920e0f1c0afSOlaf Faaland "Export the pool on the other " 1921e0f1c0afSOlaf Faaland "system, then run 'zpool import'."), 1922e0f1c0afSOlaf Faaland hostname, (unsigned long) hostid); 1923e0f1c0afSOlaf Faaland } else if (mmp_state == MMP_STATE_NO_HOSTID) { 1924e0f1c0afSOlaf Faaland (void) snprintf(aux, sizeof (aux), 1925e0f1c0afSOlaf Faaland dgettext(TEXT_DOMAIN, "pool has " 1926e0f1c0afSOlaf Faaland "the multihost property on and " 1927e0f1c0afSOlaf Faaland "the\nsystem's hostid is not " 1928e0f1c0afSOlaf Faaland "set.\n")); 1929e0f1c0afSOlaf Faaland } 1930e0f1c0afSOlaf Faaland 1931e0f1c0afSOlaf Faaland (void) zfs_error_aux(hdl, aux); 1932e0f1c0afSOlaf Faaland } 1933e0f1c0afSOlaf Faaland (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc); 1934e0f1c0afSOlaf Faaland break; 1935e0f1c0afSOlaf Faaland 1936b5989ec7Seschrock case EINVAL: 1937b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc); 1938b5989ec7Seschrock break; 1939b5989ec7Seschrock 194054a91118SChris Kirby case EROFS: 194154a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 194254a91118SChris Kirby "one or more devices is read only")); 194354a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc); 194454a91118SChris Kirby break; 194554a91118SChris Kirby 19464b964adaSGeorge Wilson case ENXIO: 19474b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv, 19484b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 && 19494b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo, 19504b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) { 19514b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN, 19526f793812SPavel Zakharov "The devices below are missing or " 19536f793812SPavel Zakharov "corrupted, use '-m' to import the pool " 19546f793812SPavel Zakharov "anyway:\n")); 19554b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2); 19564b964adaSGeorge Wilson (void) printf("\n"); 19574b964adaSGeorge Wilson } 19584b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 19594b964adaSGeorge Wilson break; 19604b964adaSGeorge Wilson 19614b964adaSGeorge Wilson case EEXIST: 19624b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 19634b964adaSGeorge Wilson break; 1964c971037bSPaul Dagnelie case ENAMETOOLONG: 1965c971037bSPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1966c971037bSPaul Dagnelie "new name of at least one dataset is longer than " 1967c971037bSPaul Dagnelie "the maximum allowable length")); 1968c971037bSPaul Dagnelie (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc); 1969c971037bSPaul Dagnelie break; 1970fa9e4066Sahrens default: 19714b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc); 1972468c413aSTim Haley zpool_explain_recover(hdl, 19734b964adaSGeorge Wilson newname ? origname : thename, -error, nv); 1974468c413aSTim Haley break; 1975fa9e4066Sahrens } 1976fa9e4066Sahrens 19774b964adaSGeorge Wilson nvlist_free(nv); 1978fa9e4066Sahrens ret = -1; 1979fa9e4066Sahrens } else { 1980fa9e4066Sahrens zpool_handle_t *zhp; 1981ecd6cf80Smarks 1982fa9e4066Sahrens /* 1983fa9e4066Sahrens * This should never fail, but play it safe anyway. 1984fa9e4066Sahrens */ 1985681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0) 198694de1d4cSeschrock ret = -1; 1987681d9761SEric Taylor else if (zhp != NULL) 1988fa9e4066Sahrens zpool_close(zhp); 19895dafeea3SPavel Zakharov if (policy.zlp_rewind & 1990468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 1991468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename, 19925dafeea3SPavel Zakharov ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv); 1993468c413aSTim Haley } 19944b964adaSGeorge Wilson nvlist_free(nv); 1995468c413aSTim Haley return (0); 1996fa9e4066Sahrens } 1997fa9e4066Sahrens 1998fa9e4066Sahrens return (ret); 1999fa9e4066Sahrens } 2000fa9e4066Sahrens 2001fa9e4066Sahrens /* 20023f9d6ad7SLin Ling * Scan the pool. 2003fa9e4066Sahrens */ 2004fa9e4066Sahrens int 20051702cce7SAlek Pinchuk zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) 2006fa9e4066Sahrens { 2007fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2008fa9e4066Sahrens char msg[1024]; 20091702cce7SAlek Pinchuk int err; 201099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2011fa9e4066Sahrens 2012fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 20133f9d6ad7SLin Ling zc.zc_cookie = func; 20141702cce7SAlek Pinchuk zc.zc_flags = cmd; 2015fa9e4066Sahrens 20161702cce7SAlek Pinchuk if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0) 20171702cce7SAlek Pinchuk return (0); 20181702cce7SAlek Pinchuk 20191702cce7SAlek Pinchuk err = errno; 20201702cce7SAlek Pinchuk 20211702cce7SAlek Pinchuk /* ECANCELED on a scrub means we resumed a paused scrub */ 20221702cce7SAlek Pinchuk if (err == ECANCELED && func == POOL_SCAN_SCRUB && 20231702cce7SAlek Pinchuk cmd == POOL_SCRUB_NORMAL) 20241702cce7SAlek Pinchuk return (0); 20251702cce7SAlek Pinchuk 20261702cce7SAlek Pinchuk if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL) 2027fa9e4066Sahrens return (0); 2028fa9e4066Sahrens 20293f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) { 20301702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) { 20311702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 20321702cce7SAlek Pinchuk "cannot pause scrubbing %s"), zc.zc_name); 20331702cce7SAlek Pinchuk } else { 20341702cce7SAlek Pinchuk assert(cmd == POOL_SCRUB_NORMAL); 20351702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 20361702cce7SAlek Pinchuk "cannot scrub %s"), zc.zc_name); 20371702cce7SAlek Pinchuk } 20383f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) { 20393f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg), 20403f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"), 20413f9d6ad7SLin Ling zc.zc_name); 20423f9d6ad7SLin Ling } else { 20433f9d6ad7SLin Ling assert(!"unexpected result"); 20443f9d6ad7SLin Ling } 2045fa9e4066Sahrens 20461702cce7SAlek Pinchuk if (err == EBUSY) { 20473f9d6ad7SLin Ling nvlist_t *nvroot; 20483f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL; 20493f9d6ad7SLin Ling uint_t psc; 20503f9d6ad7SLin Ling 20513f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config, 20523f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 20533f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot, 20543f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc); 20551702cce7SAlek Pinchuk if (ps && ps->pss_func == POOL_SCAN_SCRUB) { 20561702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) 20571702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg)); 20581702cce7SAlek Pinchuk else 20591702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUBBING, msg)); 20601702cce7SAlek Pinchuk } else { 20613f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg)); 20621702cce7SAlek Pinchuk } 20631702cce7SAlek Pinchuk } else if (err == ENOENT) { 20643f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg)); 20653f9d6ad7SLin Ling } else { 20661702cce7SAlek Pinchuk return (zpool_standard_error(hdl, err, msg)); 20673f9d6ad7SLin Ling } 2068fa9e4066Sahrens } 2069fa9e4066Sahrens 2070094e47e9SGeorge Wilson static int 2071094e47e9SGeorge Wilson xlate_init_err(int err) 2072094e47e9SGeorge Wilson { 2073094e47e9SGeorge Wilson switch (err) { 2074094e47e9SGeorge Wilson case ENODEV: 2075094e47e9SGeorge Wilson return (EZFS_NODEVICE); 2076094e47e9SGeorge Wilson case EINVAL: 2077094e47e9SGeorge Wilson case EROFS: 2078094e47e9SGeorge Wilson return (EZFS_BADDEV); 2079094e47e9SGeorge Wilson case EBUSY: 2080094e47e9SGeorge Wilson return (EZFS_INITIALIZING); 2081094e47e9SGeorge Wilson case ESRCH: 2082094e47e9SGeorge Wilson return (EZFS_NO_INITIALIZE); 2083094e47e9SGeorge Wilson } 2084094e47e9SGeorge Wilson return (err); 2085094e47e9SGeorge Wilson } 2086094e47e9SGeorge Wilson 2087094e47e9SGeorge Wilson /* 2088094e47e9SGeorge Wilson * Begin, suspend, or cancel the initialization (initializing of all free 2089094e47e9SGeorge Wilson * blocks) for the given vdevs in the given pool. 2090094e47e9SGeorge Wilson */ 2091094e47e9SGeorge Wilson int 2092094e47e9SGeorge Wilson zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type, 2093094e47e9SGeorge Wilson nvlist_t *vds) 2094094e47e9SGeorge Wilson { 2095094e47e9SGeorge Wilson char msg[1024]; 2096094e47e9SGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 2097094e47e9SGeorge Wilson 2098094e47e9SGeorge Wilson nvlist_t *errlist; 2099094e47e9SGeorge Wilson 2100094e47e9SGeorge Wilson /* translate vdev names to guids */ 2101094e47e9SGeorge Wilson nvlist_t *vdev_guids = fnvlist_alloc(); 2102094e47e9SGeorge Wilson nvlist_t *guids_to_paths = fnvlist_alloc(); 2103094e47e9SGeorge Wilson boolean_t spare, cache; 2104094e47e9SGeorge Wilson nvlist_t *tgt; 2105094e47e9SGeorge Wilson nvpair_t *elem; 2106094e47e9SGeorge Wilson 2107094e47e9SGeorge Wilson for (elem = nvlist_next_nvpair(vds, NULL); elem != NULL; 2108094e47e9SGeorge Wilson elem = nvlist_next_nvpair(vds, elem)) { 2109094e47e9SGeorge Wilson char *vd_path = nvpair_name(elem); 2110094e47e9SGeorge Wilson tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache, NULL); 2111094e47e9SGeorge Wilson 2112094e47e9SGeorge Wilson if ((tgt == NULL) || cache || spare) { 2113094e47e9SGeorge Wilson (void) snprintf(msg, sizeof (msg), 2114094e47e9SGeorge Wilson dgettext(TEXT_DOMAIN, "cannot initialize '%s'"), 2115094e47e9SGeorge Wilson vd_path); 2116094e47e9SGeorge Wilson int err = (tgt == NULL) ? EZFS_NODEVICE : 2117094e47e9SGeorge Wilson (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE); 2118094e47e9SGeorge Wilson fnvlist_free(vdev_guids); 2119094e47e9SGeorge Wilson fnvlist_free(guids_to_paths); 2120094e47e9SGeorge Wilson return (zfs_error(hdl, err, msg)); 2121094e47e9SGeorge Wilson } 2122094e47e9SGeorge Wilson 2123094e47e9SGeorge Wilson uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 2124094e47e9SGeorge Wilson fnvlist_add_uint64(vdev_guids, vd_path, guid); 2125094e47e9SGeorge Wilson 2126094e47e9SGeorge Wilson (void) snprintf(msg, sizeof (msg), "%llu", guid); 2127094e47e9SGeorge Wilson fnvlist_add_string(guids_to_paths, msg, vd_path); 2128094e47e9SGeorge Wilson } 2129094e47e9SGeorge Wilson 2130094e47e9SGeorge Wilson int err = lzc_initialize(zhp->zpool_name, cmd_type, vdev_guids, 2131094e47e9SGeorge Wilson &errlist); 2132094e47e9SGeorge Wilson fnvlist_free(vdev_guids); 2133094e47e9SGeorge Wilson 2134094e47e9SGeorge Wilson if (err == 0) { 2135094e47e9SGeorge Wilson fnvlist_free(guids_to_paths); 2136094e47e9SGeorge Wilson return (0); 2137094e47e9SGeorge Wilson } 2138094e47e9SGeorge Wilson 2139094e47e9SGeorge Wilson nvlist_t *vd_errlist = NULL; 2140094e47e9SGeorge Wilson if (errlist != NULL) { 2141094e47e9SGeorge Wilson vd_errlist = fnvlist_lookup_nvlist(errlist, 2142094e47e9SGeorge Wilson ZPOOL_INITIALIZE_VDEVS); 2143094e47e9SGeorge Wilson } 2144094e47e9SGeorge Wilson 2145094e47e9SGeorge Wilson (void) snprintf(msg, sizeof (msg), 2146094e47e9SGeorge Wilson dgettext(TEXT_DOMAIN, "operation failed")); 2147094e47e9SGeorge Wilson 2148094e47e9SGeorge Wilson for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL; 2149094e47e9SGeorge Wilson elem = nvlist_next_nvpair(vd_errlist, elem)) { 2150094e47e9SGeorge Wilson int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem)); 2151094e47e9SGeorge Wilson char *path = fnvlist_lookup_string(guids_to_paths, 2152094e47e9SGeorge Wilson nvpair_name(elem)); 2153094e47e9SGeorge Wilson (void) zfs_error_fmt(hdl, vd_error, "cannot initialize '%s'", 2154094e47e9SGeorge Wilson path); 2155094e47e9SGeorge Wilson } 2156094e47e9SGeorge Wilson 2157094e47e9SGeorge Wilson fnvlist_free(guids_to_paths); 2158094e47e9SGeorge Wilson if (vd_errlist != NULL) 2159094e47e9SGeorge Wilson return (-1); 2160094e47e9SGeorge Wilson 2161094e47e9SGeorge Wilson return (zpool_standard_error(hdl, err, msg)); 2162094e47e9SGeorge Wilson } 2163094e47e9SGeorge Wilson 21643fdda499SJohn Harres /* 21653fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a 21663fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own 21673fdda499SJohn Harres * verification of the s# part. 21683fdda499SJohn Harres */ 21693fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1])) 21703fdda499SJohn Harres 21713fdda499SJohn Harres /* 21723fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/" 21733fdda499SJohn Harres * and the like. 21743fdda499SJohn Harres */ 21753fdda499SJohn Harres static int 21769a686fbcSPaul Dagnelie ctd_check_path(char *str) 21779a686fbcSPaul Dagnelie { 21783fdda499SJohn Harres /* 21793fdda499SJohn Harres * If it starts with a slash, check the last component. 21803fdda499SJohn Harres */ 21813fdda499SJohn Harres if (str && str[0] == '/') { 21823fdda499SJohn Harres char *tmp = strrchr(str, '/'); 21833fdda499SJohn Harres 21843fdda499SJohn Harres /* 21853fdda499SJohn Harres * If it ends in "/old", check the second-to-last 21863fdda499SJohn Harres * component of the string instead. 21873fdda499SJohn Harres */ 21883fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) { 21893fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--) 21903fdda499SJohn Harres ; 21913fdda499SJohn Harres } 21923fdda499SJohn Harres str = tmp + 1; 21933fdda499SJohn Harres } 21943fdda499SJohn Harres return (CTD_CHECK(str)); 21953fdda499SJohn Harres } 21963fdda499SJohn Harres 2197a43d325bSek /* 2198573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the 2199573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device. 2200a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL 2201a43d325bSek * spare; but FALSE if its an INUSE spare. 2202a43d325bSek */ 220399653d4eSeschrock static nvlist_t * 2204573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare, 2205573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log) 2206ea8dc4b6Seschrock { 2207ea8dc4b6Seschrock uint_t c, children; 2208ea8dc4b6Seschrock nvlist_t **child; 220999653d4eSeschrock nvlist_t *ret; 2210ee0eb9f2SEric Schrock uint64_t is_log; 2211573ca77eSGeorge Wilson char *srchkey; 2212573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL); 2213573ca77eSGeorge Wilson 2214573ca77eSGeorge Wilson /* Nothing to look for */ 2215573ca77eSGeorge Wilson if (search == NULL || pair == NULL) 2216573ca77eSGeorge Wilson return (NULL); 2217ea8dc4b6Seschrock 2218573ca77eSGeorge Wilson /* Obtain the key we will use to search */ 2219573ca77eSGeorge Wilson srchkey = nvpair_name(pair); 2220573ca77eSGeorge Wilson 2221573ca77eSGeorge Wilson switch (nvpair_type(pair)) { 2222cb04b873SMark J Musante case DATA_TYPE_UINT64: 2223573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) { 2224cb04b873SMark J Musante uint64_t srchval, theguid; 2225cb04b873SMark J Musante 2226cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0); 2227cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 2228cb04b873SMark J Musante &theguid) == 0); 2229cb04b873SMark J Musante if (theguid == srchval) 2230cb04b873SMark J Musante return (nv); 2231573ca77eSGeorge Wilson } 2232573ca77eSGeorge Wilson break; 2233573ca77eSGeorge Wilson 2234573ca77eSGeorge Wilson case DATA_TYPE_STRING: { 2235573ca77eSGeorge Wilson char *srchval, *val; 2236573ca77eSGeorge Wilson 2237573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0); 2238573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0) 2239573ca77eSGeorge Wilson break; 2240ea8dc4b6Seschrock 2241ea8dc4b6Seschrock /* 22423fdda499SJohn Harres * Search for the requested value. Special cases: 22433fdda499SJohn Harres * 22447855d95bSToomas Soome * - ZPOOL_CONFIG_PATH for whole disk entries. To support 22457855d95bSToomas Soome * UEFI boot, these end in "s0" or "s0/old" or "s1" or 22467855d95bSToomas Soome * "s1/old". The "s0" or "s1" part is hidden from the user, 22473fdda499SJohn Harres * but included in the string, so this matches around it. 22483fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). 22493fdda499SJohn Harres * 225088ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares. 2251ea8dc4b6Seschrock */ 22523fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 && 22533fdda499SJohn Harres ctd_check_path(val)) { 2254573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2255573ca77eSGeorge Wilson 2256573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, 2257573ca77eSGeorge Wilson &wholedisk); 2258573ca77eSGeorge Wilson if (wholedisk) { 22593fdda499SJohn Harres int slen = strlen(srchval); 22603fdda499SJohn Harres int vlen = strlen(val); 22613fdda499SJohn Harres 22623fdda499SJohn Harres if (slen != vlen - 2) 22633fdda499SJohn Harres break; 22643fdda499SJohn Harres 22653fdda499SJohn Harres /* 22663fdda499SJohn Harres * make_leaf_vdev() should only set 22673fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which 22683fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of 22693fdda499SJohn Harres * room for the indices used next. 22703fdda499SJohn Harres */ 22713fdda499SJohn Harres ASSERT(vlen >= 6); 22723fdda499SJohn Harres 22733fdda499SJohn Harres /* 22743fdda499SJohn Harres * strings identical except trailing "s0" 22753fdda499SJohn Harres */ 22767855d95bSToomas Soome if ((strcmp(&val[vlen - 2], "s0") == 0 || 22777855d95bSToomas Soome strcmp(&val[vlen - 2], "s1") == 0) && 22783fdda499SJohn Harres strncmp(srchval, val, slen) == 0) 22793fdda499SJohn Harres return (nv); 22803fdda499SJohn Harres 2281573ca77eSGeorge Wilson /* 22823fdda499SJohn Harres * strings identical except trailing "s0/old" 2283573ca77eSGeorge Wilson */ 22847855d95bSToomas Soome if ((strcmp(&val[vlen - 6], "s0/old") == 0 || 22857855d95bSToomas Soome strcmp(&val[vlen - 6], "s1/old") == 0) && 22863fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 && 22873fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0) 2288573ca77eSGeorge Wilson return (nv); 22893fdda499SJohn Harres 2290573ca77eSGeorge Wilson break; 2291573ca77eSGeorge Wilson } 229288ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { 229388ecc943SGeorge Wilson char *type, *idx, *end, *p; 229488ecc943SGeorge Wilson uint64_t id, vdev_id; 229588ecc943SGeorge Wilson 229688ecc943SGeorge Wilson /* 229788ecc943SGeorge Wilson * Determine our vdev type, keeping in mind 229888ecc943SGeorge Wilson * that the srchval is composed of a type and 229988ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4). 230088ecc943SGeorge Wilson */ 230188ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL) 230288ecc943SGeorge Wilson return (NULL); 230388ecc943SGeorge Wilson 230488ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) { 230588ecc943SGeorge Wilson free(type); 230688ecc943SGeorge Wilson break; 230788ecc943SGeorge Wilson } 230888ecc943SGeorge Wilson idx = p + 1; 230988ecc943SGeorge Wilson *p = '\0'; 231088ecc943SGeorge Wilson 231188ecc943SGeorge Wilson /* 231288ecc943SGeorge Wilson * If the types don't match then keep looking. 231388ecc943SGeorge Wilson */ 231488ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) { 231588ecc943SGeorge Wilson free(type); 231688ecc943SGeorge Wilson break; 231788ecc943SGeorge Wilson } 231888ecc943SGeorge Wilson 23192ba5f978SAlan Somers verify(zpool_vdev_is_interior(type)); 232088ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 232188ecc943SGeorge Wilson &id) == 0); 232288ecc943SGeorge Wilson 232388ecc943SGeorge Wilson errno = 0; 232488ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10); 232588ecc943SGeorge Wilson 232688ecc943SGeorge Wilson free(type); 232788ecc943SGeorge Wilson if (errno != 0) 232888ecc943SGeorge Wilson return (NULL); 232988ecc943SGeorge Wilson 233088ecc943SGeorge Wilson /* 233188ecc943SGeorge Wilson * Now verify that we have the correct vdev id. 233288ecc943SGeorge Wilson */ 233388ecc943SGeorge Wilson if (vdev_id == id) 233488ecc943SGeorge Wilson return (nv); 2335ea8dc4b6Seschrock } 2336573ca77eSGeorge Wilson 2337573ca77eSGeorge Wilson /* 2338573ca77eSGeorge Wilson * Common case 2339573ca77eSGeorge Wilson */ 2340573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0) 2341573ca77eSGeorge Wilson return (nv); 2342573ca77eSGeorge Wilson break; 2343573ca77eSGeorge Wilson } 2344573ca77eSGeorge Wilson 2345573ca77eSGeorge Wilson default: 2346573ca77eSGeorge Wilson break; 2347ea8dc4b6Seschrock } 2348ea8dc4b6Seschrock 2349ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 2350ea8dc4b6Seschrock &child, &children) != 0) 235199653d4eSeschrock return (NULL); 2352ea8dc4b6Seschrock 2353ee0eb9f2SEric Schrock for (c = 0; c < children; c++) { 2354573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2355ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2356ee0eb9f2SEric Schrock /* 2357ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel 2358ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the 2359ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where 2360ee0eb9f2SEric Schrock * 'log' is non-NULL). 2361ee0eb9f2SEric Schrock */ 2362ee0eb9f2SEric Schrock if (log != NULL && 2363ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c], 2364ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 && 2365ee0eb9f2SEric Schrock is_log) { 2366ee0eb9f2SEric Schrock *log = B_TRUE; 2367ee0eb9f2SEric Schrock } 2368ea8dc4b6Seschrock return (ret); 2369ee0eb9f2SEric Schrock } 2370ee0eb9f2SEric Schrock } 2371ea8dc4b6Seschrock 237299653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, 237399653d4eSeschrock &child, &children) == 0) { 237499653d4eSeschrock for (c = 0; c < children; c++) { 2375573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2376ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2377a43d325bSek *avail_spare = B_TRUE; 237899653d4eSeschrock return (ret); 237999653d4eSeschrock } 238099653d4eSeschrock } 238199653d4eSeschrock } 238299653d4eSeschrock 2383fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE, 2384fa94a07fSbrendan &child, &children) == 0) { 2385fa94a07fSbrendan for (c = 0; c < children; c++) { 2386573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search, 2387ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) { 2388fa94a07fSbrendan *l2cache = B_TRUE; 2389fa94a07fSbrendan return (ret); 2390fa94a07fSbrendan } 2391fa94a07fSbrendan } 2392fa94a07fSbrendan } 2393fa94a07fSbrendan 239499653d4eSeschrock return (NULL); 2395ea8dc4b6Seschrock } 2396ea8dc4b6Seschrock 2397573ca77eSGeorge Wilson /* 2398573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the 2399573ca77eSGeorge Wilson * associated vdev. 2400573ca77eSGeorge Wilson */ 2401573ca77eSGeorge Wilson nvlist_t * 2402573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath, 2403573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log) 2404573ca77eSGeorge Wilson { 2405573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret; 2406573ca77eSGeorge Wilson 2407573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2408573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0); 2409573ca77eSGeorge Wilson 2410573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2411573ca77eSGeorge Wilson &nvroot) == 0); 2412573ca77eSGeorge Wilson 2413573ca77eSGeorge Wilson *avail_spare = B_FALSE; 2414cb04b873SMark J Musante *l2cache = B_FALSE; 2415daeb70e5SMark J Musante if (log != NULL) 2416daeb70e5SMark J Musante *log = B_FALSE; 2417573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2418573ca77eSGeorge Wilson nvlist_free(search); 2419573ca77eSGeorge Wilson 2420573ca77eSGeorge Wilson return (ret); 2421573ca77eSGeorge Wilson } 2422573ca77eSGeorge Wilson 242388ecc943SGeorge Wilson /* 242488ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz). 242588ecc943SGeorge Wilson */ 24262ba5f978SAlan Somers static boolean_t 242788ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name) 242888ecc943SGeorge Wilson { 242988ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 || 24302ba5f978SAlan Somers strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 || 24312ba5f978SAlan Somers strncmp(name, 24322ba5f978SAlan Somers VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 || 243388ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0) 243488ecc943SGeorge Wilson return (B_TRUE); 243588ecc943SGeorge Wilson return (B_FALSE); 243688ecc943SGeorge Wilson } 243788ecc943SGeorge Wilson 243899653d4eSeschrock nvlist_t * 2439fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, 2440ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log) 2441ea8dc4b6Seschrock { 2442ea8dc4b6Seschrock char buf[MAXPATHLEN]; 2443ea8dc4b6Seschrock char *end; 2444573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret; 2445ea8dc4b6Seschrock uint64_t guid; 2446ea8dc4b6Seschrock 2447573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0); 2448573ca77eSGeorge Wilson 24490917b783Seschrock guid = strtoull(path, &end, 10); 2450ea8dc4b6Seschrock if (guid != 0 && *end == '\0') { 2451573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); 245288ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) { 245388ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); 2454ea8dc4b6Seschrock } else if (path[0] != '/') { 24556401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT, 24566401734dSWill Andrews path); 2457573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0); 2458ea8dc4b6Seschrock } else { 2459573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); 2460ea8dc4b6Seschrock } 2461ea8dc4b6Seschrock 2462ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 2463ea8dc4b6Seschrock &nvroot) == 0); 2464ea8dc4b6Seschrock 2465a43d325bSek *avail_spare = B_FALSE; 2466fa94a07fSbrendan *l2cache = B_FALSE; 2467ee0eb9f2SEric Schrock if (log != NULL) 2468ee0eb9f2SEric Schrock *log = B_FALSE; 2469573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log); 2470573ca77eSGeorge Wilson nvlist_free(search); 2471573ca77eSGeorge Wilson 2472573ca77eSGeorge Wilson return (ret); 2473a43d325bSek } 2474a43d325bSek 247519397407SSherry Moore static int 2476e0f1c0afSOlaf Faaland vdev_is_online(nvlist_t *nv) 247719397407SSherry Moore { 247819397407SSherry Moore uint64_t ival; 247919397407SSherry Moore 248019397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 || 248119397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 || 248219397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0) 248319397407SSherry Moore return (0); 248419397407SSherry Moore 248519397407SSherry Moore return (1); 248619397407SSherry Moore } 248719397407SSherry Moore 248819397407SSherry Moore /* 248921ecdf64SLin Ling * Helper function for zpool_get_physpaths(). 249019397407SSherry Moore */ 2491753a6d45SSherry Moore static int 249221ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size, 2493753a6d45SSherry Moore size_t *bytes_written) 249419397407SSherry Moore { 2495753a6d45SSherry Moore size_t bytes_left, pos, rsz; 2496753a6d45SSherry Moore char *tmppath; 2497753a6d45SSherry Moore const char *format; 2498753a6d45SSherry Moore 2499753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH, 2500753a6d45SSherry Moore &tmppath) != 0) 2501753a6d45SSherry Moore return (EZFS_NODEVICE); 2502753a6d45SSherry Moore 2503753a6d45SSherry Moore pos = *bytes_written; 2504753a6d45SSherry Moore bytes_left = physpath_size - pos; 2505753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s"; 2506753a6d45SSherry Moore 2507753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath); 2508753a6d45SSherry Moore *bytes_written += rsz; 2509753a6d45SSherry Moore 2510753a6d45SSherry Moore if (rsz >= bytes_left) { 2511753a6d45SSherry Moore /* if physpath was not copied properly, clear it */ 2512753a6d45SSherry Moore if (bytes_left != 0) { 2513753a6d45SSherry Moore physpath[pos] = 0; 2514753a6d45SSherry Moore } 2515753a6d45SSherry Moore return (EZFS_NOSPC); 2516753a6d45SSherry Moore } 2517753a6d45SSherry Moore return (0); 2518753a6d45SSherry Moore } 2519753a6d45SSherry Moore 252021ecdf64SLin Ling static int 252121ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size, 252221ecdf64SLin Ling size_t *rsz, boolean_t is_spare) 252321ecdf64SLin Ling { 252421ecdf64SLin Ling char *type; 252521ecdf64SLin Ling int ret; 252621ecdf64SLin Ling 252721ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0) 252821ecdf64SLin Ling return (EZFS_INVALCONFIG); 252921ecdf64SLin Ling 253021ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) { 253121ecdf64SLin Ling /* 253221ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set. 253321ecdf64SLin Ling * For a spare vdev, we only want to boot from the active 253421ecdf64SLin Ling * spare device. 253521ecdf64SLin Ling */ 253621ecdf64SLin Ling if (is_spare) { 253721ecdf64SLin Ling uint64_t spare = 0; 253821ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 253921ecdf64SLin Ling &spare); 254021ecdf64SLin Ling if (!spare) 254121ecdf64SLin Ling return (EZFS_INVALCONFIG); 254221ecdf64SLin Ling } 254321ecdf64SLin Ling 2544e0f1c0afSOlaf Faaland if (vdev_is_online(nv)) { 254521ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath, 254621ecdf64SLin Ling phypath_size, rsz)) != 0) 254721ecdf64SLin Ling return (ret); 254821ecdf64SLin Ling } 254921ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 || 2550d5f26ad8SToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 || 255121ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 || 255221ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) { 255321ecdf64SLin Ling nvlist_t **child; 255421ecdf64SLin Ling uint_t count; 255521ecdf64SLin Ling int i, ret; 255621ecdf64SLin Ling 255721ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv, 255821ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0) 255921ecdf64SLin Ling return (EZFS_INVALCONFIG); 256021ecdf64SLin Ling 256121ecdf64SLin Ling for (i = 0; i < count; i++) { 256221ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath, 256321ecdf64SLin Ling phypath_size, rsz, is_spare); 256421ecdf64SLin Ling if (ret == EZFS_NOSPC) 256521ecdf64SLin Ling return (ret); 256621ecdf64SLin Ling } 256721ecdf64SLin Ling } 256821ecdf64SLin Ling 256921ecdf64SLin Ling return (EZFS_POOL_INVALARG); 257021ecdf64SLin Ling } 257121ecdf64SLin Ling 2572753a6d45SSherry Moore /* 2573753a6d45SSherry Moore * Get phys_path for a root pool config. 2574753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2575753a6d45SSherry Moore */ 2576753a6d45SSherry Moore static int 2577753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size) 2578753a6d45SSherry Moore { 2579753a6d45SSherry Moore size_t rsz; 258019397407SSherry Moore nvlist_t *vdev_root; 258119397407SSherry Moore nvlist_t **child; 258219397407SSherry Moore uint_t count; 2583753a6d45SSherry Moore char *type; 2584753a6d45SSherry Moore 2585753a6d45SSherry Moore rsz = 0; 2586753a6d45SSherry Moore 2587753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 2588753a6d45SSherry Moore &vdev_root) != 0) 2589753a6d45SSherry Moore return (EZFS_INVALCONFIG); 2590753a6d45SSherry Moore 2591753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 || 2592753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN, 2593753a6d45SSherry Moore &child, &count) != 0) 2594753a6d45SSherry Moore return (EZFS_INVALCONFIG); 259519397407SSherry Moore 259619397407SSherry Moore /* 25971a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev. 259819397407SSherry Moore */ 25991a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1) 2600753a6d45SSherry Moore return (EZFS_POOL_INVALARG); 260119397407SSherry Moore 260221ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz, 260321ecdf64SLin Ling B_FALSE); 260419397407SSherry Moore 2605753a6d45SSherry Moore /* No online devices */ 2606753a6d45SSherry Moore if (rsz == 0) 2607753a6d45SSherry Moore return (EZFS_NODEVICE); 2608753a6d45SSherry Moore 260919397407SSherry Moore return (0); 261019397407SSherry Moore } 261119397407SSherry Moore 2612753a6d45SSherry Moore /* 2613753a6d45SSherry Moore * Get phys_path for a root pool 2614753a6d45SSherry Moore * Return 0 on success; non-zero on failure. 2615753a6d45SSherry Moore */ 2616753a6d45SSherry Moore int 2617753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size) 2618753a6d45SSherry Moore { 2619753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath, 2620753a6d45SSherry Moore phypath_size)); 2621753a6d45SSherry Moore } 2622753a6d45SSherry Moore 2623573ca77eSGeorge Wilson /* 2624573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel 2625573ca77eSGeorge Wilson * the disk to use the new unallocated space. 2626573ca77eSGeorge Wilson */ 2627573ca77eSGeorge Wilson static int 2628573ca77eSGeorge Wilson zpool_relabel_disk(libzfs_handle_t *hdl, const char *name) 2629573ca77eSGeorge Wilson { 2630573ca77eSGeorge Wilson char path[MAXPATHLEN]; 2631573ca77eSGeorge Wilson char errbuf[1024]; 2632573ca77eSGeorge Wilson int fd, error; 2633573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int); 2634573ca77eSGeorge Wilson 2635573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT, 2636573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL) 2637573ca77eSGeorge Wilson return (-1); 2638573ca77eSGeorge Wilson 26396401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name); 2640573ca77eSGeorge Wilson 2641573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 2642573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2643573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name); 2644573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 2645573ca77eSGeorge Wilson } 2646573ca77eSGeorge Wilson 2647573ca77eSGeorge Wilson /* 2648573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device 2649573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply 2650573ca77eSGeorge Wilson * ignore that error and continue on. 2651573ca77eSGeorge Wilson */ 2652573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd); 2653573ca77eSGeorge Wilson (void) close(fd); 2654573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) { 2655573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot " 2656573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name); 2657573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 2658573ca77eSGeorge Wilson } 2659573ca77eSGeorge Wilson return (0); 2660573ca77eSGeorge Wilson } 2661573ca77eSGeorge Wilson 2662fa9e4066Sahrens /* 26633d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the 26643d7072f8Seschrock * ZFS_ONLINE_* flags. 2665fa9e4066Sahrens */ 2666fa9e4066Sahrens int 26673d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags, 26683d7072f8Seschrock vdev_state_t *newstate) 2669fa9e4066Sahrens { 2670fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2671fa9e4066Sahrens char msg[1024]; 26729a551dd6SYuri Pankov char *pathname; 267399653d4eSeschrock nvlist_t *tgt; 2674573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog; 267599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2676fa9e4066Sahrens 2677573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) { 2678573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2679573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path); 2680573ca77eSGeorge Wilson } else { 2681573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg), 2682573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path); 2683573ca77eSGeorge Wilson } 2684ea8dc4b6Seschrock 2685fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2686ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2687573ca77eSGeorge Wilson &islog)) == NULL) 268899653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 2689fa9e4066Sahrens 269099653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2691fa9e4066Sahrens 2692069f55e2SEric Schrock if (avail_spare) 2693a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2694a43d325bSek 26959a551dd6SYuri Pankov if ((flags & ZFS_ONLINE_EXPAND || 26969a551dd6SYuri Pankov zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) && 26979a551dd6SYuri Pankov nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) { 2698573ca77eSGeorge Wilson uint64_t wholedisk = 0; 2699573ca77eSGeorge Wilson 2700573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 2701573ca77eSGeorge Wilson &wholedisk); 2702573ca77eSGeorge Wilson 2703573ca77eSGeorge Wilson /* 2704573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion. 2705573ca77eSGeorge Wilson */ 2706573ca77eSGeorge Wilson if (l2cache) { 2707573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2708573ca77eSGeorge Wilson "cannot expand cache devices")); 2709573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg)); 2710573ca77eSGeorge Wilson } 2711573ca77eSGeorge Wilson 2712573ca77eSGeorge Wilson if (wholedisk) { 27136401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1; 2714cb04b873SMark J Musante (void) zpool_relabel_disk(hdl, pathname); 2715573ca77eSGeorge Wilson } 2716573ca77eSGeorge Wilson } 2717573ca77eSGeorge Wilson 27183d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE; 27193d7072f8Seschrock zc.zc_obj = flags; 2720fa9e4066Sahrens 2721cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) { 27221195e687SMark J Musante if (errno == EINVAL) { 27231195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split " 27241195e687SMark J Musante "from this pool into a new one. Use '%s' " 27251195e687SMark J Musante "instead"), "zpool detach"); 27261195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg)); 27271195e687SMark J Musante } 27283d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 27291195e687SMark J Musante } 27303d7072f8Seschrock 27313d7072f8Seschrock *newstate = zc.zc_cookie; 27323d7072f8Seschrock return (0); 2733fa9e4066Sahrens } 2734fa9e4066Sahrens 2735fa9e4066Sahrens /* 2736fa9e4066Sahrens * Take the specified vdev offline 2737fa9e4066Sahrens */ 2738fa9e4066Sahrens int 27393d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp) 2740fa9e4066Sahrens { 2741fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2742fa9e4066Sahrens char msg[1024]; 274399653d4eSeschrock nvlist_t *tgt; 2744fa94a07fSbrendan boolean_t avail_spare, l2cache; 274599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 2746fa9e4066Sahrens 2747ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 2748ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path); 2749ea8dc4b6Seschrock 2750fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2751ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 2752ee0eb9f2SEric Schrock NULL)) == NULL) 275399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 275499653d4eSeschrock 275599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2756fa9e4066Sahrens 2757069f55e2SEric Schrock if (avail_spare) 2758a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg)); 2759a43d325bSek 27603d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE; 27613d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0; 27623d7072f8Seschrock 2763cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 27643d7072f8Seschrock return (0); 27653d7072f8Seschrock 27663d7072f8Seschrock switch (errno) { 27673d7072f8Seschrock case EBUSY: 27683d7072f8Seschrock 27693d7072f8Seschrock /* 27703d7072f8Seschrock * There are no other replicas of this device. 27713d7072f8Seschrock */ 27723d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 27733d7072f8Seschrock 2774e6ca193dSGeorge Wilson case EEXIST: 2775e6ca193dSGeorge Wilson /* 2776e6ca193dSGeorge Wilson * The log device has unplayed logs 2777e6ca193dSGeorge Wilson */ 2778e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg)); 2779e6ca193dSGeorge Wilson 27803d7072f8Seschrock default: 27813d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 27823d7072f8Seschrock } 27833d7072f8Seschrock } 27843d7072f8Seschrock 27853d7072f8Seschrock /* 27863d7072f8Seschrock * Mark the given vdev faulted. 27873d7072f8Seschrock */ 27883d7072f8Seschrock int 2789069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 27903d7072f8Seschrock { 27913d7072f8Seschrock zfs_cmd_t zc = { 0 }; 27923d7072f8Seschrock char msg[1024]; 27933d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 27943d7072f8Seschrock 27953d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 27963d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid); 2797441d80aaSlling 27983d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 27993d7072f8Seschrock zc.zc_guid = guid; 28003d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED; 2801069f55e2SEric Schrock zc.zc_obj = aux; 28023d7072f8Seschrock 2803cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 2804fa9e4066Sahrens return (0); 2805fa9e4066Sahrens 2806fa9e4066Sahrens switch (errno) { 280799653d4eSeschrock case EBUSY: 2808fa9e4066Sahrens 2809fa9e4066Sahrens /* 2810fa9e4066Sahrens * There are no other replicas of this device. 2811fa9e4066Sahrens */ 281299653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); 2813fa9e4066Sahrens 281499653d4eSeschrock default: 281599653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 2816fa9e4066Sahrens } 28173d7072f8Seschrock 28183d7072f8Seschrock } 28193d7072f8Seschrock 28203d7072f8Seschrock /* 28213d7072f8Seschrock * Mark the given vdev degraded. 28223d7072f8Seschrock */ 28233d7072f8Seschrock int 2824069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux) 28253d7072f8Seschrock { 28263d7072f8Seschrock zfs_cmd_t zc = { 0 }; 28273d7072f8Seschrock char msg[1024]; 28283d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 28293d7072f8Seschrock 28303d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 28313d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid); 28323d7072f8Seschrock 28333d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 28343d7072f8Seschrock zc.zc_guid = guid; 28353d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED; 2836069f55e2SEric Schrock zc.zc_obj = aux; 28373d7072f8Seschrock 2838cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0) 28393d7072f8Seschrock return (0); 28403d7072f8Seschrock 28413d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 284299653d4eSeschrock } 284399653d4eSeschrock 284499653d4eSeschrock /* 284599653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as 284699653d4eSeschrock * a hot spare. 284799653d4eSeschrock */ 284899653d4eSeschrock static boolean_t 284999653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) 285099653d4eSeschrock { 285199653d4eSeschrock nvlist_t **child; 285299653d4eSeschrock uint_t c, children; 285399653d4eSeschrock char *type; 285499653d4eSeschrock 285599653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, 285699653d4eSeschrock &children) == 0) { 285799653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, 285899653d4eSeschrock &type) == 0); 285999653d4eSeschrock 286099653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 && 286199653d4eSeschrock children == 2 && child[which] == tgt) 286299653d4eSeschrock return (B_TRUE); 286399653d4eSeschrock 286499653d4eSeschrock for (c = 0; c < children; c++) 286599653d4eSeschrock if (is_replacing_spare(child[c], tgt, which)) 286699653d4eSeschrock return (B_TRUE); 286799653d4eSeschrock } 286899653d4eSeschrock 286999653d4eSeschrock return (B_FALSE); 2870fa9e4066Sahrens } 2871fa9e4066Sahrens 2872fa9e4066Sahrens /* 2873fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk. 28748654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one. 2875fa9e4066Sahrens */ 2876fa9e4066Sahrens int 2877fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp, 2878fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) 2879fa9e4066Sahrens { 2880fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2881fa9e4066Sahrens char msg[1024]; 2882fa9e4066Sahrens int ret; 288399653d4eSeschrock nvlist_t *tgt; 2884ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog; 2885ee0eb9f2SEric Schrock uint64_t val; 2886cb04b873SMark J Musante char *newname; 288799653d4eSeschrock nvlist_t **child; 288899653d4eSeschrock uint_t children; 288999653d4eSeschrock nvlist_t *config_root; 289099653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 28914263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp); 2892fa9e4066Sahrens 2893ea8dc4b6Seschrock if (replacing) 2894ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2895ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk); 2896ea8dc4b6Seschrock else 2897ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, 2898ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk); 2899ea8dc4b6Seschrock 2900fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 2901ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache, 29025cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 290399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 290499653d4eSeschrock 2905a43d325bSek if (avail_spare) 290699653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 290799653d4eSeschrock 2908fa94a07fSbrendan if (l2cache) 2909fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 2910fa94a07fSbrendan 291199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 2912fa9e4066Sahrens zc.zc_cookie = replacing; 2913fa9e4066Sahrens 291499653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 291599653d4eSeschrock &child, &children) != 0 || children != 1) { 291699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 291799653d4eSeschrock "new device must be a single disk")); 291899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); 291999653d4eSeschrock } 292099653d4eSeschrock 292199653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 292299653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); 292399653d4eSeschrock 2924*663207adSDon Brady if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL) 29250430f8daSeschrock return (-1); 29260430f8daSeschrock 292799653d4eSeschrock /* 292899653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only 292999653d4eSeschrock * replace it with another hot spare. 293099653d4eSeschrock */ 293199653d4eSeschrock if (replacing && 293299653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && 2933ee0eb9f2SEric Schrock (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, 2934ee0eb9f2SEric Schrock NULL) == NULL || !avail_spare) && 2935ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) { 293699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 293799653d4eSeschrock "can only be replaced by another hot spare")); 29380430f8daSeschrock free(newname); 293999653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg)); 294099653d4eSeschrock } 294199653d4eSeschrock 29420430f8daSeschrock free(newname); 29430430f8daSeschrock 2944990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0) 294599653d4eSeschrock return (-1); 2946fa9e4066Sahrens 2947cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc); 2948fa9e4066Sahrens 2949e9dbad6fSeschrock zcmd_free_nvlists(&zc); 2950fa9e4066Sahrens 2951b5b76fecSGeorge Wilson if (ret == 0) { 2952b5b76fecSGeorge Wilson if (rootpool) { 295321ecdf64SLin Ling /* 295421ecdf64SLin Ling * XXX need a better way to prevent user from 295521ecdf64SLin Ling * booting up a half-baked vdev. 295621ecdf64SLin Ling */ 295721ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make " 295821ecdf64SLin Ling "sure to wait until resilver is done " 295921ecdf64SLin Ling "before rebooting.\n")); 2960b5b76fecSGeorge Wilson } 2961fa9e4066Sahrens return (0); 2962b5b76fecSGeorge Wilson } 2963fa9e4066Sahrens 2964fa9e4066Sahrens switch (errno) { 2965ea8dc4b6Seschrock case ENOTSUP: 2966fa9e4066Sahrens /* 2967fa9e4066Sahrens * Can't attach to or replace this type of vdev. 2968fa9e4066Sahrens */ 29698654d025Sperrin if (replacing) { 2970cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp, 2971cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL); 2972cb04b873SMark J Musante 2973ee0eb9f2SEric Schrock if (islog) 29748654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29758654d025Sperrin "cannot replace a log with a spare")); 2976cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE) 2977cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2978cb04b873SMark J Musante "already in replacing/spare config; wait " 2979cb04b873SMark J Musante "for completion or use 'zpool detach'")); 29808654d025Sperrin else 29818654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29828654d025Sperrin "cannot replace a replacing device")); 29838654d025Sperrin } else { 298499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 298599653d4eSeschrock "can only attach to mirrors and top-level " 298699653d4eSeschrock "disks")); 29878654d025Sperrin } 298899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg); 2989fa9e4066Sahrens break; 2990fa9e4066Sahrens 2991ea8dc4b6Seschrock case EINVAL: 2992fa9e4066Sahrens /* 2993fa9e4066Sahrens * The new device must be a single disk. 2994fa9e4066Sahrens */ 299599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 299699653d4eSeschrock "new device must be a single disk")); 299799653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 2998fa9e4066Sahrens break; 2999fa9e4066Sahrens 3000ea8dc4b6Seschrock case EBUSY: 30015cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, " 30023a4b1be9SMatthew Ahrens "or device removal is in progress"), 300399653d4eSeschrock new_disk); 300499653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 3005fa9e4066Sahrens break; 3006fa9e4066Sahrens 3007ea8dc4b6Seschrock case EOVERFLOW: 3008fa9e4066Sahrens /* 3009fa9e4066Sahrens * The new device is too small. 3010fa9e4066Sahrens */ 301199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 301299653d4eSeschrock "device is too small")); 301399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 3014fa9e4066Sahrens break; 3015fa9e4066Sahrens 3016ea8dc4b6Seschrock case EDOM: 3017fa9e4066Sahrens /* 3018fa9e4066Sahrens * The new device has a different alignment requirement. 3019fa9e4066Sahrens */ 302099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 302199653d4eSeschrock "devices have different sector alignment")); 302299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg); 3023fa9e4066Sahrens break; 3024fa9e4066Sahrens 3025ea8dc4b6Seschrock case ENAMETOOLONG: 3026fa9e4066Sahrens /* 3027fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label. 3028fa9e4066Sahrens */ 302999653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); 3030fa9e4066Sahrens break; 3031fa9e4066Sahrens 3032ea8dc4b6Seschrock default: 303399653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 3034fa9e4066Sahrens } 3035fa9e4066Sahrens 303699653d4eSeschrock return (-1); 3037fa9e4066Sahrens } 3038fa9e4066Sahrens 3039fa9e4066Sahrens /* 3040fa9e4066Sahrens * Detach the specified device. 3041fa9e4066Sahrens */ 3042fa9e4066Sahrens int 3043fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path) 3044fa9e4066Sahrens { 3045fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3046fa9e4066Sahrens char msg[1024]; 304799653d4eSeschrock nvlist_t *tgt; 3048fa94a07fSbrendan boolean_t avail_spare, l2cache; 304999653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3050fa9e4066Sahrens 3051ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3052ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path); 3053ea8dc4b6Seschrock 3054fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3055ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 30565cabbc6bSPrashanth Sreenivasa NULL)) == NULL) 305799653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3058fa9e4066Sahrens 3059a43d325bSek if (avail_spare) 306099653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 306199653d4eSeschrock 3062fa94a07fSbrendan if (l2cache) 3063fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg)); 3064fa94a07fSbrendan 306599653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); 306699653d4eSeschrock 3067ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0) 3068fa9e4066Sahrens return (0); 3069fa9e4066Sahrens 3070fa9e4066Sahrens switch (errno) { 3071fa9e4066Sahrens 3072ea8dc4b6Seschrock case ENOTSUP: 3073fa9e4066Sahrens /* 3074fa9e4066Sahrens * Can't detach from this type of vdev. 3075fa9e4066Sahrens */ 307699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " 307799653d4eSeschrock "applicable to mirror and replacing vdevs")); 3078cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg); 3079fa9e4066Sahrens break; 3080fa9e4066Sahrens 3081ea8dc4b6Seschrock case EBUSY: 3082fa9e4066Sahrens /* 3083fa9e4066Sahrens * There are no other replicas of this device. 3084fa9e4066Sahrens */ 308599653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg); 3086fa9e4066Sahrens break; 3087fa9e4066Sahrens 3088ea8dc4b6Seschrock default: 308999653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg); 3090ea8dc4b6Seschrock } 3091ea8dc4b6Seschrock 309299653d4eSeschrock return (-1); 309399653d4eSeschrock } 309499653d4eSeschrock 30951195e687SMark J Musante /* 30961195e687SMark J Musante * Find a mirror vdev in the source nvlist. 30971195e687SMark J Musante * 30981195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors 30991195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the 31001195e687SMark J Musante * user specified on the command line. We loop over the mchild array to 31011195e687SMark J Musante * see if any entry in the schild array matches. 31021195e687SMark J Musante * 31031195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return 31041195e687SMark J Musante * the index of that entry. Otherwise we return -1. 31051195e687SMark J Musante */ 31061195e687SMark J Musante static int 31071195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren, 31081195e687SMark J Musante nvlist_t **schild, uint_t schildren) 31091195e687SMark J Musante { 31101195e687SMark J Musante uint_t mc; 31111195e687SMark J Musante 31121195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) { 31131195e687SMark J Musante uint_t sc; 31141195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3115*663207adSDon Brady mchild[mc], 0); 31161195e687SMark J Musante 31171195e687SMark J Musante for (sc = 0; sc < schildren; sc++) { 31181195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp, 3119*663207adSDon Brady schild[sc], 0); 31201195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0); 31211195e687SMark J Musante 31221195e687SMark J Musante free(spath); 31231195e687SMark J Musante if (result) { 31241195e687SMark J Musante free(mpath); 31251195e687SMark J Musante return (mc); 31261195e687SMark J Musante } 31271195e687SMark J Musante } 31281195e687SMark J Musante 31291195e687SMark J Musante free(mpath); 31301195e687SMark J Musante } 31311195e687SMark J Musante 31321195e687SMark J Musante return (-1); 31331195e687SMark J Musante } 31341195e687SMark J Musante 31351195e687SMark J Musante /* 31361195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist 31371195e687SMark J Musante * is generated and it is the responsibility of the caller to free it. 31381195e687SMark J Musante */ 31391195e687SMark J Musante int 31401195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot, 31411195e687SMark J Musante nvlist_t *props, splitflags_t flags) 31421195e687SMark J Musante { 31431195e687SMark J Musante zfs_cmd_t zc = { 0 }; 31441195e687SMark J Musante char msg[1024]; 31451195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL; 31461195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL; 31471195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0; 31481195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl; 31491195e687SMark J Musante uint64_t vers; 31501195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE; 31511195e687SMark J Musante int retval = 0; 31521195e687SMark J Musante 31531195e687SMark J Musante (void) snprintf(msg, sizeof (msg), 31541195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name); 31551195e687SMark J Musante 31561195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname)) 31571195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); 31581195e687SMark J Musante 31591195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) { 31601195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to " 31611195e687SMark J Musante "retrieve pool configuration\n")); 31621195e687SMark J Musante return (-1); 31631195e687SMark J Musante } 31641195e687SMark J Musante 31651195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) 31661195e687SMark J Musante == 0); 31671195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0); 31681195e687SMark J Musante 31691195e687SMark J Musante if (props) { 3170f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE }; 31711195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name, 3172f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL) 31731195e687SMark J Musante return (-1); 31741195e687SMark J Musante } 31751195e687SMark J Musante 31761195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child, 31771195e687SMark J Musante &children) != 0) { 31781195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31791195e687SMark J Musante "Source pool is missing vdev tree")); 3180aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 31811195e687SMark J Musante return (-1); 31821195e687SMark J Musante } 31831195e687SMark J Musante 31841195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *)); 31851195e687SMark J Musante vcount = 0; 31861195e687SMark J Musante 31871195e687SMark J Musante if (*newroot == NULL || 31881195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, 31891195e687SMark J Musante &newchild, &newchildren) != 0) 31901195e687SMark J Musante newchildren = 0; 31911195e687SMark J Musante 31921195e687SMark J Musante for (c = 0; c < children; c++) { 31931195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE; 31941195e687SMark J Musante char *type; 31951195e687SMark J Musante nvlist_t **mchild, *vdev; 31961195e687SMark J Musante uint_t mchildren; 31971195e687SMark J Musante int entry; 31981195e687SMark J Musante 31991195e687SMark J Musante /* 32001195e687SMark J Musante * Unlike cache & spares, slogs are stored in the 32011195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here. 32021195e687SMark J Musante */ 32031195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG, 32041195e687SMark J Musante &is_log); 32051195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, 32061195e687SMark J Musante &is_hole); 32071195e687SMark J Musante if (is_log || is_hole) { 32081195e687SMark J Musante /* 32091195e687SMark J Musante * Create a hole vdev and put it in the config. 32101195e687SMark J Musante */ 32111195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0) 32121195e687SMark J Musante goto out; 32131195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, 32141195e687SMark J Musante VDEV_TYPE_HOLE) != 0) 32151195e687SMark J Musante goto out; 32161195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE, 32171195e687SMark J Musante 1) != 0) 32181195e687SMark J Musante goto out; 32191195e687SMark J Musante if (lastlog == 0) 32201195e687SMark J Musante lastlog = vcount; 32211195e687SMark J Musante varray[vcount++] = vdev; 32221195e687SMark J Musante continue; 32231195e687SMark J Musante } 32241195e687SMark J Musante lastlog = 0; 32251195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type) 32261195e687SMark J Musante == 0); 32271195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) { 32281195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32291195e687SMark J Musante "Source pool must be composed only of mirrors\n")); 32301195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 32311195e687SMark J Musante goto out; 32321195e687SMark J Musante } 32331195e687SMark J Musante 32341195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c], 32351195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0); 32361195e687SMark J Musante 32371195e687SMark J Musante /* find or add an entry for this top-level vdev */ 32381195e687SMark J Musante if (newchildren > 0 && 32391195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren, 32401195e687SMark J Musante newchild, newchildren)) >= 0) { 32411195e687SMark J Musante /* We found a disk that the user specified. */ 32421195e687SMark J Musante vdev = mchild[entry]; 32431195e687SMark J Musante ++found; 32441195e687SMark J Musante } else { 32451195e687SMark J Musante /* User didn't specify a disk for this vdev. */ 32461195e687SMark J Musante vdev = mchild[mchildren - 1]; 32471195e687SMark J Musante } 32481195e687SMark J Musante 32491195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0) 32501195e687SMark J Musante goto out; 32511195e687SMark J Musante } 32521195e687SMark J Musante 32531195e687SMark J Musante /* did we find every disk the user specified? */ 32541195e687SMark J Musante if (found != newchildren) { 32551195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must " 32561195e687SMark J Musante "include at most one disk from each mirror")); 32571195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg); 32581195e687SMark J Musante goto out; 32591195e687SMark J Musante } 32601195e687SMark J Musante 32611195e687SMark J Musante /* Prepare the nvlist for populating. */ 32621195e687SMark J Musante if (*newroot == NULL) { 32631195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0) 32641195e687SMark J Musante goto out; 32651195e687SMark J Musante freelist = B_TRUE; 32661195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE, 32671195e687SMark J Musante VDEV_TYPE_ROOT) != 0) 32681195e687SMark J Musante goto out; 32691195e687SMark J Musante } else { 32701195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0); 32711195e687SMark J Musante } 32721195e687SMark J Musante 32731195e687SMark J Musante /* Add all the children we found */ 32741195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray, 32751195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0) 32761195e687SMark J Musante goto out; 32771195e687SMark J Musante 32781195e687SMark J Musante /* 32791195e687SMark J Musante * If we're just doing a dry run, exit now with success. 32801195e687SMark J Musante */ 32811195e687SMark J Musante if (flags.dryrun) { 32821195e687SMark J Musante memory_err = B_FALSE; 32831195e687SMark J Musante freelist = B_FALSE; 32841195e687SMark J Musante goto out; 32851195e687SMark J Musante } 32861195e687SMark J Musante 32871195e687SMark J Musante /* now build up the config list & call the ioctl */ 32881195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0) 32891195e687SMark J Musante goto out; 32901195e687SMark J Musante 32911195e687SMark J Musante if (nvlist_add_nvlist(newconfig, 32921195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 || 32931195e687SMark J Musante nvlist_add_string(newconfig, 32941195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 || 32951195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0) 32961195e687SMark J Musante goto out; 32971195e687SMark J Musante 32981195e687SMark J Musante /* 32991195e687SMark J Musante * The new pool is automatically part of the namespace unless we 33001195e687SMark J Musante * explicitly export it. 33011195e687SMark J Musante */ 33021195e687SMark J Musante if (!flags.import) 33031195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT; 33041195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 33051195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string)); 33061195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0) 33071195e687SMark J Musante goto out; 33081195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0) 33091195e687SMark J Musante goto out; 33101195e687SMark J Musante 33111195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) { 33121195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg); 33131195e687SMark J Musante goto out; 33141195e687SMark J Musante } 33151195e687SMark J Musante 33161195e687SMark J Musante freelist = B_FALSE; 33171195e687SMark J Musante memory_err = B_FALSE; 33181195e687SMark J Musante 33191195e687SMark J Musante out: 33201195e687SMark J Musante if (varray != NULL) { 33211195e687SMark J Musante int v; 33221195e687SMark J Musante 33231195e687SMark J Musante for (v = 0; v < vcount; v++) 33241195e687SMark J Musante nvlist_free(varray[v]); 33251195e687SMark J Musante free(varray); 33261195e687SMark J Musante } 33271195e687SMark J Musante zcmd_free_nvlists(&zc); 3328aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props); 3329aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig); 33301195e687SMark J Musante if (freelist) { 33311195e687SMark J Musante nvlist_free(*newroot); 33321195e687SMark J Musante *newroot = NULL; 33331195e687SMark J Musante } 33341195e687SMark J Musante 33351195e687SMark J Musante if (retval != 0) 33361195e687SMark J Musante return (retval); 33371195e687SMark J Musante 33381195e687SMark J Musante if (memory_err) 33391195e687SMark J Musante return (no_memory(hdl)); 33401195e687SMark J Musante 33411195e687SMark J Musante return (0); 33421195e687SMark J Musante } 33431195e687SMark J Musante 334499653d4eSeschrock /* 33455cabbc6bSPrashanth Sreenivasa * Remove the given device. 334699653d4eSeschrock */ 334799653d4eSeschrock int 334899653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path) 334999653d4eSeschrock { 335099653d4eSeschrock zfs_cmd_t zc = { 0 }; 335199653d4eSeschrock char msg[1024]; 335299653d4eSeschrock nvlist_t *tgt; 335388ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog; 335499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 335588ecc943SGeorge Wilson uint64_t version; 335699653d4eSeschrock 335799653d4eSeschrock (void) snprintf(msg, sizeof (msg), 335899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path); 335999653d4eSeschrock 336099653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3361ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 33625cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 336399653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 336499653d4eSeschrock 336588ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL); 336688ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) { 336788ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33685cabbc6bSPrashanth Sreenivasa "pool must be upgraded to support log removal")); 336988ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg)); 337088ecc943SGeorge Wilson } 337188ecc943SGeorge Wilson 33725cabbc6bSPrashanth Sreenivasa if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) { 33735cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33745cabbc6bSPrashanth Sreenivasa "root pool can not have removed devices, " 33755cabbc6bSPrashanth Sreenivasa "because GRUB does not understand them")); 33765cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EINVAL, msg)); 33775cabbc6bSPrashanth Sreenivasa } 33785cabbc6bSPrashanth Sreenivasa 33795cabbc6bSPrashanth Sreenivasa zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID); 33805cabbc6bSPrashanth Sreenivasa 33815cabbc6bSPrashanth Sreenivasa if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 33825cabbc6bSPrashanth Sreenivasa return (0); 33835cabbc6bSPrashanth Sreenivasa 33845cabbc6bSPrashanth Sreenivasa switch (errno) { 33855cabbc6bSPrashanth Sreenivasa 33865cabbc6bSPrashanth Sreenivasa case EINVAL: 33875cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33885cabbc6bSPrashanth Sreenivasa "invalid config; all top-level vdevs must " 33895cabbc6bSPrashanth Sreenivasa "have the same sector size and not be raidz.")); 33905cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_INVALCONFIG, msg); 33915cabbc6bSPrashanth Sreenivasa break; 33925cabbc6bSPrashanth Sreenivasa 33935cabbc6bSPrashanth Sreenivasa case EBUSY: 33945cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33955cabbc6bSPrashanth Sreenivasa "Pool busy; removal may already be in progress")); 33965cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BUSY, msg); 33975cabbc6bSPrashanth Sreenivasa break; 33985cabbc6bSPrashanth Sreenivasa 33995cabbc6bSPrashanth Sreenivasa default: 34005cabbc6bSPrashanth Sreenivasa (void) zpool_standard_error(hdl, errno, msg); 34015cabbc6bSPrashanth Sreenivasa } 34025cabbc6bSPrashanth Sreenivasa return (-1); 34035cabbc6bSPrashanth Sreenivasa } 34045cabbc6bSPrashanth Sreenivasa 34055cabbc6bSPrashanth Sreenivasa int 34065cabbc6bSPrashanth Sreenivasa zpool_vdev_remove_cancel(zpool_handle_t *zhp) 34075cabbc6bSPrashanth Sreenivasa { 34085cabbc6bSPrashanth Sreenivasa zfs_cmd_t zc = { 0 }; 34095cabbc6bSPrashanth Sreenivasa char msg[1024]; 34105cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl; 34115cabbc6bSPrashanth Sreenivasa 34125cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg), 34135cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot cancel removal")); 34145cabbc6bSPrashanth Sreenivasa 34155cabbc6bSPrashanth Sreenivasa (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 34165cabbc6bSPrashanth Sreenivasa zc.zc_cookie = 1; 341799653d4eSeschrock 3418ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0) 341999653d4eSeschrock return (0); 342099653d4eSeschrock 342199653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3422ea8dc4b6Seschrock } 3423ea8dc4b6Seschrock 34245cabbc6bSPrashanth Sreenivasa int 34255cabbc6bSPrashanth Sreenivasa zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path, 34265cabbc6bSPrashanth Sreenivasa uint64_t *sizep) 34275cabbc6bSPrashanth Sreenivasa { 34285cabbc6bSPrashanth Sreenivasa char msg[1024]; 34295cabbc6bSPrashanth Sreenivasa nvlist_t *tgt; 34305cabbc6bSPrashanth Sreenivasa boolean_t avail_spare, l2cache, islog; 34315cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl; 34325cabbc6bSPrashanth Sreenivasa 34335cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg), 34345cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"), 34355cabbc6bSPrashanth Sreenivasa path); 34365cabbc6bSPrashanth Sreenivasa 34375cabbc6bSPrashanth Sreenivasa if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache, 34385cabbc6bSPrashanth Sreenivasa &islog)) == NULL) 34395cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EZFS_NODEVICE, msg)); 34405cabbc6bSPrashanth Sreenivasa 34415cabbc6bSPrashanth Sreenivasa if (avail_spare || l2cache || islog) { 34425cabbc6bSPrashanth Sreenivasa *sizep = 0; 34435cabbc6bSPrashanth Sreenivasa return (0); 34445cabbc6bSPrashanth Sreenivasa } 34455cabbc6bSPrashanth Sreenivasa 34465cabbc6bSPrashanth Sreenivasa if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) { 34475cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34485cabbc6bSPrashanth Sreenivasa "indirect size not available")); 34495cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EINVAL, msg)); 34505cabbc6bSPrashanth Sreenivasa } 34515cabbc6bSPrashanth Sreenivasa return (0); 34525cabbc6bSPrashanth Sreenivasa } 34535cabbc6bSPrashanth Sreenivasa 3454ea8dc4b6Seschrock /* 3455ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified. 3456ea8dc4b6Seschrock */ 3457ea8dc4b6Seschrock int 3458468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl) 3459ea8dc4b6Seschrock { 3460ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3461ea8dc4b6Seschrock char msg[1024]; 346299653d4eSeschrock nvlist_t *tgt; 34635dafeea3SPavel Zakharov zpool_load_policy_t policy; 3464fa94a07fSbrendan boolean_t avail_spare, l2cache; 346599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3466468c413aSTim Haley nvlist_t *nvi = NULL; 34674b964adaSGeorge Wilson int error; 3468ea8dc4b6Seschrock 3469ea8dc4b6Seschrock if (path) 3470ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3471ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3472e9dbad6fSeschrock path); 3473ea8dc4b6Seschrock else 3474ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), 3475ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), 3476ea8dc4b6Seschrock zhp->zpool_name); 3477ea8dc4b6Seschrock 3478ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 347999653d4eSeschrock if (path) { 3480fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, 34815cabbc6bSPrashanth Sreenivasa &l2cache, NULL)) == NULL) 348299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg)); 3483ea8dc4b6Seschrock 3484fa94a07fSbrendan /* 3485fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow 3486fa94a07fSbrendan * error clearing for l2cache devices. 3487fa94a07fSbrendan */ 3488a43d325bSek if (avail_spare) 348999653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg)); 3490ea8dc4b6Seschrock 349199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, 349299653d4eSeschrock &zc.zc_guid) == 0); 3493fa9e4066Sahrens } 3494fa9e4066Sahrens 34955dafeea3SPavel Zakharov zpool_get_load_policy(rewindnvl, &policy); 34965dafeea3SPavel Zakharov zc.zc_cookie = policy.zlp_rewind; 3497468c413aSTim Haley 349857f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0) 3499468c413aSTim Haley return (-1); 3500468c413aSTim Haley 3501cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0) 3502468c413aSTim Haley return (-1); 3503468c413aSTim Haley 35044b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 && 35054b964adaSGeorge Wilson errno == ENOMEM) { 35064b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 35074b964adaSGeorge Wilson zcmd_free_nvlists(&zc); 35084b964adaSGeorge Wilson return (-1); 35094b964adaSGeorge Wilson } 35104b964adaSGeorge Wilson } 35114b964adaSGeorge Wilson 35125dafeea3SPavel Zakharov if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) && 3513468c413aSTim Haley errno != EPERM && errno != EACCES)) { 35145dafeea3SPavel Zakharov if (policy.zlp_rewind & 3515468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) { 3516468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi); 3517468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name, 35185dafeea3SPavel Zakharov ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), 3519468c413aSTim Haley nvi); 3520468c413aSTim Haley nvlist_free(nvi); 3521468c413aSTim Haley } 3522468c413aSTim Haley zcmd_free_nvlists(&zc); 352399653d4eSeschrock return (0); 3524468c413aSTim Haley } 352599653d4eSeschrock 3526468c413aSTim Haley zcmd_free_nvlists(&zc); 352799653d4eSeschrock return (zpool_standard_error(hdl, errno, msg)); 3528fa9e4066Sahrens } 3529fa9e4066Sahrens 35303d7072f8Seschrock /* 35313d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd). 35323d7072f8Seschrock */ 35333d7072f8Seschrock int 35343d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid) 35353d7072f8Seschrock { 35363d7072f8Seschrock zfs_cmd_t zc = { 0 }; 35373d7072f8Seschrock char msg[1024]; 35383d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 35393d7072f8Seschrock 35403d7072f8Seschrock (void) snprintf(msg, sizeof (msg), 35413d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"), 35423d7072f8Seschrock guid); 35433d7072f8Seschrock 35443d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 35453d7072f8Seschrock zc.zc_guid = guid; 354614f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND; 35473d7072f8Seschrock 35483d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) 35493d7072f8Seschrock return (0); 35503d7072f8Seschrock 35513d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg)); 35523d7072f8Seschrock } 35533d7072f8Seschrock 3554e9103aaeSGarrett D'Amore /* 3555e9103aaeSGarrett D'Amore * Change the GUID for a pool. 3556e9103aaeSGarrett D'Amore */ 3557e9103aaeSGarrett D'Amore int 3558e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp) 3559e9103aaeSGarrett D'Amore { 3560e9103aaeSGarrett D'Amore char msg[1024]; 3561e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl; 3562e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 }; 3563e9103aaeSGarrett D'Amore 3564e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg), 3565e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name); 3566e9103aaeSGarrett D'Amore 3567e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3568e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0) 3569e9103aaeSGarrett D'Amore return (0); 3570e9103aaeSGarrett D'Amore 3571e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg)); 3572e9103aaeSGarrett D'Amore } 3573e9103aaeSGarrett D'Amore 35744263d13fSGeorge Wilson /* 35754263d13fSGeorge Wilson * Reopen the pool. 35764263d13fSGeorge Wilson */ 35774263d13fSGeorge Wilson int 35784263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp) 35794263d13fSGeorge Wilson { 35804263d13fSGeorge Wilson zfs_cmd_t zc = { 0 }; 35814263d13fSGeorge Wilson char msg[1024]; 35824263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl; 35834263d13fSGeorge Wilson 35844263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg), 35854263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), 35864263d13fSGeorge Wilson zhp->zpool_name); 35874263d13fSGeorge Wilson 35884263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 35894263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0) 35904263d13fSGeorge Wilson return (0); 35914263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg)); 35924263d13fSGeorge Wilson } 35934263d13fSGeorge Wilson 35949c2acf00SAlek Pinchuk /* call into libzfs_core to execute the sync IOCTL per pool */ 35959c2acf00SAlek Pinchuk int 35969c2acf00SAlek Pinchuk zpool_sync_one(zpool_handle_t *zhp, void *data) 35979c2acf00SAlek Pinchuk { 35989c2acf00SAlek Pinchuk int ret; 35999c2acf00SAlek Pinchuk libzfs_handle_t *hdl = zpool_get_handle(zhp); 36009c2acf00SAlek Pinchuk const char *pool_name = zpool_get_name(zhp); 36019c2acf00SAlek Pinchuk boolean_t *force = data; 36029c2acf00SAlek Pinchuk nvlist_t *innvl = fnvlist_alloc(); 36039c2acf00SAlek Pinchuk 36049c2acf00SAlek Pinchuk fnvlist_add_boolean_value(innvl, "force", *force); 36059c2acf00SAlek Pinchuk if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) { 36069c2acf00SAlek Pinchuk nvlist_free(innvl); 36079c2acf00SAlek Pinchuk return (zpool_standard_error_fmt(hdl, ret, 36089c2acf00SAlek Pinchuk dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name)); 36099c2acf00SAlek Pinchuk } 36109c2acf00SAlek Pinchuk nvlist_free(innvl); 36119c2acf00SAlek Pinchuk 36129c2acf00SAlek Pinchuk return (0); 36139c2acf00SAlek Pinchuk } 36149c2acf00SAlek Pinchuk 3615c67d9675Seschrock /* 3616c67d9675Seschrock * Convert from a devid string to a path. 3617c67d9675Seschrock */ 3618c67d9675Seschrock static char * 3619c67d9675Seschrock devid_to_path(char *devid_str) 3620c67d9675Seschrock { 3621c67d9675Seschrock ddi_devid_t devid; 3622c67d9675Seschrock char *minor; 3623c67d9675Seschrock char *path; 3624c67d9675Seschrock devid_nmlist_t *list = NULL; 3625c67d9675Seschrock int ret; 3626c67d9675Seschrock 3627c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0) 3628c67d9675Seschrock return (NULL); 3629c67d9675Seschrock 3630c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); 3631c67d9675Seschrock 3632c67d9675Seschrock devid_str_free(minor); 3633c67d9675Seschrock devid_free(devid); 3634c67d9675Seschrock 3635c67d9675Seschrock if (ret != 0) 3636c67d9675Seschrock return (NULL); 3637c67d9675Seschrock 3638078266a5SMarcel Telka /* 3639078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below. 3640078266a5SMarcel Telka */ 3641078266a5SMarcel Telka path = strdup(list[0].devname); 364299653d4eSeschrock 3643c67d9675Seschrock devid_free_nmlist(list); 3644c67d9675Seschrock 3645c67d9675Seschrock return (path); 3646c67d9675Seschrock } 3647c67d9675Seschrock 3648c67d9675Seschrock /* 3649c67d9675Seschrock * Convert from a path to a devid string. 3650c67d9675Seschrock */ 3651c67d9675Seschrock static char * 3652c67d9675Seschrock path_to_devid(const char *path) 3653c67d9675Seschrock { 3654c67d9675Seschrock int fd; 3655c67d9675Seschrock ddi_devid_t devid; 3656c67d9675Seschrock char *minor, *ret; 3657c67d9675Seschrock 3658c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0) 3659c67d9675Seschrock return (NULL); 3660c67d9675Seschrock 3661c67d9675Seschrock minor = NULL; 3662c67d9675Seschrock ret = NULL; 3663c67d9675Seschrock if (devid_get(fd, &devid) == 0) { 3664c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0) 3665c67d9675Seschrock ret = devid_str_encode(devid, minor); 3666c67d9675Seschrock if (minor != NULL) 3667c67d9675Seschrock devid_str_free(minor); 3668c67d9675Seschrock devid_free(devid); 3669c67d9675Seschrock } 3670c67d9675Seschrock (void) close(fd); 3671c67d9675Seschrock 3672c67d9675Seschrock return (ret); 3673c67d9675Seschrock } 3674c67d9675Seschrock 3675c67d9675Seschrock /* 3676c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We 3677c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to 3678c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway. 3679c67d9675Seschrock */ 3680c67d9675Seschrock static void 3681c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) 3682c67d9675Seschrock { 3683c67d9675Seschrock zfs_cmd_t zc = { 0 }; 3684c67d9675Seschrock 3685c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 3686e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); 3687c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, 3688ea8dc4b6Seschrock &zc.zc_guid) == 0); 3689c67d9675Seschrock 369099653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); 3691c67d9675Seschrock } 3692c67d9675Seschrock 3693c67d9675Seschrock /* 3694c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path, 3695c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. 3696c67d9675Seschrock * We also check if this is a whole disk, in which case we strip off the 3697c67d9675Seschrock * trailing 's0' slice name. 3698c67d9675Seschrock * 3699c67d9675Seschrock * This routine is also responsible for identifying when disks have been 3700c67d9675Seschrock * reconfigured in a new location. The kernel will have opened the device by 3701c67d9675Seschrock * devid, but the path will still refer to the old location. To catch this, we 3702c67d9675Seschrock * first do a path -> devid translation (which is fast for the common case). If 3703c67d9675Seschrock * the devid matches, we're done. If not, we do a reverse devid -> path 3704c67d9675Seschrock * translation and issue the appropriate ioctl() to update the path of the vdev. 3705c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any 3706c67d9675Seschrock * of these checks. 3707c67d9675Seschrock */ 3708c67d9675Seschrock char * 370988ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv, 3710*663207adSDon Brady int name_flags) 3711c67d9675Seschrock { 3712*663207adSDon Brady char *path, *devid, *env; 3713ea8dc4b6Seschrock uint64_t value; 3714ea8dc4b6Seschrock char buf[64]; 37153d7072f8Seschrock vdev_stat_t *vs; 37163d7072f8Seschrock uint_t vsc; 3717c67d9675Seschrock 3718*663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_PATH"); 3719*663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 || 3720*663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3721*663207adSDon Brady name_flags |= VDEV_NAME_PATH; 3722*663207adSDon Brady 3723*663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_GUID"); 3724*663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 || 3725*663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3726*663207adSDon Brady name_flags |= VDEV_NAME_GUID; 3727*663207adSDon Brady 3728*663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS"); 3729*663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 || 3730*663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) 3731*663207adSDon Brady name_flags |= VDEV_NAME_FOLLOW_LINKS; 3732*663207adSDon Brady 3733*663207adSDon Brady if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 || 3734*663207adSDon Brady name_flags & VDEV_NAME_GUID) { 3735*663207adSDon Brady nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value); 3736*663207adSDon Brady (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value); 3737ea8dc4b6Seschrock path = buf; 3738ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { 3739c67d9675Seschrock 37403d7072f8Seschrock /* 37413d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't 37423d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to 37433d7072f8Seschrock * open a misbehaving device, which can have undesirable 37443d7072f8Seschrock * effects. 37453d7072f8Seschrock */ 37463f9d6ad7SLin Ling if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS, 37473d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 || 37483d7072f8Seschrock vs->vs_state >= VDEV_STATE_DEGRADED) && 37493d7072f8Seschrock zhp != NULL && 3750c67d9675Seschrock nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { 3751c67d9675Seschrock /* 3752c67d9675Seschrock * Determine if the current path is correct. 3753c67d9675Seschrock */ 3754c67d9675Seschrock char *newdevid = path_to_devid(path); 3755c67d9675Seschrock 3756c67d9675Seschrock if (newdevid == NULL || 3757c67d9675Seschrock strcmp(devid, newdevid) != 0) { 3758c67d9675Seschrock char *newpath; 3759c67d9675Seschrock 3760c67d9675Seschrock if ((newpath = devid_to_path(devid)) != NULL) { 3761c67d9675Seschrock /* 3762c67d9675Seschrock * Update the path appropriately. 3763c67d9675Seschrock */ 3764c67d9675Seschrock set_path(zhp, nv, newpath); 376599653d4eSeschrock if (nvlist_add_string(nv, 376699653d4eSeschrock ZPOOL_CONFIG_PATH, newpath) == 0) 376799653d4eSeschrock verify(nvlist_lookup_string(nv, 376899653d4eSeschrock ZPOOL_CONFIG_PATH, 376999653d4eSeschrock &path) == 0); 3770c67d9675Seschrock free(newpath); 3771c67d9675Seschrock } 3772c67d9675Seschrock } 3773c67d9675Seschrock 377499653d4eSeschrock if (newdevid) 377599653d4eSeschrock devid_str_free(newdevid); 3776c67d9675Seschrock } 3777c67d9675Seschrock 3778*663207adSDon Brady if (name_flags & VDEV_NAME_FOLLOW_LINKS) { 3779*663207adSDon Brady char *rp = realpath(path, NULL); 3780*663207adSDon Brady if (rp) { 3781*663207adSDon Brady strlcpy(buf, rp, sizeof (buf)); 3782*663207adSDon Brady path = buf; 3783*663207adSDon Brady free(rp); 3784*663207adSDon Brady } 3785*663207adSDon Brady } 3786*663207adSDon Brady 37876401734dSWill Andrews if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) 37886401734dSWill Andrews path += strlen(ZFS_DISK_ROOTD); 3789c67d9675Seschrock 3790*663207adSDon Brady /* 3791*663207adSDon Brady * Remove the partition from the path it this is a whole disk. 3792*663207adSDon Brady */ 3793*663207adSDon Brady if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value) 3794*663207adSDon Brady == 0 && value && !(name_flags & VDEV_NAME_PATH)) { 37953fdda499SJohn Harres int pathlen = strlen(path); 379699653d4eSeschrock char *tmp = zfs_strdup(hdl, path); 37973fdda499SJohn Harres 37983fdda499SJohn Harres /* 37997855d95bSToomas Soome * If it starts with c#, and ends with "s0" or "s1", 38007855d95bSToomas Soome * chop the slice off, or if it ends with "s0/old" or 38017855d95bSToomas Soome * "s1/old", remove the slice from the middle. 38023fdda499SJohn Harres */ 38033fdda499SJohn Harres if (CTD_CHECK(tmp)) { 38047855d95bSToomas Soome if (strcmp(&tmp[pathlen - 2], "s0") == 0 || 38057855d95bSToomas Soome strcmp(&tmp[pathlen - 2], "s1") == 0) { 38063fdda499SJohn Harres tmp[pathlen - 2] = '\0'; 38073fdda499SJohn Harres } else if (pathlen > 6 && 38087855d95bSToomas Soome (strcmp(&tmp[pathlen - 6], "s0/old") == 0 || 38097855d95bSToomas Soome strcmp(&tmp[pathlen - 6], "s1/old") == 0)) { 38103fdda499SJohn Harres (void) strcpy(&tmp[pathlen - 6], 38113fdda499SJohn Harres "/old"); 38123fdda499SJohn Harres } 38133fdda499SJohn Harres } 3814c67d9675Seschrock return (tmp); 3815c67d9675Seschrock } 3816c67d9675Seschrock } else { 3817c67d9675Seschrock verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); 381899653d4eSeschrock 381999653d4eSeschrock /* 382099653d4eSeschrock * If it's a raidz device, we need to stick in the parity level. 382199653d4eSeschrock */ 382299653d4eSeschrock if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { 382399653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, 382499653d4eSeschrock &value) == 0); 382599653d4eSeschrock (void) snprintf(buf, sizeof (buf), "%s%llu", path, 38265ad82045Snd (u_longlong_t)value); 382799653d4eSeschrock path = buf; 382899653d4eSeschrock } 382988ecc943SGeorge Wilson 383088ecc943SGeorge Wilson /* 383188ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id> 383288ecc943SGeorge Wilson * naming convention. 383388ecc943SGeorge Wilson */ 3834*663207adSDon Brady if (name_flags & VDEV_NAME_TYPE_ID) { 383588ecc943SGeorge Wilson uint64_t id; 383688ecc943SGeorge Wilson 383788ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, 383888ecc943SGeorge Wilson &id) == 0); 383988ecc943SGeorge Wilson (void) snprintf(buf, sizeof (buf), "%s-%llu", path, 384088ecc943SGeorge Wilson (u_longlong_t)id); 384188ecc943SGeorge Wilson path = buf; 384288ecc943SGeorge Wilson } 3843c67d9675Seschrock } 3844c67d9675Seschrock 384599653d4eSeschrock return (zfs_strdup(hdl, path)); 3846c67d9675Seschrock } 3847ea8dc4b6Seschrock 3848ea8dc4b6Seschrock static int 3849a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b) 3850ea8dc4b6Seschrock { 38517802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t))); 3852ea8dc4b6Seschrock } 3853ea8dc4b6Seschrock 3854ea8dc4b6Seschrock /* 3855ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the 3856ea8dc4b6Seschrock * caller. 3857ea8dc4b6Seschrock */ 3858ea8dc4b6Seschrock int 385955434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp) 3860ea8dc4b6Seschrock { 3861ea8dc4b6Seschrock zfs_cmd_t zc = { 0 }; 3862ea8dc4b6Seschrock uint64_t count; 38637802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL; 386455434c77Sek int i; 3865ea8dc4b6Seschrock 3866ea8dc4b6Seschrock /* 3867ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors 3868ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the 3869ea8dc4b6Seschrock * entire list. 3870ea8dc4b6Seschrock */ 3871ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, 3872ea8dc4b6Seschrock &count) == 0); 387375519f38Sek if (count == 0) 387475519f38Sek return (0); 3875e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, 38767802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL) 387799653d4eSeschrock return (-1); 3878e9dbad6fSeschrock zc.zc_nvlist_dst_size = count; 3879ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3880ea8dc4b6Seschrock for (;;) { 388199653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, 388299653d4eSeschrock &zc) != 0) { 3883e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 3884ea8dc4b6Seschrock if (errno == ENOMEM) { 38857802d7bfSMatthew Ahrens void *dst; 38867802d7bfSMatthew Ahrens 3887bf561db0Svb count = zc.zc_nvlist_dst_size; 38887802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count * 38897802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t)); 38907802d7bfSMatthew Ahrens if (dst == NULL) 389199653d4eSeschrock return (-1); 38927802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst; 3893ea8dc4b6Seschrock } else { 3894ea8dc4b6Seschrock return (-1); 3895ea8dc4b6Seschrock } 3896ea8dc4b6Seschrock } else { 3897ea8dc4b6Seschrock break; 3898ea8dc4b6Seschrock } 3899ea8dc4b6Seschrock } 3900ea8dc4b6Seschrock 3901ea8dc4b6Seschrock /* 3902ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the 3903ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last 3904e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks 3905ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our 3906ea8dc4b6Seschrock * array appropriate and decrement the total number of elements. 3907ea8dc4b6Seschrock */ 39087802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) + 3909e9dbad6fSeschrock zc.zc_nvlist_dst_size; 3910e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size; 3911ea8dc4b6Seschrock 3912a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare); 3913ea8dc4b6Seschrock 391455434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0); 3915ea8dc4b6Seschrock 3916ea8dc4b6Seschrock /* 391755434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers. 3918ea8dc4b6Seschrock */ 3919ea8dc4b6Seschrock for (i = 0; i < count; i++) { 3920ea8dc4b6Seschrock nvlist_t *nv; 3921ea8dc4b6Seschrock 3922c0a81264Sek /* ignoring zb_blkid and zb_level for now */ 3923c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset && 3924c0a81264Sek zb[i-1].zb_object == zb[i].zb_object) 3925ea8dc4b6Seschrock continue; 3926ea8dc4b6Seschrock 392755434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0) 392855434c77Sek goto nomem; 392955434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET, 393055434c77Sek zb[i].zb_objset) != 0) { 393155434c77Sek nvlist_free(nv); 393299653d4eSeschrock goto nomem; 3933ea8dc4b6Seschrock } 393455434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT, 393555434c77Sek zb[i].zb_object) != 0) { 393655434c77Sek nvlist_free(nv); 393755434c77Sek goto nomem; 393855434c77Sek } 393955434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) { 394055434c77Sek nvlist_free(nv); 394155434c77Sek goto nomem; 394255434c77Sek } 394355434c77Sek nvlist_free(nv); 3944ea8dc4b6Seschrock } 3945ea8dc4b6Seschrock 39463ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst); 3947ea8dc4b6Seschrock return (0); 394899653d4eSeschrock 394999653d4eSeschrock nomem: 3950e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst); 395199653d4eSeschrock return (no_memory(zhp->zpool_hdl)); 3952ea8dc4b6Seschrock } 3953eaca9bbdSeschrock 3954eaca9bbdSeschrock /* 3955eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version. 3956eaca9bbdSeschrock */ 3957eaca9bbdSeschrock int 3958990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version) 3959eaca9bbdSeschrock { 3960eaca9bbdSeschrock zfs_cmd_t zc = { 0 }; 396199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl; 3962eaca9bbdSeschrock 3963eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name); 3964990b4856Slling zc.zc_cookie = new_version; 3965990b4856Slling 3966ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0) 3967ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 396899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), 396999653d4eSeschrock zhp->zpool_name)); 3970eaca9bbdSeschrock return (0); 3971eaca9bbdSeschrock } 397206eeb2adSek 397306eeb2adSek void 39744445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len) 397506eeb2adSek { 39764445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len); 39774445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) { 39784445fffbSMatthew Ahrens (void) strlcat(string, " ", len); 39794445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len); 39802a6b87f0Sek } 39812a6b87f0Sek } 39822a6b87f0Sek 39832a6b87f0Sek int 39844445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message) 39852a6b87f0Sek { 39864445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 }; 39874445fffbSMatthew Ahrens nvlist_t *args; 39884445fffbSMatthew Ahrens int err; 39894445fffbSMatthew Ahrens 39904445fffbSMatthew Ahrens args = fnvlist_alloc(); 39914445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message); 39924445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args); 39934445fffbSMatthew Ahrens if (err == 0) 39944445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc); 39954445fffbSMatthew Ahrens nvlist_free(args); 39964445fffbSMatthew Ahrens zcmd_free_nvlists(&zc); 39974445fffbSMatthew Ahrens return (err); 399806eeb2adSek } 399906eeb2adSek 400006eeb2adSek /* 400106eeb2adSek * Perform ioctl to get some command history of a pool. 400206eeb2adSek * 400306eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the 400406eeb2adSek * logical offset of the history buffer to start reading from. 400506eeb2adSek * 400606eeb2adSek * Upon return, 'off' is the next logical offset to read from and 400706eeb2adSek * 'len' is the actual amount of bytes read into 'buf'. 400806eeb2adSek */ 400906eeb2adSek static int 401006eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len) 401106eeb2adSek { 401206eeb2adSek zfs_cmd_t zc = { 0 }; 401306eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl; 401406eeb2adSek 401506eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 401606eeb2adSek 401706eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf; 401806eeb2adSek zc.zc_history_len = *len; 401906eeb2adSek zc.zc_history_offset = *off; 402006eeb2adSek 402106eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) { 402206eeb2adSek switch (errno) { 402306eeb2adSek case EPERM: 4024ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM, 4025ece3d9b3Slling dgettext(TEXT_DOMAIN, 402606eeb2adSek "cannot show history for pool '%s'"), 402706eeb2adSek zhp->zpool_name)); 402806eeb2adSek case ENOENT: 4029ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY, 403006eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool " 403106eeb2adSek "'%s'"), zhp->zpool_name)); 4032d7306b64Sek case ENOTSUP: 4033d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION, 4034d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool " 4035d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name)); 403606eeb2adSek default: 4037ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno, 403806eeb2adSek dgettext(TEXT_DOMAIN, 403906eeb2adSek "cannot get history for '%s'"), zhp->zpool_name)); 404006eeb2adSek } 404106eeb2adSek } 404206eeb2adSek 404306eeb2adSek *len = zc.zc_history_len; 404406eeb2adSek *off = zc.zc_history_offset; 404506eeb2adSek 404606eeb2adSek return (0); 404706eeb2adSek } 404806eeb2adSek 404906eeb2adSek /* 405006eeb2adSek * Process the buffer of nvlists, unpacking and storing each nvlist record 405106eeb2adSek * into 'records'. 'leftover' is set to the number of bytes that weren't 405206eeb2adSek * processed as there wasn't a complete record. 405306eeb2adSek */ 40548f18d1faSGeorge Wilson int 405506eeb2adSek zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover, 405606eeb2adSek nvlist_t ***records, uint_t *numrecords) 405706eeb2adSek { 405806eeb2adSek uint64_t reclen; 405906eeb2adSek nvlist_t *nv; 406006eeb2adSek int i; 406106eeb2adSek 406206eeb2adSek while (bytes_read > sizeof (reclen)) { 406306eeb2adSek 406406eeb2adSek /* get length of packed record (stored as little endian) */ 406506eeb2adSek for (i = 0, reclen = 0; i < sizeof (reclen); i++) 406606eeb2adSek reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i); 406706eeb2adSek 406806eeb2adSek if (bytes_read < sizeof (reclen) + reclen) 406906eeb2adSek break; 407006eeb2adSek 407106eeb2adSek /* unpack record */ 407206eeb2adSek if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0) 407306eeb2adSek return (ENOMEM); 407406eeb2adSek bytes_read -= sizeof (reclen) + reclen; 407506eeb2adSek buf += sizeof (reclen) + reclen; 407606eeb2adSek 407706eeb2adSek /* add record to nvlist array */ 407806eeb2adSek (*numrecords)++; 407906eeb2adSek if (ISP2(*numrecords + 1)) { 408006eeb2adSek *records = realloc(*records, 408106eeb2adSek *numrecords * 2 * sizeof (nvlist_t *)); 408206eeb2adSek } 408306eeb2adSek (*records)[*numrecords - 1] = nv; 408406eeb2adSek } 408506eeb2adSek 408606eeb2adSek *leftover = bytes_read; 408706eeb2adSek return (0); 408806eeb2adSek } 408906eeb2adSek 409006eeb2adSek /* 409106eeb2adSek * Retrieve the command history of a pool. 409206eeb2adSek */ 409306eeb2adSek int 409406eeb2adSek zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp) 409506eeb2adSek { 40963339867aSMatthew Ahrens char *buf; 40973339867aSMatthew Ahrens int buflen = 128 * 1024; 409806eeb2adSek uint64_t off = 0; 409906eeb2adSek nvlist_t **records = NULL; 410006eeb2adSek uint_t numrecords = 0; 410106eeb2adSek int err, i; 410206eeb2adSek 41033339867aSMatthew Ahrens buf = malloc(buflen); 41043339867aSMatthew Ahrens if (buf == NULL) 41053339867aSMatthew Ahrens return (ENOMEM); 410606eeb2adSek do { 41073339867aSMatthew Ahrens uint64_t bytes_read = buflen; 410806eeb2adSek uint64_t leftover; 410906eeb2adSek 411006eeb2adSek if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0) 411106eeb2adSek break; 411206eeb2adSek 411306eeb2adSek /* if nothing else was read in, we're at EOF, just return */ 411406eeb2adSek if (!bytes_read) 411506eeb2adSek break; 411606eeb2adSek 411706eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read, 411806eeb2adSek &leftover, &records, &numrecords)) != 0) 411906eeb2adSek break; 412006eeb2adSek off -= leftover; 41213339867aSMatthew Ahrens if (leftover == bytes_read) { 41223339867aSMatthew Ahrens /* 41233339867aSMatthew Ahrens * no progress made, because buffer is not big enough 41243339867aSMatthew Ahrens * to hold this record; resize and retry. 41253339867aSMatthew Ahrens */ 41263339867aSMatthew Ahrens buflen *= 2; 41273339867aSMatthew Ahrens free(buf); 41283339867aSMatthew Ahrens buf = malloc(buflen); 41293339867aSMatthew Ahrens if (buf == NULL) 41303339867aSMatthew Ahrens return (ENOMEM); 41313339867aSMatthew Ahrens } 413206eeb2adSek 413306eeb2adSek /* CONSTCOND */ 413406eeb2adSek } while (1); 413506eeb2adSek 41363339867aSMatthew Ahrens free(buf); 41373339867aSMatthew Ahrens 413806eeb2adSek if (!err) { 413906eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0); 414006eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD, 414106eeb2adSek records, numrecords) == 0); 414206eeb2adSek } 414306eeb2adSek for (i = 0; i < numrecords; i++) 414406eeb2adSek nvlist_free(records[i]); 414506eeb2adSek free(records); 414606eeb2adSek 414706eeb2adSek return (err); 414806eeb2adSek } 414955434c77Sek 415055434c77Sek void 415155434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj, 415255434c77Sek char *pathname, size_t len) 415355434c77Sek { 415455434c77Sek zfs_cmd_t zc = { 0 }; 415555434c77Sek boolean_t mounted = B_FALSE; 415655434c77Sek char *mntpnt = NULL; 41579adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 415855434c77Sek 415955434c77Sek if (dsobj == 0) { 416055434c77Sek /* special case for the MOS */ 416155434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj); 416255434c77Sek return; 416355434c77Sek } 416455434c77Sek 416555434c77Sek /* get the dataset's name */ 416655434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); 416755434c77Sek zc.zc_obj = dsobj; 416855434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, 416955434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) { 417055434c77Sek /* just write out a path of two object numbers */ 417155434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>", 417255434c77Sek dsobj, obj); 417355434c77Sek return; 417455434c77Sek } 417555434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname)); 417655434c77Sek 417755434c77Sek /* find out if the dataset is mounted */ 417855434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt); 417955434c77Sek 418055434c77Sek /* get the corrupted object's path */ 418155434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name)); 418255434c77Sek zc.zc_obj = obj; 418355434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH, 418455434c77Sek &zc) == 0) { 418555434c77Sek if (mounted) { 418655434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt, 418755434c77Sek zc.zc_value); 418855434c77Sek } else { 418955434c77Sek (void) snprintf(pathname, len, "%s:%s", 419055434c77Sek dsname, zc.zc_value); 419155434c77Sek } 419255434c77Sek } else { 419355434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj); 419455434c77Sek } 419555434c77Sek free(mntpnt); 419655434c77Sek } 4197b1b8ab34Slling 419815e6edf1Sgw /* 419915e6edf1Sgw * Read the EFI label from the config, if a label does not exist then 420015e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL 420115e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI 42027855d95bSToomas Soome * partition. If the caller has passed a non-NULL boolean argument, then 42037855d95bSToomas Soome * we set it to indicate if the disk does have efi system partition. 420415e6edf1Sgw */ 420515e6edf1Sgw static int 42067855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system) 420715e6edf1Sgw { 420815e6edf1Sgw char *path; 420915e6edf1Sgw int fd; 421015e6edf1Sgw char diskname[MAXPATHLEN]; 42117855d95bSToomas Soome boolean_t boot = B_FALSE; 421215e6edf1Sgw int err = -1; 42137855d95bSToomas Soome int slice; 421415e6edf1Sgw 421515e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) 421615e6edf1Sgw return (err); 421715e6edf1Sgw 42186401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT, 421915e6edf1Sgw strrchr(path, '/')); 422015e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) { 422115e6edf1Sgw struct dk_gpt *vtoc; 422215e6edf1Sgw 422315e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) { 42247855d95bSToomas Soome for (slice = 0; slice < vtoc->efi_nparts; slice++) { 42257855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_SYSTEM) 42267855d95bSToomas Soome boot = B_TRUE; 42277855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_USR) 42287855d95bSToomas Soome break; 42297855d95bSToomas Soome } 42307855d95bSToomas Soome if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR) 42317855d95bSToomas Soome *sb = vtoc->efi_parts[slice].p_start; 42327855d95bSToomas Soome if (system != NULL) 42337855d95bSToomas Soome *system = boot; 423415e6edf1Sgw efi_free(vtoc); 423515e6edf1Sgw } 423615e6edf1Sgw (void) close(fd); 423715e6edf1Sgw } 423815e6edf1Sgw return (err); 423915e6edf1Sgw } 424015e6edf1Sgw 42418488aeb5Staylor /* 42428488aeb5Staylor * determine where a partition starts on a disk in the current 42438488aeb5Staylor * configuration 42448488aeb5Staylor */ 42458488aeb5Staylor static diskaddr_t 42468488aeb5Staylor find_start_block(nvlist_t *config) 42478488aeb5Staylor { 42488488aeb5Staylor nvlist_t **child; 42498488aeb5Staylor uint_t c, children; 42508488aeb5Staylor diskaddr_t sb = MAXOFFSET_T; 42518488aeb5Staylor uint64_t wholedisk; 42528488aeb5Staylor 42538488aeb5Staylor if (nvlist_lookup_nvlist_array(config, 42548488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) { 42558488aeb5Staylor if (nvlist_lookup_uint64(config, 42568488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK, 42578488aeb5Staylor &wholedisk) != 0 || !wholedisk) { 42588488aeb5Staylor return (MAXOFFSET_T); 42598488aeb5Staylor } 42607855d95bSToomas Soome if (read_efi_label(config, &sb, NULL) < 0) 426115e6edf1Sgw sb = MAXOFFSET_T; 42628488aeb5Staylor return (sb); 42638488aeb5Staylor } 42648488aeb5Staylor 42658488aeb5Staylor for (c = 0; c < children; c++) { 42668488aeb5Staylor sb = find_start_block(child[c]); 42678488aeb5Staylor if (sb != MAXOFFSET_T) { 42688488aeb5Staylor return (sb); 42698488aeb5Staylor } 42708488aeb5Staylor } 42718488aeb5Staylor return (MAXOFFSET_T); 42728488aeb5Staylor } 42738488aeb5Staylor 42748488aeb5Staylor /* 42758488aeb5Staylor * Label an individual disk. The name provided is the short name, 42768488aeb5Staylor * stripped of any leading /dev path. 42778488aeb5Staylor */ 42788488aeb5Staylor int 42797855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name, 42807855d95bSToomas Soome zpool_boot_label_t boot_type, uint64_t boot_size, int *slice) 42818488aeb5Staylor { 42828488aeb5Staylor char path[MAXPATHLEN]; 42838488aeb5Staylor struct dk_gpt *vtoc; 42848488aeb5Staylor int fd; 42858488aeb5Staylor size_t resv = EFI_MIN_RESV_SIZE; 42868488aeb5Staylor uint64_t slice_size; 42878488aeb5Staylor diskaddr_t start_block; 42888488aeb5Staylor char errbuf[1024]; 42898488aeb5Staylor 4290c6ef114fSmmusante /* prepare an error message just in case */ 4291c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf), 4292c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name); 4293c6ef114fSmmusante 42948488aeb5Staylor if (zhp) { 42958488aeb5Staylor nvlist_t *nvroot; 42968488aeb5Staylor 42978488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config, 42988488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 42998488aeb5Staylor 43008488aeb5Staylor if (zhp->zpool_start_block == 0) 43018488aeb5Staylor start_block = find_start_block(nvroot); 43028488aeb5Staylor else 43038488aeb5Staylor start_block = zhp->zpool_start_block; 43048488aeb5Staylor zhp->zpool_start_block = start_block; 43058488aeb5Staylor } else { 43068488aeb5Staylor /* new pool */ 43078488aeb5Staylor start_block = NEW_START_BLOCK; 43088488aeb5Staylor } 43098488aeb5Staylor 43106401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name, 43118488aeb5Staylor BACKUP_SLICE); 43128488aeb5Staylor 43138488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) { 43148488aeb5Staylor /* 43158488aeb5Staylor * This shouldn't happen. We've long since verified that this 43168488aeb5Staylor * is a valid device. 43178488aeb5Staylor */ 4318c6ef114fSmmusante zfs_error_aux(hdl, 4319c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device")); 43208488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf)); 43218488aeb5Staylor } 43228488aeb5Staylor 43238488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 43248488aeb5Staylor /* 43258488aeb5Staylor * The only way this can fail is if we run out of memory, or we 43268488aeb5Staylor * were unable to read the disk's capacity 43278488aeb5Staylor */ 43288488aeb5Staylor if (errno == ENOMEM) 43298488aeb5Staylor (void) no_memory(hdl); 43308488aeb5Staylor 43318488aeb5Staylor (void) close(fd); 4332c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4333c6ef114fSmmusante "unable to read disk capacity"), name); 43348488aeb5Staylor 43358488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf)); 43368488aeb5Staylor } 43378488aeb5Staylor 43388488aeb5Staylor /* 43398488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered 43408488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup 43418488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size 43428488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT, 43438488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we 43448488aeb5Staylor * can get, in the absence of V_OTHER. 43458488aeb5Staylor */ 43467855d95bSToomas Soome /* first fix the partition start block */ 43477855d95bSToomas Soome if (start_block == MAXOFFSET_T) 43487855d95bSToomas Soome start_block = NEW_START_BLOCK; 43498488aeb5Staylor 43507855d95bSToomas Soome /* 43517855d95bSToomas Soome * EFI System partition is using slice 0. 43527855d95bSToomas Soome * ZFS is on slice 1 and slice 8 is reserved. 43537855d95bSToomas Soome * We assume the GPT partition table without system 43547855d95bSToomas Soome * partition has zfs p_start == NEW_START_BLOCK. 43557855d95bSToomas Soome * If start_block != NEW_START_BLOCK, it means we have 43567855d95bSToomas Soome * system partition. Correct solution would be to query/cache vtoc 43577855d95bSToomas Soome * from existing vdev member. 43587855d95bSToomas Soome */ 43597855d95bSToomas Soome if (boot_type == ZPOOL_CREATE_BOOT_LABEL) { 43607855d95bSToomas Soome if (boot_size % vtoc->efi_lbasize != 0) { 43617855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43627855d95bSToomas Soome "boot partition size must be a multiple of %d"), 43637855d95bSToomas Soome vtoc->efi_lbasize); 43647855d95bSToomas Soome (void) close(fd); 43657855d95bSToomas Soome efi_free(vtoc); 43667855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 43677855d95bSToomas Soome } 43687855d95bSToomas Soome /* 43697855d95bSToomas Soome * System partition size checks. 43707855d95bSToomas Soome * Note the 1MB is quite arbitrary value, since we 43717855d95bSToomas Soome * are creating dedicated pool, it should be enough 43727855d95bSToomas Soome * to hold fat + efi bootloader. May need to be 43737855d95bSToomas Soome * adjusted if the bootloader size will grow. 43747855d95bSToomas Soome */ 43757855d95bSToomas Soome if (boot_size < 1024 * 1024) { 43767855d95bSToomas Soome char buf[64]; 43777855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 43787855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43797855d95bSToomas Soome "Specified size %s for EFI System partition is too " 43807855d95bSToomas Soome "small, the minimum size is 1MB."), buf); 43817855d95bSToomas Soome (void) close(fd); 43827855d95bSToomas Soome efi_free(vtoc); 43837855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 43847855d95bSToomas Soome } 43857855d95bSToomas Soome /* 33MB is tested with mkfs -F pcfs */ 43867855d95bSToomas Soome if (hdl->libzfs_printerr && 43877855d95bSToomas Soome ((vtoc->efi_lbasize == 512 && 43887855d95bSToomas Soome boot_size < 33 * 1024 * 1024) || 43897855d95bSToomas Soome (vtoc->efi_lbasize == 4096 && 43907855d95bSToomas Soome boot_size < 256 * 1024 * 1024))) { 43917855d95bSToomas Soome char buf[64]; 43927855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf)); 43937855d95bSToomas Soome (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 43947855d95bSToomas Soome "Warning: EFI System partition size %s is " 43957855d95bSToomas Soome "not allowing to create FAT32 file\nsystem, which " 43967855d95bSToomas Soome "may result in unbootable system.\n"), buf); 43977855d95bSToomas Soome } 43987855d95bSToomas Soome /* Adjust zfs partition start by size of system partition. */ 43997855d95bSToomas Soome start_block += boot_size / vtoc->efi_lbasize; 44007855d95bSToomas Soome } 44017855d95bSToomas Soome 44027855d95bSToomas Soome if (start_block == NEW_START_BLOCK) { 44037855d95bSToomas Soome /* 44047855d95bSToomas Soome * Use default layout. 44057855d95bSToomas Soome * ZFS is on slice 0 and slice 8 is reserved. 44067855d95bSToomas Soome */ 44077855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1; 44087855d95bSToomas Soome slice_size -= EFI_MIN_RESV_SIZE; 44097855d95bSToomas Soome slice_size -= start_block; 44107855d95bSToomas Soome if (slice != NULL) 44117855d95bSToomas Soome *slice = 0; 44127855d95bSToomas Soome 44137855d95bSToomas Soome vtoc->efi_parts[0].p_start = start_block; 44147855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 44157855d95bSToomas Soome 44167855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_USR; 44177855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "zfs"); 44187855d95bSToomas Soome 44197855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 44207855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 44217855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 44227855d95bSToomas Soome } else { 44237855d95bSToomas Soome slice_size = start_block - NEW_START_BLOCK; 44247855d95bSToomas Soome vtoc->efi_parts[0].p_start = NEW_START_BLOCK; 44257855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size; 44267855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_SYSTEM; 44277855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "loader"); 44287855d95bSToomas Soome if (slice != NULL) 44297855d95bSToomas Soome *slice = 1; 44307855d95bSToomas Soome /* prepare slice 1 */ 44317855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1 - slice_size; 44327855d95bSToomas Soome slice_size -= resv; 44337855d95bSToomas Soome slice_size -= NEW_START_BLOCK; 44347855d95bSToomas Soome vtoc->efi_parts[1].p_start = start_block; 44357855d95bSToomas Soome vtoc->efi_parts[1].p_size = slice_size; 44367855d95bSToomas Soome vtoc->efi_parts[1].p_tag = V_USR; 44377855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[1].p_name, "zfs"); 44387855d95bSToomas Soome 44397855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block; 44407855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv; 44417855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED; 44427855d95bSToomas Soome } 44438488aeb5Staylor 44448488aeb5Staylor if (efi_write(fd, vtoc) != 0) { 44458488aeb5Staylor /* 44468488aeb5Staylor * Some block drivers (like pcata) may not support EFI 44478488aeb5Staylor * GPT labels. Print out a helpful error message dir- 44488488aeb5Staylor * ecting the user to manually label the disk and give 44498488aeb5Staylor * a specific slice. 44508488aeb5Staylor */ 44518488aeb5Staylor (void) close(fd); 44528488aeb5Staylor efi_free(vtoc); 44538488aeb5Staylor 44548488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4455c6ef114fSmmusante "try using fdisk(1M) and then provide a specific slice")); 44568488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf)); 44578488aeb5Staylor } 44588488aeb5Staylor 44598488aeb5Staylor (void) close(fd); 44608488aeb5Staylor efi_free(vtoc); 44618488aeb5Staylor return (0); 44628488aeb5Staylor } 4463e7cbe64fSgw 4464e7cbe64fSgw static boolean_t 4465e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf) 4466e7cbe64fSgw { 4467e7cbe64fSgw char *type; 4468e7cbe64fSgw nvlist_t **child; 4469e7cbe64fSgw uint_t children, c; 4470e7cbe64fSgw 4471e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0); 4472810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 || 447388ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 || 4474e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) { 4475e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4476e7cbe64fSgw "vdev type '%s' is not supported"), type); 4477e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf); 4478e7cbe64fSgw return (B_FALSE); 4479e7cbe64fSgw } 4480e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, 4481e7cbe64fSgw &child, &children) == 0) { 4482e7cbe64fSgw for (c = 0; c < children; c++) { 4483e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf)) 4484e7cbe64fSgw return (B_FALSE); 4485e7cbe64fSgw } 4486e7cbe64fSgw } 4487e7cbe64fSgw return (B_TRUE); 4488e7cbe64fSgw } 4489e7cbe64fSgw 4490e7cbe64fSgw /* 4491810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if 4492810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol. 4493810e43b2SBill Pijewski * 4494810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and 4495810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or 4496810e43b2SBill Pijewski * have missing/hole vdevs are not suitable. 4497e7cbe64fSgw */ 4498e7cbe64fSgw int 4499e7cbe64fSgw zvol_check_dump_config(char *arg) 4500e7cbe64fSgw { 4501e7cbe64fSgw zpool_handle_t *zhp = NULL; 4502e7cbe64fSgw nvlist_t *config, *nvroot; 4503e7cbe64fSgw char *p, *volname; 4504e7cbe64fSgw nvlist_t **top; 4505e7cbe64fSgw uint_t toplevels; 4506e7cbe64fSgw libzfs_handle_t *hdl; 4507e7cbe64fSgw char errbuf[1024]; 45089adfa60dSMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN]; 4509e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR); 4510e7cbe64fSgw int ret = 1; 4511e7cbe64fSgw 4512e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) { 4513e7cbe64fSgw return (-1); 4514e7cbe64fSgw } 4515e7cbe64fSgw 4516e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4517e7cbe64fSgw "dump is not supported on device '%s'"), arg); 4518e7cbe64fSgw 4519e7cbe64fSgw if ((hdl = libzfs_init()) == NULL) 4520e7cbe64fSgw return (1); 4521e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE); 4522e7cbe64fSgw 4523e7cbe64fSgw volname = arg + pathlen; 4524e7cbe64fSgw 4525e7cbe64fSgw /* check the configuration of the pool */ 4526e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) { 4527e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4528e7cbe64fSgw "malformed dataset name")); 4529e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4530e7cbe64fSgw return (1); 45319adfa60dSMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) { 4532e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4533e7cbe64fSgw "dataset name is too long")); 4534e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf); 4535e7cbe64fSgw return (1); 4536e7cbe64fSgw } else { 4537e7cbe64fSgw (void) strncpy(poolname, volname, p - volname); 4538e7cbe64fSgw poolname[p - volname] = '\0'; 4539e7cbe64fSgw } 4540e7cbe64fSgw 4541e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) { 4542e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4543e7cbe64fSgw "could not open pool '%s'"), poolname); 4544e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf); 4545e7cbe64fSgw goto out; 4546e7cbe64fSgw } 4547e7cbe64fSgw config = zpool_get_config(zhp, NULL); 4548e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 4549e7cbe64fSgw &nvroot) != 0) { 4550e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4551e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname); 4552e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf); 4553e7cbe64fSgw goto out; 4554e7cbe64fSgw } 4555e7cbe64fSgw 4556e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 4557e7cbe64fSgw &top, &toplevels) == 0); 4558e7cbe64fSgw 4559e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) { 4560e7cbe64fSgw goto out; 4561e7cbe64fSgw } 4562e7cbe64fSgw ret = 0; 4563e7cbe64fSgw 4564e7cbe64fSgw out: 4565e7cbe64fSgw if (zhp) 4566e7cbe64fSgw zpool_close(zhp); 4567e7cbe64fSgw libzfs_fini(hdl); 4568e7cbe64fSgw return (ret); 4569e7cbe64fSgw } 4570