2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21078266aMarcel Telka
23f9af39bGeorge Wilson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
245dafeeaPavel Zakharov * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25bd0f709Andrew Stormont * Copyright 2015 RackTop Systems.
2646d46cdYuri Pankov * Copyright 2017 Nexenta Systems, Inc.
27fa9e406ahrens */
30fa9e406ahrens * Pool import support functions.
31fa9e406ahrens *
32fa9e406ahrens * To import a pool, we rely on reading the configuration information from the
33fa9e406ahrens * ZFS label of each device.  If we successfully read the label, then we
34fa9e406ahrens * organize the configuration information in the following hierarchy:
35fa9e406ahrens *
36ddfe901sara hartse *	pool guid -> toplevel vdev guid -> label txg
37fa9e406ahrens *
38fa9e406ahrens * Duplicate entries matching this same tuple will be discarded.  Once we have
39fa9e406ahrens * examined every device, we pick the best label txg config for each toplevel
40fa9e406ahrens * vdev.  We then arrange these toplevel vdevs into a complete pool config, and
41fa9e406ahrens * update any paths that have changed.  Finally, we attempt to import the pool
42fa9e406ahrens * using our derived config, and record the results.
43fa9e406ahrens */
454f67d75Eric Taylor#include <ctype.h>
46fa9e406ahrens#include <devid.h>
47fa9e406ahrens#include <dirent.h>
48fa9e406ahrens#include <errno.h>
49fa9e406ahrens#include <libintl.h>
504f67d75Eric Taylor#include <stddef.h>
51fa9e406ahrens#include <stdlib.h>
52fa9e406ahrens#include <string.h>
53fa9e406ahrens#include <sys/stat.h>
54fa9e406ahrens#include <unistd.h>
55fa9e406ahrens#include <fcntl.h>
564f67d75Eric Taylor#include <sys/vtoc.h>
574f67d75Eric Taylor#include <sys/dktp/fdisk.h>
584f67d75Eric Taylor#include <sys/efi_partition.h>
594f67d75Eric Taylor#include <thread_pool.h>
61fa9e406ahrens#include <sys/vdev_impl.h>
62d8ab6e1Don Brady#include <libzutil.h>
64fa9e406ahrens#include "libzfs.h"
65fa9e406ahrens#include "libzfs_impl.h"
68eaca9bbeschrock * Returns true if the named pool matches the given GUID.
69eaca9bbeschrock */
7094de1d4eschrockstatic int
7194de1d4eschrockpool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
7294de1d4eschrock    boolean_t *isactive)
74eaca9bbeschrock	zpool_handle_t *zhp;
75eaca9bbeschrock	uint64_t theguid;
7794de1d4eschrock	if (zpool_open_silent(hdl, name, &zhp) != 0)
7894de1d4eschrock		return (-1);
8094de1d4eschrock	if (zhp == NULL) {
8194de1d4eschrock		*isactive = B_FALSE;
8294de1d4eschrock		return (0);
8394de1d4eschrock	}
85eaca9bbeschrock	verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
86eaca9bbeschrock	    &theguid) == 0);
88eaca9bbeschrock	zpool_close(zhp);
9094de1d4eschrock	*isactive = (theguid == guid);
9194de1d4eschrock	return (0);
942f8aaabeschrockstatic nvlist_t *
952f8aaabeschrockrefresh_config(libzfs_handle_t *hdl, nvlist_t *config)
972f8aaabeschrock	nvlist_t *nvl;
98d8ab6e1Don Brady	zfs_cmd_t zc = {"\0"};
998b65a70Pavel Zakharov	int err, dstbuf_size;
1012f8aaabeschrock	if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
1022f8aaabeschrock		return (NULL);
1048b65a70Pavel Zakharov	dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
1058b65a70Pavel Zakharov
1068b65a70Pavel Zakharov	if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
1072f8aaabeschrock		zcmd_free_nvlists(&zc);
1082f8aaabeschrock		return (NULL);
1092f8aaabeschrock	}
111d8ab6e1Don Brady	while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
1122f8aaabeschrock	    &zc)) != 0 && errno == ENOMEM) {
1132f8aaabeschrock		if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1142f8aaabeschrock			zcmd_free_nvlists(&zc);
1152f8aaabeschrock			return (NULL);
1162f8aaabeschrock		}
1172f8aaabeschrock	}
1192f8aaabeschrock	if (err) {
1202f8aaabeschrock		zcmd_free_nvlists(&zc);
1212f8aaabeschrock		return (NULL);
1222f8aaabeschrock	}
1242f8aaabeschrock	if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
1252f8aaabeschrock		zcmd_free_nvlists(&zc);
1262f8aaabeschrock		return (NULL);
1272f8aaabeschrock	}
1292f8aaabeschrock	zcmd_free_nvlists(&zc);
1302f8aaabeschrock	return (nvl);
133d8ab6e1Don Bradystatic nvlist_t *
134d8ab6e1Don Bradyrefresh_config_libzfs(void *handle, nvlist_t *tryconfig)
13588ecc94George Wilson{
136d8ab6e1Don Brady	return (refresh_config((libzfs_handle_t *)handle, tryconfig));
13788ecc94George Wilson}
13888ecc94George Wilson
139d8ab6e1Don Bradystatic int
140d8ab6e1Don Bradypool_active_libzfs(void *handle, const char *name, uint64_t guid,
141d8ab6e1Don Brady    boolean_t *isactive)
143d8ab6e1Don Brady	return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
146d8ab6e1Don Bradyconst pool_config_ops_t libzfs_config_ops = {
147d8ab6e1Don Brady	.pco_refresh_config = refresh_config_libzfs,
148d8ab6e1Don Brady	.pco_pool_active = pool_active_libzfs,
149d8ab6e1Don Brady};
150d8ab6e1Don Brady
152fa9e406ahrens * Return the offset of the given label.
153fa9e406ahrens */
154fa9e406ahrensstatic uint64_t
155e743726ahrenslabel_offset(uint64_t size, int l)
157e743726ahrens	ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
158fa9e406ahrens	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
159fa9e406ahrens	    0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
1636401734Will Andrews * Given a file descriptor, clear (zero) the label information.
164096d22dEric Schrock */
165096d22dEric Schrockint
166096d22dEric Schrockzpool_clear_label(int fd)
167096d22dEric Schrock{
168096d22dEric Schrock	struct stat64 statbuf;
169096d22dEric Schrock	int l;
170096d22dEric Schrock	vdev_label_t *label;
171096d22dEric Schrock	uint64_t size;
172096d22dEric Schrock
173096d22dEric Schrock	if (fstat64(fd, &statbuf) == -1)
174096d22dEric Schrock		return (0);
175096d22dEric Schrock	size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
176096d22dEric Schrock
177096d22dEric Schrock	if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
178096d22dEric Schrock		return (-1);
179096d22dEric Schrock
180096d22dEric Schrock	for (l = 0; l < VDEV_LABELS; l++) {
181096d22dEric Schrock		if (pwrite64(fd, label, sizeof (vdev_label_t),
182078266aMarcel Telka		    label_offset(size, l)) != sizeof (vdev_label_t)) {
183078266aMarcel Telka			free(label);
184096d22dEric Schrock			return (-1);
185078266aMarcel Telka		}
186096d22dEric Schrock	}
187096d22dEric Schrock
188096d22dEric Schrock	free(label);
189096d22dEric Schrock	return (0);
190096d22dEric Schrock}
191096d22dEric Schrock
193fa9e406ahrensfind_guid(nvlist_t *nv, uint64_t guid)
195fa9e406ahrens	uint64_t tmp;
196fa9e406ahrens	nvlist_t **child;
197fa9e406ahrens	uint_t c, children;
199fa9e406ahrens	verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
200fa9e406ahrens	if (tmp == guid)
20199653d4eschrock		return (B_TRUE);
203fa9e406ahrens	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
204fa9e406ahrens	    &child, &children) == 0) {
205fa9e406ahrens		for (c = 0; c < children; c++)
206fa9e406ahrens			if (find_guid(child[c], guid))
20799653d4eschrock				return (B_TRUE);
20899653d4eschrock	}
21099653d4eschrock	return (B_FALSE);
213fa94a07brendantypedef struct aux_cbdata {
214fa94a07brendan	const char	*cb_type;
21599653d4eschrock	uint64_t	cb_guid;
21699653d4eschrock	zpool_handle_t	*cb_zhp;
217fa94a07brendan} aux_cbdata_t;
21999653d4eschrockstatic int
220fa94a07brendanfind_aux(zpool_handle_t *zhp, void *data)
222fa94a07brendan	aux_cbdata_t *cbp = data;
223fa94a07brendan	nvlist_t **list;
224fa94a07brendan	uint_t i, count;
22599653d4eschrock	uint64_t guid;
22699653d4eschrock	nvlist_t *nvroot;
22899653d4eschrock	verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
22999653d4eschrock	    &nvroot) == 0);
231fa94a07brendan	if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
232fa94a07brendan	    &list, &count) == 0) {
233fa94a07brendan		for (i = 0; i < count; i++) {
234fa94a07brendan			verify(nvlist_lookup_uint64(list[i],
23599653d4eschrock			    ZPOOL_CONFIG_GUID, &guid) == 0);
23699653d4eschrock			if (guid == cbp->cb_guid) {
23799653d4eschrock				cbp->cb_zhp = zhp;
23899653d4eschrock				return (1);
23999653d4eschrock			}
24099653d4eschrock		}
241fa9e406ahrens	}
24399653d4eschrock	zpool_close(zhp);
24499653d4eschrock	return (0);
24899653d4eschrock * Determines if the pool is in use.  If so, it returns true and the state of
249fa9e406ahrens * the pool as well as the name of the pool.  Both strings are allocated and
250fa9e406ahrens * must be freed by the caller.
251fa9e406ahrens */
25399653d4eschrockzpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
25499653d4eschrock    boolean_t *inuse)
256fa9e406ahrens	nvlist_t *config;
257fa9e406ahrens	char *name;
25899653d4eschrock	boolean_t ret;
259fa9e406ahrens	uint64_t guid, vdev_guid;
260fa9e406ahrens	zpool_handle_t *zhp;
261fa9e406ahrens	nvlist_t *pool_config;
26239c2341eschrock	uint64_t stateval, isspare;
263fa94a07brendan	aux_cbdata_t cb = { 0 };
26494de1d4eschrock	boolean_t isactive;
26699653d4eschrock	*inuse = B_FALSE;
268d8ab6e1Don Brady	if (zpool_read_label(fd, &config, NULL) != 0 && errno == ENOMEM) {
26999653d4eschrock		(void) no_memory(hdl);
27099653d4eschrock		return (-1);
27199653d4eschrock	}
27399653d4eschrock	if (config == NULL)
27499653d4eschrock		return (0);
276fa9e406ahrens	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
27746a2abfeschrock	    &stateval) == 0);
278fa9e406ahrens	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
279fa9e406ahrens	    &vdev_guid) == 0);
281fa94a07brendan	if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
28299653d4eschrock		verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
28399653d4eschrock		    &name) == 0);
28499653d4eschrock		verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
28599653d4eschrock		    &guid) == 0);
28699653d4eschrock	}
28846a2abfeschrock	switch (stateval) {
289fa9e406ahrens	case POOL_STATE_EXPORTED:
290f9af39bGeorge Wilson		/*
291f9af39bGeorge Wilson		 * A pool with an exported state may in fact be imported
292f9af39bGeorge Wilson		 * read-only, so check the in-core state to see if it's
293f9af39bGeorge Wilson		 * active and imported read-only.  If it is, set
294f9af39bGeorge Wilson		 * its state to active.
295f9af39bGeorge Wilson		 */
296f9af39bGeorge Wilson		if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
297fb13f48Josef 'Jeff' Sipek		    (zhp = zpool_open_canfail(hdl, name)) != NULL) {
298fb13f48Josef 'Jeff' Sipek			if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
299fb13f48Josef 'Jeff' Sipek				stateval = POOL_STATE_ACTIVE;
300fb13f48Josef 'Jeff' Sipek
301fb13f48Josef 'Jeff' Sipek			/*
302fb13f48Josef 'Jeff' Sipek			 * All we needed the zpool handle for is the
303fb13f48Josef 'Jeff' Sipek			 * readonly prop check.
304fb13f48Josef 'Jeff' Sipek			 */
305fb13f48Josef 'Jeff' Sipek			zpool_close(zhp);
306fb13f48Josef 'Jeff' Sipek		}
307f9af39bGeorge Wilson
30899653d4eschrock		ret = B_TRUE;
309fa9e406ahrens		break;
311fa9e406ahrens	case POOL_STATE_ACTIVE:
312fa9e406ahrens		/*
313fa9e406ahrens		 * For an active pool, we have to determine if it's really part
314eaca9bbeschrock		 * of a currently active pool (in which case the pool will exist
315eaca9bbeschrock		 * and the guid will be the same), or whether it's part of an
316eaca9bbeschrock		 * active pool that was disconnected without being explicitly
317eaca9bbeschrock		 * exported.
318fa9e406ahrens		 */
31994de1d4eschrock		if (pool_active(hdl, name, guid, &isactive) != 0) {
32094de1d4eschrock			nvlist_free(config);
32194de1d4eschrock			return (-1);
32294de1d4eschrock		}
32494de1d4eschrock		if (isactive) {
325fa9e406ahrens			/*
326fa9e406ahrens			 * Because the device may have been removed while
327fa9e406ahrens			 * offlined, we only report it as active if the vdev is
328fa9e406ahrens			 * still present in the config.  Otherwise, pretend like
329fa9e406ahrens			 * it's not in use.
330fa9e406ahrens			 */
33199653d4eschrock			if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
332088e9d4eschrock			    (pool_config = zpool_get_config(zhp, NULL))
333088e9d4eschrock			    != NULL) {
334fa9e406ahrens				nvlist_t *nvroot;
336fa9e406ahrens				verify(nvlist_lookup_nvlist(pool_config,
337fa9e406ahrens				    ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
33846a2abfeschrock				ret = find_guid(nvroot, vdev_guid);
339fa9e406ahrens			} else {
34099653d4eschrock				ret = B_FALSE;
341fa9e406ahrens			}
34339c2341eschrock			/*
34439c2341eschrock			 * If this is an active spare within another pool, we
34539c2341eschrock			 * treat it like an unused hot spare.  This allows the
34639c2341eschrock			 * user to create a pool with a hot spare that currently
34739c2341eschrock			 * in use within another pool.  Since we return B_TRUE,
34839c2341eschrock			 * libdiskmgt will continue to prevent generic consumers
34939c2341eschrock			 * from using the device.
35039c2341eschrock			 */
35139c2341eschrock			if (ret && nvlist_lookup_uint64(config,
35239c2341eschrock			    ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
35339c2341eschrock				stateval = POOL_STATE_SPARE;
35599653d4eschrock			if (zhp != NULL)
35699653d4eschrock				zpool_close(zhp);
357fa9e406ahrens		} else {
35846a2abfeschrock			stateval = POOL_STATE_POTENTIALLY_ACTIVE;
35999653d4eschrock			ret = B_TRUE;
36099653d4eschrock		}
36199653d4eschrock		break;
36399653d4eschrock	case POOL_STATE_SPARE:
36499653d4eschrock		/*
36599653d4eschrock		 * For a hot spare, it can be either definitively in use, or
36699653d4eschrock		 * potentially active.  To determine if it's in use, we iterate
36799653d4eschrock		 * over all pools in the system and search for one with a spare
36899653d4eschrock		 * with a matching guid.
36999653d4eschrock		 *
37099653d4eschrock		 * Due to the shared nature of spares, we don't actually report
37199653d4eschrock		 * the potentially active case as in use.  This means the user
37299653d4eschrock		 * can freely create pools on the hot spares of exported pools,
37399653d4eschrock		 * but to do otherwise makes the resulting code complicated, and
37499653d4eschrock		 * we end up having to deal with this case anyway.
37599653d4eschrock		 */
37699653d4eschrock		cb.cb_zhp = NULL;
37799653d4eschrock		cb.cb_guid = vdev_guid;
378fa94a07brendan		cb.cb_type = ZPOOL_CONFIG_SPARES;
379fa94a07brendan		if (zpool_iter(hdl, find_aux, &cb) == 1) {
380fa94a07brendan			name = (char *)zpool_get_name(cb.cb_zhp);
381078266aMarcel Telka			ret = B_TRUE;
382fa94a07brendan		} else {
383078266aMarcel Telka			ret = B_FALSE;
384fa94a07brendan		}
385fa94a07brendan		break;
387fa94a07brendan	case POOL_STATE_L2CACHE:
389fa94a07brendan		/*
390fa94a07brendan		 * Check if any pool is currently using this l2cache device.
391fa94a07brendan		 */
392fa94a07brendan		cb.cb_zhp = NULL;
393fa94a07brendan		cb.cb_guid = vdev_guid;
394fa94a07brendan		cb.cb_type = ZPOOL_CONFIG_L2CACHE;
395fa94a07brendan		if (zpool_iter(hdl, find_aux, &cb) == 1) {
39699653d4eschrock			name = (char *)zpool_get_name(cb.cb_zhp);
397078266aMarcel Telka			ret = B_TRUE;
39899653d4eschrock		} else {
399078266aMarcel Telka			ret = B_FALSE;
400fa9e406ahrens		}
401fa9e406ahrens		break;
403fa9e406ahrens	default:
40499653d4eschrock		ret = B_FALSE;
405fa9e406ahrens	}
40846a2abfeschrock	if (ret) {
40999653d4eschrock		if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
410c6765aaeschrock			if (cb.cb_zhp)
411c6765aaeschrock				zpool_close(cb.cb_zhp);
41299653d4eschrock			nvlist_free(config);
41399653d4eschrock			return (-1);
41499653d4eschrock		}
41546a2abfeschrock		*state = (pool_state_t)stateval;
41646a2abfeschrock	}
41899653d4eschrock	if (cb.cb_zhp)
41999653d4eschrock		zpool_close(cb.cb_zhp);
421fa9e406ahrens	nvlist_free(config);
42299653d4eschrock	*inuse = ret;
42399653d4eschrock	return (0);