xref: /titanic_41/usr/src/cmd/zpool/zpool_vdev.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1*fa9e4066Sahrens /*
2*fa9e4066Sahrens  * CDDL HEADER START
3*fa9e4066Sahrens  *
4*fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*fa9e4066Sahrens  * Common Development and Distribution License, Version 1.0 only
6*fa9e4066Sahrens  * (the "License").  You may not use this file except in compliance
7*fa9e4066Sahrens  * with the License.
8*fa9e4066Sahrens  *
9*fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
11*fa9e4066Sahrens  * See the License for the specific language governing permissions
12*fa9e4066Sahrens  * and limitations under the License.
13*fa9e4066Sahrens  *
14*fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15*fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17*fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18*fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19*fa9e4066Sahrens  *
20*fa9e4066Sahrens  * CDDL HEADER END
21*fa9e4066Sahrens  */
22*fa9e4066Sahrens /*
23*fa9e4066Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*fa9e4066Sahrens  * Use is subject to license terms.
25*fa9e4066Sahrens  */
26*fa9e4066Sahrens 
27*fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*fa9e4066Sahrens 
29*fa9e4066Sahrens /*
30*fa9e4066Sahrens  * Functions to convert between a list of vdevs and an nvlist representing the
31*fa9e4066Sahrens  * configuration.  Each entry in the list can be one of:
32*fa9e4066Sahrens  *
33*fa9e4066Sahrens  * 	Device vdevs
34*fa9e4066Sahrens  * 		disk=(path=..., devid=...)
35*fa9e4066Sahrens  * 		file=(path=...)
36*fa9e4066Sahrens  *
37*fa9e4066Sahrens  * 	Group vdevs
38*fa9e4066Sahrens  * 		raidz=(...)
39*fa9e4066Sahrens  * 		mirror=(...)
40*fa9e4066Sahrens  *
41*fa9e4066Sahrens  * While the underlying implementation supports it, group vdevs cannot contain
42*fa9e4066Sahrens  * other group vdevs.  All userland verification of devices is contained within
43*fa9e4066Sahrens  * this file.  If successful, the nvlist returned can be passed directly to the
44*fa9e4066Sahrens  * kernel; we've done as much verification as possible in userland.
45*fa9e4066Sahrens  *
46*fa9e4066Sahrens  * The only function exported by this file is 'get_vdev_spec'.  The function
47*fa9e4066Sahrens  * performs several passes:
48*fa9e4066Sahrens  *
49*fa9e4066Sahrens  * 	1. Construct the vdev specification.  Performs syntax validation and
50*fa9e4066Sahrens  *         makes sure each device is valid.
51*fa9e4066Sahrens  * 	2. Check for devices in use.  Using libdiskmgt, makes sure that no
52*fa9e4066Sahrens  *         devices are also in use.  Some can be overridden using the 'force'
53*fa9e4066Sahrens  *         flag, others cannot.
54*fa9e4066Sahrens  * 	3. Check for replication errors if the 'force' flag is not specified.
55*fa9e4066Sahrens  *         validates that the replication level is consistent across the
56*fa9e4066Sahrens  *         entire pool.
57*fa9e4066Sahrens  * 	4. Label any whole disks with an EFI label.
58*fa9e4066Sahrens  */
59*fa9e4066Sahrens 
60*fa9e4066Sahrens #include <assert.h>
61*fa9e4066Sahrens #include <devid.h>
62*fa9e4066Sahrens #include <errno.h>
63*fa9e4066Sahrens #include <fcntl.h>
64*fa9e4066Sahrens #include <libdiskmgt.h>
65*fa9e4066Sahrens #include <libintl.h>
66*fa9e4066Sahrens #include <libnvpair.h>
67*fa9e4066Sahrens #include <stdio.h>
68*fa9e4066Sahrens #include <string.h>
69*fa9e4066Sahrens #include <unistd.h>
70*fa9e4066Sahrens #include <sys/efi_partition.h>
71*fa9e4066Sahrens #include <sys/stat.h>
72*fa9e4066Sahrens #include <sys/vtoc.h>
73*fa9e4066Sahrens #include <sys/mntent.h>
74*fa9e4066Sahrens 
75*fa9e4066Sahrens #include <libzfs.h>
76*fa9e4066Sahrens 
77*fa9e4066Sahrens #include "zpool_util.h"
78*fa9e4066Sahrens 
79*fa9e4066Sahrens #define	DISK_ROOT	"/dev/dsk"
80*fa9e4066Sahrens #define	RDISK_ROOT	"/dev/rdsk"
81*fa9e4066Sahrens #define	BACKUP_SLICE	"s2"
82*fa9e4066Sahrens 
83*fa9e4066Sahrens /*
84*fa9e4066Sahrens  * For any given vdev specification, we can have multiple errors.  The
85*fa9e4066Sahrens  * vdev_error() function keeps track of whether we have seen an error yet, and
86*fa9e4066Sahrens  * prints out a header if its the first error we've seen.
87*fa9e4066Sahrens  */
88*fa9e4066Sahrens int error_seen;
89*fa9e4066Sahrens int is_force;
90*fa9e4066Sahrens 
91*fa9e4066Sahrens void
92*fa9e4066Sahrens vdev_error(const char *fmt, ...)
93*fa9e4066Sahrens {
94*fa9e4066Sahrens 	va_list ap;
95*fa9e4066Sahrens 
96*fa9e4066Sahrens 	if (!error_seen) {
97*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("invalid vdev specification\n"));
98*fa9e4066Sahrens 		if (!is_force)
99*fa9e4066Sahrens 			(void) fprintf(stderr, gettext("use '-f' to override "
100*fa9e4066Sahrens 			    "the following errors:\n"));
101*fa9e4066Sahrens 		else
102*fa9e4066Sahrens 			(void) fprintf(stderr, gettext("the following errors "
103*fa9e4066Sahrens 			    "must be manually repaired:\n"));
104*fa9e4066Sahrens 		error_seen = TRUE;
105*fa9e4066Sahrens 	}
106*fa9e4066Sahrens 
107*fa9e4066Sahrens 	va_start(ap, fmt);
108*fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
109*fa9e4066Sahrens 	va_end(ap);
110*fa9e4066Sahrens }
111*fa9e4066Sahrens 
112*fa9e4066Sahrens void
113*fa9e4066Sahrens _libdskmgt_error(int err, const char *file, int line)
114*fa9e4066Sahrens {
115*fa9e4066Sahrens 	if (err == 0)
116*fa9e4066Sahrens 		no_memory();
117*fa9e4066Sahrens 
118*fa9e4066Sahrens 	/*
119*fa9e4066Sahrens 	 * Some of the libdiskmgt stuff requires root privileges in order to
120*fa9e4066Sahrens 	 * examine devices.  Bail out gracefully in this case.
121*fa9e4066Sahrens 	 */
122*fa9e4066Sahrens 	if (err == EACCES) {
123*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot determine disk "
124*fa9e4066Sahrens 		    "configuration: permission denied\n"));
125*fa9e4066Sahrens 		exit(1);
126*fa9e4066Sahrens 	}
127*fa9e4066Sahrens 
128*fa9e4066Sahrens 	(void) fprintf(stderr, gettext("internal error: disk configuration "
129*fa9e4066Sahrens 	    "error %d at line %d of file %s\n"), err, line, file);
130*fa9e4066Sahrens 	abort();
131*fa9e4066Sahrens }
132*fa9e4066Sahrens 
133*fa9e4066Sahrens #define	libdskmgt_error(err)	(_libdskmgt_error((err), __FILE__, __LINE__))
134*fa9e4066Sahrens 
135*fa9e4066Sahrens /*
136*fa9e4066Sahrens  * Checks whether a single slice overlaps with any of the slices in the provided
137*fa9e4066Sahrens  * list.  Called by check_overlapping().
138*fa9e4066Sahrens  */
139*fa9e4066Sahrens int
140*fa9e4066Sahrens is_overlapping(dm_descriptor_t slice, dm_descriptor_t media,
141*fa9e4066Sahrens 	dm_descriptor_t *slice_list, int *error, char **overlaps_with)
142*fa9e4066Sahrens {
143*fa9e4066Sahrens 	int 		i = 0;
144*fa9e4066Sahrens 	uint32_t	in_snum;
145*fa9e4066Sahrens 	uint64_t 	start_block = 0;
146*fa9e4066Sahrens 	uint64_t 	end_block = 0;
147*fa9e4066Sahrens 	uint64_t 	media_size = 0;
148*fa9e4066Sahrens 	uint64_t 	size = 0;
149*fa9e4066Sahrens 	nvlist_t 	*media_attrs;
150*fa9e4066Sahrens 	nvlist_t 	*slice_attrs;
151*fa9e4066Sahrens 
152*fa9e4066Sahrens 	media_attrs = dm_get_attributes(media, error);
153*fa9e4066Sahrens 	if (*error != 0) {
154*fa9e4066Sahrens 		return (-1);
155*fa9e4066Sahrens 	}
156*fa9e4066Sahrens 
157*fa9e4066Sahrens 	if (media_attrs == NULL) {
158*fa9e4066Sahrens 		return (0);
159*fa9e4066Sahrens 	}
160*fa9e4066Sahrens 
161*fa9e4066Sahrens 	*error = nvlist_lookup_uint64(media_attrs, DM_NACCESSIBLE, &media_size);
162*fa9e4066Sahrens 	if (*error != 0) {
163*fa9e4066Sahrens 		nvlist_free(media_attrs);
164*fa9e4066Sahrens 		return (-1);
165*fa9e4066Sahrens 	}
166*fa9e4066Sahrens 
167*fa9e4066Sahrens 	slice_attrs = dm_get_attributes(slice, error);
168*fa9e4066Sahrens 	if (*error != 0) {
169*fa9e4066Sahrens 		nvlist_free(media_attrs);
170*fa9e4066Sahrens 		return (-1);
171*fa9e4066Sahrens 	}
172*fa9e4066Sahrens 	/*
173*fa9e4066Sahrens 	 * Not really possible, but the error above would catch any system
174*fa9e4066Sahrens 	 * errors.
175*fa9e4066Sahrens 	 */
176*fa9e4066Sahrens 	if (slice_attrs == NULL) {
177*fa9e4066Sahrens 		nvlist_free(media_attrs);
178*fa9e4066Sahrens 		return (0);
179*fa9e4066Sahrens 	}
180*fa9e4066Sahrens 
181*fa9e4066Sahrens 	*error = nvlist_lookup_uint64(slice_attrs, DM_START, &start_block);
182*fa9e4066Sahrens 	if (*error != 0) {
183*fa9e4066Sahrens 		nvlist_free(media_attrs);
184*fa9e4066Sahrens 		nvlist_free(slice_attrs);
185*fa9e4066Sahrens 		return (-1);
186*fa9e4066Sahrens 	}
187*fa9e4066Sahrens 
188*fa9e4066Sahrens 	*error = nvlist_lookup_uint64(slice_attrs, DM_SIZE, &size);
189*fa9e4066Sahrens 	if (*error != 0) {
190*fa9e4066Sahrens 		nvlist_free(media_attrs);
191*fa9e4066Sahrens 		nvlist_free(slice_attrs);
192*fa9e4066Sahrens 		return (-1);
193*fa9e4066Sahrens 	}
194*fa9e4066Sahrens 	*error = nvlist_lookup_uint32(slice_attrs, DM_INDEX, &in_snum);
195*fa9e4066Sahrens 	if (*error != 0) {
196*fa9e4066Sahrens 		nvlist_free(media_attrs);
197*fa9e4066Sahrens 		nvlist_free(slice_attrs);
198*fa9e4066Sahrens 		return (-1);
199*fa9e4066Sahrens 	}
200*fa9e4066Sahrens 
201*fa9e4066Sahrens 	end_block = (start_block + size) - 1;
202*fa9e4066Sahrens 
203*fa9e4066Sahrens 	for (i = 0; slice_list[i]; i ++) {
204*fa9e4066Sahrens 		uint64_t other_start;
205*fa9e4066Sahrens 		uint64_t other_end;
206*fa9e4066Sahrens 		uint64_t other_size;
207*fa9e4066Sahrens 		uint32_t snum;
208*fa9e4066Sahrens 
209*fa9e4066Sahrens 		nvlist_t *other_attrs = dm_get_attributes(slice_list[i], error);
210*fa9e4066Sahrens 		if (*error != 0) {
211*fa9e4066Sahrens 			return (-1);
212*fa9e4066Sahrens 		}
213*fa9e4066Sahrens 
214*fa9e4066Sahrens 		if (other_attrs == NULL)
215*fa9e4066Sahrens 			continue;
216*fa9e4066Sahrens 
217*fa9e4066Sahrens 		*error = nvlist_lookup_uint64(other_attrs, DM_START,
218*fa9e4066Sahrens 			&other_start);
219*fa9e4066Sahrens 		if (*error) {
220*fa9e4066Sahrens 		    nvlist_free(media_attrs);
221*fa9e4066Sahrens 		    nvlist_free(slice_attrs);
222*fa9e4066Sahrens 		    nvlist_free(other_attrs);
223*fa9e4066Sahrens 		    return (-1);
224*fa9e4066Sahrens 		}
225*fa9e4066Sahrens 
226*fa9e4066Sahrens 		*error = nvlist_lookup_uint64(other_attrs, DM_SIZE,
227*fa9e4066Sahrens 			&other_size);
228*fa9e4066Sahrens 
229*fa9e4066Sahrens 		if (*error) {
230*fa9e4066Sahrens 		    nvlist_free(media_attrs);
231*fa9e4066Sahrens 		    nvlist_free(slice_attrs);
232*fa9e4066Sahrens 		    nvlist_free(other_attrs);
233*fa9e4066Sahrens 		    return (-1);
234*fa9e4066Sahrens 		}
235*fa9e4066Sahrens 
236*fa9e4066Sahrens 		other_end = (other_size + other_start) - 1;
237*fa9e4066Sahrens 
238*fa9e4066Sahrens 		*error = nvlist_lookup_uint32(other_attrs, DM_INDEX,
239*fa9e4066Sahrens 			&snum);
240*fa9e4066Sahrens 
241*fa9e4066Sahrens 		if (*error) {
242*fa9e4066Sahrens 		    nvlist_free(media_attrs);
243*fa9e4066Sahrens 		    nvlist_free(slice_attrs);
244*fa9e4066Sahrens 		    nvlist_free(other_attrs);
245*fa9e4066Sahrens 		    return (-1);
246*fa9e4066Sahrens 		}
247*fa9e4066Sahrens 
248*fa9e4066Sahrens 		/*
249*fa9e4066Sahrens 		 * Check to see if there are > 2 overlapping regions
250*fa9e4066Sahrens 		 * on this media in the same region as this slice.
251*fa9e4066Sahrens 		 * This is done by assuming the following:
252*fa9e4066Sahrens 		 *   	Slice 2 is the backup slice if it is the size
253*fa9e4066Sahrens 		 *	of the whole disk
254*fa9e4066Sahrens 		 * If slice 2 is the overlap and slice 2 is the size of
255*fa9e4066Sahrens 		 * the whole disk, continue. If another slice is found
256*fa9e4066Sahrens 		 * that overlaps with our slice, return it.
257*fa9e4066Sahrens 		 * There is the potential that there is more than one slice
258*fa9e4066Sahrens 		 * that our slice overlaps with, however, we only return
259*fa9e4066Sahrens 		 * the first overlapping slice we find.
260*fa9e4066Sahrens 		 *
261*fa9e4066Sahrens 		 */
262*fa9e4066Sahrens 
263*fa9e4066Sahrens 		if (start_block >= other_start && start_block <= other_end) {
264*fa9e4066Sahrens 			if ((snum == 2 && (other_size == media_size)) ||
265*fa9e4066Sahrens 				snum == in_snum) {
266*fa9e4066Sahrens 				continue;
267*fa9e4066Sahrens 			} else {
268*fa9e4066Sahrens 				char *str = dm_get_name(slice_list[i], error);
269*fa9e4066Sahrens 				if (*error != 0) {
270*fa9e4066Sahrens 					nvlist_free(media_attrs);
271*fa9e4066Sahrens 					nvlist_free(slice_attrs);
272*fa9e4066Sahrens 					nvlist_free(other_attrs);
273*fa9e4066Sahrens 					return (-1);
274*fa9e4066Sahrens 				}
275*fa9e4066Sahrens 				*overlaps_with = strdup(str);
276*fa9e4066Sahrens 				dm_free_name(str);
277*fa9e4066Sahrens 				nvlist_free(media_attrs);
278*fa9e4066Sahrens 				nvlist_free(slice_attrs);
279*fa9e4066Sahrens 				nvlist_free(other_attrs);
280*fa9e4066Sahrens 				return (1);
281*fa9e4066Sahrens 			}
282*fa9e4066Sahrens 		} else if (other_start >= start_block &&
283*fa9e4066Sahrens 			other_start <= end_block) {
284*fa9e4066Sahrens 			if ((snum == 2 && (other_size == media_size)) ||
285*fa9e4066Sahrens 				snum == in_snum) {
286*fa9e4066Sahrens 				continue;
287*fa9e4066Sahrens 			} else {
288*fa9e4066Sahrens 				char *str = dm_get_name(slice_list[i], error);
289*fa9e4066Sahrens 				if (*error != 0) {
290*fa9e4066Sahrens 					nvlist_free(media_attrs);
291*fa9e4066Sahrens 					nvlist_free(slice_attrs);
292*fa9e4066Sahrens 					nvlist_free(other_attrs);
293*fa9e4066Sahrens 					return (-1);
294*fa9e4066Sahrens 				}
295*fa9e4066Sahrens 				*overlaps_with = strdup(str);
296*fa9e4066Sahrens 				dm_free_name(str);
297*fa9e4066Sahrens 				nvlist_free(media_attrs);
298*fa9e4066Sahrens 				nvlist_free(slice_attrs);
299*fa9e4066Sahrens 				nvlist_free(other_attrs);
300*fa9e4066Sahrens 				return (1);
301*fa9e4066Sahrens 			}
302*fa9e4066Sahrens 		}
303*fa9e4066Sahrens 		nvlist_free(other_attrs);
304*fa9e4066Sahrens 	}
305*fa9e4066Sahrens 	nvlist_free(media_attrs);
306*fa9e4066Sahrens 	nvlist_free(slice_attrs);
307*fa9e4066Sahrens 	return (0);
308*fa9e4066Sahrens }
309*fa9e4066Sahrens 
310*fa9e4066Sahrens /*
311*fa9e4066Sahrens  * Check to see whether the given slice overlaps with any other slices.  Get the
312*fa9e4066Sahrens  * associated slice information and pass on to is_overlapping().
313*fa9e4066Sahrens  */
314*fa9e4066Sahrens int
315*fa9e4066Sahrens check_overlapping(const char *slicename, dm_descriptor_t slice)
316*fa9e4066Sahrens {
317*fa9e4066Sahrens 	dm_descriptor_t *media;
318*fa9e4066Sahrens 	dm_descriptor_t *slices;
319*fa9e4066Sahrens 	int error;
320*fa9e4066Sahrens 	char *overlaps;
321*fa9e4066Sahrens 	int ret = 0;
322*fa9e4066Sahrens 
323*fa9e4066Sahrens 	/*
324*fa9e4066Sahrens 	 * Get the list of slices be fetching the associated media, and then all
325*fa9e4066Sahrens 	 * associated slices.
326*fa9e4066Sahrens 	 */
327*fa9e4066Sahrens 	media = dm_get_associated_descriptors(slice, DM_MEDIA, &error);
328*fa9e4066Sahrens 	if (media == NULL || *media == NULL || error != 0)
329*fa9e4066Sahrens 		libdskmgt_error(error);
330*fa9e4066Sahrens 
331*fa9e4066Sahrens 	slices = dm_get_associated_descriptors(*media, DM_SLICE, &error);
332*fa9e4066Sahrens 	if (slices == NULL || *slices == NULL || error != 0)
333*fa9e4066Sahrens 		libdskmgt_error(error);
334*fa9e4066Sahrens 
335*fa9e4066Sahrens 
336*fa9e4066Sahrens 	overlaps = NULL;
337*fa9e4066Sahrens 	if (is_overlapping(slice, *media, slices, &error, &overlaps)) {
338*fa9e4066Sahrens 		vdev_error(gettext("device '%s' overlaps with '%s'\n"),
339*fa9e4066Sahrens 		    slicename, overlaps);
340*fa9e4066Sahrens 		ret = -1;
341*fa9e4066Sahrens 	}
342*fa9e4066Sahrens 
343*fa9e4066Sahrens 	if (overlaps != NULL)
344*fa9e4066Sahrens 		free(overlaps);
345*fa9e4066Sahrens 	dm_free_descriptors(slices);
346*fa9e4066Sahrens 	dm_free_descriptors(media);
347*fa9e4066Sahrens 
348*fa9e4066Sahrens 	return (ret);
349*fa9e4066Sahrens }
350*fa9e4066Sahrens 
351*fa9e4066Sahrens /*
352*fa9e4066Sahrens  * Validate the given slice.  If 'diskname' is non-NULL, then this is a single
353*fa9e4066Sahrens  * slice on a complete disk.  If 'force' is set, then the user specified '-f'
354*fa9e4066Sahrens  * and we only want to report error for completely forbidden uses.
355*fa9e4066Sahrens  */
356*fa9e4066Sahrens int
357*fa9e4066Sahrens check_slice(const char *slicename, dm_descriptor_t slice, int force,
358*fa9e4066Sahrens     int overlap)
359*fa9e4066Sahrens {
360*fa9e4066Sahrens 	nvlist_t *stats;
361*fa9e4066Sahrens 	int err;
362*fa9e4066Sahrens 	nvpair_t *nvwhat, *nvdesc;
363*fa9e4066Sahrens 	char *what, *desc, *name;
364*fa9e4066Sahrens 	int found = FALSE;
365*fa9e4066Sahrens 	int found_zfs = FALSE;
366*fa9e4066Sahrens 	int fd;
367*fa9e4066Sahrens 
368*fa9e4066Sahrens 	if ((stats = dm_get_stats(slice, DM_SLICE_STAT_USE, &err)) == NULL)
369*fa9e4066Sahrens 		libdskmgt_error(err);
370*fa9e4066Sahrens 
371*fa9e4066Sahrens 	/*
372*fa9e4066Sahrens 	 * Always check to see if this is used by an active ZFS pool.
373*fa9e4066Sahrens 	 */
374*fa9e4066Sahrens 	if ((fd = open(slicename, O_RDONLY)) > 0) {
375*fa9e4066Sahrens 		if (zpool_in_use(fd, &desc, &name)) {
376*fa9e4066Sahrens 
377*fa9e4066Sahrens 			if (!force) {
378*fa9e4066Sahrens 				vdev_error(gettext("%s is part of %s pool "
379*fa9e4066Sahrens 				    "'%s'\n"), slicename, desc, name);
380*fa9e4066Sahrens 				found = found_zfs = TRUE;
381*fa9e4066Sahrens 			}
382*fa9e4066Sahrens 
383*fa9e4066Sahrens 			free(desc);
384*fa9e4066Sahrens 			free(name);
385*fa9e4066Sahrens 		}
386*fa9e4066Sahrens 
387*fa9e4066Sahrens 		(void) close(fd);
388*fa9e4066Sahrens 	}
389*fa9e4066Sahrens 
390*fa9e4066Sahrens 	/*
391*fa9e4066Sahrens 	 * This slice is in use.  Print out a descriptive message describing who
392*fa9e4066Sahrens 	 * is using it.  The 'used_by' nvlist is formatted as:
393*fa9e4066Sahrens 	 *
394*fa9e4066Sahrens 	 * 	(used_by=what, used_name=desc, ...)
395*fa9e4066Sahrens 	 *
396*fa9e4066Sahrens 	 * Each 'used_by' must be accompanied by a 'used_name'.
397*fa9e4066Sahrens 	 */
398*fa9e4066Sahrens 	nvdesc = NULL;
399*fa9e4066Sahrens 	for (;;) {
400*fa9e4066Sahrens 		nvwhat = nvlist_next_nvpair(stats, nvdesc);
401*fa9e4066Sahrens 		nvdesc = nvlist_next_nvpair(stats, nvwhat);
402*fa9e4066Sahrens 
403*fa9e4066Sahrens 		if (nvwhat == NULL || nvdesc == NULL)
404*fa9e4066Sahrens 			break;
405*fa9e4066Sahrens 
406*fa9e4066Sahrens 		assert(strcmp(nvpair_name(nvwhat), DM_USED_BY) == 0);
407*fa9e4066Sahrens 		assert(strcmp(nvpair_name(nvdesc), DM_USED_NAME) == 0);
408*fa9e4066Sahrens 
409*fa9e4066Sahrens 		verify(nvpair_value_string(nvwhat, &what) == 0);
410*fa9e4066Sahrens 		verify(nvpair_value_string(nvdesc, &desc) == 0);
411*fa9e4066Sahrens 
412*fa9e4066Sahrens 		/*
413*fa9e4066Sahrens 		 * For currently mounted filesystems, filesystems in
414*fa9e4066Sahrens 		 * /etc/vfstab, or dedicated dump devices, we can never use
415*fa9e4066Sahrens 		 * them, even if '-f' is specified.  The rest of the errors
416*fa9e4066Sahrens 		 * indicate that a filesystem was detected on disk, which can be
417*fa9e4066Sahrens 		 * overridden with '-f'.
418*fa9e4066Sahrens 		 */
419*fa9e4066Sahrens 		if (strcmp(what, DM_USE_MOUNT) == 0 ||
420*fa9e4066Sahrens 		    strcmp(what, DM_USE_VFSTAB) == 0 ||
421*fa9e4066Sahrens 		    strcmp(what, DM_USE_DUMP) == 0) {
422*fa9e4066Sahrens 			found = TRUE;
423*fa9e4066Sahrens 			if (strcmp(what, DM_USE_MOUNT) == 0) {
424*fa9e4066Sahrens 				vdev_error(gettext("%s is "
425*fa9e4066Sahrens 				    "currently mounted on %s\n"),
426*fa9e4066Sahrens 				    slicename, desc);
427*fa9e4066Sahrens 			} else if (strcmp(what, DM_USE_VFSTAB) == 0) {
428*fa9e4066Sahrens 				vdev_error(gettext("%s is usually "
429*fa9e4066Sahrens 				    "mounted at %s in /etc/vfstab\n"),
430*fa9e4066Sahrens 				    slicename, desc);
431*fa9e4066Sahrens 			} else if (strcmp(what, DM_USE_DUMP) == 0) {
432*fa9e4066Sahrens 				vdev_error(gettext("%s is the "
433*fa9e4066Sahrens 				    "dedicated dump device\n"), slicename);
434*fa9e4066Sahrens 			}
435*fa9e4066Sahrens 		} else if (!force) {
436*fa9e4066Sahrens 			found = TRUE;
437*fa9e4066Sahrens 			if (strcmp(what, DM_USE_SVM) == 0) {
438*fa9e4066Sahrens 				vdev_error(gettext("%s is part of "
439*fa9e4066Sahrens 				    "SVM volume %s\n"), slicename, desc);
440*fa9e4066Sahrens 			} else if (strcmp(what, DM_USE_LU) == 0) {
441*fa9e4066Sahrens 				vdev_error(gettext("%s is in use "
442*fa9e4066Sahrens 				    "for live upgrade %s\n"), slicename, desc);
443*fa9e4066Sahrens 			} else if (strcmp(what, DM_USE_VXVM) == 0) {
444*fa9e4066Sahrens 				vdev_error(gettext("%s is part of "
445*fa9e4066Sahrens 				    "VxVM volume %s\n"), slicename, desc);
446*fa9e4066Sahrens 			} else if (strcmp(what, DM_USE_FS) == 0) {
447*fa9e4066Sahrens 				/*
448*fa9e4066Sahrens 				 * We should have already caught ZFS in-use
449*fa9e4066Sahrens 				 * filesystems above.  If the ZFS version is
450*fa9e4066Sahrens 				 * different, or there was some other critical
451*fa9e4066Sahrens 				 * failure, it's possible for fstyp to report it
452*fa9e4066Sahrens 				 * as in-use, but zpool_open_by_dev() to fail.
453*fa9e4066Sahrens 				 */
454*fa9e4066Sahrens 				if (strcmp(desc, MNTTYPE_ZFS) != 0)
455*fa9e4066Sahrens 					vdev_error(gettext("%s contains a %s "
456*fa9e4066Sahrens 					    "filesystem\n"), slicename, desc);
457*fa9e4066Sahrens 				else if (!found_zfs)
458*fa9e4066Sahrens 					vdev_error(gettext("%s is part of an "
459*fa9e4066Sahrens 					    "outdated or damaged ZFS "
460*fa9e4066Sahrens 					    "pool\n"), slicename);
461*fa9e4066Sahrens 			} else {
462*fa9e4066Sahrens 				vdev_error(gettext("is used by %s as %s\n"),
463*fa9e4066Sahrens 				    slicename, what, desc);
464*fa9e4066Sahrens 			}
465*fa9e4066Sahrens 		} else {
466*fa9e4066Sahrens 			found = FALSE;
467*fa9e4066Sahrens 		}
468*fa9e4066Sahrens 	}
469*fa9e4066Sahrens 
470*fa9e4066Sahrens 	/*
471*fa9e4066Sahrens 	 * Perform any overlap checking if requested to do so.
472*fa9e4066Sahrens 	 */
473*fa9e4066Sahrens 	if (overlap && !force)
474*fa9e4066Sahrens 		found |= (check_overlapping(slicename, slice) != 0);
475*fa9e4066Sahrens 
476*fa9e4066Sahrens 	return (found ? -1 : 0);
477*fa9e4066Sahrens }
478*fa9e4066Sahrens 
479*fa9e4066Sahrens /*
480*fa9e4066Sahrens  * Validate a whole disk.  Iterate over all slices on the disk and make sure
481*fa9e4066Sahrens  * that none is in use by calling check_slice().
482*fa9e4066Sahrens  */
483*fa9e4066Sahrens /* ARGSUSED */
484*fa9e4066Sahrens int
485*fa9e4066Sahrens check_disk(const char *name, dm_descriptor_t disk, int force)
486*fa9e4066Sahrens {
487*fa9e4066Sahrens 	dm_descriptor_t *drive, *media, *slice;
488*fa9e4066Sahrens 	int err = 0;
489*fa9e4066Sahrens 	int i;
490*fa9e4066Sahrens 	int ret;
491*fa9e4066Sahrens 
492*fa9e4066Sahrens 	/*
493*fa9e4066Sahrens 	 * Get the drive associated with this disk.  This should never fail,
494*fa9e4066Sahrens 	 * because we already have an alias handle open for the device.
495*fa9e4066Sahrens 	 */
496*fa9e4066Sahrens 	if ((drive = dm_get_associated_descriptors(disk, DM_DRIVE,
497*fa9e4066Sahrens 	    &err)) == NULL || *drive == NULL)
498*fa9e4066Sahrens 		libdskmgt_error(err);
499*fa9e4066Sahrens 
500*fa9e4066Sahrens 	if ((media = dm_get_associated_descriptors(*drive, DM_MEDIA,
501*fa9e4066Sahrens 	    &err)) == NULL)
502*fa9e4066Sahrens 		libdskmgt_error(err);
503*fa9e4066Sahrens 
504*fa9e4066Sahrens 	dm_free_descriptors(drive);
505*fa9e4066Sahrens 
506*fa9e4066Sahrens 	/*
507*fa9e4066Sahrens 	 * It is possible that the user has specified a removable media drive,
508*fa9e4066Sahrens 	 * and the media is not present.
509*fa9e4066Sahrens 	 */
510*fa9e4066Sahrens 	if (*media == NULL) {
511*fa9e4066Sahrens 		vdev_error(gettext("'%s' has no media in drive\n"), name);
512*fa9e4066Sahrens 		dm_free_descriptors(media);
513*fa9e4066Sahrens 		return (-1);
514*fa9e4066Sahrens 	}
515*fa9e4066Sahrens 
516*fa9e4066Sahrens 	if ((slice = dm_get_associated_descriptors(*media, DM_SLICE,
517*fa9e4066Sahrens 	    &err)) == NULL)
518*fa9e4066Sahrens 		libdskmgt_error(err);
519*fa9e4066Sahrens 
520*fa9e4066Sahrens 	dm_free_descriptors(media);
521*fa9e4066Sahrens 
522*fa9e4066Sahrens 	ret = 0;
523*fa9e4066Sahrens 
524*fa9e4066Sahrens 	/*
525*fa9e4066Sahrens 	 * Iterate over all slices and report any errors.  We don't care about
526*fa9e4066Sahrens 	 * overlapping slices because we are using the whole disk.
527*fa9e4066Sahrens 	 */
528*fa9e4066Sahrens 	for (i = 0; slice[i] != NULL; i++) {
529*fa9e4066Sahrens 		if (check_slice(dm_get_name(slice[i], &err), slice[i],
530*fa9e4066Sahrens 		    force, FALSE) != 0)
531*fa9e4066Sahrens 			ret = -1;
532*fa9e4066Sahrens 	}
533*fa9e4066Sahrens 
534*fa9e4066Sahrens 	dm_free_descriptors(slice);
535*fa9e4066Sahrens 	return (ret);
536*fa9e4066Sahrens }
537*fa9e4066Sahrens 
538*fa9e4066Sahrens 
539*fa9e4066Sahrens /*
540*fa9e4066Sahrens  * Validate a device.  Determines whether the device is a disk, slice, or
541*fa9e4066Sahrens  * partition, and passes it off to an appropriate function.
542*fa9e4066Sahrens  */
543*fa9e4066Sahrens int
544*fa9e4066Sahrens check_device(const char *path, int force)
545*fa9e4066Sahrens {
546*fa9e4066Sahrens 	dm_descriptor_t desc;
547*fa9e4066Sahrens 	int err;
548*fa9e4066Sahrens 	char *dev, rpath[MAXPATHLEN];
549*fa9e4066Sahrens 
550*fa9e4066Sahrens 	/*
551*fa9e4066Sahrens 	 * For whole disks, libdiskmgt does not include the leading dev path.
552*fa9e4066Sahrens 	 */
553*fa9e4066Sahrens 	dev = strrchr(path, '/');
554*fa9e4066Sahrens 	assert(dev != NULL);
555*fa9e4066Sahrens 	dev++;
556*fa9e4066Sahrens 	if ((desc = dm_get_descriptor_by_name(DM_ALIAS, dev, &err)) != NULL)
557*fa9e4066Sahrens 		return (check_disk(path, desc, force));
558*fa9e4066Sahrens 
559*fa9e4066Sahrens 	/*
560*fa9e4066Sahrens 	 * If 'err' is not ENODEV, then we've had an unexpected error from
561*fa9e4066Sahrens 	 * libdiskmgt.  The only explanation is that we ran out of memory.
562*fa9e4066Sahrens 	 */
563*fa9e4066Sahrens 	if (err != ENODEV)
564*fa9e4066Sahrens 		libdskmgt_error(err);
565*fa9e4066Sahrens 
566*fa9e4066Sahrens 	/*
567*fa9e4066Sahrens 	 * Determine if this is a slice.
568*fa9e4066Sahrens 	 */
569*fa9e4066Sahrens 	if ((desc = dm_get_descriptor_by_name(DM_SLICE, (char *)path, &err))
570*fa9e4066Sahrens 	    != NULL)
571*fa9e4066Sahrens 		return (check_slice(path, desc, force, TRUE));
572*fa9e4066Sahrens 
573*fa9e4066Sahrens 	if (err != ENODEV)
574*fa9e4066Sahrens 		libdskmgt_error(err);
575*fa9e4066Sahrens 
576*fa9e4066Sahrens 	/*
577*fa9e4066Sahrens 	 * Check for a partition.  libdiskmgt expects path of /dev/rdsk when
578*fa9e4066Sahrens 	 * dealing with partitions, so convert it.
579*fa9e4066Sahrens 	 */
580*fa9e4066Sahrens 	(void) snprintf(rpath, sizeof (rpath), "/dev/rdsk/%s", dev);
581*fa9e4066Sahrens 	if ((desc = dm_get_descriptor_by_name(DM_PARTITION, rpath, &err))
582*fa9e4066Sahrens 	    != NULL) {
583*fa9e4066Sahrens 		/* XXZFS perform checking on partitions */
584*fa9e4066Sahrens 		return (0);
585*fa9e4066Sahrens 	}
586*fa9e4066Sahrens 
587*fa9e4066Sahrens 	if (err != ENODEV)
588*fa9e4066Sahrens 		libdskmgt_error(err);
589*fa9e4066Sahrens 
590*fa9e4066Sahrens 	/*
591*fa9e4066Sahrens 	 * At this point, libdiskmgt failed to find the device as either a whole
592*fa9e4066Sahrens 	 * disk or a slice.  Ignore these errors, as we know that it at least a
593*fa9e4066Sahrens 	 * block device.  The user may have provided us with some unknown device
594*fa9e4066Sahrens 	 * that libdiskmgt doesn't know about.
595*fa9e4066Sahrens 	 */
596*fa9e4066Sahrens 	return (0);
597*fa9e4066Sahrens }
598*fa9e4066Sahrens 
599*fa9e4066Sahrens /*
600*fa9e4066Sahrens  * Check that a file is valid.  All we can do in this case is check that it's
601*fa9e4066Sahrens  * not in use by another pool.
602*fa9e4066Sahrens  */
603*fa9e4066Sahrens int
604*fa9e4066Sahrens check_file(const char *file, int force)
605*fa9e4066Sahrens {
606*fa9e4066Sahrens 	char *desc, *name;
607*fa9e4066Sahrens 	int fd;
608*fa9e4066Sahrens 	int ret = 0;
609*fa9e4066Sahrens 
610*fa9e4066Sahrens 	if ((fd = open(file, O_RDONLY)) < 0)
611*fa9e4066Sahrens 		return (0);
612*fa9e4066Sahrens 
613*fa9e4066Sahrens 	if (zpool_in_use(fd, &desc, &name)) {
614*fa9e4066Sahrens 		if (strcmp(desc, gettext("active")) == 0 ||
615*fa9e4066Sahrens 		    !force) {
616*fa9e4066Sahrens 			vdev_error(gettext("%s is part of %s pool '%s'\n"),
617*fa9e4066Sahrens 			    file, desc, name);
618*fa9e4066Sahrens 			ret = -1;
619*fa9e4066Sahrens 		}
620*fa9e4066Sahrens 
621*fa9e4066Sahrens 		free(desc);
622*fa9e4066Sahrens 		free(name);
623*fa9e4066Sahrens 	}
624*fa9e4066Sahrens 
625*fa9e4066Sahrens 	(void) close(fd);
626*fa9e4066Sahrens 	return (ret);
627*fa9e4066Sahrens }
628*fa9e4066Sahrens 
629*fa9e4066Sahrens static int
630*fa9e4066Sahrens is_whole_disk(const char *arg, struct stat64 *statbuf)
631*fa9e4066Sahrens {
632*fa9e4066Sahrens 	char path[MAXPATHLEN];
633*fa9e4066Sahrens 
634*fa9e4066Sahrens 	(void) snprintf(path, sizeof (path), "%s%s", arg, BACKUP_SLICE);
635*fa9e4066Sahrens 	if (stat64(path, statbuf) == 0)
636*fa9e4066Sahrens 		return (TRUE);
637*fa9e4066Sahrens 
638*fa9e4066Sahrens 	return (FALSE);
639*fa9e4066Sahrens }
640*fa9e4066Sahrens 
641*fa9e4066Sahrens /*
642*fa9e4066Sahrens  * Create a leaf vdev.  Determine if this is a file or a device.  If it's a
643*fa9e4066Sahrens  * device, fill in the device id to make a complete nvlist.  Valid forms for a
644*fa9e4066Sahrens  * leaf vdev are:
645*fa9e4066Sahrens  *
646*fa9e4066Sahrens  * 	/dev/dsk/xxx	Complete disk path
647*fa9e4066Sahrens  * 	/xxx		Full path to file
648*fa9e4066Sahrens  * 	xxx		Shorthand for /dev/dsk/xxx
649*fa9e4066Sahrens  */
650*fa9e4066Sahrens nvlist_t *
651*fa9e4066Sahrens make_leaf_vdev(const char *arg)
652*fa9e4066Sahrens {
653*fa9e4066Sahrens 	char path[MAXPATHLEN];
654*fa9e4066Sahrens 	struct stat64 statbuf;
655*fa9e4066Sahrens 	nvlist_t *vdev = NULL;
656*fa9e4066Sahrens 	char *type = NULL;
657*fa9e4066Sahrens 	int wholedisk = FALSE;
658*fa9e4066Sahrens 
659*fa9e4066Sahrens 	/*
660*fa9e4066Sahrens 	 * Determine what type of vdev this is, and put the full path into
661*fa9e4066Sahrens 	 * 'path'.  We detect whether this is a device of file afterwards by
662*fa9e4066Sahrens 	 * checking the st_mode of the file.
663*fa9e4066Sahrens 	 */
664*fa9e4066Sahrens 	if (arg[0] == '/') {
665*fa9e4066Sahrens 		/*
666*fa9e4066Sahrens 		 * Complete device or file path.  Exact type is determined by
667*fa9e4066Sahrens 		 * examining the file descriptor afterwards.
668*fa9e4066Sahrens 		 */
669*fa9e4066Sahrens 		if (is_whole_disk(arg, &statbuf)) {
670*fa9e4066Sahrens 			wholedisk = TRUE;
671*fa9e4066Sahrens 		} else if (stat64(arg, &statbuf) != 0) {
672*fa9e4066Sahrens 			(void) fprintf(stderr,
673*fa9e4066Sahrens 			    gettext("cannot open '%s': %s\n"),
674*fa9e4066Sahrens 			    arg, strerror(errno));
675*fa9e4066Sahrens 			return (NULL);
676*fa9e4066Sahrens 		}
677*fa9e4066Sahrens 
678*fa9e4066Sahrens 		(void) strlcpy(path, arg, sizeof (path));
679*fa9e4066Sahrens 	} else {
680*fa9e4066Sahrens 		/*
681*fa9e4066Sahrens 		 * This may be a short path for a device, or it could be total
682*fa9e4066Sahrens 		 * gibberish.  Check to see if it's a known device in
683*fa9e4066Sahrens 		 * /dev/dsk/.  As part of this check, see if we've been given a
684*fa9e4066Sahrens 		 * an entire disk (minus the slice number).
685*fa9e4066Sahrens 		 */
686*fa9e4066Sahrens 		(void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT,
687*fa9e4066Sahrens 		    arg);
688*fa9e4066Sahrens 		if (is_whole_disk(path, &statbuf)) {
689*fa9e4066Sahrens 			wholedisk = TRUE;
690*fa9e4066Sahrens 		} else if (stat64(path, &statbuf) != 0) {
691*fa9e4066Sahrens 			/*
692*fa9e4066Sahrens 			 * If we got ENOENT, then the user gave us
693*fa9e4066Sahrens 			 * gibberish, so try to direct them with a
694*fa9e4066Sahrens 			 * reasonable error message.  Otherwise,
695*fa9e4066Sahrens 			 * regurgitate strerror() since it's the best we
696*fa9e4066Sahrens 			 * can do.
697*fa9e4066Sahrens 			 */
698*fa9e4066Sahrens 			if (errno == ENOENT) {
699*fa9e4066Sahrens 				(void) fprintf(stderr,
700*fa9e4066Sahrens 				    gettext("cannot open '%s': no such "
701*fa9e4066Sahrens 				    "device in %s\n"), arg, DISK_ROOT);
702*fa9e4066Sahrens 				(void) fprintf(stderr,
703*fa9e4066Sahrens 				    gettext("must be a full path or "
704*fa9e4066Sahrens 				    "shorthand device name\n"));
705*fa9e4066Sahrens 				return (NULL);
706*fa9e4066Sahrens 			} else {
707*fa9e4066Sahrens 				(void) fprintf(stderr,
708*fa9e4066Sahrens 				    gettext("cannot open '%s': %s\n"),
709*fa9e4066Sahrens 				    path, strerror(errno));
710*fa9e4066Sahrens 				return (NULL);
711*fa9e4066Sahrens 			}
712*fa9e4066Sahrens 		}
713*fa9e4066Sahrens 	}
714*fa9e4066Sahrens 
715*fa9e4066Sahrens 	/*
716*fa9e4066Sahrens 	 * Determine whether this is a device or a file.
717*fa9e4066Sahrens 	 */
718*fa9e4066Sahrens 	if (S_ISBLK(statbuf.st_mode)) {
719*fa9e4066Sahrens 		type = VDEV_TYPE_DISK;
720*fa9e4066Sahrens 	} else if (S_ISREG(statbuf.st_mode)) {
721*fa9e4066Sahrens 		type = VDEV_TYPE_FILE;
722*fa9e4066Sahrens 	} else {
723*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot use '%s': must be a "
724*fa9e4066Sahrens 		    "block device or regular file\n"), path);
725*fa9e4066Sahrens 		return (NULL);
726*fa9e4066Sahrens 	}
727*fa9e4066Sahrens 
728*fa9e4066Sahrens 	/*
729*fa9e4066Sahrens 	 * Finally, we have the complete device or file, and we know that it is
730*fa9e4066Sahrens 	 * acceptable to use.  Construct the nvlist to describe this vdev.  All
731*fa9e4066Sahrens 	 * vdevs have a 'path' element, and devices also have a 'devid' element.
732*fa9e4066Sahrens 	 */
733*fa9e4066Sahrens 	verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
734*fa9e4066Sahrens 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
735*fa9e4066Sahrens 	verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
736*fa9e4066Sahrens 
737*fa9e4066Sahrens 	/*
738*fa9e4066Sahrens 	 * For a whole disk, defer getting its devid until after labeling it.
739*fa9e4066Sahrens 	 */
740*fa9e4066Sahrens 	if (S_ISBLK(statbuf.st_mode) && !wholedisk) {
741*fa9e4066Sahrens 		/*
742*fa9e4066Sahrens 		 * Get the devid for the device.
743*fa9e4066Sahrens 		 */
744*fa9e4066Sahrens 		int fd;
745*fa9e4066Sahrens 		ddi_devid_t devid;
746*fa9e4066Sahrens 		char *minor = NULL, *devid_str = NULL;
747*fa9e4066Sahrens 
748*fa9e4066Sahrens 		if ((fd = open(path, O_RDONLY)) < 0) {
749*fa9e4066Sahrens 			(void) fprintf(stderr, gettext("cannot open '%s': "
750*fa9e4066Sahrens 			    "%s\n"), path, strerror(errno));
751*fa9e4066Sahrens 			nvlist_free(vdev);
752*fa9e4066Sahrens 			return (NULL);
753*fa9e4066Sahrens 		}
754*fa9e4066Sahrens 
755*fa9e4066Sahrens 		if (devid_get(fd, &devid) == 0) {
756*fa9e4066Sahrens 			if (devid_get_minor_name(fd, &minor) == 0 &&
757*fa9e4066Sahrens 			    (devid_str = devid_str_encode(devid, minor)) !=
758*fa9e4066Sahrens 			    NULL) {
759*fa9e4066Sahrens 				verify(nvlist_add_string(vdev,
760*fa9e4066Sahrens 				    ZPOOL_CONFIG_DEVID, devid_str) == 0);
761*fa9e4066Sahrens 			}
762*fa9e4066Sahrens 			if (devid_str != NULL)
763*fa9e4066Sahrens 				devid_str_free(devid_str);
764*fa9e4066Sahrens 			if (minor != NULL)
765*fa9e4066Sahrens 				devid_str_free(minor);
766*fa9e4066Sahrens 			devid_free(devid);
767*fa9e4066Sahrens 		}
768*fa9e4066Sahrens 
769*fa9e4066Sahrens 		(void) close(fd);
770*fa9e4066Sahrens 	}
771*fa9e4066Sahrens 
772*fa9e4066Sahrens 	return (vdev);
773*fa9e4066Sahrens }
774*fa9e4066Sahrens 
775*fa9e4066Sahrens /*
776*fa9e4066Sahrens  * Go through and verify the replication level of the pool is consistent.
777*fa9e4066Sahrens  * Performs the following checks:
778*fa9e4066Sahrens  *
779*fa9e4066Sahrens  * 	For the new spec, verifies that devices in mirrors and raidz are the
780*fa9e4066Sahrens  * 	same size.
781*fa9e4066Sahrens  *
782*fa9e4066Sahrens  * 	If the current configuration already has inconsistent replication
783*fa9e4066Sahrens  * 	levels, ignore any other potential problems in the new spec.
784*fa9e4066Sahrens  *
785*fa9e4066Sahrens  * 	Otherwise, make sure that the current spec (if there is one) and the new
786*fa9e4066Sahrens  * 	spec have consistent replication levels.
787*fa9e4066Sahrens  */
788*fa9e4066Sahrens typedef struct replication_level {
789*fa9e4066Sahrens 	char	*type;
790*fa9e4066Sahrens 	int	level;
791*fa9e4066Sahrens } replication_level_t;
792*fa9e4066Sahrens 
793*fa9e4066Sahrens /*
794*fa9e4066Sahrens  * Given a list of toplevel vdevs, return the current replication level.  If
795*fa9e4066Sahrens  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
796*fa9e4066Sahrens  * an error message will be displayed for each self-inconsistent vdev.
797*fa9e4066Sahrens  */
798*fa9e4066Sahrens replication_level_t *
799*fa9e4066Sahrens get_replication(nvlist_t *nvroot, int fatal)
800*fa9e4066Sahrens {
801*fa9e4066Sahrens 	nvlist_t **top;
802*fa9e4066Sahrens 	uint_t t, toplevels;
803*fa9e4066Sahrens 	nvlist_t **child;
804*fa9e4066Sahrens 	uint_t c, children;
805*fa9e4066Sahrens 	nvlist_t *nv;
806*fa9e4066Sahrens 	char *type;
807*fa9e4066Sahrens 	replication_level_t lastrep, rep, *ret;
808*fa9e4066Sahrens 	int dontreport;
809*fa9e4066Sahrens 
810*fa9e4066Sahrens 	ret = safe_malloc(sizeof (replication_level_t));
811*fa9e4066Sahrens 
812*fa9e4066Sahrens 	verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
813*fa9e4066Sahrens 	    &top, &toplevels) == 0);
814*fa9e4066Sahrens 
815*fa9e4066Sahrens 	lastrep.type = NULL;
816*fa9e4066Sahrens 	for (t = 0; t < toplevels; t++) {
817*fa9e4066Sahrens 		nv = top[t];
818*fa9e4066Sahrens 
819*fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
820*fa9e4066Sahrens 
821*fa9e4066Sahrens 		if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
822*fa9e4066Sahrens 		    &child, &children) != 0) {
823*fa9e4066Sahrens 			/*
824*fa9e4066Sahrens 			 * This is a 'file' or 'disk' vdev.
825*fa9e4066Sahrens 			 */
826*fa9e4066Sahrens 			rep.type = type;
827*fa9e4066Sahrens 			rep.level = 1;
828*fa9e4066Sahrens 		} else {
829*fa9e4066Sahrens 			uint64_t vdev_size;
830*fa9e4066Sahrens 
831*fa9e4066Sahrens 			/*
832*fa9e4066Sahrens 			 * This is a mirror or RAID-Z vdev.  Go through and make
833*fa9e4066Sahrens 			 * sure the contents are all the same (files vs. disks),
834*fa9e4066Sahrens 			 * keeping track of the number of elements in the
835*fa9e4066Sahrens 			 * process.
836*fa9e4066Sahrens 			 *
837*fa9e4066Sahrens 			 * We also check that the size of each vdev (if it can
838*fa9e4066Sahrens 			 * be determined) is the same.
839*fa9e4066Sahrens 			 */
840*fa9e4066Sahrens 			rep.type = type;
841*fa9e4066Sahrens 			rep.level = 0;
842*fa9e4066Sahrens 
843*fa9e4066Sahrens 			/*
844*fa9e4066Sahrens 			 * The 'dontreport' variable indicatest that we've
845*fa9e4066Sahrens 			 * already reported an error for this spec, so don't
846*fa9e4066Sahrens 			 * bother doing it again.
847*fa9e4066Sahrens 			 */
848*fa9e4066Sahrens 			type = NULL;
849*fa9e4066Sahrens 			dontreport = 0;
850*fa9e4066Sahrens 			vdev_size = -1ULL;
851*fa9e4066Sahrens 			for (c = 0; c < children; c++) {
852*fa9e4066Sahrens 				nvlist_t *cnv = child[c];
853*fa9e4066Sahrens 				char *path;
854*fa9e4066Sahrens 				struct stat64 statbuf;
855*fa9e4066Sahrens 				uint64_t size = -1ULL;
856*fa9e4066Sahrens 				char *childtype;
857*fa9e4066Sahrens 				int fd, err;
858*fa9e4066Sahrens 
859*fa9e4066Sahrens 				rep.level++;
860*fa9e4066Sahrens 
861*fa9e4066Sahrens 				verify(nvlist_lookup_string(cnv,
862*fa9e4066Sahrens 				    ZPOOL_CONFIG_TYPE, &childtype) == 0);
863*fa9e4066Sahrens 				verify(nvlist_lookup_string(cnv,
864*fa9e4066Sahrens 				    ZPOOL_CONFIG_PATH, &path) == 0);
865*fa9e4066Sahrens 
866*fa9e4066Sahrens 				/*
867*fa9e4066Sahrens 				 * If we have a raidz/mirror that combines disks
868*fa9e4066Sahrens 				 * with files, report it as an error.
869*fa9e4066Sahrens 				 */
870*fa9e4066Sahrens 				if (!dontreport && type != NULL &&
871*fa9e4066Sahrens 				    strcmp(type, childtype) != 0) {
872*fa9e4066Sahrens 					if (ret != NULL)
873*fa9e4066Sahrens 						free(ret);
874*fa9e4066Sahrens 					ret = NULL;
875*fa9e4066Sahrens 					if (fatal)
876*fa9e4066Sahrens 						vdev_error(gettext(
877*fa9e4066Sahrens 						    "mismatched replication "
878*fa9e4066Sahrens 						    "level: %s contains both "
879*fa9e4066Sahrens 						    "files and devices\n"),
880*fa9e4066Sahrens 						    rep.type);
881*fa9e4066Sahrens 					else
882*fa9e4066Sahrens 						return (NULL);
883*fa9e4066Sahrens 					dontreport = TRUE;
884*fa9e4066Sahrens 				}
885*fa9e4066Sahrens 
886*fa9e4066Sahrens 				/*
887*fa9e4066Sahrens 				 * According to stat(2), the value of 'st_size'
888*fa9e4066Sahrens 				 * is undefined for block devices and character
889*fa9e4066Sahrens 				 * devices.  But there is no effective way to
890*fa9e4066Sahrens 				 * determine the real size in userland.
891*fa9e4066Sahrens 				 *
892*fa9e4066Sahrens 				 * Instead, we'll take advantage of an
893*fa9e4066Sahrens 				 * implementation detail of spec_size().  If the
894*fa9e4066Sahrens 				 * device is currently open, then we (should)
895*fa9e4066Sahrens 				 * return a valid size.
896*fa9e4066Sahrens 				 *
897*fa9e4066Sahrens 				 * If we still don't get a valid size (indicated
898*fa9e4066Sahrens 				 * by a size of 0 or MAXOFFSET_T), then ignore
899*fa9e4066Sahrens 				 * this device altogether.
900*fa9e4066Sahrens 				 */
901*fa9e4066Sahrens 				if ((fd = open(path, O_RDONLY)) >= 0) {
902*fa9e4066Sahrens 					err = fstat64(fd, &statbuf);
903*fa9e4066Sahrens 					(void) close(fd);
904*fa9e4066Sahrens 				} else {
905*fa9e4066Sahrens 					err = stat64(path, &statbuf);
906*fa9e4066Sahrens 				}
907*fa9e4066Sahrens 
908*fa9e4066Sahrens 				if (err != 0 ||
909*fa9e4066Sahrens 				    statbuf.st_size == 0 ||
910*fa9e4066Sahrens 				    statbuf.st_size == MAXOFFSET_T)
911*fa9e4066Sahrens 					continue;
912*fa9e4066Sahrens 
913*fa9e4066Sahrens 				size = statbuf.st_size;
914*fa9e4066Sahrens 
915*fa9e4066Sahrens 				/*
916*fa9e4066Sahrens 				 * Also check the size of each device.  If they
917*fa9e4066Sahrens 				 * differ, then report an error.
918*fa9e4066Sahrens 				 */
919*fa9e4066Sahrens 				if (!dontreport && vdev_size != -1ULL &&
920*fa9e4066Sahrens 				    size != vdev_size) {
921*fa9e4066Sahrens 					if (ret != NULL)
922*fa9e4066Sahrens 						free(ret);
923*fa9e4066Sahrens 					ret = NULL;
924*fa9e4066Sahrens 					if (fatal)
925*fa9e4066Sahrens 						vdev_error(gettext(
926*fa9e4066Sahrens 						    "%s contains devices of "
927*fa9e4066Sahrens 						    "different sizes\n"),
928*fa9e4066Sahrens 						    rep.type);
929*fa9e4066Sahrens 					else
930*fa9e4066Sahrens 						return (NULL);
931*fa9e4066Sahrens 					dontreport = TRUE;
932*fa9e4066Sahrens 				}
933*fa9e4066Sahrens 
934*fa9e4066Sahrens 				type = childtype;
935*fa9e4066Sahrens 				vdev_size = size;
936*fa9e4066Sahrens 			}
937*fa9e4066Sahrens 		}
938*fa9e4066Sahrens 
939*fa9e4066Sahrens 		/*
940*fa9e4066Sahrens 		 * At this point, we have the replication of the last toplevel
941*fa9e4066Sahrens 		 * vdev in 'rep'.  Compare it to 'lastrep' to see if its
942*fa9e4066Sahrens 		 * different.
943*fa9e4066Sahrens 		 */
944*fa9e4066Sahrens 		if (lastrep.type != NULL) {
945*fa9e4066Sahrens 			if (strcmp(lastrep.type, rep.type) != 0) {
946*fa9e4066Sahrens 				if (ret != NULL)
947*fa9e4066Sahrens 					free(ret);
948*fa9e4066Sahrens 				ret = NULL;
949*fa9e4066Sahrens 				if (fatal)
950*fa9e4066Sahrens 					vdev_error(gettext(
951*fa9e4066Sahrens 					    "mismatched replication "
952*fa9e4066Sahrens 					    "level: both %s and %s vdevs are "
953*fa9e4066Sahrens 					    "present\n"),
954*fa9e4066Sahrens 					    lastrep.type, rep.type);
955*fa9e4066Sahrens 				else
956*fa9e4066Sahrens 					return (NULL);
957*fa9e4066Sahrens 			} else if (lastrep.level != rep.level) {
958*fa9e4066Sahrens 				if (ret)
959*fa9e4066Sahrens 					free(ret);
960*fa9e4066Sahrens 				ret = NULL;
961*fa9e4066Sahrens 				if (fatal)
962*fa9e4066Sahrens 					vdev_error(gettext(
963*fa9e4066Sahrens 					    "mismatched replication "
964*fa9e4066Sahrens 					    "level: %d-way %s and %d-way %s "
965*fa9e4066Sahrens 					    "vdevs are present\n"),
966*fa9e4066Sahrens 					    lastrep.level, lastrep.type,
967*fa9e4066Sahrens 					    rep.level, rep.type);
968*fa9e4066Sahrens 				else
969*fa9e4066Sahrens 					return (NULL);
970*fa9e4066Sahrens 			}
971*fa9e4066Sahrens 		}
972*fa9e4066Sahrens 		lastrep = rep;
973*fa9e4066Sahrens 	}
974*fa9e4066Sahrens 
975*fa9e4066Sahrens 	if (ret != NULL) {
976*fa9e4066Sahrens 		ret->type = rep.type;
977*fa9e4066Sahrens 		ret->level = rep.level;
978*fa9e4066Sahrens 	}
979*fa9e4066Sahrens 
980*fa9e4066Sahrens 	return (ret);
981*fa9e4066Sahrens }
982*fa9e4066Sahrens 
983*fa9e4066Sahrens /*
984*fa9e4066Sahrens  * Check the replication level of the vdev spec against the current pool.  Calls
985*fa9e4066Sahrens  * get_replication() to make sure the new spec is self-consistent.  If the pool
986*fa9e4066Sahrens  * has a consistent replication level, then we ignore any errors.  Otherwise,
987*fa9e4066Sahrens  * report any difference between the two.
988*fa9e4066Sahrens  */
989*fa9e4066Sahrens int
990*fa9e4066Sahrens check_replication(nvlist_t *config, nvlist_t *newroot)
991*fa9e4066Sahrens {
992*fa9e4066Sahrens 	replication_level_t *current = NULL, *new;
993*fa9e4066Sahrens 	int ret;
994*fa9e4066Sahrens 
995*fa9e4066Sahrens 	/*
996*fa9e4066Sahrens 	 * If we have a current pool configuration, check to see if it's
997*fa9e4066Sahrens 	 * self-consistent.  If not, simply return success.
998*fa9e4066Sahrens 	 */
999*fa9e4066Sahrens 	if (config != NULL) {
1000*fa9e4066Sahrens 		nvlist_t *nvroot;
1001*fa9e4066Sahrens 
1002*fa9e4066Sahrens 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1003*fa9e4066Sahrens 		    &nvroot) == 0);
1004*fa9e4066Sahrens 		if ((current = get_replication(nvroot, FALSE)) == NULL)
1005*fa9e4066Sahrens 			return (0);
1006*fa9e4066Sahrens 	}
1007*fa9e4066Sahrens 
1008*fa9e4066Sahrens 	/*
1009*fa9e4066Sahrens 	 * Get the replication level of the new vdev spec, reporting any
1010*fa9e4066Sahrens 	 * inconsistencies found.
1011*fa9e4066Sahrens 	 */
1012*fa9e4066Sahrens 	if ((new = get_replication(newroot, TRUE)) == NULL) {
1013*fa9e4066Sahrens 		free(current);
1014*fa9e4066Sahrens 		return (-1);
1015*fa9e4066Sahrens 	}
1016*fa9e4066Sahrens 
1017*fa9e4066Sahrens 	/*
1018*fa9e4066Sahrens 	 * Check to see if the new vdev spec matches the replication level of
1019*fa9e4066Sahrens 	 * the current pool.
1020*fa9e4066Sahrens 	 */
1021*fa9e4066Sahrens 	ret = 0;
1022*fa9e4066Sahrens 	if (current != NULL) {
1023*fa9e4066Sahrens 		if (strcmp(current->type, new->type) != 0 ||
1024*fa9e4066Sahrens 		    current->level != new->level) {
1025*fa9e4066Sahrens 			vdev_error(gettext(
1026*fa9e4066Sahrens 			    "mismatched replication level: pool uses %d-way %s "
1027*fa9e4066Sahrens 			    "and new vdev uses %d-way %s\n"),
1028*fa9e4066Sahrens 			    current->level, current->type, new->level,
1029*fa9e4066Sahrens 			    new->type);
1030*fa9e4066Sahrens 			ret = -1;
1031*fa9e4066Sahrens 		}
1032*fa9e4066Sahrens 	}
1033*fa9e4066Sahrens 
1034*fa9e4066Sahrens 	free(new);
1035*fa9e4066Sahrens 	if (current != NULL)
1036*fa9e4066Sahrens 		free(current);
1037*fa9e4066Sahrens 
1038*fa9e4066Sahrens 	return (ret);
1039*fa9e4066Sahrens }
1040*fa9e4066Sahrens 
1041*fa9e4066Sahrens /*
1042*fa9e4066Sahrens  * Label an individual disk.  The name provided is the short name, stripped of
1043*fa9e4066Sahrens  * any leading /dev path.
1044*fa9e4066Sahrens  */
1045*fa9e4066Sahrens int
1046*fa9e4066Sahrens label_disk(char *name)
1047*fa9e4066Sahrens {
1048*fa9e4066Sahrens 	char path[MAXPATHLEN];
1049*fa9e4066Sahrens 	struct dk_gpt *vtoc;
1050*fa9e4066Sahrens 	int fd;
1051*fa9e4066Sahrens 	size_t resv = 16384;
1052*fa9e4066Sahrens 
1053*fa9e4066Sahrens 	(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
1054*fa9e4066Sahrens 	    BACKUP_SLICE);
1055*fa9e4066Sahrens 
1056*fa9e4066Sahrens 	if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
1057*fa9e4066Sahrens 		/*
1058*fa9e4066Sahrens 		 * This shouldn't happen.  We've long since verified that this
1059*fa9e4066Sahrens 		 * is a valid device.
1060*fa9e4066Sahrens 		 */
1061*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
1062*fa9e4066Sahrens 		    path, strerror(errno));
1063*fa9e4066Sahrens 		return (-1);
1064*fa9e4066Sahrens 	}
1065*fa9e4066Sahrens 
1066*fa9e4066Sahrens 
1067*fa9e4066Sahrens 	if (efi_alloc_and_init(fd, 9, &vtoc) != 0) {
1068*fa9e4066Sahrens 		/*
1069*fa9e4066Sahrens 		 * The only way this can fail is if we run out of memory, or we
1070*fa9e4066Sahrens 		 * were unable to read the disk geometry.
1071*fa9e4066Sahrens 		 */
1072*fa9e4066Sahrens 		if (errno == ENOMEM)
1073*fa9e4066Sahrens 			no_memory();
1074*fa9e4066Sahrens 
1075*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot label '%s': unable to "
1076*fa9e4066Sahrens 		    "read disk geometry\n"), name);
1077*fa9e4066Sahrens 		(void) close(fd);
1078*fa9e4066Sahrens 		return (-1);
1079*fa9e4066Sahrens 	}
1080*fa9e4066Sahrens 
1081*fa9e4066Sahrens 	vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba;
1082*fa9e4066Sahrens 	vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba + 1 -
1083*fa9e4066Sahrens 	    vtoc->efi_first_u_lba - resv;
1084*fa9e4066Sahrens 
1085*fa9e4066Sahrens 	/*
1086*fa9e4066Sahrens 	 * Why we use V_USR: V_BACKUP confuses users, and is considered
1087*fa9e4066Sahrens 	 * disposable by some EFI utilities (since EFI doesn't have a backup
1088*fa9e4066Sahrens 	 * slice).  V_UNASSIGNED is supposed to be used only for zero size
1089*fa9e4066Sahrens 	 * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
1090*fa9e4066Sahrens 	 * etc. were all pretty specific.  V_USR is as close to reality as we
1091*fa9e4066Sahrens 	 * can get, in the absence of V_OTHER.
1092*fa9e4066Sahrens 	 */
1093*fa9e4066Sahrens 	vtoc->efi_parts[0].p_tag = V_USR;
1094*fa9e4066Sahrens 	(void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
1095*fa9e4066Sahrens 
1096*fa9e4066Sahrens 	vtoc->efi_parts[8].p_start = vtoc->efi_last_u_lba + 1 - resv;
1097*fa9e4066Sahrens 	vtoc->efi_parts[8].p_size = resv;
1098*fa9e4066Sahrens 	vtoc->efi_parts[8].p_tag = V_RESERVED;
1099*fa9e4066Sahrens 
1100*fa9e4066Sahrens 	if (efi_write(fd, vtoc) != 0) {
1101*fa9e4066Sahrens 		/*
1102*fa9e4066Sahrens 		 * Currently, EFI labels are not supported for IDE disks, and it
1103*fa9e4066Sahrens 		 * is likely that they will not be supported on other drives for
1104*fa9e4066Sahrens 		 * some time.  Print out a helpful error message directing the
1105*fa9e4066Sahrens 		 * user to manually label the disk and give a specific slice.
1106*fa9e4066Sahrens 		 */
1107*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("cannot label '%s': failed to "
1108*fa9e4066Sahrens 		    "write EFI label\n"), name);
1109*fa9e4066Sahrens 		(void) fprintf(stderr, gettext("use fdisk(1M) to partition "
1110*fa9e4066Sahrens 		    "the disk, and provide a specific slice\n"));
1111*fa9e4066Sahrens 		(void) close(fd);
1112*fa9e4066Sahrens 		return (-1);
1113*fa9e4066Sahrens 	}
1114*fa9e4066Sahrens 
1115*fa9e4066Sahrens 	(void) close(fd);
1116*fa9e4066Sahrens 	return (0);
1117*fa9e4066Sahrens }
1118*fa9e4066Sahrens 
1119*fa9e4066Sahrens /*
1120*fa9e4066Sahrens  * Go through and find any whole disks in the vdev specification, labelling them
1121*fa9e4066Sahrens  * as appropriate.  When constructing the vdev spec, we were unable to open this
1122*fa9e4066Sahrens  * device in order to provide a devid.  Now that we have labelled the disk and
1123*fa9e4066Sahrens  * know that slice 0 is valid, we can construct the devid now.
1124*fa9e4066Sahrens  *
1125*fa9e4066Sahrens  * If the disk was already labelled with an EFI label, we will have gotten the
1126*fa9e4066Sahrens  * devid already (because we were able to open the whole disk).  Otherwise, we
1127*fa9e4066Sahrens  * need to get the devid after we label the disk.
1128*fa9e4066Sahrens  */
1129*fa9e4066Sahrens int
1130*fa9e4066Sahrens make_disks(nvlist_t *nv)
1131*fa9e4066Sahrens {
1132*fa9e4066Sahrens 	nvlist_t **child;
1133*fa9e4066Sahrens 	uint_t c, children;
1134*fa9e4066Sahrens 	char *type, *path, *diskname;
1135*fa9e4066Sahrens 	char buf[MAXPATHLEN];
1136*fa9e4066Sahrens 	struct stat64 statbuf;
1137*fa9e4066Sahrens 	int fd;
1138*fa9e4066Sahrens 	int ret;
1139*fa9e4066Sahrens 	ddi_devid_t devid;
1140*fa9e4066Sahrens 	char *minor = NULL, *devid_str = NULL;
1141*fa9e4066Sahrens 
1142*fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1143*fa9e4066Sahrens 
1144*fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1145*fa9e4066Sahrens 	    &child, &children) != 0) {
1146*fa9e4066Sahrens 
1147*fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_DISK) != 0)
1148*fa9e4066Sahrens 			return (0);
1149*fa9e4066Sahrens 
1150*fa9e4066Sahrens 		/*
1151*fa9e4066Sahrens 		 * We have a disk device.  Get the path to the device
1152*fa9e4066Sahrens 		 * and see if its a whole disk by appending the backup
1153*fa9e4066Sahrens 		 * slice and stat()ing the device.
1154*fa9e4066Sahrens 		 */
1155*fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1156*fa9e4066Sahrens 
1157*fa9e4066Sahrens 		if (!is_whole_disk(path, &statbuf))
1158*fa9e4066Sahrens 			return (0);
1159*fa9e4066Sahrens 
1160*fa9e4066Sahrens 		diskname = strrchr(path, '/');
1161*fa9e4066Sahrens 		assert(diskname != NULL);
1162*fa9e4066Sahrens 		diskname++;
1163*fa9e4066Sahrens 		if (label_disk(diskname) != 0)
1164*fa9e4066Sahrens 			return (-1);
1165*fa9e4066Sahrens 
1166*fa9e4066Sahrens 		/*
1167*fa9e4066Sahrens 		 * Fill in the devid, now that we've labeled the disk.
1168*fa9e4066Sahrens 		 */
1169*fa9e4066Sahrens 		(void) snprintf(buf, sizeof (buf), "%ss0", path);
1170*fa9e4066Sahrens 		if ((fd = open(buf, O_RDONLY)) < 0) {
1171*fa9e4066Sahrens 			(void) fprintf(stderr,
1172*fa9e4066Sahrens 			    gettext("cannot open '%s': %s\n"),
1173*fa9e4066Sahrens 			    buf, strerror(errno));
1174*fa9e4066Sahrens 			return (-1);
1175*fa9e4066Sahrens 		}
1176*fa9e4066Sahrens 
1177*fa9e4066Sahrens 		if (devid_get(fd, &devid) == 0) {
1178*fa9e4066Sahrens 			if (devid_get_minor_name(fd, &minor) == 0 &&
1179*fa9e4066Sahrens 			    (devid_str = devid_str_encode(devid, minor)) !=
1180*fa9e4066Sahrens 			    NULL) {
1181*fa9e4066Sahrens 				verify(nvlist_add_string(nv,
1182*fa9e4066Sahrens 				    ZPOOL_CONFIG_DEVID, devid_str) == 0);
1183*fa9e4066Sahrens 			}
1184*fa9e4066Sahrens 			if (devid_str != NULL)
1185*fa9e4066Sahrens 				devid_str_free(devid_str);
1186*fa9e4066Sahrens 			if (minor != NULL)
1187*fa9e4066Sahrens 				devid_str_free(minor);
1188*fa9e4066Sahrens 			devid_free(devid);
1189*fa9e4066Sahrens 		}
1190*fa9e4066Sahrens 
1191*fa9e4066Sahrens 		(void) close(fd);
1192*fa9e4066Sahrens 
1193*fa9e4066Sahrens 		return (0);
1194*fa9e4066Sahrens 	}
1195*fa9e4066Sahrens 
1196*fa9e4066Sahrens 	for (c = 0; c < children; c++)
1197*fa9e4066Sahrens 		if ((ret = make_disks(child[c])) != 0)
1198*fa9e4066Sahrens 			return (ret);
1199*fa9e4066Sahrens 
1200*fa9e4066Sahrens 	return (0);
1201*fa9e4066Sahrens }
1202*fa9e4066Sahrens 
1203*fa9e4066Sahrens /*
1204*fa9e4066Sahrens  * Go through and find any devices that are in use.  We rely on libdiskmgt for
1205*fa9e4066Sahrens  * the majority of this task.
1206*fa9e4066Sahrens  */
1207*fa9e4066Sahrens int
1208*fa9e4066Sahrens check_in_use(nvlist_t *nv, int force)
1209*fa9e4066Sahrens {
1210*fa9e4066Sahrens 	nvlist_t **child;
1211*fa9e4066Sahrens 	uint_t c, children;
1212*fa9e4066Sahrens 	char *type, *path;
1213*fa9e4066Sahrens 	int ret;
1214*fa9e4066Sahrens 
1215*fa9e4066Sahrens 	verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1216*fa9e4066Sahrens 
1217*fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1218*fa9e4066Sahrens 	    &child, &children) != 0) {
1219*fa9e4066Sahrens 
1220*fa9e4066Sahrens 		verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
1221*fa9e4066Sahrens 
1222*fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_DISK) == 0)
1223*fa9e4066Sahrens 			ret = check_device(path, force);
1224*fa9e4066Sahrens 
1225*fa9e4066Sahrens 		if (strcmp(type, VDEV_TYPE_FILE) == 0)
1226*fa9e4066Sahrens 			ret = check_file(path, force);
1227*fa9e4066Sahrens 
1228*fa9e4066Sahrens 		return (ret);
1229*fa9e4066Sahrens 	}
1230*fa9e4066Sahrens 
1231*fa9e4066Sahrens 	for (c = 0; c < children; c++)
1232*fa9e4066Sahrens 		if ((ret = check_in_use(child[c], force)) != 0)
1233*fa9e4066Sahrens 			return (ret);
1234*fa9e4066Sahrens 
1235*fa9e4066Sahrens 	return (0);
1236*fa9e4066Sahrens }
1237*fa9e4066Sahrens 
1238*fa9e4066Sahrens /*
1239*fa9e4066Sahrens  * Construct a syntactically valid vdev specification,
1240*fa9e4066Sahrens  * and ensure that all devices and files exist and can be opened.
1241*fa9e4066Sahrens  * Note: we don't bother freeing anything in the error paths
1242*fa9e4066Sahrens  * because the program is just going to exit anyway.
1243*fa9e4066Sahrens  */
1244*fa9e4066Sahrens nvlist_t *
1245*fa9e4066Sahrens construct_spec(int argc, char **argv)
1246*fa9e4066Sahrens {
1247*fa9e4066Sahrens 	nvlist_t *nvroot, *nv, **top;
1248*fa9e4066Sahrens 	int t, toplevels;
1249*fa9e4066Sahrens 
1250*fa9e4066Sahrens 	top = NULL;
1251*fa9e4066Sahrens 	toplevels = 0;
1252*fa9e4066Sahrens 
1253*fa9e4066Sahrens 	while (argc > 0) {
1254*fa9e4066Sahrens 		nv = NULL;
1255*fa9e4066Sahrens 
1256*fa9e4066Sahrens 		/*
1257*fa9e4066Sahrens 		 * If it's a mirror or raidz, the subsequent arguments are
1258*fa9e4066Sahrens 		 * its leaves -- until we encounter the next mirror or raidz.
1259*fa9e4066Sahrens 		 */
1260*fa9e4066Sahrens 		if (strcmp(argv[0], VDEV_TYPE_MIRROR) == 0 ||
1261*fa9e4066Sahrens 		    strcmp(argv[0], VDEV_TYPE_RAIDZ) == 0) {
1262*fa9e4066Sahrens 
1263*fa9e4066Sahrens 			char *type = argv[0];
1264*fa9e4066Sahrens 			nvlist_t **child = NULL;
1265*fa9e4066Sahrens 			int children = 0;
1266*fa9e4066Sahrens 			int c;
1267*fa9e4066Sahrens 
1268*fa9e4066Sahrens 			for (c = 1; c < argc; c++) {
1269*fa9e4066Sahrens 				if (strcmp(argv[c], VDEV_TYPE_MIRROR) == 0 ||
1270*fa9e4066Sahrens 				    strcmp(argv[c], VDEV_TYPE_RAIDZ) == 0)
1271*fa9e4066Sahrens 					break;
1272*fa9e4066Sahrens 				children++;
1273*fa9e4066Sahrens 				child = realloc(child,
1274*fa9e4066Sahrens 				    children * sizeof (nvlist_t *));
1275*fa9e4066Sahrens 				if (child == NULL)
1276*fa9e4066Sahrens 					no_memory();
1277*fa9e4066Sahrens 				if ((nv = make_leaf_vdev(argv[c])) == NULL)
1278*fa9e4066Sahrens 					return (NULL);
1279*fa9e4066Sahrens 				child[children - 1] = nv;
1280*fa9e4066Sahrens 			}
1281*fa9e4066Sahrens 
1282*fa9e4066Sahrens 			argc -= c;
1283*fa9e4066Sahrens 			argv += c;
1284*fa9e4066Sahrens 
1285*fa9e4066Sahrens 			/*
1286*fa9e4066Sahrens 			 * Mirrors and RAID-Z devices require at least
1287*fa9e4066Sahrens 			 * two components.
1288*fa9e4066Sahrens 			 */
1289*fa9e4066Sahrens 			if (children < 2) {
1290*fa9e4066Sahrens 				(void) fprintf(stderr,
1291*fa9e4066Sahrens 				    gettext("invalid vdev specification: "
1292*fa9e4066Sahrens 				    "%s requires at least 2 devices\n"), type);
1293*fa9e4066Sahrens 				return (NULL);
1294*fa9e4066Sahrens 			}
1295*fa9e4066Sahrens 
1296*fa9e4066Sahrens 			verify(nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) == 0);
1297*fa9e4066Sahrens 			verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1298*fa9e4066Sahrens 			    type) == 0);
1299*fa9e4066Sahrens 			verify(nvlist_add_nvlist_array(nv,
1300*fa9e4066Sahrens 			    ZPOOL_CONFIG_CHILDREN, child, children) == 0);
1301*fa9e4066Sahrens 
1302*fa9e4066Sahrens 			for (c = 0; c < children; c++)
1303*fa9e4066Sahrens 				nvlist_free(child[c]);
1304*fa9e4066Sahrens 			free(child);
1305*fa9e4066Sahrens 		} else {
1306*fa9e4066Sahrens 			/*
1307*fa9e4066Sahrens 			 * We have a device.  Pass off to make_leaf_vdev() to
1308*fa9e4066Sahrens 			 * construct the appropriate nvlist describing the vdev.
1309*fa9e4066Sahrens 			 */
1310*fa9e4066Sahrens 			if ((nv = make_leaf_vdev(argv[0])) == NULL)
1311*fa9e4066Sahrens 				return (NULL);
1312*fa9e4066Sahrens 			argc--;
1313*fa9e4066Sahrens 			argv++;
1314*fa9e4066Sahrens 		}
1315*fa9e4066Sahrens 
1316*fa9e4066Sahrens 		toplevels++;
1317*fa9e4066Sahrens 		top = realloc(top, toplevels * sizeof (nvlist_t *));
1318*fa9e4066Sahrens 		if (top == NULL)
1319*fa9e4066Sahrens 			no_memory();
1320*fa9e4066Sahrens 		top[toplevels - 1] = nv;
1321*fa9e4066Sahrens 	}
1322*fa9e4066Sahrens 
1323*fa9e4066Sahrens 	/*
1324*fa9e4066Sahrens 	 * Finally, create nvroot and add all top-level vdevs to it.
1325*fa9e4066Sahrens 	 */
1326*fa9e4066Sahrens 	verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1327*fa9e4066Sahrens 	verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1328*fa9e4066Sahrens 	    VDEV_TYPE_ROOT) == 0);
1329*fa9e4066Sahrens 	verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1330*fa9e4066Sahrens 	    top, toplevels) == 0);
1331*fa9e4066Sahrens 
1332*fa9e4066Sahrens 	for (t = 0; t < toplevels; t++)
1333*fa9e4066Sahrens 		nvlist_free(top[t]);
1334*fa9e4066Sahrens 	free(top);
1335*fa9e4066Sahrens 
1336*fa9e4066Sahrens 	return (nvroot);
1337*fa9e4066Sahrens }
1338*fa9e4066Sahrens 
1339*fa9e4066Sahrens /*
1340*fa9e4066Sahrens  * Get and validate the contents of the given vdev specification.  This ensures
1341*fa9e4066Sahrens  * that the nvlist returned is well-formed, that all the devices exist, and that
1342*fa9e4066Sahrens  * they are not currently in use by any other known consumer.  The 'poolconfig'
1343*fa9e4066Sahrens  * parameter is the current configuration of the pool when adding devices
1344*fa9e4066Sahrens  * existing pool, and is used to perform additional checks, such as changing the
1345*fa9e4066Sahrens  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1346*fa9e4066Sahrens  * new pool.  The 'force' flag controls whether devices should be forcefully
1347*fa9e4066Sahrens  * added, even if they appear in use.
1348*fa9e4066Sahrens  */
1349*fa9e4066Sahrens nvlist_t *
1350*fa9e4066Sahrens make_root_vdev(nvlist_t *poolconfig, int force, int check_rep,
1351*fa9e4066Sahrens     int argc, char **argv)
1352*fa9e4066Sahrens {
1353*fa9e4066Sahrens 	nvlist_t *newroot;
1354*fa9e4066Sahrens 
1355*fa9e4066Sahrens 	is_force = force;
1356*fa9e4066Sahrens 
1357*fa9e4066Sahrens 	/*
1358*fa9e4066Sahrens 	 * Construct the vdev specification.  If this is successful, we know
1359*fa9e4066Sahrens 	 * that we have a valid specification, and that all devices can be
1360*fa9e4066Sahrens 	 * opened.
1361*fa9e4066Sahrens 	 */
1362*fa9e4066Sahrens 	if ((newroot = construct_spec(argc, argv)) == NULL)
1363*fa9e4066Sahrens 		return (NULL);
1364*fa9e4066Sahrens 
1365*fa9e4066Sahrens 	/*
1366*fa9e4066Sahrens 	 * Validate each device to make sure that its not shared with another
1367*fa9e4066Sahrens 	 * subsystem.  We do this even if 'force' is set, because there are some
1368*fa9e4066Sahrens 	 * uses (such as a dedicated dump device) that even '-f' cannot
1369*fa9e4066Sahrens 	 * override.
1370*fa9e4066Sahrens 	 */
1371*fa9e4066Sahrens 	if (check_in_use(newroot, force) != 0) {
1372*fa9e4066Sahrens 		nvlist_free(newroot);
1373*fa9e4066Sahrens 		return (NULL);
1374*fa9e4066Sahrens 	}
1375*fa9e4066Sahrens 
1376*fa9e4066Sahrens 	/*
1377*fa9e4066Sahrens 	 * Check the replication level of the given vdevs and report any errors
1378*fa9e4066Sahrens 	 * found.  We include the existing pool spec, if any, as we need to
1379*fa9e4066Sahrens 	 * catch changes against the existing replication level.
1380*fa9e4066Sahrens 	 */
1381*fa9e4066Sahrens 	if (check_rep && check_replication(poolconfig, newroot) != 0) {
1382*fa9e4066Sahrens 		nvlist_free(newroot);
1383*fa9e4066Sahrens 		return (NULL);
1384*fa9e4066Sahrens 	}
1385*fa9e4066Sahrens 
1386*fa9e4066Sahrens 	/*
1387*fa9e4066Sahrens 	 * Run through the vdev specification and label any whole disks found.
1388*fa9e4066Sahrens 	 */
1389*fa9e4066Sahrens 	if (make_disks(newroot) != 0) {
1390*fa9e4066Sahrens 		nvlist_free(newroot);
1391*fa9e4066Sahrens 		return (NULL);
1392*fa9e4066Sahrens 	}
1393*fa9e4066Sahrens 
1394*fa9e4066Sahrens 	return (newroot);
1395*fa9e4066Sahrens }
1396