1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
25eda14cbcSMatt Macy * Copyright (c) 2016, 2017 Intel Corporation.
26eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
27eda14cbcSMatt Macy */
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy /*
30eda14cbcSMatt Macy * Functions to convert between a list of vdevs and an nvlist representing the
31eda14cbcSMatt Macy * configuration. Each entry in the list can be one of:
32eda14cbcSMatt Macy *
33eda14cbcSMatt Macy * Device vdevs
34eda14cbcSMatt Macy * disk=(path=..., devid=...)
35eda14cbcSMatt Macy * file=(path=...)
36eda14cbcSMatt Macy *
37eda14cbcSMatt Macy * Group vdevs
38eda14cbcSMatt Macy * raidz[1|2]=(...)
39eda14cbcSMatt Macy * mirror=(...)
40eda14cbcSMatt Macy *
41eda14cbcSMatt Macy * Hot spares
42eda14cbcSMatt Macy *
43eda14cbcSMatt Macy * While the underlying implementation supports it, group vdevs cannot contain
44eda14cbcSMatt Macy * other group vdevs. All userland verification of devices is contained within
45eda14cbcSMatt Macy * this file. If successful, the nvlist returned can be passed directly to the
46eda14cbcSMatt Macy * kernel; we've done as much verification as possible in userland.
47eda14cbcSMatt Macy *
48eda14cbcSMatt Macy * Hot spares are a special case, and passed down as an array of disk vdevs, at
49eda14cbcSMatt Macy * the same level as the root of the vdev tree.
50eda14cbcSMatt Macy *
51eda14cbcSMatt Macy * The only function exported by this file is 'make_root_vdev'. The
52eda14cbcSMatt Macy * function performs several passes:
53eda14cbcSMatt Macy *
54eda14cbcSMatt Macy * 1. Construct the vdev specification. Performs syntax validation and
55eda14cbcSMatt Macy * makes sure each device is valid.
56eda14cbcSMatt Macy * 2. Check for devices in use. Using libblkid to make sure that no
57eda14cbcSMatt Macy * devices are also in use. Some can be overridden using the 'force'
58eda14cbcSMatt Macy * flag, others cannot.
59eda14cbcSMatt Macy * 3. Check for replication errors if the 'force' flag is not specified.
60eda14cbcSMatt Macy * validates that the replication level is consistent across the
61eda14cbcSMatt Macy * entire pool.
62eda14cbcSMatt Macy * 4. Call libzfs to label any whole disks with an EFI label.
63eda14cbcSMatt Macy */
64eda14cbcSMatt Macy
65eda14cbcSMatt Macy #include <assert.h>
66eda14cbcSMatt Macy #include <ctype.h>
67eda14cbcSMatt Macy #include <errno.h>
68eda14cbcSMatt Macy #include <fcntl.h>
69eda14cbcSMatt Macy #include <libintl.h>
70eda14cbcSMatt Macy #include <libnvpair.h>
71eda14cbcSMatt Macy #include <libzutil.h>
72eda14cbcSMatt Macy #include <limits.h>
73eda14cbcSMatt Macy #include <sys/spa.h>
74eda14cbcSMatt Macy #include <stdio.h>
75eda14cbcSMatt Macy #include <string.h>
76eda14cbcSMatt Macy #include <unistd.h>
77eda14cbcSMatt Macy #include "zpool_util.h"
78eda14cbcSMatt Macy #include <sys/zfs_context.h>
79eda14cbcSMatt Macy #include <sys/stat.h>
80eda14cbcSMatt Macy
81eda14cbcSMatt Macy /*
82eda14cbcSMatt Macy * For any given vdev specification, we can have multiple errors. The
83eda14cbcSMatt Macy * vdev_error() function keeps track of whether we have seen an error yet, and
84eda14cbcSMatt Macy * prints out a header if its the first error we've seen.
85eda14cbcSMatt Macy */
86eda14cbcSMatt Macy boolean_t error_seen;
87eda14cbcSMatt Macy boolean_t is_force;
88eda14cbcSMatt Macy
89eda14cbcSMatt Macy void
vdev_error(const char * fmt,...)90eda14cbcSMatt Macy vdev_error(const char *fmt, ...)
91eda14cbcSMatt Macy {
92eda14cbcSMatt Macy va_list ap;
93eda14cbcSMatt Macy
94eda14cbcSMatt Macy if (!error_seen) {
95eda14cbcSMatt Macy (void) fprintf(stderr, gettext("invalid vdev specification\n"));
96eda14cbcSMatt Macy if (!is_force)
97eda14cbcSMatt Macy (void) fprintf(stderr, gettext("use '-f' to override "
98eda14cbcSMatt Macy "the following errors:\n"));
99eda14cbcSMatt Macy else
100eda14cbcSMatt Macy (void) fprintf(stderr, gettext("the following errors "
101eda14cbcSMatt Macy "must be manually repaired:\n"));
102eda14cbcSMatt Macy error_seen = B_TRUE;
103eda14cbcSMatt Macy }
104eda14cbcSMatt Macy
105eda14cbcSMatt Macy va_start(ap, fmt);
106eda14cbcSMatt Macy (void) vfprintf(stderr, fmt, ap);
107eda14cbcSMatt Macy va_end(ap);
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy
110eda14cbcSMatt Macy /*
111eda14cbcSMatt Macy * Check that a file is valid. All we can do in this case is check that it's
112eda14cbcSMatt Macy * not in use by another pool, and not in use by swap.
113eda14cbcSMatt Macy */
114eda14cbcSMatt Macy int
check_file_generic(const char * file,boolean_t force,boolean_t isspare)1151f88aa09SMartin Matuska check_file_generic(const char *file, boolean_t force, boolean_t isspare)
116eda14cbcSMatt Macy {
117eda14cbcSMatt Macy char *name;
118eda14cbcSMatt Macy int fd;
119eda14cbcSMatt Macy int ret = 0;
120eda14cbcSMatt Macy pool_state_t state;
121eda14cbcSMatt Macy boolean_t inuse;
122eda14cbcSMatt Macy
123eda14cbcSMatt Macy if ((fd = open(file, O_RDONLY)) < 0)
124eda14cbcSMatt Macy return (0);
125eda14cbcSMatt Macy
126eda14cbcSMatt Macy if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
127eda14cbcSMatt Macy const char *desc;
128eda14cbcSMatt Macy
129eda14cbcSMatt Macy switch (state) {
130eda14cbcSMatt Macy case POOL_STATE_ACTIVE:
131eda14cbcSMatt Macy desc = gettext("active");
132eda14cbcSMatt Macy break;
133eda14cbcSMatt Macy
134eda14cbcSMatt Macy case POOL_STATE_EXPORTED:
135eda14cbcSMatt Macy desc = gettext("exported");
136eda14cbcSMatt Macy break;
137eda14cbcSMatt Macy
138eda14cbcSMatt Macy case POOL_STATE_POTENTIALLY_ACTIVE:
139eda14cbcSMatt Macy desc = gettext("potentially active");
140eda14cbcSMatt Macy break;
141eda14cbcSMatt Macy
142eda14cbcSMatt Macy default:
143eda14cbcSMatt Macy desc = gettext("unknown");
144eda14cbcSMatt Macy break;
145eda14cbcSMatt Macy }
146eda14cbcSMatt Macy
147eda14cbcSMatt Macy /*
148eda14cbcSMatt Macy * Allow hot spares to be shared between pools.
149eda14cbcSMatt Macy */
150eda14cbcSMatt Macy if (state == POOL_STATE_SPARE && isspare) {
151eda14cbcSMatt Macy free(name);
152eda14cbcSMatt Macy (void) close(fd);
153eda14cbcSMatt Macy return (0);
154eda14cbcSMatt Macy }
155eda14cbcSMatt Macy
156eda14cbcSMatt Macy if (state == POOL_STATE_ACTIVE ||
157eda14cbcSMatt Macy state == POOL_STATE_SPARE || !force) {
158eda14cbcSMatt Macy switch (state) {
159eda14cbcSMatt Macy case POOL_STATE_SPARE:
160eda14cbcSMatt Macy vdev_error(gettext("%s is reserved as a hot "
161eda14cbcSMatt Macy "spare for pool %s\n"), file, name);
162eda14cbcSMatt Macy break;
163eda14cbcSMatt Macy default:
164eda14cbcSMatt Macy vdev_error(gettext("%s is part of %s pool "
165eda14cbcSMatt Macy "'%s'\n"), file, desc, name);
166eda14cbcSMatt Macy break;
167eda14cbcSMatt Macy }
168eda14cbcSMatt Macy ret = -1;
169eda14cbcSMatt Macy }
170eda14cbcSMatt Macy
171eda14cbcSMatt Macy free(name);
172eda14cbcSMatt Macy }
173eda14cbcSMatt Macy
174eda14cbcSMatt Macy (void) close(fd);
175eda14cbcSMatt Macy return (ret);
176eda14cbcSMatt Macy }
177eda14cbcSMatt Macy
178eda14cbcSMatt Macy /*
179eda14cbcSMatt Macy * This may be a shorthand device path or it could be total gibberish.
180eda14cbcSMatt Macy * Check to see if it is a known device available in zfs_vdev_paths.
181eda14cbcSMatt Macy * As part of this check, see if we've been given an entire disk
182eda14cbcSMatt Macy * (minus the slice number).
183eda14cbcSMatt Macy */
184eda14cbcSMatt Macy static int
is_shorthand_path(const char * arg,char * path,size_t path_size,struct stat64 * statbuf,boolean_t * wholedisk)185eda14cbcSMatt Macy is_shorthand_path(const char *arg, char *path, size_t path_size,
186eda14cbcSMatt Macy struct stat64 *statbuf, boolean_t *wholedisk)
187eda14cbcSMatt Macy {
188eda14cbcSMatt Macy int error;
189eda14cbcSMatt Macy
190eda14cbcSMatt Macy error = zfs_resolve_shortname(arg, path, path_size);
191eda14cbcSMatt Macy if (error == 0) {
192eda14cbcSMatt Macy *wholedisk = zfs_dev_is_whole_disk(path);
193eda14cbcSMatt Macy if (*wholedisk || (stat64(path, statbuf) == 0))
194eda14cbcSMatt Macy return (0);
195eda14cbcSMatt Macy }
196eda14cbcSMatt Macy
197eda14cbcSMatt Macy strlcpy(path, arg, path_size);
198eda14cbcSMatt Macy memset(statbuf, 0, sizeof (*statbuf));
199eda14cbcSMatt Macy *wholedisk = B_FALSE;
200eda14cbcSMatt Macy
201eda14cbcSMatt Macy return (error);
202eda14cbcSMatt Macy }
203eda14cbcSMatt Macy
204eda14cbcSMatt Macy /*
205eda14cbcSMatt Macy * Determine if the given path is a hot spare within the given configuration.
206eda14cbcSMatt Macy * If no configuration is given we rely solely on the label.
207eda14cbcSMatt Macy */
208eda14cbcSMatt Macy static boolean_t
is_spare(nvlist_t * config,const char * path)209eda14cbcSMatt Macy is_spare(nvlist_t *config, const char *path)
210eda14cbcSMatt Macy {
211eda14cbcSMatt Macy int fd;
212eda14cbcSMatt Macy pool_state_t state;
213eda14cbcSMatt Macy char *name = NULL;
214eda14cbcSMatt Macy nvlist_t *label;
215eda14cbcSMatt Macy uint64_t guid, spareguid;
216eda14cbcSMatt Macy nvlist_t *nvroot;
217eda14cbcSMatt Macy nvlist_t **spares;
218eda14cbcSMatt Macy uint_t i, nspares;
219eda14cbcSMatt Macy boolean_t inuse;
220eda14cbcSMatt Macy
2217877fdebSMatt Macy if (zpool_is_draid_spare(path))
2227877fdebSMatt Macy return (B_TRUE);
2237877fdebSMatt Macy
224eda14cbcSMatt Macy if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
225eda14cbcSMatt Macy return (B_FALSE);
226eda14cbcSMatt Macy
227eda14cbcSMatt Macy if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
228eda14cbcSMatt Macy !inuse ||
229eda14cbcSMatt Macy state != POOL_STATE_SPARE ||
230eda14cbcSMatt Macy zpool_read_label(fd, &label, NULL) != 0) {
231eda14cbcSMatt Macy free(name);
232eda14cbcSMatt Macy (void) close(fd);
233eda14cbcSMatt Macy return (B_FALSE);
234eda14cbcSMatt Macy }
235eda14cbcSMatt Macy free(name);
236eda14cbcSMatt Macy (void) close(fd);
237eda14cbcSMatt Macy
238eda14cbcSMatt Macy if (config == NULL) {
239eda14cbcSMatt Macy nvlist_free(label);
240eda14cbcSMatt Macy return (B_TRUE);
241eda14cbcSMatt Macy }
242eda14cbcSMatt Macy
243eda14cbcSMatt Macy verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
244eda14cbcSMatt Macy nvlist_free(label);
245eda14cbcSMatt Macy
246eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
247eda14cbcSMatt Macy &nvroot) == 0);
248eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
249eda14cbcSMatt Macy &spares, &nspares) == 0) {
250eda14cbcSMatt Macy for (i = 0; i < nspares; i++) {
251eda14cbcSMatt Macy verify(nvlist_lookup_uint64(spares[i],
252eda14cbcSMatt Macy ZPOOL_CONFIG_GUID, &spareguid) == 0);
253eda14cbcSMatt Macy if (spareguid == guid)
254eda14cbcSMatt Macy return (B_TRUE);
255eda14cbcSMatt Macy }
256eda14cbcSMatt Macy }
257eda14cbcSMatt Macy
258eda14cbcSMatt Macy return (B_FALSE);
259eda14cbcSMatt Macy }
260eda14cbcSMatt Macy
261eda14cbcSMatt Macy /*
262eda14cbcSMatt Macy * Create a leaf vdev. Determine if this is a file or a device. If it's a
263eda14cbcSMatt Macy * device, fill in the device id to make a complete nvlist. Valid forms for a
264eda14cbcSMatt Macy * leaf vdev are:
265eda14cbcSMatt Macy *
266eda14cbcSMatt Macy * /dev/xxx Complete disk path
267eda14cbcSMatt Macy * /xxx Full path to file
268eda14cbcSMatt Macy * xxx Shorthand for <zfs_vdev_paths>/xxx
2697877fdebSMatt Macy * draid* Virtual dRAID spare
270eda14cbcSMatt Macy */
271eda14cbcSMatt Macy static nvlist_t *
make_leaf_vdev(nvlist_t * props,const char * arg,boolean_t is_primary)2727877fdebSMatt Macy make_leaf_vdev(nvlist_t *props, const char *arg, boolean_t is_primary)
273eda14cbcSMatt Macy {
274eda14cbcSMatt Macy char path[MAXPATHLEN];
275eda14cbcSMatt Macy struct stat64 statbuf;
276eda14cbcSMatt Macy nvlist_t *vdev = NULL;
277a0b956f5SMartin Matuska const char *type = NULL;
278eda14cbcSMatt Macy boolean_t wholedisk = B_FALSE;
279eda14cbcSMatt Macy uint64_t ashift = 0;
280eda14cbcSMatt Macy int err;
281eda14cbcSMatt Macy
282eda14cbcSMatt Macy /*
283eda14cbcSMatt Macy * Determine what type of vdev this is, and put the full path into
284eda14cbcSMatt Macy * 'path'. We detect whether this is a device of file afterwards by
285eda14cbcSMatt Macy * checking the st_mode of the file.
286eda14cbcSMatt Macy */
287eda14cbcSMatt Macy if (arg[0] == '/') {
288eda14cbcSMatt Macy /*
289eda14cbcSMatt Macy * Complete device or file path. Exact type is determined by
290eda14cbcSMatt Macy * examining the file descriptor afterwards. Symbolic links
291eda14cbcSMatt Macy * are resolved to their real paths to determine whole disk
292eda14cbcSMatt Macy * and S_ISBLK/S_ISREG type checks. However, we are careful
293eda14cbcSMatt Macy * to store the given path as ZPOOL_CONFIG_PATH to ensure we
294eda14cbcSMatt Macy * can leverage udev's persistent device labels.
295eda14cbcSMatt Macy */
296eda14cbcSMatt Macy if (realpath(arg, path) == NULL) {
297eda14cbcSMatt Macy (void) fprintf(stderr,
298eda14cbcSMatt Macy gettext("cannot resolve path '%s'\n"), arg);
299eda14cbcSMatt Macy return (NULL);
300eda14cbcSMatt Macy }
301eda14cbcSMatt Macy
302eda14cbcSMatt Macy wholedisk = zfs_dev_is_whole_disk(path);
303eda14cbcSMatt Macy if (!wholedisk && (stat64(path, &statbuf) != 0)) {
304eda14cbcSMatt Macy (void) fprintf(stderr,
305eda14cbcSMatt Macy gettext("cannot open '%s': %s\n"),
306eda14cbcSMatt Macy path, strerror(errno));
307eda14cbcSMatt Macy return (NULL);
308eda14cbcSMatt Macy }
309eda14cbcSMatt Macy
310eda14cbcSMatt Macy /* After whole disk check restore original passed path */
311eda14cbcSMatt Macy strlcpy(path, arg, sizeof (path));
3127877fdebSMatt Macy } else if (zpool_is_draid_spare(arg)) {
3137877fdebSMatt Macy if (!is_primary) {
3147877fdebSMatt Macy (void) fprintf(stderr,
3157877fdebSMatt Macy gettext("cannot open '%s': dRAID spares can only "
3167877fdebSMatt Macy "be used to replace primary vdevs\n"), arg);
3177877fdebSMatt Macy return (NULL);
3187877fdebSMatt Macy }
3197877fdebSMatt Macy
3207877fdebSMatt Macy wholedisk = B_TRUE;
3217877fdebSMatt Macy strlcpy(path, arg, sizeof (path));
3227877fdebSMatt Macy type = VDEV_TYPE_DRAID_SPARE;
323eda14cbcSMatt Macy } else {
324eda14cbcSMatt Macy err = is_shorthand_path(arg, path, sizeof (path),
325eda14cbcSMatt Macy &statbuf, &wholedisk);
326eda14cbcSMatt Macy if (err != 0) {
327eda14cbcSMatt Macy /*
328eda14cbcSMatt Macy * If we got ENOENT, then the user gave us
329eda14cbcSMatt Macy * gibberish, so try to direct them with a
330eda14cbcSMatt Macy * reasonable error message. Otherwise,
331eda14cbcSMatt Macy * regurgitate strerror() since it's the best we
332eda14cbcSMatt Macy * can do.
333eda14cbcSMatt Macy */
334eda14cbcSMatt Macy if (err == ENOENT) {
335eda14cbcSMatt Macy (void) fprintf(stderr,
336eda14cbcSMatt Macy gettext("cannot open '%s': no such "
337eda14cbcSMatt Macy "device in %s\n"), arg, DISK_ROOT);
338eda14cbcSMatt Macy (void) fprintf(stderr,
339eda14cbcSMatt Macy gettext("must be a full path or "
340eda14cbcSMatt Macy "shorthand device name\n"));
341eda14cbcSMatt Macy return (NULL);
342eda14cbcSMatt Macy } else {
343eda14cbcSMatt Macy (void) fprintf(stderr,
344eda14cbcSMatt Macy gettext("cannot open '%s': %s\n"),
345eda14cbcSMatt Macy path, strerror(errno));
346eda14cbcSMatt Macy return (NULL);
347eda14cbcSMatt Macy }
348eda14cbcSMatt Macy }
349eda14cbcSMatt Macy }
350eda14cbcSMatt Macy
3517877fdebSMatt Macy if (type == NULL) {
352eda14cbcSMatt Macy /*
353eda14cbcSMatt Macy * Determine whether this is a device or a file.
354eda14cbcSMatt Macy */
355eda14cbcSMatt Macy if (wholedisk || S_ISBLK(statbuf.st_mode)) {
356eda14cbcSMatt Macy type = VDEV_TYPE_DISK;
357eda14cbcSMatt Macy } else if (S_ISREG(statbuf.st_mode)) {
358eda14cbcSMatt Macy type = VDEV_TYPE_FILE;
359eda14cbcSMatt Macy } else {
3607877fdebSMatt Macy fprintf(stderr, gettext("cannot use '%s': must "
3617877fdebSMatt Macy "be a block device or regular file\n"), path);
362eda14cbcSMatt Macy return (NULL);
363eda14cbcSMatt Macy }
3647877fdebSMatt Macy }
365eda14cbcSMatt Macy
366eda14cbcSMatt Macy /*
367eda14cbcSMatt Macy * Finally, we have the complete device or file, and we know that it is
368eda14cbcSMatt Macy * acceptable to use. Construct the nvlist to describe this vdev. All
369eda14cbcSMatt Macy * vdevs have a 'path' element, and devices also have a 'devid' element.
370eda14cbcSMatt Macy */
371eda14cbcSMatt Macy verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
372eda14cbcSMatt Macy verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
373eda14cbcSMatt Macy verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
3747877fdebSMatt Macy
375*14c2e0a0SMartin Matuska /* Lookup and add the enclosure sysfs path (if exists) */
376*14c2e0a0SMartin Matuska update_vdev_config_dev_sysfs_path(vdev, path,
377*14c2e0a0SMartin Matuska ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
378*14c2e0a0SMartin Matuska
379eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_DISK) == 0)
380eda14cbcSMatt Macy verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
381eda14cbcSMatt Macy (uint64_t)wholedisk) == 0);
382eda14cbcSMatt Macy
383eda14cbcSMatt Macy /*
384eda14cbcSMatt Macy * Override defaults if custom properties are provided.
385eda14cbcSMatt Macy */
386eda14cbcSMatt Macy if (props != NULL) {
3872a58b312SMartin Matuska const char *value = NULL;
388eda14cbcSMatt Macy
389eda14cbcSMatt Macy if (nvlist_lookup_string(props,
390eda14cbcSMatt Macy zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0) {
391eda14cbcSMatt Macy if (zfs_nicestrtonum(NULL, value, &ashift) != 0) {
392eda14cbcSMatt Macy (void) fprintf(stderr,
393eda14cbcSMatt Macy gettext("ashift must be a number.\n"));
394eda14cbcSMatt Macy return (NULL);
395eda14cbcSMatt Macy }
396eda14cbcSMatt Macy if (ashift != 0 &&
397eda14cbcSMatt Macy (ashift < ASHIFT_MIN || ashift > ASHIFT_MAX)) {
398eda14cbcSMatt Macy (void) fprintf(stderr,
399eda14cbcSMatt Macy gettext("invalid 'ashift=%" PRIu64 "' "
400eda14cbcSMatt Macy "property: only values between %" PRId32 " "
401eda14cbcSMatt Macy "and %" PRId32 " are allowed.\n"),
402eda14cbcSMatt Macy ashift, ASHIFT_MIN, ASHIFT_MAX);
403eda14cbcSMatt Macy return (NULL);
404eda14cbcSMatt Macy }
405eda14cbcSMatt Macy }
406eda14cbcSMatt Macy }
407eda14cbcSMatt Macy
408eda14cbcSMatt Macy /*
409eda14cbcSMatt Macy * If the device is known to incorrectly report its physical sector
410eda14cbcSMatt Macy * size explicitly provide the known correct value.
411eda14cbcSMatt Macy */
412eda14cbcSMatt Macy if (ashift == 0) {
413eda14cbcSMatt Macy int sector_size;
414eda14cbcSMatt Macy
415eda14cbcSMatt Macy if (check_sector_size_database(path, §or_size) == B_TRUE)
416eda14cbcSMatt Macy ashift = highbit64(sector_size) - 1;
417eda14cbcSMatt Macy }
418eda14cbcSMatt Macy
419eda14cbcSMatt Macy if (ashift > 0)
420eda14cbcSMatt Macy (void) nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
421eda14cbcSMatt Macy
422eda14cbcSMatt Macy return (vdev);
423eda14cbcSMatt Macy }
424eda14cbcSMatt Macy
425eda14cbcSMatt Macy /*
426eda14cbcSMatt Macy * Go through and verify the replication level of the pool is consistent.
427eda14cbcSMatt Macy * Performs the following checks:
428eda14cbcSMatt Macy *
429eda14cbcSMatt Macy * For the new spec, verifies that devices in mirrors and raidz are the
430eda14cbcSMatt Macy * same size.
431eda14cbcSMatt Macy *
432eda14cbcSMatt Macy * If the current configuration already has inconsistent replication
433eda14cbcSMatt Macy * levels, ignore any other potential problems in the new spec.
434eda14cbcSMatt Macy *
435eda14cbcSMatt Macy * Otherwise, make sure that the current spec (if there is one) and the new
436eda14cbcSMatt Macy * spec have consistent replication levels.
437eda14cbcSMatt Macy *
438eda14cbcSMatt Macy * If there is no current spec (create), make sure new spec has at least
439eda14cbcSMatt Macy * one general purpose vdev.
440eda14cbcSMatt Macy */
441eda14cbcSMatt Macy typedef struct replication_level {
4422a58b312SMartin Matuska const char *zprl_type;
443eda14cbcSMatt Macy uint64_t zprl_children;
444eda14cbcSMatt Macy uint64_t zprl_parity;
445eda14cbcSMatt Macy } replication_level_t;
446eda14cbcSMatt Macy
447eda14cbcSMatt Macy #define ZPOOL_FUZZ (16 * 1024 * 1024)
448eda14cbcSMatt Macy
4497877fdebSMatt Macy /*
4507877fdebSMatt Macy * N.B. For the purposes of comparing replication levels dRAID can be
45116038816SMartin Matuska * considered functionally equivalent to raidz.
4527877fdebSMatt Macy */
453eda14cbcSMatt Macy static boolean_t
is_raidz_mirror(replication_level_t * a,replication_level_t * b,replication_level_t ** raidz,replication_level_t ** mirror)454eda14cbcSMatt Macy is_raidz_mirror(replication_level_t *a, replication_level_t *b,
455eda14cbcSMatt Macy replication_level_t **raidz, replication_level_t **mirror)
456eda14cbcSMatt Macy {
4577877fdebSMatt Macy if ((strcmp(a->zprl_type, "raidz") == 0 ||
4587877fdebSMatt Macy strcmp(a->zprl_type, "draid") == 0) &&
459eda14cbcSMatt Macy strcmp(b->zprl_type, "mirror") == 0) {
460eda14cbcSMatt Macy *raidz = a;
461eda14cbcSMatt Macy *mirror = b;
462eda14cbcSMatt Macy return (B_TRUE);
463eda14cbcSMatt Macy }
464eda14cbcSMatt Macy return (B_FALSE);
465eda14cbcSMatt Macy }
466eda14cbcSMatt Macy
467eda14cbcSMatt Macy /*
4687877fdebSMatt Macy * Comparison for determining if dRAID and raidz where passed in either order.
4697877fdebSMatt Macy */
4707877fdebSMatt Macy static boolean_t
is_raidz_draid(replication_level_t * a,replication_level_t * b)4717877fdebSMatt Macy is_raidz_draid(replication_level_t *a, replication_level_t *b)
4727877fdebSMatt Macy {
4737877fdebSMatt Macy if ((strcmp(a->zprl_type, "raidz") == 0 ||
4747877fdebSMatt Macy strcmp(a->zprl_type, "draid") == 0) &&
4757877fdebSMatt Macy (strcmp(b->zprl_type, "raidz") == 0 ||
4767877fdebSMatt Macy strcmp(b->zprl_type, "draid") == 0)) {
4777877fdebSMatt Macy return (B_TRUE);
4787877fdebSMatt Macy }
4797877fdebSMatt Macy
4807877fdebSMatt Macy return (B_FALSE);
4817877fdebSMatt Macy }
4827877fdebSMatt Macy
4837877fdebSMatt Macy /*
484eda14cbcSMatt Macy * Given a list of toplevel vdevs, return the current replication level. If
485eda14cbcSMatt Macy * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
486eda14cbcSMatt Macy * an error message will be displayed for each self-inconsistent vdev.
487eda14cbcSMatt Macy */
488eda14cbcSMatt Macy static replication_level_t *
get_replication(nvlist_t * nvroot,boolean_t fatal)489eda14cbcSMatt Macy get_replication(nvlist_t *nvroot, boolean_t fatal)
490eda14cbcSMatt Macy {
491eda14cbcSMatt Macy nvlist_t **top;
492eda14cbcSMatt Macy uint_t t, toplevels;
493eda14cbcSMatt Macy nvlist_t **child;
494eda14cbcSMatt Macy uint_t c, children;
495eda14cbcSMatt Macy nvlist_t *nv;
4962a58b312SMartin Matuska const char *type;
497eda14cbcSMatt Macy replication_level_t lastrep = {0};
498eda14cbcSMatt Macy replication_level_t rep;
499eda14cbcSMatt Macy replication_level_t *ret;
500eda14cbcSMatt Macy replication_level_t *raidz, *mirror;
501eda14cbcSMatt Macy boolean_t dontreport;
502eda14cbcSMatt Macy
503eda14cbcSMatt Macy ret = safe_malloc(sizeof (replication_level_t));
504eda14cbcSMatt Macy
505eda14cbcSMatt Macy verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
506eda14cbcSMatt Macy &top, &toplevels) == 0);
507eda14cbcSMatt Macy
508eda14cbcSMatt Macy for (t = 0; t < toplevels; t++) {
509eda14cbcSMatt Macy uint64_t is_log = B_FALSE;
510eda14cbcSMatt Macy
511eda14cbcSMatt Macy nv = top[t];
512eda14cbcSMatt Macy
513eda14cbcSMatt Macy /*
514eda14cbcSMatt Macy * For separate logs we ignore the top level vdev replication
515eda14cbcSMatt Macy * constraints.
516eda14cbcSMatt Macy */
517eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
518eda14cbcSMatt Macy if (is_log)
519eda14cbcSMatt Macy continue;
520eda14cbcSMatt Macy
521271171e0SMartin Matuska /*
522271171e0SMartin Matuska * Ignore holes introduced by removing aux devices, along
523271171e0SMartin Matuska * with indirect vdevs introduced by previously removed
524271171e0SMartin Matuska * vdevs.
525271171e0SMartin Matuska */
526eda14cbcSMatt Macy verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
527271171e0SMartin Matuska if (strcmp(type, VDEV_TYPE_HOLE) == 0 ||
528271171e0SMartin Matuska strcmp(type, VDEV_TYPE_INDIRECT) == 0)
529eda14cbcSMatt Macy continue;
530eda14cbcSMatt Macy
531eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
532eda14cbcSMatt Macy &child, &children) != 0) {
533eda14cbcSMatt Macy /*
534eda14cbcSMatt Macy * This is a 'file' or 'disk' vdev.
535eda14cbcSMatt Macy */
536eda14cbcSMatt Macy rep.zprl_type = type;
537eda14cbcSMatt Macy rep.zprl_children = 1;
538eda14cbcSMatt Macy rep.zprl_parity = 0;
539eda14cbcSMatt Macy } else {
540eda14cbcSMatt Macy int64_t vdev_size;
541eda14cbcSMatt Macy
542eda14cbcSMatt Macy /*
543eda14cbcSMatt Macy * This is a mirror or RAID-Z vdev. Go through and make
544eda14cbcSMatt Macy * sure the contents are all the same (files vs. disks),
545eda14cbcSMatt Macy * keeping track of the number of elements in the
546eda14cbcSMatt Macy * process.
547eda14cbcSMatt Macy *
548eda14cbcSMatt Macy * We also check that the size of each vdev (if it can
549eda14cbcSMatt Macy * be determined) is the same.
550eda14cbcSMatt Macy */
551eda14cbcSMatt Macy rep.zprl_type = type;
552eda14cbcSMatt Macy rep.zprl_children = 0;
553eda14cbcSMatt Macy
5547877fdebSMatt Macy if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
5557877fdebSMatt Macy strcmp(type, VDEV_TYPE_DRAID) == 0) {
556eda14cbcSMatt Macy verify(nvlist_lookup_uint64(nv,
557eda14cbcSMatt Macy ZPOOL_CONFIG_NPARITY,
558eda14cbcSMatt Macy &rep.zprl_parity) == 0);
559eda14cbcSMatt Macy assert(rep.zprl_parity != 0);
560eda14cbcSMatt Macy } else {
561eda14cbcSMatt Macy rep.zprl_parity = 0;
562eda14cbcSMatt Macy }
563eda14cbcSMatt Macy
564eda14cbcSMatt Macy /*
565eda14cbcSMatt Macy * The 'dontreport' variable indicates that we've
566eda14cbcSMatt Macy * already reported an error for this spec, so don't
567eda14cbcSMatt Macy * bother doing it again.
568eda14cbcSMatt Macy */
569eda14cbcSMatt Macy type = NULL;
570eda14cbcSMatt Macy dontreport = 0;
571eda14cbcSMatt Macy vdev_size = -1LL;
572eda14cbcSMatt Macy for (c = 0; c < children; c++) {
573eda14cbcSMatt Macy nvlist_t *cnv = child[c];
5742a58b312SMartin Matuska const char *path;
575eda14cbcSMatt Macy struct stat64 statbuf;
576eda14cbcSMatt Macy int64_t size = -1LL;
5772a58b312SMartin Matuska const char *childtype;
578eda14cbcSMatt Macy int fd, err;
579eda14cbcSMatt Macy
580eda14cbcSMatt Macy rep.zprl_children++;
581eda14cbcSMatt Macy
582eda14cbcSMatt Macy verify(nvlist_lookup_string(cnv,
583eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE, &childtype) == 0);
584eda14cbcSMatt Macy
585eda14cbcSMatt Macy /*
586eda14cbcSMatt Macy * If this is a replacing or spare vdev, then
587eda14cbcSMatt Macy * get the real first child of the vdev: do this
588eda14cbcSMatt Macy * in a loop because replacing and spare vdevs
589eda14cbcSMatt Macy * can be nested.
590eda14cbcSMatt Macy */
591eda14cbcSMatt Macy while (strcmp(childtype,
592eda14cbcSMatt Macy VDEV_TYPE_REPLACING) == 0 ||
593eda14cbcSMatt Macy strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
594eda14cbcSMatt Macy nvlist_t **rchild;
595eda14cbcSMatt Macy uint_t rchildren;
596eda14cbcSMatt Macy
597eda14cbcSMatt Macy verify(nvlist_lookup_nvlist_array(cnv,
598eda14cbcSMatt Macy ZPOOL_CONFIG_CHILDREN, &rchild,
599eda14cbcSMatt Macy &rchildren) == 0);
600eda14cbcSMatt Macy assert(rchildren == 2);
601eda14cbcSMatt Macy cnv = rchild[0];
602eda14cbcSMatt Macy
603eda14cbcSMatt Macy verify(nvlist_lookup_string(cnv,
604eda14cbcSMatt Macy ZPOOL_CONFIG_TYPE,
605eda14cbcSMatt Macy &childtype) == 0);
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy
608eda14cbcSMatt Macy verify(nvlist_lookup_string(cnv,
609eda14cbcSMatt Macy ZPOOL_CONFIG_PATH, &path) == 0);
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy /*
612eda14cbcSMatt Macy * If we have a raidz/mirror that combines disks
613eda14cbcSMatt Macy * with files, report it as an error.
614eda14cbcSMatt Macy */
615eda14cbcSMatt Macy if (!dontreport && type != NULL &&
616eda14cbcSMatt Macy strcmp(type, childtype) != 0) {
617eda14cbcSMatt Macy if (ret != NULL)
618eda14cbcSMatt Macy free(ret);
619eda14cbcSMatt Macy ret = NULL;
620eda14cbcSMatt Macy if (fatal)
621eda14cbcSMatt Macy vdev_error(gettext(
622eda14cbcSMatt Macy "mismatched replication "
623eda14cbcSMatt Macy "level: %s contains both "
624eda14cbcSMatt Macy "files and devices\n"),
625eda14cbcSMatt Macy rep.zprl_type);
626eda14cbcSMatt Macy else
627eda14cbcSMatt Macy return (NULL);
628eda14cbcSMatt Macy dontreport = B_TRUE;
629eda14cbcSMatt Macy }
630eda14cbcSMatt Macy
631eda14cbcSMatt Macy /*
632eda14cbcSMatt Macy * According to stat(2), the value of 'st_size'
633eda14cbcSMatt Macy * is undefined for block devices and character
634eda14cbcSMatt Macy * devices. But there is no effective way to
635eda14cbcSMatt Macy * determine the real size in userland.
636eda14cbcSMatt Macy *
637eda14cbcSMatt Macy * Instead, we'll take advantage of an
638eda14cbcSMatt Macy * implementation detail of spec_size(). If the
639eda14cbcSMatt Macy * device is currently open, then we (should)
640eda14cbcSMatt Macy * return a valid size.
641eda14cbcSMatt Macy *
642eda14cbcSMatt Macy * If we still don't get a valid size (indicated
643eda14cbcSMatt Macy * by a size of 0 or MAXOFFSET_T), then ignore
644eda14cbcSMatt Macy * this device altogether.
645eda14cbcSMatt Macy */
646eda14cbcSMatt Macy if ((fd = open(path, O_RDONLY)) >= 0) {
647eda14cbcSMatt Macy err = fstat64_blk(fd, &statbuf);
648eda14cbcSMatt Macy (void) close(fd);
649eda14cbcSMatt Macy } else {
650eda14cbcSMatt Macy err = stat64(path, &statbuf);
651eda14cbcSMatt Macy }
652eda14cbcSMatt Macy
653eda14cbcSMatt Macy if (err != 0 ||
654eda14cbcSMatt Macy statbuf.st_size == 0 ||
655eda14cbcSMatt Macy statbuf.st_size == MAXOFFSET_T)
656eda14cbcSMatt Macy continue;
657eda14cbcSMatt Macy
658eda14cbcSMatt Macy size = statbuf.st_size;
659eda14cbcSMatt Macy
660eda14cbcSMatt Macy /*
661eda14cbcSMatt Macy * Also make sure that devices and
662eda14cbcSMatt Macy * slices have a consistent size. If
663eda14cbcSMatt Macy * they differ by a significant amount
664eda14cbcSMatt Macy * (~16MB) then report an error.
665eda14cbcSMatt Macy */
666eda14cbcSMatt Macy if (!dontreport &&
667eda14cbcSMatt Macy (vdev_size != -1LL &&
668eda14cbcSMatt Macy (llabs(size - vdev_size) >
669eda14cbcSMatt Macy ZPOOL_FUZZ))) {
670eda14cbcSMatt Macy if (ret != NULL)
671eda14cbcSMatt Macy free(ret);
672eda14cbcSMatt Macy ret = NULL;
673eda14cbcSMatt Macy if (fatal)
674eda14cbcSMatt Macy vdev_error(gettext(
675eda14cbcSMatt Macy "%s contains devices of "
676eda14cbcSMatt Macy "different sizes\n"),
677eda14cbcSMatt Macy rep.zprl_type);
678eda14cbcSMatt Macy else
679eda14cbcSMatt Macy return (NULL);
680eda14cbcSMatt Macy dontreport = B_TRUE;
681eda14cbcSMatt Macy }
682eda14cbcSMatt Macy
683eda14cbcSMatt Macy type = childtype;
684eda14cbcSMatt Macy vdev_size = size;
685eda14cbcSMatt Macy }
686eda14cbcSMatt Macy }
687eda14cbcSMatt Macy
688eda14cbcSMatt Macy /*
689eda14cbcSMatt Macy * At this point, we have the replication of the last toplevel
690eda14cbcSMatt Macy * vdev in 'rep'. Compare it to 'lastrep' to see if it is
691eda14cbcSMatt Macy * different.
692eda14cbcSMatt Macy */
693eda14cbcSMatt Macy if (lastrep.zprl_type != NULL) {
694eda14cbcSMatt Macy if (is_raidz_mirror(&lastrep, &rep, &raidz, &mirror) ||
695eda14cbcSMatt Macy is_raidz_mirror(&rep, &lastrep, &raidz, &mirror)) {
696eda14cbcSMatt Macy /*
697eda14cbcSMatt Macy * Accepted raidz and mirror when they can
698eda14cbcSMatt Macy * handle the same number of disk failures.
699eda14cbcSMatt Macy */
700eda14cbcSMatt Macy if (raidz->zprl_parity !=
701eda14cbcSMatt Macy mirror->zprl_children - 1) {
702eda14cbcSMatt Macy if (ret != NULL)
703eda14cbcSMatt Macy free(ret);
704eda14cbcSMatt Macy ret = NULL;
705eda14cbcSMatt Macy if (fatal)
706eda14cbcSMatt Macy vdev_error(gettext(
707eda14cbcSMatt Macy "mismatched replication "
708eda14cbcSMatt Macy "level: "
709eda14cbcSMatt Macy "%s and %s vdevs with "
710eda14cbcSMatt Macy "different redundancy, "
711eda14cbcSMatt Macy "%llu vs. %llu (%llu-way) "
712eda14cbcSMatt Macy "are present\n"),
713eda14cbcSMatt Macy raidz->zprl_type,
714eda14cbcSMatt Macy mirror->zprl_type,
7151f88aa09SMartin Matuska (u_longlong_t)
716eda14cbcSMatt Macy raidz->zprl_parity,
7171f88aa09SMartin Matuska (u_longlong_t)
718eda14cbcSMatt Macy mirror->zprl_children - 1,
7191f88aa09SMartin Matuska (u_longlong_t)
720eda14cbcSMatt Macy mirror->zprl_children);
721eda14cbcSMatt Macy else
722eda14cbcSMatt Macy return (NULL);
723eda14cbcSMatt Macy }
7247877fdebSMatt Macy } else if (is_raidz_draid(&lastrep, &rep)) {
7257877fdebSMatt Macy /*
7267877fdebSMatt Macy * Accepted raidz and draid when they can
7277877fdebSMatt Macy * handle the same number of disk failures.
7287877fdebSMatt Macy */
7297877fdebSMatt Macy if (lastrep.zprl_parity != rep.zprl_parity) {
7307877fdebSMatt Macy if (ret != NULL)
7317877fdebSMatt Macy free(ret);
7327877fdebSMatt Macy ret = NULL;
7337877fdebSMatt Macy if (fatal)
7347877fdebSMatt Macy vdev_error(gettext(
7357877fdebSMatt Macy "mismatched replication "
7367877fdebSMatt Macy "level: %s and %s vdevs "
7377877fdebSMatt Macy "with different "
7387877fdebSMatt Macy "redundancy, %llu vs. "
7397877fdebSMatt Macy "%llu are present\n"),
7407877fdebSMatt Macy lastrep.zprl_type,
7417877fdebSMatt Macy rep.zprl_type,
7421f88aa09SMartin Matuska (u_longlong_t)
7437877fdebSMatt Macy lastrep.zprl_parity,
7441f88aa09SMartin Matuska (u_longlong_t)
7457877fdebSMatt Macy rep.zprl_parity);
7467877fdebSMatt Macy else
7477877fdebSMatt Macy return (NULL);
7487877fdebSMatt Macy }
749eda14cbcSMatt Macy } else if (strcmp(lastrep.zprl_type, rep.zprl_type) !=
750eda14cbcSMatt Macy 0) {
751eda14cbcSMatt Macy if (ret != NULL)
752eda14cbcSMatt Macy free(ret);
753eda14cbcSMatt Macy ret = NULL;
754eda14cbcSMatt Macy if (fatal)
755eda14cbcSMatt Macy vdev_error(gettext(
756eda14cbcSMatt Macy "mismatched replication level: "
757eda14cbcSMatt Macy "both %s and %s vdevs are "
758eda14cbcSMatt Macy "present\n"),
759eda14cbcSMatt Macy lastrep.zprl_type, rep.zprl_type);
760eda14cbcSMatt Macy else
761eda14cbcSMatt Macy return (NULL);
762eda14cbcSMatt Macy } else if (lastrep.zprl_parity != rep.zprl_parity) {
763eda14cbcSMatt Macy if (ret)
764eda14cbcSMatt Macy free(ret);
765eda14cbcSMatt Macy ret = NULL;
766eda14cbcSMatt Macy if (fatal)
767eda14cbcSMatt Macy vdev_error(gettext(
768eda14cbcSMatt Macy "mismatched replication level: "
769eda14cbcSMatt Macy "both %llu and %llu device parity "
770eda14cbcSMatt Macy "%s vdevs are present\n"),
7711f88aa09SMartin Matuska (u_longlong_t)
772eda14cbcSMatt Macy lastrep.zprl_parity,
7731f88aa09SMartin Matuska (u_longlong_t)rep.zprl_parity,
774eda14cbcSMatt Macy rep.zprl_type);
775eda14cbcSMatt Macy else
776eda14cbcSMatt Macy return (NULL);
777eda14cbcSMatt Macy } else if (lastrep.zprl_children != rep.zprl_children) {
778eda14cbcSMatt Macy if (ret)
779eda14cbcSMatt Macy free(ret);
780eda14cbcSMatt Macy ret = NULL;
781eda14cbcSMatt Macy if (fatal)
782eda14cbcSMatt Macy vdev_error(gettext(
783eda14cbcSMatt Macy "mismatched replication level: "
784eda14cbcSMatt Macy "both %llu-way and %llu-way %s "
785eda14cbcSMatt Macy "vdevs are present\n"),
7861f88aa09SMartin Matuska (u_longlong_t)
787eda14cbcSMatt Macy lastrep.zprl_children,
7881f88aa09SMartin Matuska (u_longlong_t)
789eda14cbcSMatt Macy rep.zprl_children,
790eda14cbcSMatt Macy rep.zprl_type);
791eda14cbcSMatt Macy else
792eda14cbcSMatt Macy return (NULL);
793eda14cbcSMatt Macy }
794eda14cbcSMatt Macy }
795eda14cbcSMatt Macy lastrep = rep;
796eda14cbcSMatt Macy }
797eda14cbcSMatt Macy
798eda14cbcSMatt Macy if (ret != NULL)
799eda14cbcSMatt Macy *ret = rep;
800eda14cbcSMatt Macy
801eda14cbcSMatt Macy return (ret);
802eda14cbcSMatt Macy }
803eda14cbcSMatt Macy
804eda14cbcSMatt Macy /*
805eda14cbcSMatt Macy * Check the replication level of the vdev spec against the current pool. Calls
806eda14cbcSMatt Macy * get_replication() to make sure the new spec is self-consistent. If the pool
807eda14cbcSMatt Macy * has a consistent replication level, then we ignore any errors. Otherwise,
808eda14cbcSMatt Macy * report any difference between the two.
809eda14cbcSMatt Macy */
810eda14cbcSMatt Macy static int
check_replication(nvlist_t * config,nvlist_t * newroot)811eda14cbcSMatt Macy check_replication(nvlist_t *config, nvlist_t *newroot)
812eda14cbcSMatt Macy {
813eda14cbcSMatt Macy nvlist_t **child;
814eda14cbcSMatt Macy uint_t children;
815eda14cbcSMatt Macy replication_level_t *current = NULL, *new;
816eda14cbcSMatt Macy replication_level_t *raidz, *mirror;
817eda14cbcSMatt Macy int ret;
818eda14cbcSMatt Macy
819eda14cbcSMatt Macy /*
820eda14cbcSMatt Macy * If we have a current pool configuration, check to see if it's
821eda14cbcSMatt Macy * self-consistent. If not, simply return success.
822eda14cbcSMatt Macy */
823eda14cbcSMatt Macy if (config != NULL) {
824eda14cbcSMatt Macy nvlist_t *nvroot;
825eda14cbcSMatt Macy
826eda14cbcSMatt Macy verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
827eda14cbcSMatt Macy &nvroot) == 0);
828eda14cbcSMatt Macy if ((current = get_replication(nvroot, B_FALSE)) == NULL)
829eda14cbcSMatt Macy return (0);
830eda14cbcSMatt Macy }
831eda14cbcSMatt Macy /*
832eda14cbcSMatt Macy * for spares there may be no children, and therefore no
833eda14cbcSMatt Macy * replication level to check
834eda14cbcSMatt Macy */
835eda14cbcSMatt Macy if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
836eda14cbcSMatt Macy &child, &children) != 0) || (children == 0)) {
837eda14cbcSMatt Macy free(current);
838eda14cbcSMatt Macy return (0);
839eda14cbcSMatt Macy }
840eda14cbcSMatt Macy
841eda14cbcSMatt Macy /*
842eda14cbcSMatt Macy * If all we have is logs then there's no replication level to check.
843eda14cbcSMatt Macy */
844eda14cbcSMatt Macy if (num_logs(newroot) == children) {
845eda14cbcSMatt Macy free(current);
846eda14cbcSMatt Macy return (0);
847eda14cbcSMatt Macy }
848eda14cbcSMatt Macy
849eda14cbcSMatt Macy /*
850eda14cbcSMatt Macy * Get the replication level of the new vdev spec, reporting any
851eda14cbcSMatt Macy * inconsistencies found.
852eda14cbcSMatt Macy */
853eda14cbcSMatt Macy if ((new = get_replication(newroot, B_TRUE)) == NULL) {
854eda14cbcSMatt Macy free(current);
855eda14cbcSMatt Macy return (-1);
856eda14cbcSMatt Macy }
857eda14cbcSMatt Macy
858eda14cbcSMatt Macy /*
859eda14cbcSMatt Macy * Check to see if the new vdev spec matches the replication level of
860eda14cbcSMatt Macy * the current pool.
861eda14cbcSMatt Macy */
862eda14cbcSMatt Macy ret = 0;
863eda14cbcSMatt Macy if (current != NULL) {
864eda14cbcSMatt Macy if (is_raidz_mirror(current, new, &raidz, &mirror) ||
865eda14cbcSMatt Macy is_raidz_mirror(new, current, &raidz, &mirror)) {
866eda14cbcSMatt Macy if (raidz->zprl_parity != mirror->zprl_children - 1) {
867eda14cbcSMatt Macy vdev_error(gettext(
868eda14cbcSMatt Macy "mismatched replication level: pool and "
869eda14cbcSMatt Macy "new vdev with different redundancy, %s "
870eda14cbcSMatt Macy "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
871eda14cbcSMatt Macy raidz->zprl_type,
872eda14cbcSMatt Macy mirror->zprl_type,
8731f88aa09SMartin Matuska (u_longlong_t)raidz->zprl_parity,
8741f88aa09SMartin Matuska (u_longlong_t)mirror->zprl_children - 1,
8751f88aa09SMartin Matuska (u_longlong_t)mirror->zprl_children);
876eda14cbcSMatt Macy ret = -1;
877eda14cbcSMatt Macy }
878eda14cbcSMatt Macy } else if (strcmp(current->zprl_type, new->zprl_type) != 0) {
879eda14cbcSMatt Macy vdev_error(gettext(
880eda14cbcSMatt Macy "mismatched replication level: pool uses %s "
881eda14cbcSMatt Macy "and new vdev is %s\n"),
882eda14cbcSMatt Macy current->zprl_type, new->zprl_type);
883eda14cbcSMatt Macy ret = -1;
884eda14cbcSMatt Macy } else if (current->zprl_parity != new->zprl_parity) {
885eda14cbcSMatt Macy vdev_error(gettext(
886eda14cbcSMatt Macy "mismatched replication level: pool uses %llu "
887eda14cbcSMatt Macy "device parity and new vdev uses %llu\n"),
8881f88aa09SMartin Matuska (u_longlong_t)current->zprl_parity,
8891f88aa09SMartin Matuska (u_longlong_t)new->zprl_parity);
890eda14cbcSMatt Macy ret = -1;
891eda14cbcSMatt Macy } else if (current->zprl_children != new->zprl_children) {
892eda14cbcSMatt Macy vdev_error(gettext(
893eda14cbcSMatt Macy "mismatched replication level: pool uses %llu-way "
894eda14cbcSMatt Macy "%s and new vdev uses %llu-way %s\n"),
8951f88aa09SMartin Matuska (u_longlong_t)current->zprl_children,
8961f88aa09SMartin Matuska current->zprl_type,
8971f88aa09SMartin Matuska (u_longlong_t)new->zprl_children,
8981f88aa09SMartin Matuska new->zprl_type);
899eda14cbcSMatt Macy ret = -1;
900eda14cbcSMatt Macy }
901eda14cbcSMatt Macy }
902eda14cbcSMatt Macy
903eda14cbcSMatt Macy free(new);
904eda14cbcSMatt Macy if (current != NULL)
905eda14cbcSMatt Macy free(current);
906eda14cbcSMatt Macy
907eda14cbcSMatt Macy return (ret);
908eda14cbcSMatt Macy }
909eda14cbcSMatt Macy
910eda14cbcSMatt Macy static int
zero_label(const char * path)9112a58b312SMartin Matuska zero_label(const char *path)
912eda14cbcSMatt Macy {
913eda14cbcSMatt Macy const int size = 4096;
914eda14cbcSMatt Macy char buf[size];
915eda14cbcSMatt Macy int err, fd;
916eda14cbcSMatt Macy
917eda14cbcSMatt Macy if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
918eda14cbcSMatt Macy (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
919eda14cbcSMatt Macy path, strerror(errno));
920eda14cbcSMatt Macy return (-1);
921eda14cbcSMatt Macy }
922eda14cbcSMatt Macy
923eda14cbcSMatt Macy memset(buf, 0, size);
924eda14cbcSMatt Macy err = write(fd, buf, size);
925eda14cbcSMatt Macy (void) fdatasync(fd);
926eda14cbcSMatt Macy (void) close(fd);
927eda14cbcSMatt Macy
928eda14cbcSMatt Macy if (err == -1) {
929eda14cbcSMatt Macy (void) fprintf(stderr, gettext("cannot zero first %d bytes "
930eda14cbcSMatt Macy "of '%s': %s\n"), size, path, strerror(errno));
931eda14cbcSMatt Macy return (-1);
932eda14cbcSMatt Macy }
933eda14cbcSMatt Macy
934eda14cbcSMatt Macy if (err != size) {
935eda14cbcSMatt Macy (void) fprintf(stderr, gettext("could only zero %d/%d bytes "
936eda14cbcSMatt Macy "of '%s'\n"), err, size, path);
937eda14cbcSMatt Macy return (-1);
938eda14cbcSMatt Macy }
939eda14cbcSMatt Macy
940eda14cbcSMatt Macy return (0);
941eda14cbcSMatt Macy }
942eda14cbcSMatt Macy
943abcdc1b9SMartin Matuska static void
lines_to_stderr(char * lines[],int lines_cnt)944abcdc1b9SMartin Matuska lines_to_stderr(char *lines[], int lines_cnt)
945abcdc1b9SMartin Matuska {
946abcdc1b9SMartin Matuska int i;
947abcdc1b9SMartin Matuska for (i = 0; i < lines_cnt; i++) {
948abcdc1b9SMartin Matuska fprintf(stderr, "%s\n", lines[i]);
949abcdc1b9SMartin Matuska }
950abcdc1b9SMartin Matuska }
951abcdc1b9SMartin Matuska
952eda14cbcSMatt Macy /*
953eda14cbcSMatt Macy * Go through and find any whole disks in the vdev specification, labelling them
954eda14cbcSMatt Macy * as appropriate. When constructing the vdev spec, we were unable to open this
955eda14cbcSMatt Macy * device in order to provide a devid. Now that we have labelled the disk and
956eda14cbcSMatt Macy * know that slice 0 is valid, we can construct the devid now.
957eda14cbcSMatt Macy *
958eda14cbcSMatt Macy * If the disk was already labeled with an EFI label, we will have gotten the
959eda14cbcSMatt Macy * devid already (because we were able to open the whole disk). Otherwise, we
960eda14cbcSMatt Macy * need to get the devid after we label the disk.
961eda14cbcSMatt Macy */
962eda14cbcSMatt Macy static int
make_disks(zpool_handle_t * zhp,nvlist_t * nv,boolean_t replacing)963abcdc1b9SMartin Matuska make_disks(zpool_handle_t *zhp, nvlist_t *nv, boolean_t replacing)
964eda14cbcSMatt Macy {
965eda14cbcSMatt Macy nvlist_t **child;
966eda14cbcSMatt Macy uint_t c, children;
9672a58b312SMartin Matuska const char *type, *path;
968eda14cbcSMatt Macy char devpath[MAXPATHLEN];
969eda14cbcSMatt Macy char udevpath[MAXPATHLEN];
970eda14cbcSMatt Macy uint64_t wholedisk;
971eda14cbcSMatt Macy struct stat64 statbuf;
972eda14cbcSMatt Macy int is_exclusive = 0;
973eda14cbcSMatt Macy int fd;
974eda14cbcSMatt Macy int ret;
975eda14cbcSMatt Macy
976eda14cbcSMatt Macy verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
977eda14cbcSMatt Macy
978eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
979eda14cbcSMatt Macy &child, &children) != 0) {
980eda14cbcSMatt Macy
981eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_DISK) != 0)
982eda14cbcSMatt Macy return (0);
983eda14cbcSMatt Macy
984eda14cbcSMatt Macy /*
985eda14cbcSMatt Macy * We have a disk device. If this is a whole disk write
986eda14cbcSMatt Macy * out the efi partition table, otherwise write zero's to
987eda14cbcSMatt Macy * the first 4k of the partition. This is to ensure that
988eda14cbcSMatt Macy * libblkid will not misidentify the partition due to a
989eda14cbcSMatt Macy * magic value left by the previous filesystem.
990eda14cbcSMatt Macy */
991eda14cbcSMatt Macy verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
992eda14cbcSMatt Macy verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
993eda14cbcSMatt Macy &wholedisk));
994eda14cbcSMatt Macy
995eda14cbcSMatt Macy if (!wholedisk) {
996eda14cbcSMatt Macy /*
997eda14cbcSMatt Macy * Update device id string for mpath nodes (Linux only)
998eda14cbcSMatt Macy */
999eda14cbcSMatt Macy if (is_mpath_whole_disk(path))
1000eda14cbcSMatt Macy update_vdev_config_dev_strs(nv);
1001eda14cbcSMatt Macy
1002eda14cbcSMatt Macy if (!is_spare(NULL, path))
1003eda14cbcSMatt Macy (void) zero_label(path);
1004eda14cbcSMatt Macy return (0);
1005eda14cbcSMatt Macy }
1006eda14cbcSMatt Macy
1007eda14cbcSMatt Macy if (realpath(path, devpath) == NULL) {
1008eda14cbcSMatt Macy ret = errno;
1009eda14cbcSMatt Macy (void) fprintf(stderr,
1010eda14cbcSMatt Macy gettext("cannot resolve path '%s'\n"), path);
1011eda14cbcSMatt Macy return (ret);
1012eda14cbcSMatt Macy }
1013eda14cbcSMatt Macy
1014eda14cbcSMatt Macy /*
1015eda14cbcSMatt Macy * Remove any previously existing symlink from a udev path to
1016eda14cbcSMatt Macy * the device before labeling the disk. This ensures that
1017eda14cbcSMatt Macy * only newly created links are used. Otherwise there is a
1018eda14cbcSMatt Macy * window between when udev deletes and recreates the link
1019eda14cbcSMatt Macy * during which access attempts will fail with ENOENT.
1020eda14cbcSMatt Macy */
1021eda14cbcSMatt Macy strlcpy(udevpath, path, MAXPATHLEN);
1022eda14cbcSMatt Macy (void) zfs_append_partition(udevpath, MAXPATHLEN);
1023eda14cbcSMatt Macy
1024eda14cbcSMatt Macy fd = open(devpath, O_RDWR|O_EXCL);
1025eda14cbcSMatt Macy if (fd == -1) {
1026eda14cbcSMatt Macy if (errno == EBUSY)
1027eda14cbcSMatt Macy is_exclusive = 1;
1028eda14cbcSMatt Macy #ifdef __FreeBSD__
1029eda14cbcSMatt Macy if (errno == EPERM)
1030eda14cbcSMatt Macy is_exclusive = 1;
1031eda14cbcSMatt Macy #endif
1032eda14cbcSMatt Macy } else {
1033eda14cbcSMatt Macy (void) close(fd);
1034eda14cbcSMatt Macy }
1035eda14cbcSMatt Macy
1036eda14cbcSMatt Macy /*
1037eda14cbcSMatt Macy * If the partition exists, contains a valid spare label,
1038eda14cbcSMatt Macy * and is opened exclusively there is no need to partition
1039eda14cbcSMatt Macy * it. Hot spares have already been partitioned and are
1040eda14cbcSMatt Macy * held open exclusively by the kernel as a safety measure.
1041eda14cbcSMatt Macy *
1042eda14cbcSMatt Macy * If the provided path is for a /dev/disk/ device its
1043eda14cbcSMatt Macy * symbolic link will be removed, partition table created,
1044eda14cbcSMatt Macy * and then block until udev creates the new link.
1045eda14cbcSMatt Macy */
1046eda14cbcSMatt Macy if (!is_exclusive && !is_spare(NULL, udevpath)) {
1047eda14cbcSMatt Macy char *devnode = strrchr(devpath, '/') + 1;
1048abcdc1b9SMartin Matuska char **lines = NULL;
1049abcdc1b9SMartin Matuska int lines_cnt = 0;
1050eda14cbcSMatt Macy
1051eda14cbcSMatt Macy ret = strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT));
1052eda14cbcSMatt Macy if (ret == 0) {
1053eda14cbcSMatt Macy ret = lstat64(udevpath, &statbuf);
1054eda14cbcSMatt Macy if (ret == 0 && S_ISLNK(statbuf.st_mode))
1055eda14cbcSMatt Macy (void) unlink(udevpath);
1056eda14cbcSMatt Macy }
1057eda14cbcSMatt Macy
1058eda14cbcSMatt Macy /*
1059eda14cbcSMatt Macy * When labeling a pool the raw device node name
1060eda14cbcSMatt Macy * is provided as it appears under /dev/.
1061abcdc1b9SMartin Matuska *
1062abcdc1b9SMartin Matuska * Note that 'zhp' will be NULL when we're creating a
1063abcdc1b9SMartin Matuska * pool.
1064eda14cbcSMatt Macy */
1065abcdc1b9SMartin Matuska if (zpool_prepare_and_label_disk(g_zfs, zhp, devnode,
1066abcdc1b9SMartin Matuska nv, zhp == NULL ? "create" :
1067abcdc1b9SMartin Matuska replacing ? "replace" : "add", &lines,
1068abcdc1b9SMartin Matuska &lines_cnt) != 0) {
1069abcdc1b9SMartin Matuska (void) fprintf(stderr,
1070abcdc1b9SMartin Matuska gettext(
1071abcdc1b9SMartin Matuska "Error preparing/labeling disk.\n"));
1072abcdc1b9SMartin Matuska if (lines_cnt > 0) {
1073abcdc1b9SMartin Matuska (void) fprintf(stderr,
1074abcdc1b9SMartin Matuska gettext("zfs_prepare_disk output:\n"));
1075abcdc1b9SMartin Matuska lines_to_stderr(lines, lines_cnt);
1076abcdc1b9SMartin Matuska }
1077abcdc1b9SMartin Matuska
1078abcdc1b9SMartin Matuska libzfs_free_str_array(lines, lines_cnt);
1079eda14cbcSMatt Macy return (-1);
1080abcdc1b9SMartin Matuska }
1081abcdc1b9SMartin Matuska libzfs_free_str_array(lines, lines_cnt);
1082eda14cbcSMatt Macy
1083eda14cbcSMatt Macy /*
1084eda14cbcSMatt Macy * Wait for udev to signal the device is available
1085eda14cbcSMatt Macy * by the provided path.
1086eda14cbcSMatt Macy */
1087eda14cbcSMatt Macy ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
1088eda14cbcSMatt Macy if (ret) {
1089eda14cbcSMatt Macy (void) fprintf(stderr,
1090eda14cbcSMatt Macy gettext("missing link: %s was "
1091eda14cbcSMatt Macy "partitioned but %s is missing\n"),
1092eda14cbcSMatt Macy devnode, udevpath);
1093eda14cbcSMatt Macy return (ret);
1094eda14cbcSMatt Macy }
1095eda14cbcSMatt Macy
1096eda14cbcSMatt Macy ret = zero_label(udevpath);
1097eda14cbcSMatt Macy if (ret)
1098eda14cbcSMatt Macy return (ret);
1099eda14cbcSMatt Macy }
1100eda14cbcSMatt Macy
1101eda14cbcSMatt Macy /*
1102eda14cbcSMatt Macy * Update the path to refer to the partition. The presence of
1103eda14cbcSMatt Macy * the 'whole_disk' field indicates to the CLI that we should
1104eda14cbcSMatt Macy * chop off the partition number when displaying the device in
1105eda14cbcSMatt Macy * future output.
1106eda14cbcSMatt Macy */
1107eda14cbcSMatt Macy verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1108eda14cbcSMatt Macy
1109eda14cbcSMatt Macy /*
1110eda14cbcSMatt Macy * Update device id strings for whole disks (Linux only)
1111eda14cbcSMatt Macy */
1112eda14cbcSMatt Macy update_vdev_config_dev_strs(nv);
1113eda14cbcSMatt Macy
1114eda14cbcSMatt Macy return (0);
1115eda14cbcSMatt Macy }
1116eda14cbcSMatt Macy
1117eda14cbcSMatt Macy for (c = 0; c < children; c++)
1118abcdc1b9SMartin Matuska if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1119eda14cbcSMatt Macy return (ret);
1120eda14cbcSMatt Macy
1121eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1122eda14cbcSMatt Macy &child, &children) == 0)
1123eda14cbcSMatt Macy for (c = 0; c < children; c++)
1124abcdc1b9SMartin Matuska if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1125eda14cbcSMatt Macy return (ret);
1126eda14cbcSMatt Macy
1127eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1128eda14cbcSMatt Macy &child, &children) == 0)
1129eda14cbcSMatt Macy for (c = 0; c < children; c++)
1130abcdc1b9SMartin Matuska if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1131eda14cbcSMatt Macy return (ret);
1132eda14cbcSMatt Macy
1133eda14cbcSMatt Macy return (0);
1134eda14cbcSMatt Macy }
1135eda14cbcSMatt Macy
1136eda14cbcSMatt Macy /*
1137eda14cbcSMatt Macy * Go through and find any devices that are in use. We rely on libdiskmgt for
1138eda14cbcSMatt Macy * the majority of this task.
1139eda14cbcSMatt Macy */
1140eda14cbcSMatt Macy static boolean_t
is_device_in_use(nvlist_t * config,nvlist_t * nv,boolean_t force,boolean_t replacing,boolean_t isspare)1141eda14cbcSMatt Macy is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1142eda14cbcSMatt Macy boolean_t replacing, boolean_t isspare)
1143eda14cbcSMatt Macy {
1144eda14cbcSMatt Macy nvlist_t **child;
1145eda14cbcSMatt Macy uint_t c, children;
11462a58b312SMartin Matuska const char *type, *path;
1147eda14cbcSMatt Macy int ret = 0;
1148eda14cbcSMatt Macy char buf[MAXPATHLEN];
1149eda14cbcSMatt Macy uint64_t wholedisk = B_FALSE;
1150eda14cbcSMatt Macy boolean_t anyinuse = B_FALSE;
1151eda14cbcSMatt Macy
1152eda14cbcSMatt Macy verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1153eda14cbcSMatt Macy
1154eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1155eda14cbcSMatt Macy &child, &children) != 0) {
1156eda14cbcSMatt Macy
1157eda14cbcSMatt Macy verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1158eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_DISK) == 0)
1159eda14cbcSMatt Macy verify(!nvlist_lookup_uint64(nv,
1160eda14cbcSMatt Macy ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1161eda14cbcSMatt Macy
1162eda14cbcSMatt Macy /*
1163eda14cbcSMatt Macy * As a generic check, we look to see if this is a replace of a
1164eda14cbcSMatt Macy * hot spare within the same pool. If so, we allow it
1165eda14cbcSMatt Macy * regardless of what libblkid or zpool_in_use() says.
1166eda14cbcSMatt Macy */
1167eda14cbcSMatt Macy if (replacing) {
1168eda14cbcSMatt Macy (void) strlcpy(buf, path, sizeof (buf));
1169eda14cbcSMatt Macy if (wholedisk) {
1170eda14cbcSMatt Macy ret = zfs_append_partition(buf, sizeof (buf));
1171eda14cbcSMatt Macy if (ret == -1)
1172eda14cbcSMatt Macy return (-1);
1173eda14cbcSMatt Macy }
1174eda14cbcSMatt Macy
1175eda14cbcSMatt Macy if (is_spare(config, buf))
1176eda14cbcSMatt Macy return (B_FALSE);
1177eda14cbcSMatt Macy }
1178eda14cbcSMatt Macy
1179eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_DISK) == 0)
1180eda14cbcSMatt Macy ret = check_device(path, force, isspare, wholedisk);
1181eda14cbcSMatt Macy
1182eda14cbcSMatt Macy else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1183eda14cbcSMatt Macy ret = check_file(path, force, isspare);
1184eda14cbcSMatt Macy
1185eda14cbcSMatt Macy return (ret != 0);
1186eda14cbcSMatt Macy }
1187eda14cbcSMatt Macy
1188eda14cbcSMatt Macy for (c = 0; c < children; c++)
1189eda14cbcSMatt Macy if (is_device_in_use(config, child[c], force, replacing,
1190eda14cbcSMatt Macy B_FALSE))
1191eda14cbcSMatt Macy anyinuse = B_TRUE;
1192eda14cbcSMatt Macy
1193eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1194eda14cbcSMatt Macy &child, &children) == 0)
1195eda14cbcSMatt Macy for (c = 0; c < children; c++)
1196eda14cbcSMatt Macy if (is_device_in_use(config, child[c], force, replacing,
1197eda14cbcSMatt Macy B_TRUE))
1198eda14cbcSMatt Macy anyinuse = B_TRUE;
1199eda14cbcSMatt Macy
1200eda14cbcSMatt Macy if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1201eda14cbcSMatt Macy &child, &children) == 0)
1202eda14cbcSMatt Macy for (c = 0; c < children; c++)
1203eda14cbcSMatt Macy if (is_device_in_use(config, child[c], force, replacing,
1204eda14cbcSMatt Macy B_FALSE))
1205eda14cbcSMatt Macy anyinuse = B_TRUE;
1206eda14cbcSMatt Macy
1207eda14cbcSMatt Macy return (anyinuse);
1208eda14cbcSMatt Macy }
1209eda14cbcSMatt Macy
12107877fdebSMatt Macy /*
12117877fdebSMatt Macy * Returns the parity level extracted from a raidz or draid type.
12127877fdebSMatt Macy * If the parity cannot be determined zero is returned.
12137877fdebSMatt Macy */
12147877fdebSMatt Macy static int
get_parity(const char * type)12157877fdebSMatt Macy get_parity(const char *type)
12167877fdebSMatt Macy {
12177877fdebSMatt Macy long parity = 0;
12187877fdebSMatt Macy const char *p;
12197877fdebSMatt Macy
12207877fdebSMatt Macy if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0) {
12217877fdebSMatt Macy p = type + strlen(VDEV_TYPE_RAIDZ);
12227877fdebSMatt Macy
12237877fdebSMatt Macy if (*p == '\0') {
12247877fdebSMatt Macy /* when unspecified default to single parity */
12257877fdebSMatt Macy return (1);
12267877fdebSMatt Macy } else if (*p == '0') {
12277877fdebSMatt Macy /* no zero prefixes allowed */
12287877fdebSMatt Macy return (0);
12297877fdebSMatt Macy } else {
12307877fdebSMatt Macy /* 0-3, no suffixes allowed */
12317877fdebSMatt Macy char *end;
12327877fdebSMatt Macy errno = 0;
12337877fdebSMatt Macy parity = strtol(p, &end, 10);
12347877fdebSMatt Macy if (errno != 0 || *end != '\0' ||
12357877fdebSMatt Macy parity < 1 || parity > VDEV_RAIDZ_MAXPARITY) {
12367877fdebSMatt Macy return (0);
12377877fdebSMatt Macy }
12387877fdebSMatt Macy }
12397877fdebSMatt Macy } else if (strncmp(type, VDEV_TYPE_DRAID,
12407877fdebSMatt Macy strlen(VDEV_TYPE_DRAID)) == 0) {
12417877fdebSMatt Macy p = type + strlen(VDEV_TYPE_DRAID);
12427877fdebSMatt Macy
12437877fdebSMatt Macy if (*p == '\0' || *p == ':') {
12447877fdebSMatt Macy /* when unspecified default to single parity */
12457877fdebSMatt Macy return (1);
12467877fdebSMatt Macy } else if (*p == '0') {
12477877fdebSMatt Macy /* no zero prefixes allowed */
12487877fdebSMatt Macy return (0);
12497877fdebSMatt Macy } else {
12507877fdebSMatt Macy /* 0-3, allowed suffixes: '\0' or ':' */
12517877fdebSMatt Macy char *end;
12527877fdebSMatt Macy errno = 0;
12537877fdebSMatt Macy parity = strtol(p, &end, 10);
12547877fdebSMatt Macy if (errno != 0 ||
12557877fdebSMatt Macy parity < 1 || parity > VDEV_DRAID_MAXPARITY ||
12567877fdebSMatt Macy (*end != '\0' && *end != ':')) {
12577877fdebSMatt Macy return (0);
12587877fdebSMatt Macy }
12597877fdebSMatt Macy }
12607877fdebSMatt Macy }
12617877fdebSMatt Macy
12627877fdebSMatt Macy return ((int)parity);
12637877fdebSMatt Macy }
12647877fdebSMatt Macy
12657877fdebSMatt Macy /*
12667877fdebSMatt Macy * Assign the minimum and maximum number of devices allowed for
12677877fdebSMatt Macy * the specified type. On error NULL is returned, otherwise the
12687877fdebSMatt Macy * type prefix is returned (raidz, mirror, etc).
12697877fdebSMatt Macy */
1270eda14cbcSMatt Macy static const char *
is_grouping(const char * type,int * mindev,int * maxdev)1271eda14cbcSMatt Macy is_grouping(const char *type, int *mindev, int *maxdev)
1272eda14cbcSMatt Macy {
12737877fdebSMatt Macy int nparity;
1274eda14cbcSMatt Macy
12757877fdebSMatt Macy if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
12767877fdebSMatt Macy strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0) {
12777877fdebSMatt Macy nparity = get_parity(type);
12787877fdebSMatt Macy if (nparity == 0)
1279eda14cbcSMatt Macy return (NULL);
1280eda14cbcSMatt Macy if (mindev != NULL)
1281eda14cbcSMatt Macy *mindev = nparity + 1;
1282eda14cbcSMatt Macy if (maxdev != NULL)
1283eda14cbcSMatt Macy *maxdev = 255;
12847877fdebSMatt Macy
12857877fdebSMatt Macy if (strncmp(type, VDEV_TYPE_RAIDZ,
12867877fdebSMatt Macy strlen(VDEV_TYPE_RAIDZ)) == 0) {
1287eda14cbcSMatt Macy return (VDEV_TYPE_RAIDZ);
12887877fdebSMatt Macy } else {
12897877fdebSMatt Macy return (VDEV_TYPE_DRAID);
12907877fdebSMatt Macy }
1291eda14cbcSMatt Macy }
1292eda14cbcSMatt Macy
1293eda14cbcSMatt Macy if (maxdev != NULL)
1294eda14cbcSMatt Macy *maxdev = INT_MAX;
1295eda14cbcSMatt Macy
1296eda14cbcSMatt Macy if (strcmp(type, "mirror") == 0) {
1297eda14cbcSMatt Macy if (mindev != NULL)
1298eda14cbcSMatt Macy *mindev = 2;
1299eda14cbcSMatt Macy return (VDEV_TYPE_MIRROR);
1300eda14cbcSMatt Macy }
1301eda14cbcSMatt Macy
1302eda14cbcSMatt Macy if (strcmp(type, "spare") == 0) {
1303eda14cbcSMatt Macy if (mindev != NULL)
1304eda14cbcSMatt Macy *mindev = 1;
1305eda14cbcSMatt Macy return (VDEV_TYPE_SPARE);
1306eda14cbcSMatt Macy }
1307eda14cbcSMatt Macy
1308eda14cbcSMatt Macy if (strcmp(type, "log") == 0) {
1309eda14cbcSMatt Macy if (mindev != NULL)
1310eda14cbcSMatt Macy *mindev = 1;
1311eda14cbcSMatt Macy return (VDEV_TYPE_LOG);
1312eda14cbcSMatt Macy }
1313eda14cbcSMatt Macy
1314eda14cbcSMatt Macy if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
1315eda14cbcSMatt Macy strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1316eda14cbcSMatt Macy if (mindev != NULL)
1317eda14cbcSMatt Macy *mindev = 1;
1318eda14cbcSMatt Macy return (type);
1319eda14cbcSMatt Macy }
1320eda14cbcSMatt Macy
1321eda14cbcSMatt Macy if (strcmp(type, "cache") == 0) {
1322eda14cbcSMatt Macy if (mindev != NULL)
1323eda14cbcSMatt Macy *mindev = 1;
1324eda14cbcSMatt Macy return (VDEV_TYPE_L2CACHE);
1325eda14cbcSMatt Macy }
1326eda14cbcSMatt Macy
1327eda14cbcSMatt Macy return (NULL);
1328eda14cbcSMatt Macy }
1329eda14cbcSMatt Macy
1330eda14cbcSMatt Macy /*
13317877fdebSMatt Macy * Extract the configuration parameters encoded in the dRAID type and
13327877fdebSMatt Macy * use them to generate a dRAID configuration. The expected format is:
13337877fdebSMatt Macy *
13347877fdebSMatt Macy * draid[<parity>][:<data><d|D>][:<children><c|C>][:<spares><s|S>]
13357877fdebSMatt Macy *
13367877fdebSMatt Macy * The intent is to be able to generate a good configuration when no
13377877fdebSMatt Macy * additional information is provided. The only mandatory component
13387877fdebSMatt Macy * of the 'type' is the 'draid' prefix. If a value is not provided
13397877fdebSMatt Macy * then reasonable defaults are used. The optional components may
13407877fdebSMatt Macy * appear in any order but the d/s/c suffix is required.
13417877fdebSMatt Macy *
13427877fdebSMatt Macy * Valid inputs:
13437877fdebSMatt Macy * - data: number of data devices per group (1-255)
13447877fdebSMatt Macy * - parity: number of parity blocks per group (1-3)
13457877fdebSMatt Macy * - spares: number of distributed spare (0-100)
13467877fdebSMatt Macy * - children: total number of devices (1-255)
13477877fdebSMatt Macy *
13487877fdebSMatt Macy * Examples:
13497877fdebSMatt Macy * - zpool create tank draid <devices...>
13507877fdebSMatt Macy * - zpool create tank draid2:8d:51c:2s <devices...>
13517877fdebSMatt Macy */
13527877fdebSMatt Macy static int
draid_config_by_type(nvlist_t * nv,const char * type,uint64_t children)13537877fdebSMatt Macy draid_config_by_type(nvlist_t *nv, const char *type, uint64_t children)
13547877fdebSMatt Macy {
13557877fdebSMatt Macy uint64_t nparity = 1;
13567877fdebSMatt Macy uint64_t nspares = 0;
13577877fdebSMatt Macy uint64_t ndata = UINT64_MAX;
13587877fdebSMatt Macy uint64_t ngroups = 1;
13597877fdebSMatt Macy long value;
13607877fdebSMatt Macy
13617877fdebSMatt Macy if (strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) != 0)
13627877fdebSMatt Macy return (EINVAL);
13637877fdebSMatt Macy
13647877fdebSMatt Macy nparity = (uint64_t)get_parity(type);
13652a58b312SMartin Matuska if (nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
13662a58b312SMartin Matuska fprintf(stderr,
13672a58b312SMartin Matuska gettext("invalid dRAID parity level %llu; must be "
13682a58b312SMartin Matuska "between 1 and %d\n"), (u_longlong_t)nparity,
13692a58b312SMartin Matuska VDEV_DRAID_MAXPARITY);
13707877fdebSMatt Macy return (EINVAL);
13712a58b312SMartin Matuska }
13727877fdebSMatt Macy
13737877fdebSMatt Macy char *p = (char *)type;
13747877fdebSMatt Macy while ((p = strchr(p, ':')) != NULL) {
13757877fdebSMatt Macy char *end;
13767877fdebSMatt Macy
13777877fdebSMatt Macy p = p + 1;
13787877fdebSMatt Macy errno = 0;
13797877fdebSMatt Macy
13807877fdebSMatt Macy if (!isdigit(p[0])) {
13817877fdebSMatt Macy (void) fprintf(stderr, gettext("invalid dRAID "
13827877fdebSMatt Macy "syntax; expected [:<number><c|d|s>] not '%s'\n"),
13837877fdebSMatt Macy type);
13847877fdebSMatt Macy return (EINVAL);
13857877fdebSMatt Macy }
13867877fdebSMatt Macy
13877877fdebSMatt Macy /* Expected non-zero value with c/d/s suffix */
13887877fdebSMatt Macy value = strtol(p, &end, 10);
13897877fdebSMatt Macy char suffix = tolower(*end);
13907877fdebSMatt Macy if (errno != 0 ||
13917877fdebSMatt Macy (suffix != 'c' && suffix != 'd' && suffix != 's')) {
13927877fdebSMatt Macy (void) fprintf(stderr, gettext("invalid dRAID "
13937877fdebSMatt Macy "syntax; expected [:<number><c|d|s>] not '%s'\n"),
13947877fdebSMatt Macy type);
13957877fdebSMatt Macy return (EINVAL);
13967877fdebSMatt Macy }
13977877fdebSMatt Macy
13987877fdebSMatt Macy if (suffix == 'c') {
13997877fdebSMatt Macy if ((uint64_t)value != children) {
14007877fdebSMatt Macy fprintf(stderr,
14017877fdebSMatt Macy gettext("invalid number of dRAID children; "
14027877fdebSMatt Macy "%llu required but %llu provided\n"),
14037877fdebSMatt Macy (u_longlong_t)value,
14047877fdebSMatt Macy (u_longlong_t)children);
14057877fdebSMatt Macy return (EINVAL);
14067877fdebSMatt Macy }
14077877fdebSMatt Macy } else if (suffix == 'd') {
14087877fdebSMatt Macy ndata = (uint64_t)value;
14097877fdebSMatt Macy } else if (suffix == 's') {
14107877fdebSMatt Macy nspares = (uint64_t)value;
14117877fdebSMatt Macy } else {
14127877fdebSMatt Macy verify(0); /* Unreachable */
14137877fdebSMatt Macy }
14147877fdebSMatt Macy }
14157877fdebSMatt Macy
14167877fdebSMatt Macy /*
14177877fdebSMatt Macy * When a specific number of data disks is not provided limit a
14187877fdebSMatt Macy * redundancy group to 8 data disks. This value was selected to
14197877fdebSMatt Macy * provide a reasonable tradeoff between capacity and performance.
14207877fdebSMatt Macy */
14217877fdebSMatt Macy if (ndata == UINT64_MAX) {
14227877fdebSMatt Macy if (children > nspares + nparity) {
14237877fdebSMatt Macy ndata = MIN(children - nspares - nparity, 8);
14247877fdebSMatt Macy } else {
14257877fdebSMatt Macy fprintf(stderr, gettext("request number of "
14267877fdebSMatt Macy "distributed spares %llu and parity level %llu\n"
14277877fdebSMatt Macy "leaves no disks available for data\n"),
14287877fdebSMatt Macy (u_longlong_t)nspares, (u_longlong_t)nparity);
14297877fdebSMatt Macy return (EINVAL);
14307877fdebSMatt Macy }
14317877fdebSMatt Macy }
14327877fdebSMatt Macy
14337877fdebSMatt Macy /* Verify the maximum allowed group size is never exceeded. */
14347877fdebSMatt Macy if (ndata == 0 || (ndata + nparity > children - nspares)) {
14357877fdebSMatt Macy fprintf(stderr, gettext("requested number of dRAID data "
14367877fdebSMatt Macy "disks per group %llu is too high,\nat most %llu disks "
14377877fdebSMatt Macy "are available for data\n"), (u_longlong_t)ndata,
14387877fdebSMatt Macy (u_longlong_t)(children - nspares - nparity));
14397877fdebSMatt Macy return (EINVAL);
14407877fdebSMatt Macy }
14417877fdebSMatt Macy
14427877fdebSMatt Macy /*
14437877fdebSMatt Macy * Verify the requested number of spares can be satisfied.
14447877fdebSMatt Macy * An arbitrary limit of 100 distributed spares is applied.
14457877fdebSMatt Macy */
14467877fdebSMatt Macy if (nspares > 100 || nspares > (children - (ndata + nparity))) {
14477877fdebSMatt Macy fprintf(stderr,
14487877fdebSMatt Macy gettext("invalid number of dRAID spares %llu; additional "
14497877fdebSMatt Macy "disks would be required\n"), (u_longlong_t)nspares);
14507877fdebSMatt Macy return (EINVAL);
14517877fdebSMatt Macy }
14527877fdebSMatt Macy
14537877fdebSMatt Macy /* Verify the requested number children is sufficient. */
14547877fdebSMatt Macy if (children < (ndata + nparity + nspares)) {
14557877fdebSMatt Macy fprintf(stderr, gettext("%llu disks were provided, but at "
14567877fdebSMatt Macy "least %llu disks are required for this config\n"),
14577877fdebSMatt Macy (u_longlong_t)children,
14587877fdebSMatt Macy (u_longlong_t)(ndata + nparity + nspares));
14597877fdebSMatt Macy }
14607877fdebSMatt Macy
14617877fdebSMatt Macy if (children > VDEV_DRAID_MAX_CHILDREN) {
14627877fdebSMatt Macy fprintf(stderr, gettext("%llu disks were provided, but "
14637877fdebSMatt Macy "dRAID only supports up to %u disks"),
14647877fdebSMatt Macy (u_longlong_t)children, VDEV_DRAID_MAX_CHILDREN);
14657877fdebSMatt Macy }
14667877fdebSMatt Macy
14677877fdebSMatt Macy /*
14687877fdebSMatt Macy * Calculate the minimum number of groups required to fill a slice.
14697877fdebSMatt Macy * This is the LCM of the stripe width (ndata + nparity) and the
14707877fdebSMatt Macy * number of data drives (children - nspares).
14717877fdebSMatt Macy */
14727877fdebSMatt Macy while (ngroups * (ndata + nparity) % (children - nspares) != 0)
14737877fdebSMatt Macy ngroups++;
14747877fdebSMatt Macy
14757877fdebSMatt Macy /* Store the basic dRAID configuration. */
14767877fdebSMatt Macy fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, nparity);
14777877fdebSMatt Macy fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, ndata);
14787877fdebSMatt Macy fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, nspares);
14797877fdebSMatt Macy fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, ngroups);
14807877fdebSMatt Macy
14817877fdebSMatt Macy return (0);
14827877fdebSMatt Macy }
14837877fdebSMatt Macy
14847877fdebSMatt Macy /*
1485eda14cbcSMatt Macy * Construct a syntactically valid vdev specification,
1486eda14cbcSMatt Macy * and ensure that all devices and files exist and can be opened.
1487eda14cbcSMatt Macy * Note: we don't bother freeing anything in the error paths
1488eda14cbcSMatt Macy * because the program is just going to exit anyway.
1489eda14cbcSMatt Macy */
1490eda14cbcSMatt Macy static nvlist_t *
construct_spec(nvlist_t * props,int argc,char ** argv)1491eda14cbcSMatt Macy construct_spec(nvlist_t *props, int argc, char **argv)
1492eda14cbcSMatt Macy {
1493eda14cbcSMatt Macy nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1494eda14cbcSMatt Macy int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
14957877fdebSMatt Macy const char *type, *fulltype;
14967877fdebSMatt Macy boolean_t is_log, is_special, is_dedup, is_spare;
1497eda14cbcSMatt Macy boolean_t seen_logs;
1498eda14cbcSMatt Macy
1499eda14cbcSMatt Macy top = NULL;
1500eda14cbcSMatt Macy toplevels = 0;
1501eda14cbcSMatt Macy spares = NULL;
1502eda14cbcSMatt Macy l2cache = NULL;
1503eda14cbcSMatt Macy nspares = 0;
1504eda14cbcSMatt Macy nlogs = 0;
1505eda14cbcSMatt Macy nl2cache = 0;
15067877fdebSMatt Macy is_log = is_special = is_dedup = is_spare = B_FALSE;
1507eda14cbcSMatt Macy seen_logs = B_FALSE;
1508eda14cbcSMatt Macy nvroot = NULL;
1509eda14cbcSMatt Macy
1510eda14cbcSMatt Macy while (argc > 0) {
15117877fdebSMatt Macy fulltype = argv[0];
1512eda14cbcSMatt Macy nv = NULL;
1513eda14cbcSMatt Macy
1514eda14cbcSMatt Macy /*
15157877fdebSMatt Macy * If it's a mirror, raidz, or draid the subsequent arguments
15167877fdebSMatt Macy * are its leaves -- until we encounter the next mirror,
15177877fdebSMatt Macy * raidz or draid.
1518eda14cbcSMatt Macy */
15197877fdebSMatt Macy if ((type = is_grouping(fulltype, &mindev, &maxdev)) != NULL) {
1520eda14cbcSMatt Macy nvlist_t **child = NULL;
1521eda14cbcSMatt Macy int c, children = 0;
1522eda14cbcSMatt Macy
1523eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1524eda14cbcSMatt Macy if (spares != NULL) {
1525eda14cbcSMatt Macy (void) fprintf(stderr,
1526eda14cbcSMatt Macy gettext("invalid vdev "
1527eda14cbcSMatt Macy "specification: 'spare' can be "
1528eda14cbcSMatt Macy "specified only once\n"));
1529eda14cbcSMatt Macy goto spec_out;
1530eda14cbcSMatt Macy }
15317877fdebSMatt Macy is_spare = B_TRUE;
1532eda14cbcSMatt Macy is_log = is_special = is_dedup = B_FALSE;
1533eda14cbcSMatt Macy }
1534eda14cbcSMatt Macy
1535eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1536eda14cbcSMatt Macy if (seen_logs) {
1537eda14cbcSMatt Macy (void) fprintf(stderr,
1538eda14cbcSMatt Macy gettext("invalid vdev "
1539eda14cbcSMatt Macy "specification: 'log' can be "
1540eda14cbcSMatt Macy "specified only once\n"));
1541eda14cbcSMatt Macy goto spec_out;
1542eda14cbcSMatt Macy }
1543eda14cbcSMatt Macy seen_logs = B_TRUE;
1544eda14cbcSMatt Macy is_log = B_TRUE;
15457877fdebSMatt Macy is_special = is_dedup = is_spare = B_FALSE;
1546eda14cbcSMatt Macy argc--;
1547eda14cbcSMatt Macy argv++;
1548eda14cbcSMatt Macy /*
1549eda14cbcSMatt Macy * A log is not a real grouping device.
1550eda14cbcSMatt Macy * We just set is_log and continue.
1551eda14cbcSMatt Macy */
1552eda14cbcSMatt Macy continue;
1553eda14cbcSMatt Macy }
1554eda14cbcSMatt Macy
1555eda14cbcSMatt Macy if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1556eda14cbcSMatt Macy is_special = B_TRUE;
15577877fdebSMatt Macy is_log = is_dedup = is_spare = B_FALSE;
1558eda14cbcSMatt Macy argc--;
1559eda14cbcSMatt Macy argv++;
1560eda14cbcSMatt Macy continue;
1561eda14cbcSMatt Macy }
1562eda14cbcSMatt Macy
1563eda14cbcSMatt Macy if (strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1564eda14cbcSMatt Macy is_dedup = B_TRUE;
15657877fdebSMatt Macy is_log = is_special = is_spare = B_FALSE;
1566eda14cbcSMatt Macy argc--;
1567eda14cbcSMatt Macy argv++;
1568eda14cbcSMatt Macy continue;
1569eda14cbcSMatt Macy }
1570eda14cbcSMatt Macy
1571eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1572eda14cbcSMatt Macy if (l2cache != NULL) {
1573eda14cbcSMatt Macy (void) fprintf(stderr,
1574eda14cbcSMatt Macy gettext("invalid vdev "
1575eda14cbcSMatt Macy "specification: 'cache' can be "
1576eda14cbcSMatt Macy "specified only once\n"));
1577eda14cbcSMatt Macy goto spec_out;
1578eda14cbcSMatt Macy }
15797877fdebSMatt Macy is_log = is_special = B_FALSE;
15807877fdebSMatt Macy is_dedup = is_spare = B_FALSE;
1581eda14cbcSMatt Macy }
1582eda14cbcSMatt Macy
1583eda14cbcSMatt Macy if (is_log || is_special || is_dedup) {
1584eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1585eda14cbcSMatt Macy (void) fprintf(stderr,
1586eda14cbcSMatt Macy gettext("invalid vdev "
1587eda14cbcSMatt Macy "specification: unsupported '%s' "
1588eda14cbcSMatt Macy "device: %s\n"), is_log ? "log" :
1589eda14cbcSMatt Macy "special", type);
1590eda14cbcSMatt Macy goto spec_out;
1591eda14cbcSMatt Macy }
1592eda14cbcSMatt Macy nlogs++;
1593eda14cbcSMatt Macy }
1594eda14cbcSMatt Macy
1595eda14cbcSMatt Macy for (c = 1; c < argc; c++) {
1596eda14cbcSMatt Macy if (is_grouping(argv[c], NULL, NULL) != NULL)
1597eda14cbcSMatt Macy break;
15987877fdebSMatt Macy
1599eda14cbcSMatt Macy children++;
1600eda14cbcSMatt Macy child = realloc(child,
1601eda14cbcSMatt Macy children * sizeof (nvlist_t *));
1602eda14cbcSMatt Macy if (child == NULL)
1603eda14cbcSMatt Macy zpool_no_memory();
1604eda14cbcSMatt Macy if ((nv = make_leaf_vdev(props, argv[c],
16057877fdebSMatt Macy !(is_log || is_special || is_dedup ||
16067877fdebSMatt Macy is_spare))) == NULL) {
1607eda14cbcSMatt Macy for (c = 0; c < children - 1; c++)
1608eda14cbcSMatt Macy nvlist_free(child[c]);
1609eda14cbcSMatt Macy free(child);
1610eda14cbcSMatt Macy goto spec_out;
1611eda14cbcSMatt Macy }
1612eda14cbcSMatt Macy
1613eda14cbcSMatt Macy child[children - 1] = nv;
1614eda14cbcSMatt Macy }
1615eda14cbcSMatt Macy
1616eda14cbcSMatt Macy if (children < mindev) {
1617eda14cbcSMatt Macy (void) fprintf(stderr, gettext("invalid vdev "
1618eda14cbcSMatt Macy "specification: %s requires at least %d "
1619eda14cbcSMatt Macy "devices\n"), argv[0], mindev);
1620eda14cbcSMatt Macy for (c = 0; c < children; c++)
1621eda14cbcSMatt Macy nvlist_free(child[c]);
1622eda14cbcSMatt Macy free(child);
1623eda14cbcSMatt Macy goto spec_out;
1624eda14cbcSMatt Macy }
1625eda14cbcSMatt Macy
1626eda14cbcSMatt Macy if (children > maxdev) {
1627eda14cbcSMatt Macy (void) fprintf(stderr, gettext("invalid vdev "
1628eda14cbcSMatt Macy "specification: %s supports no more than "
1629eda14cbcSMatt Macy "%d devices\n"), argv[0], maxdev);
1630eda14cbcSMatt Macy for (c = 0; c < children; c++)
1631eda14cbcSMatt Macy nvlist_free(child[c]);
1632eda14cbcSMatt Macy free(child);
1633eda14cbcSMatt Macy goto spec_out;
1634eda14cbcSMatt Macy }
1635eda14cbcSMatt Macy
1636eda14cbcSMatt Macy argc -= c;
1637eda14cbcSMatt Macy argv += c;
1638eda14cbcSMatt Macy
1639eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1640eda14cbcSMatt Macy spares = child;
1641eda14cbcSMatt Macy nspares = children;
1642eda14cbcSMatt Macy continue;
1643eda14cbcSMatt Macy } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1644eda14cbcSMatt Macy l2cache = child;
1645eda14cbcSMatt Macy nl2cache = children;
1646eda14cbcSMatt Macy continue;
1647eda14cbcSMatt Macy } else {
1648eda14cbcSMatt Macy /* create a top-level vdev with children */
1649eda14cbcSMatt Macy verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1650eda14cbcSMatt Macy 0) == 0);
1651eda14cbcSMatt Macy verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1652eda14cbcSMatt Macy type) == 0);
1653eda14cbcSMatt Macy verify(nvlist_add_uint64(nv,
1654eda14cbcSMatt Macy ZPOOL_CONFIG_IS_LOG, is_log) == 0);
16557877fdebSMatt Macy if (is_log) {
1656eda14cbcSMatt Macy verify(nvlist_add_string(nv,
1657eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
1658eda14cbcSMatt Macy VDEV_ALLOC_BIAS_LOG) == 0);
16597877fdebSMatt Macy }
1660eda14cbcSMatt Macy if (is_special) {
1661eda14cbcSMatt Macy verify(nvlist_add_string(nv,
1662eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
1663eda14cbcSMatt Macy VDEV_ALLOC_BIAS_SPECIAL) == 0);
1664eda14cbcSMatt Macy }
1665eda14cbcSMatt Macy if (is_dedup) {
1666eda14cbcSMatt Macy verify(nvlist_add_string(nv,
1667eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
1668eda14cbcSMatt Macy VDEV_ALLOC_BIAS_DEDUP) == 0);
1669eda14cbcSMatt Macy }
1670eda14cbcSMatt Macy if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1671eda14cbcSMatt Macy verify(nvlist_add_uint64(nv,
1672eda14cbcSMatt Macy ZPOOL_CONFIG_NPARITY,
1673eda14cbcSMatt Macy mindev - 1) == 0);
1674eda14cbcSMatt Macy }
16757877fdebSMatt Macy if (strcmp(type, VDEV_TYPE_DRAID) == 0) {
16767877fdebSMatt Macy if (draid_config_by_type(nv,
16777877fdebSMatt Macy fulltype, children) != 0) {
16787877fdebSMatt Macy for (c = 0; c < children; c++)
16797877fdebSMatt Macy nvlist_free(child[c]);
16807877fdebSMatt Macy free(child);
16817877fdebSMatt Macy goto spec_out;
16827877fdebSMatt Macy }
16837877fdebSMatt Macy }
1684eda14cbcSMatt Macy verify(nvlist_add_nvlist_array(nv,
1685681ce946SMartin Matuska ZPOOL_CONFIG_CHILDREN,
1686681ce946SMartin Matuska (const nvlist_t **)child, children) == 0);
1687eda14cbcSMatt Macy
1688eda14cbcSMatt Macy for (c = 0; c < children; c++)
1689eda14cbcSMatt Macy nvlist_free(child[c]);
1690eda14cbcSMatt Macy free(child);
1691eda14cbcSMatt Macy }
1692eda14cbcSMatt Macy } else {
1693eda14cbcSMatt Macy /*
1694eda14cbcSMatt Macy * We have a device. Pass off to make_leaf_vdev() to
1695eda14cbcSMatt Macy * construct the appropriate nvlist describing the vdev.
1696eda14cbcSMatt Macy */
16977877fdebSMatt Macy if ((nv = make_leaf_vdev(props, argv[0], !(is_log ||
16987877fdebSMatt Macy is_special || is_dedup || is_spare))) == NULL)
1699eda14cbcSMatt Macy goto spec_out;
1700eda14cbcSMatt Macy
17017877fdebSMatt Macy verify(nvlist_add_uint64(nv,
17027877fdebSMatt Macy ZPOOL_CONFIG_IS_LOG, is_log) == 0);
17037877fdebSMatt Macy if (is_log) {
17047877fdebSMatt Macy verify(nvlist_add_string(nv,
17057877fdebSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
17067877fdebSMatt Macy VDEV_ALLOC_BIAS_LOG) == 0);
1707eda14cbcSMatt Macy nlogs++;
17087877fdebSMatt Macy }
17097877fdebSMatt Macy
1710eda14cbcSMatt Macy if (is_special) {
1711eda14cbcSMatt Macy verify(nvlist_add_string(nv,
1712eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
1713eda14cbcSMatt Macy VDEV_ALLOC_BIAS_SPECIAL) == 0);
1714eda14cbcSMatt Macy }
1715eda14cbcSMatt Macy if (is_dedup) {
1716eda14cbcSMatt Macy verify(nvlist_add_string(nv,
1717eda14cbcSMatt Macy ZPOOL_CONFIG_ALLOCATION_BIAS,
1718eda14cbcSMatt Macy VDEV_ALLOC_BIAS_DEDUP) == 0);
1719eda14cbcSMatt Macy }
1720eda14cbcSMatt Macy argc--;
1721eda14cbcSMatt Macy argv++;
1722eda14cbcSMatt Macy }
1723eda14cbcSMatt Macy
1724eda14cbcSMatt Macy toplevels++;
1725eda14cbcSMatt Macy top = realloc(top, toplevels * sizeof (nvlist_t *));
1726eda14cbcSMatt Macy if (top == NULL)
1727eda14cbcSMatt Macy zpool_no_memory();
1728eda14cbcSMatt Macy top[toplevels - 1] = nv;
1729eda14cbcSMatt Macy }
1730eda14cbcSMatt Macy
1731eda14cbcSMatt Macy if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1732eda14cbcSMatt Macy (void) fprintf(stderr, gettext("invalid vdev "
1733eda14cbcSMatt Macy "specification: at least one toplevel vdev must be "
1734eda14cbcSMatt Macy "specified\n"));
1735eda14cbcSMatt Macy goto spec_out;
1736eda14cbcSMatt Macy }
1737eda14cbcSMatt Macy
1738eda14cbcSMatt Macy if (seen_logs && nlogs == 0) {
1739eda14cbcSMatt Macy (void) fprintf(stderr, gettext("invalid vdev specification: "
1740eda14cbcSMatt Macy "log requires at least 1 device\n"));
1741eda14cbcSMatt Macy goto spec_out;
1742eda14cbcSMatt Macy }
1743eda14cbcSMatt Macy
1744eda14cbcSMatt Macy /*
1745eda14cbcSMatt Macy * Finally, create nvroot and add all top-level vdevs to it.
1746eda14cbcSMatt Macy */
1747eda14cbcSMatt Macy verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1748eda14cbcSMatt Macy verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1749eda14cbcSMatt Macy VDEV_TYPE_ROOT) == 0);
1750eda14cbcSMatt Macy verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1751681ce946SMartin Matuska (const nvlist_t **)top, toplevels) == 0);
1752eda14cbcSMatt Macy if (nspares != 0)
1753eda14cbcSMatt Macy verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1754681ce946SMartin Matuska (const nvlist_t **)spares, nspares) == 0);
1755eda14cbcSMatt Macy if (nl2cache != 0)
1756eda14cbcSMatt Macy verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1757681ce946SMartin Matuska (const nvlist_t **)l2cache, nl2cache) == 0);
1758eda14cbcSMatt Macy
1759eda14cbcSMatt Macy spec_out:
1760eda14cbcSMatt Macy for (t = 0; t < toplevels; t++)
1761eda14cbcSMatt Macy nvlist_free(top[t]);
1762eda14cbcSMatt Macy for (t = 0; t < nspares; t++)
1763eda14cbcSMatt Macy nvlist_free(spares[t]);
1764eda14cbcSMatt Macy for (t = 0; t < nl2cache; t++)
1765eda14cbcSMatt Macy nvlist_free(l2cache[t]);
1766eda14cbcSMatt Macy
1767eda14cbcSMatt Macy free(spares);
1768eda14cbcSMatt Macy free(l2cache);
1769eda14cbcSMatt Macy free(top);
1770eda14cbcSMatt Macy
1771eda14cbcSMatt Macy return (nvroot);
1772eda14cbcSMatt Macy }
1773eda14cbcSMatt Macy
1774eda14cbcSMatt Macy nvlist_t *
split_mirror_vdev(zpool_handle_t * zhp,char * newname,nvlist_t * props,splitflags_t flags,int argc,char ** argv)1775eda14cbcSMatt Macy split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1776eda14cbcSMatt Macy splitflags_t flags, int argc, char **argv)
1777eda14cbcSMatt Macy {
1778eda14cbcSMatt Macy nvlist_t *newroot = NULL, **child;
1779eda14cbcSMatt Macy uint_t c, children;
1780eda14cbcSMatt Macy
1781eda14cbcSMatt Macy if (argc > 0) {
1782eda14cbcSMatt Macy if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1783eda14cbcSMatt Macy (void) fprintf(stderr, gettext("Unable to build a "
1784eda14cbcSMatt Macy "pool from the specified devices\n"));
1785eda14cbcSMatt Macy return (NULL);
1786eda14cbcSMatt Macy }
1787eda14cbcSMatt Macy
1788abcdc1b9SMartin Matuska if (!flags.dryrun && make_disks(zhp, newroot, B_FALSE) != 0) {
1789eda14cbcSMatt Macy nvlist_free(newroot);
1790eda14cbcSMatt Macy return (NULL);
1791eda14cbcSMatt Macy }
1792eda14cbcSMatt Macy
1793eda14cbcSMatt Macy /* avoid any tricks in the spec */
1794eda14cbcSMatt Macy verify(nvlist_lookup_nvlist_array(newroot,
1795eda14cbcSMatt Macy ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1796eda14cbcSMatt Macy for (c = 0; c < children; c++) {
17972a58b312SMartin Matuska const char *path;
1798eda14cbcSMatt Macy const char *type;
1799eda14cbcSMatt Macy int min, max;
1800eda14cbcSMatt Macy
1801eda14cbcSMatt Macy verify(nvlist_lookup_string(child[c],
1802eda14cbcSMatt Macy ZPOOL_CONFIG_PATH, &path) == 0);
1803eda14cbcSMatt Macy if ((type = is_grouping(path, &min, &max)) != NULL) {
1804eda14cbcSMatt Macy (void) fprintf(stderr, gettext("Cannot use "
1805eda14cbcSMatt Macy "'%s' as a device for splitting\n"), type);
1806eda14cbcSMatt Macy nvlist_free(newroot);
1807eda14cbcSMatt Macy return (NULL);
1808eda14cbcSMatt Macy }
1809eda14cbcSMatt Macy }
1810eda14cbcSMatt Macy }
1811eda14cbcSMatt Macy
1812eda14cbcSMatt Macy if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1813eda14cbcSMatt Macy nvlist_free(newroot);
1814eda14cbcSMatt Macy return (NULL);
1815eda14cbcSMatt Macy }
1816eda14cbcSMatt Macy
1817eda14cbcSMatt Macy return (newroot);
1818eda14cbcSMatt Macy }
1819eda14cbcSMatt Macy
1820eda14cbcSMatt Macy static int
num_normal_vdevs(nvlist_t * nvroot)1821eda14cbcSMatt Macy num_normal_vdevs(nvlist_t *nvroot)
1822eda14cbcSMatt Macy {
1823eda14cbcSMatt Macy nvlist_t **top;
1824eda14cbcSMatt Macy uint_t t, toplevels, normal = 0;
1825eda14cbcSMatt Macy
1826eda14cbcSMatt Macy verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1827eda14cbcSMatt Macy &top, &toplevels) == 0);
1828eda14cbcSMatt Macy
1829eda14cbcSMatt Macy for (t = 0; t < toplevels; t++) {
1830eda14cbcSMatt Macy uint64_t log = B_FALSE;
1831eda14cbcSMatt Macy
1832eda14cbcSMatt Macy (void) nvlist_lookup_uint64(top[t], ZPOOL_CONFIG_IS_LOG, &log);
1833eda14cbcSMatt Macy if (log)
1834eda14cbcSMatt Macy continue;
1835eda14cbcSMatt Macy if (nvlist_exists(top[t], ZPOOL_CONFIG_ALLOCATION_BIAS))
1836eda14cbcSMatt Macy continue;
1837eda14cbcSMatt Macy
1838eda14cbcSMatt Macy normal++;
1839eda14cbcSMatt Macy }
1840eda14cbcSMatt Macy
1841eda14cbcSMatt Macy return (normal);
1842eda14cbcSMatt Macy }
1843eda14cbcSMatt Macy
1844eda14cbcSMatt Macy /*
1845eda14cbcSMatt Macy * Get and validate the contents of the given vdev specification. This ensures
1846eda14cbcSMatt Macy * that the nvlist returned is well-formed, that all the devices exist, and that
1847eda14cbcSMatt Macy * they are not currently in use by any other known consumer. The 'poolconfig'
1848eda14cbcSMatt Macy * parameter is the current configuration of the pool when adding devices
1849eda14cbcSMatt Macy * existing pool, and is used to perform additional checks, such as changing the
1850eda14cbcSMatt Macy * replication level of the pool. It can be 'NULL' to indicate that this is a
1851eda14cbcSMatt Macy * new pool. The 'force' flag controls whether devices should be forcefully
1852eda14cbcSMatt Macy * added, even if they appear in use.
1853eda14cbcSMatt Macy */
1854eda14cbcSMatt Macy nvlist_t *
make_root_vdev(zpool_handle_t * zhp,nvlist_t * props,int force,int check_rep,boolean_t replacing,boolean_t dryrun,int argc,char ** argv)1855eda14cbcSMatt Macy make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1856eda14cbcSMatt Macy boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1857eda14cbcSMatt Macy {
1858eda14cbcSMatt Macy nvlist_t *newroot;
1859eda14cbcSMatt Macy nvlist_t *poolconfig = NULL;
1860eda14cbcSMatt Macy is_force = force;
1861eda14cbcSMatt Macy
1862eda14cbcSMatt Macy /*
1863eda14cbcSMatt Macy * Construct the vdev specification. If this is successful, we know
1864eda14cbcSMatt Macy * that we have a valid specification, and that all devices can be
1865eda14cbcSMatt Macy * opened.
1866eda14cbcSMatt Macy */
1867eda14cbcSMatt Macy if ((newroot = construct_spec(props, argc, argv)) == NULL)
1868eda14cbcSMatt Macy return (NULL);
1869eda14cbcSMatt Macy
1870eda14cbcSMatt Macy if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
1871eda14cbcSMatt Macy nvlist_free(newroot);
1872eda14cbcSMatt Macy return (NULL);
1873eda14cbcSMatt Macy }
1874eda14cbcSMatt Macy
1875eda14cbcSMatt Macy /*
1876eda14cbcSMatt Macy * Validate each device to make sure that it's not shared with another
1877eda14cbcSMatt Macy * subsystem. We do this even if 'force' is set, because there are some
1878eda14cbcSMatt Macy * uses (such as a dedicated dump device) that even '-f' cannot
1879eda14cbcSMatt Macy * override.
1880eda14cbcSMatt Macy */
1881eda14cbcSMatt Macy if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1882eda14cbcSMatt Macy nvlist_free(newroot);
1883eda14cbcSMatt Macy return (NULL);
1884eda14cbcSMatt Macy }
1885eda14cbcSMatt Macy
1886eda14cbcSMatt Macy /*
1887eda14cbcSMatt Macy * Check the replication level of the given vdevs and report any errors
1888eda14cbcSMatt Macy * found. We include the existing pool spec, if any, as we need to
1889eda14cbcSMatt Macy * catch changes against the existing replication level.
1890eda14cbcSMatt Macy */
1891eda14cbcSMatt Macy if (check_rep && check_replication(poolconfig, newroot) != 0) {
1892eda14cbcSMatt Macy nvlist_free(newroot);
1893eda14cbcSMatt Macy return (NULL);
1894eda14cbcSMatt Macy }
1895eda14cbcSMatt Macy
1896eda14cbcSMatt Macy /*
1897eda14cbcSMatt Macy * On pool create the new vdev spec must have one normal vdev.
1898eda14cbcSMatt Macy */
1899eda14cbcSMatt Macy if (poolconfig == NULL && num_normal_vdevs(newroot) == 0) {
1900eda14cbcSMatt Macy vdev_error(gettext("at least one general top-level vdev must "
1901eda14cbcSMatt Macy "be specified\n"));
1902eda14cbcSMatt Macy nvlist_free(newroot);
1903eda14cbcSMatt Macy return (NULL);
1904eda14cbcSMatt Macy }
1905eda14cbcSMatt Macy
1906eda14cbcSMatt Macy /*
1907eda14cbcSMatt Macy * Run through the vdev specification and label any whole disks found.
1908eda14cbcSMatt Macy */
1909abcdc1b9SMartin Matuska if (!dryrun && make_disks(zhp, newroot, replacing) != 0) {
1910eda14cbcSMatt Macy nvlist_free(newroot);
1911eda14cbcSMatt Macy return (NULL);
1912eda14cbcSMatt Macy }
1913eda14cbcSMatt Macy
1914eda14cbcSMatt Macy return (newroot);
1915eda14cbcSMatt Macy }
1916