13b005d3pjd/*
23b005d3pjd * CDDL HEADER START
33b005d3pjd *
43b005d3pjd * The contents of this file are subject to the terms of the
53b005d3pjd * Common Development and Distribution License (the "License").
63b005d3pjd * You may not use this file except in compliance with the License.
73b005d3pjd *
83b005d3pjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93b005d3pjd * or http://www.opensolaris.org/os/licensing.
103b005d3pjd * See the License for the specific language governing permissions
113b005d3pjd * and limitations under the License.
123b005d3pjd *
133b005d3pjd * When distributing Covered Code, include this CDDL HEADER in each
143b005d3pjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153b005d3pjd * If applicable, add the following below this CDDL HEADER, with the
163b005d3pjd * fields enclosed by brackets "[]" replaced with your own identifying
173b005d3pjd * information: Portions Copyright [yyyy] [name of copyright owner]
183b005d3pjd *
193b005d3pjd * CDDL HEADER END
203b005d3pjd */
213b005d3pjd
223b005d3pjd/*
231b03c5bpjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
246da4389mav * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25d04a7f0delphij * Copyright (c) 2013, Joyent, Inc. All rights reserved.
266dcdb61mav * Copyright 2016 Nexenta Systems, Inc.
279765603mav * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
282766e30avg * Copyright (c) 2017 Datto Inc.
298f9d694avg * Copyright (c) 2017, Intel Corporation.
303b005d3pjd */
313b005d3pjd
323b005d3pjd#include <sys/types.h>
333b005d3pjd#include <sys/stat.h>
343b005d3pjd#include <ctype.h>
353b005d3pjd#include <errno.h>
363b005d3pjd#include <devid.h>
373b005d3pjd#include <fcntl.h>
383b005d3pjd#include <libintl.h>
393b005d3pjd#include <stdio.h>
403b005d3pjd#include <stdlib.h>
413b005d3pjd#include <strings.h>
423b005d3pjd#include <unistd.h>
43d506372mm#include <libgen.h>
443b005d3pjd#include <sys/zfs_ioctl.h>
451b03c5bpjd#include <dlfcn.h>
463b005d3pjd
473b005d3pjd#include "zfs_namecheck.h"
483b005d3pjd#include "zfs_prop.h"
493b005d3pjd#include "libzfs_impl.h"
501b03c5bpjd#include "zfs_comutil.h"
51cc61ab2mm#include "zfeature_common.h"
523b005d3pjd
53eb97814mavstatic int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
547b32288mavstatic boolean_t zpool_vdev_is_interior(const char *name);
55bbe899bpjd
561b03c5bpjd#define	BACKUP_SLICE	"s2"
571b03c5bpjd
581b03c5bpjdtypedef struct prop_flags {
591b03c5bpjd	int create:1;	/* Validate property on creation */
601b03c5bpjd	int import:1;	/* Validate property on import */
611b03c5bpjd} prop_flags_t;
62b2946e8mm
63bbe899bpjd/*
64bbe899bpjd * ====================================================================
65bbe899bpjd *   zpool property functions
66bbe899bpjd * ====================================================================
67bbe899bpjd */
68bbe899bpjd
69bbe899bpjdstatic int
70bbe899bpjdzpool_get_all_props(zpool_handle_t *zhp)
71bbe899bpjd{
72bbe899bpjd	zfs_cmd_t zc = { 0 };
73bbe899bpjd	libzfs_handle_t *hdl = zhp->zpool_hdl;
74bbe899bpjd
75bbe899bpjd	(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76bbe899bpjd
77bbe899bpjd	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
78bbe899bpjd		return (-1);
79bbe899bpjd
80bbe899bpjd	while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
81bbe899bpjd		if (errno == ENOMEM) {
82bbe899bpjd			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
83bbe899bpjd				zcmd_free_nvlists(&zc);
84bbe899bpjd				return (-1);
85bbe899bpjd			}
86bbe899bpjd		} else {
87bbe899bpjd			zcmd_free_nvlists(&zc);
88bbe899bpjd			return (-1);
89bbe899bpjd		}
90bbe899bpjd	}
91bbe899bpjd
92bbe899bpjd	if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
93bbe899bpjd		zcmd_free_nvlists(&zc);
94bbe899bpjd		return (-1);
95bbe899bpjd	}
96bbe899bpjd
97bbe899bpjd	zcmd_free_nvlists(&zc);
98bbe899bpjd
99bbe899bpjd	return (0);
100bbe899bpjd}
101bbe899bpjd
102bbe899bpjdstatic int
103bbe899bpjdzpool_props_refresh(zpool_handle_t *zhp)
104bbe899bpjd{
105bbe899bpjd	nvlist_t *old_props;
106bbe899bpjd
107bbe899bpjd	old_props = zhp->zpool_props;
108bbe899bpjd
109bbe899bpjd	if (zpool_get_all_props(zhp) != 0)
110bbe899bpjd		return (-1);
111bbe899bpjd
112bbe899bpjd	nvlist_free(old_props);
113bbe899bpjd	return (0);
114bbe899bpjd}
115bbe899bpjd
116bbe899bpjdstatic char *
117bbe899bpjdzpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
118bbe899bpjd    zprop_source_t *src)
119bbe899bpjd{
120bbe899bpjd	nvlist_t *nv, *nvl;
121bbe899bpjd	uint64_t ival;
122bbe899bpjd	char *value;
123bbe899bpjd	zprop_source_t source;
124bbe899bpjd
125bbe899bpjd	nvl = zhp->zpool_props;
126bbe899bpjd	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
127bbe899bpjd		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
128bbe899bpjd		source = ival;
129bbe899bpjd		verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
130bbe899bpjd	} else {
131bbe899bpjd		source = ZPROP_SRC_DEFAULT;
132bbe899bpjd		if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
133bbe899bpjd			value = "-";
134bbe899bpjd	}
135bbe899bpjd
136bbe899bpjd	if (src)
137bbe899bpjd		*src = source;
138bbe899bpjd
139bbe899bpjd	return (value);
140bbe899bpjd}
141bbe899bpjd
142bbe899bpjduint64_t
143bbe899bpjdzpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
144bbe899bpjd{
145bbe899bpjd	nvlist_t *nv, *nvl;
146bbe899bpjd	uint64_t value;
147bbe899bpjd	zprop_source_t source;
148bbe899bpjd
149bbe899bpjd	if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150bbe899bpjd		/*
151bbe899bpjd		 * zpool_get_all_props() has most likely failed because
152bbe899bpjd		 * the pool is faulted, but if all we need is the top level
153bbe899bpjd		 * vdev's guid then get it from the zhp config nvlist.
154bbe899bpjd		 */
155bbe899bpjd		if ((prop == ZPOOL_PROP_GUID) &&
156bbe899bpjd		    (nvlist_lookup_nvlist(zhp->zpool_config,
157bbe899bpjd		    ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
158bbe899bpjd		    (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
159bbe899bpjd		    == 0)) {
160bbe899bpjd			return (value);
161bbe899bpjd		}
162bbe899bpjd		return (zpool_prop_default_numeric(prop));
163bbe899bpjd	}
164bbe899bpjd
165bbe899bpjd	nvl = zhp->zpool_props;
166bbe899bpjd	if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
167bbe899bpjd		verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
168bbe899bpjd		source = value;
169bbe899bpjd		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
170bbe899bpjd	} else {
171bbe899bpjd		source = ZPROP_SRC_DEFAULT;
172bbe899bpjd		value = zpool_prop_default_numeric(prop);
173bbe899bpjd	}
174bbe899bpjd
175bbe899bpjd	if (src)
176bbe899bpjd		*src = source;
177bbe899bpjd
178bbe899bpjd	return (value);
179bbe899bpjd}
180bbe899bpjd
181bbe899bpjd/*
182bbe899bpjd * Map VDEV STATE to printed strings.
183bbe899bpjd */
184122ada4gibbsconst char *
185bbe899bpjdzpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186bbe899bpjd{
187bbe899bpjd	switch (state) {
188bbe899bpjd	case VDEV_STATE_CLOSED:
189bbe899bpjd	case VDEV_STATE_OFFLINE:
190bbe899bpjd		return (gettext("OFFLINE"));
191bbe899bpjd	case VDEV_STATE_REMOVED:
192bbe899bpjd		return (gettext("REMOVED"));
193bbe899bpjd	case VDEV_STATE_CANT_OPEN:
194bbe899bpjd		if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
195bbe899bpjd			return (gettext("FAULTED"));
1961b03c5bpjd		else if (aux == VDEV_AUX_SPLIT_POOL)
1971b03c5bpjd			return (gettext("SPLIT"));
198bbe899bpjd		else
199bbe899bpjd			return (gettext("UNAVAIL"));
200bbe899bpjd	case VDEV_STATE_FAULTED:
201bbe899bpjd		return (gettext("FAULTED"));
202bbe899bpjd	case VDEV_STATE_DEGRADED:
203bbe899bpjd		return (gettext("DEGRADED"));
204bbe899bpjd	case VDEV_STATE_HEALTHY:
205bbe899bpjd		return (gettext("ONLINE"));
2069765603mav
2079765603mav	default:
2089765603mav		break;
209bbe899bpjd	}
210bbe899bpjd
211bbe899bpjd	return (gettext("UNKNOWN"));
212bbe899bpjd}
213