1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51544Seschrock * Common Development and Distribution License (the "License").
61544Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
22*13049SGeorge.Wilson@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
25789Sahrens /*
26789Sahrens * Pool import support functions.
27789Sahrens *
28789Sahrens * To import a pool, we rely on reading the configuration information from the
29789Sahrens * ZFS label of each device. If we successfully read the label, then we
30789Sahrens * organize the configuration information in the following hierarchy:
31789Sahrens *
32789Sahrens * pool guid -> toplevel vdev guid -> label txg
33789Sahrens *
34789Sahrens * Duplicate entries matching this same tuple will be discarded. Once we have
35789Sahrens * examined every device, we pick the best label txg config for each toplevel
36789Sahrens * vdev. We then arrange these toplevel vdevs into a complete pool config, and
37789Sahrens * update any paths that have changed. Finally, we attempt to import the pool
38789Sahrens * using our derived config, and record the results.
39789Sahrens */
40789Sahrens
4110980SEric.Taylor@Sun.COM #include <ctype.h>
42789Sahrens #include <devid.h>
43789Sahrens #include <dirent.h>
44789Sahrens #include <errno.h>
45789Sahrens #include <libintl.h>
4610980SEric.Taylor@Sun.COM #include <stddef.h>
47789Sahrens #include <stdlib.h>
48789Sahrens #include <string.h>
49789Sahrens #include <sys/stat.h>
50789Sahrens #include <unistd.h>
51789Sahrens #include <fcntl.h>
5210980SEric.Taylor@Sun.COM #include <sys/vtoc.h>
5310980SEric.Taylor@Sun.COM #include <sys/dktp/fdisk.h>
5410980SEric.Taylor@Sun.COM #include <sys/efi_partition.h>
5510980SEric.Taylor@Sun.COM #include <thread_pool.h>
56789Sahrens
57789Sahrens #include <sys/vdev_impl.h>
58789Sahrens
59789Sahrens #include "libzfs.h"
60789Sahrens #include "libzfs_impl.h"
61789Sahrens
62789Sahrens /*
63789Sahrens * Intermediate structures used to gather configuration information.
64789Sahrens */
65789Sahrens typedef struct config_entry {
66789Sahrens uint64_t ce_txg;
67789Sahrens nvlist_t *ce_config;
68789Sahrens struct config_entry *ce_next;
69789Sahrens } config_entry_t;
70789Sahrens
71789Sahrens typedef struct vdev_entry {
72789Sahrens uint64_t ve_guid;
73789Sahrens config_entry_t *ve_configs;
74789Sahrens struct vdev_entry *ve_next;
75789Sahrens } vdev_entry_t;
76789Sahrens
77789Sahrens typedef struct pool_entry {
78789Sahrens uint64_t pe_guid;
79789Sahrens vdev_entry_t *pe_vdevs;
80789Sahrens struct pool_entry *pe_next;
81789Sahrens } pool_entry_t;
82789Sahrens
83789Sahrens typedef struct name_entry {
842082Seschrock char *ne_name;
85789Sahrens uint64_t ne_guid;
86789Sahrens struct name_entry *ne_next;
87789Sahrens } name_entry_t;
88789Sahrens
89789Sahrens typedef struct pool_list {
90789Sahrens pool_entry_t *pools;
91789Sahrens name_entry_t *names;
92789Sahrens } pool_list_t;
93789Sahrens
94789Sahrens static char *
get_devid(const char * path)95789Sahrens get_devid(const char *path)
96789Sahrens {
97789Sahrens int fd;
98789Sahrens ddi_devid_t devid;
99789Sahrens char *minor, *ret;
100789Sahrens
101789Sahrens if ((fd = open(path, O_RDONLY)) < 0)
102789Sahrens return (NULL);
103789Sahrens
104789Sahrens minor = NULL;
105789Sahrens ret = NULL;
106789Sahrens if (devid_get(fd, &devid) == 0) {
107789Sahrens if (devid_get_minor_name(fd, &minor) == 0)
108789Sahrens ret = devid_str_encode(devid, minor);
109789Sahrens if (minor != NULL)
110789Sahrens devid_str_free(minor);
111789Sahrens devid_free(devid);
112789Sahrens }
1131354Seschrock (void) close(fd);
114789Sahrens
115789Sahrens return (ret);
116789Sahrens }
117789Sahrens
118789Sahrens
119789Sahrens /*
120789Sahrens * Go through and fix up any path and/or devid information for the given vdev
121789Sahrens * configuration.
122789Sahrens */
1232082Seschrock static int
fix_paths(nvlist_t * nv,name_entry_t * names)124789Sahrens fix_paths(nvlist_t *nv, name_entry_t *names)
125789Sahrens {
126789Sahrens nvlist_t **child;
127789Sahrens uint_t c, children;
128789Sahrens uint64_t guid;
1291354Seschrock name_entry_t *ne, *best;
1301354Seschrock char *path, *devid;
1311354Seschrock int matched;
132789Sahrens
133789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
134789Sahrens &child, &children) == 0) {
135789Sahrens for (c = 0; c < children; c++)
1362082Seschrock if (fix_paths(child[c], names) != 0)
1372082Seschrock return (-1);
1382082Seschrock return (0);
139789Sahrens }
140789Sahrens
141789Sahrens /*
142789Sahrens * This is a leaf (file or disk) vdev. In either case, go through
143789Sahrens * the name list and see if we find a matching guid. If so, replace
144789Sahrens * the path and see if we can calculate a new devid.
1451354Seschrock *
1461354Seschrock * There may be multiple names associated with a particular guid, in
1471354Seschrock * which case we have overlapping slices or multiple paths to the same
1481354Seschrock * disk. If this is the case, then we want to pick the path that is
1491354Seschrock * the most similar to the original, where "most similar" is the number
1501354Seschrock * of matching characters starting from the end of the path. This will
1511354Seschrock * preserve slice numbers even if the disks have been reorganized, and
1521354Seschrock * will also catch preferred disk names if multiple paths exist.
153789Sahrens */
154789Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
1551354Seschrock if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
1561354Seschrock path = NULL;
157789Sahrens
1581354Seschrock matched = 0;
1591354Seschrock best = NULL;
1601354Seschrock for (ne = names; ne != NULL; ne = ne->ne_next) {
1611354Seschrock if (ne->ne_guid == guid) {
1621354Seschrock const char *src, *dst;
1631354Seschrock int count;
1641354Seschrock
1651354Seschrock if (path == NULL) {
1661354Seschrock best = ne;
1671354Seschrock break;
1681354Seschrock }
169789Sahrens
1701354Seschrock src = ne->ne_name + strlen(ne->ne_name) - 1;
1711354Seschrock dst = path + strlen(path) - 1;
1721354Seschrock for (count = 0; src >= ne->ne_name && dst >= path;
1731354Seschrock src--, dst--, count++)
1741354Seschrock if (*src != *dst)
1751354Seschrock break;
1761354Seschrock
1771354Seschrock /*
1781354Seschrock * At this point, 'count' is the number of characters
1791354Seschrock * matched from the end.
1801354Seschrock */
1811354Seschrock if (count > matched || best == NULL) {
1821354Seschrock best = ne;
1831354Seschrock matched = count;
1841354Seschrock }
1851354Seschrock }
1861354Seschrock }
1871354Seschrock
1881354Seschrock if (best == NULL)
1892082Seschrock return (0);
190789Sahrens
1912082Seschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
1922082Seschrock return (-1);
193789Sahrens
1941354Seschrock if ((devid = get_devid(best->ne_name)) == NULL) {
195789Sahrens (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
196789Sahrens } else {
1972082Seschrock if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
1982082Seschrock return (-1);
199789Sahrens devid_str_free(devid);
200789Sahrens }
2012082Seschrock
2022082Seschrock return (0);
203789Sahrens }
204789Sahrens
205789Sahrens /*
206789Sahrens * Add the given configuration to the list of known devices.
207789Sahrens */
2082082Seschrock static int
add_config(libzfs_handle_t * hdl,pool_list_t * pl,const char * path,nvlist_t * config)2092082Seschrock add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
2102082Seschrock nvlist_t *config)
211789Sahrens {
2122082Seschrock uint64_t pool_guid, vdev_guid, top_guid, txg, state;
213789Sahrens pool_entry_t *pe;
214789Sahrens vdev_entry_t *ve;
215789Sahrens config_entry_t *ce;
216789Sahrens name_entry_t *ne;
217789Sahrens
218789Sahrens /*
2195450Sbrendan * If this is a hot spare not currently in use or level 2 cache
2205450Sbrendan * device, add it to the list of names to translate, but don't do
2215450Sbrendan * anything else.
2222082Seschrock */
2232082Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2245450Sbrendan &state) == 0 &&
2255450Sbrendan (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
2262082Seschrock nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
2272082Seschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
2284055Seschrock return (-1);
2292082Seschrock
2302082Seschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
2312082Seschrock free(ne);
2322082Seschrock return (-1);
2332082Seschrock }
2342082Seschrock ne->ne_guid = vdev_guid;
2352082Seschrock ne->ne_next = pl->names;
2362082Seschrock pl->names = ne;
2372082Seschrock return (0);
2382082Seschrock }
2392082Seschrock
2402082Seschrock /*
241789Sahrens * If we have a valid config but cannot read any of these fields, then
242789Sahrens * it means we have a half-initialized label. In vdev_label_init()
243789Sahrens * we write a label with txg == 0 so that we can identify the device
244789Sahrens * in case the user refers to the same disk later on. If we fail to
245789Sahrens * create the pool, we'll be left with a label in this state
246789Sahrens * which should not be considered part of a valid pool.
247789Sahrens */
248789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
249789Sahrens &pool_guid) != 0 ||
250789Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
251789Sahrens &vdev_guid) != 0 ||
252789Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
253789Sahrens &top_guid) != 0 ||
254789Sahrens nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
255789Sahrens &txg) != 0 || txg == 0) {
256789Sahrens nvlist_free(config);
2572082Seschrock return (0);
258789Sahrens }
259789Sahrens
260789Sahrens /*
261789Sahrens * First, see if we know about this pool. If not, then add it to the
262789Sahrens * list of known pools.
263789Sahrens */
264789Sahrens for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
265789Sahrens if (pe->pe_guid == pool_guid)
266789Sahrens break;
267789Sahrens }
268789Sahrens
269789Sahrens if (pe == NULL) {
2702082Seschrock if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
2712082Seschrock nvlist_free(config);
2722082Seschrock return (-1);
2732082Seschrock }
274789Sahrens pe->pe_guid = pool_guid;
275789Sahrens pe->pe_next = pl->pools;
276789Sahrens pl->pools = pe;
277789Sahrens }
278789Sahrens
279789Sahrens /*
280789Sahrens * Second, see if we know about this toplevel vdev. Add it if its
281789Sahrens * missing.
282789Sahrens */
283789Sahrens for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
284789Sahrens if (ve->ve_guid == top_guid)
285789Sahrens break;
286789Sahrens }
287789Sahrens
288789Sahrens if (ve == NULL) {
2892082Seschrock if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
2902082Seschrock nvlist_free(config);
2912082Seschrock return (-1);
2922082Seschrock }
293789Sahrens ve->ve_guid = top_guid;
294789Sahrens ve->ve_next = pe->pe_vdevs;
295789Sahrens pe->pe_vdevs = ve;
296789Sahrens }
297789Sahrens
298789Sahrens /*
299789Sahrens * Third, see if we have a config with a matching transaction group. If
300789Sahrens * so, then we do nothing. Otherwise, add it to the list of known
301789Sahrens * configs.
302789Sahrens */
303789Sahrens for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
304789Sahrens if (ce->ce_txg == txg)
305789Sahrens break;
306789Sahrens }
307789Sahrens
308789Sahrens if (ce == NULL) {
3092082Seschrock if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
3102082Seschrock nvlist_free(config);
3112082Seschrock return (-1);
3122082Seschrock }
313789Sahrens ce->ce_txg = txg;
314789Sahrens ce->ce_config = config;
315789Sahrens ce->ce_next = ve->ve_configs;
316789Sahrens ve->ve_configs = ce;
317789Sahrens } else {
318789Sahrens nvlist_free(config);
319789Sahrens }
320789Sahrens
321789Sahrens /*
322789Sahrens * At this point we've successfully added our config to the list of
323789Sahrens * known configs. The last thing to do is add the vdev guid -> path
324789Sahrens * mappings so that we can fix up the configuration as necessary before
325789Sahrens * doing the import.
326789Sahrens */
3272082Seschrock if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
3282082Seschrock return (-1);
329789Sahrens
3302082Seschrock if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
3312082Seschrock free(ne);
3322082Seschrock return (-1);
3332082Seschrock }
3342082Seschrock
335789Sahrens ne->ne_guid = vdev_guid;
336789Sahrens ne->ne_next = pl->names;
337789Sahrens pl->names = ne;
3382082Seschrock
3392082Seschrock return (0);
340789Sahrens }
341789Sahrens
342789Sahrens /*
3431760Seschrock * Returns true if the named pool matches the given GUID.
3441760Seschrock */
3452142Seschrock static int
pool_active(libzfs_handle_t * hdl,const char * name,uint64_t guid,boolean_t * isactive)3462142Seschrock pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
3472142Seschrock boolean_t *isactive)
3481760Seschrock {
3491760Seschrock zpool_handle_t *zhp;
3501760Seschrock uint64_t theguid;
3511760Seschrock
3522142Seschrock if (zpool_open_silent(hdl, name, &zhp) != 0)
3532142Seschrock return (-1);
3542142Seschrock
3552142Seschrock if (zhp == NULL) {
3562142Seschrock *isactive = B_FALSE;
3572142Seschrock return (0);
3582142Seschrock }
3591760Seschrock
3601760Seschrock verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
3611760Seschrock &theguid) == 0);
3621760Seschrock
3631760Seschrock zpool_close(zhp);
3641760Seschrock
3652142Seschrock *isactive = (theguid == guid);
3662142Seschrock return (0);
3671760Seschrock }
3681760Seschrock
3695363Seschrock static nvlist_t *
refresh_config(libzfs_handle_t * hdl,nvlist_t * config)3705363Seschrock refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
3715363Seschrock {
3725363Seschrock nvlist_t *nvl;
3735363Seschrock zfs_cmd_t zc = { 0 };
3745363Seschrock int err;
3755363Seschrock
3765363Seschrock if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
3775363Seschrock return (NULL);
3785363Seschrock
3795363Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc,
3805363Seschrock zc.zc_nvlist_conf_size * 2) != 0) {
3815363Seschrock zcmd_free_nvlists(&zc);
3825363Seschrock return (NULL);
3835363Seschrock }
3845363Seschrock
3855363Seschrock while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
3865363Seschrock &zc)) != 0 && errno == ENOMEM) {
3875363Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3885363Seschrock zcmd_free_nvlists(&zc);
3895363Seschrock return (NULL);
3905363Seschrock }
3915363Seschrock }
3925363Seschrock
3935363Seschrock if (err) {
3945363Seschrock zcmd_free_nvlists(&zc);
3955363Seschrock return (NULL);
3965363Seschrock }
3975363Seschrock
3985363Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
3995363Seschrock zcmd_free_nvlists(&zc);
4005363Seschrock return (NULL);
4015363Seschrock }
4025363Seschrock
4035363Seschrock zcmd_free_nvlists(&zc);
4045363Seschrock return (nvl);
4055363Seschrock }
4065363Seschrock
4071760Seschrock /*
40810594SGeorge.Wilson@Sun.COM * Determine if the vdev id is a hole in the namespace.
40910594SGeorge.Wilson@Sun.COM */
41010594SGeorge.Wilson@Sun.COM boolean_t
vdev_is_hole(uint64_t * hole_array,uint_t holes,uint_t id)41110594SGeorge.Wilson@Sun.COM vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
41210594SGeorge.Wilson@Sun.COM {
41310594SGeorge.Wilson@Sun.COM for (int c = 0; c < holes; c++) {
41410594SGeorge.Wilson@Sun.COM
41510594SGeorge.Wilson@Sun.COM /* Top-level is a hole */
41610594SGeorge.Wilson@Sun.COM if (hole_array[c] == id)
41710594SGeorge.Wilson@Sun.COM return (B_TRUE);
41810594SGeorge.Wilson@Sun.COM }
41910594SGeorge.Wilson@Sun.COM return (B_FALSE);
42010594SGeorge.Wilson@Sun.COM }
42110594SGeorge.Wilson@Sun.COM
42210594SGeorge.Wilson@Sun.COM /*
423789Sahrens * Convert our list of pools into the definitive set of configurations. We
424789Sahrens * start by picking the best config for each toplevel vdev. Once that's done,
425789Sahrens * we assemble the toplevel vdevs into a full config for the pool. We make a
426789Sahrens * pass to fix up any incorrect paths, and then add it to the main list to
427789Sahrens * return to the user.
428789Sahrens */
429789Sahrens static nvlist_t *
get_configs(libzfs_handle_t * hdl,pool_list_t * pl,boolean_t active_ok)4305994Sck153898 get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
431789Sahrens {
4322082Seschrock pool_entry_t *pe;
4332082Seschrock vdev_entry_t *ve;
4342082Seschrock config_entry_t *ce;
4352082Seschrock nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
4365450Sbrendan nvlist_t **spares, **l2cache;
4375450Sbrendan uint_t i, nspares, nl2cache;
4382082Seschrock boolean_t config_seen;
439789Sahrens uint64_t best_txg;
4403975Sek110237 char *name, *hostname;
4412082Seschrock uint64_t version, guid;
4422082Seschrock uint_t children = 0;
4432082Seschrock nvlist_t **child = NULL;
44410594SGeorge.Wilson@Sun.COM uint_t holes;
44510594SGeorge.Wilson@Sun.COM uint64_t *hole_array, max_id;
4462082Seschrock uint_t c;
4472142Seschrock boolean_t isactive;
4483975Sek110237 uint64_t hostid;
4495363Seschrock nvlist_t *nvl;
4506807Sck153898 boolean_t found_one = B_FALSE;
45110594SGeorge.Wilson@Sun.COM boolean_t valid_top_config = B_FALSE;
452789Sahrens
4532082Seschrock if (nvlist_alloc(&ret, 0, 0) != 0)
4542082Seschrock goto nomem;
4552082Seschrock
4562082Seschrock for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
45710594SGeorge.Wilson@Sun.COM uint64_t id, max_txg = 0;
458789Sahrens
4592082Seschrock if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
4602082Seschrock goto nomem;
4612082Seschrock config_seen = B_FALSE;
462789Sahrens
463789Sahrens /*
464789Sahrens * Iterate over all toplevel vdevs. Grab the pool configuration
465789Sahrens * from the first one we find, and then go through the rest and
466789Sahrens * add them as necessary to the 'vdevs' member of the config.
467789Sahrens */
4682082Seschrock for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
469789Sahrens
470789Sahrens /*
471789Sahrens * Determine the best configuration for this vdev by
472789Sahrens * selecting the config with the latest transaction
473789Sahrens * group.
474789Sahrens */
475789Sahrens best_txg = 0;
476789Sahrens for (ce = ve->ve_configs; ce != NULL;
477789Sahrens ce = ce->ce_next) {
478789Sahrens
4792082Seschrock if (ce->ce_txg > best_txg) {
480789Sahrens tmp = ce->ce_config;
4812082Seschrock best_txg = ce->ce_txg;
4822082Seschrock }
483789Sahrens }
484789Sahrens
48510594SGeorge.Wilson@Sun.COM /*
48610594SGeorge.Wilson@Sun.COM * We rely on the fact that the max txg for the
48710594SGeorge.Wilson@Sun.COM * pool will contain the most up-to-date information
48810594SGeorge.Wilson@Sun.COM * about the valid top-levels in the vdev namespace.
48910594SGeorge.Wilson@Sun.COM */
49010594SGeorge.Wilson@Sun.COM if (best_txg > max_txg) {
49110594SGeorge.Wilson@Sun.COM (void) nvlist_remove(config,
49210594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_VDEV_CHILDREN,
49310594SGeorge.Wilson@Sun.COM DATA_TYPE_UINT64);
49410594SGeorge.Wilson@Sun.COM (void) nvlist_remove(config,
49510594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_HOLE_ARRAY,
49610594SGeorge.Wilson@Sun.COM DATA_TYPE_UINT64_ARRAY);
49710594SGeorge.Wilson@Sun.COM
49810594SGeorge.Wilson@Sun.COM max_txg = best_txg;
49910594SGeorge.Wilson@Sun.COM hole_array = NULL;
50010594SGeorge.Wilson@Sun.COM holes = 0;
50110594SGeorge.Wilson@Sun.COM max_id = 0;
50210594SGeorge.Wilson@Sun.COM valid_top_config = B_FALSE;
50310594SGeorge.Wilson@Sun.COM
50410594SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64(tmp,
50510594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
50610594SGeorge.Wilson@Sun.COM verify(nvlist_add_uint64(config,
50710594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_VDEV_CHILDREN,
50810594SGeorge.Wilson@Sun.COM max_id) == 0);
50910594SGeorge.Wilson@Sun.COM valid_top_config = B_TRUE;
51010594SGeorge.Wilson@Sun.COM }
51110594SGeorge.Wilson@Sun.COM
51210594SGeorge.Wilson@Sun.COM if (nvlist_lookup_uint64_array(tmp,
51310594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
51410594SGeorge.Wilson@Sun.COM &holes) == 0) {
51510594SGeorge.Wilson@Sun.COM verify(nvlist_add_uint64_array(config,
51610594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_HOLE_ARRAY,
51710594SGeorge.Wilson@Sun.COM hole_array, holes) == 0);
51810594SGeorge.Wilson@Sun.COM }
51910594SGeorge.Wilson@Sun.COM }
52010594SGeorge.Wilson@Sun.COM
521789Sahrens if (!config_seen) {
522789Sahrens /*
523789Sahrens * Copy the relevant pieces of data to the pool
524789Sahrens * configuration:
525789Sahrens *
5262082Seschrock * version
527789Sahrens * pool guid
528789Sahrens * name
529789Sahrens * pool state
5303975Sek110237 * hostid (if available)
5313975Sek110237 * hostname (if available)
532789Sahrens */
533789Sahrens uint64_t state;
534789Sahrens
535789Sahrens verify(nvlist_lookup_uint64(tmp,
5362082Seschrock ZPOOL_CONFIG_VERSION, &version) == 0);
5372082Seschrock if (nvlist_add_uint64(config,
5382082Seschrock ZPOOL_CONFIG_VERSION, version) != 0)
5392082Seschrock goto nomem;
5402082Seschrock verify(nvlist_lookup_uint64(tmp,
541789Sahrens ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
5422082Seschrock if (nvlist_add_uint64(config,
5432082Seschrock ZPOOL_CONFIG_POOL_GUID, guid) != 0)
5442082Seschrock goto nomem;
545789Sahrens verify(nvlist_lookup_string(tmp,
546789Sahrens ZPOOL_CONFIG_POOL_NAME, &name) == 0);
5472082Seschrock if (nvlist_add_string(config,
5482082Seschrock ZPOOL_CONFIG_POOL_NAME, name) != 0)
5492082Seschrock goto nomem;
550789Sahrens verify(nvlist_lookup_uint64(tmp,
551789Sahrens ZPOOL_CONFIG_POOL_STATE, &state) == 0);
5522082Seschrock if (nvlist_add_uint64(config,
5532082Seschrock ZPOOL_CONFIG_POOL_STATE, state) != 0)
5542082Seschrock goto nomem;
5553975Sek110237 hostid = 0;
5563975Sek110237 if (nvlist_lookup_uint64(tmp,
5573975Sek110237 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
5583975Sek110237 if (nvlist_add_uint64(config,
5593975Sek110237 ZPOOL_CONFIG_HOSTID, hostid) != 0)
5603975Sek110237 goto nomem;
5613975Sek110237 verify(nvlist_lookup_string(tmp,
5623975Sek110237 ZPOOL_CONFIG_HOSTNAME,
5633975Sek110237 &hostname) == 0);
5643975Sek110237 if (nvlist_add_string(config,
5653975Sek110237 ZPOOL_CONFIG_HOSTNAME,
5663975Sek110237 hostname) != 0)
5673975Sek110237 goto nomem;
5683975Sek110237 }
569789Sahrens
5702082Seschrock config_seen = B_TRUE;
571789Sahrens }
572789Sahrens
573789Sahrens /*
574789Sahrens * Add this top-level vdev to the child array.
575789Sahrens */
576789Sahrens verify(nvlist_lookup_nvlist(tmp,
577789Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
578789Sahrens verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
579789Sahrens &id) == 0);
58010594SGeorge.Wilson@Sun.COM
581789Sahrens if (id >= children) {
582789Sahrens nvlist_t **newchild;
583789Sahrens
5842082Seschrock newchild = zfs_alloc(hdl, (id + 1) *
585789Sahrens sizeof (nvlist_t *));
5862082Seschrock if (newchild == NULL)
5872082Seschrock goto nomem;
588789Sahrens
589789Sahrens for (c = 0; c < children; c++)
590789Sahrens newchild[c] = child[c];
591789Sahrens
592789Sahrens free(child);
593789Sahrens child = newchild;
594789Sahrens children = id + 1;
595789Sahrens }
5962082Seschrock if (nvlist_dup(nvtop, &child[id], 0) != 0)
5972082Seschrock goto nomem;
598789Sahrens
599789Sahrens }
600789Sahrens
60110594SGeorge.Wilson@Sun.COM /*
60210594SGeorge.Wilson@Sun.COM * If we have information about all the top-levels then
60310594SGeorge.Wilson@Sun.COM * clean up the nvlist which we've constructed. This
60410594SGeorge.Wilson@Sun.COM * means removing any extraneous devices that are
60510594SGeorge.Wilson@Sun.COM * beyond the valid range or adding devices to the end
60610594SGeorge.Wilson@Sun.COM * of our array which appear to be missing.
60710594SGeorge.Wilson@Sun.COM */
60810594SGeorge.Wilson@Sun.COM if (valid_top_config) {
60910594SGeorge.Wilson@Sun.COM if (max_id < children) {
61010594SGeorge.Wilson@Sun.COM for (c = max_id; c < children; c++)
61110594SGeorge.Wilson@Sun.COM nvlist_free(child[c]);
61210594SGeorge.Wilson@Sun.COM children = max_id;
61310594SGeorge.Wilson@Sun.COM } else if (max_id > children) {
61410594SGeorge.Wilson@Sun.COM nvlist_t **newchild;
61510594SGeorge.Wilson@Sun.COM
61610594SGeorge.Wilson@Sun.COM newchild = zfs_alloc(hdl, (max_id) *
61710594SGeorge.Wilson@Sun.COM sizeof (nvlist_t *));
61810594SGeorge.Wilson@Sun.COM if (newchild == NULL)
61910594SGeorge.Wilson@Sun.COM goto nomem;
62010594SGeorge.Wilson@Sun.COM
62110594SGeorge.Wilson@Sun.COM for (c = 0; c < children; c++)
62210594SGeorge.Wilson@Sun.COM newchild[c] = child[c];
62310594SGeorge.Wilson@Sun.COM
62410594SGeorge.Wilson@Sun.COM free(child);
62510594SGeorge.Wilson@Sun.COM child = newchild;
62610594SGeorge.Wilson@Sun.COM children = max_id;
62710594SGeorge.Wilson@Sun.COM }
62810594SGeorge.Wilson@Sun.COM }
62910594SGeorge.Wilson@Sun.COM
630789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
631789Sahrens &guid) == 0);
632789Sahrens
633789Sahrens /*
63410594SGeorge.Wilson@Sun.COM * The vdev namespace may contain holes as a result of
63510594SGeorge.Wilson@Sun.COM * device removal. We must add them back into the vdev
63610594SGeorge.Wilson@Sun.COM * tree before we process any missing devices.
63710594SGeorge.Wilson@Sun.COM */
63810594SGeorge.Wilson@Sun.COM if (holes > 0) {
63910594SGeorge.Wilson@Sun.COM ASSERT(valid_top_config);
64010594SGeorge.Wilson@Sun.COM
64110594SGeorge.Wilson@Sun.COM for (c = 0; c < children; c++) {
64210594SGeorge.Wilson@Sun.COM nvlist_t *holey;
64310594SGeorge.Wilson@Sun.COM
64410594SGeorge.Wilson@Sun.COM if (child[c] != NULL ||
64510594SGeorge.Wilson@Sun.COM !vdev_is_hole(hole_array, holes, c))
64610594SGeorge.Wilson@Sun.COM continue;
64710594SGeorge.Wilson@Sun.COM
64810594SGeorge.Wilson@Sun.COM if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
64910594SGeorge.Wilson@Sun.COM 0) != 0)
65010594SGeorge.Wilson@Sun.COM goto nomem;
65110594SGeorge.Wilson@Sun.COM
65210594SGeorge.Wilson@Sun.COM /*
65310594SGeorge.Wilson@Sun.COM * Holes in the namespace are treated as
65410594SGeorge.Wilson@Sun.COM * "hole" top-level vdevs and have a
65510594SGeorge.Wilson@Sun.COM * special flag set on them.
65610594SGeorge.Wilson@Sun.COM */
65710594SGeorge.Wilson@Sun.COM if (nvlist_add_string(holey,
65810594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_TYPE,
65910594SGeorge.Wilson@Sun.COM VDEV_TYPE_HOLE) != 0 ||
66010594SGeorge.Wilson@Sun.COM nvlist_add_uint64(holey,
66110594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_ID, c) != 0 ||
66210594SGeorge.Wilson@Sun.COM nvlist_add_uint64(holey,
66310594SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_GUID, 0ULL) != 0)
66410594SGeorge.Wilson@Sun.COM goto nomem;
66510594SGeorge.Wilson@Sun.COM child[c] = holey;
66610594SGeorge.Wilson@Sun.COM }
66710594SGeorge.Wilson@Sun.COM }
66810594SGeorge.Wilson@Sun.COM
66910594SGeorge.Wilson@Sun.COM /*
670789Sahrens * Look for any missing top-level vdevs. If this is the case,
671789Sahrens * create a faked up 'missing' vdev as a placeholder. We cannot
672789Sahrens * simply compress the child array, because the kernel performs
673789Sahrens * certain checks to make sure the vdev IDs match their location
674789Sahrens * in the configuration.
675789Sahrens */
67610594SGeorge.Wilson@Sun.COM for (c = 0; c < children; c++) {
677789Sahrens if (child[c] == NULL) {
678789Sahrens nvlist_t *missing;
6792082Seschrock if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
6802082Seschrock 0) != 0)
6812082Seschrock goto nomem;
6822082Seschrock if (nvlist_add_string(missing,
6832082Seschrock ZPOOL_CONFIG_TYPE,
6842082Seschrock VDEV_TYPE_MISSING) != 0 ||
6852082Seschrock nvlist_add_uint64(missing,
6862082Seschrock ZPOOL_CONFIG_ID, c) != 0 ||
6872082Seschrock nvlist_add_uint64(missing,
6882082Seschrock ZPOOL_CONFIG_GUID, 0ULL) != 0) {
6892082Seschrock nvlist_free(missing);
6902082Seschrock goto nomem;
6912082Seschrock }
692789Sahrens child[c] = missing;
693789Sahrens }
69410594SGeorge.Wilson@Sun.COM }
695789Sahrens
696789Sahrens /*
697789Sahrens * Put all of this pool's top-level vdevs into a root vdev.
698789Sahrens */
6992082Seschrock if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
7002082Seschrock goto nomem;
7012082Seschrock if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
7022082Seschrock VDEV_TYPE_ROOT) != 0 ||
7032082Seschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
7042082Seschrock nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
7052082Seschrock nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
7062082Seschrock child, children) != 0) {
7072082Seschrock nvlist_free(nvroot);
7082082Seschrock goto nomem;
7092082Seschrock }
710789Sahrens
711789Sahrens for (c = 0; c < children; c++)
712789Sahrens nvlist_free(child[c]);
713789Sahrens free(child);
7142082Seschrock children = 0;
7152082Seschrock child = NULL;
716789Sahrens
717789Sahrens /*
718789Sahrens * Go through and fix up any paths and/or devids based on our
719789Sahrens * known list of vdev GUID -> path mappings.
720789Sahrens */
7212082Seschrock if (fix_paths(nvroot, pl->names) != 0) {
7222082Seschrock nvlist_free(nvroot);
7232082Seschrock goto nomem;
7242082Seschrock }
725789Sahrens
726789Sahrens /*
727789Sahrens * Add the root vdev to this pool's configuration.
728789Sahrens */
7292082Seschrock if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
7302082Seschrock nvroot) != 0) {
7312082Seschrock nvlist_free(nvroot);
7322082Seschrock goto nomem;
7332082Seschrock }
734789Sahrens nvlist_free(nvroot);
735789Sahrens
736789Sahrens /*
7375994Sck153898 * zdb uses this path to report on active pools that were
7385994Sck153898 * imported or created using -R.
7395994Sck153898 */
7405994Sck153898 if (active_ok)
7415994Sck153898 goto add_pool;
7425994Sck153898
7435994Sck153898 /*
744789Sahrens * Determine if this pool is currently active, in which case we
745789Sahrens * can't actually import it.
746789Sahrens */
747789Sahrens verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
748789Sahrens &name) == 0);
749789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
750789Sahrens &guid) == 0);
751789Sahrens
7522142Seschrock if (pool_active(hdl, name, guid, &isactive) != 0)
7532142Seschrock goto error;
7542142Seschrock
7552144Seschrock if (isactive) {
756789Sahrens nvlist_free(config);
7572082Seschrock config = NULL;
758789Sahrens continue;
759789Sahrens }
760789Sahrens
76110594SGeorge.Wilson@Sun.COM if ((nvl = refresh_config(hdl, config)) == NULL) {
76210594SGeorge.Wilson@Sun.COM nvlist_free(config);
76310594SGeorge.Wilson@Sun.COM config = NULL;
76410594SGeorge.Wilson@Sun.COM continue;
76510594SGeorge.Wilson@Sun.COM }
766789Sahrens
767789Sahrens nvlist_free(config);
7685363Seschrock config = nvl;
769789Sahrens
7702082Seschrock /*
7712082Seschrock * Go through and update the paths for spares, now that we have
7722082Seschrock * them.
7732082Seschrock */
7742082Seschrock verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
7752082Seschrock &nvroot) == 0);
7762082Seschrock if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
7772082Seschrock &spares, &nspares) == 0) {
7782082Seschrock for (i = 0; i < nspares; i++) {
7792082Seschrock if (fix_paths(spares[i], pl->names) != 0)
7802082Seschrock goto nomem;
7812082Seschrock }
7822082Seschrock }
783789Sahrens
784789Sahrens /*
7855450Sbrendan * Update the paths for l2cache devices.
7865450Sbrendan */
7875450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
7885450Sbrendan &l2cache, &nl2cache) == 0) {
7895450Sbrendan for (i = 0; i < nl2cache; i++) {
7905450Sbrendan if (fix_paths(l2cache[i], pl->names) != 0)
7915450Sbrendan goto nomem;
7925450Sbrendan }
7935450Sbrendan }
7945450Sbrendan
7955450Sbrendan /*
7963975Sek110237 * Restore the original information read from the actual label.
7973975Sek110237 */
7983975Sek110237 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
7993975Sek110237 DATA_TYPE_UINT64);
8003975Sek110237 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
8013975Sek110237 DATA_TYPE_STRING);
8023975Sek110237 if (hostid != 0) {
8033975Sek110237 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
8043975Sek110237 hostid) == 0);
8053975Sek110237 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
8063975Sek110237 hostname) == 0);
8073975Sek110237 }
8083975Sek110237
8095994Sck153898 add_pool:
8103975Sek110237 /*
811789Sahrens * Add this pool to the list of configs.
812789Sahrens */
8132676Seschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
8142676Seschrock &name) == 0);
8152082Seschrock if (nvlist_add_nvlist(ret, name, config) != 0)
8162082Seschrock goto nomem;
817789Sahrens
8186807Sck153898 found_one = B_TRUE;
819789Sahrens nvlist_free(config);
8202082Seschrock config = NULL;
821789Sahrens }
822789Sahrens
8236807Sck153898 if (!found_one) {
8246807Sck153898 nvlist_free(ret);
8256807Sck153898 ret = NULL;
8266807Sck153898 }
8276807Sck153898
828789Sahrens return (ret);
8292082Seschrock
8302082Seschrock nomem:
8312082Seschrock (void) no_memory(hdl);
8322082Seschrock error:
8332142Seschrock nvlist_free(config);
8342142Seschrock nvlist_free(ret);
8352082Seschrock for (c = 0; c < children; c++)
8362082Seschrock nvlist_free(child[c]);
8372142Seschrock free(child);
8382082Seschrock
8392082Seschrock return (NULL);
840789Sahrens }
841789Sahrens
842789Sahrens /*
843789Sahrens * Return the offset of the given label.
844789Sahrens */
845789Sahrens static uint64_t
label_offset(uint64_t size,int l)8464577Sahrens label_offset(uint64_t size, int l)
847789Sahrens {
8484577Sahrens ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
849789Sahrens return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
850789Sahrens 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
851789Sahrens }
852789Sahrens
853789Sahrens /*
854789Sahrens * Given a file descriptor, read the label information and return an nvlist
855789Sahrens * describing the configuration, if there is one.
856789Sahrens */
8572082Seschrock int
zpool_read_label(int fd,nvlist_t ** config)8582082Seschrock zpool_read_label(int fd, nvlist_t **config)
859789Sahrens {
860789Sahrens struct stat64 statbuf;
861789Sahrens int l;
862789Sahrens vdev_label_t *label;
8634577Sahrens uint64_t state, txg, size;
864789Sahrens
8652082Seschrock *config = NULL;
8662082Seschrock
867789Sahrens if (fstat64(fd, &statbuf) == -1)
8682082Seschrock return (0);
8694577Sahrens size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
870789Sahrens
8712082Seschrock if ((label = malloc(sizeof (vdev_label_t))) == NULL)
8722082Seschrock return (-1);
873789Sahrens
874789Sahrens for (l = 0; l < VDEV_LABELS; l++) {
8756643Seschrock if (pread64(fd, label, sizeof (vdev_label_t),
8764577Sahrens label_offset(size, l)) != sizeof (vdev_label_t))
877789Sahrens continue;
878789Sahrens
879789Sahrens if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
8802082Seschrock sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
881789Sahrens continue;
882789Sahrens
8832082Seschrock if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
8845450Sbrendan &state) != 0 || state > POOL_STATE_L2CACHE) {
8852082Seschrock nvlist_free(*config);
886789Sahrens continue;
887789Sahrens }
888789Sahrens
8895450Sbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
8902082Seschrock (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
8912082Seschrock &txg) != 0 || txg == 0)) {
8922082Seschrock nvlist_free(*config);
893789Sahrens continue;
894789Sahrens }
895789Sahrens
896789Sahrens free(label);
8972082Seschrock return (0);
898789Sahrens }
899789Sahrens
900789Sahrens free(label);
9012082Seschrock *config = NULL;
9022082Seschrock return (0);
903789Sahrens }
904789Sahrens
90510980SEric.Taylor@Sun.COM typedef struct rdsk_node {
90610980SEric.Taylor@Sun.COM char *rn_name;
90710980SEric.Taylor@Sun.COM int rn_dfd;
90810980SEric.Taylor@Sun.COM libzfs_handle_t *rn_hdl;
90910980SEric.Taylor@Sun.COM nvlist_t *rn_config;
91010980SEric.Taylor@Sun.COM avl_tree_t *rn_avl;
91110980SEric.Taylor@Sun.COM avl_node_t rn_node;
91210980SEric.Taylor@Sun.COM boolean_t rn_nozpool;
91310980SEric.Taylor@Sun.COM } rdsk_node_t;
91410980SEric.Taylor@Sun.COM
91510980SEric.Taylor@Sun.COM static int
slice_cache_compare(const void * arg1,const void * arg2)91610980SEric.Taylor@Sun.COM slice_cache_compare(const void *arg1, const void *arg2)
91710980SEric.Taylor@Sun.COM {
91810980SEric.Taylor@Sun.COM const char *nm1 = ((rdsk_node_t *)arg1)->rn_name;
91910980SEric.Taylor@Sun.COM const char *nm2 = ((rdsk_node_t *)arg2)->rn_name;
92010980SEric.Taylor@Sun.COM char *nm1slice, *nm2slice;
92110980SEric.Taylor@Sun.COM int rv;
92210980SEric.Taylor@Sun.COM
92310980SEric.Taylor@Sun.COM /*
92410980SEric.Taylor@Sun.COM * slices zero and two are the most likely to provide results,
92510980SEric.Taylor@Sun.COM * so put those first
92610980SEric.Taylor@Sun.COM */
92710980SEric.Taylor@Sun.COM nm1slice = strstr(nm1, "s0");
92810980SEric.Taylor@Sun.COM nm2slice = strstr(nm2, "s0");
92910980SEric.Taylor@Sun.COM if (nm1slice && !nm2slice) {
93010980SEric.Taylor@Sun.COM return (-1);
93110980SEric.Taylor@Sun.COM }
93210980SEric.Taylor@Sun.COM if (!nm1slice && nm2slice) {
93310980SEric.Taylor@Sun.COM return (1);
93410980SEric.Taylor@Sun.COM }
93510980SEric.Taylor@Sun.COM nm1slice = strstr(nm1, "s2");
93610980SEric.Taylor@Sun.COM nm2slice = strstr(nm2, "s2");
93710980SEric.Taylor@Sun.COM if (nm1slice && !nm2slice) {
93810980SEric.Taylor@Sun.COM return (-1);
93910980SEric.Taylor@Sun.COM }
94010980SEric.Taylor@Sun.COM if (!nm1slice && nm2slice) {
94110980SEric.Taylor@Sun.COM return (1);
94210980SEric.Taylor@Sun.COM }
94310980SEric.Taylor@Sun.COM
94410980SEric.Taylor@Sun.COM rv = strcmp(nm1, nm2);
94510980SEric.Taylor@Sun.COM if (rv == 0)
94610980SEric.Taylor@Sun.COM return (0);
94710980SEric.Taylor@Sun.COM return (rv > 0 ? 1 : -1);
94810980SEric.Taylor@Sun.COM }
94910980SEric.Taylor@Sun.COM
95010980SEric.Taylor@Sun.COM static void
check_one_slice(avl_tree_t * r,char * diskname,uint_t partno,diskaddr_t size,uint_t blksz)95110980SEric.Taylor@Sun.COM check_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
95210980SEric.Taylor@Sun.COM diskaddr_t size, uint_t blksz)
95310980SEric.Taylor@Sun.COM {
95410980SEric.Taylor@Sun.COM rdsk_node_t tmpnode;
95510980SEric.Taylor@Sun.COM rdsk_node_t *node;
95610980SEric.Taylor@Sun.COM char sname[MAXNAMELEN];
95710980SEric.Taylor@Sun.COM
95810980SEric.Taylor@Sun.COM tmpnode.rn_name = &sname[0];
95910980SEric.Taylor@Sun.COM (void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u",
96010980SEric.Taylor@Sun.COM diskname, partno);
96111123SEric.Taylor@Sun.COM /*
96211123SEric.Taylor@Sun.COM * protect against division by zero for disk labels that
96311123SEric.Taylor@Sun.COM * contain a bogus sector size
96411123SEric.Taylor@Sun.COM */
96511123SEric.Taylor@Sun.COM if (blksz == 0)
96611123SEric.Taylor@Sun.COM blksz = DEV_BSIZE;
96710980SEric.Taylor@Sun.COM /* too small to contain a zpool? */
96810980SEric.Taylor@Sun.COM if ((size < (SPA_MINDEVSIZE / blksz)) &&
96910980SEric.Taylor@Sun.COM (node = avl_find(r, &tmpnode, NULL)))
97010980SEric.Taylor@Sun.COM node->rn_nozpool = B_TRUE;
97110980SEric.Taylor@Sun.COM }
97210980SEric.Taylor@Sun.COM
97310980SEric.Taylor@Sun.COM static void
nozpool_all_slices(avl_tree_t * r,const char * sname)97410980SEric.Taylor@Sun.COM nozpool_all_slices(avl_tree_t *r, const char *sname)
97510980SEric.Taylor@Sun.COM {
97610980SEric.Taylor@Sun.COM char diskname[MAXNAMELEN];
97710980SEric.Taylor@Sun.COM char *ptr;
97810980SEric.Taylor@Sun.COM int i;
97910980SEric.Taylor@Sun.COM
98010980SEric.Taylor@Sun.COM (void) strncpy(diskname, sname, MAXNAMELEN);
98110980SEric.Taylor@Sun.COM if (((ptr = strrchr(diskname, 's')) == NULL) &&
98210980SEric.Taylor@Sun.COM ((ptr = strrchr(diskname, 'p')) == NULL))
98310980SEric.Taylor@Sun.COM return;
98410980SEric.Taylor@Sun.COM ptr[0] = 's';
98510980SEric.Taylor@Sun.COM ptr[1] = '\0';
98610980SEric.Taylor@Sun.COM for (i = 0; i < NDKMAP; i++)
98710980SEric.Taylor@Sun.COM check_one_slice(r, diskname, i, 0, 1);
98810980SEric.Taylor@Sun.COM ptr[0] = 'p';
98910980SEric.Taylor@Sun.COM for (i = 0; i <= FD_NUMPART; i++)
99010980SEric.Taylor@Sun.COM check_one_slice(r, diskname, i, 0, 1);
99110980SEric.Taylor@Sun.COM }
99210980SEric.Taylor@Sun.COM
99310980SEric.Taylor@Sun.COM static void
check_slices(avl_tree_t * r,int fd,const char * sname)99410980SEric.Taylor@Sun.COM check_slices(avl_tree_t *r, int fd, const char *sname)
99510980SEric.Taylor@Sun.COM {
99610980SEric.Taylor@Sun.COM struct extvtoc vtoc;
99710980SEric.Taylor@Sun.COM struct dk_gpt *gpt;
99810980SEric.Taylor@Sun.COM char diskname[MAXNAMELEN];
99910980SEric.Taylor@Sun.COM char *ptr;
100010980SEric.Taylor@Sun.COM int i;
100110980SEric.Taylor@Sun.COM
100210980SEric.Taylor@Sun.COM (void) strncpy(diskname, sname, MAXNAMELEN);
100310980SEric.Taylor@Sun.COM if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
100410980SEric.Taylor@Sun.COM return;
100510980SEric.Taylor@Sun.COM ptr[1] = '\0';
100610980SEric.Taylor@Sun.COM
100710980SEric.Taylor@Sun.COM if (read_extvtoc(fd, &vtoc) >= 0) {
100810980SEric.Taylor@Sun.COM for (i = 0; i < NDKMAP; i++)
100910980SEric.Taylor@Sun.COM check_one_slice(r, diskname, i,
101010980SEric.Taylor@Sun.COM vtoc.v_part[i].p_size, vtoc.v_sectorsz);
101110980SEric.Taylor@Sun.COM } else if (efi_alloc_and_read(fd, &gpt) >= 0) {
101210980SEric.Taylor@Sun.COM /*
101310980SEric.Taylor@Sun.COM * on x86 we'll still have leftover links that point
101410980SEric.Taylor@Sun.COM * to slices s[9-15], so use NDKMAP instead
101510980SEric.Taylor@Sun.COM */
101610980SEric.Taylor@Sun.COM for (i = 0; i < NDKMAP; i++)
101710980SEric.Taylor@Sun.COM check_one_slice(r, diskname, i,
101810980SEric.Taylor@Sun.COM gpt->efi_parts[i].p_size, gpt->efi_lbasize);
101910980SEric.Taylor@Sun.COM /* nodes p[1-4] are never used with EFI labels */
102010980SEric.Taylor@Sun.COM ptr[0] = 'p';
102110980SEric.Taylor@Sun.COM for (i = 1; i <= FD_NUMPART; i++)
102210980SEric.Taylor@Sun.COM check_one_slice(r, diskname, i, 0, 1);
102310980SEric.Taylor@Sun.COM efi_free(gpt);
102410980SEric.Taylor@Sun.COM }
102510980SEric.Taylor@Sun.COM }
102610980SEric.Taylor@Sun.COM
102710980SEric.Taylor@Sun.COM static void
zpool_open_func(void * arg)102810980SEric.Taylor@Sun.COM zpool_open_func(void *arg)
102910980SEric.Taylor@Sun.COM {
103010980SEric.Taylor@Sun.COM rdsk_node_t *rn = arg;
103110980SEric.Taylor@Sun.COM struct stat64 statbuf;
103210980SEric.Taylor@Sun.COM nvlist_t *config;
103310980SEric.Taylor@Sun.COM int fd;
103410980SEric.Taylor@Sun.COM
103510980SEric.Taylor@Sun.COM if (rn->rn_nozpool)
103610980SEric.Taylor@Sun.COM return;
103710980SEric.Taylor@Sun.COM if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) {
103810980SEric.Taylor@Sun.COM /* symlink to a device that's no longer there */
103910980SEric.Taylor@Sun.COM if (errno == ENOENT)
104010980SEric.Taylor@Sun.COM nozpool_all_slices(rn->rn_avl, rn->rn_name);
104110980SEric.Taylor@Sun.COM return;
104210980SEric.Taylor@Sun.COM }
104310980SEric.Taylor@Sun.COM /*
104410980SEric.Taylor@Sun.COM * Ignore failed stats. We only want regular
104510980SEric.Taylor@Sun.COM * files, character devs and block devs.
104610980SEric.Taylor@Sun.COM */
104710980SEric.Taylor@Sun.COM if (fstat64(fd, &statbuf) != 0 ||
104810980SEric.Taylor@Sun.COM (!S_ISREG(statbuf.st_mode) &&
104910980SEric.Taylor@Sun.COM !S_ISCHR(statbuf.st_mode) &&
105010980SEric.Taylor@Sun.COM !S_ISBLK(statbuf.st_mode))) {
105110980SEric.Taylor@Sun.COM (void) close(fd);
105210980SEric.Taylor@Sun.COM return;
105310980SEric.Taylor@Sun.COM }
105410980SEric.Taylor@Sun.COM /* this file is too small to hold a zpool */
105510980SEric.Taylor@Sun.COM if (S_ISREG(statbuf.st_mode) &&
105610980SEric.Taylor@Sun.COM statbuf.st_size < SPA_MINDEVSIZE) {
105710980SEric.Taylor@Sun.COM (void) close(fd);
105810980SEric.Taylor@Sun.COM return;
105910980SEric.Taylor@Sun.COM } else if (!S_ISREG(statbuf.st_mode)) {
106010980SEric.Taylor@Sun.COM /*
106110980SEric.Taylor@Sun.COM * Try to read the disk label first so we don't have to
106210980SEric.Taylor@Sun.COM * open a bunch of minor nodes that can't have a zpool.
106310980SEric.Taylor@Sun.COM */
106410980SEric.Taylor@Sun.COM check_slices(rn->rn_avl, fd, rn->rn_name);
106510980SEric.Taylor@Sun.COM }
106610980SEric.Taylor@Sun.COM
106710980SEric.Taylor@Sun.COM if ((zpool_read_label(fd, &config)) != 0) {
106810980SEric.Taylor@Sun.COM (void) close(fd);
106910980SEric.Taylor@Sun.COM (void) no_memory(rn->rn_hdl);
107010980SEric.Taylor@Sun.COM return;
107110980SEric.Taylor@Sun.COM }
107210980SEric.Taylor@Sun.COM (void) close(fd);
107310980SEric.Taylor@Sun.COM
107410980SEric.Taylor@Sun.COM
107510980SEric.Taylor@Sun.COM rn->rn_config = config;
107610980SEric.Taylor@Sun.COM if (config != NULL) {
107710980SEric.Taylor@Sun.COM assert(rn->rn_nozpool == B_FALSE);
107810980SEric.Taylor@Sun.COM }
107910980SEric.Taylor@Sun.COM }
108010980SEric.Taylor@Sun.COM
1081789Sahrens /*
108210830SEric.Schrock@Sun.COM * Given a file descriptor, clear (zero) the label information. This function
108310830SEric.Schrock@Sun.COM * is currently only used in the appliance stack as part of the ZFS sysevent
108410830SEric.Schrock@Sun.COM * module.
108510830SEric.Schrock@Sun.COM */
108610830SEric.Schrock@Sun.COM int
zpool_clear_label(int fd)108710830SEric.Schrock@Sun.COM zpool_clear_label(int fd)
108810830SEric.Schrock@Sun.COM {
108910830SEric.Schrock@Sun.COM struct stat64 statbuf;
109010830SEric.Schrock@Sun.COM int l;
109110830SEric.Schrock@Sun.COM vdev_label_t *label;
109210830SEric.Schrock@Sun.COM uint64_t size;
109310830SEric.Schrock@Sun.COM
109410830SEric.Schrock@Sun.COM if (fstat64(fd, &statbuf) == -1)
109510830SEric.Schrock@Sun.COM return (0);
109610830SEric.Schrock@Sun.COM size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
109710830SEric.Schrock@Sun.COM
109810830SEric.Schrock@Sun.COM if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
109910830SEric.Schrock@Sun.COM return (-1);
110010830SEric.Schrock@Sun.COM
110110830SEric.Schrock@Sun.COM for (l = 0; l < VDEV_LABELS; l++) {
110210830SEric.Schrock@Sun.COM if (pwrite64(fd, label, sizeof (vdev_label_t),
110310830SEric.Schrock@Sun.COM label_offset(size, l)) != sizeof (vdev_label_t))
110410830SEric.Schrock@Sun.COM return (-1);
110510830SEric.Schrock@Sun.COM }
110610830SEric.Schrock@Sun.COM
110710830SEric.Schrock@Sun.COM free(label);
110810830SEric.Schrock@Sun.COM return (0);
110910830SEric.Schrock@Sun.COM }
111010830SEric.Schrock@Sun.COM
111110830SEric.Schrock@Sun.COM /*
1112789Sahrens * Given a list of directories to search, find all pools stored on disk. This
1113789Sahrens * includes partial pools which are not available to import. If no args are
1114789Sahrens * given (argc is 0), then the default directory (/dev/dsk) is searched.
11156807Sck153898 * poolname or guid (but not both) are provided by the caller when trying
11166807Sck153898 * to import a specific pool.
1117789Sahrens */
11186807Sck153898 static nvlist_t *
zpool_find_import_impl(libzfs_handle_t * hdl,importargs_t * iarg)111911497SMark.Musante@Sun.COM zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
1120789Sahrens {
112111497SMark.Musante@Sun.COM int i, dirs = iarg->paths;
11224055Seschrock DIR *dirp = NULL;
1123789Sahrens struct dirent64 *dp;
1124789Sahrens char path[MAXPATHLEN];
112511497SMark.Musante@Sun.COM char *end, **dir = iarg->path;
11266376Sjwadams size_t pathleft;
112710980SEric.Taylor@Sun.COM nvlist_t *ret = NULL;
1128789Sahrens static char *default_dir = "/dev/dsk";
1129789Sahrens pool_list_t pools = { 0 };
11302082Seschrock pool_entry_t *pe, *penext;
11312082Seschrock vdev_entry_t *ve, *venext;
11322082Seschrock config_entry_t *ce, *cenext;
11332082Seschrock name_entry_t *ne, *nenext;
113410980SEric.Taylor@Sun.COM avl_tree_t slice_cache;
113510980SEric.Taylor@Sun.COM rdsk_node_t *slice;
113610980SEric.Taylor@Sun.COM void *cookie;
11372082Seschrock
113811497SMark.Musante@Sun.COM if (dirs == 0) {
113911497SMark.Musante@Sun.COM dirs = 1;
114011497SMark.Musante@Sun.COM dir = &default_dir;
1141789Sahrens }
1142789Sahrens
1143789Sahrens /*
1144789Sahrens * Go through and read the label configuration information from every
1145789Sahrens * possible device, organizing the information according to pool GUID
1146789Sahrens * and toplevel GUID.
1147789Sahrens */
114811497SMark.Musante@Sun.COM for (i = 0; i < dirs; i++) {
114910980SEric.Taylor@Sun.COM tpool_t *t;
11506376Sjwadams char *rdsk;
11516376Sjwadams int dfd;
11526376Sjwadams
11536376Sjwadams /* use realpath to normalize the path */
115411497SMark.Musante@Sun.COM if (realpath(dir[i], path) == 0) {
11553237Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH,
115611497SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]);
11572082Seschrock goto error;
1158789Sahrens }
11596376Sjwadams end = &path[strlen(path)];
11606376Sjwadams *end++ = '/';
11616376Sjwadams *end = 0;
11626376Sjwadams pathleft = &path[sizeof (path)] - end;
1163789Sahrens
11646376Sjwadams /*
11656376Sjwadams * Using raw devices instead of block devices when we're
11666376Sjwadams * reading the labels skips a bunch of slow operations during
11676376Sjwadams * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
11686376Sjwadams */
11696376Sjwadams if (strcmp(path, "/dev/dsk/") == 0)
11706376Sjwadams rdsk = "/dev/rdsk/";
11716376Sjwadams else
11726376Sjwadams rdsk = path;
11736376Sjwadams
11746376Sjwadams if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
11756376Sjwadams (dirp = fdopendir(dfd)) == NULL) {
11762082Seschrock zfs_error_aux(hdl, strerror(errno));
11773237Slling (void) zfs_error_fmt(hdl, EZFS_BADPATH,
11782082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"),
11796376Sjwadams rdsk);
11802082Seschrock goto error;
1181789Sahrens }
1182789Sahrens
118310980SEric.Taylor@Sun.COM avl_create(&slice_cache, slice_cache_compare,
118410980SEric.Taylor@Sun.COM sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
1185789Sahrens /*
1186789Sahrens * This is not MT-safe, but we have no MT consumers of libzfs
1187789Sahrens */
1188789Sahrens while ((dp = readdir64(dirp)) != NULL) {
11896376Sjwadams const char *name = dp->d_name;
11906376Sjwadams if (name[0] == '.' &&
11916376Sjwadams (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
11926376Sjwadams continue;
1193789Sahrens
119410980SEric.Taylor@Sun.COM slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
119510980SEric.Taylor@Sun.COM slice->rn_name = zfs_strdup(hdl, name);
119610980SEric.Taylor@Sun.COM slice->rn_avl = &slice_cache;
119710980SEric.Taylor@Sun.COM slice->rn_dfd = dfd;
119810980SEric.Taylor@Sun.COM slice->rn_hdl = hdl;
119910980SEric.Taylor@Sun.COM slice->rn_nozpool = B_FALSE;
120010980SEric.Taylor@Sun.COM avl_add(&slice_cache, slice);
120110980SEric.Taylor@Sun.COM }
120210980SEric.Taylor@Sun.COM /*
120310980SEric.Taylor@Sun.COM * create a thread pool to do all of this in parallel;
120410980SEric.Taylor@Sun.COM * rn_nozpool is not protected, so this is racy in that
120510980SEric.Taylor@Sun.COM * multiple tasks could decide that the same slice can
120610980SEric.Taylor@Sun.COM * not hold a zpool, which is benign. Also choose
120710980SEric.Taylor@Sun.COM * double the number of processors; we hold a lot of
120810980SEric.Taylor@Sun.COM * locks in the kernel, so going beyond this doesn't
120910980SEric.Taylor@Sun.COM * buy us much.
121010980SEric.Taylor@Sun.COM */
121110980SEric.Taylor@Sun.COM t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
121210980SEric.Taylor@Sun.COM 0, NULL);
121310980SEric.Taylor@Sun.COM for (slice = avl_first(&slice_cache); slice;
121410980SEric.Taylor@Sun.COM (slice = avl_walk(&slice_cache, slice,
121510980SEric.Taylor@Sun.COM AVL_AFTER)))
121610980SEric.Taylor@Sun.COM (void) tpool_dispatch(t, zpool_open_func, slice);
121710980SEric.Taylor@Sun.COM tpool_wait(t);
121810980SEric.Taylor@Sun.COM tpool_destroy(t);
1219789Sahrens
122010980SEric.Taylor@Sun.COM cookie = NULL;
122110980SEric.Taylor@Sun.COM while ((slice = avl_destroy_nodes(&slice_cache,
122210980SEric.Taylor@Sun.COM &cookie)) != NULL) {
122310980SEric.Taylor@Sun.COM if (slice->rn_config != NULL) {
122410980SEric.Taylor@Sun.COM nvlist_t *config = slice->rn_config;
12256807Sck153898 boolean_t matched = B_TRUE;
12266807Sck153898
122711497SMark.Musante@Sun.COM if (iarg->poolname != NULL) {
12286807Sck153898 char *pname;
12296895Sck153898
12306895Sck153898 matched = nvlist_lookup_string(config,
12316807Sck153898 ZPOOL_CONFIG_POOL_NAME,
12326895Sck153898 &pname) == 0 &&
123311497SMark.Musante@Sun.COM strcmp(iarg->poolname, pname) == 0;
123411497SMark.Musante@Sun.COM } else if (iarg->guid != 0) {
12356807Sck153898 uint64_t this_guid;
12367043Sck153898
12377043Sck153898 matched = nvlist_lookup_uint64(config,
12386807Sck153898 ZPOOL_CONFIG_POOL_GUID,
12397043Sck153898 &this_guid) == 0 &&
124011497SMark.Musante@Sun.COM iarg->guid == this_guid;
12416807Sck153898 }
12426807Sck153898 if (!matched) {
12436807Sck153898 nvlist_free(config);
12446807Sck153898 config = NULL;
12456807Sck153898 continue;
12466807Sck153898 }
12476376Sjwadams /* use the non-raw path for the config */
124810980SEric.Taylor@Sun.COM (void) strlcpy(end, slice->rn_name, pathleft);
12492082Seschrock if (add_config(hdl, &pools, path, config) != 0)
12502082Seschrock goto error;
12516376Sjwadams }
125210980SEric.Taylor@Sun.COM free(slice->rn_name);
125310980SEric.Taylor@Sun.COM free(slice);
1254789Sahrens }
125510980SEric.Taylor@Sun.COM avl_destroy(&slice_cache);
12564055Seschrock
12574055Seschrock (void) closedir(dirp);
12584055Seschrock dirp = NULL;
1259789Sahrens }
1260789Sahrens
126111497SMark.Musante@Sun.COM ret = get_configs(hdl, &pools, iarg->can_be_active);
12622082Seschrock
12632082Seschrock error:
12642082Seschrock for (pe = pools.pools; pe != NULL; pe = penext) {
12652082Seschrock penext = pe->pe_next;
12662082Seschrock for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
12672082Seschrock venext = ve->ve_next;
12682082Seschrock for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
12692082Seschrock cenext = ce->ce_next;
12702082Seschrock if (ce->ce_config)
12712082Seschrock nvlist_free(ce->ce_config);
12722082Seschrock free(ce);
12732082Seschrock }
12742082Seschrock free(ve);
12752082Seschrock }
12762082Seschrock free(pe);
12772082Seschrock }
12782082Seschrock
12792082Seschrock for (ne = pools.names; ne != NULL; ne = nenext) {
12802082Seschrock nenext = ne->ne_next;
12812082Seschrock if (ne->ne_name)
12822082Seschrock free(ne->ne_name);
12832082Seschrock free(ne);
12842082Seschrock }
12852082Seschrock
12864055Seschrock if (dirp)
12874055Seschrock (void) closedir(dirp);
1288789Sahrens
1289789Sahrens return (ret);
1290789Sahrens }
1291789Sahrens
12926807Sck153898 nvlist_t *
zpool_find_import(libzfs_handle_t * hdl,int argc,char ** argv)12936807Sck153898 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
12946807Sck153898 {
129511497SMark.Musante@Sun.COM importargs_t iarg = { 0 };
12966807Sck153898
129711497SMark.Musante@Sun.COM iarg.paths = argc;
129811497SMark.Musante@Sun.COM iarg.path = argv;
12996807Sck153898
130011497SMark.Musante@Sun.COM return (zpool_find_import_impl(hdl, &iarg));
13016807Sck153898 }
13026807Sck153898
13035363Seschrock /*
13045363Seschrock * Given a cache file, return the contents as a list of importable pools.
13056807Sck153898 * poolname or guid (but not both) are provided by the caller when trying
13066807Sck153898 * to import a specific pool.
13075363Seschrock */
13085363Seschrock nvlist_t *
zpool_find_import_cached(libzfs_handle_t * hdl,const char * cachefile,char * poolname,uint64_t guid)13095994Sck153898 zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
13106957Sck153898 char *poolname, uint64_t guid)
13115363Seschrock {
13125363Seschrock char *buf;
13135363Seschrock int fd;
13145363Seschrock struct stat64 statbuf;
13155363Seschrock nvlist_t *raw, *src, *dst;
13165363Seschrock nvlist_t *pools;
13175363Seschrock nvpair_t *elem;
13185363Seschrock char *name;
13196807Sck153898 uint64_t this_guid;
13205363Seschrock boolean_t active;
13215363Seschrock
13226807Sck153898 verify(poolname == NULL || guid == 0);
13236807Sck153898
13245363Seschrock if ((fd = open(cachefile, O_RDONLY)) < 0) {
13255363Seschrock zfs_error_aux(hdl, "%s", strerror(errno));
13265363Seschrock (void) zfs_error(hdl, EZFS_BADCACHE,
13275363Seschrock dgettext(TEXT_DOMAIN, "failed to open cache file"));
13285363Seschrock return (NULL);
13295363Seschrock }
13305363Seschrock
13315363Seschrock if (fstat64(fd, &statbuf) != 0) {
13325363Seschrock zfs_error_aux(hdl, "%s", strerror(errno));
13335363Seschrock (void) close(fd);
13345363Seschrock (void) zfs_error(hdl, EZFS_BADCACHE,
13355363Seschrock dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
13365363Seschrock return (NULL);
13375363Seschrock }
13385363Seschrock
13395363Seschrock if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
13405363Seschrock (void) close(fd);
13415363Seschrock return (NULL);
13425363Seschrock }
13435363Seschrock
13445363Seschrock if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
13455363Seschrock (void) close(fd);
13465363Seschrock free(buf);
13475363Seschrock (void) zfs_error(hdl, EZFS_BADCACHE,
13485363Seschrock dgettext(TEXT_DOMAIN,
13495363Seschrock "failed to read cache file contents"));
13505363Seschrock return (NULL);
13515363Seschrock }
13525363Seschrock
13535363Seschrock (void) close(fd);
13545363Seschrock
13555363Seschrock if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
13565363Seschrock free(buf);
13575363Seschrock (void) zfs_error(hdl, EZFS_BADCACHE,
13585363Seschrock dgettext(TEXT_DOMAIN,
13595363Seschrock "invalid or corrupt cache file contents"));
13605363Seschrock return (NULL);
13615363Seschrock }
13625363Seschrock
13635363Seschrock free(buf);
13645363Seschrock
13655363Seschrock /*
13665363Seschrock * Go through and get the current state of the pools and refresh their
13675363Seschrock * state.
13685363Seschrock */
13695363Seschrock if (nvlist_alloc(&pools, 0, 0) != 0) {
13705363Seschrock (void) no_memory(hdl);
13715363Seschrock nvlist_free(raw);
13725363Seschrock return (NULL);
13735363Seschrock }
13745363Seschrock
13755363Seschrock elem = NULL;
13765363Seschrock while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
13775363Seschrock verify(nvpair_value_nvlist(elem, &src) == 0);
13785363Seschrock
13795363Seschrock verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME,
13805363Seschrock &name) == 0);
13816807Sck153898 if (poolname != NULL && strcmp(poolname, name) != 0)
13826807Sck153898 continue;
13836807Sck153898
13845363Seschrock verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
13856807Sck153898 &this_guid) == 0);
13866807Sck153898 if (guid != 0) {
13876807Sck153898 verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
13886807Sck153898 &this_guid) == 0);
13896807Sck153898 if (guid != this_guid)
13906807Sck153898 continue;
13916807Sck153898 }
13925363Seschrock
13936957Sck153898 if (pool_active(hdl, name, this_guid, &active) != 0) {
13946957Sck153898 nvlist_free(raw);
13956957Sck153898 nvlist_free(pools);
13966957Sck153898 return (NULL);
13976957Sck153898 }
13985363Seschrock
13996957Sck153898 if (active)
14006957Sck153898 continue;
14015363Seschrock
14026957Sck153898 if ((dst = refresh_config(hdl, src)) == NULL) {
14036957Sck153898 nvlist_free(raw);
14046957Sck153898 nvlist_free(pools);
14056957Sck153898 return (NULL);
14066957Sck153898 }
14076957Sck153898
14086957Sck153898 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
14096957Sck153898 (void) no_memory(hdl);
14105994Sck153898 nvlist_free(dst);
14116957Sck153898 nvlist_free(raw);
14126957Sck153898 nvlist_free(pools);
14136957Sck153898 return (NULL);
14145363Seschrock }
14156957Sck153898 nvlist_free(dst);
14165363Seschrock }
14175363Seschrock
14185363Seschrock nvlist_free(raw);
14195363Seschrock return (pools);
14205363Seschrock }
14215363Seschrock
142211497SMark.Musante@Sun.COM static int
name_or_guid_exists(zpool_handle_t * zhp,void * data)142311497SMark.Musante@Sun.COM name_or_guid_exists(zpool_handle_t *zhp, void *data)
142411497SMark.Musante@Sun.COM {
142511497SMark.Musante@Sun.COM importargs_t *import = data;
142611497SMark.Musante@Sun.COM int found = 0;
142711497SMark.Musante@Sun.COM
142811497SMark.Musante@Sun.COM if (import->poolname != NULL) {
142911497SMark.Musante@Sun.COM char *pool_name;
143011497SMark.Musante@Sun.COM
143111497SMark.Musante@Sun.COM verify(nvlist_lookup_string(zhp->zpool_config,
143211497SMark.Musante@Sun.COM ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
143311497SMark.Musante@Sun.COM if (strcmp(pool_name, import->poolname) == 0)
143411497SMark.Musante@Sun.COM found = 1;
143511497SMark.Musante@Sun.COM } else {
143611497SMark.Musante@Sun.COM uint64_t pool_guid;
143711497SMark.Musante@Sun.COM
143811497SMark.Musante@Sun.COM verify(nvlist_lookup_uint64(zhp->zpool_config,
143911497SMark.Musante@Sun.COM ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
144011497SMark.Musante@Sun.COM if (pool_guid == import->guid)
144111497SMark.Musante@Sun.COM found = 1;
144211497SMark.Musante@Sun.COM }
144311497SMark.Musante@Sun.COM
144411497SMark.Musante@Sun.COM zpool_close(zhp);
144511497SMark.Musante@Sun.COM return (found);
144611497SMark.Musante@Sun.COM }
144711497SMark.Musante@Sun.COM
144811497SMark.Musante@Sun.COM nvlist_t *
zpool_search_import(libzfs_handle_t * hdl,importargs_t * import)144911497SMark.Musante@Sun.COM zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
145011497SMark.Musante@Sun.COM {
145111497SMark.Musante@Sun.COM verify(import->poolname == NULL || import->guid == 0);
145211497SMark.Musante@Sun.COM
145311497SMark.Musante@Sun.COM if (import->unique)
145411497SMark.Musante@Sun.COM import->exists = zpool_iter(hdl, name_or_guid_exists, import);
145511497SMark.Musante@Sun.COM
145611497SMark.Musante@Sun.COM if (import->cachefile != NULL)
145711497SMark.Musante@Sun.COM return (zpool_find_import_cached(hdl, import->cachefile,
145811497SMark.Musante@Sun.COM import->poolname, import->guid));
145911497SMark.Musante@Sun.COM
146011497SMark.Musante@Sun.COM return (zpool_find_import_impl(hdl, import));
146111497SMark.Musante@Sun.COM }
14625363Seschrock
14632082Seschrock boolean_t
find_guid(nvlist_t * nv,uint64_t guid)1464789Sahrens find_guid(nvlist_t *nv, uint64_t guid)
1465789Sahrens {
1466789Sahrens uint64_t tmp;
1467789Sahrens nvlist_t **child;
1468789Sahrens uint_t c, children;
1469789Sahrens
1470789Sahrens verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
1471789Sahrens if (tmp == guid)
14722082Seschrock return (B_TRUE);
1473789Sahrens
1474789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1475789Sahrens &child, &children) == 0) {
1476789Sahrens for (c = 0; c < children; c++)
1477789Sahrens if (find_guid(child[c], guid))
14782082Seschrock return (B_TRUE);
1479789Sahrens }
1480789Sahrens
14812082Seschrock return (B_FALSE);
14822082Seschrock }
14832082Seschrock
14845450Sbrendan typedef struct aux_cbdata {
14855450Sbrendan const char *cb_type;
14862082Seschrock uint64_t cb_guid;
14872082Seschrock zpool_handle_t *cb_zhp;
14885450Sbrendan } aux_cbdata_t;
14892082Seschrock
14902082Seschrock static int
find_aux(zpool_handle_t * zhp,void * data)14915450Sbrendan find_aux(zpool_handle_t *zhp, void *data)
14922082Seschrock {
14935450Sbrendan aux_cbdata_t *cbp = data;
14945450Sbrendan nvlist_t **list;
14955450Sbrendan uint_t i, count;
14962082Seschrock uint64_t guid;
14972082Seschrock nvlist_t *nvroot;
14982082Seschrock
14992082Seschrock verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
15002082Seschrock &nvroot) == 0);
15012082Seschrock
15025450Sbrendan if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
15035450Sbrendan &list, &count) == 0) {
15045450Sbrendan for (i = 0; i < count; i++) {
15055450Sbrendan verify(nvlist_lookup_uint64(list[i],
15062082Seschrock ZPOOL_CONFIG_GUID, &guid) == 0);
15072082Seschrock if (guid == cbp->cb_guid) {
15082082Seschrock cbp->cb_zhp = zhp;
15092082Seschrock return (1);
15102082Seschrock }
15112082Seschrock }
15122082Seschrock }
15132082Seschrock
15142082Seschrock zpool_close(zhp);
15152082Seschrock return (0);
1516789Sahrens }
1517789Sahrens
1518789Sahrens /*
15192082Seschrock * Determines if the pool is in use. If so, it returns true and the state of
1520789Sahrens * the pool as well as the name of the pool. Both strings are allocated and
1521789Sahrens * must be freed by the caller.
1522789Sahrens */
1523789Sahrens int
zpool_in_use(libzfs_handle_t * hdl,int fd,pool_state_t * state,char ** namestr,boolean_t * inuse)15242082Seschrock zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
15252082Seschrock boolean_t *inuse)
1526789Sahrens {
1527789Sahrens nvlist_t *config;
1528789Sahrens char *name;
15292082Seschrock boolean_t ret;
1530789Sahrens uint64_t guid, vdev_guid;
1531789Sahrens zpool_handle_t *zhp;
1532789Sahrens nvlist_t *pool_config;
15333377Seschrock uint64_t stateval, isspare;
15345450Sbrendan aux_cbdata_t cb = { 0 };
15352142Seschrock boolean_t isactive;
1536789Sahrens
15372082Seschrock *inuse = B_FALSE;
1538789Sahrens
15392082Seschrock if (zpool_read_label(fd, &config) != 0) {
15402082Seschrock (void) no_memory(hdl);
15412082Seschrock return (-1);
15422082Seschrock }
15432082Seschrock
15442082Seschrock if (config == NULL)
15452082Seschrock return (0);
15462082Seschrock
1547789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
15481352Seschrock &stateval) == 0);
1549789Sahrens verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1550789Sahrens &vdev_guid) == 0);
1551789Sahrens
15525450Sbrendan if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
15532082Seschrock verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
15542082Seschrock &name) == 0);
15552082Seschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
15562082Seschrock &guid) == 0);
15572082Seschrock }
15582082Seschrock
15591352Seschrock switch (stateval) {
1560789Sahrens case POOL_STATE_EXPORTED:
1561*13049SGeorge.Wilson@Sun.COM /*
1562*13049SGeorge.Wilson@Sun.COM * A pool with an exported state may in fact be imported
1563*13049SGeorge.Wilson@Sun.COM * read-only, so check the in-core state to see if it's
1564*13049SGeorge.Wilson@Sun.COM * active and imported read-only. If it is, set
1565*13049SGeorge.Wilson@Sun.COM * its state to active.
1566*13049SGeorge.Wilson@Sun.COM */
1567*13049SGeorge.Wilson@Sun.COM if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
1568*13049SGeorge.Wilson@Sun.COM (zhp = zpool_open_canfail(hdl, name)) != NULL &&
1569*13049SGeorge.Wilson@Sun.COM zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
1570*13049SGeorge.Wilson@Sun.COM stateval = POOL_STATE_ACTIVE;
1571*13049SGeorge.Wilson@Sun.COM
15722082Seschrock ret = B_TRUE;
1573789Sahrens break;
1574789Sahrens
1575789Sahrens case POOL_STATE_ACTIVE:
1576789Sahrens /*
1577789Sahrens * For an active pool, we have to determine if it's really part
15781760Seschrock * of a currently active pool (in which case the pool will exist
15791760Seschrock * and the guid will be the same), or whether it's part of an
15801760Seschrock * active pool that was disconnected without being explicitly
15811760Seschrock * exported.
1582789Sahrens */
15832142Seschrock if (pool_active(hdl, name, guid, &isactive) != 0) {
15842142Seschrock nvlist_free(config);
15852142Seschrock return (-1);
15862142Seschrock }
15872142Seschrock
15882142Seschrock if (isactive) {
1589789Sahrens /*
1590789Sahrens * Because the device may have been removed while
1591789Sahrens * offlined, we only report it as active if the vdev is
1592789Sahrens * still present in the config. Otherwise, pretend like
1593789Sahrens * it's not in use.
1594789Sahrens */
15952082Seschrock if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
1596952Seschrock (pool_config = zpool_get_config(zhp, NULL))
1597952Seschrock != NULL) {
1598789Sahrens nvlist_t *nvroot;
1599789Sahrens
1600789Sahrens verify(nvlist_lookup_nvlist(pool_config,
1601789Sahrens ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
16021352Seschrock ret = find_guid(nvroot, vdev_guid);
1603789Sahrens } else {
16042082Seschrock ret = B_FALSE;
1605789Sahrens }
16062082Seschrock
16073377Seschrock /*
16083377Seschrock * If this is an active spare within another pool, we
16093377Seschrock * treat it like an unused hot spare. This allows the
16103377Seschrock * user to create a pool with a hot spare that currently
16113377Seschrock * in use within another pool. Since we return B_TRUE,
16123377Seschrock * libdiskmgt will continue to prevent generic consumers
16133377Seschrock * from using the device.
16143377Seschrock */
16153377Seschrock if (ret && nvlist_lookup_uint64(config,
16163377Seschrock ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
16173377Seschrock stateval = POOL_STATE_SPARE;
16183377Seschrock
16192082Seschrock if (zhp != NULL)
16202082Seschrock zpool_close(zhp);
1621789Sahrens } else {
16221352Seschrock stateval = POOL_STATE_POTENTIALLY_ACTIVE;
16232082Seschrock ret = B_TRUE;
16242082Seschrock }
16252082Seschrock break;
16262082Seschrock
16272082Seschrock case POOL_STATE_SPARE:
16282082Seschrock /*
16292082Seschrock * For a hot spare, it can be either definitively in use, or
16302082Seschrock * potentially active. To determine if it's in use, we iterate
16312082Seschrock * over all pools in the system and search for one with a spare
16322082Seschrock * with a matching guid.
16332082Seschrock *
16342082Seschrock * Due to the shared nature of spares, we don't actually report
16352082Seschrock * the potentially active case as in use. This means the user
16362082Seschrock * can freely create pools on the hot spares of exported pools,
16372082Seschrock * but to do otherwise makes the resulting code complicated, and
16382082Seschrock * we end up having to deal with this case anyway.
16392082Seschrock */
16402082Seschrock cb.cb_zhp = NULL;
16412082Seschrock cb.cb_guid = vdev_guid;
16425450Sbrendan cb.cb_type = ZPOOL_CONFIG_SPARES;
16435450Sbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) {
16445450Sbrendan name = (char *)zpool_get_name(cb.cb_zhp);
16455450Sbrendan ret = TRUE;
16465450Sbrendan } else {
16475450Sbrendan ret = FALSE;
16485450Sbrendan }
16495450Sbrendan break;
16505450Sbrendan
16515450Sbrendan case POOL_STATE_L2CACHE:
16525450Sbrendan
16535450Sbrendan /*
16545450Sbrendan * Check if any pool is currently using this l2cache device.
16555450Sbrendan */
16565450Sbrendan cb.cb_zhp = NULL;
16575450Sbrendan cb.cb_guid = vdev_guid;
16585450Sbrendan cb.cb_type = ZPOOL_CONFIG_L2CACHE;
16595450Sbrendan if (zpool_iter(hdl, find_aux, &cb) == 1) {
16602082Seschrock name = (char *)zpool_get_name(cb.cb_zhp);
1661789Sahrens ret = TRUE;
16622082Seschrock } else {
16632082Seschrock ret = FALSE;
1664789Sahrens }
1665789Sahrens break;
1666789Sahrens
1667789Sahrens default:
16682082Seschrock ret = B_FALSE;
1669789Sahrens }
1670789Sahrens
16711352Seschrock
16721352Seschrock if (ret) {
16732082Seschrock if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
16745501Seschrock if (cb.cb_zhp)
16755501Seschrock zpool_close(cb.cb_zhp);
16762082Seschrock nvlist_free(config);
16772082Seschrock return (-1);
16782082Seschrock }
16791352Seschrock *state = (pool_state_t)stateval;
16801352Seschrock }
16811352Seschrock
16822082Seschrock if (cb.cb_zhp)
16832082Seschrock zpool_close(cb.cb_zhp);
16842082Seschrock
1685789Sahrens nvlist_free(config);
16862082Seschrock *inuse = ret;
16872082Seschrock return (0);
1688789Sahrens }
1689