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.
24c4ecba8aSPaul Dagnelie * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25c1d5c2a4SKody Kantor * Copyright 2019 Joyent, Inc.
266401734dSWill Andrews * Copyright 2016 Nexenta Systems, Inc.
2788f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
281702cce7SAlek Pinchuk * Copyright (c) 2017 Datto Inc.
29663207adSDon Brady * Copyright (c) 2017, Intel Corporation.
30c2cd3a44SAndy Fiddaman * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
31b4fb0039SJoshua M. Clulow * Copyright 2022 Oxide Computer Company
32fa9e4066Sahrens */
33fa9e4066Sahrens
34fa9e4066Sahrens #include <ctype.h>
35fa9e4066Sahrens #include <errno.h>
36fa9e4066Sahrens #include <devid.h>
37fa9e4066Sahrens #include <fcntl.h>
38fa9e4066Sahrens #include <libintl.h>
39fa9e4066Sahrens #include <stdio.h>
40fa9e4066Sahrens #include <stdlib.h>
41f3861e1aSahl #include <strings.h>
42fa9e4066Sahrens #include <unistd.h>
434445fffbSMatthew Ahrens #include <libgen.h>
44c2cd3a44SAndy Fiddaman #include <sys/dkio.h>
458488aeb5Staylor #include <sys/efi_partition.h>
468488aeb5Staylor #include <sys/vtoc.h>
47fa9e4066Sahrens #include <sys/zfs_ioctl.h>
487d6cab3fSJoshua M. Clulow #include <sys/modctl.h>
497d6cab3fSJoshua M. Clulow #include <sys/mkdev.h>
50573ca77eSGeorge Wilson #include <dlfcn.h>
51d8ab6e12SDon Brady #include <libzutil.h>
52fa9e4066Sahrens
53fa9e4066Sahrens #include "zfs_namecheck.h"
54b1b8ab34Slling #include "zfs_prop.h"
55fa9e4066Sahrens #include "libzfs_impl.h"
56468c413aSTim Haley #include "zfs_comutil.h"
57ad135b5dSChristopher Siden #include "zfeature_common.h"
58fa9e4066Sahrens
597855d95bSToomas Soome static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
602ba5f978SAlan Somers static boolean_t zpool_vdev_is_interior(const char *name);
61990b4856Slling
62573ca77eSGeorge Wilson #define BACKUP_SLICE "s2"
63573ca77eSGeorge Wilson
64f9af39baSGeorge Wilson typedef struct prop_flags {
65f9af39baSGeorge Wilson int create:1; /* Validate property on creation */
66f9af39baSGeorge Wilson int import:1; /* Validate property on import */
67f9af39baSGeorge Wilson } prop_flags_t;
68f9af39baSGeorge Wilson
69990b4856Slling /*
70990b4856Slling * ====================================================================
71990b4856Slling * zpool property functions
72990b4856Slling * ====================================================================
73990b4856Slling */
74990b4856Slling
75990b4856Slling static int
zpool_get_all_props(zpool_handle_t * zhp)76990b4856Slling zpool_get_all_props(zpool_handle_t *zhp)
77990b4856Slling {
78990b4856Slling zfs_cmd_t zc = { 0 };
79990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl;
80990b4856Slling
81990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
82990b4856Slling
83990b4856Slling if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
84990b4856Slling return (-1);
85990b4856Slling
86990b4856Slling while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
87990b4856Slling if (errno == ENOMEM) {
88990b4856Slling if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
89990b4856Slling zcmd_free_nvlists(&zc);
90990b4856Slling return (-1);
91990b4856Slling }
92990b4856Slling } else {
93990b4856Slling zcmd_free_nvlists(&zc);
94990b4856Slling return (-1);
95990b4856Slling }
96990b4856Slling }
97990b4856Slling
98990b4856Slling if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
99990b4856Slling zcmd_free_nvlists(&zc);
100990b4856Slling return (-1);
101990b4856Slling }
102990b4856Slling
103990b4856Slling zcmd_free_nvlists(&zc);
104990b4856Slling
105990b4856Slling return (0);
106990b4856Slling }
107990b4856Slling
108990b4856Slling static int
zpool_props_refresh(zpool_handle_t * zhp)109990b4856Slling zpool_props_refresh(zpool_handle_t *zhp)
110990b4856Slling {
111990b4856Slling nvlist_t *old_props;
112990b4856Slling
113990b4856Slling old_props = zhp->zpool_props;
114990b4856Slling
115990b4856Slling if (zpool_get_all_props(zhp) != 0)
116990b4856Slling return (-1);
117990b4856Slling
118990b4856Slling nvlist_free(old_props);
119990b4856Slling return (0);
120990b4856Slling }
121990b4856Slling
122990b4856Slling static char *
zpool_get_prop_string(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)123990b4856Slling zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
124990b4856Slling zprop_source_t *src)
125990b4856Slling {
126990b4856Slling nvlist_t *nv, *nvl;
127990b4856Slling uint64_t ival;
128990b4856Slling char *value;
129990b4856Slling zprop_source_t source;
130990b4856Slling
131990b4856Slling nvl = zhp->zpool_props;
132990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
133990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
134990b4856Slling source = ival;
135990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
136990b4856Slling } else {
137990b4856Slling source = ZPROP_SRC_DEFAULT;
138990b4856Slling if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
139990b4856Slling value = "-";
140990b4856Slling }
141990b4856Slling
142990b4856Slling if (src)
143990b4856Slling *src = source;
144990b4856Slling
145990b4856Slling return (value);
146990b4856Slling }
147990b4856Slling
148990b4856Slling uint64_t
zpool_get_prop_int(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)149990b4856Slling zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
150990b4856Slling {
151990b4856Slling nvlist_t *nv, *nvl;
152990b4856Slling uint64_t value;
153990b4856Slling zprop_source_t source;
154990b4856Slling
155b87f3af3Sperrin if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
156b87f3af3Sperrin /*
157b87f3af3Sperrin * zpool_get_all_props() has most likely failed because
158b87f3af3Sperrin * the pool is faulted, but if all we need is the top level
159b87f3af3Sperrin * vdev's guid then get it from the zhp config nvlist.
160b87f3af3Sperrin */
161b87f3af3Sperrin if ((prop == ZPOOL_PROP_GUID) &&
162b87f3af3Sperrin (nvlist_lookup_nvlist(zhp->zpool_config,
163b87f3af3Sperrin ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
164b87f3af3Sperrin (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
165b87f3af3Sperrin == 0)) {
166b87f3af3Sperrin return (value);
167b87f3af3Sperrin }
168990b4856Slling return (zpool_prop_default_numeric(prop));
169b87f3af3Sperrin }
170990b4856Slling
171990b4856Slling nvl = zhp->zpool_props;
172990b4856Slling if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
173990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
174990b4856Slling source = value;
175990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
176990b4856Slling } else {
177990b4856Slling source = ZPROP_SRC_DEFAULT;
178990b4856Slling value = zpool_prop_default_numeric(prop);
179990b4856Slling }
180990b4856Slling
181990b4856Slling if (src)
182990b4856Slling *src = source;
183990b4856Slling
184990b4856Slling return (value);
185990b4856Slling }
186990b4856Slling
187990b4856Slling /*
188990b4856Slling * Map VDEV STATE to printed strings.
189990b4856Slling */
1906401734dSWill Andrews const char *
zpool_state_to_name(vdev_state_t state,vdev_aux_t aux)191990b4856Slling zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
192990b4856Slling {
193990b4856Slling switch (state) {
194990b4856Slling case VDEV_STATE_CLOSED:
195990b4856Slling case VDEV_STATE_OFFLINE:
196990b4856Slling return (gettext("OFFLINE"));
197990b4856Slling case VDEV_STATE_REMOVED:
198990b4856Slling return (gettext("REMOVED"));
199990b4856Slling case VDEV_STATE_CANT_OPEN:
200b87f3af3Sperrin if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
201990b4856Slling return (gettext("FAULTED"));
2021195e687SMark J Musante else if (aux == VDEV_AUX_SPLIT_POOL)
2031195e687SMark J Musante return (gettext("SPLIT"));
204990b4856Slling else
205990b4856Slling return (gettext("UNAVAIL"));
206990b4856Slling case VDEV_STATE_FAULTED:
207990b4856Slling return (gettext("FAULTED"));
208990b4856Slling case VDEV_STATE_DEGRADED:
209990b4856Slling return (gettext("DEGRADED"));
210990b4856Slling case VDEV_STATE_HEALTHY:
211990b4856Slling return (gettext("ONLINE"));
21288f61deeSIgor Kozhukhov
21388f61deeSIgor Kozhukhov default:
21488f61deeSIgor Kozhukhov break;
215990b4856Slling }
216990b4856Slling
217990b4856Slling return (gettext("UNKNOWN"));
218990b4856Slling }
219990b4856Slling
2206401734dSWill Andrews /*
2216401734dSWill Andrews * Map POOL STATE to printed strings.
2226401734dSWill Andrews */
2236401734dSWill Andrews const char *
zpool_pool_state_to_name(pool_state_t state)2246401734dSWill Andrews zpool_pool_state_to_name(pool_state_t state)
2256401734dSWill Andrews {
2266401734dSWill Andrews switch (state) {
2276401734dSWill Andrews case POOL_STATE_ACTIVE:
2286401734dSWill Andrews return (gettext("ACTIVE"));
2296401734dSWill Andrews case POOL_STATE_EXPORTED:
2306401734dSWill Andrews return (gettext("EXPORTED"));
2316401734dSWill Andrews case POOL_STATE_DESTROYED:
2326401734dSWill Andrews return (gettext("DESTROYED"));
2336401734dSWill Andrews case POOL_STATE_SPARE:
2346401734dSWill Andrews return (gettext("SPARE"));
2356401734dSWill Andrews case POOL_STATE_L2CACHE:
2366401734dSWill Andrews return (gettext("L2CACHE"));
2376401734dSWill Andrews case POOL_STATE_UNINITIALIZED:
2386401734dSWill Andrews return (gettext("UNINITIALIZED"));
2396401734dSWill Andrews case POOL_STATE_UNAVAIL:
2406401734dSWill Andrews return (gettext("UNAVAIL"));
2416401734dSWill Andrews case POOL_STATE_POTENTIALLY_ACTIVE:
2426401734dSWill Andrews return (gettext("POTENTIALLY_ACTIVE"));
2436401734dSWill Andrews }
2446401734dSWill Andrews
2456401734dSWill Andrews return (gettext("UNKNOWN"));
2466401734dSWill Andrews }
2476401734dSWill Andrews
248990b4856Slling /*
249990b4856Slling * Get a zpool property value for 'prop' and return the value in
250990b4856Slling * a pre-allocated buffer.
251990b4856Slling */
252990b4856Slling int
zpool_get_prop(zpool_handle_t * zhp,zpool_prop_t prop,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)253990b4856Slling zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
254c58b3526SAdam Stevko zprop_source_t *srctype, boolean_t literal)
255990b4856Slling {
256990b4856Slling uint64_t intval;
257990b4856Slling const char *strval;
258990b4856Slling zprop_source_t src = ZPROP_SRC_NONE;
259990b4856Slling nvlist_t *nvroot;
260990b4856Slling vdev_stat_t *vs;
261990b4856Slling uint_t vsc;
262990b4856Slling
263990b4856Slling if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
264379c004dSEric Schrock switch (prop) {
265379c004dSEric Schrock case ZPOOL_PROP_NAME:
266990b4856Slling (void) strlcpy(buf, zpool_get_name(zhp), len);
267379c004dSEric Schrock break;
268379c004dSEric Schrock
269379c004dSEric Schrock case ZPOOL_PROP_HEALTH:
270990b4856Slling (void) strlcpy(buf, "FAULTED", len);
271379c004dSEric Schrock break;
272379c004dSEric Schrock
273379c004dSEric Schrock case ZPOOL_PROP_GUID:
274379c004dSEric Schrock intval = zpool_get_prop_int(zhp, prop, &src);
275379c004dSEric Schrock (void) snprintf(buf, len, "%llu", intval);
276379c004dSEric Schrock break;
277379c004dSEric Schrock
278379c004dSEric Schrock case ZPOOL_PROP_ALTROOT:
279379c004dSEric Schrock case ZPOOL_PROP_CACHEFILE:
2808704186eSDan McDonald case ZPOOL_PROP_COMMENT:
281379c004dSEric Schrock if (zhp->zpool_props != NULL ||
282379c004dSEric Schrock zpool_get_all_props(zhp) == 0) {
283379c004dSEric Schrock (void) strlcpy(buf,
284379c004dSEric Schrock zpool_get_prop_string(zhp, prop, &src),
285379c004dSEric Schrock len);
286c58b3526SAdam Stevko break;
287379c004dSEric Schrock }
288379c004dSEric Schrock /* FALLTHROUGH */
289379c004dSEric Schrock default:
290990b4856Slling (void) strlcpy(buf, "-", len);
291379c004dSEric Schrock break;
292379c004dSEric Schrock }
293379c004dSEric Schrock
294379c004dSEric Schrock if (srctype != NULL)
295379c004dSEric Schrock *srctype = src;
296990b4856Slling return (0);
297990b4856Slling }
298990b4856Slling
299990b4856Slling if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
300990b4856Slling prop != ZPOOL_PROP_NAME)
301990b4856Slling return (-1);
302990b4856Slling
303990b4856Slling switch (zpool_prop_get_type(prop)) {
304990b4856Slling case PROP_TYPE_STRING:
305990b4856Slling (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
306990b4856Slling len);
307990b4856Slling break;
308990b4856Slling
309990b4856Slling case PROP_TYPE_NUMBER:
310990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src);
311990b4856Slling
312990b4856Slling switch (prop) {
313990b4856Slling case ZPOOL_PROP_SIZE:
314485bbbf5SGeorge Wilson case ZPOOL_PROP_ALLOCATED:
315485bbbf5SGeorge Wilson case ZPOOL_PROP_FREE:
316ad135b5dSChristopher Siden case ZPOOL_PROP_FREEING:
3177fd05ac4SMatthew Ahrens case ZPOOL_PROP_LEAKED:
3185711d393Sloli case ZPOOL_PROP_ASHIFT:
319c58b3526SAdam Stevko if (literal) {
320c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu",
321c58b3526SAdam Stevko (u_longlong_t)intval);
322c58b3526SAdam Stevko } else {
323c58b3526SAdam Stevko (void) zfs_nicenum(intval, buf, len);
324c58b3526SAdam Stevko }
325990b4856Slling break;
3267855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE:
3277a09f97bSGeorge Wilson case ZPOOL_PROP_EXPANDSZ:
32886714001SSerapheim Dimitropoulos case ZPOOL_PROP_CHECKPOINT:
3297a09f97bSGeorge Wilson if (intval == 0) {
3307a09f97bSGeorge Wilson (void) strlcpy(buf, "-", len);
3317a09f97bSGeorge Wilson } else if (literal) {
3327a09f97bSGeorge Wilson (void) snprintf(buf, len, "%llu",
3337a09f97bSGeorge Wilson (u_longlong_t)intval);
3347a09f97bSGeorge Wilson } else {
335*6520eed5SToomas Soome (void) zfs_nicebytes(intval, buf, len);
3367a09f97bSGeorge Wilson }
3377a09f97bSGeorge Wilson break;
338990b4856Slling case ZPOOL_PROP_CAPACITY:
339c58b3526SAdam Stevko if (literal) {
340c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu",
341c58b3526SAdam Stevko (u_longlong_t)intval);
342c58b3526SAdam Stevko } else {
343c58b3526SAdam Stevko (void) snprintf(buf, len, "%llu%%",
344c58b3526SAdam Stevko (u_longlong_t)intval);
345c58b3526SAdam Stevko }
346990b4856Slling break;
3472e4c9986SGeorge Wilson case ZPOOL_PROP_FRAGMENTATION:
3482e4c9986SGeorge Wilson if (intval == UINT64_MAX) {
3492e4c9986SGeorge Wilson (void) strlcpy(buf, "-", len);
3503dfe8017SChrister Ekholm } else if (literal) {
3513dfe8017SChrister Ekholm (void) snprintf(buf, len, "%llu",
3523dfe8017SChrister Ekholm (u_longlong_t)intval);
3532e4c9986SGeorge Wilson } else {
3542e4c9986SGeorge Wilson (void) snprintf(buf, len, "%llu%%",
3552e4c9986SGeorge Wilson (u_longlong_t)intval);
3562e4c9986SGeorge Wilson }
3572e4c9986SGeorge Wilson break;
358b24ab676SJeff Bonwick case ZPOOL_PROP_DEDUPRATIO:
3593dfe8017SChrister Ekholm if (literal)
3603dfe8017SChrister Ekholm (void) snprintf(buf, len, "%llu.%02llu",
3613dfe8017SChrister Ekholm (u_longlong_t)(intval / 100),
3623dfe8017SChrister Ekholm (u_longlong_t)(intval % 100));
3633dfe8017SChrister Ekholm else
3643dfe8017SChrister Ekholm (void) snprintf(buf, len, "%llu.%02llux",
3653dfe8017SChrister Ekholm (u_longlong_t)(intval / 100),
3663dfe8017SChrister Ekholm (u_longlong_t)(intval % 100));
367b24ab676SJeff Bonwick break;
368990b4856Slling case ZPOOL_PROP_HEALTH:
369990b4856Slling verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
370990b4856Slling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
371990b4856Slling verify(nvlist_lookup_uint64_array(nvroot,
3723f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3733f9d6ad7SLin Ling == 0);
374990b4856Slling
375990b4856Slling (void) strlcpy(buf, zpool_state_to_name(intval,
376990b4856Slling vs->vs_aux), len);
377990b4856Slling break;
378ad135b5dSChristopher Siden case ZPOOL_PROP_VERSION:
379ad135b5dSChristopher Siden if (intval >= SPA_VERSION_FEATURES) {
380ad135b5dSChristopher Siden (void) snprintf(buf, len, "-");
381ad135b5dSChristopher Siden break;
382ad135b5dSChristopher Siden }
383ad135b5dSChristopher Siden /* FALLTHROUGH */
384990b4856Slling default:
385990b4856Slling (void) snprintf(buf, len, "%llu", intval);
386990b4856Slling }
387990b4856Slling break;
388990b4856Slling
389990b4856Slling case PROP_TYPE_INDEX:
390990b4856Slling intval = zpool_get_prop_int(zhp, prop, &src);
391990b4856Slling if (zpool_prop_index_to_string(prop, intval, &strval)
392990b4856Slling != 0)
393990b4856Slling return (-1);
394990b4856Slling (void) strlcpy(buf, strval, len);
395990b4856Slling break;
396990b4856Slling
397990b4856Slling default:
398990b4856Slling abort();
399990b4856Slling }
400990b4856Slling
401990b4856Slling if (srctype)
402990b4856Slling *srctype = src;
403990b4856Slling
404990b4856Slling return (0);
405990b4856Slling }
406990b4856Slling
407990b4856Slling /*
408990b4856Slling * Check if the bootfs name has the same pool name as it is set to.
409990b4856Slling * Assuming bootfs is a valid dataset name.
410990b4856Slling */
411990b4856Slling static boolean_t
bootfs_name_valid(const char * pool,const char * bootfs)412c4ecba8aSPaul Dagnelie bootfs_name_valid(const char *pool, const char *bootfs)
413990b4856Slling {
414990b4856Slling int len = strlen(pool);
415015f38bbSPaul Dagnelie if (bootfs[0] == '\0')
416015f38bbSPaul Dagnelie return (B_TRUE);
417990b4856Slling
418fe3e2633SEric Taylor if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
419990b4856Slling return (B_FALSE);
420990b4856Slling
421990b4856Slling if (strncmp(pool, bootfs, len) == 0 &&
422990b4856Slling (bootfs[len] == '/' || bootfs[len] == '\0'))
423990b4856Slling return (B_TRUE);
424990b4856Slling
425990b4856Slling return (B_FALSE);
426990b4856Slling }
427990b4856Slling
4284263d13fSGeorge Wilson boolean_t
zpool_is_bootable(zpool_handle_t * zhp)4294263d13fSGeorge Wilson zpool_is_bootable(zpool_handle_t *zhp)
430b5b76fecSGeorge Wilson {
4319adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN];
432b5b76fecSGeorge Wilson
433b5b76fecSGeorge Wilson return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
434c58b3526SAdam Stevko sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
435b5b76fecSGeorge Wilson sizeof (bootfs)) != 0);
436b5b76fecSGeorge Wilson }
437b5b76fecSGeorge Wilson
438b5b76fecSGeorge Wilson
439990b4856Slling /*
440990b4856Slling * Given an nvlist of zpool properties to be set, validate that they are
441990b4856Slling * correct, and parse any numeric properties (index, boolean, etc) if they are
442990b4856Slling * specified as strings.
443990b4856Slling */
444990b4856Slling static nvlist_t *
zpool_valid_proplist(libzfs_handle_t * hdl,const char * poolname,nvlist_t * props,uint64_t version,prop_flags_t flags,char * errbuf)4450a48a24eStimh zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
446f9af39baSGeorge Wilson nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
447990b4856Slling {
448990b4856Slling nvpair_t *elem;
449990b4856Slling nvlist_t *retprops;
450990b4856Slling zpool_prop_t prop;
451990b4856Slling char *strval;
452990b4856Slling uint64_t intval;
4538704186eSDan McDonald char *slash, *check;
4542f8aaab3Seschrock struct stat64 statbuf;
45515e6edf1Sgw zpool_handle_t *zhp;
456990b4856Slling
457990b4856Slling if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
458990b4856Slling (void) no_memory(hdl);
459990b4856Slling return (NULL);
460990b4856Slling }
461990b4856Slling
462990b4856Slling elem = NULL;
463990b4856Slling while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
464990b4856Slling const char *propname = nvpair_name(elem);
465990b4856Slling
466ad135b5dSChristopher Siden prop = zpool_name_to_prop(propname);
4674ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
468ad135b5dSChristopher Siden int err;
469ad135b5dSChristopher Siden char *fname = strchr(propname, '@') + 1;
470ad135b5dSChristopher Siden
4712acef22dSMatthew Ahrens err = zfeature_lookup_name(fname, NULL);
472ad135b5dSChristopher Siden if (err != 0) {
473ad135b5dSChristopher Siden ASSERT3U(err, ==, ENOENT);
474ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4755711d393Sloli "invalid feature '%s', '%s'"), fname,
4765711d393Sloli propname);
477ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
478ad135b5dSChristopher Siden goto error;
479ad135b5dSChristopher Siden }
480ad135b5dSChristopher Siden
481ad135b5dSChristopher Siden if (nvpair_type(elem) != DATA_TYPE_STRING) {
482ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
483ad135b5dSChristopher Siden "'%s' must be a string"), propname);
484ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
485ad135b5dSChristopher Siden goto error;
486ad135b5dSChristopher Siden }
487ad135b5dSChristopher Siden
488ad135b5dSChristopher Siden (void) nvpair_value_string(elem, &strval);
489f3434519SLOLi if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
490f3434519SLOLi strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
491ad135b5dSChristopher Siden zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492ad135b5dSChristopher Siden "property '%s' can only be set to "
493f3434519SLOLi "'enabled' or 'disabled'"), propname);
494ad135b5dSChristopher Siden (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
495ad135b5dSChristopher Siden goto error;
496ad135b5dSChristopher Siden }
497ad135b5dSChristopher Siden
498ad135b5dSChristopher Siden if (nvlist_add_uint64(retprops, propname, 0) != 0) {
499ad135b5dSChristopher Siden (void) no_memory(hdl);
500ad135b5dSChristopher Siden goto error;
501ad135b5dSChristopher Siden }
502ad135b5dSChristopher Siden continue;
503ad135b5dSChristopher Siden }
504ad135b5dSChristopher Siden
505990b4856Slling /*
506990b4856Slling * Make sure this property is valid and applies to this type.
507990b4856Slling */
5084ae5f5f0SAlan Somers if (prop == ZPOOL_PROP_INVAL) {
509990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
510990b4856Slling "invalid property '%s'"), propname);
511990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
512990b4856Slling goto error;
513990b4856Slling }
514990b4856Slling
515990b4856Slling if (zpool_prop_readonly(prop)) {
516990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
517990b4856Slling "is readonly"), propname);
518990b4856Slling (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
519990b4856Slling goto error;
520990b4856Slling }
521990b4856Slling
522990b4856Slling if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
523990b4856Slling &strval, &intval, errbuf) != 0)
524990b4856Slling goto error;
525990b4856Slling
526990b4856Slling /*
527990b4856Slling * Perform additional checking for specific properties.
528990b4856Slling */
529990b4856Slling switch (prop) {
530990b4856Slling case ZPOOL_PROP_VERSION:
531ad135b5dSChristopher Siden if (intval < version ||
532ad135b5dSChristopher Siden !SPA_VERSION_IS_SUPPORTED(intval)) {
533990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
534990b4856Slling "property '%s' number %d is invalid."),
535990b4856Slling propname, intval);
536990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
537990b4856Slling goto error;
538990b4856Slling }
539990b4856Slling break;
540990b4856Slling
5417855d95bSToomas Soome case ZPOOL_PROP_BOOTSIZE:
5427855d95bSToomas Soome if (!flags.create) {
5437855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5447855d95bSToomas Soome "property '%s' can only be set during pool "
5457855d95bSToomas Soome "creation"), propname);
5467855d95bSToomas Soome (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5477855d95bSToomas Soome goto error;
5487855d95bSToomas Soome }
5497855d95bSToomas Soome break;
5507855d95bSToomas Soome
5515711d393Sloli case ZPOOL_PROP_ASHIFT:
5525711d393Sloli if (intval != 0 &&
5535711d393Sloli (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
5545711d393Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5555711d393Sloli "invalid '%s=%d' property: only values "
5565711d393Sloli "between %" PRId32 " and %" PRId32 " "
5575711d393Sloli "are allowed.\n"),
5585711d393Sloli propname, intval, ASHIFT_MIN, ASHIFT_MAX);
5595711d393Sloli (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5605711d393Sloli goto error;
5615711d393Sloli }
5625711d393Sloli break;
5635711d393Sloli
564990b4856Slling case ZPOOL_PROP_BOOTFS:
565f9af39baSGeorge Wilson if (flags.create || flags.import) {
566990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
567990b4856Slling "property '%s' cannot be set at creation "
568990b4856Slling "or import time"), propname);
569990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
570990b4856Slling goto error;
571990b4856Slling }
572990b4856Slling
573990b4856Slling if (version < SPA_VERSION_BOOTFS) {
574990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
575990b4856Slling "pool must be upgraded to support "
576990b4856Slling "'%s' property"), propname);
577990b4856Slling (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
578990b4856Slling goto error;
579990b4856Slling }
580990b4856Slling
581990b4856Slling /*
582990b4856Slling * bootfs property value has to be a dataset name and
583990b4856Slling * the dataset has to be in the same pool as it sets to.
584990b4856Slling */
585015f38bbSPaul Dagnelie if (!bootfs_name_valid(poolname, strval)) {
586990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
587990b4856Slling "is an invalid name"), strval);
588990b4856Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
589990b4856Slling goto error;
590990b4856Slling }
59115e6edf1Sgw
59215e6edf1Sgw if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
59315e6edf1Sgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
59415e6edf1Sgw "could not open pool '%s'"), poolname);
59515e6edf1Sgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
59615e6edf1Sgw goto error;
59715e6edf1Sgw }
59815e6edf1Sgw zpool_close(zhp);
599990b4856Slling break;
600990b4856Slling
6012f8aaab3Seschrock case ZPOOL_PROP_ALTROOT:
602f9af39baSGeorge Wilson if (!flags.create && !flags.import) {
603990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
604990b4856Slling "property '%s' can only be set during pool "
605990b4856Slling "creation or import"), propname);
606990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
607990b4856Slling goto error;
608990b4856Slling }
609990b4856Slling
6102f8aaab3Seschrock if (strval[0] != '/') {
611990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6122f8aaab3Seschrock "bad alternate root '%s'"), strval);
6132f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
614990b4856Slling goto error;
615990b4856Slling }
6162f8aaab3Seschrock break;
6172f8aaab3Seschrock
6182f8aaab3Seschrock case ZPOOL_PROP_CACHEFILE:
6192f8aaab3Seschrock if (strval[0] == '\0')
6202f8aaab3Seschrock break;
6212f8aaab3Seschrock
6222f8aaab3Seschrock if (strcmp(strval, "none") == 0)
6232f8aaab3Seschrock break;
624990b4856Slling
625990b4856Slling if (strval[0] != '/') {
626990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6272f8aaab3Seschrock "property '%s' must be empty, an "
6282f8aaab3Seschrock "absolute path, or 'none'"), propname);
629990b4856Slling (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
630990b4856Slling goto error;
631990b4856Slling }
632990b4856Slling
6332f8aaab3Seschrock slash = strrchr(strval, '/');
634990b4856Slling
6352f8aaab3Seschrock if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
6362f8aaab3Seschrock strcmp(slash, "/..") == 0) {
6372f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6382f8aaab3Seschrock "'%s' is not a valid file"), strval);
6392f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
6402f8aaab3Seschrock goto error;
6412f8aaab3Seschrock }
642990b4856Slling
6432f8aaab3Seschrock *slash = '\0';
6442f8aaab3Seschrock
6452c32020fSeschrock if (strval[0] != '\0' &&
6462c32020fSeschrock (stat64(strval, &statbuf) != 0 ||
6472c32020fSeschrock !S_ISDIR(statbuf.st_mode))) {
6482f8aaab3Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6492f8aaab3Seschrock "'%s' is not a valid directory"),
6502f8aaab3Seschrock strval);
6512f8aaab3Seschrock (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
6522f8aaab3Seschrock goto error;
6532f8aaab3Seschrock }
6542f8aaab3Seschrock
6552f8aaab3Seschrock *slash = '/';
6562f8aaab3Seschrock break;
657f9af39baSGeorge Wilson
6588704186eSDan McDonald case ZPOOL_PROP_COMMENT:
6598704186eSDan McDonald for (check = strval; *check != '\0'; check++) {
6608704186eSDan McDonald if (!isprint(*check)) {
6618704186eSDan McDonald zfs_error_aux(hdl,
6628704186eSDan McDonald dgettext(TEXT_DOMAIN,
6638704186eSDan McDonald "comment may only have printable "
6648704186eSDan McDonald "characters"));
6658704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP,
6668704186eSDan McDonald errbuf);
6678704186eSDan McDonald goto error;
6688704186eSDan McDonald }
6698704186eSDan McDonald }
6708704186eSDan McDonald if (strlen(strval) > ZPROP_MAX_COMMENT) {
6718704186eSDan McDonald zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6728704186eSDan McDonald "comment must not exceed %d characters"),
6738704186eSDan McDonald ZPROP_MAX_COMMENT);
6748704186eSDan McDonald (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
6758704186eSDan McDonald goto error;
6768704186eSDan McDonald }
6778704186eSDan McDonald break;
67804e56356SAndriy Gapon
679f9af39baSGeorge Wilson case ZPOOL_PROP_READONLY:
680f9af39baSGeorge Wilson if (!flags.import) {
681f9af39baSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
682f9af39baSGeorge Wilson "property '%s' can only be set at "
683f9af39baSGeorge Wilson "import time"), propname);
684f9af39baSGeorge Wilson (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
685f9af39baSGeorge Wilson goto error;
686f9af39baSGeorge Wilson }
687f9af39baSGeorge Wilson break;
68888f61deeSIgor Kozhukhov
68904e56356SAndriy Gapon case ZPOOL_PROP_TNAME:
69004e56356SAndriy Gapon if (!flags.create) {
69104e56356SAndriy Gapon zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
69204e56356SAndriy Gapon "property '%s' can only be set at "
69304e56356SAndriy Gapon "creation time"), propname);
69404e56356SAndriy Gapon (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
69504e56356SAndriy Gapon goto error;
69604e56356SAndriy Gapon }
69704e56356SAndriy Gapon break;
69804e56356SAndriy Gapon
699e0f1c0afSOlaf Faaland case ZPOOL_PROP_MULTIHOST:
700e0f1c0afSOlaf Faaland if (get_system_hostid() == 0) {
701e0f1c0afSOlaf Faaland zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
702e0f1c0afSOlaf Faaland "requires a non-zero system hostid"));
703e0f1c0afSOlaf Faaland (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
704e0f1c0afSOlaf Faaland goto error;
705e0f1c0afSOlaf Faaland }
706e0f1c0afSOlaf Faaland break;
707e0f1c0afSOlaf Faaland
70888f61deeSIgor Kozhukhov default:
70988f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
71088f61deeSIgor Kozhukhov "property '%s'(%d) not defined"), propname, prop);
71188f61deeSIgor Kozhukhov break;
712990b4856Slling }
713990b4856Slling }
714990b4856Slling
715990b4856Slling return (retprops);
716990b4856Slling error:
717990b4856Slling nvlist_free(retprops);
718990b4856Slling return (NULL);
719990b4856Slling }
720990b4856Slling
721990b4856Slling /*
722990b4856Slling * Set zpool property : propname=propval.
723990b4856Slling */
724990b4856Slling int
zpool_set_prop(zpool_handle_t * zhp,const char * propname,const char * propval)725990b4856Slling zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
726990b4856Slling {
727990b4856Slling zfs_cmd_t zc = { 0 };
728990b4856Slling int ret = -1;
729990b4856Slling char errbuf[1024];
730990b4856Slling nvlist_t *nvl = NULL;
731990b4856Slling nvlist_t *realprops;
732990b4856Slling uint64_t version;
733f9af39baSGeorge Wilson prop_flags_t flags = { 0 };
734990b4856Slling
735990b4856Slling (void) snprintf(errbuf, sizeof (errbuf),
736990b4856Slling dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
737990b4856Slling zhp->zpool_name);
738990b4856Slling
739990b4856Slling if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
740990b4856Slling return (no_memory(zhp->zpool_hdl));
741990b4856Slling
742990b4856Slling if (nvlist_add_string(nvl, propname, propval) != 0) {
743990b4856Slling nvlist_free(nvl);
744990b4856Slling return (no_memory(zhp->zpool_hdl));
745990b4856Slling }
746990b4856Slling
747990b4856Slling version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
7480a48a24eStimh if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
749f9af39baSGeorge Wilson zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
750990b4856Slling nvlist_free(nvl);
751990b4856Slling return (-1);
752990b4856Slling }
753990b4856Slling
754990b4856Slling nvlist_free(nvl);
755990b4856Slling nvl = realprops;
756990b4856Slling
757990b4856Slling /*
758990b4856Slling * Execute the corresponding ioctl() to set this property.
759990b4856Slling */
760990b4856Slling (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
761990b4856Slling
762990b4856Slling if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
763990b4856Slling nvlist_free(nvl);
764990b4856Slling return (-1);
765990b4856Slling }
766990b4856Slling
767990b4856Slling ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
768990b4856Slling
769990b4856Slling zcmd_free_nvlists(&zc);
770990b4856Slling nvlist_free(nvl);
771990b4856Slling
772990b4856Slling if (ret)
773990b4856Slling (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
774990b4856Slling else
775990b4856Slling (void) zpool_props_refresh(zhp);
776990b4856Slling
777990b4856Slling return (ret);
778990b4856Slling }
779990b4856Slling
780990b4856Slling int
zpool_expand_proplist(zpool_handle_t * zhp,zprop_list_t ** plp)781990b4856Slling zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
782990b4856Slling {
783990b4856Slling libzfs_handle_t *hdl = zhp->zpool_hdl;
784990b4856Slling zprop_list_t *entry;
785990b4856Slling char buf[ZFS_MAXPROPLEN];
786ad135b5dSChristopher Siden nvlist_t *features = NULL;
787ad135b5dSChristopher Siden zprop_list_t **last;
788ad135b5dSChristopher Siden boolean_t firstexpand = (NULL == *plp);
789990b4856Slling
790990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
791990b4856Slling return (-1);
792990b4856Slling
793ad135b5dSChristopher Siden last = plp;
794ad135b5dSChristopher Siden while (*last != NULL)
795ad135b5dSChristopher Siden last = &(*last)->pl_next;
796ad135b5dSChristopher Siden
797ad135b5dSChristopher Siden if ((*plp)->pl_all)
798ad135b5dSChristopher Siden features = zpool_get_features(zhp);
799ad135b5dSChristopher Siden
800ad135b5dSChristopher Siden if ((*plp)->pl_all && firstexpand) {
801ad135b5dSChristopher Siden for (int i = 0; i < SPA_FEATURES; i++) {
802ad135b5dSChristopher Siden zprop_list_t *entry = zfs_alloc(hdl,
803ad135b5dSChristopher Siden sizeof (zprop_list_t));
804ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL;
805ad135b5dSChristopher Siden entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
806ad135b5dSChristopher Siden spa_feature_table[i].fi_uname);
807ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop);
808ad135b5dSChristopher Siden entry->pl_all = B_TRUE;
809ad135b5dSChristopher Siden
810ad135b5dSChristopher Siden *last = entry;
811ad135b5dSChristopher Siden last = &entry->pl_next;
812ad135b5dSChristopher Siden }
813ad135b5dSChristopher Siden }
814ad135b5dSChristopher Siden
815ad135b5dSChristopher Siden /* add any unsupported features */
816ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
817ad135b5dSChristopher Siden nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
818ad135b5dSChristopher Siden char *propname;
819ad135b5dSChristopher Siden boolean_t found;
820ad135b5dSChristopher Siden zprop_list_t *entry;
821ad135b5dSChristopher Siden
822ad135b5dSChristopher Siden if (zfeature_is_supported(nvpair_name(nvp)))
823ad135b5dSChristopher Siden continue;
824ad135b5dSChristopher Siden
825ad135b5dSChristopher Siden propname = zfs_asprintf(hdl, "unsupported@%s",
826ad135b5dSChristopher Siden nvpair_name(nvp));
827ad135b5dSChristopher Siden
828ad135b5dSChristopher Siden /*
829ad135b5dSChristopher Siden * Before adding the property to the list make sure that no
830ad135b5dSChristopher Siden * other pool already added the same property.
831ad135b5dSChristopher Siden */
832ad135b5dSChristopher Siden found = B_FALSE;
833ad135b5dSChristopher Siden entry = *plp;
834ad135b5dSChristopher Siden while (entry != NULL) {
835ad135b5dSChristopher Siden if (entry->pl_user_prop != NULL &&
836ad135b5dSChristopher Siden strcmp(propname, entry->pl_user_prop) == 0) {
837ad135b5dSChristopher Siden found = B_TRUE;
838ad135b5dSChristopher Siden break;
839ad135b5dSChristopher Siden }
840ad135b5dSChristopher Siden entry = entry->pl_next;
841ad135b5dSChristopher Siden }
842ad135b5dSChristopher Siden if (found) {
843ad135b5dSChristopher Siden free(propname);
844ad135b5dSChristopher Siden continue;
845ad135b5dSChristopher Siden }
846ad135b5dSChristopher Siden
847ad135b5dSChristopher Siden entry = zfs_alloc(hdl, sizeof (zprop_list_t));
848ad135b5dSChristopher Siden entry->pl_prop = ZPROP_INVAL;
849ad135b5dSChristopher Siden entry->pl_user_prop = propname;
850ad135b5dSChristopher Siden entry->pl_width = strlen(entry->pl_user_prop);
851ad135b5dSChristopher Siden entry->pl_all = B_TRUE;
852ad135b5dSChristopher Siden
853ad135b5dSChristopher Siden *last = entry;
854ad135b5dSChristopher Siden last = &entry->pl_next;
855ad135b5dSChristopher Siden }
856ad135b5dSChristopher Siden
857990b4856Slling for (entry = *plp; entry != NULL; entry = entry->pl_next) {
858990b4856Slling
859990b4856Slling if (entry->pl_fixed)
860990b4856Slling continue;
861990b4856Slling
862990b4856Slling if (entry->pl_prop != ZPROP_INVAL &&
863990b4856Slling zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
864c58b3526SAdam Stevko NULL, B_FALSE) == 0) {
865990b4856Slling if (strlen(buf) > entry->pl_width)
866990b4856Slling entry->pl_width = strlen(buf);
867990b4856Slling }
868990b4856Slling }
869990b4856Slling
870990b4856Slling return (0);
871990b4856Slling }
872990b4856Slling
873ad135b5dSChristopher Siden /*
874ad135b5dSChristopher Siden * Get the state for the given feature on the given ZFS pool.
875ad135b5dSChristopher Siden */
876ad135b5dSChristopher Siden int
zpool_prop_get_feature(zpool_handle_t * zhp,const char * propname,char * buf,size_t len)877ad135b5dSChristopher Siden zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
878ad135b5dSChristopher Siden size_t len)
879ad135b5dSChristopher Siden {
880ad135b5dSChristopher Siden uint64_t refcount;
881ad135b5dSChristopher Siden boolean_t found = B_FALSE;
882ad135b5dSChristopher Siden nvlist_t *features = zpool_get_features(zhp);
883ad135b5dSChristopher Siden boolean_t supported;
884ad135b5dSChristopher Siden const char *feature = strchr(propname, '@') + 1;
885ad135b5dSChristopher Siden
886ad135b5dSChristopher Siden supported = zpool_prop_feature(propname);
8877c13517fSSerapheim Dimitropoulos ASSERT(supported || zpool_prop_unsupported(propname));
888ad135b5dSChristopher Siden
889ad135b5dSChristopher Siden /*
890ad135b5dSChristopher Siden * Convert from feature name to feature guid. This conversion is
891ad135b5dSChristopher Siden * unecessary for unsupported@... properties because they already
892ad135b5dSChristopher Siden * use guids.
893ad135b5dSChristopher Siden */
894ad135b5dSChristopher Siden if (supported) {
895ad135b5dSChristopher Siden int ret;
8962acef22dSMatthew Ahrens spa_feature_t fid;
897ad135b5dSChristopher Siden
8982acef22dSMatthew Ahrens ret = zfeature_lookup_name(feature, &fid);
899ad135b5dSChristopher Siden if (ret != 0) {
900ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len);
901ad135b5dSChristopher Siden return (ENOTSUP);
902ad135b5dSChristopher Siden }
9032acef22dSMatthew Ahrens feature = spa_feature_table[fid].fi_guid;
904ad135b5dSChristopher Siden }
905ad135b5dSChristopher Siden
906ad135b5dSChristopher Siden if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
907ad135b5dSChristopher Siden found = B_TRUE;
908ad135b5dSChristopher Siden
909ad135b5dSChristopher Siden if (supported) {
910ad135b5dSChristopher Siden if (!found) {
911ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
912ad135b5dSChristopher Siden } else {
913ad135b5dSChristopher Siden if (refcount == 0)
914ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
915ad135b5dSChristopher Siden else
916ad135b5dSChristopher Siden (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
917ad135b5dSChristopher Siden }
918ad135b5dSChristopher Siden } else {
919ad135b5dSChristopher Siden if (found) {
920ad135b5dSChristopher Siden if (refcount == 0) {
921ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
922ad135b5dSChristopher Siden } else {
923ad135b5dSChristopher Siden (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
924ad135b5dSChristopher Siden }
925ad135b5dSChristopher Siden } else {
926ad135b5dSChristopher Siden (void) strlcpy(buf, "-", len);
927ad135b5dSChristopher Siden return (ENOTSUP);
928ad135b5dSChristopher Siden }
929ad135b5dSChristopher Siden }
930ad135b5dSChristopher Siden
931ad135b5dSChristopher Siden return (0);
932ad135b5dSChristopher Siden }
933990b4856Slling
934573ca77eSGeorge Wilson /*
935573ca77eSGeorge Wilson * Don't start the slice at the default block of 34; many storage
936573ca77eSGeorge Wilson * devices will use a stripe width of 128k, so start there instead.
937573ca77eSGeorge Wilson */
938573ca77eSGeorge Wilson #define NEW_START_BLOCK 256
939573ca77eSGeorge Wilson
940fa9e4066Sahrens /*
941fa9e4066Sahrens * Validate the given pool name, optionally putting an extended error message in
942fa9e4066Sahrens * 'buf'.
943fa9e4066Sahrens */
944e7cbe64fSgw boolean_t
zpool_name_valid(libzfs_handle_t * hdl,boolean_t isopen,const char * pool)94599653d4eSeschrock zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
946fa9e4066Sahrens {
947fa9e4066Sahrens namecheck_err_t why;
948fa9e4066Sahrens char what;
949b468a217Seschrock int ret;
950b468a217Seschrock
951b468a217Seschrock ret = pool_namecheck(pool, &why, &what);
952b468a217Seschrock
953b468a217Seschrock /*
954b468a217Seschrock * The rules for reserved pool names were extended at a later point.
955b468a217Seschrock * But we need to support users with existing pools that may now be
956b468a217Seschrock * invalid. So we only check for this expanded set of names during a
957b468a217Seschrock * create (or import), and only in userland.
958b468a217Seschrock */
959b468a217Seschrock if (ret == 0 && !isopen &&
960b468a217Seschrock (strncmp(pool, "mirror", 6) == 0 ||
961b468a217Seschrock strncmp(pool, "raidz", 5) == 0 ||
9628654d025Sperrin strncmp(pool, "spare", 5) == 0 ||
9638654d025Sperrin strcmp(pool, "log") == 0)) {
964e7cbe64fSgw if (hdl != NULL)
965e7cbe64fSgw zfs_error_aux(hdl,
966e7cbe64fSgw dgettext(TEXT_DOMAIN, "name is reserved"));
96799653d4eSeschrock return (B_FALSE);
968b468a217Seschrock }
969b468a217Seschrock
970fa9e4066Sahrens
971b468a217Seschrock if (ret != 0) {
97299653d4eSeschrock if (hdl != NULL) {
973fa9e4066Sahrens switch (why) {
974b81d61a6Slling case NAME_ERR_TOOLONG:
97599653d4eSeschrock zfs_error_aux(hdl,
976b81d61a6Slling dgettext(TEXT_DOMAIN, "name is too long"));
977b81d61a6Slling break;
978b81d61a6Slling
979fa9e4066Sahrens case NAME_ERR_INVALCHAR:
98099653d4eSeschrock zfs_error_aux(hdl,
981fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character "
982fa9e4066Sahrens "'%c' in pool name"), what);
983fa9e4066Sahrens break;
984fa9e4066Sahrens
985fa9e4066Sahrens case NAME_ERR_NOLETTER:
98699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
98799653d4eSeschrock "name must begin with a letter"));
988fa9e4066Sahrens break;
989fa9e4066Sahrens
990fa9e4066Sahrens case NAME_ERR_RESERVED:
99199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99299653d4eSeschrock "name is reserved"));
993fa9e4066Sahrens break;
994fa9e4066Sahrens
995fa9e4066Sahrens case NAME_ERR_DISKLIKE:
99699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
99799653d4eSeschrock "pool name is reserved"));
998fa9e4066Sahrens break;
9995ad82045Snd
10005ad82045Snd case NAME_ERR_LEADING_SLASH:
10015ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10025ad82045Snd "leading slash in name"));
10035ad82045Snd break;
10045ad82045Snd
10055ad82045Snd case NAME_ERR_EMPTY_COMPONENT:
10065ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10075ad82045Snd "empty component in name"));
10085ad82045Snd break;
10095ad82045Snd
10105ad82045Snd case NAME_ERR_TRAILING_SLASH:
10115ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
10125ad82045Snd "trailing slash in name"));
10135ad82045Snd break;
10145ad82045Snd
1015edb901aaSMarcel Telka case NAME_ERR_MULTIPLE_DELIMITERS:
10165ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1017edb901aaSMarcel Telka "multiple '@' and/or '#' delimiters in "
1018edb901aaSMarcel Telka "name"));
10195ad82045Snd break;
10205ad82045Snd
102188f61deeSIgor Kozhukhov default:
102288f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
102388f61deeSIgor Kozhukhov "(%d) not defined"), why);
102488f61deeSIgor Kozhukhov break;
1025fa9e4066Sahrens }
1026fa9e4066Sahrens }
102799653d4eSeschrock return (B_FALSE);
1028fa9e4066Sahrens }
1029fa9e4066Sahrens
103099653d4eSeschrock return (B_TRUE);
1031fa9e4066Sahrens }
1032fa9e4066Sahrens
1033fa9e4066Sahrens /*
1034fa9e4066Sahrens * Open a handle to the given pool, even if the pool is currently in the FAULTED
1035fa9e4066Sahrens * state.
1036fa9e4066Sahrens */
1037fa9e4066Sahrens zpool_handle_t *
zpool_open_canfail(libzfs_handle_t * hdl,const char * pool)103899653d4eSeschrock zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1039fa9e4066Sahrens {
1040fa9e4066Sahrens zpool_handle_t *zhp;
104194de1d4cSeschrock boolean_t missing;
1042fa9e4066Sahrens
1043fa9e4066Sahrens /*
1044fa9e4066Sahrens * Make sure the pool name is valid.
1045fa9e4066Sahrens */
104699653d4eSeschrock if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1047ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
104899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"),
104999653d4eSeschrock pool);
1050fa9e4066Sahrens return (NULL);
1051fa9e4066Sahrens }
1052fa9e4066Sahrens
105399653d4eSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
105499653d4eSeschrock return (NULL);
1055fa9e4066Sahrens
105699653d4eSeschrock zhp->zpool_hdl = hdl;
1057fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1058fa9e4066Sahrens
105994de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) {
106094de1d4cSeschrock zpool_close(zhp);
106194de1d4cSeschrock return (NULL);
106294de1d4cSeschrock }
106394de1d4cSeschrock
106494de1d4cSeschrock if (missing) {
1065990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1066ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_NOENT,
1067990b4856Slling dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
106894de1d4cSeschrock zpool_close(zhp);
106994de1d4cSeschrock return (NULL);
1070fa9e4066Sahrens }
1071fa9e4066Sahrens
1072fa9e4066Sahrens return (zhp);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens
1075fa9e4066Sahrens /*
1076fa9e4066Sahrens * Like the above, but silent on error. Used when iterating over pools (because
1077fa9e4066Sahrens * the configuration cache may be out of date).
1078fa9e4066Sahrens */
107994de1d4cSeschrock int
zpool_open_silent(libzfs_handle_t * hdl,const char * pool,zpool_handle_t ** ret)108094de1d4cSeschrock zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1081fa9e4066Sahrens {
1082fa9e4066Sahrens zpool_handle_t *zhp;
108394de1d4cSeschrock boolean_t missing;
1084fa9e4066Sahrens
108594de1d4cSeschrock if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
108694de1d4cSeschrock return (-1);
1087fa9e4066Sahrens
108899653d4eSeschrock zhp->zpool_hdl = hdl;
1089fa9e4066Sahrens (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1090fa9e4066Sahrens
109194de1d4cSeschrock if (zpool_refresh_stats(zhp, &missing) != 0) {
109294de1d4cSeschrock zpool_close(zhp);
109394de1d4cSeschrock return (-1);
1094fa9e4066Sahrens }
1095fa9e4066Sahrens
109694de1d4cSeschrock if (missing) {
109794de1d4cSeschrock zpool_close(zhp);
109894de1d4cSeschrock *ret = NULL;
109994de1d4cSeschrock return (0);
110094de1d4cSeschrock }
110194de1d4cSeschrock
110294de1d4cSeschrock *ret = zhp;
110394de1d4cSeschrock return (0);
1104fa9e4066Sahrens }
1105fa9e4066Sahrens
1106fa9e4066Sahrens /*
1107fa9e4066Sahrens * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1108fa9e4066Sahrens * state.
1109fa9e4066Sahrens */
1110fa9e4066Sahrens zpool_handle_t *
zpool_open(libzfs_handle_t * hdl,const char * pool)111199653d4eSeschrock zpool_open(libzfs_handle_t *hdl, const char *pool)
1112fa9e4066Sahrens {
1113fa9e4066Sahrens zpool_handle_t *zhp;
1114fa9e4066Sahrens
111599653d4eSeschrock if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1116fa9e4066Sahrens return (NULL);
1117fa9e4066Sahrens
1118fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1119ece3d9b3Slling (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
112099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1121fa9e4066Sahrens zpool_close(zhp);
1122fa9e4066Sahrens return (NULL);
1123fa9e4066Sahrens }
1124fa9e4066Sahrens
1125fa9e4066Sahrens return (zhp);
1126fa9e4066Sahrens }
1127fa9e4066Sahrens
1128fa9e4066Sahrens /*
1129fa9e4066Sahrens * Close the handle. Simply frees the memory associated with the handle.
1130fa9e4066Sahrens */
1131fa9e4066Sahrens void
zpool_close(zpool_handle_t * zhp)1132fa9e4066Sahrens zpool_close(zpool_handle_t *zhp)
1133fa9e4066Sahrens {
1134aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_config);
1135aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_old_config);
1136aab83bb8SJosef 'Jeff' Sipek nvlist_free(zhp->zpool_props);
1137fa9e4066Sahrens free(zhp);
1138fa9e4066Sahrens }
1139fa9e4066Sahrens
1140fa9e4066Sahrens /*
1141fa9e4066Sahrens * Return the name of the pool.
1142fa9e4066Sahrens */
1143fa9e4066Sahrens const char *
zpool_get_name(zpool_handle_t * zhp)1144fa9e4066Sahrens zpool_get_name(zpool_handle_t *zhp)
1145fa9e4066Sahrens {
1146fa9e4066Sahrens return (zhp->zpool_name);
1147fa9e4066Sahrens }
1148fa9e4066Sahrens
1149fa9e4066Sahrens
1150fa9e4066Sahrens /*
1151fa9e4066Sahrens * Return the state of the pool (ACTIVE or UNAVAILABLE)
1152fa9e4066Sahrens */
1153fa9e4066Sahrens int
zpool_get_state(zpool_handle_t * zhp)1154fa9e4066Sahrens zpool_get_state(zpool_handle_t *zhp)
1155fa9e4066Sahrens {
1156fa9e4066Sahrens return (zhp->zpool_state);
1157fa9e4066Sahrens }
1158fa9e4066Sahrens
1159663207adSDon Brady /*
1160663207adSDon Brady * Check if vdev list contains a special vdev
1161663207adSDon Brady */
1162663207adSDon Brady static boolean_t
zpool_has_special_vdev(nvlist_t * nvroot)1163663207adSDon Brady zpool_has_special_vdev(nvlist_t *nvroot)
1164663207adSDon Brady {
1165663207adSDon Brady nvlist_t **child;
1166663207adSDon Brady uint_t children;
1167663207adSDon Brady
1168663207adSDon Brady if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1169663207adSDon Brady &children) == 0) {
1170663207adSDon Brady for (uint_t c = 0; c < children; c++) {
1171663207adSDon Brady char *bias;
1172663207adSDon Brady
1173663207adSDon Brady if (nvlist_lookup_string(child[c],
1174663207adSDon Brady ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1175663207adSDon Brady strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1176663207adSDon Brady return (B_TRUE);
1177663207adSDon Brady }
1178663207adSDon Brady }
1179663207adSDon Brady }
1180663207adSDon Brady return (B_FALSE);
1181663207adSDon Brady }
1182663207adSDon Brady
1183fa9e4066Sahrens /*
1184fa9e4066Sahrens * Create the named pool, using the provided vdev list. It is assumed
1185fa9e4066Sahrens * that the consumer has already validated the contents of the nvlist, so we
1186fa9e4066Sahrens * don't have to worry about error semantics.
1187fa9e4066Sahrens */
1188fa9e4066Sahrens int
zpool_create(libzfs_handle_t * hdl,const char * pool,nvlist_t * nvroot,nvlist_t * props,nvlist_t * fsprops)118999653d4eSeschrock zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
11900a48a24eStimh nvlist_t *props, nvlist_t *fsprops)
1191fa9e4066Sahrens {
1192fa9e4066Sahrens zfs_cmd_t zc = { 0 };
11930a48a24eStimh nvlist_t *zc_fsprops = NULL;
11940a48a24eStimh nvlist_t *zc_props = NULL;
1195eb633035STom Caputi nvlist_t *hidden_args = NULL;
1196eb633035STom Caputi uint8_t *wkeydata = NULL;
1197eb633035STom Caputi uint_t wkeylen = 0;
119899653d4eSeschrock char msg[1024];
11990a48a24eStimh int ret = -1;
1200fa9e4066Sahrens
120199653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
120299653d4eSeschrock "cannot create '%s'"), pool);
1203fa9e4066Sahrens
120499653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, pool))
120599653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1206fa9e4066Sahrens
1207351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1208990b4856Slling return (-1);
1209fa9e4066Sahrens
12100a48a24eStimh if (props) {
1211f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1212f9af39baSGeorge Wilson
12130a48a24eStimh if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1214f9af39baSGeorge Wilson SPA_VERSION_1, flags, msg)) == NULL) {
12150a48a24eStimh goto create_failed;
12160a48a24eStimh }
12170a48a24eStimh }
121899653d4eSeschrock
12190a48a24eStimh if (fsprops) {
12200a48a24eStimh uint64_t zoned;
12210a48a24eStimh char *zonestr;
12220a48a24eStimh
12230a48a24eStimh zoned = ((nvlist_lookup_string(fsprops,
12240a48a24eStimh zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
12250a48a24eStimh strcmp(zonestr, "on") == 0);
12260a48a24eStimh
1227e9316f76SJoe Stein if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1228eb633035STom Caputi fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
12290a48a24eStimh goto create_failed;
12300a48a24eStimh }
1231663207adSDon Brady
1232663207adSDon Brady if (nvlist_exists(zc_fsprops,
1233663207adSDon Brady zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1234663207adSDon Brady !zpool_has_special_vdev(nvroot)) {
1235663207adSDon Brady zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1236663207adSDon Brady "%s property requires a special vdev"),
1237663207adSDon Brady zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1238663207adSDon Brady (void) zfs_error(hdl, EZFS_BADPROP, msg);
1239663207adSDon Brady goto create_failed;
1240663207adSDon Brady }
1241663207adSDon Brady
12420a48a24eStimh if (!zc_props &&
12430a48a24eStimh (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
12440a48a24eStimh goto create_failed;
12450a48a24eStimh }
12466ccda740Sloli if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1247eb633035STom Caputi &wkeydata, &wkeylen) != 0) {
1248eb633035STom Caputi (void) zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1249eb633035STom Caputi goto create_failed;
1250eb633035STom Caputi }
12510a48a24eStimh if (nvlist_add_nvlist(zc_props,
12520a48a24eStimh ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
12530a48a24eStimh goto create_failed;
12540a48a24eStimh }
1255eb633035STom Caputi if (wkeydata != NULL) {
1256eb633035STom Caputi if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1257eb633035STom Caputi goto create_failed;
1258eb633035STom Caputi
1259eb633035STom Caputi if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1260eb633035STom Caputi wkeydata, wkeylen) != 0)
1261eb633035STom Caputi goto create_failed;
1262eb633035STom Caputi
1263eb633035STom Caputi if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1264eb633035STom Caputi hidden_args) != 0)
1265eb633035STom Caputi goto create_failed;
1266eb633035STom Caputi }
1267351420b3Slling }
1268fa9e4066Sahrens
12690a48a24eStimh if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
12700a48a24eStimh goto create_failed;
12710a48a24eStimh
1272990b4856Slling (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1273fa9e4066Sahrens
12740a48a24eStimh if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1275351420b3Slling
1276e9dbad6fSeschrock zcmd_free_nvlists(&zc);
12770a48a24eStimh nvlist_free(zc_props);
12780a48a24eStimh nvlist_free(zc_fsprops);
1279eb633035STom Caputi nvlist_free(hidden_args);
1280eb633035STom Caputi if (wkeydata != NULL)
1281eb633035STom Caputi free(wkeydata);
1282fa9e4066Sahrens
128399653d4eSeschrock switch (errno) {
1284fa9e4066Sahrens case EBUSY:
1285fa9e4066Sahrens /*
1286fa9e4066Sahrens * This can happen if the user has specified the same
1287fa9e4066Sahrens * device multiple times. We can't reliably detect this
1288fa9e4066Sahrens * until we try to add it and see we already have a
1289fa9e4066Sahrens * label.
1290fa9e4066Sahrens */
129199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
129299653d4eSeschrock "one or more vdevs refer to the same device"));
129399653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg));
1294fa9e4066Sahrens
1295e9316f76SJoe Stein case ERANGE:
1296e9316f76SJoe Stein /*
1297e9316f76SJoe Stein * This happens if the record size is smaller or larger
1298e9316f76SJoe Stein * than the allowed size range, or not a power of 2.
1299e9316f76SJoe Stein *
1300e9316f76SJoe Stein * NOTE: although zfs_valid_proplist is called earlier,
1301e9316f76SJoe Stein * this case may have slipped through since the
1302e9316f76SJoe Stein * pool does not exist yet and it is therefore
1303e9316f76SJoe Stein * impossible to read properties e.g. max blocksize
1304e9316f76SJoe Stein * from the pool.
1305e9316f76SJoe Stein */
1306e9316f76SJoe Stein zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1307e9316f76SJoe Stein "record size invalid"));
1308e9316f76SJoe Stein return (zfs_error(hdl, EZFS_BADPROP, msg));
1309e9316f76SJoe Stein
1310fa9e4066Sahrens case EOVERFLOW:
1311fa9e4066Sahrens /*
131299653d4eSeschrock * This occurs when one of the devices is below
1313fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1314fa9e4066Sahrens * device was the problem device since there's no
1315fa9e4066Sahrens * reliable way to determine device size from userland.
1316fa9e4066Sahrens */
1317fa9e4066Sahrens {
1318fa9e4066Sahrens char buf[64];
1319fa9e4066Sahrens
1320*6520eed5SToomas Soome zfs_nicebytes(SPA_MINDEVSIZE, buf,
1321*6520eed5SToomas Soome sizeof (buf));
1322fa9e4066Sahrens
132399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132499653d4eSeschrock "one or more devices is less than the "
132599653d4eSeschrock "minimum size (%s)"), buf);
1326fa9e4066Sahrens }
132799653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg));
1328fa9e4066Sahrens
1329fa9e4066Sahrens case ENOSPC:
133099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133199653d4eSeschrock "one or more devices is out of space"));
133299653d4eSeschrock return (zfs_error(hdl, EZFS_BADDEV, msg));
1333fa9e4066Sahrens
1334fa9e4066Sahrens default:
133599653d4eSeschrock return (zpool_standard_error(hdl, errno, msg));
1336fa9e4066Sahrens }
1337fa9e4066Sahrens }
1338fa9e4066Sahrens
13390a48a24eStimh create_failed:
1340351420b3Slling zcmd_free_nvlists(&zc);
13410a48a24eStimh nvlist_free(zc_props);
13420a48a24eStimh nvlist_free(zc_fsprops);
1343eb633035STom Caputi nvlist_free(hidden_args);
1344eb633035STom Caputi if (wkeydata != NULL)
1345eb633035STom Caputi free(wkeydata);
13460a48a24eStimh return (ret);
1347fa9e4066Sahrens }
1348fa9e4066Sahrens
1349fa9e4066Sahrens /*
1350fa9e4066Sahrens * Destroy the given pool. It is up to the caller to ensure that there are no
1351fa9e4066Sahrens * datasets left in the pool.
1352fa9e4066Sahrens */
1353fa9e4066Sahrens int
zpool_destroy(zpool_handle_t * zhp,const char * log_str)13544445fffbSMatthew Ahrens zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1355fa9e4066Sahrens {
1356fa9e4066Sahrens zfs_cmd_t zc = { 0 };
1357fa9e4066Sahrens zfs_handle_t *zfp = NULL;
135899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
135999653d4eSeschrock char msg[1024];
1360fa9e4066Sahrens
1361fa9e4066Sahrens if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1362cb04b873SMark J Musante (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1363fa9e4066Sahrens return (-1);
1364fa9e4066Sahrens
1365fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
13664445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str;
1367fa9e4066Sahrens
1368cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
136999653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
137099653d4eSeschrock "cannot destroy '%s'"), zhp->zpool_name);
1371fa9e4066Sahrens
137299653d4eSeschrock if (errno == EROFS) {
137399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137499653d4eSeschrock "one or more devices is read only"));
137599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
137699653d4eSeschrock } else {
137799653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg);
1378fa9e4066Sahrens }
1379fa9e4066Sahrens
1380fa9e4066Sahrens if (zfp)
1381fa9e4066Sahrens zfs_close(zfp);
1382fa9e4066Sahrens return (-1);
1383fa9e4066Sahrens }
1384fa9e4066Sahrens
1385fa9e4066Sahrens if (zfp) {
1386fa9e4066Sahrens remove_mountpoint(zfp);
1387fa9e4066Sahrens zfs_close(zfp);
1388fa9e4066Sahrens }
1389fa9e4066Sahrens
1390fa9e4066Sahrens return (0);
1391fa9e4066Sahrens }
1392fa9e4066Sahrens
139386714001SSerapheim Dimitropoulos /*
139486714001SSerapheim Dimitropoulos * Create a checkpoint in the given pool.
139586714001SSerapheim Dimitropoulos */
139686714001SSerapheim Dimitropoulos int
zpool_checkpoint(zpool_handle_t * zhp)139786714001SSerapheim Dimitropoulos zpool_checkpoint(zpool_handle_t *zhp)
139886714001SSerapheim Dimitropoulos {
139986714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl;
140086714001SSerapheim Dimitropoulos char msg[1024];
140186714001SSerapheim Dimitropoulos int error;
140286714001SSerapheim Dimitropoulos
140386714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint(zhp->zpool_name);
140486714001SSerapheim Dimitropoulos if (error != 0) {
140586714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
140686714001SSerapheim Dimitropoulos "cannot checkpoint '%s'"), zhp->zpool_name);
140786714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg);
140886714001SSerapheim Dimitropoulos return (-1);
140986714001SSerapheim Dimitropoulos }
141086714001SSerapheim Dimitropoulos
141186714001SSerapheim Dimitropoulos return (0);
141286714001SSerapheim Dimitropoulos }
141386714001SSerapheim Dimitropoulos
141486714001SSerapheim Dimitropoulos /*
141586714001SSerapheim Dimitropoulos * Discard the checkpoint from the given pool.
141686714001SSerapheim Dimitropoulos */
141786714001SSerapheim Dimitropoulos int
zpool_discard_checkpoint(zpool_handle_t * zhp)141886714001SSerapheim Dimitropoulos zpool_discard_checkpoint(zpool_handle_t *zhp)
141986714001SSerapheim Dimitropoulos {
142086714001SSerapheim Dimitropoulos libzfs_handle_t *hdl = zhp->zpool_hdl;
142186714001SSerapheim Dimitropoulos char msg[1024];
142286714001SSerapheim Dimitropoulos int error;
142386714001SSerapheim Dimitropoulos
142486714001SSerapheim Dimitropoulos error = lzc_pool_checkpoint_discard(zhp->zpool_name);
142586714001SSerapheim Dimitropoulos if (error != 0) {
142686714001SSerapheim Dimitropoulos (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
142786714001SSerapheim Dimitropoulos "cannot discard checkpoint in '%s'"), zhp->zpool_name);
142886714001SSerapheim Dimitropoulos (void) zpool_standard_error(hdl, error, msg);
142986714001SSerapheim Dimitropoulos return (-1);
143086714001SSerapheim Dimitropoulos }
143186714001SSerapheim Dimitropoulos
143286714001SSerapheim Dimitropoulos return (0);
143386714001SSerapheim Dimitropoulos }
143486714001SSerapheim Dimitropoulos
1435fa9e4066Sahrens /*
1436fa9e4066Sahrens * Add the given vdevs to the pool. The caller must have already performed the
1437fa9e4066Sahrens * necessary verification to ensure that the vdev specification is well-formed.
1438fa9e4066Sahrens */
1439fa9e4066Sahrens int
zpool_add(zpool_handle_t * zhp,nvlist_t * nvroot)1440fa9e4066Sahrens zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1441fa9e4066Sahrens {
1442e9dbad6fSeschrock zfs_cmd_t zc = { 0 };
144399653d4eSeschrock int ret;
144499653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
144599653d4eSeschrock char msg[1024];
1446fa94a07fSbrendan nvlist_t **spares, **l2cache;
1447fa94a07fSbrendan uint_t nspares, nl2cache;
144899653d4eSeschrock
144999653d4eSeschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
145099653d4eSeschrock "cannot add to '%s'"), zhp->zpool_name);
145199653d4eSeschrock
1452fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1453fa94a07fSbrendan SPA_VERSION_SPARES &&
145499653d4eSeschrock nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
145599653d4eSeschrock &spares, &nspares) == 0) {
145699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
145799653d4eSeschrock "upgraded to add hot spares"));
145899653d4eSeschrock return (zfs_error(hdl, EZFS_BADVERSION, msg));
145999653d4eSeschrock }
1460fa9e4066Sahrens
1461fa94a07fSbrendan if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1462fa94a07fSbrendan SPA_VERSION_L2CACHE &&
1463fa94a07fSbrendan nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1464fa94a07fSbrendan &l2cache, &nl2cache) == 0) {
1465fa94a07fSbrendan zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1466fa94a07fSbrendan "upgraded to add cache devices"));
1467fa94a07fSbrendan return (zfs_error(hdl, EZFS_BADVERSION, msg));
1468fa94a07fSbrendan }
1469fa94a07fSbrendan
1470990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
147199653d4eSeschrock return (-1);
1472fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1473fa9e4066Sahrens
1474cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1475fa9e4066Sahrens switch (errno) {
1476fa9e4066Sahrens case EBUSY:
1477fa9e4066Sahrens /*
1478fa9e4066Sahrens * This can happen if the user has specified the same
1479fa9e4066Sahrens * device multiple times. We can't reliably detect this
1480fa9e4066Sahrens * until we try to add it and see we already have a
1481fa9e4066Sahrens * label.
1482fa9e4066Sahrens */
148399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148499653d4eSeschrock "one or more vdevs refer to the same device"));
148599653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
1486fa9e4066Sahrens break;
1487fa9e4066Sahrens
14885cabbc6bSPrashanth Sreenivasa case EINVAL:
14895cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
14905cabbc6bSPrashanth Sreenivasa "invalid config; a pool with removing/removed "
14915cabbc6bSPrashanth Sreenivasa "vdevs does not support adding raidz vdevs"));
14925cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BADDEV, msg);
14935cabbc6bSPrashanth Sreenivasa break;
14945cabbc6bSPrashanth Sreenivasa
1495fa9e4066Sahrens case EOVERFLOW:
1496fa9e4066Sahrens /*
1497fa9e4066Sahrens * This occurrs when one of the devices is below
1498fa9e4066Sahrens * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1499fa9e4066Sahrens * device was the problem device since there's no
1500fa9e4066Sahrens * reliable way to determine device size from userland.
1501fa9e4066Sahrens */
1502fa9e4066Sahrens {
1503fa9e4066Sahrens char buf[64];
1504fa9e4066Sahrens
1505*6520eed5SToomas Soome zfs_nicebytes(SPA_MINDEVSIZE, buf,
1506*6520eed5SToomas Soome sizeof (buf));
1507fa9e4066Sahrens
150899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150999653d4eSeschrock "device is less than the minimum "
151099653d4eSeschrock "size (%s)"), buf);
1511fa9e4066Sahrens }
151299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
151399653d4eSeschrock break;
151499653d4eSeschrock
151599653d4eSeschrock case ENOTSUP:
151699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
15178654d025Sperrin "pool must be upgraded to add these vdevs"));
151899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1519fa9e4066Sahrens break;
1520fa9e4066Sahrens
1521fa9e4066Sahrens default:
152299653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg);
1523fa9e4066Sahrens }
1524fa9e4066Sahrens
152599653d4eSeschrock ret = -1;
152699653d4eSeschrock } else {
152799653d4eSeschrock ret = 0;
1528fa9e4066Sahrens }
1529fa9e4066Sahrens
1530e9dbad6fSeschrock zcmd_free_nvlists(&zc);
1531fa9e4066Sahrens
153299653d4eSeschrock return (ret);
1533fa9e4066Sahrens }
1534fa9e4066Sahrens
1535fa9e4066Sahrens /*
1536fa9e4066Sahrens * Exports the pool from the system. The caller must ensure that there are no
1537fa9e4066Sahrens * mounted datasets in the pool.
1538fa9e4066Sahrens */
15394445fffbSMatthew Ahrens static int
zpool_export_common(zpool_handle_t * zhp,boolean_t force,boolean_t hardforce,const char * log_str)15404445fffbSMatthew Ahrens zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
15414445fffbSMatthew Ahrens const char *log_str)
1542fa9e4066Sahrens {
1543fa9e4066Sahrens zfs_cmd_t zc = { 0 };
154489a89ebfSlling char msg[1024];
1545fa9e4066Sahrens
154689a89ebfSlling (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
154789a89ebfSlling "cannot export '%s'"), zhp->zpool_name);
154889a89ebfSlling
1549fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
155089a89ebfSlling zc.zc_cookie = force;
1551394ab0cbSGeorge Wilson zc.zc_guid = hardforce;
15524445fffbSMatthew Ahrens zc.zc_history = (uint64_t)(uintptr_t)log_str;
155389a89ebfSlling
155489a89ebfSlling if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
155589a89ebfSlling switch (errno) {
155689a89ebfSlling case EXDEV:
155789a89ebfSlling zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
155889a89ebfSlling "use '-f' to override the following errors:\n"
155989a89ebfSlling "'%s' has an active shared spare which could be"
156089a89ebfSlling " used by other pools once '%s' is exported."),
156189a89ebfSlling zhp->zpool_name, zhp->zpool_name);
156289a89ebfSlling return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
156389a89ebfSlling msg));
156489a89ebfSlling default:
156589a89ebfSlling return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
156689a89ebfSlling msg));
156789a89ebfSlling }
156889a89ebfSlling }
1569fa9e4066Sahrens
1570fa9e4066Sahrens return (0);
1571fa9e4066Sahrens }
1572fa9e4066Sahrens
1573394ab0cbSGeorge Wilson int
zpool_export(zpool_handle_t * zhp,boolean_t force,const char * log_str)15744445fffbSMatthew Ahrens zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1575394ab0cbSGeorge Wilson {
15764445fffbSMatthew Ahrens return (zpool_export_common(zhp, force, B_FALSE, log_str));
1577394ab0cbSGeorge Wilson }
1578394ab0cbSGeorge Wilson
1579394ab0cbSGeorge Wilson int
zpool_export_force(zpool_handle_t * zhp,const char * log_str)15804445fffbSMatthew Ahrens zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1581394ab0cbSGeorge Wilson {
15824445fffbSMatthew Ahrens return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1583394ab0cbSGeorge Wilson }
1584394ab0cbSGeorge Wilson
1585468c413aSTim Haley static void
zpool_rewind_exclaim(libzfs_handle_t * hdl,const char * name,boolean_t dryrun,nvlist_t * config)1586468c413aSTim Haley zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
15874b964adaSGeorge Wilson nvlist_t *config)
1588468c413aSTim Haley {
15894b964adaSGeorge Wilson nvlist_t *nv = NULL;
1590468c413aSTim Haley uint64_t rewindto;
1591468c413aSTim Haley int64_t loss = -1;
1592468c413aSTim Haley struct tm t;
1593468c413aSTim Haley char timestr[128];
1594468c413aSTim Haley
15954b964adaSGeorge Wilson if (!hdl->libzfs_printerr || config == NULL)
15964b964adaSGeorge Wilson return;
15974b964adaSGeorge Wilson
1598ad135b5dSChristopher Siden if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1599ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1600468c413aSTim Haley return;
1601ad135b5dSChristopher Siden }
1602468c413aSTim Haley
16034b964adaSGeorge Wilson if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1604468c413aSTim Haley return;
16054b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1606468c413aSTim Haley
1607468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1608468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) {
1609468c413aSTim Haley if (dryrun) {
1610468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1611468c413aSTim Haley "Would be able to return %s "
1612468c413aSTim Haley "to its state as of %s.\n"),
1613468c413aSTim Haley name, timestr);
1614468c413aSTim Haley } else {
1615468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1616468c413aSTim Haley "Pool %s returned to its state as of %s.\n"),
1617468c413aSTim Haley name, timestr);
1618468c413aSTim Haley }
1619468c413aSTim Haley if (loss > 120) {
1620468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1621468c413aSTim Haley "%s approximately %lld "),
1622468c413aSTim Haley dryrun ? "Would discard" : "Discarded",
1623468c413aSTim Haley (loss + 30) / 60);
1624468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1625468c413aSTim Haley "minutes of transactions.\n"));
1626468c413aSTim Haley } else if (loss > 0) {
1627468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1628468c413aSTim Haley "%s approximately %lld "),
1629468c413aSTim Haley dryrun ? "Would discard" : "Discarded", loss);
1630468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1631468c413aSTim Haley "seconds of transactions.\n"));
1632468c413aSTim Haley }
1633468c413aSTim Haley }
1634468c413aSTim Haley }
1635468c413aSTim Haley
1636468c413aSTim Haley void
zpool_explain_recover(libzfs_handle_t * hdl,const char * name,int reason,nvlist_t * config)1637468c413aSTim Haley zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1638468c413aSTim Haley nvlist_t *config)
1639468c413aSTim Haley {
16404b964adaSGeorge Wilson nvlist_t *nv = NULL;
1641468c413aSTim Haley int64_t loss = -1;
1642468c413aSTim Haley uint64_t edata = UINT64_MAX;
1643468c413aSTim Haley uint64_t rewindto;
1644468c413aSTim Haley struct tm t;
1645468c413aSTim Haley char timestr[128];
1646468c413aSTim Haley
1647468c413aSTim Haley if (!hdl->libzfs_printerr)
1648468c413aSTim Haley return;
1649468c413aSTim Haley
1650468c413aSTim Haley if (reason >= 0)
1651468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1652468c413aSTim Haley else
1653468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1654468c413aSTim Haley
1655468c413aSTim Haley /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
16564b964adaSGeorge Wilson if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1657ad135b5dSChristopher Siden nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
16584b964adaSGeorge Wilson nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1659468c413aSTim Haley goto no_info;
1660468c413aSTim Haley
16614b964adaSGeorge Wilson (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
16624b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1663468c413aSTim Haley &edata);
1664468c413aSTim Haley
1665468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1666468c413aSTim Haley "Recovery is possible, but will result in some data loss.\n"));
1667468c413aSTim Haley
1668468c413aSTim Haley if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1669468c413aSTim Haley strftime(timestr, 128, 0, &t) != 0) {
1670468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1671468c413aSTim Haley "\tReturning the pool to its state as of %s\n"
1672468c413aSTim Haley "\tshould correct the problem. "),
1673468c413aSTim Haley timestr);
1674468c413aSTim Haley } else {
1675468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1676468c413aSTim Haley "\tReverting the pool to an earlier state "
1677468c413aSTim Haley "should correct the problem.\n\t"));
1678468c413aSTim Haley }
1679468c413aSTim Haley
1680468c413aSTim Haley if (loss > 120) {
1681468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1682468c413aSTim Haley "Approximately %lld minutes of data\n"
1683468c413aSTim Haley "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1684468c413aSTim Haley } else if (loss > 0) {
1685468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1686468c413aSTim Haley "Approximately %lld seconds of data\n"
1687468c413aSTim Haley "\tmust be discarded, irreversibly. "), loss);
1688468c413aSTim Haley }
1689468c413aSTim Haley if (edata != 0 && edata != UINT64_MAX) {
1690468c413aSTim Haley if (edata == 1) {
1691468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1692468c413aSTim Haley "After rewind, at least\n"
1693468c413aSTim Haley "\tone persistent user-data error will remain. "));
1694468c413aSTim Haley } else {
1695468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1696468c413aSTim Haley "After rewind, several\n"
1697468c413aSTim Haley "\tpersistent user-data errors will remain. "));
1698468c413aSTim Haley }
1699468c413aSTim Haley }
1700468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1701a33cae98STim Haley "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1702a33cae98STim Haley reason >= 0 ? "clear" : "import", name);
1703468c413aSTim Haley
1704468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1705468c413aSTim Haley "A scrub of the pool\n"
1706468c413aSTim Haley "\tis strongly recommended after recovery.\n"));
1707468c413aSTim Haley return;
1708468c413aSTim Haley
1709468c413aSTim Haley no_info:
1710468c413aSTim Haley (void) printf(dgettext(TEXT_DOMAIN,
1711468c413aSTim Haley "Destroy and re-create the pool from\n\ta backup source.\n"));
1712468c413aSTim Haley }
1713468c413aSTim Haley
1714fa9e4066Sahrens /*
1715990b4856Slling * zpool_import() is a contracted interface. Should be kept the same
1716990b4856Slling * if possible.
1717990b4856Slling *
1718990b4856Slling * Applications should use zpool_import_props() to import a pool with
1719990b4856Slling * new properties value to be set.
1720fa9e4066Sahrens */
1721fa9e4066Sahrens int
zpool_import(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,char * altroot)172299653d4eSeschrock zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1723990b4856Slling char *altroot)
1724990b4856Slling {
1725990b4856Slling nvlist_t *props = NULL;
1726990b4856Slling int ret;
1727990b4856Slling
1728990b4856Slling if (altroot != NULL) {
1729990b4856Slling if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1730990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM,
1731990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1732990b4856Slling newname));
1733990b4856Slling }
1734990b4856Slling
1735990b4856Slling if (nvlist_add_string(props,
1736352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1737352d8027SGeorge Wilson nvlist_add_string(props,
1738352d8027SGeorge Wilson zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1739990b4856Slling nvlist_free(props);
1740990b4856Slling return (zfs_error_fmt(hdl, EZFS_NOMEM,
1741990b4856Slling dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1742990b4856Slling newname));
1743990b4856Slling }
1744990b4856Slling }
1745990b4856Slling
17464b964adaSGeorge Wilson ret = zpool_import_props(hdl, config, newname, props,
17474b964adaSGeorge Wilson ZFS_IMPORT_NORMAL);
1748aab83bb8SJosef 'Jeff' Sipek nvlist_free(props);
1749990b4856Slling return (ret);
1750990b4856Slling }
1751990b4856Slling
17524b964adaSGeorge Wilson static void
print_vdev_tree(libzfs_handle_t * hdl,const char * name,nvlist_t * nv,int indent)17534b964adaSGeorge Wilson print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
17544b964adaSGeorge Wilson int indent)
17554b964adaSGeorge Wilson {
17564b964adaSGeorge Wilson nvlist_t **child;
17574b964adaSGeorge Wilson uint_t c, children;
17584b964adaSGeorge Wilson char *vname;
17594b964adaSGeorge Wilson uint64_t is_log = 0;
17604b964adaSGeorge Wilson
17614b964adaSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
17624b964adaSGeorge Wilson &is_log);
17634b964adaSGeorge Wilson
17644b964adaSGeorge Wilson if (name != NULL)
17654b964adaSGeorge Wilson (void) printf("\t%*s%s%s\n", indent, "", name,
17664b964adaSGeorge Wilson is_log ? " [log]" : "");
17674b964adaSGeorge Wilson
17684b964adaSGeorge Wilson if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
17694b964adaSGeorge Wilson &child, &children) != 0)
17704b964adaSGeorge Wilson return;
17714b964adaSGeorge Wilson
17724b964adaSGeorge Wilson for (c = 0; c < children; c++) {
1773663207adSDon Brady vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
17744b964adaSGeorge Wilson print_vdev_tree(hdl, vname, child[c], indent + 2);
17754b964adaSGeorge Wilson free(vname);
17764b964adaSGeorge Wilson }
17774b964adaSGeorge Wilson }
17784b964adaSGeorge Wilson
1779ad135b5dSChristopher Siden void
zpool_print_unsup_feat(nvlist_t * config)1780ad135b5dSChristopher Siden zpool_print_unsup_feat(nvlist_t *config)
1781ad135b5dSChristopher Siden {
1782ad135b5dSChristopher Siden nvlist_t *nvinfo, *unsup_feat;
1783ad135b5dSChristopher Siden
1784ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1785ad135b5dSChristopher Siden 0);
1786ad135b5dSChristopher Siden verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1787ad135b5dSChristopher Siden &unsup_feat) == 0);
1788ad135b5dSChristopher Siden
1789ad135b5dSChristopher Siden for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1790ad135b5dSChristopher Siden nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1791ad135b5dSChristopher Siden char *desc;
1792ad135b5dSChristopher Siden
1793ad135b5dSChristopher Siden verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1794ad135b5dSChristopher Siden verify(nvpair_value_string(nvp, &desc) == 0);
1795ad135b5dSChristopher Siden
1796ad135b5dSChristopher Siden if (strlen(desc) > 0)
1797ad135b5dSChristopher Siden (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1798ad135b5dSChristopher Siden else
1799ad135b5dSChristopher Siden (void) printf("\t%s\n", nvpair_name(nvp));
1800ad135b5dSChristopher Siden }
1801ad135b5dSChristopher Siden }
1802ad135b5dSChristopher Siden
1803990b4856Slling /*
1804990b4856Slling * Import the given pool using the known configuration and a list of
1805990b4856Slling * properties to be set. The configuration should have come from
1806990b4856Slling * zpool_find_import(). The 'newname' parameters control whether the pool
1807990b4856Slling * is imported with a different name.
1808990b4856Slling */
1809990b4856Slling int
zpool_import_props(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,nvlist_t * props,int flags)1810990b4856Slling zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
18114b964adaSGeorge Wilson nvlist_t *props, int flags)
1812fa9e4066Sahrens {
1813e9dbad6fSeschrock zfs_cmd_t zc = { 0 };
18145dafeea3SPavel Zakharov zpool_load_policy_t policy;
18154b964adaSGeorge Wilson nvlist_t *nv = NULL;
18164b964adaSGeorge Wilson nvlist_t *nvinfo = NULL;
18174b964adaSGeorge Wilson nvlist_t *missing = NULL;
1818fa9e4066Sahrens char *thename;
1819fa9e4066Sahrens char *origname;
1820fa9e4066Sahrens int ret;
18214b964adaSGeorge Wilson int error = 0;
1822990b4856Slling char errbuf[1024];
1823fa9e4066Sahrens
1824fa9e4066Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1825fa9e4066Sahrens &origname) == 0);
1826fa9e4066Sahrens
1827990b4856Slling (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1828990b4856Slling "cannot import pool '%s'"), origname);
1829990b4856Slling
1830fa9e4066Sahrens if (newname != NULL) {
183199653d4eSeschrock if (!zpool_name_valid(hdl, B_FALSE, newname))
1832ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
183399653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot import '%s'"),
183499653d4eSeschrock newname));
1835fa9e4066Sahrens thename = (char *)newname;
1836fa9e4066Sahrens } else {
1837fa9e4066Sahrens thename = origname;
1838fa9e4066Sahrens }
1839fa9e4066Sahrens
1840078266a5SMarcel Telka if (props != NULL) {
1841990b4856Slling uint64_t version;
1842f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1843fa9e4066Sahrens
1844990b4856Slling verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1845990b4856Slling &version) == 0);
1846fa9e4066Sahrens
18470a48a24eStimh if ((props = zpool_valid_proplist(hdl, origname,
1848078266a5SMarcel Telka props, version, flags, errbuf)) == NULL)
1849990b4856Slling return (-1);
1850078266a5SMarcel Telka if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1851351420b3Slling nvlist_free(props);
1852990b4856Slling return (-1);
1853351420b3Slling }
1854078266a5SMarcel Telka nvlist_free(props);
1855990b4856Slling }
1856990b4856Slling
1857990b4856Slling (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1858fa9e4066Sahrens
1859fa9e4066Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1860ea8dc4b6Seschrock &zc.zc_guid) == 0);
1861fa9e4066Sahrens
1862351420b3Slling if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1863078266a5SMarcel Telka zcmd_free_nvlists(&zc);
186499653d4eSeschrock return (-1);
1865351420b3Slling }
186657f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1867078266a5SMarcel Telka zcmd_free_nvlists(&zc);
1868468c413aSTim Haley return (-1);
1869468c413aSTim Haley }
1870fa9e4066Sahrens
18714b964adaSGeorge Wilson zc.zc_cookie = flags;
18724b964adaSGeorge Wilson while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
18734b964adaSGeorge Wilson errno == ENOMEM) {
18744b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
18754b964adaSGeorge Wilson zcmd_free_nvlists(&zc);
18764b964adaSGeorge Wilson return (-1);
18774b964adaSGeorge Wilson }
18784b964adaSGeorge Wilson }
18794b964adaSGeorge Wilson if (ret != 0)
18804b964adaSGeorge Wilson error = errno;
18814b964adaSGeorge Wilson
18824b964adaSGeorge Wilson (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1883078266a5SMarcel Telka
1884078266a5SMarcel Telka zcmd_free_nvlists(&zc);
1885078266a5SMarcel Telka
18865dafeea3SPavel Zakharov zpool_get_load_policy(config, &policy);
18874b964adaSGeorge Wilson
18884b964adaSGeorge Wilson if (error) {
1889fa9e4066Sahrens char desc[1024];
1890e0f1c0afSOlaf Faaland char aux[256];
1891468c413aSTim Haley
1892468c413aSTim Haley /*
1893468c413aSTim Haley * Dry-run failed, but we print out what success
1894468c413aSTim Haley * looks like if we found a best txg
1895468c413aSTim Haley */
18965dafeea3SPavel Zakharov if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1897468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename,
18984b964adaSGeorge Wilson B_TRUE, nv);
18994b964adaSGeorge Wilson nvlist_free(nv);
1900468c413aSTim Haley return (-1);
1901468c413aSTim Haley }
1902468c413aSTim Haley
1903fa9e4066Sahrens if (newname == NULL)
1904fa9e4066Sahrens (void) snprintf(desc, sizeof (desc),
1905fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1906fa9e4066Sahrens thename);
1907fa9e4066Sahrens else
1908fa9e4066Sahrens (void) snprintf(desc, sizeof (desc),
1909fa9e4066Sahrens dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1910fa9e4066Sahrens origname, thename);
1911fa9e4066Sahrens
19124b964adaSGeorge Wilson switch (error) {
1913ea8dc4b6Seschrock case ENOTSUP:
1914ad135b5dSChristopher Siden if (nv != NULL && nvlist_lookup_nvlist(nv,
1915ad135b5dSChristopher Siden ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1916ad135b5dSChristopher Siden nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1917ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN, "This "
1918ad135b5dSChristopher Siden "pool uses the following feature(s) not "
1919ad135b5dSChristopher Siden "supported by this system:\n"));
1920ad135b5dSChristopher Siden zpool_print_unsup_feat(nv);
1921ad135b5dSChristopher Siden if (nvlist_exists(nvinfo,
1922ad135b5dSChristopher Siden ZPOOL_CONFIG_CAN_RDONLY)) {
1923ad135b5dSChristopher Siden (void) printf(dgettext(TEXT_DOMAIN,
1924ad135b5dSChristopher Siden "All unsupported features are only "
1925ad135b5dSChristopher Siden "required for writing to the pool."
1926ad135b5dSChristopher Siden "\nThe pool can be imported using "
1927ad135b5dSChristopher Siden "'-o readonly=on'.\n"));
1928ad135b5dSChristopher Siden }
1929ad135b5dSChristopher Siden }
1930ea8dc4b6Seschrock /*
1931ea8dc4b6Seschrock * Unsupported version.
1932ea8dc4b6Seschrock */
193399653d4eSeschrock (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1934ea8dc4b6Seschrock break;
1935ea8dc4b6Seschrock
1936e0f1c0afSOlaf Faaland case EREMOTEIO:
1937e0f1c0afSOlaf Faaland if (nv != NULL && nvlist_lookup_nvlist(nv,
1938e0f1c0afSOlaf Faaland ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1939e0f1c0afSOlaf Faaland char *hostname = "<unknown>";
1940e0f1c0afSOlaf Faaland uint64_t hostid = 0;
1941e0f1c0afSOlaf Faaland mmp_state_t mmp_state;
1942e0f1c0afSOlaf Faaland
1943e0f1c0afSOlaf Faaland mmp_state = fnvlist_lookup_uint64(nvinfo,
1944e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_STATE);
1945e0f1c0afSOlaf Faaland
1946e0f1c0afSOlaf Faaland if (nvlist_exists(nvinfo,
1947e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTNAME))
1948e0f1c0afSOlaf Faaland hostname = fnvlist_lookup_string(nvinfo,
1949e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTNAME);
1950e0f1c0afSOlaf Faaland
1951e0f1c0afSOlaf Faaland if (nvlist_exists(nvinfo,
1952e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTID))
1953e0f1c0afSOlaf Faaland hostid = fnvlist_lookup_uint64(nvinfo,
1954e0f1c0afSOlaf Faaland ZPOOL_CONFIG_MMP_HOSTID);
1955e0f1c0afSOlaf Faaland
1956e0f1c0afSOlaf Faaland if (mmp_state == MMP_STATE_ACTIVE) {
1957e0f1c0afSOlaf Faaland (void) snprintf(aux, sizeof (aux),
1958e0f1c0afSOlaf Faaland dgettext(TEXT_DOMAIN, "pool is imp"
1959e0f1c0afSOlaf Faaland "orted on host '%s' (hostid=%lx).\n"
1960e0f1c0afSOlaf Faaland "Export the pool on the other "
1961e0f1c0afSOlaf Faaland "system, then run 'zpool import'."),
1962e0f1c0afSOlaf Faaland hostname, (unsigned long) hostid);
1963e0f1c0afSOlaf Faaland } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1964e0f1c0afSOlaf Faaland (void) snprintf(aux, sizeof (aux),
1965e0f1c0afSOlaf Faaland dgettext(TEXT_DOMAIN, "pool has "
1966e0f1c0afSOlaf Faaland "the multihost property on and "
1967e0f1c0afSOlaf Faaland "the\nsystem's hostid is not "
1968e0f1c0afSOlaf Faaland "set.\n"));
1969e0f1c0afSOlaf Faaland }
1970e0f1c0afSOlaf Faaland
1971e0f1c0afSOlaf Faaland (void) zfs_error_aux(hdl, aux);
1972e0f1c0afSOlaf Faaland }
1973e0f1c0afSOlaf Faaland (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1974e0f1c0afSOlaf Faaland break;
1975e0f1c0afSOlaf Faaland
1976b5989ec7Seschrock case EINVAL:
1977b5989ec7Seschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1978b5989ec7Seschrock break;
1979b5989ec7Seschrock
198054a91118SChris Kirby case EROFS:
198154a91118SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198254a91118SChris Kirby "one or more devices is read only"));
198354a91118SChris Kirby (void) zfs_error(hdl, EZFS_BADDEV, desc);
198454a91118SChris Kirby break;
198554a91118SChris Kirby
19864b964adaSGeorge Wilson case ENXIO:
19874b964adaSGeorge Wilson if (nv && nvlist_lookup_nvlist(nv,
19884b964adaSGeorge Wilson ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
19894b964adaSGeorge Wilson nvlist_lookup_nvlist(nvinfo,
19904b964adaSGeorge Wilson ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
19914b964adaSGeorge Wilson (void) printf(dgettext(TEXT_DOMAIN,
19926f793812SPavel Zakharov "The devices below are missing or "
19936f793812SPavel Zakharov "corrupted, use '-m' to import the pool "
19946f793812SPavel Zakharov "anyway:\n"));
19954b964adaSGeorge Wilson print_vdev_tree(hdl, NULL, missing, 2);
19964b964adaSGeorge Wilson (void) printf("\n");
19974b964adaSGeorge Wilson }
19984b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc);
19994b964adaSGeorge Wilson break;
20004b964adaSGeorge Wilson
20014b964adaSGeorge Wilson case EEXIST:
20024b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc);
20034b964adaSGeorge Wilson break;
2004c971037bSPaul Dagnelie case ENAMETOOLONG:
2005c971037bSPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2006c971037bSPaul Dagnelie "new name of at least one dataset is longer than "
2007c971037bSPaul Dagnelie "the maximum allowable length"));
2008c971037bSPaul Dagnelie (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2009c971037bSPaul Dagnelie break;
2010fa9e4066Sahrens default:
20114b964adaSGeorge Wilson (void) zpool_standard_error(hdl, error, desc);
2012468c413aSTim Haley zpool_explain_recover(hdl,
20134b964adaSGeorge Wilson newname ? origname : thename, -error, nv);
2014468c413aSTim Haley break;
2015fa9e4066Sahrens }
2016fa9e4066Sahrens
20174b964adaSGeorge Wilson nvlist_free(nv);
2018fa9e4066Sahrens ret = -1;
2019fa9e4066Sahrens } else {
2020fa9e4066Sahrens zpool_handle_t *zhp;
2021ecd6cf80Smarks
2022fa9e4066Sahrens /*
2023fa9e4066Sahrens * This should never fail, but play it safe anyway.
2024fa9e4066Sahrens */
2025681d9761SEric Taylor if (zpool_open_silent(hdl, thename, &zhp) != 0)
202694de1d4cSeschrock ret = -1;
2027681d9761SEric Taylor else if (zhp != NULL)
2028fa9e4066Sahrens zpool_close(zhp);
20295dafeea3SPavel Zakharov if (policy.zlp_rewind &
2030468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2031468c413aSTim Haley zpool_rewind_exclaim(hdl, newname ? origname : thename,
20325dafeea3SPavel Zakharov ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2033468c413aSTim Haley }
20344b964adaSGeorge Wilson nvlist_free(nv);
2035468c413aSTim Haley return (0);
2036fa9e4066Sahrens }
2037fa9e4066Sahrens
2038fa9e4066Sahrens return (ret);
2039fa9e4066Sahrens }
2040fa9e4066Sahrens
2041084fd14fSBrian Behlendorf /*
2042084fd14fSBrian Behlendorf * Translate vdev names to guids. If a vdev_path is determined to be
2043084fd14fSBrian Behlendorf * unsuitable then a vd_errlist is allocated and the vdev path and errno
2044084fd14fSBrian Behlendorf * are added to it.
2045084fd14fSBrian Behlendorf */
2046084fd14fSBrian Behlendorf static int
zpool_translate_vdev_guids(zpool_handle_t * zhp,nvlist_t * vds,nvlist_t * vdev_guids,nvlist_t * guids_to_paths,nvlist_t ** vd_errlist)2047084fd14fSBrian Behlendorf zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2048084fd14fSBrian Behlendorf nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2049084fd14fSBrian Behlendorf {
2050084fd14fSBrian Behlendorf nvlist_t *errlist = NULL;
2051084fd14fSBrian Behlendorf int error = 0;
2052084fd14fSBrian Behlendorf
2053084fd14fSBrian Behlendorf for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2054084fd14fSBrian Behlendorf elem = nvlist_next_nvpair(vds, elem)) {
2055084fd14fSBrian Behlendorf boolean_t spare, cache;
2056084fd14fSBrian Behlendorf
2057084fd14fSBrian Behlendorf char *vd_path = nvpair_name(elem);
2058084fd14fSBrian Behlendorf nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2059084fd14fSBrian Behlendorf NULL);
2060084fd14fSBrian Behlendorf
2061084fd14fSBrian Behlendorf if ((tgt == NULL) || cache || spare) {
2062084fd14fSBrian Behlendorf if (errlist == NULL) {
2063084fd14fSBrian Behlendorf errlist = fnvlist_alloc();
2064084fd14fSBrian Behlendorf error = EINVAL;
2065084fd14fSBrian Behlendorf }
2066084fd14fSBrian Behlendorf
2067084fd14fSBrian Behlendorf uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2068084fd14fSBrian Behlendorf (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2069084fd14fSBrian Behlendorf fnvlist_add_int64(errlist, vd_path, err);
2070084fd14fSBrian Behlendorf continue;
2071084fd14fSBrian Behlendorf }
2072084fd14fSBrian Behlendorf
2073084fd14fSBrian Behlendorf uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2074084fd14fSBrian Behlendorf fnvlist_add_uint64(vdev_guids, vd_path, guid);
2075084fd14fSBrian Behlendorf
2076084fd14fSBrian Behlendorf char msg[MAXNAMELEN];
2077084fd14fSBrian Behlendorf (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2078084fd14fSBrian Behlendorf fnvlist_add_string(guids_to_paths, msg, vd_path);
2079084fd14fSBrian Behlendorf }
2080084fd14fSBrian Behlendorf
2081084fd14fSBrian Behlendorf if (error != 0) {
2082084fd14fSBrian Behlendorf verify(errlist != NULL);
2083084fd14fSBrian Behlendorf if (vd_errlist != NULL)
2084084fd14fSBrian Behlendorf *vd_errlist = errlist;
2085084fd14fSBrian Behlendorf else
2086084fd14fSBrian Behlendorf fnvlist_free(errlist);
2087084fd14fSBrian Behlendorf }
2088084fd14fSBrian Behlendorf
2089084fd14fSBrian Behlendorf return (error);
2090084fd14fSBrian Behlendorf }
2091084fd14fSBrian Behlendorf
2092fa9e4066Sahrens /*
20933f9d6ad7SLin Ling * Scan the pool.
2094fa9e4066Sahrens */
2095fa9e4066Sahrens int
zpool_scan(zpool_handle_t * zhp,pool_scan_func_t func,pool_scrub_cmd_t cmd)20961702cce7SAlek Pinchuk zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2097fa9e4066Sahrens {
2098fa9e4066Sahrens zfs_cmd_t zc = { 0 };
2099fa9e4066Sahrens char msg[1024];
21001702cce7SAlek Pinchuk int err;
210199653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
2102fa9e4066Sahrens
2103fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
21043f9d6ad7SLin Ling zc.zc_cookie = func;
21051702cce7SAlek Pinchuk zc.zc_flags = cmd;
2106fa9e4066Sahrens
21071702cce7SAlek Pinchuk if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
21081702cce7SAlek Pinchuk return (0);
21091702cce7SAlek Pinchuk
21101702cce7SAlek Pinchuk err = errno;
21111702cce7SAlek Pinchuk
21121702cce7SAlek Pinchuk /* ECANCELED on a scrub means we resumed a paused scrub */
21131702cce7SAlek Pinchuk if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
21141702cce7SAlek Pinchuk cmd == POOL_SCRUB_NORMAL)
21151702cce7SAlek Pinchuk return (0);
21161702cce7SAlek Pinchuk
21171702cce7SAlek Pinchuk if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2118fa9e4066Sahrens return (0);
2119fa9e4066Sahrens
21203f9d6ad7SLin Ling if (func == POOL_SCAN_SCRUB) {
21211702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE) {
21221702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
21231702cce7SAlek Pinchuk "cannot pause scrubbing %s"), zc.zc_name);
21241702cce7SAlek Pinchuk } else {
21251702cce7SAlek Pinchuk assert(cmd == POOL_SCRUB_NORMAL);
21261702cce7SAlek Pinchuk (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
21271702cce7SAlek Pinchuk "cannot scrub %s"), zc.zc_name);
21281702cce7SAlek Pinchuk }
2129e4c795beSTom Caputi } else if (func == POOL_SCAN_RESILVER) {
2130e4c795beSTom Caputi assert(cmd == POOL_SCRUB_NORMAL);
2131e4c795beSTom Caputi (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2132e4c795beSTom Caputi "cannot restart resilver on %s"), zc.zc_name);
21333f9d6ad7SLin Ling } else if (func == POOL_SCAN_NONE) {
21343f9d6ad7SLin Ling (void) snprintf(msg, sizeof (msg),
21353f9d6ad7SLin Ling dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
21363f9d6ad7SLin Ling zc.zc_name);
21373f9d6ad7SLin Ling } else {
21383f9d6ad7SLin Ling assert(!"unexpected result");
21393f9d6ad7SLin Ling }
2140fa9e4066Sahrens
21411702cce7SAlek Pinchuk if (err == EBUSY) {
21423f9d6ad7SLin Ling nvlist_t *nvroot;
21433f9d6ad7SLin Ling pool_scan_stat_t *ps = NULL;
21443f9d6ad7SLin Ling uint_t psc;
21453f9d6ad7SLin Ling
21463f9d6ad7SLin Ling verify(nvlist_lookup_nvlist(zhp->zpool_config,
21473f9d6ad7SLin Ling ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
21483f9d6ad7SLin Ling (void) nvlist_lookup_uint64_array(nvroot,
21493f9d6ad7SLin Ling ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
21501702cce7SAlek Pinchuk if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
21511702cce7SAlek Pinchuk if (cmd == POOL_SCRUB_PAUSE)
21521702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
21531702cce7SAlek Pinchuk else
21541702cce7SAlek Pinchuk return (zfs_error(hdl, EZFS_SCRUBBING, msg));
21551702cce7SAlek Pinchuk } else {
21563f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_RESILVERING, msg));
21571702cce7SAlek Pinchuk }
21581702cce7SAlek Pinchuk } else if (err == ENOENT) {
21593f9d6ad7SLin Ling return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2160e4c795beSTom Caputi } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2161e4c795beSTom Caputi return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
21623f9d6ad7SLin Ling } else {
21631702cce7SAlek Pinchuk return (zpool_standard_error(hdl, err, msg));
21643f9d6ad7SLin Ling }
2165fa9e4066Sahrens }
2166fa9e4066Sahrens
2167094e47e9SGeorge Wilson static int
xlate_init_err(int err)2168094e47e9SGeorge Wilson xlate_init_err(int err)
2169094e47e9SGeorge Wilson {
2170094e47e9SGeorge Wilson switch (err) {
2171094e47e9SGeorge Wilson case ENODEV:
2172094e47e9SGeorge Wilson return (EZFS_NODEVICE);
2173094e47e9SGeorge Wilson case EINVAL:
2174094e47e9SGeorge Wilson case EROFS:
2175094e47e9SGeorge Wilson return (EZFS_BADDEV);
2176094e47e9SGeorge Wilson case EBUSY:
2177094e47e9SGeorge Wilson return (EZFS_INITIALIZING);
2178094e47e9SGeorge Wilson case ESRCH:
2179094e47e9SGeorge Wilson return (EZFS_NO_INITIALIZE);
2180094e47e9SGeorge Wilson }
2181094e47e9SGeorge Wilson return (err);
2182094e47e9SGeorge Wilson }
2183094e47e9SGeorge Wilson
2184094e47e9SGeorge Wilson /*
2185094e47e9SGeorge Wilson * Begin, suspend, or cancel the initialization (initializing of all free
2186094e47e9SGeorge Wilson * blocks) for the given vdevs in the given pool.
2187094e47e9SGeorge Wilson */
2188094e47e9SGeorge Wilson int
zpool_initialize(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds)2189094e47e9SGeorge Wilson zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2190094e47e9SGeorge Wilson nvlist_t *vds)
2191094e47e9SGeorge Wilson {
2192094e47e9SGeorge Wilson char msg[1024];
2193084fd14fSBrian Behlendorf int err;
2194094e47e9SGeorge Wilson
2195094e47e9SGeorge Wilson nvlist_t *vdev_guids = fnvlist_alloc();
2196094e47e9SGeorge Wilson nvlist_t *guids_to_paths = fnvlist_alloc();
2197084fd14fSBrian Behlendorf nvlist_t *vd_errlist = NULL;
2198084fd14fSBrian Behlendorf nvlist_t *errlist;
2199094e47e9SGeorge Wilson nvpair_t *elem;
2200094e47e9SGeorge Wilson
2201084fd14fSBrian Behlendorf err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2202084fd14fSBrian Behlendorf guids_to_paths, &vd_errlist);
2203094e47e9SGeorge Wilson
2204084fd14fSBrian Behlendorf if (err == 0) {
2205084fd14fSBrian Behlendorf err = lzc_initialize(zhp->zpool_name, cmd_type,
2206084fd14fSBrian Behlendorf vdev_guids, &errlist);
2207084fd14fSBrian Behlendorf if (err == 0) {
2208094e47e9SGeorge Wilson fnvlist_free(vdev_guids);
2209094e47e9SGeorge Wilson fnvlist_free(guids_to_paths);
2210084fd14fSBrian Behlendorf return (0);
2211094e47e9SGeorge Wilson }
2212094e47e9SGeorge Wilson
2213084fd14fSBrian Behlendorf if (errlist != NULL) {
2214084fd14fSBrian Behlendorf vd_errlist = fnvlist_lookup_nvlist(errlist,
2215084fd14fSBrian Behlendorf ZPOOL_INITIALIZE_VDEVS);
2216084fd14fSBrian Behlendorf }
2217094e47e9SGeorge Wilson
2218084fd14fSBrian Behlendorf (void) snprintf(msg, sizeof (msg),
2219084fd14fSBrian Behlendorf dgettext(TEXT_DOMAIN, "operation failed"));
2220084fd14fSBrian Behlendorf } else {
2221084fd14fSBrian Behlendorf verify(vd_errlist != NULL);
2222084fd14fSBrian Behlendorf }
2223084fd14fSBrian Behlendorf
2224084fd14fSBrian Behlendorf for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2225084fd14fSBrian Behlendorf elem = nvlist_next_nvpair(vd_errlist, elem)) {
2226084fd14fSBrian Behlendorf int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2227084fd14fSBrian Behlendorf char *path;
2228084fd14fSBrian Behlendorf
2229084fd14fSBrian Behlendorf if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2230084fd14fSBrian Behlendorf &path) != 0)
2231084fd14fSBrian Behlendorf path = nvpair_name(elem);
2232084fd14fSBrian Behlendorf
2233084fd14fSBrian Behlendorf (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2234084fd14fSBrian Behlendorf "cannot initialize '%s'", path);
2235094e47e9SGeorge Wilson }
2236094e47e9SGeorge Wilson
2237094e47e9SGeorge Wilson fnvlist_free(vdev_guids);
2238084fd14fSBrian Behlendorf fnvlist_free(guids_to_paths);
2239094e47e9SGeorge Wilson
2240084fd14fSBrian Behlendorf if (vd_errlist != NULL) {
2241084fd14fSBrian Behlendorf fnvlist_free(vd_errlist);
2242084fd14fSBrian Behlendorf return (-1);
2243084fd14fSBrian Behlendorf }
2244084fd14fSBrian Behlendorf
2245084fd14fSBrian Behlendorf return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2246084fd14fSBrian Behlendorf }
2247084fd14fSBrian Behlendorf
2248084fd14fSBrian Behlendorf static int
xlate_trim_err(int err)2249084fd14fSBrian Behlendorf xlate_trim_err(int err)
2250084fd14fSBrian Behlendorf {
2251084fd14fSBrian Behlendorf switch (err) {
2252084fd14fSBrian Behlendorf case ENODEV:
2253084fd14fSBrian Behlendorf return (EZFS_NODEVICE);
2254084fd14fSBrian Behlendorf case EINVAL:
2255084fd14fSBrian Behlendorf case EROFS:
2256084fd14fSBrian Behlendorf return (EZFS_BADDEV);
2257084fd14fSBrian Behlendorf case EBUSY:
2258084fd14fSBrian Behlendorf return (EZFS_TRIMMING);
2259084fd14fSBrian Behlendorf case ESRCH:
2260084fd14fSBrian Behlendorf return (EZFS_NO_TRIM);
2261084fd14fSBrian Behlendorf case EOPNOTSUPP:
2262084fd14fSBrian Behlendorf return (EZFS_TRIM_NOTSUP);
2263094e47e9SGeorge Wilson }
2264084fd14fSBrian Behlendorf return (err);
2265084fd14fSBrian Behlendorf }
2266084fd14fSBrian Behlendorf
2267084fd14fSBrian Behlendorf /*
2268084fd14fSBrian Behlendorf * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2269084fd14fSBrian Behlendorf * the given vdevs in the given pool.
2270084fd14fSBrian Behlendorf */
2271084fd14fSBrian Behlendorf int
zpool_trim(zpool_handle_t * zhp,pool_trim_func_t cmd_type,nvlist_t * vds,trimflags_t * trim_flags)2272084fd14fSBrian Behlendorf zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2273084fd14fSBrian Behlendorf trimflags_t *trim_flags)
2274084fd14fSBrian Behlendorf {
2275084fd14fSBrian Behlendorf char msg[1024];
2276084fd14fSBrian Behlendorf int err;
2277094e47e9SGeorge Wilson
2278084fd14fSBrian Behlendorf nvlist_t *vdev_guids = fnvlist_alloc();
2279084fd14fSBrian Behlendorf nvlist_t *guids_to_paths = fnvlist_alloc();
2280094e47e9SGeorge Wilson nvlist_t *vd_errlist = NULL;
2281084fd14fSBrian Behlendorf nvlist_t *errlist;
2282084fd14fSBrian Behlendorf nvpair_t *elem;
2283084fd14fSBrian Behlendorf
2284084fd14fSBrian Behlendorf err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2285084fd14fSBrian Behlendorf guids_to_paths, &vd_errlist);
2286084fd14fSBrian Behlendorf if (err == 0) {
2287084fd14fSBrian Behlendorf err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2288084fd14fSBrian Behlendorf trim_flags->secure, vdev_guids, &errlist);
2289084fd14fSBrian Behlendorf if (err == 0) {
2290084fd14fSBrian Behlendorf fnvlist_free(vdev_guids);
2291084fd14fSBrian Behlendorf fnvlist_free(guids_to_paths);
2292084fd14fSBrian Behlendorf return (0);
2293084fd14fSBrian Behlendorf }
2294084fd14fSBrian Behlendorf
2295084fd14fSBrian Behlendorf if (errlist != NULL) {
2296084fd14fSBrian Behlendorf vd_errlist = fnvlist_lookup_nvlist(errlist,
2297084fd14fSBrian Behlendorf ZPOOL_TRIM_VDEVS);
2298084fd14fSBrian Behlendorf }
2299084fd14fSBrian Behlendorf
2300084fd14fSBrian Behlendorf (void) snprintf(msg, sizeof (msg),
2301084fd14fSBrian Behlendorf dgettext(TEXT_DOMAIN, "operation failed"));
2302084fd14fSBrian Behlendorf } else {
2303084fd14fSBrian Behlendorf verify(vd_errlist != NULL);
2304094e47e9SGeorge Wilson }
2305094e47e9SGeorge Wilson
2306084fd14fSBrian Behlendorf for (elem = nvlist_next_nvpair(vd_errlist, NULL);
2307084fd14fSBrian Behlendorf elem != NULL; elem = nvlist_next_nvpair(vd_errlist, elem)) {
2308084fd14fSBrian Behlendorf int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2309084fd14fSBrian Behlendorf char *path;
2310084fd14fSBrian Behlendorf /*
2311084fd14fSBrian Behlendorf * If only the pool was specified, and it was not a secure
2312084fd14fSBrian Behlendorf * trim then suppress warnings for individual vdevs which
2313084fd14fSBrian Behlendorf * do not support trimming.
2314084fd14fSBrian Behlendorf */
2315084fd14fSBrian Behlendorf if (vd_error == EZFS_TRIM_NOTSUP &&
2316084fd14fSBrian Behlendorf trim_flags->fullpool &&
2317084fd14fSBrian Behlendorf !trim_flags->secure) {
2318084fd14fSBrian Behlendorf continue;
2319084fd14fSBrian Behlendorf }
2320094e47e9SGeorge Wilson
2321084fd14fSBrian Behlendorf if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2322084fd14fSBrian Behlendorf &path) != 0)
2323084fd14fSBrian Behlendorf path = nvpair_name(elem);
2324084fd14fSBrian Behlendorf
2325084fd14fSBrian Behlendorf (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2326084fd14fSBrian Behlendorf "cannot trim '%s'", path);
2327094e47e9SGeorge Wilson }
2328094e47e9SGeorge Wilson
2329084fd14fSBrian Behlendorf fnvlist_free(vdev_guids);
2330094e47e9SGeorge Wilson fnvlist_free(guids_to_paths);
2331084fd14fSBrian Behlendorf
2332084fd14fSBrian Behlendorf if (vd_errlist != NULL) {
2333084fd14fSBrian Behlendorf fnvlist_free(vd_errlist);
2334094e47e9SGeorge Wilson return (-1);
2335084fd14fSBrian Behlendorf }
2336094e47e9SGeorge Wilson
2337084fd14fSBrian Behlendorf return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2338094e47e9SGeorge Wilson }
2339094e47e9SGeorge Wilson
23403fdda499SJohn Harres /*
23413fdda499SJohn Harres * This provides a very minimal check whether a given string is likely a
23423fdda499SJohn Harres * c#t#d# style string. Users of this are expected to do their own
23433fdda499SJohn Harres * verification of the s# part.
23443fdda499SJohn Harres */
23453fdda499SJohn Harres #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
23463fdda499SJohn Harres
23473fdda499SJohn Harres /*
23483fdda499SJohn Harres * More elaborate version for ones which may start with "/dev/dsk/"
23493fdda499SJohn Harres * and the like.
23503fdda499SJohn Harres */
23513fdda499SJohn Harres static int
ctd_check_path(char * str)23529a686fbcSPaul Dagnelie ctd_check_path(char *str)
23539a686fbcSPaul Dagnelie {
23543fdda499SJohn Harres /*
23553fdda499SJohn Harres * If it starts with a slash, check the last component.
23563fdda499SJohn Harres */
23573fdda499SJohn Harres if (str && str[0] == '/') {
23583fdda499SJohn Harres char *tmp = strrchr(str, '/');
23593fdda499SJohn Harres
23603fdda499SJohn Harres /*
23613fdda499SJohn Harres * If it ends in "/old", check the second-to-last
23623fdda499SJohn Harres * component of the string instead.
23633fdda499SJohn Harres */
23643fdda499SJohn Harres if (tmp != str && strcmp(tmp, "/old") == 0) {
23653fdda499SJohn Harres for (tmp--; *tmp != '/'; tmp--)
23663fdda499SJohn Harres ;
23673fdda499SJohn Harres }
23683fdda499SJohn Harres str = tmp + 1;
23693fdda499SJohn Harres }
23703fdda499SJohn Harres return (CTD_CHECK(str));
23713fdda499SJohn Harres }
23723fdda499SJohn Harres
2373a43d325bSek /*
2374573ca77eSGeorge Wilson * Find a vdev that matches the search criteria specified. We use the
2375573ca77eSGeorge Wilson * the nvpair name to determine how we should look for the device.
2376a43d325bSek * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2377a43d325bSek * spare; but FALSE if its an INUSE spare.
2378a43d325bSek */
237999653d4eSeschrock static nvlist_t *
vdev_to_nvlist_iter(nvlist_t * nv,nvlist_t * search,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)2380573ca77eSGeorge Wilson vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2381573ca77eSGeorge Wilson boolean_t *l2cache, boolean_t *log)
2382ea8dc4b6Seschrock {
2383ea8dc4b6Seschrock uint_t c, children;
2384ea8dc4b6Seschrock nvlist_t **child;
238599653d4eSeschrock nvlist_t *ret;
2386ee0eb9f2SEric Schrock uint64_t is_log;
2387573ca77eSGeorge Wilson char *srchkey;
2388573ca77eSGeorge Wilson nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2389573ca77eSGeorge Wilson
2390573ca77eSGeorge Wilson /* Nothing to look for */
2391573ca77eSGeorge Wilson if (search == NULL || pair == NULL)
2392573ca77eSGeorge Wilson return (NULL);
2393ea8dc4b6Seschrock
2394573ca77eSGeorge Wilson /* Obtain the key we will use to search */
2395573ca77eSGeorge Wilson srchkey = nvpair_name(pair);
2396573ca77eSGeorge Wilson
2397573ca77eSGeorge Wilson switch (nvpair_type(pair)) {
2398cb04b873SMark J Musante case DATA_TYPE_UINT64:
2399573ca77eSGeorge Wilson if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2400cb04b873SMark J Musante uint64_t srchval, theguid;
2401cb04b873SMark J Musante
2402cb04b873SMark J Musante verify(nvpair_value_uint64(pair, &srchval) == 0);
2403cb04b873SMark J Musante verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2404cb04b873SMark J Musante &theguid) == 0);
2405cb04b873SMark J Musante if (theguid == srchval)
2406cb04b873SMark J Musante return (nv);
2407573ca77eSGeorge Wilson }
2408573ca77eSGeorge Wilson break;
2409573ca77eSGeorge Wilson
2410573ca77eSGeorge Wilson case DATA_TYPE_STRING: {
2411573ca77eSGeorge Wilson char *srchval, *val;
2412573ca77eSGeorge Wilson
2413573ca77eSGeorge Wilson verify(nvpair_value_string(pair, &srchval) == 0);
2414573ca77eSGeorge Wilson if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2415573ca77eSGeorge Wilson break;
2416ea8dc4b6Seschrock
2417ea8dc4b6Seschrock /*
24183fdda499SJohn Harres * Search for the requested value. Special cases:
24193fdda499SJohn Harres *
24207855d95bSToomas Soome * - ZPOOL_CONFIG_PATH for whole disk entries. To support
24217855d95bSToomas Soome * UEFI boot, these end in "s0" or "s0/old" or "s1" or
24227855d95bSToomas Soome * "s1/old". The "s0" or "s1" part is hidden from the user,
24233fdda499SJohn Harres * but included in the string, so this matches around it.
24243fdda499SJohn Harres * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
24253fdda499SJohn Harres *
242688ecc943SGeorge Wilson * Otherwise, all other searches are simple string compares.
2427ea8dc4b6Seschrock */
24283fdda499SJohn Harres if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
24293fdda499SJohn Harres ctd_check_path(val)) {
2430573ca77eSGeorge Wilson uint64_t wholedisk = 0;
2431573ca77eSGeorge Wilson
2432573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2433573ca77eSGeorge Wilson &wholedisk);
2434573ca77eSGeorge Wilson if (wholedisk) {
24353fdda499SJohn Harres int slen = strlen(srchval);
24363fdda499SJohn Harres int vlen = strlen(val);
24373fdda499SJohn Harres
24383fdda499SJohn Harres if (slen != vlen - 2)
24393fdda499SJohn Harres break;
24403fdda499SJohn Harres
24413fdda499SJohn Harres /*
24423fdda499SJohn Harres * make_leaf_vdev() should only set
24433fdda499SJohn Harres * wholedisk for ZPOOL_CONFIG_PATHs which
24443fdda499SJohn Harres * will include "/dev/dsk/", giving plenty of
24453fdda499SJohn Harres * room for the indices used next.
24463fdda499SJohn Harres */
24473fdda499SJohn Harres ASSERT(vlen >= 6);
24483fdda499SJohn Harres
24493fdda499SJohn Harres /*
24503fdda499SJohn Harres * strings identical except trailing "s0"
24513fdda499SJohn Harres */
24527855d95bSToomas Soome if ((strcmp(&val[vlen - 2], "s0") == 0 ||
24537855d95bSToomas Soome strcmp(&val[vlen - 2], "s1") == 0) &&
24543fdda499SJohn Harres strncmp(srchval, val, slen) == 0)
24553fdda499SJohn Harres return (nv);
24563fdda499SJohn Harres
2457573ca77eSGeorge Wilson /*
24583fdda499SJohn Harres * strings identical except trailing "s0/old"
2459573ca77eSGeorge Wilson */
24607855d95bSToomas Soome if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
24617855d95bSToomas Soome strcmp(&val[vlen - 6], "s1/old") == 0) &&
24623fdda499SJohn Harres strcmp(&srchval[slen - 4], "/old") == 0 &&
24633fdda499SJohn Harres strncmp(srchval, val, slen - 4) == 0)
2464573ca77eSGeorge Wilson return (nv);
24653fdda499SJohn Harres
2466573ca77eSGeorge Wilson break;
2467573ca77eSGeorge Wilson }
246888ecc943SGeorge Wilson } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
246988ecc943SGeorge Wilson char *type, *idx, *end, *p;
247088ecc943SGeorge Wilson uint64_t id, vdev_id;
247188ecc943SGeorge Wilson
247288ecc943SGeorge Wilson /*
247388ecc943SGeorge Wilson * Determine our vdev type, keeping in mind
247488ecc943SGeorge Wilson * that the srchval is composed of a type and
247588ecc943SGeorge Wilson * vdev id pair (i.e. mirror-4).
247688ecc943SGeorge Wilson */
247788ecc943SGeorge Wilson if ((type = strdup(srchval)) == NULL)
247888ecc943SGeorge Wilson return (NULL);
247988ecc943SGeorge Wilson
248088ecc943SGeorge Wilson if ((p = strrchr(type, '-')) == NULL) {
248188ecc943SGeorge Wilson free(type);
248288ecc943SGeorge Wilson break;
248388ecc943SGeorge Wilson }
248488ecc943SGeorge Wilson idx = p + 1;
248588ecc943SGeorge Wilson *p = '\0';
248688ecc943SGeorge Wilson
248788ecc943SGeorge Wilson /*
248888ecc943SGeorge Wilson * If the types don't match then keep looking.
248988ecc943SGeorge Wilson */
249088ecc943SGeorge Wilson if (strncmp(val, type, strlen(val)) != 0) {
249188ecc943SGeorge Wilson free(type);
249288ecc943SGeorge Wilson break;
249388ecc943SGeorge Wilson }
249488ecc943SGeorge Wilson
24952ba5f978SAlan Somers verify(zpool_vdev_is_interior(type));
249688ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
249788ecc943SGeorge Wilson &id) == 0);
249888ecc943SGeorge Wilson
249988ecc943SGeorge Wilson errno = 0;
250088ecc943SGeorge Wilson vdev_id = strtoull(idx, &end, 10);
250188ecc943SGeorge Wilson
250288ecc943SGeorge Wilson free(type);
250388ecc943SGeorge Wilson if (errno != 0)
250488ecc943SGeorge Wilson return (NULL);
250588ecc943SGeorge Wilson
250688ecc943SGeorge Wilson /*
250788ecc943SGeorge Wilson * Now verify that we have the correct vdev id.
250888ecc943SGeorge Wilson */
250988ecc943SGeorge Wilson if (vdev_id == id)
251088ecc943SGeorge Wilson return (nv);
2511ea8dc4b6Seschrock }
2512573ca77eSGeorge Wilson
2513573ca77eSGeorge Wilson /*
2514573ca77eSGeorge Wilson * Common case
2515573ca77eSGeorge Wilson */
2516573ca77eSGeorge Wilson if (strcmp(srchval, val) == 0)
2517573ca77eSGeorge Wilson return (nv);
2518573ca77eSGeorge Wilson break;
2519573ca77eSGeorge Wilson }
2520573ca77eSGeorge Wilson
2521573ca77eSGeorge Wilson default:
2522573ca77eSGeorge Wilson break;
2523ea8dc4b6Seschrock }
2524ea8dc4b6Seschrock
2525ea8dc4b6Seschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2526ea8dc4b6Seschrock &child, &children) != 0)
252799653d4eSeschrock return (NULL);
2528ea8dc4b6Seschrock
2529ee0eb9f2SEric Schrock for (c = 0; c < children; c++) {
2530573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search,
2531ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) {
2532ee0eb9f2SEric Schrock /*
2533ee0eb9f2SEric Schrock * The 'is_log' value is only set for the toplevel
2534ee0eb9f2SEric Schrock * vdev, not the leaf vdevs. So we always lookup the
2535ee0eb9f2SEric Schrock * log device from the root of the vdev tree (where
2536ee0eb9f2SEric Schrock * 'log' is non-NULL).
2537ee0eb9f2SEric Schrock */
2538ee0eb9f2SEric Schrock if (log != NULL &&
2539ee0eb9f2SEric Schrock nvlist_lookup_uint64(child[c],
2540ee0eb9f2SEric Schrock ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2541ee0eb9f2SEric Schrock is_log) {
2542ee0eb9f2SEric Schrock *log = B_TRUE;
2543ee0eb9f2SEric Schrock }
2544ea8dc4b6Seschrock return (ret);
2545ee0eb9f2SEric Schrock }
2546ee0eb9f2SEric Schrock }
2547ea8dc4b6Seschrock
254899653d4eSeschrock if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
254999653d4eSeschrock &child, &children) == 0) {
255099653d4eSeschrock for (c = 0; c < children; c++) {
2551573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search,
2552ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) {
2553a43d325bSek *avail_spare = B_TRUE;
255499653d4eSeschrock return (ret);
255599653d4eSeschrock }
255699653d4eSeschrock }
255799653d4eSeschrock }
255899653d4eSeschrock
2559fa94a07fSbrendan if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2560fa94a07fSbrendan &child, &children) == 0) {
2561fa94a07fSbrendan for (c = 0; c < children; c++) {
2562573ca77eSGeorge Wilson if ((ret = vdev_to_nvlist_iter(child[c], search,
2563ee0eb9f2SEric Schrock avail_spare, l2cache, NULL)) != NULL) {
2564fa94a07fSbrendan *l2cache = B_TRUE;
2565fa94a07fSbrendan return (ret);
2566fa94a07fSbrendan }
2567fa94a07fSbrendan }
2568fa94a07fSbrendan }
2569fa94a07fSbrendan
257099653d4eSeschrock return (NULL);
2571ea8dc4b6Seschrock }
2572ea8dc4b6Seschrock
2573573ca77eSGeorge Wilson /*
2574573ca77eSGeorge Wilson * Given a physical path (minus the "/devices" prefix), find the
2575573ca77eSGeorge Wilson * associated vdev.
2576573ca77eSGeorge Wilson */
2577573ca77eSGeorge Wilson nvlist_t *
zpool_find_vdev_by_physpath(zpool_handle_t * zhp,const char * ppath,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)2578573ca77eSGeorge Wilson zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2579573ca77eSGeorge Wilson boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2580573ca77eSGeorge Wilson {
2581573ca77eSGeorge Wilson nvlist_t *search, *nvroot, *ret;
2582573ca77eSGeorge Wilson
2583573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2584573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2585573ca77eSGeorge Wilson
2586573ca77eSGeorge Wilson verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2587573ca77eSGeorge Wilson &nvroot) == 0);
2588573ca77eSGeorge Wilson
2589573ca77eSGeorge Wilson *avail_spare = B_FALSE;
2590cb04b873SMark J Musante *l2cache = B_FALSE;
2591daeb70e5SMark J Musante if (log != NULL)
2592daeb70e5SMark J Musante *log = B_FALSE;
2593573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2594573ca77eSGeorge Wilson nvlist_free(search);
2595573ca77eSGeorge Wilson
2596573ca77eSGeorge Wilson return (ret);
2597573ca77eSGeorge Wilson }
2598573ca77eSGeorge Wilson
259988ecc943SGeorge Wilson /*
260088ecc943SGeorge Wilson * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
260188ecc943SGeorge Wilson */
26022ba5f978SAlan Somers static boolean_t
zpool_vdev_is_interior(const char * name)260388ecc943SGeorge Wilson zpool_vdev_is_interior(const char *name)
260488ecc943SGeorge Wilson {
260588ecc943SGeorge Wilson if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
26062ba5f978SAlan Somers strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
26072ba5f978SAlan Somers strncmp(name,
26082ba5f978SAlan Somers VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
260988ecc943SGeorge Wilson strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
261088ecc943SGeorge Wilson return (B_TRUE);
261188ecc943SGeorge Wilson return (B_FALSE);
261288ecc943SGeorge Wilson }
261388ecc943SGeorge Wilson
261499653d4eSeschrock nvlist_t *
zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)2615fa94a07fSbrendan zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2616ee0eb9f2SEric Schrock boolean_t *l2cache, boolean_t *log)
2617ea8dc4b6Seschrock {
2618ea8dc4b6Seschrock char buf[MAXPATHLEN];
2619ea8dc4b6Seschrock char *end;
2620573ca77eSGeorge Wilson nvlist_t *nvroot, *search, *ret;
2621ea8dc4b6Seschrock uint64_t guid;
2622ea8dc4b6Seschrock
2623573ca77eSGeorge Wilson verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2624573ca77eSGeorge Wilson
26250917b783Seschrock guid = strtoull(path, &end, 10);
2626ea8dc4b6Seschrock if (guid != 0 && *end == '\0') {
2627573ca77eSGeorge Wilson verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
262888ecc943SGeorge Wilson } else if (zpool_vdev_is_interior(path)) {
262988ecc943SGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2630ea8dc4b6Seschrock } else if (path[0] != '/') {
26316401734dSWill Andrews (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
26326401734dSWill Andrews path);
2633573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2634ea8dc4b6Seschrock } else {
2635573ca77eSGeorge Wilson verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2636ea8dc4b6Seschrock }
2637ea8dc4b6Seschrock
2638ea8dc4b6Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2639ea8dc4b6Seschrock &nvroot) == 0);
2640ea8dc4b6Seschrock
2641a43d325bSek *avail_spare = B_FALSE;
2642fa94a07fSbrendan *l2cache = B_FALSE;
2643ee0eb9f2SEric Schrock if (log != NULL)
2644ee0eb9f2SEric Schrock *log = B_FALSE;
2645573ca77eSGeorge Wilson ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2646573ca77eSGeorge Wilson nvlist_free(search);
2647573ca77eSGeorge Wilson
2648573ca77eSGeorge Wilson return (ret);
2649a43d325bSek }
2650a43d325bSek
265119397407SSherry Moore static int
vdev_is_online(nvlist_t * nv)2652e0f1c0afSOlaf Faaland vdev_is_online(nvlist_t *nv)
265319397407SSherry Moore {
265419397407SSherry Moore uint64_t ival;
265519397407SSherry Moore
265619397407SSherry Moore if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
265719397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
265819397407SSherry Moore nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
265919397407SSherry Moore return (0);
266019397407SSherry Moore
266119397407SSherry Moore return (1);
266219397407SSherry Moore }
266319397407SSherry Moore
266419397407SSherry Moore /*
266521ecdf64SLin Ling * Helper function for zpool_get_physpaths().
266619397407SSherry Moore */
2667753a6d45SSherry Moore static int
vdev_get_one_physpath(nvlist_t * config,char * physpath,size_t physpath_size,size_t * bytes_written)266821ecdf64SLin Ling vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2669753a6d45SSherry Moore size_t *bytes_written)
267019397407SSherry Moore {
2671753a6d45SSherry Moore size_t bytes_left, pos, rsz;
2672753a6d45SSherry Moore char *tmppath;
2673753a6d45SSherry Moore const char *format;
2674753a6d45SSherry Moore
2675753a6d45SSherry Moore if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2676753a6d45SSherry Moore &tmppath) != 0)
2677753a6d45SSherry Moore return (EZFS_NODEVICE);
2678753a6d45SSherry Moore
2679753a6d45SSherry Moore pos = *bytes_written;
2680753a6d45SSherry Moore bytes_left = physpath_size - pos;
2681753a6d45SSherry Moore format = (pos == 0) ? "%s" : " %s";
2682753a6d45SSherry Moore
2683753a6d45SSherry Moore rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2684753a6d45SSherry Moore *bytes_written += rsz;
2685753a6d45SSherry Moore
2686753a6d45SSherry Moore if (rsz >= bytes_left) {
2687753a6d45SSherry Moore /* if physpath was not copied properly, clear it */
2688753a6d45SSherry Moore if (bytes_left != 0) {
2689753a6d45SSherry Moore physpath[pos] = 0;
2690753a6d45SSherry Moore }
2691753a6d45SSherry Moore return (EZFS_NOSPC);
2692753a6d45SSherry Moore }
2693753a6d45SSherry Moore return (0);
2694753a6d45SSherry Moore }
2695753a6d45SSherry Moore
269621ecdf64SLin Ling static int
vdev_get_physpaths(nvlist_t * nv,char * physpath,size_t phypath_size,size_t * rsz,boolean_t is_spare)269721ecdf64SLin Ling vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
269821ecdf64SLin Ling size_t *rsz, boolean_t is_spare)
269921ecdf64SLin Ling {
270021ecdf64SLin Ling char *type;
270121ecdf64SLin Ling int ret;
270221ecdf64SLin Ling
270321ecdf64SLin Ling if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
270421ecdf64SLin Ling return (EZFS_INVALCONFIG);
270521ecdf64SLin Ling
270621ecdf64SLin Ling if (strcmp(type, VDEV_TYPE_DISK) == 0) {
270721ecdf64SLin Ling /*
270821ecdf64SLin Ling * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
270921ecdf64SLin Ling * For a spare vdev, we only want to boot from the active
271021ecdf64SLin Ling * spare device.
271121ecdf64SLin Ling */
271221ecdf64SLin Ling if (is_spare) {
271321ecdf64SLin Ling uint64_t spare = 0;
271421ecdf64SLin Ling (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
271521ecdf64SLin Ling &spare);
271621ecdf64SLin Ling if (!spare)
271721ecdf64SLin Ling return (EZFS_INVALCONFIG);
271821ecdf64SLin Ling }
271921ecdf64SLin Ling
2720e0f1c0afSOlaf Faaland if (vdev_is_online(nv)) {
272121ecdf64SLin Ling if ((ret = vdev_get_one_physpath(nv, physpath,
272221ecdf64SLin Ling phypath_size, rsz)) != 0)
272321ecdf64SLin Ling return (ret);
272421ecdf64SLin Ling }
272521ecdf64SLin Ling } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2726d5f26ad8SToomas Soome strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
272721ecdf64SLin Ling strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
272821ecdf64SLin Ling (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
272921ecdf64SLin Ling nvlist_t **child;
273021ecdf64SLin Ling uint_t count;
273121ecdf64SLin Ling int i, ret;
273221ecdf64SLin Ling
273321ecdf64SLin Ling if (nvlist_lookup_nvlist_array(nv,
273421ecdf64SLin Ling ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
273521ecdf64SLin Ling return (EZFS_INVALCONFIG);
273621ecdf64SLin Ling
273721ecdf64SLin Ling for (i = 0; i < count; i++) {
273821ecdf64SLin Ling ret = vdev_get_physpaths(child[i], physpath,
273921ecdf64SLin Ling phypath_size, rsz, is_spare);
274021ecdf64SLin Ling if (ret == EZFS_NOSPC)
274121ecdf64SLin Ling return (ret);
274221ecdf64SLin Ling }
274321ecdf64SLin Ling }
274421ecdf64SLin Ling
274521ecdf64SLin Ling return (EZFS_POOL_INVALARG);
274621ecdf64SLin Ling }
274721ecdf64SLin Ling
2748753a6d45SSherry Moore /*
2749753a6d45SSherry Moore * Get phys_path for a root pool config.
2750753a6d45SSherry Moore * Return 0 on success; non-zero on failure.
2751753a6d45SSherry Moore */
2752753a6d45SSherry Moore static int
zpool_get_config_physpath(nvlist_t * config,char * physpath,size_t phypath_size)2753753a6d45SSherry Moore zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2754753a6d45SSherry Moore {
2755753a6d45SSherry Moore size_t rsz;
275619397407SSherry Moore nvlist_t *vdev_root;
275719397407SSherry Moore nvlist_t **child;
275819397407SSherry Moore uint_t count;
2759753a6d45SSherry Moore char *type;
2760753a6d45SSherry Moore
2761753a6d45SSherry Moore rsz = 0;
2762753a6d45SSherry Moore
2763753a6d45SSherry Moore if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2764753a6d45SSherry Moore &vdev_root) != 0)
2765753a6d45SSherry Moore return (EZFS_INVALCONFIG);
2766753a6d45SSherry Moore
2767753a6d45SSherry Moore if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2768753a6d45SSherry Moore nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2769753a6d45SSherry Moore &child, &count) != 0)
2770753a6d45SSherry Moore return (EZFS_INVALCONFIG);
277119397407SSherry Moore
277219397407SSherry Moore /*
27731a902ef8SHans Rosenfeld * root pool can only have a single top-level vdev.
277419397407SSherry Moore */
27751a902ef8SHans Rosenfeld if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2776753a6d45SSherry Moore return (EZFS_POOL_INVALARG);
277719397407SSherry Moore
277821ecdf64SLin Ling (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
277921ecdf64SLin Ling B_FALSE);
278019397407SSherry Moore
2781753a6d45SSherry Moore /* No online devices */
2782753a6d45SSherry Moore if (rsz == 0)
2783753a6d45SSherry Moore return (EZFS_NODEVICE);
2784753a6d45SSherry Moore
278519397407SSherry Moore return (0);
278619397407SSherry Moore }
278719397407SSherry Moore
2788753a6d45SSherry Moore /*
2789753a6d45SSherry Moore * Get phys_path for a root pool
2790753a6d45SSherry Moore * Return 0 on success; non-zero on failure.
2791753a6d45SSherry Moore */
2792753a6d45SSherry Moore int
zpool_get_physpath(zpool_handle_t * zhp,char * physpath,size_t phypath_size)2793753a6d45SSherry Moore zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2794753a6d45SSherry Moore {
2795753a6d45SSherry Moore return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2796753a6d45SSherry Moore phypath_size));
2797753a6d45SSherry Moore }
2798753a6d45SSherry Moore
2799573ca77eSGeorge Wilson /*
2800573ca77eSGeorge Wilson * If the device has being dynamically expanded then we need to relabel
2801573ca77eSGeorge Wilson * the disk to use the new unallocated space.
2802573ca77eSGeorge Wilson */
2803573ca77eSGeorge Wilson static int
zpool_relabel_disk(libzfs_handle_t * hdl,const char * name,const char * msg)28045cf013a3SEtienne Dechamps zpool_relabel_disk(libzfs_handle_t *hdl, const char *name, const char *msg)
2805573ca77eSGeorge Wilson {
2806573ca77eSGeorge Wilson char path[MAXPATHLEN];
2807573ca77eSGeorge Wilson int fd, error;
2808573ca77eSGeorge Wilson int (*_efi_use_whole_disk)(int);
28097d6cab3fSJoshua M. Clulow char drv[MODMAXNAMELEN];
28107d6cab3fSJoshua M. Clulow major_t maj;
28117d6cab3fSJoshua M. Clulow struct stat st;
2812573ca77eSGeorge Wilson
2813573ca77eSGeorge Wilson if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2814573ca77eSGeorge Wilson "efi_use_whole_disk")) == NULL)
2815573ca77eSGeorge Wilson return (-1);
2816573ca77eSGeorge Wilson
28176401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2818573ca77eSGeorge Wilson
2819573ca77eSGeorge Wilson if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2820573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2821573ca77eSGeorge Wilson "relabel '%s': unable to open device"), name);
28225cf013a3SEtienne Dechamps return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2823573ca77eSGeorge Wilson }
2824573ca77eSGeorge Wilson
2825573ca77eSGeorge Wilson /*
2826573ca77eSGeorge Wilson * It's possible that we might encounter an error if the device
2827573ca77eSGeorge Wilson * does not have any unallocated space left. If so, we simply
2828573ca77eSGeorge Wilson * ignore that error and continue on.
2829573ca77eSGeorge Wilson */
2830573ca77eSGeorge Wilson error = _efi_use_whole_disk(fd);
2831573ca77eSGeorge Wilson if (error && error != VT_ENOSPC) {
2832c2cd3a44SAndy Fiddaman (void) close(fd);
2833573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2834573ca77eSGeorge Wilson "relabel '%s': unable to read disk capacity"), name);
28355cf013a3SEtienne Dechamps return (zfs_error(hdl, EZFS_NOCAP, msg));
2836573ca77eSGeorge Wilson }
2837c2cd3a44SAndy Fiddaman
2838c2cd3a44SAndy Fiddaman /*
2839c2cd3a44SAndy Fiddaman * Writing a new EFI partition table to the disk will have marked
2840c2cd3a44SAndy Fiddaman * the geometry as needing re-validation. Before returning, force
2841c2cd3a44SAndy Fiddaman * it to be checked by querying the device state, otherwise the
2842c2cd3a44SAndy Fiddaman * subsequent vdev_reopen() will very likely fail to read the device
2843c2cd3a44SAndy Fiddaman * size, faulting the pool.
28447d6cab3fSJoshua M. Clulow *
28457d6cab3fSJoshua M. Clulow * The dkio(4I) ioctls are implemented by the disk driver rather than
28467d6cab3fSJoshua M. Clulow * some generic framework, so we limit its use here to drivers with
28477d6cab3fSJoshua M. Clulow * which it has been tested.
2848c2cd3a44SAndy Fiddaman */
28497d6cab3fSJoshua M. Clulow if (fstat(fd, &st) == 0 &&
28507d6cab3fSJoshua M. Clulow (maj = major(st.st_rdev)) != (major_t)NODEV &&
28517d6cab3fSJoshua M. Clulow modctl(MODGETNAME, drv, sizeof (drv), &maj) == 0 &&
28527d6cab3fSJoshua M. Clulow (strcmp(drv, "blkdev") == 0 || strcmp(drv, "sd") == 0)) {
28537d6cab3fSJoshua M. Clulow enum dkio_state dkst = DKIO_NONE;
28547d6cab3fSJoshua M. Clulow (void) ioctl(fd, DKIOCSTATE, &dkst);
28557d6cab3fSJoshua M. Clulow }
2856c2cd3a44SAndy Fiddaman
2857c2cd3a44SAndy Fiddaman (void) close(fd);
2858c2cd3a44SAndy Fiddaman
2859573ca77eSGeorge Wilson return (0);
2860573ca77eSGeorge Wilson }
2861573ca77eSGeorge Wilson
2862fa9e4066Sahrens /*
28633d7072f8Seschrock * Bring the specified vdev online. The 'flags' parameter is a set of the
28643d7072f8Seschrock * ZFS_ONLINE_* flags.
2865fa9e4066Sahrens */
2866fa9e4066Sahrens int
zpool_vdev_online(zpool_handle_t * zhp,const char * path,int flags,vdev_state_t * newstate)28673d7072f8Seschrock zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
28683d7072f8Seschrock vdev_state_t *newstate)
2869fa9e4066Sahrens {
2870fa9e4066Sahrens zfs_cmd_t zc = { 0 };
2871fa9e4066Sahrens char msg[1024];
28729a551dd6SYuri Pankov char *pathname;
287399653d4eSeschrock nvlist_t *tgt;
2874573ca77eSGeorge Wilson boolean_t avail_spare, l2cache, islog;
287599653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
28765cf013a3SEtienne Dechamps int error;
2877fa9e4066Sahrens
2878573ca77eSGeorge Wilson if (flags & ZFS_ONLINE_EXPAND) {
2879573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg),
2880573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2881573ca77eSGeorge Wilson } else {
2882573ca77eSGeorge Wilson (void) snprintf(msg, sizeof (msg),
2883573ca77eSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2884573ca77eSGeorge Wilson }
2885ea8dc4b6Seschrock
2886fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2887ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2888573ca77eSGeorge Wilson &islog)) == NULL)
288999653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
2890fa9e4066Sahrens
289199653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2892fa9e4066Sahrens
2893069f55e2SEric Schrock if (avail_spare)
2894a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg));
2895a43d325bSek
28969a551dd6SYuri Pankov if ((flags & ZFS_ONLINE_EXPAND ||
28979a551dd6SYuri Pankov zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
28989a551dd6SYuri Pankov nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2899573ca77eSGeorge Wilson uint64_t wholedisk = 0;
2900573ca77eSGeorge Wilson
2901573ca77eSGeorge Wilson (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2902573ca77eSGeorge Wilson &wholedisk);
2903573ca77eSGeorge Wilson
2904573ca77eSGeorge Wilson /*
2905573ca77eSGeorge Wilson * XXX - L2ARC 1.0 devices can't support expansion.
2906573ca77eSGeorge Wilson */
2907573ca77eSGeorge Wilson if (l2cache) {
2908573ca77eSGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2909573ca77eSGeorge Wilson "cannot expand cache devices"));
2910573ca77eSGeorge Wilson return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2911573ca77eSGeorge Wilson }
2912573ca77eSGeorge Wilson
2913573ca77eSGeorge Wilson if (wholedisk) {
29146401734dSWill Andrews pathname += strlen(ZFS_DISK_ROOT) + 1;
29155cf013a3SEtienne Dechamps error = zpool_relabel_disk(hdl, pathname, msg);
29165cf013a3SEtienne Dechamps if (error != 0)
29175cf013a3SEtienne Dechamps return (error);
2918573ca77eSGeorge Wilson }
2919573ca77eSGeorge Wilson }
2920573ca77eSGeorge Wilson
29213d7072f8Seschrock zc.zc_cookie = VDEV_STATE_ONLINE;
29223d7072f8Seschrock zc.zc_obj = flags;
2923fa9e4066Sahrens
2924cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
29251195e687SMark J Musante if (errno == EINVAL) {
29261195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
29271195e687SMark J Musante "from this pool into a new one. Use '%s' "
29281195e687SMark J Musante "instead"), "zpool detach");
29291195e687SMark J Musante return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
29301195e687SMark J Musante }
29313d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg));
29321195e687SMark J Musante }
29333d7072f8Seschrock
29343d7072f8Seschrock *newstate = zc.zc_cookie;
29353d7072f8Seschrock return (0);
2936fa9e4066Sahrens }
2937fa9e4066Sahrens
2938fa9e4066Sahrens /*
2939fa9e4066Sahrens * Take the specified vdev offline
2940fa9e4066Sahrens */
2941fa9e4066Sahrens int
zpool_vdev_offline(zpool_handle_t * zhp,const char * path,boolean_t istmp)29423d7072f8Seschrock zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2943fa9e4066Sahrens {
2944fa9e4066Sahrens zfs_cmd_t zc = { 0 };
2945fa9e4066Sahrens char msg[1024];
294699653d4eSeschrock nvlist_t *tgt;
2947fa94a07fSbrendan boolean_t avail_spare, l2cache;
294899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
2949fa9e4066Sahrens
2950ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg),
2951ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2952ea8dc4b6Seschrock
2953fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2954ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2955ee0eb9f2SEric Schrock NULL)) == NULL)
295699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
295799653d4eSeschrock
295899653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2959fa9e4066Sahrens
2960069f55e2SEric Schrock if (avail_spare)
2961a43d325bSek return (zfs_error(hdl, EZFS_ISSPARE, msg));
2962a43d325bSek
29633d7072f8Seschrock zc.zc_cookie = VDEV_STATE_OFFLINE;
29643d7072f8Seschrock zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
29653d7072f8Seschrock
2966cb04b873SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
29673d7072f8Seschrock return (0);
29683d7072f8Seschrock
29693d7072f8Seschrock switch (errno) {
29703d7072f8Seschrock case EBUSY:
29713d7072f8Seschrock
29723d7072f8Seschrock /*
29733d7072f8Seschrock * There are no other replicas of this device.
29743d7072f8Seschrock */
29753d7072f8Seschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
29763d7072f8Seschrock
2977e6ca193dSGeorge Wilson case EEXIST:
2978e6ca193dSGeorge Wilson /*
2979e6ca193dSGeorge Wilson * The log device has unplayed logs
2980e6ca193dSGeorge Wilson */
2981e6ca193dSGeorge Wilson return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2982e6ca193dSGeorge Wilson
29833d7072f8Seschrock default:
29843d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg));
29853d7072f8Seschrock }
29863d7072f8Seschrock }
29873d7072f8Seschrock
29883d7072f8Seschrock /*
29893d7072f8Seschrock * Mark the given vdev faulted.
29903d7072f8Seschrock */
29913d7072f8Seschrock int
zpool_vdev_fault(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)2992069f55e2SEric Schrock zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
29933d7072f8Seschrock {
29943d7072f8Seschrock zfs_cmd_t zc = { 0 };
29953d7072f8Seschrock char msg[1024];
29963d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
29973d7072f8Seschrock
29983d7072f8Seschrock (void) snprintf(msg, sizeof (msg),
29993d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
3000441d80aaSlling
30013d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30023d7072f8Seschrock zc.zc_guid = guid;
30033d7072f8Seschrock zc.zc_cookie = VDEV_STATE_FAULTED;
3004069f55e2SEric Schrock zc.zc_obj = aux;
30053d7072f8Seschrock
3006cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3007fa9e4066Sahrens return (0);
3008fa9e4066Sahrens
3009fa9e4066Sahrens switch (errno) {
301099653d4eSeschrock case EBUSY:
3011fa9e4066Sahrens
3012fa9e4066Sahrens /*
3013fa9e4066Sahrens * There are no other replicas of this device.
3014fa9e4066Sahrens */
301599653d4eSeschrock return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3016fa9e4066Sahrens
301799653d4eSeschrock default:
301899653d4eSeschrock return (zpool_standard_error(hdl, errno, msg));
3019fa9e4066Sahrens }
30203d7072f8Seschrock
30213d7072f8Seschrock }
30223d7072f8Seschrock
30233d7072f8Seschrock /*
30243d7072f8Seschrock * Mark the given vdev degraded.
30253d7072f8Seschrock */
30263d7072f8Seschrock int
zpool_vdev_degrade(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3027069f55e2SEric Schrock zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
30283d7072f8Seschrock {
30293d7072f8Seschrock zfs_cmd_t zc = { 0 };
30303d7072f8Seschrock char msg[1024];
30313d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
30323d7072f8Seschrock
30333d7072f8Seschrock (void) snprintf(msg, sizeof (msg),
30343d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
30353d7072f8Seschrock
30363d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
30373d7072f8Seschrock zc.zc_guid = guid;
30383d7072f8Seschrock zc.zc_cookie = VDEV_STATE_DEGRADED;
3039069f55e2SEric Schrock zc.zc_obj = aux;
30403d7072f8Seschrock
3041cb04b873SMark J Musante if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
30423d7072f8Seschrock return (0);
30433d7072f8Seschrock
30443d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg));
304599653d4eSeschrock }
304699653d4eSeschrock
304799653d4eSeschrock /*
304899653d4eSeschrock * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
304999653d4eSeschrock * a hot spare.
305099653d4eSeschrock */
305199653d4eSeschrock static boolean_t
is_replacing_spare(nvlist_t * search,nvlist_t * tgt,int which)305299653d4eSeschrock is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
305399653d4eSeschrock {
305499653d4eSeschrock nvlist_t **child;
305599653d4eSeschrock uint_t c, children;
305699653d4eSeschrock char *type;
305799653d4eSeschrock
305899653d4eSeschrock if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
305999653d4eSeschrock &children) == 0) {
306099653d4eSeschrock verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
306199653d4eSeschrock &type) == 0);
306299653d4eSeschrock
306399653d4eSeschrock if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
306499653d4eSeschrock children == 2 && child[which] == tgt)
306599653d4eSeschrock return (B_TRUE);
306699653d4eSeschrock
306799653d4eSeschrock for (c = 0; c < children; c++)
306899653d4eSeschrock if (is_replacing_spare(child[c], tgt, which))
306999653d4eSeschrock return (B_TRUE);
307099653d4eSeschrock }
307199653d4eSeschrock
307299653d4eSeschrock return (B_FALSE);
3073fa9e4066Sahrens }
3074fa9e4066Sahrens
3075fa9e4066Sahrens /*
3076fa9e4066Sahrens * Attach new_disk (fully described by nvroot) to old_disk.
30778654d025Sperrin * If 'replacing' is specified, the new disk will replace the old one.
3078fa9e4066Sahrens */
3079fa9e4066Sahrens int
zpool_vdev_attach(zpool_handle_t * zhp,const char * old_disk,const char * new_disk,nvlist_t * nvroot,int replacing)3080fa9e4066Sahrens zpool_vdev_attach(zpool_handle_t *zhp,
3081fa9e4066Sahrens const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
3082fa9e4066Sahrens {
3083fa9e4066Sahrens zfs_cmd_t zc = { 0 };
3084fa9e4066Sahrens char msg[1024];
3085fa9e4066Sahrens int ret;
3086e830fb12SKody A Kantor nvlist_t *tgt, *newvd;
3087ee0eb9f2SEric Schrock boolean_t avail_spare, l2cache, islog;
3088ee0eb9f2SEric Schrock uint64_t val;
3089cb04b873SMark J Musante char *newname;
309099653d4eSeschrock nvlist_t **child;
309199653d4eSeschrock uint_t children;
309299653d4eSeschrock nvlist_t *config_root;
309399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
30944263d13fSGeorge Wilson boolean_t rootpool = zpool_is_bootable(zhp);
3095fa9e4066Sahrens
3096ea8dc4b6Seschrock if (replacing)
3097ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3098ea8dc4b6Seschrock "cannot replace %s with %s"), old_disk, new_disk);
3099ea8dc4b6Seschrock else
3100ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3101ea8dc4b6Seschrock "cannot attach %s to %s"), new_disk, old_disk);
3102ea8dc4b6Seschrock
3103fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3104ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
31055cabbc6bSPrashanth Sreenivasa &islog)) == NULL)
310699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
310799653d4eSeschrock
3108a43d325bSek if (avail_spare)
310999653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg));
311099653d4eSeschrock
3111fa94a07fSbrendan if (l2cache)
3112fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3113fa94a07fSbrendan
311499653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3115fa9e4066Sahrens zc.zc_cookie = replacing;
3116fa9e4066Sahrens
311799653d4eSeschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
311899653d4eSeschrock &child, &children) != 0 || children != 1) {
311999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
312099653d4eSeschrock "new device must be a single disk"));
312199653d4eSeschrock return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
312299653d4eSeschrock }
312399653d4eSeschrock
312499653d4eSeschrock verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
312599653d4eSeschrock ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
312699653d4eSeschrock
3127663207adSDon Brady if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
31280430f8daSeschrock return (-1);
31290430f8daSeschrock
3130e830fb12SKody A Kantor newvd = zpool_find_vdev(zhp, newname, &avail_spare, &l2cache, NULL);
313199653d4eSeschrock /*
313299653d4eSeschrock * If the target is a hot spare that has been swapped in, we can only
313399653d4eSeschrock * replace it with another hot spare.
313499653d4eSeschrock */
313599653d4eSeschrock if (replacing &&
313699653d4eSeschrock nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3137e830fb12SKody A Kantor (newvd == NULL || !avail_spare) &&
3138ee0eb9f2SEric Schrock is_replacing_spare(config_root, tgt, 1)) {
313999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
314099653d4eSeschrock "can only be replaced by another hot spare"));
31410430f8daSeschrock free(newname);
314299653d4eSeschrock return (zfs_error(hdl, EZFS_BADTARGET, msg));
314399653d4eSeschrock }
314499653d4eSeschrock
31450430f8daSeschrock free(newname);
31460430f8daSeschrock
3147e830fb12SKody A Kantor if (replacing && avail_spare && !vdev_is_online(newvd)) {
3148e830fb12SKody A Kantor (void) zpool_standard_error(hdl, ENXIO, msg);
3149e830fb12SKody A Kantor return (-1);
3150e830fb12SKody A Kantor }
3151e830fb12SKody A Kantor
3152990b4856Slling if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
315399653d4eSeschrock return (-1);
3154fa9e4066Sahrens
3155cb04b873SMark J Musante ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3156fa9e4066Sahrens
3157e9dbad6fSeschrock zcmd_free_nvlists(&zc);
3158fa9e4066Sahrens
3159b5b76fecSGeorge Wilson if (ret == 0) {
3160b5b76fecSGeorge Wilson if (rootpool) {
316121ecdf64SLin Ling /*
316221ecdf64SLin Ling * XXX need a better way to prevent user from
316321ecdf64SLin Ling * booting up a half-baked vdev.
316421ecdf64SLin Ling */
316521ecdf64SLin Ling (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
316621ecdf64SLin Ling "sure to wait until resilver is done "
316721ecdf64SLin Ling "before rebooting.\n"));
3168b5b76fecSGeorge Wilson }
3169fa9e4066Sahrens return (0);
3170b5b76fecSGeorge Wilson }
3171fa9e4066Sahrens
3172fa9e4066Sahrens switch (errno) {
3173ea8dc4b6Seschrock case ENOTSUP:
3174fa9e4066Sahrens /*
3175fa9e4066Sahrens * Can't attach to or replace this type of vdev.
3176fa9e4066Sahrens */
31778654d025Sperrin if (replacing) {
3178cb04b873SMark J Musante uint64_t version = zpool_get_prop_int(zhp,
3179cb04b873SMark J Musante ZPOOL_PROP_VERSION, NULL);
3180cb04b873SMark J Musante
3181ee0eb9f2SEric Schrock if (islog)
31828654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31838654d025Sperrin "cannot replace a log with a spare"));
3184cb04b873SMark J Musante else if (version >= SPA_VERSION_MULTI_REPLACE)
3185cb04b873SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3186cb04b873SMark J Musante "already in replacing/spare config; wait "
3187cb04b873SMark J Musante "for completion or use 'zpool detach'"));
31888654d025Sperrin else
31898654d025Sperrin zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31908654d025Sperrin "cannot replace a replacing device"));
31918654d025Sperrin } else {
319299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
319399653d4eSeschrock "can only attach to mirrors and top-level "
319499653d4eSeschrock "disks"));
31958654d025Sperrin }
319699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3197fa9e4066Sahrens break;
3198fa9e4066Sahrens
3199ea8dc4b6Seschrock case EINVAL:
3200fa9e4066Sahrens /*
3201fa9e4066Sahrens * The new device must be a single disk.
3202fa9e4066Sahrens */
320399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
320499653d4eSeschrock "new device must be a single disk"));
320599653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3206fa9e4066Sahrens break;
3207fa9e4066Sahrens
3208ea8dc4b6Seschrock case EBUSY:
32095cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
32103a4b1be9SMatthew Ahrens "or device removal is in progress"),
321199653d4eSeschrock new_disk);
321299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
3213fa9e4066Sahrens break;
3214fa9e4066Sahrens
3215ea8dc4b6Seschrock case EOVERFLOW:
3216fa9e4066Sahrens /*
3217fa9e4066Sahrens * The new device is too small.
3218fa9e4066Sahrens */
321999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
322099653d4eSeschrock "device is too small"));
322199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
3222fa9e4066Sahrens break;
3223fa9e4066Sahrens
3224ea8dc4b6Seschrock case EDOM:
3225fa9e4066Sahrens /*
32265711d393Sloli * The new device has a different optimal sector size.
3227fa9e4066Sahrens */
322899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
32295711d393Sloli "new device has a different optimal sector size; use the "
32305711d393Sloli "option '-o ashift=N' to override the optimal size"));
323199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADDEV, msg);
3232fa9e4066Sahrens break;
3233fa9e4066Sahrens
3234ea8dc4b6Seschrock case ENAMETOOLONG:
3235fa9e4066Sahrens /*
3236fa9e4066Sahrens * The resulting top-level vdev spec won't fit in the label.
3237fa9e4066Sahrens */
323899653d4eSeschrock (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3239fa9e4066Sahrens break;
3240fa9e4066Sahrens
3241ea8dc4b6Seschrock default:
324299653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg);
3243fa9e4066Sahrens }
3244fa9e4066Sahrens
324599653d4eSeschrock return (-1);
3246fa9e4066Sahrens }
3247fa9e4066Sahrens
3248fa9e4066Sahrens /*
3249fa9e4066Sahrens * Detach the specified device.
3250fa9e4066Sahrens */
3251fa9e4066Sahrens int
zpool_vdev_detach(zpool_handle_t * zhp,const char * path)3252fa9e4066Sahrens zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3253fa9e4066Sahrens {
3254fa9e4066Sahrens zfs_cmd_t zc = { 0 };
3255fa9e4066Sahrens char msg[1024];
325699653d4eSeschrock nvlist_t *tgt;
3257fa94a07fSbrendan boolean_t avail_spare, l2cache;
325899653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
3259fa9e4066Sahrens
3260ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg),
3261ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3262ea8dc4b6Seschrock
3263fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3264ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
32655cabbc6bSPrashanth Sreenivasa NULL)) == NULL)
326699653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
3267fa9e4066Sahrens
3268a43d325bSek if (avail_spare)
326999653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg));
327099653d4eSeschrock
3271fa94a07fSbrendan if (l2cache)
3272fa94a07fSbrendan return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3273fa94a07fSbrendan
327499653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
327599653d4eSeschrock
3276ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3277fa9e4066Sahrens return (0);
3278fa9e4066Sahrens
3279fa9e4066Sahrens switch (errno) {
3280fa9e4066Sahrens
3281ea8dc4b6Seschrock case ENOTSUP:
3282fa9e4066Sahrens /*
3283fa9e4066Sahrens * Can't detach from this type of vdev.
3284fa9e4066Sahrens */
328599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
328699653d4eSeschrock "applicable to mirror and replacing vdevs"));
3287cb04b873SMark J Musante (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3288fa9e4066Sahrens break;
3289fa9e4066Sahrens
3290ea8dc4b6Seschrock case EBUSY:
3291fa9e4066Sahrens /*
3292fa9e4066Sahrens * There are no other replicas of this device.
3293fa9e4066Sahrens */
329499653d4eSeschrock (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3295fa9e4066Sahrens break;
3296fa9e4066Sahrens
3297ea8dc4b6Seschrock default:
329899653d4eSeschrock (void) zpool_standard_error(hdl, errno, msg);
3299ea8dc4b6Seschrock }
3300ea8dc4b6Seschrock
330199653d4eSeschrock return (-1);
330299653d4eSeschrock }
330399653d4eSeschrock
33041195e687SMark J Musante /*
33051195e687SMark J Musante * Find a mirror vdev in the source nvlist.
33061195e687SMark J Musante *
33071195e687SMark J Musante * The mchild array contains a list of disks in one of the top-level mirrors
33081195e687SMark J Musante * of the source pool. The schild array contains a list of disks that the
33091195e687SMark J Musante * user specified on the command line. We loop over the mchild array to
33101195e687SMark J Musante * see if any entry in the schild array matches.
33111195e687SMark J Musante *
33121195e687SMark J Musante * If a disk in the mchild array is found in the schild array, we return
33131195e687SMark J Musante * the index of that entry. Otherwise we return -1.
33141195e687SMark J Musante */
33151195e687SMark J Musante static int
find_vdev_entry(zpool_handle_t * zhp,nvlist_t ** mchild,uint_t mchildren,nvlist_t ** schild,uint_t schildren)33161195e687SMark J Musante find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
33171195e687SMark J Musante nvlist_t **schild, uint_t schildren)
33181195e687SMark J Musante {
33191195e687SMark J Musante uint_t mc;
33201195e687SMark J Musante
33211195e687SMark J Musante for (mc = 0; mc < mchildren; mc++) {
33221195e687SMark J Musante uint_t sc;
33231195e687SMark J Musante char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3324663207adSDon Brady mchild[mc], 0);
33251195e687SMark J Musante
33261195e687SMark J Musante for (sc = 0; sc < schildren; sc++) {
33271195e687SMark J Musante char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3328663207adSDon Brady schild[sc], 0);
33291195e687SMark J Musante boolean_t result = (strcmp(mpath, spath) == 0);
33301195e687SMark J Musante
33311195e687SMark J Musante free(spath);
33321195e687SMark J Musante if (result) {
33331195e687SMark J Musante free(mpath);
33341195e687SMark J Musante return (mc);
33351195e687SMark J Musante }
33361195e687SMark J Musante }
33371195e687SMark J Musante
33381195e687SMark J Musante free(mpath);
33391195e687SMark J Musante }
33401195e687SMark J Musante
33411195e687SMark J Musante return (-1);
33421195e687SMark J Musante }
33431195e687SMark J Musante
33441195e687SMark J Musante /*
33451195e687SMark J Musante * Split a mirror pool. If newroot points to null, then a new nvlist
33461195e687SMark J Musante * is generated and it is the responsibility of the caller to free it.
33471195e687SMark J Musante */
33481195e687SMark J Musante int
zpool_vdev_split(zpool_handle_t * zhp,char * newname,nvlist_t ** newroot,nvlist_t * props,splitflags_t flags)33491195e687SMark J Musante zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
33501195e687SMark J Musante nvlist_t *props, splitflags_t flags)
33511195e687SMark J Musante {
33521195e687SMark J Musante zfs_cmd_t zc = { 0 };
33531195e687SMark J Musante char msg[1024];
33541195e687SMark J Musante nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
33551195e687SMark J Musante nvlist_t **varray = NULL, *zc_props = NULL;
33561195e687SMark J Musante uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
33571195e687SMark J Musante libzfs_handle_t *hdl = zhp->zpool_hdl;
33581195e687SMark J Musante uint64_t vers;
33591195e687SMark J Musante boolean_t freelist = B_FALSE, memory_err = B_TRUE;
33601195e687SMark J Musante int retval = 0;
33611195e687SMark J Musante
33621195e687SMark J Musante (void) snprintf(msg, sizeof (msg),
33631195e687SMark J Musante dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
33641195e687SMark J Musante
33651195e687SMark J Musante if (!zpool_name_valid(hdl, B_FALSE, newname))
33661195e687SMark J Musante return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
33671195e687SMark J Musante
33681195e687SMark J Musante if ((config = zpool_get_config(zhp, NULL)) == NULL) {
33691195e687SMark J Musante (void) fprintf(stderr, gettext("Internal error: unable to "
33701195e687SMark J Musante "retrieve pool configuration\n"));
33711195e687SMark J Musante return (-1);
33721195e687SMark J Musante }
33731195e687SMark J Musante
33741195e687SMark J Musante verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
33751195e687SMark J Musante == 0);
33761195e687SMark J Musante verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
33771195e687SMark J Musante
33781195e687SMark J Musante if (props) {
3379f9af39baSGeorge Wilson prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
33801195e687SMark J Musante if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3381f9af39baSGeorge Wilson props, vers, flags, msg)) == NULL)
33821195e687SMark J Musante return (-1);
33831195e687SMark J Musante }
33841195e687SMark J Musante
33851195e687SMark J Musante if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
33861195e687SMark J Musante &children) != 0) {
33871195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33881195e687SMark J Musante "Source pool is missing vdev tree"));
3389aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props);
33901195e687SMark J Musante return (-1);
33911195e687SMark J Musante }
33921195e687SMark J Musante
33931195e687SMark J Musante varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
33941195e687SMark J Musante vcount = 0;
33951195e687SMark J Musante
33961195e687SMark J Musante if (*newroot == NULL ||
33971195e687SMark J Musante nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
33981195e687SMark J Musante &newchild, &newchildren) != 0)
33991195e687SMark J Musante newchildren = 0;
34001195e687SMark J Musante
34011195e687SMark J Musante for (c = 0; c < children; c++) {
34021195e687SMark J Musante uint64_t is_log = B_FALSE, is_hole = B_FALSE;
34031195e687SMark J Musante char *type;
34041195e687SMark J Musante nvlist_t **mchild, *vdev;
34051195e687SMark J Musante uint_t mchildren;
34061195e687SMark J Musante int entry;
34071195e687SMark J Musante
34081195e687SMark J Musante /*
34091195e687SMark J Musante * Unlike cache & spares, slogs are stored in the
34101195e687SMark J Musante * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
34111195e687SMark J Musante */
34121195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
34131195e687SMark J Musante &is_log);
34141195e687SMark J Musante (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
34151195e687SMark J Musante &is_hole);
34161195e687SMark J Musante if (is_log || is_hole) {
34171195e687SMark J Musante /*
34181195e687SMark J Musante * Create a hole vdev and put it in the config.
34191195e687SMark J Musante */
34201195e687SMark J Musante if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
34211195e687SMark J Musante goto out;
34221195e687SMark J Musante if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
34231195e687SMark J Musante VDEV_TYPE_HOLE) != 0)
34241195e687SMark J Musante goto out;
34251195e687SMark J Musante if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
34261195e687SMark J Musante 1) != 0)
34271195e687SMark J Musante goto out;
34281195e687SMark J Musante if (lastlog == 0)
34291195e687SMark J Musante lastlog = vcount;
34301195e687SMark J Musante varray[vcount++] = vdev;
34311195e687SMark J Musante continue;
34321195e687SMark J Musante }
34331195e687SMark J Musante lastlog = 0;
34341195e687SMark J Musante verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
34351195e687SMark J Musante == 0);
34361195e687SMark J Musante if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
34371195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34381195e687SMark J Musante "Source pool must be composed only of mirrors\n"));
34391195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
34401195e687SMark J Musante goto out;
34411195e687SMark J Musante }
34421195e687SMark J Musante
34431195e687SMark J Musante verify(nvlist_lookup_nvlist_array(child[c],
34441195e687SMark J Musante ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
34451195e687SMark J Musante
34461195e687SMark J Musante /* find or add an entry for this top-level vdev */
34471195e687SMark J Musante if (newchildren > 0 &&
34481195e687SMark J Musante (entry = find_vdev_entry(zhp, mchild, mchildren,
34491195e687SMark J Musante newchild, newchildren)) >= 0) {
34501195e687SMark J Musante /* We found a disk that the user specified. */
34511195e687SMark J Musante vdev = mchild[entry];
34521195e687SMark J Musante ++found;
34531195e687SMark J Musante } else {
34541195e687SMark J Musante /* User didn't specify a disk for this vdev. */
34551195e687SMark J Musante vdev = mchild[mchildren - 1];
34561195e687SMark J Musante }
34571195e687SMark J Musante
34581195e687SMark J Musante if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
34591195e687SMark J Musante goto out;
34601195e687SMark J Musante }
34611195e687SMark J Musante
34621195e687SMark J Musante /* did we find every disk the user specified? */
34631195e687SMark J Musante if (found != newchildren) {
34641195e687SMark J Musante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
34651195e687SMark J Musante "include at most one disk from each mirror"));
34661195e687SMark J Musante retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
34671195e687SMark J Musante goto out;
34681195e687SMark J Musante }
34691195e687SMark J Musante
34701195e687SMark J Musante /* Prepare the nvlist for populating. */
34711195e687SMark J Musante if (*newroot == NULL) {
34721195e687SMark J Musante if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
34731195e687SMark J Musante goto out;
34741195e687SMark J Musante freelist = B_TRUE;
34751195e687SMark J Musante if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
34761195e687SMark J Musante VDEV_TYPE_ROOT) != 0)
34771195e687SMark J Musante goto out;
34781195e687SMark J Musante } else {
34791195e687SMark J Musante verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
34801195e687SMark J Musante }
34811195e687SMark J Musante
34821195e687SMark J Musante /* Add all the children we found */
34831195e687SMark J Musante if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
34841195e687SMark J Musante lastlog == 0 ? vcount : lastlog) != 0)
34851195e687SMark J Musante goto out;
34861195e687SMark J Musante
34871195e687SMark J Musante /*
34881195e687SMark J Musante * If we're just doing a dry run, exit now with success.
34891195e687SMark J Musante */
34901195e687SMark J Musante if (flags.dryrun) {
34911195e687SMark J Musante memory_err = B_FALSE;
34921195e687SMark J Musante freelist = B_FALSE;
34931195e687SMark J Musante goto out;
34941195e687SMark J Musante }
34951195e687SMark J Musante
34961195e687SMark J Musante /* now build up the config list & call the ioctl */
34971195e687SMark J Musante if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
34981195e687SMark J Musante goto out;
34991195e687SMark J Musante
35001195e687SMark J Musante if (nvlist_add_nvlist(newconfig,
35011195e687SMark J Musante ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
35021195e687SMark J Musante nvlist_add_string(newconfig,
35031195e687SMark J Musante ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
35041195e687SMark J Musante nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
35051195e687SMark J Musante goto out;
35061195e687SMark J Musante
35071195e687SMark J Musante /*
35081195e687SMark J Musante * The new pool is automatically part of the namespace unless we
35091195e687SMark J Musante * explicitly export it.
35101195e687SMark J Musante */
35111195e687SMark J Musante if (!flags.import)
35121195e687SMark J Musante zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
35131195e687SMark J Musante (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
35141195e687SMark J Musante (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
35151195e687SMark J Musante if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
35161195e687SMark J Musante goto out;
35171195e687SMark J Musante if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
35181195e687SMark J Musante goto out;
35191195e687SMark J Musante
35201195e687SMark J Musante if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
35211195e687SMark J Musante retval = zpool_standard_error(hdl, errno, msg);
35221195e687SMark J Musante goto out;
35231195e687SMark J Musante }
35241195e687SMark J Musante
35251195e687SMark J Musante freelist = B_FALSE;
35261195e687SMark J Musante memory_err = B_FALSE;
35271195e687SMark J Musante
35281195e687SMark J Musante out:
35291195e687SMark J Musante if (varray != NULL) {
35301195e687SMark J Musante int v;
35311195e687SMark J Musante
35321195e687SMark J Musante for (v = 0; v < vcount; v++)
35331195e687SMark J Musante nvlist_free(varray[v]);
35341195e687SMark J Musante free(varray);
35351195e687SMark J Musante }
35361195e687SMark J Musante zcmd_free_nvlists(&zc);
3537aab83bb8SJosef 'Jeff' Sipek nvlist_free(zc_props);
3538aab83bb8SJosef 'Jeff' Sipek nvlist_free(newconfig);
35391195e687SMark J Musante if (freelist) {
35401195e687SMark J Musante nvlist_free(*newroot);
35411195e687SMark J Musante *newroot = NULL;
35421195e687SMark J Musante }
35431195e687SMark J Musante
35441195e687SMark J Musante if (retval != 0)
35451195e687SMark J Musante return (retval);
35461195e687SMark J Musante
35471195e687SMark J Musante if (memory_err)
35481195e687SMark J Musante return (no_memory(hdl));
35491195e687SMark J Musante
35501195e687SMark J Musante return (0);
35511195e687SMark J Musante }
35521195e687SMark J Musante
355399653d4eSeschrock /*
35545cabbc6bSPrashanth Sreenivasa * Remove the given device.
355599653d4eSeschrock */
355699653d4eSeschrock int
zpool_vdev_remove(zpool_handle_t * zhp,const char * path)355799653d4eSeschrock zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
355899653d4eSeschrock {
355999653d4eSeschrock zfs_cmd_t zc = { 0 };
356099653d4eSeschrock char msg[1024];
356199653d4eSeschrock nvlist_t *tgt;
356288ecc943SGeorge Wilson boolean_t avail_spare, l2cache, islog;
356399653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
356488ecc943SGeorge Wilson uint64_t version;
356599653d4eSeschrock
356699653d4eSeschrock (void) snprintf(msg, sizeof (msg),
356799653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
356899653d4eSeschrock
356999653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3570ee0eb9f2SEric Schrock if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
35715cabbc6bSPrashanth Sreenivasa &islog)) == NULL)
357299653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
357399653d4eSeschrock
357488ecc943SGeorge Wilson version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
357588ecc943SGeorge Wilson if (islog && version < SPA_VERSION_HOLES) {
357688ecc943SGeorge Wilson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35775cabbc6bSPrashanth Sreenivasa "pool must be upgraded to support log removal"));
357888ecc943SGeorge Wilson return (zfs_error(hdl, EZFS_BADVERSION, msg));
357988ecc943SGeorge Wilson }
358088ecc943SGeorge Wilson
35815cabbc6bSPrashanth Sreenivasa zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
35825cabbc6bSPrashanth Sreenivasa
35835cabbc6bSPrashanth Sreenivasa if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
35845cabbc6bSPrashanth Sreenivasa return (0);
35855cabbc6bSPrashanth Sreenivasa
35865cabbc6bSPrashanth Sreenivasa switch (errno) {
35875cabbc6bSPrashanth Sreenivasa
35885cabbc6bSPrashanth Sreenivasa case EINVAL:
35895cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35905cabbc6bSPrashanth Sreenivasa "invalid config; all top-level vdevs must "
35915cabbc6bSPrashanth Sreenivasa "have the same sector size and not be raidz."));
35925cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
35935cabbc6bSPrashanth Sreenivasa break;
35945cabbc6bSPrashanth Sreenivasa
35955cabbc6bSPrashanth Sreenivasa case EBUSY:
35965cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35975cabbc6bSPrashanth Sreenivasa "Pool busy; removal may already be in progress"));
35985cabbc6bSPrashanth Sreenivasa (void) zfs_error(hdl, EZFS_BUSY, msg);
35995cabbc6bSPrashanth Sreenivasa break;
36005cabbc6bSPrashanth Sreenivasa
36015cabbc6bSPrashanth Sreenivasa default:
36025cabbc6bSPrashanth Sreenivasa (void) zpool_standard_error(hdl, errno, msg);
36035cabbc6bSPrashanth Sreenivasa }
36045cabbc6bSPrashanth Sreenivasa return (-1);
36055cabbc6bSPrashanth Sreenivasa }
36065cabbc6bSPrashanth Sreenivasa
36075cabbc6bSPrashanth Sreenivasa int
zpool_vdev_remove_cancel(zpool_handle_t * zhp)36085cabbc6bSPrashanth Sreenivasa zpool_vdev_remove_cancel(zpool_handle_t *zhp)
36095cabbc6bSPrashanth Sreenivasa {
36105cabbc6bSPrashanth Sreenivasa zfs_cmd_t zc = { 0 };
36115cabbc6bSPrashanth Sreenivasa char msg[1024];
36125cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl;
36135cabbc6bSPrashanth Sreenivasa
36145cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg),
36155cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot cancel removal"));
36165cabbc6bSPrashanth Sreenivasa
36175cabbc6bSPrashanth Sreenivasa (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
36185cabbc6bSPrashanth Sreenivasa zc.zc_cookie = 1;
361999653d4eSeschrock
3620ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
362199653d4eSeschrock return (0);
362299653d4eSeschrock
362399653d4eSeschrock return (zpool_standard_error(hdl, errno, msg));
3624ea8dc4b6Seschrock }
3625ea8dc4b6Seschrock
36265cabbc6bSPrashanth Sreenivasa int
zpool_vdev_indirect_size(zpool_handle_t * zhp,const char * path,uint64_t * sizep)36275cabbc6bSPrashanth Sreenivasa zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
36285cabbc6bSPrashanth Sreenivasa uint64_t *sizep)
36295cabbc6bSPrashanth Sreenivasa {
36305cabbc6bSPrashanth Sreenivasa char msg[1024];
36315cabbc6bSPrashanth Sreenivasa nvlist_t *tgt;
36325cabbc6bSPrashanth Sreenivasa boolean_t avail_spare, l2cache, islog;
36335cabbc6bSPrashanth Sreenivasa libzfs_handle_t *hdl = zhp->zpool_hdl;
36345cabbc6bSPrashanth Sreenivasa
36355cabbc6bSPrashanth Sreenivasa (void) snprintf(msg, sizeof (msg),
36365cabbc6bSPrashanth Sreenivasa dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
36375cabbc6bSPrashanth Sreenivasa path);
36385cabbc6bSPrashanth Sreenivasa
36395cabbc6bSPrashanth Sreenivasa if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
36405cabbc6bSPrashanth Sreenivasa &islog)) == NULL)
36415cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EZFS_NODEVICE, msg));
36425cabbc6bSPrashanth Sreenivasa
36435cabbc6bSPrashanth Sreenivasa if (avail_spare || l2cache || islog) {
36445cabbc6bSPrashanth Sreenivasa *sizep = 0;
36455cabbc6bSPrashanth Sreenivasa return (0);
36465cabbc6bSPrashanth Sreenivasa }
36475cabbc6bSPrashanth Sreenivasa
36485cabbc6bSPrashanth Sreenivasa if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
36495cabbc6bSPrashanth Sreenivasa zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
36505cabbc6bSPrashanth Sreenivasa "indirect size not available"));
36515cabbc6bSPrashanth Sreenivasa return (zfs_error(hdl, EINVAL, msg));
36525cabbc6bSPrashanth Sreenivasa }
36535cabbc6bSPrashanth Sreenivasa return (0);
36545cabbc6bSPrashanth Sreenivasa }
36555cabbc6bSPrashanth Sreenivasa
3656ea8dc4b6Seschrock /*
3657ea8dc4b6Seschrock * Clear the errors for the pool, or the particular device if specified.
3658ea8dc4b6Seschrock */
3659ea8dc4b6Seschrock int
zpool_clear(zpool_handle_t * zhp,const char * path,nvlist_t * rewindnvl)3660468c413aSTim Haley zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3661ea8dc4b6Seschrock {
3662ea8dc4b6Seschrock zfs_cmd_t zc = { 0 };
3663ea8dc4b6Seschrock char msg[1024];
366499653d4eSeschrock nvlist_t *tgt;
36655dafeea3SPavel Zakharov zpool_load_policy_t policy;
3666fa94a07fSbrendan boolean_t avail_spare, l2cache;
366799653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
3668468c413aSTim Haley nvlist_t *nvi = NULL;
36694b964adaSGeorge Wilson int error;
3670ea8dc4b6Seschrock
3671ea8dc4b6Seschrock if (path)
3672ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg),
3673ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3674e9dbad6fSeschrock path);
3675ea8dc4b6Seschrock else
3676ea8dc4b6Seschrock (void) snprintf(msg, sizeof (msg),
3677ea8dc4b6Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3678ea8dc4b6Seschrock zhp->zpool_name);
3679ea8dc4b6Seschrock
3680ea8dc4b6Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
368199653d4eSeschrock if (path) {
3682fa94a07fSbrendan if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
36835cabbc6bSPrashanth Sreenivasa &l2cache, NULL)) == NULL)
368499653d4eSeschrock return (zfs_error(hdl, EZFS_NODEVICE, msg));
3685ea8dc4b6Seschrock
3686fa94a07fSbrendan /*
3687fa94a07fSbrendan * Don't allow error clearing for hot spares. Do allow
3688fa94a07fSbrendan * error clearing for l2cache devices.
3689fa94a07fSbrendan */
3690a43d325bSek if (avail_spare)
369199653d4eSeschrock return (zfs_error(hdl, EZFS_ISSPARE, msg));
3692ea8dc4b6Seschrock
369399653d4eSeschrock verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
369499653d4eSeschrock &zc.zc_guid) == 0);
3695fa9e4066Sahrens }
3696fa9e4066Sahrens
36975dafeea3SPavel Zakharov zpool_get_load_policy(rewindnvl, &policy);
36985dafeea3SPavel Zakharov zc.zc_cookie = policy.zlp_rewind;
3699468c413aSTim Haley
370057f304caSGeorge Wilson if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3701468c413aSTim Haley return (-1);
3702468c413aSTim Haley
3703cb04b873SMark J Musante if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3704468c413aSTim Haley return (-1);
3705468c413aSTim Haley
37064b964adaSGeorge Wilson while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
37074b964adaSGeorge Wilson errno == ENOMEM) {
37084b964adaSGeorge Wilson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
37094b964adaSGeorge Wilson zcmd_free_nvlists(&zc);
37104b964adaSGeorge Wilson return (-1);
37114b964adaSGeorge Wilson }
37124b964adaSGeorge Wilson }
37134b964adaSGeorge Wilson
37145dafeea3SPavel Zakharov if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3715468c413aSTim Haley errno != EPERM && errno != EACCES)) {
37165dafeea3SPavel Zakharov if (policy.zlp_rewind &
3717468c413aSTim Haley (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3718468c413aSTim Haley (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3719468c413aSTim Haley zpool_rewind_exclaim(hdl, zc.zc_name,
37205dafeea3SPavel Zakharov ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3721468c413aSTim Haley nvi);
3722468c413aSTim Haley nvlist_free(nvi);
3723468c413aSTim Haley }
3724468c413aSTim Haley zcmd_free_nvlists(&zc);
372599653d4eSeschrock return (0);
3726468c413aSTim Haley }
372799653d4eSeschrock
3728468c413aSTim Haley zcmd_free_nvlists(&zc);
372999653d4eSeschrock return (zpool_standard_error(hdl, errno, msg));
3730fa9e4066Sahrens }
3731fa9e4066Sahrens
37323d7072f8Seschrock /*
37333d7072f8Seschrock * Similar to zpool_clear(), but takes a GUID (used by fmd).
37343d7072f8Seschrock */
37353d7072f8Seschrock int
zpool_vdev_clear(zpool_handle_t * zhp,uint64_t guid)37363d7072f8Seschrock zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
37373d7072f8Seschrock {
37383d7072f8Seschrock zfs_cmd_t zc = { 0 };
37393d7072f8Seschrock char msg[1024];
37403d7072f8Seschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
37413d7072f8Seschrock
37423d7072f8Seschrock (void) snprintf(msg, sizeof (msg),
37433d7072f8Seschrock dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
37443d7072f8Seschrock guid);
37453d7072f8Seschrock
37463d7072f8Seschrock (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
37473d7072f8Seschrock zc.zc_guid = guid;
374814f8ce41SVictor Latushkin zc.zc_cookie = ZPOOL_NO_REWIND;
37493d7072f8Seschrock
37503d7072f8Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
37513d7072f8Seschrock return (0);
37523d7072f8Seschrock
37533d7072f8Seschrock return (zpool_standard_error(hdl, errno, msg));
37543d7072f8Seschrock }
37553d7072f8Seschrock
3756e9103aaeSGarrett D'Amore /*
3757e9103aaeSGarrett D'Amore * Change the GUID for a pool.
3758e9103aaeSGarrett D'Amore */
3759e9103aaeSGarrett D'Amore int
zpool_reguid(zpool_handle_t * zhp)3760e9103aaeSGarrett D'Amore zpool_reguid(zpool_handle_t *zhp)
3761e9103aaeSGarrett D'Amore {
3762e9103aaeSGarrett D'Amore char msg[1024];
3763e9103aaeSGarrett D'Amore libzfs_handle_t *hdl = zhp->zpool_hdl;
3764e9103aaeSGarrett D'Amore zfs_cmd_t zc = { 0 };
3765e9103aaeSGarrett D'Amore
3766e9103aaeSGarrett D'Amore (void) snprintf(msg, sizeof (msg),
3767e9103aaeSGarrett D'Amore dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3768e9103aaeSGarrett D'Amore
3769e9103aaeSGarrett D'Amore (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3770e9103aaeSGarrett D'Amore if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3771e9103aaeSGarrett D'Amore return (0);
3772e9103aaeSGarrett D'Amore
3773e9103aaeSGarrett D'Amore return (zpool_standard_error(hdl, errno, msg));
3774e9103aaeSGarrett D'Amore }
3775e9103aaeSGarrett D'Amore
37764263d13fSGeorge Wilson /*
37774263d13fSGeorge Wilson * Reopen the pool.
37784263d13fSGeorge Wilson */
37794263d13fSGeorge Wilson int
zpool_reopen(zpool_handle_t * zhp)37804263d13fSGeorge Wilson zpool_reopen(zpool_handle_t *zhp)
37814263d13fSGeorge Wilson {
37824263d13fSGeorge Wilson zfs_cmd_t zc = { 0 };
37834263d13fSGeorge Wilson char msg[1024];
37844263d13fSGeorge Wilson libzfs_handle_t *hdl = zhp->zpool_hdl;
37854263d13fSGeorge Wilson
37864263d13fSGeorge Wilson (void) snprintf(msg, sizeof (msg),
37874263d13fSGeorge Wilson dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
37884263d13fSGeorge Wilson zhp->zpool_name);
37894263d13fSGeorge Wilson
37904263d13fSGeorge Wilson (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
37914263d13fSGeorge Wilson if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
37924263d13fSGeorge Wilson return (0);
37934263d13fSGeorge Wilson return (zpool_standard_error(hdl, errno, msg));
37944263d13fSGeorge Wilson }
37954263d13fSGeorge Wilson
37969c2acf00SAlek Pinchuk /* call into libzfs_core to execute the sync IOCTL per pool */
37979c2acf00SAlek Pinchuk int
zpool_sync_one(zpool_handle_t * zhp,void * data)37989c2acf00SAlek Pinchuk zpool_sync_one(zpool_handle_t *zhp, void *data)
37999c2acf00SAlek Pinchuk {
38009c2acf00SAlek Pinchuk int ret;
38019c2acf00SAlek Pinchuk libzfs_handle_t *hdl = zpool_get_handle(zhp);
38029c2acf00SAlek Pinchuk const char *pool_name = zpool_get_name(zhp);
38039c2acf00SAlek Pinchuk boolean_t *force = data;
38049c2acf00SAlek Pinchuk nvlist_t *innvl = fnvlist_alloc();
38059c2acf00SAlek Pinchuk
38069c2acf00SAlek Pinchuk fnvlist_add_boolean_value(innvl, "force", *force);
38079c2acf00SAlek Pinchuk if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
38089c2acf00SAlek Pinchuk nvlist_free(innvl);
38099c2acf00SAlek Pinchuk return (zpool_standard_error_fmt(hdl, ret,
38109c2acf00SAlek Pinchuk dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
38119c2acf00SAlek Pinchuk }
38129c2acf00SAlek Pinchuk nvlist_free(innvl);
38139c2acf00SAlek Pinchuk
38149c2acf00SAlek Pinchuk return (0);
38159c2acf00SAlek Pinchuk }
38169c2acf00SAlek Pinchuk
3817c67d9675Seschrock /*
3818c67d9675Seschrock * Convert from a devid string to a path.
3819c67d9675Seschrock */
3820c67d9675Seschrock static char *
devid_to_path(char * devid_str)3821c67d9675Seschrock devid_to_path(char *devid_str)
3822c67d9675Seschrock {
3823c67d9675Seschrock ddi_devid_t devid;
3824c67d9675Seschrock char *minor;
3825c67d9675Seschrock char *path;
3826c67d9675Seschrock devid_nmlist_t *list = NULL;
3827c67d9675Seschrock int ret;
3828c67d9675Seschrock
3829c67d9675Seschrock if (devid_str_decode(devid_str, &devid, &minor) != 0)
3830c67d9675Seschrock return (NULL);
3831c67d9675Seschrock
3832c67d9675Seschrock ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3833c67d9675Seschrock
3834c67d9675Seschrock devid_str_free(minor);
3835c67d9675Seschrock devid_free(devid);
3836c67d9675Seschrock
3837c67d9675Seschrock if (ret != 0)
3838c67d9675Seschrock return (NULL);
3839c67d9675Seschrock
3840078266a5SMarcel Telka /*
3841078266a5SMarcel Telka * In a case the strdup() fails, we will just return NULL below.
3842078266a5SMarcel Telka */
3843078266a5SMarcel Telka path = strdup(list[0].devname);
384499653d4eSeschrock
3845c67d9675Seschrock devid_free_nmlist(list);
3846c67d9675Seschrock
3847c67d9675Seschrock return (path);
3848c67d9675Seschrock }
3849c67d9675Seschrock
3850c67d9675Seschrock /*
3851c67d9675Seschrock * Convert from a path to a devid string.
3852c67d9675Seschrock */
3853c67d9675Seschrock static char *
path_to_devid(const char * path)3854c67d9675Seschrock path_to_devid(const char *path)
3855c67d9675Seschrock {
3856c67d9675Seschrock int fd;
3857c67d9675Seschrock ddi_devid_t devid;
3858c67d9675Seschrock char *minor, *ret;
3859c67d9675Seschrock
3860c67d9675Seschrock if ((fd = open(path, O_RDONLY)) < 0)
3861c67d9675Seschrock return (NULL);
3862c67d9675Seschrock
3863c67d9675Seschrock minor = NULL;
3864c67d9675Seschrock ret = NULL;
3865c67d9675Seschrock if (devid_get(fd, &devid) == 0) {
3866c67d9675Seschrock if (devid_get_minor_name(fd, &minor) == 0)
3867c67d9675Seschrock ret = devid_str_encode(devid, minor);
3868c67d9675Seschrock if (minor != NULL)
3869c67d9675Seschrock devid_str_free(minor);
3870c67d9675Seschrock devid_free(devid);
3871c67d9675Seschrock }
3872c67d9675Seschrock (void) close(fd);
3873c67d9675Seschrock
3874c67d9675Seschrock return (ret);
3875c67d9675Seschrock }
3876c67d9675Seschrock
3877727feae5SJoshua M. Clulow struct path_from_physpath_walker_args {
3878727feae5SJoshua M. Clulow char *pfpwa_path;
3879727feae5SJoshua M. Clulow };
3880727feae5SJoshua M. Clulow
3881727feae5SJoshua M. Clulow /*
3882727feae5SJoshua M. Clulow * Walker for use with di_devlink_walk(). Stores the "/dev" path of the first
3883727feae5SJoshua M. Clulow * primary devlink (i.e., the first devlink which refers to our "/devices"
3884727feae5SJoshua M. Clulow * node) and stops walking.
3885727feae5SJoshua M. Clulow */
3886727feae5SJoshua M. Clulow static int
path_from_physpath_walker(di_devlink_t devlink,void * arg)3887727feae5SJoshua M. Clulow path_from_physpath_walker(di_devlink_t devlink, void *arg)
3888727feae5SJoshua M. Clulow {
3889727feae5SJoshua M. Clulow struct path_from_physpath_walker_args *pfpwa = arg;
3890727feae5SJoshua M. Clulow
3891727feae5SJoshua M. Clulow if (di_devlink_type(devlink) != DI_PRIMARY_LINK) {
3892727feae5SJoshua M. Clulow return (DI_WALK_CONTINUE);
3893727feae5SJoshua M. Clulow }
3894727feae5SJoshua M. Clulow
3895727feae5SJoshua M. Clulow verify(pfpwa->pfpwa_path == NULL);
3896727feae5SJoshua M. Clulow if ((pfpwa->pfpwa_path = strdup(di_devlink_path(devlink))) != NULL) {
3897727feae5SJoshua M. Clulow return (DI_WALK_TERMINATE);
3898727feae5SJoshua M. Clulow }
3899727feae5SJoshua M. Clulow
3900727feae5SJoshua M. Clulow return (DI_WALK_CONTINUE);
3901727feae5SJoshua M. Clulow }
3902727feae5SJoshua M. Clulow
3903727feae5SJoshua M. Clulow /*
3904727feae5SJoshua M. Clulow * Search for a "/dev" path that refers to our physical path. Returns the new
3905727feae5SJoshua M. Clulow * path if one is found and it does not match the existing "path" value. If
3906727feae5SJoshua M. Clulow * the value is unchanged, or one could not be found, returns NULL.
3907727feae5SJoshua M. Clulow */
3908727feae5SJoshua M. Clulow static char *
path_from_physpath(libzfs_handle_t * hdl,const char * path,const char * physpath)3909727feae5SJoshua M. Clulow path_from_physpath(libzfs_handle_t *hdl, const char *path,
3910727feae5SJoshua M. Clulow const char *physpath)
3911727feae5SJoshua M. Clulow {
3912727feae5SJoshua M. Clulow struct path_from_physpath_walker_args pfpwa;
3913727feae5SJoshua M. Clulow
3914727feae5SJoshua M. Clulow if (physpath == NULL) {
3915727feae5SJoshua M. Clulow return (NULL);
3916727feae5SJoshua M. Clulow }
3917727feae5SJoshua M. Clulow
3918727feae5SJoshua M. Clulow if (hdl->libzfs_devlink == NULL) {
3919727feae5SJoshua M. Clulow if ((hdl->libzfs_devlink = di_devlink_init(NULL, 0)) ==
3920727feae5SJoshua M. Clulow DI_LINK_NIL) {
3921727feae5SJoshua M. Clulow /*
3922727feae5SJoshua M. Clulow * We may not be able to open a handle if this process
3923727feae5SJoshua M. Clulow * is insufficiently privileged, or we are too early in
3924727feae5SJoshua M. Clulow * boot for devfsadm to be ready. Ignore this error
3925727feae5SJoshua M. Clulow * and defer the path check to a subsequent run.
3926727feae5SJoshua M. Clulow */
3927727feae5SJoshua M. Clulow return (NULL);
3928727feae5SJoshua M. Clulow }
3929727feae5SJoshua M. Clulow }
3930727feae5SJoshua M. Clulow
3931727feae5SJoshua M. Clulow pfpwa.pfpwa_path = NULL;
3932727feae5SJoshua M. Clulow (void) di_devlink_walk(hdl->libzfs_devlink, NULL, physpath,
3933727feae5SJoshua M. Clulow DI_PRIMARY_LINK, &pfpwa, path_from_physpath_walker);
3934727feae5SJoshua M. Clulow
3935727feae5SJoshua M. Clulow if (path != NULL && pfpwa.pfpwa_path != NULL &&
3936727feae5SJoshua M. Clulow strcmp(path, pfpwa.pfpwa_path) == 0) {
3937727feae5SJoshua M. Clulow /*
3938727feae5SJoshua M. Clulow * If the path is already correct, no change is required.
3939727feae5SJoshua M. Clulow */
3940727feae5SJoshua M. Clulow free(pfpwa.pfpwa_path);
3941727feae5SJoshua M. Clulow return (NULL);
3942727feae5SJoshua M. Clulow }
3943727feae5SJoshua M. Clulow
3944727feae5SJoshua M. Clulow return (pfpwa.pfpwa_path);
3945727feae5SJoshua M. Clulow }
3946727feae5SJoshua M. Clulow
3947c67d9675Seschrock /*
3948c67d9675Seschrock * Issue the necessary ioctl() to update the stored path value for the vdev. We
3949c67d9675Seschrock * ignore any failure here, since a common case is for an unprivileged user to
3950c67d9675Seschrock * type 'zpool status', and we'll display the correct information anyway.
3951c67d9675Seschrock */
3952c67d9675Seschrock static void
set_path(zpool_handle_t * zhp,nvlist_t * nv,const char * path)3953c67d9675Seschrock set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3954c67d9675Seschrock {
3955c67d9675Seschrock zfs_cmd_t zc = { 0 };
3956c67d9675Seschrock
3957c67d9675Seschrock (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3958e9dbad6fSeschrock (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3959c67d9675Seschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3960ea8dc4b6Seschrock &zc.zc_guid) == 0);
3961c67d9675Seschrock
396299653d4eSeschrock (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3963c67d9675Seschrock }
3964c67d9675Seschrock
3965b4fb0039SJoshua M. Clulow /*
3966b4fb0039SJoshua M. Clulow * This routine is responsible for identifying when disks have been
3967b4fb0039SJoshua M. Clulow * reconfigured in a new location. The kernel will have opened the device by
3968b4fb0039SJoshua M. Clulow * devid, but the path will still refer to the old location. To catch this, we
3969b4fb0039SJoshua M. Clulow * first do a path -> devid translation (which is fast for the common case).
3970b4fb0039SJoshua M. Clulow * If the devid matches, we're done. If not, we do a reverse devid -> path
3971b4fb0039SJoshua M. Clulow * translation and issue the appropriate ioctl() to update the path of the
3972b4fb0039SJoshua M. Clulow * vdev.
3973b4fb0039SJoshua M. Clulow */
3974b4fb0039SJoshua M. Clulow void
zpool_vdev_refresh_path(libzfs_handle_t * hdl,zpool_handle_t * zhp,nvlist_t * nv)3975b4fb0039SJoshua M. Clulow zpool_vdev_refresh_path(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
3976b4fb0039SJoshua M. Clulow {
3977b4fb0039SJoshua M. Clulow char *path = NULL;
3978b4fb0039SJoshua M. Clulow char *newpath = NULL;
3979b4fb0039SJoshua M. Clulow char *physpath = NULL;
3980b4fb0039SJoshua M. Clulow char *devid = NULL;
3981b4fb0039SJoshua M. Clulow
3982b4fb0039SJoshua M. Clulow if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) {
3983b4fb0039SJoshua M. Clulow return;
3984b4fb0039SJoshua M. Clulow }
3985b4fb0039SJoshua M. Clulow
3986b4fb0039SJoshua M. Clulow if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3987b4fb0039SJoshua M. Clulow /*
3988b4fb0039SJoshua M. Clulow * This vdev has a devid. We can use it to check the current
3989b4fb0039SJoshua M. Clulow * path.
3990b4fb0039SJoshua M. Clulow */
3991b4fb0039SJoshua M. Clulow char *newdevid = path_to_devid(path);
3992b4fb0039SJoshua M. Clulow
3993b4fb0039SJoshua M. Clulow if (newdevid == NULL || strcmp(devid, newdevid) != 0) {
3994b4fb0039SJoshua M. Clulow newpath = devid_to_path(devid);
3995b4fb0039SJoshua M. Clulow }
3996b4fb0039SJoshua M. Clulow
3997b4fb0039SJoshua M. Clulow if (newdevid != NULL) {
3998b4fb0039SJoshua M. Clulow devid_str_free(newdevid);
3999b4fb0039SJoshua M. Clulow }
4000b4fb0039SJoshua M. Clulow
4001b4fb0039SJoshua M. Clulow } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
4002b4fb0039SJoshua M. Clulow &physpath) == 0) {
4003b4fb0039SJoshua M. Clulow /*
4004b4fb0039SJoshua M. Clulow * This vdev does not have a devid, but it does have a physical
4005b4fb0039SJoshua M. Clulow * path. Attempt to translate this to a /dev path.
4006b4fb0039SJoshua M. Clulow */
4007b4fb0039SJoshua M. Clulow newpath = path_from_physpath(hdl, path, physpath);
4008b4fb0039SJoshua M. Clulow }
4009b4fb0039SJoshua M. Clulow
4010b4fb0039SJoshua M. Clulow if (newpath == NULL) {
4011b4fb0039SJoshua M. Clulow /*
4012b4fb0039SJoshua M. Clulow * No path update is required.
4013b4fb0039SJoshua M. Clulow */
4014b4fb0039SJoshua M. Clulow return;
4015b4fb0039SJoshua M. Clulow }
4016b4fb0039SJoshua M. Clulow
4017b4fb0039SJoshua M. Clulow set_path(zhp, nv, newpath);
4018b4fb0039SJoshua M. Clulow fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, newpath);
4019b4fb0039SJoshua M. Clulow
4020b4fb0039SJoshua M. Clulow free(newpath);
4021b4fb0039SJoshua M. Clulow }
4022b4fb0039SJoshua M. Clulow
4023c67d9675Seschrock /*
4024c67d9675Seschrock * Given a vdev, return the name to display in iostat. If the vdev has a path,
4025c67d9675Seschrock * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4026b4fb0039SJoshua M. Clulow * We will confirm that the path and name of the vdev are current, and update
4027b4fb0039SJoshua M. Clulow * them if not. We also check if this is a whole disk, in which case we strip
4028b4fb0039SJoshua M. Clulow * off the trailing 's0' slice name.
4029c67d9675Seschrock *
4030c67d9675Seschrock * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4031c67d9675Seschrock * of these checks.
4032c67d9675Seschrock */
4033c67d9675Seschrock char *
zpool_vdev_name(libzfs_handle_t * hdl,zpool_handle_t * zhp,nvlist_t * nv,int name_flags)403488ecc943SGeorge Wilson zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
4035663207adSDon Brady int name_flags)
4036c67d9675Seschrock {
4037dd50e0ccSTony Hutter char *path, *type, *env;
4038ea8dc4b6Seschrock uint64_t value;
4039c67d9675Seschrock
4040dd50e0ccSTony Hutter /*
4041dd50e0ccSTony Hutter * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4042dd50e0ccSTony Hutter * zpool name that will be displayed to the user.
4043dd50e0ccSTony Hutter */
4044dd50e0ccSTony Hutter verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
4045dd50e0ccSTony Hutter if (zhp != NULL && strcmp(type, "root") == 0)
4046dd50e0ccSTony Hutter return (zfs_strdup(hdl, zpool_get_name(zhp)));
4047dd50e0ccSTony Hutter
4048663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_PATH");
4049663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 ||
4050663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4051663207adSDon Brady name_flags |= VDEV_NAME_PATH;
4052663207adSDon Brady
4053663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_GUID");
4054663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 ||
4055663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4056663207adSDon Brady name_flags |= VDEV_NAME_GUID;
4057663207adSDon Brady
4058663207adSDon Brady env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4059663207adSDon Brady if (env && (strtoul(env, NULL, 0) > 0 ||
4060663207adSDon Brady !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4061663207adSDon Brady name_flags |= VDEV_NAME_FOLLOW_LINKS;
4062663207adSDon Brady
4063663207adSDon Brady if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4064663207adSDon Brady name_flags & VDEV_NAME_GUID) {
4065663207adSDon Brady nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
406633f4e805SAndy Fiddaman path = zfs_asprintf(hdl, "%llu", (u_longlong_t)value);
4067ea8dc4b6Seschrock } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
4068727feae5SJoshua M. Clulow vdev_stat_t *vs;
4069727feae5SJoshua M. Clulow uint_t vsc;
4070c67d9675Seschrock
40713d7072f8Seschrock /*
40723d7072f8Seschrock * If the device is dead (faulted, offline, etc) then don't
40733d7072f8Seschrock * bother opening it. Otherwise we may be forcing the user to
40743d7072f8Seschrock * open a misbehaving device, which can have undesirable
40753d7072f8Seschrock * effects.
40763d7072f8Seschrock */
4077727feae5SJoshua M. Clulow if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
40783d7072f8Seschrock (uint64_t **)&vs, &vsc) != 0 ||
4079727feae5SJoshua M. Clulow vs->vs_state < VDEV_STATE_DEGRADED ||
4080727feae5SJoshua M. Clulow zhp == NULL) {
408133f4e805SAndy Fiddaman path = zfs_strdup(hdl, path);
4082727feae5SJoshua M. Clulow goto after_open;
4083727feae5SJoshua M. Clulow }
4084727feae5SJoshua M. Clulow
4085b4fb0039SJoshua M. Clulow /*
4086b4fb0039SJoshua M. Clulow * Refresh the /dev path for this vdev if required, then ensure
4087b4fb0039SJoshua M. Clulow * we're using the latest path value:
4088b4fb0039SJoshua M. Clulow */
4089b4fb0039SJoshua M. Clulow zpool_vdev_refresh_path(hdl, zhp, nv);
4090b4fb0039SJoshua M. Clulow path = fnvlist_lookup_string(nv, ZPOOL_CONFIG_PATH);
4091c67d9675Seschrock
4092663207adSDon Brady if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4093663207adSDon Brady char *rp = realpath(path, NULL);
409433f4e805SAndy Fiddaman if (rp == NULL)
409533f4e805SAndy Fiddaman no_memory(hdl);
409633f4e805SAndy Fiddaman path = rp;
409733f4e805SAndy Fiddaman } else {
409833f4e805SAndy Fiddaman path = zfs_strdup(hdl, path);
4099663207adSDon Brady }
4100663207adSDon Brady
4101727feae5SJoshua M. Clulow after_open:
410233f4e805SAndy Fiddaman if (strncmp(path, ZFS_DISK_ROOTD,
410333f4e805SAndy Fiddaman sizeof (ZFS_DISK_ROOTD) - 1) == 0) {
4104587f87a4SMarco van Wieringen const char *p2 = path + sizeof (ZFS_DISK_ROOTD) - 1;
4105587f87a4SMarco van Wieringen
4106587f87a4SMarco van Wieringen memmove(path, p2, strlen(p2) + 1);
410733f4e805SAndy Fiddaman }
4108c67d9675Seschrock
4109663207adSDon Brady /*
4110663207adSDon Brady * Remove the partition from the path it this is a whole disk.
4111663207adSDon Brady */
4112663207adSDon Brady if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4113663207adSDon Brady == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
41143fdda499SJohn Harres int pathlen = strlen(path);
41153fdda499SJohn Harres
41163fdda499SJohn Harres /*
41177855d95bSToomas Soome * If it starts with c#, and ends with "s0" or "s1",
41187855d95bSToomas Soome * chop the slice off, or if it ends with "s0/old" or
41197855d95bSToomas Soome * "s1/old", remove the slice from the middle.
41203fdda499SJohn Harres */
412133f4e805SAndy Fiddaman if (CTD_CHECK(path)) {
412233f4e805SAndy Fiddaman if (strcmp(&path[pathlen - 2], "s0") == 0 ||
412333f4e805SAndy Fiddaman strcmp(&path[pathlen - 2], "s1") == 0) {
412433f4e805SAndy Fiddaman path[pathlen - 2] = '\0';
41253fdda499SJohn Harres } else if (pathlen > 6 &&
412633f4e805SAndy Fiddaman (strcmp(&path[pathlen - 6],
412733f4e805SAndy Fiddaman "s0/old") == 0 ||
412833f4e805SAndy Fiddaman strcmp(&path[pathlen - 6],
412933f4e805SAndy Fiddaman "s1/old") == 0)) {
413033f4e805SAndy Fiddaman (void) strcpy(&path[pathlen - 6],
41313fdda499SJohn Harres "/old");
41323fdda499SJohn Harres }
41333fdda499SJohn Harres }
413433f4e805SAndy Fiddaman return (path);
4135c67d9675Seschrock }
4136c67d9675Seschrock } else {
413799653d4eSeschrock /*
413899653d4eSeschrock * If it's a raidz device, we need to stick in the parity level.
413999653d4eSeschrock */
414033f4e805SAndy Fiddaman if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
414199653d4eSeschrock verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
414299653d4eSeschrock &value) == 0);
414333f4e805SAndy Fiddaman path = zfs_asprintf(hdl, "%s%llu", type,
41445ad82045Snd (u_longlong_t)value);
414533f4e805SAndy Fiddaman } else {
414633f4e805SAndy Fiddaman path = zfs_strdup(hdl, type);
414799653d4eSeschrock }
414888ecc943SGeorge Wilson
414988ecc943SGeorge Wilson /*
415088ecc943SGeorge Wilson * We identify each top-level vdev by using a <type-id>
415188ecc943SGeorge Wilson * naming convention.
415288ecc943SGeorge Wilson */
4153663207adSDon Brady if (name_flags & VDEV_NAME_TYPE_ID) {
415488ecc943SGeorge Wilson uint64_t id;
415533f4e805SAndy Fiddaman char *tmp;
415688ecc943SGeorge Wilson
415788ecc943SGeorge Wilson verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
415888ecc943SGeorge Wilson &id) == 0);
415933f4e805SAndy Fiddaman tmp = zfs_asprintf(hdl, "%s-%llu", path,
416088ecc943SGeorge Wilson (u_longlong_t)id);
416133f4e805SAndy Fiddaman free(path);
416233f4e805SAndy Fiddaman path = tmp;
416388ecc943SGeorge Wilson }
4164c67d9675Seschrock }
4165c67d9675Seschrock
416633f4e805SAndy Fiddaman return (path);
4167c67d9675Seschrock }
4168ea8dc4b6Seschrock
4169ea8dc4b6Seschrock static int
zbookmark_mem_compare(const void * a,const void * b)4170a2cdcdd2SPaul Dagnelie zbookmark_mem_compare(const void *a, const void *b)
4171ea8dc4b6Seschrock {
41727802d7bfSMatthew Ahrens return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4173ea8dc4b6Seschrock }
4174ea8dc4b6Seschrock
4175ea8dc4b6Seschrock /*
4176ea8dc4b6Seschrock * Retrieve the persistent error log, uniquify the members, and return to the
4177ea8dc4b6Seschrock * caller.
4178ea8dc4b6Seschrock */
4179ea8dc4b6Seschrock int
zpool_get_errlog(zpool_handle_t * zhp,nvlist_t ** nverrlistp)418055434c77Sek zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4181ea8dc4b6Seschrock {
4182ea8dc4b6Seschrock zfs_cmd_t zc = { 0 };
4183ea8dc4b6Seschrock uint64_t count;
41847802d7bfSMatthew Ahrens zbookmark_phys_t *zb = NULL;
418555434c77Sek int i;
4186ea8dc4b6Seschrock
4187ea8dc4b6Seschrock /*
4188ea8dc4b6Seschrock * Retrieve the raw error list from the kernel. If the number of errors
4189ea8dc4b6Seschrock * has increased, allocate more space and continue until we get the
4190ea8dc4b6Seschrock * entire list.
4191ea8dc4b6Seschrock */
4192ea8dc4b6Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4193ea8dc4b6Seschrock &count) == 0);
419475519f38Sek if (count == 0)
419575519f38Sek return (0);
4196e9dbad6fSeschrock if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
41977802d7bfSMatthew Ahrens count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
419899653d4eSeschrock return (-1);
4199e9dbad6fSeschrock zc.zc_nvlist_dst_size = count;
4200ea8dc4b6Seschrock (void) strcpy(zc.zc_name, zhp->zpool_name);
4201ea8dc4b6Seschrock for (;;) {
420299653d4eSeschrock if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
420399653d4eSeschrock &zc) != 0) {
4204e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst);
4205ea8dc4b6Seschrock if (errno == ENOMEM) {
42067802d7bfSMatthew Ahrens void *dst;
42077802d7bfSMatthew Ahrens
4208bf561db0Svb count = zc.zc_nvlist_dst_size;
42097802d7bfSMatthew Ahrens dst = zfs_alloc(zhp->zpool_hdl, count *
42107802d7bfSMatthew Ahrens sizeof (zbookmark_phys_t));
42117802d7bfSMatthew Ahrens if (dst == NULL)
421299653d4eSeschrock return (-1);
42137802d7bfSMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)dst;
4214ea8dc4b6Seschrock } else {
4215ea8dc4b6Seschrock return (-1);
4216ea8dc4b6Seschrock }
4217ea8dc4b6Seschrock } else {
4218ea8dc4b6Seschrock break;
4219ea8dc4b6Seschrock }
4220ea8dc4b6Seschrock }
4221ea8dc4b6Seschrock
4222ea8dc4b6Seschrock /*
4223ea8dc4b6Seschrock * Sort the resulting bookmarks. This is a little confusing due to the
4224ea8dc4b6Seschrock * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4225e9dbad6fSeschrock * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
4226ea8dc4b6Seschrock * _not_ copied as part of the process. So we point the start of our
4227ea8dc4b6Seschrock * array appropriate and decrement the total number of elements.
4228ea8dc4b6Seschrock */
42297802d7bfSMatthew Ahrens zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4230e9dbad6fSeschrock zc.zc_nvlist_dst_size;
4231e9dbad6fSeschrock count -= zc.zc_nvlist_dst_size;
4232ea8dc4b6Seschrock
4233a2cdcdd2SPaul Dagnelie qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4234ea8dc4b6Seschrock
423555434c77Sek verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4236ea8dc4b6Seschrock
4237ea8dc4b6Seschrock /*
423855434c77Sek * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4239ea8dc4b6Seschrock */
4240ea8dc4b6Seschrock for (i = 0; i < count; i++) {
4241ea8dc4b6Seschrock nvlist_t *nv;
4242ea8dc4b6Seschrock
4243c0a81264Sek /* ignoring zb_blkid and zb_level for now */
4244c0a81264Sek if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4245c0a81264Sek zb[i-1].zb_object == zb[i].zb_object)
4246ea8dc4b6Seschrock continue;
4247ea8dc4b6Seschrock
424855434c77Sek if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
424955434c77Sek goto nomem;
425055434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
425155434c77Sek zb[i].zb_objset) != 0) {
425255434c77Sek nvlist_free(nv);
425399653d4eSeschrock goto nomem;
4254ea8dc4b6Seschrock }
425555434c77Sek if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
425655434c77Sek zb[i].zb_object) != 0) {
425755434c77Sek nvlist_free(nv);
425855434c77Sek goto nomem;
425955434c77Sek }
426055434c77Sek if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
426155434c77Sek nvlist_free(nv);
426255434c77Sek goto nomem;
426355434c77Sek }
426455434c77Sek nvlist_free(nv);
4265ea8dc4b6Seschrock }
4266ea8dc4b6Seschrock
42673ccfa83cSahrens free((void *)(uintptr_t)zc.zc_nvlist_dst);
4268ea8dc4b6Seschrock return (0);
426999653d4eSeschrock
427099653d4eSeschrock nomem:
4271e9dbad6fSeschrock free((void *)(uintptr_t)zc.zc_nvlist_dst);
427299653d4eSeschrock return (no_memory(zhp->zpool_hdl));
4273ea8dc4b6Seschrock }
4274eaca9bbdSeschrock
4275eaca9bbdSeschrock /*
4276eaca9bbdSeschrock * Upgrade a ZFS pool to the latest on-disk version.
4277eaca9bbdSeschrock */
4278eaca9bbdSeschrock int
zpool_upgrade(zpool_handle_t * zhp,uint64_t new_version)4279990b4856Slling zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4280eaca9bbdSeschrock {
4281eaca9bbdSeschrock zfs_cmd_t zc = { 0 };
428299653d4eSeschrock libzfs_handle_t *hdl = zhp->zpool_hdl;
4283eaca9bbdSeschrock
4284eaca9bbdSeschrock (void) strcpy(zc.zc_name, zhp->zpool_name);
4285990b4856Slling zc.zc_cookie = new_version;
4286990b4856Slling
4287ecd6cf80Smarks if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4288ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno,
428999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
429099653d4eSeschrock zhp->zpool_name));
4291eaca9bbdSeschrock return (0);
4292eaca9bbdSeschrock }
429306eeb2adSek
429406eeb2adSek void
zfs_save_arguments(int argc,char ** argv,char * string,int len)42954445fffbSMatthew Ahrens zfs_save_arguments(int argc, char **argv, char *string, int len)
429606eeb2adSek {
42974445fffbSMatthew Ahrens (void) strlcpy(string, basename(argv[0]), len);
42984445fffbSMatthew Ahrens for (int i = 1; i < argc; i++) {
42994445fffbSMatthew Ahrens (void) strlcat(string, " ", len);
43004445fffbSMatthew Ahrens (void) strlcat(string, argv[i], len);
43012a6b87f0Sek }
43022a6b87f0Sek }
43032a6b87f0Sek
43042a6b87f0Sek int
zpool_log_history(libzfs_handle_t * hdl,const char * message)43054445fffbSMatthew Ahrens zpool_log_history(libzfs_handle_t *hdl, const char *message)
43062a6b87f0Sek {
43074445fffbSMatthew Ahrens zfs_cmd_t zc = { 0 };
43084445fffbSMatthew Ahrens nvlist_t *args;
43094445fffbSMatthew Ahrens int err;
43104445fffbSMatthew Ahrens
43114445fffbSMatthew Ahrens args = fnvlist_alloc();
43124445fffbSMatthew Ahrens fnvlist_add_string(args, "message", message);
43134445fffbSMatthew Ahrens err = zcmd_write_src_nvlist(hdl, &zc, args);
43144445fffbSMatthew Ahrens if (err == 0)
43154445fffbSMatthew Ahrens err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
43164445fffbSMatthew Ahrens nvlist_free(args);
43174445fffbSMatthew Ahrens zcmd_free_nvlists(&zc);
43184445fffbSMatthew Ahrens return (err);
431906eeb2adSek }
432006eeb2adSek
432106eeb2adSek /*
432206eeb2adSek * Perform ioctl to get some command history of a pool.
432306eeb2adSek *
432406eeb2adSek * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
432506eeb2adSek * logical offset of the history buffer to start reading from.
432606eeb2adSek *
432706eeb2adSek * Upon return, 'off' is the next logical offset to read from and
432806eeb2adSek * 'len' is the actual amount of bytes read into 'buf'.
432906eeb2adSek */
433006eeb2adSek static int
get_history(zpool_handle_t * zhp,char * buf,uint64_t * off,uint64_t * len)433106eeb2adSek get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
433206eeb2adSek {
433306eeb2adSek zfs_cmd_t zc = { 0 };
433406eeb2adSek libzfs_handle_t *hdl = zhp->zpool_hdl;
433506eeb2adSek
433606eeb2adSek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
433706eeb2adSek
433806eeb2adSek zc.zc_history = (uint64_t)(uintptr_t)buf;
433906eeb2adSek zc.zc_history_len = *len;
434006eeb2adSek zc.zc_history_offset = *off;
434106eeb2adSek
434206eeb2adSek if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
434306eeb2adSek switch (errno) {
434406eeb2adSek case EPERM:
4345ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_PERM,
4346ece3d9b3Slling dgettext(TEXT_DOMAIN,
434706eeb2adSek "cannot show history for pool '%s'"),
434806eeb2adSek zhp->zpool_name));
434906eeb2adSek case ENOENT:
4350ece3d9b3Slling return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
435106eeb2adSek dgettext(TEXT_DOMAIN, "cannot get history for pool "
435206eeb2adSek "'%s'"), zhp->zpool_name));
4353d7306b64Sek case ENOTSUP:
4354d7306b64Sek return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4355d7306b64Sek dgettext(TEXT_DOMAIN, "cannot get history for pool "
4356d7306b64Sek "'%s', pool must be upgraded"), zhp->zpool_name));
435706eeb2adSek default:
4358ece3d9b3Slling return (zpool_standard_error_fmt(hdl, errno,
435906eeb2adSek dgettext(TEXT_DOMAIN,
436006eeb2adSek "cannot get history for '%s'"), zhp->zpool_name));
436106eeb2adSek }
436206eeb2adSek }
436306eeb2adSek
436406eeb2adSek *len = zc.zc_history_len;
436506eeb2adSek *off = zc.zc_history_offset;
436606eeb2adSek
436706eeb2adSek return (0);
436806eeb2adSek }
436906eeb2adSek
437006eeb2adSek /*
437106eeb2adSek * Retrieve the command history of a pool.
437206eeb2adSek */
437306eeb2adSek int
zpool_get_history(zpool_handle_t * zhp,nvlist_t ** nvhisp,uint64_t * off,boolean_t * eof)4374a52121eaSChunwei Chen zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4375a52121eaSChunwei Chen boolean_t *eof)
437606eeb2adSek {
43773339867aSMatthew Ahrens char *buf;
43783339867aSMatthew Ahrens int buflen = 128 * 1024;
437906eeb2adSek nvlist_t **records = NULL;
438006eeb2adSek uint_t numrecords = 0;
4381a52121eaSChunwei Chen int err = 0, i;
4382a52121eaSChunwei Chen uint64_t start = *off;
438306eeb2adSek
43843339867aSMatthew Ahrens buf = malloc(buflen);
43853339867aSMatthew Ahrens if (buf == NULL)
43863339867aSMatthew Ahrens return (ENOMEM);
4387a52121eaSChunwei Chen /* process about 1MB a time */
4388a52121eaSChunwei Chen while (*off - start < 1024 * 1024) {
43893339867aSMatthew Ahrens uint64_t bytes_read = buflen;
439006eeb2adSek uint64_t leftover;
439106eeb2adSek
4392a52121eaSChunwei Chen if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
439306eeb2adSek break;
439406eeb2adSek
439506eeb2adSek /* if nothing else was read in, we're at EOF, just return */
4396a52121eaSChunwei Chen if (!bytes_read) {
4397a52121eaSChunwei Chen *eof = B_TRUE;
439806eeb2adSek break;
4399a52121eaSChunwei Chen }
440006eeb2adSek
440106eeb2adSek if ((err = zpool_history_unpack(buf, bytes_read,
440206eeb2adSek &leftover, &records, &numrecords)) != 0)
440306eeb2adSek break;
4404a52121eaSChunwei Chen *off -= leftover;
44053339867aSMatthew Ahrens if (leftover == bytes_read) {
44063339867aSMatthew Ahrens /*
44073339867aSMatthew Ahrens * no progress made, because buffer is not big enough
44083339867aSMatthew Ahrens * to hold this record; resize and retry.
44093339867aSMatthew Ahrens */
44103339867aSMatthew Ahrens buflen *= 2;
44113339867aSMatthew Ahrens free(buf);
44123339867aSMatthew Ahrens buf = malloc(buflen);
44133339867aSMatthew Ahrens if (buf == NULL)
44143339867aSMatthew Ahrens return (ENOMEM);
44153339867aSMatthew Ahrens }
4416a52121eaSChunwei Chen }
441706eeb2adSek
44183339867aSMatthew Ahrens free(buf);
44193339867aSMatthew Ahrens
442006eeb2adSek if (!err) {
442106eeb2adSek verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
442206eeb2adSek verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
442306eeb2adSek records, numrecords) == 0);
442406eeb2adSek }
442506eeb2adSek for (i = 0; i < numrecords; i++)
442606eeb2adSek nvlist_free(records[i]);
442706eeb2adSek free(records);
442806eeb2adSek
442906eeb2adSek return (err);
443006eeb2adSek }
443155434c77Sek
443255434c77Sek void
zpool_obj_to_path(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)443355434c77Sek zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
443455434c77Sek char *pathname, size_t len)
443555434c77Sek {
443655434c77Sek zfs_cmd_t zc = { 0 };
443755434c77Sek boolean_t mounted = B_FALSE;
443855434c77Sek char *mntpnt = NULL;
44399adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN];
444055434c77Sek
444155434c77Sek if (dsobj == 0) {
444255434c77Sek /* special case for the MOS */
444355434c77Sek (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
444455434c77Sek return;
444555434c77Sek }
444655434c77Sek
444755434c77Sek /* get the dataset's name */
444855434c77Sek (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
444955434c77Sek zc.zc_obj = dsobj;
445055434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd,
445155434c77Sek ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
445255434c77Sek /* just write out a path of two object numbers */
445355434c77Sek (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
445455434c77Sek dsobj, obj);
445555434c77Sek return;
445655434c77Sek }
445755434c77Sek (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
445855434c77Sek
445955434c77Sek /* find out if the dataset is mounted */
446055434c77Sek mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
446155434c77Sek
446255434c77Sek /* get the corrupted object's path */
446355434c77Sek (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
446455434c77Sek zc.zc_obj = obj;
446555434c77Sek if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
446655434c77Sek &zc) == 0) {
446755434c77Sek if (mounted) {
446855434c77Sek (void) snprintf(pathname, len, "%s%s", mntpnt,
446955434c77Sek zc.zc_value);
447055434c77Sek } else {
447155434c77Sek (void) snprintf(pathname, len, "%s:%s",
447255434c77Sek dsname, zc.zc_value);
447355434c77Sek }
447455434c77Sek } else {
447555434c77Sek (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
447655434c77Sek }
447755434c77Sek free(mntpnt);
447855434c77Sek }
4479b1b8ab34Slling
4480c4ecba8aSPaul Dagnelie int
zpool_set_bootenv(zpool_handle_t * zhp,const nvlist_t * envmap)448109fcda9fSToomas Soome zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
4482c4ecba8aSPaul Dagnelie {
4483c4ecba8aSPaul Dagnelie int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4484c4ecba8aSPaul Dagnelie if (error != 0) {
4485c4ecba8aSPaul Dagnelie (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4486c4ecba8aSPaul Dagnelie dgettext(TEXT_DOMAIN,
4487c4ecba8aSPaul Dagnelie "error setting bootenv in pool '%s'"), zhp->zpool_name);
4488c4ecba8aSPaul Dagnelie }
4489c4ecba8aSPaul Dagnelie
4490c4ecba8aSPaul Dagnelie return (error);
4491c4ecba8aSPaul Dagnelie }
4492c4ecba8aSPaul Dagnelie
4493c4ecba8aSPaul Dagnelie int
zpool_get_bootenv(zpool_handle_t * zhp,nvlist_t ** nvlp)449409fcda9fSToomas Soome zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
4495c4ecba8aSPaul Dagnelie {
4496c4ecba8aSPaul Dagnelie nvlist_t *nvl;
449709fcda9fSToomas Soome int error;
449809fcda9fSToomas Soome
449909fcda9fSToomas Soome nvl = NULL;
450009fcda9fSToomas Soome error = lzc_get_bootenv(zhp->zpool_name, &nvl);
4501c4ecba8aSPaul Dagnelie if (error != 0) {
4502c4ecba8aSPaul Dagnelie (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4503c4ecba8aSPaul Dagnelie dgettext(TEXT_DOMAIN,
4504c4ecba8aSPaul Dagnelie "error getting bootenv in pool '%s'"), zhp->zpool_name);
450509fcda9fSToomas Soome } else {
450609fcda9fSToomas Soome *nvlp = nvl;
4507c4ecba8aSPaul Dagnelie }
4508c4ecba8aSPaul Dagnelie
450909fcda9fSToomas Soome return (error);
4510c4ecba8aSPaul Dagnelie }
4511c4ecba8aSPaul Dagnelie
451215e6edf1Sgw /*
451315e6edf1Sgw * Read the EFI label from the config, if a label does not exist then
451415e6edf1Sgw * pass back the error to the caller. If the caller has passed a non-NULL
451515e6edf1Sgw * diskaddr argument then we set it to the starting address of the EFI
45167855d95bSToomas Soome * partition. If the caller has passed a non-NULL boolean argument, then
45177855d95bSToomas Soome * we set it to indicate if the disk does have efi system partition.
451815e6edf1Sgw */
451915e6edf1Sgw static int
read_efi_label(nvlist_t * config,diskaddr_t * sb,boolean_t * system)45207855d95bSToomas Soome read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
452115e6edf1Sgw {
452215e6edf1Sgw char *path;
452315e6edf1Sgw int fd;
452415e6edf1Sgw char diskname[MAXPATHLEN];
45257855d95bSToomas Soome boolean_t boot = B_FALSE;
452615e6edf1Sgw int err = -1;
45277855d95bSToomas Soome int slice;
452815e6edf1Sgw
452915e6edf1Sgw if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
453015e6edf1Sgw return (err);
453115e6edf1Sgw
45326401734dSWill Andrews (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
453315e6edf1Sgw strrchr(path, '/'));
453415e6edf1Sgw if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
453515e6edf1Sgw struct dk_gpt *vtoc;
453615e6edf1Sgw
453715e6edf1Sgw if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
45387855d95bSToomas Soome for (slice = 0; slice < vtoc->efi_nparts; slice++) {
45397855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
45407855d95bSToomas Soome boot = B_TRUE;
45417855d95bSToomas Soome if (vtoc->efi_parts[slice].p_tag == V_USR)
45427855d95bSToomas Soome break;
45437855d95bSToomas Soome }
45447855d95bSToomas Soome if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
45457855d95bSToomas Soome *sb = vtoc->efi_parts[slice].p_start;
45467855d95bSToomas Soome if (system != NULL)
45477855d95bSToomas Soome *system = boot;
454815e6edf1Sgw efi_free(vtoc);
454915e6edf1Sgw }
455015e6edf1Sgw (void) close(fd);
455115e6edf1Sgw }
455215e6edf1Sgw return (err);
455315e6edf1Sgw }
455415e6edf1Sgw
45558488aeb5Staylor /*
45568488aeb5Staylor * determine where a partition starts on a disk in the current
45578488aeb5Staylor * configuration
45588488aeb5Staylor */
45598488aeb5Staylor static diskaddr_t
find_start_block(nvlist_t * config)45608488aeb5Staylor find_start_block(nvlist_t *config)
45618488aeb5Staylor {
45628488aeb5Staylor nvlist_t **child;
45638488aeb5Staylor uint_t c, children;
45648488aeb5Staylor diskaddr_t sb = MAXOFFSET_T;
45658488aeb5Staylor uint64_t wholedisk;
45668488aeb5Staylor
45678488aeb5Staylor if (nvlist_lookup_nvlist_array(config,
45688488aeb5Staylor ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
45698488aeb5Staylor if (nvlist_lookup_uint64(config,
45708488aeb5Staylor ZPOOL_CONFIG_WHOLE_DISK,
45718488aeb5Staylor &wholedisk) != 0 || !wholedisk) {
45728488aeb5Staylor return (MAXOFFSET_T);
45738488aeb5Staylor }
45747855d95bSToomas Soome if (read_efi_label(config, &sb, NULL) < 0)
457515e6edf1Sgw sb = MAXOFFSET_T;
45768488aeb5Staylor return (sb);
45778488aeb5Staylor }
45788488aeb5Staylor
45798488aeb5Staylor for (c = 0; c < children; c++) {
45808488aeb5Staylor sb = find_start_block(child[c]);
45818488aeb5Staylor if (sb != MAXOFFSET_T) {
45828488aeb5Staylor return (sb);
45838488aeb5Staylor }
45848488aeb5Staylor }
45858488aeb5Staylor return (MAXOFFSET_T);
45868488aeb5Staylor }
45878488aeb5Staylor
45888488aeb5Staylor /*
45898488aeb5Staylor * Label an individual disk. The name provided is the short name,
45908488aeb5Staylor * stripped of any leading /dev path.
45918488aeb5Staylor */
45928488aeb5Staylor int
zpool_label_disk(libzfs_handle_t * hdl,zpool_handle_t * zhp,const char * name,zpool_boot_label_t boot_type,uint64_t boot_size,int * slice)45937855d95bSToomas Soome zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
45947855d95bSToomas Soome zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
45958488aeb5Staylor {
45968488aeb5Staylor char path[MAXPATHLEN];
45978488aeb5Staylor struct dk_gpt *vtoc;
45988488aeb5Staylor int fd;
45992dd124cdSToomas Soome size_t resv;
46008488aeb5Staylor uint64_t slice_size;
46018488aeb5Staylor diskaddr_t start_block;
46028488aeb5Staylor char errbuf[1024];
46038488aeb5Staylor
4604c6ef114fSmmusante /* prepare an error message just in case */
4605c6ef114fSmmusante (void) snprintf(errbuf, sizeof (errbuf),
4606c6ef114fSmmusante dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4607c6ef114fSmmusante
46088488aeb5Staylor if (zhp) {
46098488aeb5Staylor nvlist_t *nvroot;
46108488aeb5Staylor
46118488aeb5Staylor verify(nvlist_lookup_nvlist(zhp->zpool_config,
46128488aeb5Staylor ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
46138488aeb5Staylor
46148488aeb5Staylor if (zhp->zpool_start_block == 0)
46158488aeb5Staylor start_block = find_start_block(nvroot);
46168488aeb5Staylor else
46178488aeb5Staylor start_block = zhp->zpool_start_block;
46188488aeb5Staylor zhp->zpool_start_block = start_block;
46198488aeb5Staylor } else {
46208488aeb5Staylor /* new pool */
46218488aeb5Staylor start_block = NEW_START_BLOCK;
46228488aeb5Staylor }
46238488aeb5Staylor
46246401734dSWill Andrews (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
46258488aeb5Staylor BACKUP_SLICE);
46268488aeb5Staylor
46278488aeb5Staylor if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
46288488aeb5Staylor /*
46298488aeb5Staylor * This shouldn't happen. We've long since verified that this
46308488aeb5Staylor * is a valid device.
46318488aeb5Staylor */
4632c6ef114fSmmusante zfs_error_aux(hdl,
4633c6ef114fSmmusante dgettext(TEXT_DOMAIN, "unable to open device"));
46348488aeb5Staylor return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
46358488aeb5Staylor }
46368488aeb5Staylor
46378488aeb5Staylor if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
46388488aeb5Staylor /*
46398488aeb5Staylor * The only way this can fail is if we run out of memory, or we
46408488aeb5Staylor * were unable to read the disk's capacity
46418488aeb5Staylor */
46428488aeb5Staylor if (errno == ENOMEM)
46438488aeb5Staylor (void) no_memory(hdl);
46448488aeb5Staylor
46458488aeb5Staylor (void) close(fd);
4646c6ef114fSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4647c6ef114fSmmusante "unable to read disk capacity"), name);
46488488aeb5Staylor
46498488aeb5Staylor return (zfs_error(hdl, EZFS_NOCAP, errbuf));
46508488aeb5Staylor }
46512dd124cdSToomas Soome resv = efi_reserved_sectors(vtoc);
46528488aeb5Staylor
46538488aeb5Staylor /*
46548488aeb5Staylor * Why we use V_USR: V_BACKUP confuses users, and is considered
46558488aeb5Staylor * disposable by some EFI utilities (since EFI doesn't have a backup
46568488aeb5Staylor * slice). V_UNASSIGNED is supposed to be used only for zero size
46578488aeb5Staylor * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
46588488aeb5Staylor * etc. were all pretty specific. V_USR is as close to reality as we
46598488aeb5Staylor * can get, in the absence of V_OTHER.
46608488aeb5Staylor */
46617855d95bSToomas Soome /* first fix the partition start block */
46627855d95bSToomas Soome if (start_block == MAXOFFSET_T)
46637855d95bSToomas Soome start_block = NEW_START_BLOCK;
46648488aeb5Staylor
46657855d95bSToomas Soome /*
46667855d95bSToomas Soome * EFI System partition is using slice 0.
46677855d95bSToomas Soome * ZFS is on slice 1 and slice 8 is reserved.
46687855d95bSToomas Soome * We assume the GPT partition table without system
46697855d95bSToomas Soome * partition has zfs p_start == NEW_START_BLOCK.
46707855d95bSToomas Soome * If start_block != NEW_START_BLOCK, it means we have
46717855d95bSToomas Soome * system partition. Correct solution would be to query/cache vtoc
46727855d95bSToomas Soome * from existing vdev member.
46737855d95bSToomas Soome */
46747855d95bSToomas Soome if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
46757855d95bSToomas Soome if (boot_size % vtoc->efi_lbasize != 0) {
46767855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46777855d95bSToomas Soome "boot partition size must be a multiple of %d"),
46787855d95bSToomas Soome vtoc->efi_lbasize);
46797855d95bSToomas Soome (void) close(fd);
46807855d95bSToomas Soome efi_free(vtoc);
46817855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
46827855d95bSToomas Soome }
46837855d95bSToomas Soome /*
46847855d95bSToomas Soome * System partition size checks.
46857855d95bSToomas Soome * Note the 1MB is quite arbitrary value, since we
46867855d95bSToomas Soome * are creating dedicated pool, it should be enough
46877855d95bSToomas Soome * to hold fat + efi bootloader. May need to be
46887855d95bSToomas Soome * adjusted if the bootloader size will grow.
46897855d95bSToomas Soome */
46907855d95bSToomas Soome if (boot_size < 1024 * 1024) {
46917855d95bSToomas Soome char buf[64];
46927855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf));
46937855d95bSToomas Soome zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
46947855d95bSToomas Soome "Specified size %s for EFI System partition is too "
46957855d95bSToomas Soome "small, the minimum size is 1MB."), buf);
46967855d95bSToomas Soome (void) close(fd);
46977855d95bSToomas Soome efi_free(vtoc);
46987855d95bSToomas Soome return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
46997855d95bSToomas Soome }
47007855d95bSToomas Soome /* 33MB is tested with mkfs -F pcfs */
47017855d95bSToomas Soome if (hdl->libzfs_printerr &&
47027855d95bSToomas Soome ((vtoc->efi_lbasize == 512 &&
47037855d95bSToomas Soome boot_size < 33 * 1024 * 1024) ||
47047855d95bSToomas Soome (vtoc->efi_lbasize == 4096 &&
47057855d95bSToomas Soome boot_size < 256 * 1024 * 1024))) {
47067855d95bSToomas Soome char buf[64];
47077855d95bSToomas Soome zfs_nicenum(boot_size, buf, sizeof (buf));
47087855d95bSToomas Soome (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
47097855d95bSToomas Soome "Warning: EFI System partition size %s is "
47107855d95bSToomas Soome "not allowing to create FAT32 file\nsystem, which "
47117855d95bSToomas Soome "may result in unbootable system.\n"), buf);
47127855d95bSToomas Soome }
47137855d95bSToomas Soome /* Adjust zfs partition start by size of system partition. */
47147855d95bSToomas Soome start_block += boot_size / vtoc->efi_lbasize;
47157855d95bSToomas Soome }
47167855d95bSToomas Soome
47177855d95bSToomas Soome if (start_block == NEW_START_BLOCK) {
47187855d95bSToomas Soome /*
47197855d95bSToomas Soome * Use default layout.
47207855d95bSToomas Soome * ZFS is on slice 0 and slice 8 is reserved.
47217855d95bSToomas Soome */
47227855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1;
47232dd124cdSToomas Soome slice_size -= resv;
47247855d95bSToomas Soome slice_size -= start_block;
47257855d95bSToomas Soome if (slice != NULL)
47267855d95bSToomas Soome *slice = 0;
47277855d95bSToomas Soome
47287855d95bSToomas Soome vtoc->efi_parts[0].p_start = start_block;
47297855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size;
47307855d95bSToomas Soome
47317855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_USR;
47327855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
47337855d95bSToomas Soome
47347855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block;
47357855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv;
47367855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED;
47377855d95bSToomas Soome } else {
47387855d95bSToomas Soome slice_size = start_block - NEW_START_BLOCK;
47397855d95bSToomas Soome vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
47407855d95bSToomas Soome vtoc->efi_parts[0].p_size = slice_size;
47417855d95bSToomas Soome vtoc->efi_parts[0].p_tag = V_SYSTEM;
47427855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
47437855d95bSToomas Soome if (slice != NULL)
47447855d95bSToomas Soome *slice = 1;
47457855d95bSToomas Soome /* prepare slice 1 */
47467855d95bSToomas Soome slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
47477855d95bSToomas Soome slice_size -= resv;
47487855d95bSToomas Soome slice_size -= NEW_START_BLOCK;
47497855d95bSToomas Soome vtoc->efi_parts[1].p_start = start_block;
47507855d95bSToomas Soome vtoc->efi_parts[1].p_size = slice_size;
47517855d95bSToomas Soome vtoc->efi_parts[1].p_tag = V_USR;
47527855d95bSToomas Soome (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
47537855d95bSToomas Soome
47547855d95bSToomas Soome vtoc->efi_parts[8].p_start = slice_size + start_block;
47557855d95bSToomas Soome vtoc->efi_parts[8].p_size = resv;
47567855d95bSToomas Soome vtoc->efi_parts[8].p_tag = V_RESERVED;
47577855d95bSToomas Soome }
47588488aeb5Staylor
47598488aeb5Staylor if (efi_write(fd, vtoc) != 0) {
47608488aeb5Staylor /*
47618488aeb5Staylor * Some block drivers (like pcata) may not support EFI
47628488aeb5Staylor * GPT labels. Print out a helpful error message dir-
47638488aeb5Staylor * ecting the user to manually label the disk and give
47648488aeb5Staylor * a specific slice.
47658488aeb5Staylor */
47668488aeb5Staylor (void) close(fd);
47678488aeb5Staylor efi_free(vtoc);
47688488aeb5Staylor
47698488aeb5Staylor zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4770bbf21555SRichard Lowe "try using fdisk(8) and then provide a specific slice"));
47718488aeb5Staylor return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
47728488aeb5Staylor }
47738488aeb5Staylor
47748488aeb5Staylor (void) close(fd);
47758488aeb5Staylor efi_free(vtoc);
47768488aeb5Staylor return (0);
47778488aeb5Staylor }
4778e7cbe64fSgw
4779e7cbe64fSgw static boolean_t
supported_dump_vdev_type(libzfs_handle_t * hdl,nvlist_t * config,char * errbuf)4780e7cbe64fSgw supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4781e7cbe64fSgw {
4782e7cbe64fSgw char *type;
4783e7cbe64fSgw nvlist_t **child;
4784e7cbe64fSgw uint_t children, c;
4785e7cbe64fSgw
4786e7cbe64fSgw verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4787810e43b2SBill Pijewski if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
478888ecc943SGeorge Wilson strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4789e7cbe64fSgw strcmp(type, VDEV_TYPE_MISSING) == 0) {
4790e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4791e7cbe64fSgw "vdev type '%s' is not supported"), type);
4792e7cbe64fSgw (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4793e7cbe64fSgw return (B_FALSE);
4794e7cbe64fSgw }
4795e7cbe64fSgw if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4796e7cbe64fSgw &child, &children) == 0) {
4797e7cbe64fSgw for (c = 0; c < children; c++) {
4798e7cbe64fSgw if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4799e7cbe64fSgw return (B_FALSE);
4800e7cbe64fSgw }
4801e7cbe64fSgw }
4802e7cbe64fSgw return (B_TRUE);
4803e7cbe64fSgw }
4804e7cbe64fSgw
4805e7cbe64fSgw /*
4806810e43b2SBill Pijewski * Check if this zvol is allowable for use as a dump device; zero if
4807810e43b2SBill Pijewski * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4808810e43b2SBill Pijewski *
4809810e43b2SBill Pijewski * Allowable storage configurations include mirrors, all raidz variants, and
4810810e43b2SBill Pijewski * pools with log, cache, and spare devices. Pools which are backed by files or
4811810e43b2SBill Pijewski * have missing/hole vdevs are not suitable.
4812e7cbe64fSgw */
4813e7cbe64fSgw int
zvol_check_dump_config(char * arg)4814e7cbe64fSgw zvol_check_dump_config(char *arg)
4815e7cbe64fSgw {
4816e7cbe64fSgw zpool_handle_t *zhp = NULL;
4817e7cbe64fSgw nvlist_t *config, *nvroot;
4818e7cbe64fSgw char *p, *volname;
4819e7cbe64fSgw nvlist_t **top;
4820e7cbe64fSgw uint_t toplevels;
4821e7cbe64fSgw libzfs_handle_t *hdl;
4822e7cbe64fSgw char errbuf[1024];
48239adfa60dSMatthew Ahrens char poolname[ZFS_MAX_DATASET_NAME_LEN];
4824e7cbe64fSgw int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4825e7cbe64fSgw int ret = 1;
4826e7cbe64fSgw
4827e7cbe64fSgw if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4828e7cbe64fSgw return (-1);
4829e7cbe64fSgw }
4830e7cbe64fSgw
4831e7cbe64fSgw (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4832e7cbe64fSgw "dump is not supported on device '%s'"), arg);
4833e7cbe64fSgw
4834e7cbe64fSgw if ((hdl = libzfs_init()) == NULL)
4835e7cbe64fSgw return (1);
4836e7cbe64fSgw libzfs_print_on_error(hdl, B_TRUE);
4837e7cbe64fSgw
4838e7cbe64fSgw volname = arg + pathlen;
4839e7cbe64fSgw
4840e7cbe64fSgw /* check the configuration of the pool */
4841e7cbe64fSgw if ((p = strchr(volname, '/')) == NULL) {
4842e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4843e7cbe64fSgw "malformed dataset name"));
4844e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4845e7cbe64fSgw return (1);
48469adfa60dSMatthew Ahrens } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4847e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4848e7cbe64fSgw "dataset name is too long"));
4849e7cbe64fSgw (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4850e7cbe64fSgw return (1);
4851e7cbe64fSgw } else {
4852e7cbe64fSgw (void) strncpy(poolname, volname, p - volname);
4853e7cbe64fSgw poolname[p - volname] = '\0';
4854e7cbe64fSgw }
4855e7cbe64fSgw
4856e7cbe64fSgw if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4857e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4858e7cbe64fSgw "could not open pool '%s'"), poolname);
4859e7cbe64fSgw (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4860e7cbe64fSgw goto out;
4861e7cbe64fSgw }
4862e7cbe64fSgw config = zpool_get_config(zhp, NULL);
4863e7cbe64fSgw if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4864e7cbe64fSgw &nvroot) != 0) {
4865e7cbe64fSgw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4866e7cbe64fSgw "could not obtain vdev configuration for '%s'"), poolname);
4867e7cbe64fSgw (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4868e7cbe64fSgw goto out;
4869e7cbe64fSgw }
4870e7cbe64fSgw
4871e7cbe64fSgw verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4872e7cbe64fSgw &top, &toplevels) == 0);
4873e7cbe64fSgw
4874e7cbe64fSgw if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4875e7cbe64fSgw goto out;
4876e7cbe64fSgw }
4877e7cbe64fSgw ret = 0;
4878e7cbe64fSgw
4879e7cbe64fSgw out:
4880e7cbe64fSgw if (zhp)
4881e7cbe64fSgw zpool_close(zhp);
4882e7cbe64fSgw libzfs_fini(hdl);
4883e7cbe64fSgw return (ret);
4884e7cbe64fSgw }
4885