1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2016, 2017 Intel Corporation.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
27 */
28
29 /*
30 * Functions to convert between a list of vdevs and an nvlist representing the
31 * configuration. Each entry in the list can be one of:
32 *
33 * Device vdevs
34 * disk=(path=..., devid=...)
35 * file=(path=...)
36 *
37 * Group vdevs
38 * raidz[1|2]=(...)
39 * mirror=(...)
40 *
41 * Hot spares
42 *
43 * While the underlying implementation supports it, group vdevs cannot contain
44 * other group vdevs. All userland verification of devices is contained within
45 * this file. If successful, the nvlist returned can be passed directly to the
46 * kernel; we've done as much verification as possible in userland.
47 *
48 * Hot spares are a special case, and passed down as an array of disk vdevs, at
49 * the same level as the root of the vdev tree.
50 *
51 * The only function exported by this file is 'make_root_vdev'. The
52 * function performs several passes:
53 *
54 * 1. Construct the vdev specification. Performs syntax validation and
55 * makes sure each device is valid.
56 * 2. Check for devices in use. Using libblkid to make sure that no
57 * devices are also in use. Some can be overridden using the 'force'
58 * flag, others cannot.
59 * 3. Check for replication errors if the 'force' flag is not specified.
60 * validates that the replication level is consistent across the
61 * entire pool.
62 * 4. Call libzfs to label any whole disks with an EFI label.
63 */
64
65 #include <assert.h>
66 #include <ctype.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <libintl.h>
70 #include <libnvpair.h>
71 #include <libzutil.h>
72 #include <limits.h>
73 #include <sys/spa.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include "zpool_util.h"
78 #include <sys/zfs_context.h>
79 #include <sys/stat.h>
80
81 /*
82 * For any given vdev specification, we can have multiple errors. The
83 * vdev_error() function keeps track of whether we have seen an error yet, and
84 * prints out a header if its the first error we've seen.
85 */
86 boolean_t error_seen;
87 boolean_t is_force;
88
89 void
vdev_error(const char * fmt,...)90 vdev_error(const char *fmt, ...)
91 {
92 va_list ap;
93
94 if (!error_seen) {
95 (void) fprintf(stderr, gettext("invalid vdev specification\n"));
96 if (!is_force)
97 (void) fprintf(stderr, gettext("use '-f' to override "
98 "the following errors:\n"));
99 else
100 (void) fprintf(stderr, gettext("the following errors "
101 "must be manually repaired:\n"));
102 error_seen = B_TRUE;
103 }
104
105 va_start(ap, fmt);
106 (void) vfprintf(stderr, fmt, ap);
107 va_end(ap);
108 }
109
110 /*
111 * Check that a file is valid. All we can do in this case is check that it's
112 * not in use by another pool, and not in use by swap.
113 */
114 int
check_file_generic(const char * file,boolean_t force,boolean_t isspare)115 check_file_generic(const char *file, boolean_t force, boolean_t isspare)
116 {
117 char *name;
118 int fd;
119 int ret = 0;
120 pool_state_t state;
121 boolean_t inuse;
122
123 if ((fd = open(file, O_RDONLY)) < 0)
124 return (0);
125
126 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
127 const char *desc;
128
129 switch (state) {
130 case POOL_STATE_ACTIVE:
131 desc = gettext("active");
132 break;
133
134 case POOL_STATE_EXPORTED:
135 desc = gettext("exported");
136 break;
137
138 case POOL_STATE_POTENTIALLY_ACTIVE:
139 desc = gettext("potentially active");
140 break;
141
142 default:
143 desc = gettext("unknown");
144 break;
145 }
146
147 /*
148 * Allow hot spares to be shared between pools.
149 */
150 if (state == POOL_STATE_SPARE && isspare) {
151 free(name);
152 (void) close(fd);
153 return (0);
154 }
155
156 if (state == POOL_STATE_ACTIVE ||
157 state == POOL_STATE_SPARE || !force) {
158 switch (state) {
159 case POOL_STATE_SPARE:
160 vdev_error(gettext("%s is reserved as a hot "
161 "spare for pool %s\n"), file, name);
162 break;
163 default:
164 vdev_error(gettext("%s is part of %s pool "
165 "'%s'\n"), file, desc, name);
166 break;
167 }
168 ret = -1;
169 }
170
171 free(name);
172 }
173
174 (void) close(fd);
175 return (ret);
176 }
177
178 /*
179 * This may be a shorthand device path or it could be total gibberish.
180 * Check to see if it is a known device available in zfs_vdev_paths.
181 * As part of this check, see if we've been given an entire disk
182 * (minus the slice number).
183 */
184 static int
is_shorthand_path(const char * arg,char * path,size_t path_size,struct stat64 * statbuf,boolean_t * wholedisk)185 is_shorthand_path(const char *arg, char *path, size_t path_size,
186 struct stat64 *statbuf, boolean_t *wholedisk)
187 {
188 int error;
189
190 error = zfs_resolve_shortname(arg, path, path_size);
191 if (error == 0) {
192 *wholedisk = zfs_dev_is_whole_disk(path);
193 if (*wholedisk || (stat64(path, statbuf) == 0))
194 return (0);
195 }
196
197 strlcpy(path, arg, path_size);
198 memset(statbuf, 0, sizeof (*statbuf));
199 *wholedisk = B_FALSE;
200
201 return (error);
202 }
203
204 /*
205 * Determine if the given path is a hot spare within the given configuration.
206 * If no configuration is given we rely solely on the label.
207 */
208 static boolean_t
is_spare(nvlist_t * config,const char * path)209 is_spare(nvlist_t *config, const char *path)
210 {
211 int fd;
212 pool_state_t state;
213 char *name = NULL;
214 nvlist_t *label;
215 uint64_t guid, spareguid;
216 nvlist_t *nvroot;
217 nvlist_t **spares;
218 uint_t i, nspares;
219 boolean_t inuse;
220
221 if (zpool_is_draid_spare(path))
222 return (B_TRUE);
223
224 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
225 return (B_FALSE);
226
227 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
228 !inuse ||
229 state != POOL_STATE_SPARE ||
230 zpool_read_label(fd, &label, NULL) != 0) {
231 free(name);
232 (void) close(fd);
233 return (B_FALSE);
234 }
235 free(name);
236 (void) close(fd);
237
238 if (config == NULL) {
239 nvlist_free(label);
240 return (B_TRUE);
241 }
242
243 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
244 nvlist_free(label);
245
246 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
247 &nvroot) == 0);
248 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
249 &spares, &nspares) == 0) {
250 for (i = 0; i < nspares; i++) {
251 verify(nvlist_lookup_uint64(spares[i],
252 ZPOOL_CONFIG_GUID, &spareguid) == 0);
253 if (spareguid == guid)
254 return (B_TRUE);
255 }
256 }
257
258 return (B_FALSE);
259 }
260
261 /*
262 * Create a leaf vdev. Determine if this is a file or a device. If it's a
263 * device, fill in the device id to make a complete nvlist. Valid forms for a
264 * leaf vdev are:
265 *
266 * /dev/xxx Complete disk path
267 * /xxx Full path to file
268 * xxx Shorthand for <zfs_vdev_paths>/xxx
269 * draid* Virtual dRAID spare
270 */
271 static nvlist_t *
make_leaf_vdev(nvlist_t * props,const char * arg,boolean_t is_primary)272 make_leaf_vdev(nvlist_t *props, const char *arg, boolean_t is_primary)
273 {
274 char path[MAXPATHLEN];
275 struct stat64 statbuf;
276 nvlist_t *vdev = NULL;
277 const char *type = NULL;
278 boolean_t wholedisk = B_FALSE;
279 uint64_t ashift = 0;
280 int err;
281
282 /*
283 * Determine what type of vdev this is, and put the full path into
284 * 'path'. We detect whether this is a device of file afterwards by
285 * checking the st_mode of the file.
286 */
287 if (arg[0] == '/') {
288 /*
289 * Complete device or file path. Exact type is determined by
290 * examining the file descriptor afterwards. Symbolic links
291 * are resolved to their real paths to determine whole disk
292 * and S_ISBLK/S_ISREG type checks. However, we are careful
293 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
294 * can leverage udev's persistent device labels.
295 */
296 if (realpath(arg, path) == NULL) {
297 (void) fprintf(stderr,
298 gettext("cannot resolve path '%s'\n"), arg);
299 return (NULL);
300 }
301
302 wholedisk = zfs_dev_is_whole_disk(path);
303 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
304 (void) fprintf(stderr,
305 gettext("cannot open '%s': %s\n"),
306 path, strerror(errno));
307 return (NULL);
308 }
309
310 /* After whole disk check restore original passed path */
311 strlcpy(path, arg, sizeof (path));
312 } else if (zpool_is_draid_spare(arg)) {
313 if (!is_primary) {
314 (void) fprintf(stderr,
315 gettext("cannot open '%s': dRAID spares can only "
316 "be used to replace primary vdevs\n"), arg);
317 return (NULL);
318 }
319
320 wholedisk = B_TRUE;
321 strlcpy(path, arg, sizeof (path));
322 type = VDEV_TYPE_DRAID_SPARE;
323 } else {
324 err = is_shorthand_path(arg, path, sizeof (path),
325 &statbuf, &wholedisk);
326 if (err != 0) {
327 /*
328 * If we got ENOENT, then the user gave us
329 * gibberish, so try to direct them with a
330 * reasonable error message. Otherwise,
331 * regurgitate strerror() since it's the best we
332 * can do.
333 */
334 if (err == ENOENT) {
335 (void) fprintf(stderr,
336 gettext("cannot open '%s': no such "
337 "device in %s\n"), arg, DISK_ROOT);
338 (void) fprintf(stderr,
339 gettext("must be a full path or "
340 "shorthand device name\n"));
341 return (NULL);
342 } else {
343 (void) fprintf(stderr,
344 gettext("cannot open '%s': %s\n"),
345 path, strerror(errno));
346 return (NULL);
347 }
348 }
349 }
350
351 if (type == NULL) {
352 /*
353 * Determine whether this is a device or a file.
354 */
355 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
356 type = VDEV_TYPE_DISK;
357 } else if (S_ISREG(statbuf.st_mode)) {
358 type = VDEV_TYPE_FILE;
359 } else {
360 fprintf(stderr, gettext("cannot use '%s': must "
361 "be a block device or regular file\n"), path);
362 return (NULL);
363 }
364 }
365
366 /*
367 * Finally, we have the complete device or file, and we know that it is
368 * acceptable to use. Construct the nvlist to describe this vdev. All
369 * vdevs have a 'path' element, and devices also have a 'devid' element.
370 */
371 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
372 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
373 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
374
375 /* Lookup and add the enclosure sysfs path (if exists) */
376 update_vdev_config_dev_sysfs_path(vdev, path,
377 ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH);
378
379 if (strcmp(type, VDEV_TYPE_DISK) == 0)
380 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
381 (uint64_t)wholedisk) == 0);
382
383 /*
384 * Override defaults if custom properties are provided.
385 */
386 if (props != NULL) {
387 const char *value = NULL;
388
389 if (nvlist_lookup_string(props,
390 zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0) {
391 if (zfs_nicestrtonum(NULL, value, &ashift) != 0) {
392 (void) fprintf(stderr,
393 gettext("ashift must be a number.\n"));
394 return (NULL);
395 }
396 if (ashift != 0 &&
397 (ashift < ASHIFT_MIN || ashift > ASHIFT_MAX)) {
398 (void) fprintf(stderr,
399 gettext("invalid 'ashift=%" PRIu64 "' "
400 "property: only values between %" PRId32 " "
401 "and %" PRId32 " are allowed.\n"),
402 ashift, ASHIFT_MIN, ASHIFT_MAX);
403 return (NULL);
404 }
405 }
406 }
407
408 /*
409 * If the device is known to incorrectly report its physical sector
410 * size explicitly provide the known correct value.
411 */
412 if (ashift == 0) {
413 int sector_size;
414
415 if (check_sector_size_database(path, §or_size) == B_TRUE)
416 ashift = highbit64(sector_size) - 1;
417 }
418
419 if (ashift > 0)
420 (void) nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
421
422 return (vdev);
423 }
424
425 /*
426 * Go through and verify the replication level of the pool is consistent.
427 * Performs the following checks:
428 *
429 * For the new spec, verifies that devices in mirrors and raidz are the
430 * same size.
431 *
432 * If the current configuration already has inconsistent replication
433 * levels, ignore any other potential problems in the new spec.
434 *
435 * Otherwise, make sure that the current spec (if there is one) and the new
436 * spec have consistent replication levels.
437 *
438 * If there is no current spec (create), make sure new spec has at least
439 * one general purpose vdev.
440 */
441 typedef struct replication_level {
442 const char *zprl_type;
443 uint64_t zprl_children;
444 uint64_t zprl_parity;
445 } replication_level_t;
446
447 #define ZPOOL_FUZZ (16 * 1024 * 1024)
448
449 /*
450 * N.B. For the purposes of comparing replication levels dRAID can be
451 * considered functionally equivalent to raidz.
452 */
453 static boolean_t
is_raidz_mirror(replication_level_t * a,replication_level_t * b,replication_level_t ** raidz,replication_level_t ** mirror)454 is_raidz_mirror(replication_level_t *a, replication_level_t *b,
455 replication_level_t **raidz, replication_level_t **mirror)
456 {
457 if ((strcmp(a->zprl_type, "raidz") == 0 ||
458 strcmp(a->zprl_type, "draid") == 0) &&
459 strcmp(b->zprl_type, "mirror") == 0) {
460 *raidz = a;
461 *mirror = b;
462 return (B_TRUE);
463 }
464 return (B_FALSE);
465 }
466
467 /*
468 * Comparison for determining if dRAID and raidz where passed in either order.
469 */
470 static boolean_t
is_raidz_draid(replication_level_t * a,replication_level_t * b)471 is_raidz_draid(replication_level_t *a, replication_level_t *b)
472 {
473 if ((strcmp(a->zprl_type, "raidz") == 0 ||
474 strcmp(a->zprl_type, "draid") == 0) &&
475 (strcmp(b->zprl_type, "raidz") == 0 ||
476 strcmp(b->zprl_type, "draid") == 0)) {
477 return (B_TRUE);
478 }
479
480 return (B_FALSE);
481 }
482
483 /*
484 * Given a list of toplevel vdevs, return the current replication level. If
485 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
486 * an error message will be displayed for each self-inconsistent vdev.
487 */
488 static replication_level_t *
get_replication(nvlist_t * nvroot,boolean_t fatal)489 get_replication(nvlist_t *nvroot, boolean_t fatal)
490 {
491 nvlist_t **top;
492 uint_t t, toplevels;
493 nvlist_t **child;
494 uint_t c, children;
495 nvlist_t *nv;
496 const char *type;
497 replication_level_t lastrep = {0};
498 replication_level_t rep;
499 replication_level_t *ret;
500 replication_level_t *raidz, *mirror;
501 boolean_t dontreport;
502
503 ret = safe_malloc(sizeof (replication_level_t));
504
505 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
506 &top, &toplevels) == 0);
507
508 for (t = 0; t < toplevels; t++) {
509 uint64_t is_log = B_FALSE;
510
511 nv = top[t];
512
513 /*
514 * For separate logs we ignore the top level vdev replication
515 * constraints.
516 */
517 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
518 if (is_log)
519 continue;
520
521 /*
522 * Ignore holes introduced by removing aux devices, along
523 * with indirect vdevs introduced by previously removed
524 * vdevs.
525 */
526 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
527 if (strcmp(type, VDEV_TYPE_HOLE) == 0 ||
528 strcmp(type, VDEV_TYPE_INDIRECT) == 0)
529 continue;
530
531 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
532 &child, &children) != 0) {
533 /*
534 * This is a 'file' or 'disk' vdev.
535 */
536 rep.zprl_type = type;
537 rep.zprl_children = 1;
538 rep.zprl_parity = 0;
539 } else {
540 int64_t vdev_size;
541
542 /*
543 * This is a mirror or RAID-Z vdev. Go through and make
544 * sure the contents are all the same (files vs. disks),
545 * keeping track of the number of elements in the
546 * process.
547 *
548 * We also check that the size of each vdev (if it can
549 * be determined) is the same.
550 */
551 rep.zprl_type = type;
552 rep.zprl_children = 0;
553
554 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
555 strcmp(type, VDEV_TYPE_DRAID) == 0) {
556 verify(nvlist_lookup_uint64(nv,
557 ZPOOL_CONFIG_NPARITY,
558 &rep.zprl_parity) == 0);
559 assert(rep.zprl_parity != 0);
560 } else {
561 rep.zprl_parity = 0;
562 }
563
564 /*
565 * The 'dontreport' variable indicates that we've
566 * already reported an error for this spec, so don't
567 * bother doing it again.
568 */
569 type = NULL;
570 dontreport = 0;
571 vdev_size = -1LL;
572 for (c = 0; c < children; c++) {
573 nvlist_t *cnv = child[c];
574 const char *path;
575 struct stat64 statbuf;
576 int64_t size = -1LL;
577 const char *childtype;
578 int fd, err;
579
580 rep.zprl_children++;
581
582 verify(nvlist_lookup_string(cnv,
583 ZPOOL_CONFIG_TYPE, &childtype) == 0);
584
585 /*
586 * If this is a replacing or spare vdev, then
587 * get the real first child of the vdev: do this
588 * in a loop because replacing and spare vdevs
589 * can be nested.
590 */
591 while (strcmp(childtype,
592 VDEV_TYPE_REPLACING) == 0 ||
593 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
594 nvlist_t **rchild;
595 uint_t rchildren;
596
597 verify(nvlist_lookup_nvlist_array(cnv,
598 ZPOOL_CONFIG_CHILDREN, &rchild,
599 &rchildren) == 0);
600 assert(rchildren == 2);
601 cnv = rchild[0];
602
603 verify(nvlist_lookup_string(cnv,
604 ZPOOL_CONFIG_TYPE,
605 &childtype) == 0);
606 }
607
608 verify(nvlist_lookup_string(cnv,
609 ZPOOL_CONFIG_PATH, &path) == 0);
610
611 /*
612 * If we have a raidz/mirror that combines disks
613 * with files, report it as an error.
614 */
615 if (!dontreport && type != NULL &&
616 strcmp(type, childtype) != 0) {
617 if (ret != NULL)
618 free(ret);
619 ret = NULL;
620 if (fatal)
621 vdev_error(gettext(
622 "mismatched replication "
623 "level: %s contains both "
624 "files and devices\n"),
625 rep.zprl_type);
626 else
627 return (NULL);
628 dontreport = B_TRUE;
629 }
630
631 /*
632 * According to stat(2), the value of 'st_size'
633 * is undefined for block devices and character
634 * devices. But there is no effective way to
635 * determine the real size in userland.
636 *
637 * Instead, we'll take advantage of an
638 * implementation detail of spec_size(). If the
639 * device is currently open, then we (should)
640 * return a valid size.
641 *
642 * If we still don't get a valid size (indicated
643 * by a size of 0 or MAXOFFSET_T), then ignore
644 * this device altogether.
645 */
646 if ((fd = open(path, O_RDONLY)) >= 0) {
647 err = fstat64_blk(fd, &statbuf);
648 (void) close(fd);
649 } else {
650 err = stat64(path, &statbuf);
651 }
652
653 if (err != 0 ||
654 statbuf.st_size == 0 ||
655 statbuf.st_size == MAXOFFSET_T)
656 continue;
657
658 size = statbuf.st_size;
659
660 /*
661 * Also make sure that devices and
662 * slices have a consistent size. If
663 * they differ by a significant amount
664 * (~16MB) then report an error.
665 */
666 if (!dontreport &&
667 (vdev_size != -1LL &&
668 (llabs(size - vdev_size) >
669 ZPOOL_FUZZ))) {
670 if (ret != NULL)
671 free(ret);
672 ret = NULL;
673 if (fatal)
674 vdev_error(gettext(
675 "%s contains devices of "
676 "different sizes\n"),
677 rep.zprl_type);
678 else
679 return (NULL);
680 dontreport = B_TRUE;
681 }
682
683 type = childtype;
684 vdev_size = size;
685 }
686 }
687
688 /*
689 * At this point, we have the replication of the last toplevel
690 * vdev in 'rep'. Compare it to 'lastrep' to see if it is
691 * different.
692 */
693 if (lastrep.zprl_type != NULL) {
694 if (is_raidz_mirror(&lastrep, &rep, &raidz, &mirror) ||
695 is_raidz_mirror(&rep, &lastrep, &raidz, &mirror)) {
696 /*
697 * Accepted raidz and mirror when they can
698 * handle the same number of disk failures.
699 */
700 if (raidz->zprl_parity !=
701 mirror->zprl_children - 1) {
702 if (ret != NULL)
703 free(ret);
704 ret = NULL;
705 if (fatal)
706 vdev_error(gettext(
707 "mismatched replication "
708 "level: "
709 "%s and %s vdevs with "
710 "different redundancy, "
711 "%llu vs. %llu (%llu-way) "
712 "are present\n"),
713 raidz->zprl_type,
714 mirror->zprl_type,
715 (u_longlong_t)
716 raidz->zprl_parity,
717 (u_longlong_t)
718 mirror->zprl_children - 1,
719 (u_longlong_t)
720 mirror->zprl_children);
721 else
722 return (NULL);
723 }
724 } else if (is_raidz_draid(&lastrep, &rep)) {
725 /*
726 * Accepted raidz and draid when they can
727 * handle the same number of disk failures.
728 */
729 if (lastrep.zprl_parity != rep.zprl_parity) {
730 if (ret != NULL)
731 free(ret);
732 ret = NULL;
733 if (fatal)
734 vdev_error(gettext(
735 "mismatched replication "
736 "level: %s and %s vdevs "
737 "with different "
738 "redundancy, %llu vs. "
739 "%llu are present\n"),
740 lastrep.zprl_type,
741 rep.zprl_type,
742 (u_longlong_t)
743 lastrep.zprl_parity,
744 (u_longlong_t)
745 rep.zprl_parity);
746 else
747 return (NULL);
748 }
749 } else if (strcmp(lastrep.zprl_type, rep.zprl_type) !=
750 0) {
751 if (ret != NULL)
752 free(ret);
753 ret = NULL;
754 if (fatal)
755 vdev_error(gettext(
756 "mismatched replication level: "
757 "both %s and %s vdevs are "
758 "present\n"),
759 lastrep.zprl_type, rep.zprl_type);
760 else
761 return (NULL);
762 } else if (lastrep.zprl_parity != rep.zprl_parity) {
763 if (ret)
764 free(ret);
765 ret = NULL;
766 if (fatal)
767 vdev_error(gettext(
768 "mismatched replication level: "
769 "both %llu and %llu device parity "
770 "%s vdevs are present\n"),
771 (u_longlong_t)
772 lastrep.zprl_parity,
773 (u_longlong_t)rep.zprl_parity,
774 rep.zprl_type);
775 else
776 return (NULL);
777 } else if (lastrep.zprl_children != rep.zprl_children) {
778 if (ret)
779 free(ret);
780 ret = NULL;
781 if (fatal)
782 vdev_error(gettext(
783 "mismatched replication level: "
784 "both %llu-way and %llu-way %s "
785 "vdevs are present\n"),
786 (u_longlong_t)
787 lastrep.zprl_children,
788 (u_longlong_t)
789 rep.zprl_children,
790 rep.zprl_type);
791 else
792 return (NULL);
793 }
794 }
795 lastrep = rep;
796 }
797
798 if (ret != NULL)
799 *ret = rep;
800
801 return (ret);
802 }
803
804 /*
805 * Check the replication level of the vdev spec against the current pool. Calls
806 * get_replication() to make sure the new spec is self-consistent. If the pool
807 * has a consistent replication level, then we ignore any errors. Otherwise,
808 * report any difference between the two.
809 */
810 static int
check_replication(nvlist_t * config,nvlist_t * newroot)811 check_replication(nvlist_t *config, nvlist_t *newroot)
812 {
813 nvlist_t **child;
814 uint_t children;
815 replication_level_t *current = NULL, *new;
816 replication_level_t *raidz, *mirror;
817 int ret;
818
819 /*
820 * If we have a current pool configuration, check to see if it's
821 * self-consistent. If not, simply return success.
822 */
823 if (config != NULL) {
824 nvlist_t *nvroot;
825
826 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
827 &nvroot) == 0);
828 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
829 return (0);
830 }
831 /*
832 * for spares there may be no children, and therefore no
833 * replication level to check
834 */
835 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
836 &child, &children) != 0) || (children == 0)) {
837 free(current);
838 return (0);
839 }
840
841 /*
842 * If all we have is logs then there's no replication level to check.
843 */
844 if (num_logs(newroot) == children) {
845 free(current);
846 return (0);
847 }
848
849 /*
850 * Get the replication level of the new vdev spec, reporting any
851 * inconsistencies found.
852 */
853 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
854 free(current);
855 return (-1);
856 }
857
858 /*
859 * Check to see if the new vdev spec matches the replication level of
860 * the current pool.
861 */
862 ret = 0;
863 if (current != NULL) {
864 if (is_raidz_mirror(current, new, &raidz, &mirror) ||
865 is_raidz_mirror(new, current, &raidz, &mirror)) {
866 if (raidz->zprl_parity != mirror->zprl_children - 1) {
867 vdev_error(gettext(
868 "mismatched replication level: pool and "
869 "new vdev with different redundancy, %s "
870 "and %s vdevs, %llu vs. %llu (%llu-way)\n"),
871 raidz->zprl_type,
872 mirror->zprl_type,
873 (u_longlong_t)raidz->zprl_parity,
874 (u_longlong_t)mirror->zprl_children - 1,
875 (u_longlong_t)mirror->zprl_children);
876 ret = -1;
877 }
878 } else if (strcmp(current->zprl_type, new->zprl_type) != 0) {
879 vdev_error(gettext(
880 "mismatched replication level: pool uses %s "
881 "and new vdev is %s\n"),
882 current->zprl_type, new->zprl_type);
883 ret = -1;
884 } else if (current->zprl_parity != new->zprl_parity) {
885 vdev_error(gettext(
886 "mismatched replication level: pool uses %llu "
887 "device parity and new vdev uses %llu\n"),
888 (u_longlong_t)current->zprl_parity,
889 (u_longlong_t)new->zprl_parity);
890 ret = -1;
891 } else if (current->zprl_children != new->zprl_children) {
892 vdev_error(gettext(
893 "mismatched replication level: pool uses %llu-way "
894 "%s and new vdev uses %llu-way %s\n"),
895 (u_longlong_t)current->zprl_children,
896 current->zprl_type,
897 (u_longlong_t)new->zprl_children,
898 new->zprl_type);
899 ret = -1;
900 }
901 }
902
903 free(new);
904 if (current != NULL)
905 free(current);
906
907 return (ret);
908 }
909
910 static int
zero_label(const char * path)911 zero_label(const char *path)
912 {
913 const int size = 4096;
914 char buf[size];
915 int err, fd;
916
917 if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
918 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
919 path, strerror(errno));
920 return (-1);
921 }
922
923 memset(buf, 0, size);
924 err = write(fd, buf, size);
925 (void) fdatasync(fd);
926 (void) close(fd);
927
928 if (err == -1) {
929 (void) fprintf(stderr, gettext("cannot zero first %d bytes "
930 "of '%s': %s\n"), size, path, strerror(errno));
931 return (-1);
932 }
933
934 if (err != size) {
935 (void) fprintf(stderr, gettext("could only zero %d/%d bytes "
936 "of '%s'\n"), err, size, path);
937 return (-1);
938 }
939
940 return (0);
941 }
942
943 static void
lines_to_stderr(char * lines[],int lines_cnt)944 lines_to_stderr(char *lines[], int lines_cnt)
945 {
946 int i;
947 for (i = 0; i < lines_cnt; i++) {
948 fprintf(stderr, "%s\n", lines[i]);
949 }
950 }
951
952 /*
953 * Go through and find any whole disks in the vdev specification, labelling them
954 * as appropriate. When constructing the vdev spec, we were unable to open this
955 * device in order to provide a devid. Now that we have labelled the disk and
956 * know that slice 0 is valid, we can construct the devid now.
957 *
958 * If the disk was already labeled with an EFI label, we will have gotten the
959 * devid already (because we were able to open the whole disk). Otherwise, we
960 * need to get the devid after we label the disk.
961 */
962 static int
make_disks(zpool_handle_t * zhp,nvlist_t * nv,boolean_t replacing)963 make_disks(zpool_handle_t *zhp, nvlist_t *nv, boolean_t replacing)
964 {
965 nvlist_t **child;
966 uint_t c, children;
967 const char *type, *path;
968 char devpath[MAXPATHLEN];
969 char udevpath[MAXPATHLEN];
970 uint64_t wholedisk;
971 struct stat64 statbuf;
972 int is_exclusive = 0;
973 int fd;
974 int ret;
975
976 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
977
978 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
979 &child, &children) != 0) {
980
981 if (strcmp(type, VDEV_TYPE_DISK) != 0)
982 return (0);
983
984 /*
985 * We have a disk device. If this is a whole disk write
986 * out the efi partition table, otherwise write zero's to
987 * the first 4k of the partition. This is to ensure that
988 * libblkid will not misidentify the partition due to a
989 * magic value left by the previous filesystem.
990 */
991 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
992 verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
993 &wholedisk));
994
995 if (!wholedisk) {
996 /*
997 * Update device id string for mpath nodes (Linux only)
998 */
999 if (is_mpath_whole_disk(path))
1000 update_vdev_config_dev_strs(nv);
1001
1002 if (!is_spare(NULL, path))
1003 (void) zero_label(path);
1004 return (0);
1005 }
1006
1007 if (realpath(path, devpath) == NULL) {
1008 ret = errno;
1009 (void) fprintf(stderr,
1010 gettext("cannot resolve path '%s'\n"), path);
1011 return (ret);
1012 }
1013
1014 /*
1015 * Remove any previously existing symlink from a udev path to
1016 * the device before labeling the disk. This ensures that
1017 * only newly created links are used. Otherwise there is a
1018 * window between when udev deletes and recreates the link
1019 * during which access attempts will fail with ENOENT.
1020 */
1021 strlcpy(udevpath, path, MAXPATHLEN);
1022 (void) zfs_append_partition(udevpath, MAXPATHLEN);
1023
1024 fd = open(devpath, O_RDWR|O_EXCL);
1025 if (fd == -1) {
1026 if (errno == EBUSY)
1027 is_exclusive = 1;
1028 #ifdef __FreeBSD__
1029 if (errno == EPERM)
1030 is_exclusive = 1;
1031 #endif
1032 } else {
1033 (void) close(fd);
1034 }
1035
1036 /*
1037 * If the partition exists, contains a valid spare label,
1038 * and is opened exclusively there is no need to partition
1039 * it. Hot spares have already been partitioned and are
1040 * held open exclusively by the kernel as a safety measure.
1041 *
1042 * If the provided path is for a /dev/disk/ device its
1043 * symbolic link will be removed, partition table created,
1044 * and then block until udev creates the new link.
1045 */
1046 if (!is_exclusive && !is_spare(NULL, udevpath)) {
1047 char *devnode = strrchr(devpath, '/') + 1;
1048 char **lines = NULL;
1049 int lines_cnt = 0;
1050
1051 ret = strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT));
1052 if (ret == 0) {
1053 ret = lstat64(udevpath, &statbuf);
1054 if (ret == 0 && S_ISLNK(statbuf.st_mode))
1055 (void) unlink(udevpath);
1056 }
1057
1058 /*
1059 * When labeling a pool the raw device node name
1060 * is provided as it appears under /dev/.
1061 *
1062 * Note that 'zhp' will be NULL when we're creating a
1063 * pool.
1064 */
1065 if (zpool_prepare_and_label_disk(g_zfs, zhp, devnode,
1066 nv, zhp == NULL ? "create" :
1067 replacing ? "replace" : "add", &lines,
1068 &lines_cnt) != 0) {
1069 (void) fprintf(stderr,
1070 gettext(
1071 "Error preparing/labeling disk.\n"));
1072 if (lines_cnt > 0) {
1073 (void) fprintf(stderr,
1074 gettext("zfs_prepare_disk output:\n"));
1075 lines_to_stderr(lines, lines_cnt);
1076 }
1077
1078 libzfs_free_str_array(lines, lines_cnt);
1079 return (-1);
1080 }
1081 libzfs_free_str_array(lines, lines_cnt);
1082
1083 /*
1084 * Wait for udev to signal the device is available
1085 * by the provided path.
1086 */
1087 ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
1088 if (ret) {
1089 (void) fprintf(stderr,
1090 gettext("missing link: %s was "
1091 "partitioned but %s is missing\n"),
1092 devnode, udevpath);
1093 return (ret);
1094 }
1095
1096 ret = zero_label(udevpath);
1097 if (ret)
1098 return (ret);
1099 }
1100
1101 /*
1102 * Update the path to refer to the partition. The presence of
1103 * the 'whole_disk' field indicates to the CLI that we should
1104 * chop off the partition number when displaying the device in
1105 * future output.
1106 */
1107 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
1108
1109 /*
1110 * Update device id strings for whole disks (Linux only)
1111 */
1112 update_vdev_config_dev_strs(nv);
1113
1114 return (0);
1115 }
1116
1117 for (c = 0; c < children; c++)
1118 if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1119 return (ret);
1120
1121 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1122 &child, &children) == 0)
1123 for (c = 0; c < children; c++)
1124 if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1125 return (ret);
1126
1127 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1128 &child, &children) == 0)
1129 for (c = 0; c < children; c++)
1130 if ((ret = make_disks(zhp, child[c], replacing)) != 0)
1131 return (ret);
1132
1133 return (0);
1134 }
1135
1136 /*
1137 * Go through and find any devices that are in use. We rely on libdiskmgt for
1138 * the majority of this task.
1139 */
1140 static boolean_t
is_device_in_use(nvlist_t * config,nvlist_t * nv,boolean_t force,boolean_t replacing,boolean_t isspare)1141 is_device_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1142 boolean_t replacing, boolean_t isspare)
1143 {
1144 nvlist_t **child;
1145 uint_t c, children;
1146 const char *type, *path;
1147 int ret = 0;
1148 char buf[MAXPATHLEN];
1149 uint64_t wholedisk = B_FALSE;
1150 boolean_t anyinuse = B_FALSE;
1151
1152 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1153
1154 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1155 &child, &children) != 0) {
1156
1157 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1158 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1159 verify(!nvlist_lookup_uint64(nv,
1160 ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
1161
1162 /*
1163 * As a generic check, we look to see if this is a replace of a
1164 * hot spare within the same pool. If so, we allow it
1165 * regardless of what libblkid or zpool_in_use() says.
1166 */
1167 if (replacing) {
1168 (void) strlcpy(buf, path, sizeof (buf));
1169 if (wholedisk) {
1170 ret = zfs_append_partition(buf, sizeof (buf));
1171 if (ret == -1)
1172 return (-1);
1173 }
1174
1175 if (is_spare(config, buf))
1176 return (B_FALSE);
1177 }
1178
1179 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1180 ret = check_device(path, force, isspare, wholedisk);
1181
1182 else if (strcmp(type, VDEV_TYPE_FILE) == 0)
1183 ret = check_file(path, force, isspare);
1184
1185 return (ret != 0);
1186 }
1187
1188 for (c = 0; c < children; c++)
1189 if (is_device_in_use(config, child[c], force, replacing,
1190 B_FALSE))
1191 anyinuse = B_TRUE;
1192
1193 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1194 &child, &children) == 0)
1195 for (c = 0; c < children; c++)
1196 if (is_device_in_use(config, child[c], force, replacing,
1197 B_TRUE))
1198 anyinuse = B_TRUE;
1199
1200 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1201 &child, &children) == 0)
1202 for (c = 0; c < children; c++)
1203 if (is_device_in_use(config, child[c], force, replacing,
1204 B_FALSE))
1205 anyinuse = B_TRUE;
1206
1207 return (anyinuse);
1208 }
1209
1210 /*
1211 * Returns the parity level extracted from a raidz or draid type.
1212 * If the parity cannot be determined zero is returned.
1213 */
1214 static int
get_parity(const char * type)1215 get_parity(const char *type)
1216 {
1217 long parity = 0;
1218 const char *p;
1219
1220 if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0) {
1221 p = type + strlen(VDEV_TYPE_RAIDZ);
1222
1223 if (*p == '\0') {
1224 /* when unspecified default to single parity */
1225 return (1);
1226 } else if (*p == '0') {
1227 /* no zero prefixes allowed */
1228 return (0);
1229 } else {
1230 /* 0-3, no suffixes allowed */
1231 char *end;
1232 errno = 0;
1233 parity = strtol(p, &end, 10);
1234 if (errno != 0 || *end != '\0' ||
1235 parity < 1 || parity > VDEV_RAIDZ_MAXPARITY) {
1236 return (0);
1237 }
1238 }
1239 } else if (strncmp(type, VDEV_TYPE_DRAID,
1240 strlen(VDEV_TYPE_DRAID)) == 0) {
1241 p = type + strlen(VDEV_TYPE_DRAID);
1242
1243 if (*p == '\0' || *p == ':') {
1244 /* when unspecified default to single parity */
1245 return (1);
1246 } else if (*p == '0') {
1247 /* no zero prefixes allowed */
1248 return (0);
1249 } else {
1250 /* 0-3, allowed suffixes: '\0' or ':' */
1251 char *end;
1252 errno = 0;
1253 parity = strtol(p, &end, 10);
1254 if (errno != 0 ||
1255 parity < 1 || parity > VDEV_DRAID_MAXPARITY ||
1256 (*end != '\0' && *end != ':')) {
1257 return (0);
1258 }
1259 }
1260 }
1261
1262 return ((int)parity);
1263 }
1264
1265 /*
1266 * Assign the minimum and maximum number of devices allowed for
1267 * the specified type. On error NULL is returned, otherwise the
1268 * type prefix is returned (raidz, mirror, etc).
1269 */
1270 static const char *
is_grouping(const char * type,int * mindev,int * maxdev)1271 is_grouping(const char *type, int *mindev, int *maxdev)
1272 {
1273 int nparity;
1274
1275 if (strncmp(type, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1276 strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0) {
1277 nparity = get_parity(type);
1278 if (nparity == 0)
1279 return (NULL);
1280 if (mindev != NULL)
1281 *mindev = nparity + 1;
1282 if (maxdev != NULL)
1283 *maxdev = 255;
1284
1285 if (strncmp(type, VDEV_TYPE_RAIDZ,
1286 strlen(VDEV_TYPE_RAIDZ)) == 0) {
1287 return (VDEV_TYPE_RAIDZ);
1288 } else {
1289 return (VDEV_TYPE_DRAID);
1290 }
1291 }
1292
1293 if (maxdev != NULL)
1294 *maxdev = INT_MAX;
1295
1296 if (strcmp(type, "mirror") == 0) {
1297 if (mindev != NULL)
1298 *mindev = 2;
1299 return (VDEV_TYPE_MIRROR);
1300 }
1301
1302 if (strcmp(type, "spare") == 0) {
1303 if (mindev != NULL)
1304 *mindev = 1;
1305 return (VDEV_TYPE_SPARE);
1306 }
1307
1308 if (strcmp(type, "log") == 0) {
1309 if (mindev != NULL)
1310 *mindev = 1;
1311 return (VDEV_TYPE_LOG);
1312 }
1313
1314 if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0 ||
1315 strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1316 if (mindev != NULL)
1317 *mindev = 1;
1318 return (type);
1319 }
1320
1321 if (strcmp(type, "cache") == 0) {
1322 if (mindev != NULL)
1323 *mindev = 1;
1324 return (VDEV_TYPE_L2CACHE);
1325 }
1326
1327 return (NULL);
1328 }
1329
1330 /*
1331 * Extract the configuration parameters encoded in the dRAID type and
1332 * use them to generate a dRAID configuration. The expected format is:
1333 *
1334 * draid[<parity>][:<data><d|D>][:<children><c|C>][:<spares><s|S>]
1335 *
1336 * The intent is to be able to generate a good configuration when no
1337 * additional information is provided. The only mandatory component
1338 * of the 'type' is the 'draid' prefix. If a value is not provided
1339 * then reasonable defaults are used. The optional components may
1340 * appear in any order but the d/s/c suffix is required.
1341 *
1342 * Valid inputs:
1343 * - data: number of data devices per group (1-255)
1344 * - parity: number of parity blocks per group (1-3)
1345 * - spares: number of distributed spare (0-100)
1346 * - children: total number of devices (1-255)
1347 *
1348 * Examples:
1349 * - zpool create tank draid <devices...>
1350 * - zpool create tank draid2:8d:51c:2s <devices...>
1351 */
1352 static int
draid_config_by_type(nvlist_t * nv,const char * type,uint64_t children)1353 draid_config_by_type(nvlist_t *nv, const char *type, uint64_t children)
1354 {
1355 uint64_t nparity = 1;
1356 uint64_t nspares = 0;
1357 uint64_t ndata = UINT64_MAX;
1358 uint64_t ngroups = 1;
1359 long value;
1360
1361 if (strncmp(type, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) != 0)
1362 return (EINVAL);
1363
1364 nparity = (uint64_t)get_parity(type);
1365 if (nparity == 0 || nparity > VDEV_DRAID_MAXPARITY) {
1366 fprintf(stderr,
1367 gettext("invalid dRAID parity level %llu; must be "
1368 "between 1 and %d\n"), (u_longlong_t)nparity,
1369 VDEV_DRAID_MAXPARITY);
1370 return (EINVAL);
1371 }
1372
1373 char *p = (char *)type;
1374 while ((p = strchr(p, ':')) != NULL) {
1375 char *end;
1376
1377 p = p + 1;
1378 errno = 0;
1379
1380 if (!isdigit(p[0])) {
1381 (void) fprintf(stderr, gettext("invalid dRAID "
1382 "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1383 type);
1384 return (EINVAL);
1385 }
1386
1387 /* Expected non-zero value with c/d/s suffix */
1388 value = strtol(p, &end, 10);
1389 char suffix = tolower(*end);
1390 if (errno != 0 ||
1391 (suffix != 'c' && suffix != 'd' && suffix != 's')) {
1392 (void) fprintf(stderr, gettext("invalid dRAID "
1393 "syntax; expected [:<number><c|d|s>] not '%s'\n"),
1394 type);
1395 return (EINVAL);
1396 }
1397
1398 if (suffix == 'c') {
1399 if ((uint64_t)value != children) {
1400 fprintf(stderr,
1401 gettext("invalid number of dRAID children; "
1402 "%llu required but %llu provided\n"),
1403 (u_longlong_t)value,
1404 (u_longlong_t)children);
1405 return (EINVAL);
1406 }
1407 } else if (suffix == 'd') {
1408 ndata = (uint64_t)value;
1409 } else if (suffix == 's') {
1410 nspares = (uint64_t)value;
1411 } else {
1412 verify(0); /* Unreachable */
1413 }
1414 }
1415
1416 /*
1417 * When a specific number of data disks is not provided limit a
1418 * redundancy group to 8 data disks. This value was selected to
1419 * provide a reasonable tradeoff between capacity and performance.
1420 */
1421 if (ndata == UINT64_MAX) {
1422 if (children > nspares + nparity) {
1423 ndata = MIN(children - nspares - nparity, 8);
1424 } else {
1425 fprintf(stderr, gettext("request number of "
1426 "distributed spares %llu and parity level %llu\n"
1427 "leaves no disks available for data\n"),
1428 (u_longlong_t)nspares, (u_longlong_t)nparity);
1429 return (EINVAL);
1430 }
1431 }
1432
1433 /* Verify the maximum allowed group size is never exceeded. */
1434 if (ndata == 0 || (ndata + nparity > children - nspares)) {
1435 fprintf(stderr, gettext("requested number of dRAID data "
1436 "disks per group %llu is too high,\nat most %llu disks "
1437 "are available for data\n"), (u_longlong_t)ndata,
1438 (u_longlong_t)(children - nspares - nparity));
1439 return (EINVAL);
1440 }
1441
1442 /*
1443 * Verify the requested number of spares can be satisfied.
1444 * An arbitrary limit of 100 distributed spares is applied.
1445 */
1446 if (nspares > 100 || nspares > (children - (ndata + nparity))) {
1447 fprintf(stderr,
1448 gettext("invalid number of dRAID spares %llu; additional "
1449 "disks would be required\n"), (u_longlong_t)nspares);
1450 return (EINVAL);
1451 }
1452
1453 /* Verify the requested number children is sufficient. */
1454 if (children < (ndata + nparity + nspares)) {
1455 fprintf(stderr, gettext("%llu disks were provided, but at "
1456 "least %llu disks are required for this config\n"),
1457 (u_longlong_t)children,
1458 (u_longlong_t)(ndata + nparity + nspares));
1459 }
1460
1461 if (children > VDEV_DRAID_MAX_CHILDREN) {
1462 fprintf(stderr, gettext("%llu disks were provided, but "
1463 "dRAID only supports up to %u disks"),
1464 (u_longlong_t)children, VDEV_DRAID_MAX_CHILDREN);
1465 }
1466
1467 /*
1468 * Calculate the minimum number of groups required to fill a slice.
1469 * This is the LCM of the stripe width (ndata + nparity) and the
1470 * number of data drives (children - nspares).
1471 */
1472 while (ngroups * (ndata + nparity) % (children - nspares) != 0)
1473 ngroups++;
1474
1475 /* Store the basic dRAID configuration. */
1476 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, nparity);
1477 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NDATA, ndata);
1478 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NSPARES, nspares);
1479 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DRAID_NGROUPS, ngroups);
1480
1481 return (0);
1482 }
1483
1484 /*
1485 * Construct a syntactically valid vdev specification,
1486 * and ensure that all devices and files exist and can be opened.
1487 * Note: we don't bother freeing anything in the error paths
1488 * because the program is just going to exit anyway.
1489 */
1490 static nvlist_t *
construct_spec(nvlist_t * props,int argc,char ** argv)1491 construct_spec(nvlist_t *props, int argc, char **argv)
1492 {
1493 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
1494 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
1495 const char *type, *fulltype;
1496 boolean_t is_log, is_special, is_dedup, is_spare;
1497 boolean_t seen_logs;
1498
1499 top = NULL;
1500 toplevels = 0;
1501 spares = NULL;
1502 l2cache = NULL;
1503 nspares = 0;
1504 nlogs = 0;
1505 nl2cache = 0;
1506 is_log = is_special = is_dedup = is_spare = B_FALSE;
1507 seen_logs = B_FALSE;
1508 nvroot = NULL;
1509
1510 while (argc > 0) {
1511 fulltype = argv[0];
1512 nv = NULL;
1513
1514 /*
1515 * If it's a mirror, raidz, or draid the subsequent arguments
1516 * are its leaves -- until we encounter the next mirror,
1517 * raidz or draid.
1518 */
1519 if ((type = is_grouping(fulltype, &mindev, &maxdev)) != NULL) {
1520 nvlist_t **child = NULL;
1521 int c, children = 0;
1522
1523 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1524 if (spares != NULL) {
1525 (void) fprintf(stderr,
1526 gettext("invalid vdev "
1527 "specification: 'spare' can be "
1528 "specified only once\n"));
1529 goto spec_out;
1530 }
1531 is_spare = B_TRUE;
1532 is_log = is_special = is_dedup = B_FALSE;
1533 }
1534
1535 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1536 if (seen_logs) {
1537 (void) fprintf(stderr,
1538 gettext("invalid vdev "
1539 "specification: 'log' can be "
1540 "specified only once\n"));
1541 goto spec_out;
1542 }
1543 seen_logs = B_TRUE;
1544 is_log = B_TRUE;
1545 is_special = is_dedup = is_spare = B_FALSE;
1546 argc--;
1547 argv++;
1548 /*
1549 * A log is not a real grouping device.
1550 * We just set is_log and continue.
1551 */
1552 continue;
1553 }
1554
1555 if (strcmp(type, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1556 is_special = B_TRUE;
1557 is_log = is_dedup = is_spare = B_FALSE;
1558 argc--;
1559 argv++;
1560 continue;
1561 }
1562
1563 if (strcmp(type, VDEV_ALLOC_BIAS_DEDUP) == 0) {
1564 is_dedup = B_TRUE;
1565 is_log = is_special = is_spare = B_FALSE;
1566 argc--;
1567 argv++;
1568 continue;
1569 }
1570
1571 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1572 if (l2cache != NULL) {
1573 (void) fprintf(stderr,
1574 gettext("invalid vdev "
1575 "specification: 'cache' can be "
1576 "specified only once\n"));
1577 goto spec_out;
1578 }
1579 is_log = is_special = B_FALSE;
1580 is_dedup = is_spare = B_FALSE;
1581 }
1582
1583 if (is_log || is_special || is_dedup) {
1584 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1585 (void) fprintf(stderr,
1586 gettext("invalid vdev "
1587 "specification: unsupported '%s' "
1588 "device: %s\n"), is_log ? "log" :
1589 "special", type);
1590 goto spec_out;
1591 }
1592 nlogs++;
1593 }
1594
1595 for (c = 1; c < argc; c++) {
1596 if (is_grouping(argv[c], NULL, NULL) != NULL)
1597 break;
1598
1599 children++;
1600 child = realloc(child,
1601 children * sizeof (nvlist_t *));
1602 if (child == NULL)
1603 zpool_no_memory();
1604 if ((nv = make_leaf_vdev(props, argv[c],
1605 !(is_log || is_special || is_dedup ||
1606 is_spare))) == NULL) {
1607 for (c = 0; c < children - 1; c++)
1608 nvlist_free(child[c]);
1609 free(child);
1610 goto spec_out;
1611 }
1612
1613 child[children - 1] = nv;
1614 }
1615
1616 if (children < mindev) {
1617 (void) fprintf(stderr, gettext("invalid vdev "
1618 "specification: %s requires at least %d "
1619 "devices\n"), argv[0], mindev);
1620 for (c = 0; c < children; c++)
1621 nvlist_free(child[c]);
1622 free(child);
1623 goto spec_out;
1624 }
1625
1626 if (children > maxdev) {
1627 (void) fprintf(stderr, gettext("invalid vdev "
1628 "specification: %s supports no more than "
1629 "%d devices\n"), argv[0], maxdev);
1630 for (c = 0; c < children; c++)
1631 nvlist_free(child[c]);
1632 free(child);
1633 goto spec_out;
1634 }
1635
1636 argc -= c;
1637 argv += c;
1638
1639 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1640 spares = child;
1641 nspares = children;
1642 continue;
1643 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1644 l2cache = child;
1645 nl2cache = children;
1646 continue;
1647 } else {
1648 /* create a top-level vdev with children */
1649 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1650 0) == 0);
1651 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1652 type) == 0);
1653 verify(nvlist_add_uint64(nv,
1654 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1655 if (is_log) {
1656 verify(nvlist_add_string(nv,
1657 ZPOOL_CONFIG_ALLOCATION_BIAS,
1658 VDEV_ALLOC_BIAS_LOG) == 0);
1659 }
1660 if (is_special) {
1661 verify(nvlist_add_string(nv,
1662 ZPOOL_CONFIG_ALLOCATION_BIAS,
1663 VDEV_ALLOC_BIAS_SPECIAL) == 0);
1664 }
1665 if (is_dedup) {
1666 verify(nvlist_add_string(nv,
1667 ZPOOL_CONFIG_ALLOCATION_BIAS,
1668 VDEV_ALLOC_BIAS_DEDUP) == 0);
1669 }
1670 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1671 verify(nvlist_add_uint64(nv,
1672 ZPOOL_CONFIG_NPARITY,
1673 mindev - 1) == 0);
1674 }
1675 if (strcmp(type, VDEV_TYPE_DRAID) == 0) {
1676 if (draid_config_by_type(nv,
1677 fulltype, children) != 0) {
1678 for (c = 0; c < children; c++)
1679 nvlist_free(child[c]);
1680 free(child);
1681 goto spec_out;
1682 }
1683 }
1684 verify(nvlist_add_nvlist_array(nv,
1685 ZPOOL_CONFIG_CHILDREN,
1686 (const nvlist_t **)child, children) == 0);
1687
1688 for (c = 0; c < children; c++)
1689 nvlist_free(child[c]);
1690 free(child);
1691 }
1692 } else {
1693 /*
1694 * We have a device. Pass off to make_leaf_vdev() to
1695 * construct the appropriate nvlist describing the vdev.
1696 */
1697 if ((nv = make_leaf_vdev(props, argv[0], !(is_log ||
1698 is_special || is_dedup || is_spare))) == NULL)
1699 goto spec_out;
1700
1701 verify(nvlist_add_uint64(nv,
1702 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1703 if (is_log) {
1704 verify(nvlist_add_string(nv,
1705 ZPOOL_CONFIG_ALLOCATION_BIAS,
1706 VDEV_ALLOC_BIAS_LOG) == 0);
1707 nlogs++;
1708 }
1709
1710 if (is_special) {
1711 verify(nvlist_add_string(nv,
1712 ZPOOL_CONFIG_ALLOCATION_BIAS,
1713 VDEV_ALLOC_BIAS_SPECIAL) == 0);
1714 }
1715 if (is_dedup) {
1716 verify(nvlist_add_string(nv,
1717 ZPOOL_CONFIG_ALLOCATION_BIAS,
1718 VDEV_ALLOC_BIAS_DEDUP) == 0);
1719 }
1720 argc--;
1721 argv++;
1722 }
1723
1724 toplevels++;
1725 top = realloc(top, toplevels * sizeof (nvlist_t *));
1726 if (top == NULL)
1727 zpool_no_memory();
1728 top[toplevels - 1] = nv;
1729 }
1730
1731 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1732 (void) fprintf(stderr, gettext("invalid vdev "
1733 "specification: at least one toplevel vdev must be "
1734 "specified\n"));
1735 goto spec_out;
1736 }
1737
1738 if (seen_logs && nlogs == 0) {
1739 (void) fprintf(stderr, gettext("invalid vdev specification: "
1740 "log requires at least 1 device\n"));
1741 goto spec_out;
1742 }
1743
1744 /*
1745 * Finally, create nvroot and add all top-level vdevs to it.
1746 */
1747 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1748 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1749 VDEV_TYPE_ROOT) == 0);
1750 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1751 (const nvlist_t **)top, toplevels) == 0);
1752 if (nspares != 0)
1753 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1754 (const nvlist_t **)spares, nspares) == 0);
1755 if (nl2cache != 0)
1756 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1757 (const nvlist_t **)l2cache, nl2cache) == 0);
1758
1759 spec_out:
1760 for (t = 0; t < toplevels; t++)
1761 nvlist_free(top[t]);
1762 for (t = 0; t < nspares; t++)
1763 nvlist_free(spares[t]);
1764 for (t = 0; t < nl2cache; t++)
1765 nvlist_free(l2cache[t]);
1766
1767 free(spares);
1768 free(l2cache);
1769 free(top);
1770
1771 return (nvroot);
1772 }
1773
1774 nvlist_t *
split_mirror_vdev(zpool_handle_t * zhp,char * newname,nvlist_t * props,splitflags_t flags,int argc,char ** argv)1775 split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1776 splitflags_t flags, int argc, char **argv)
1777 {
1778 nvlist_t *newroot = NULL, **child;
1779 uint_t c, children;
1780
1781 if (argc > 0) {
1782 if ((newroot = construct_spec(props, argc, argv)) == NULL) {
1783 (void) fprintf(stderr, gettext("Unable to build a "
1784 "pool from the specified devices\n"));
1785 return (NULL);
1786 }
1787
1788 if (!flags.dryrun && make_disks(zhp, newroot, B_FALSE) != 0) {
1789 nvlist_free(newroot);
1790 return (NULL);
1791 }
1792
1793 /* avoid any tricks in the spec */
1794 verify(nvlist_lookup_nvlist_array(newroot,
1795 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1796 for (c = 0; c < children; c++) {
1797 const char *path;
1798 const char *type;
1799 int min, max;
1800
1801 verify(nvlist_lookup_string(child[c],
1802 ZPOOL_CONFIG_PATH, &path) == 0);
1803 if ((type = is_grouping(path, &min, &max)) != NULL) {
1804 (void) fprintf(stderr, gettext("Cannot use "
1805 "'%s' as a device for splitting\n"), type);
1806 nvlist_free(newroot);
1807 return (NULL);
1808 }
1809 }
1810 }
1811
1812 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1813 nvlist_free(newroot);
1814 return (NULL);
1815 }
1816
1817 return (newroot);
1818 }
1819
1820 static int
num_normal_vdevs(nvlist_t * nvroot)1821 num_normal_vdevs(nvlist_t *nvroot)
1822 {
1823 nvlist_t **top;
1824 uint_t t, toplevels, normal = 0;
1825
1826 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1827 &top, &toplevels) == 0);
1828
1829 for (t = 0; t < toplevels; t++) {
1830 uint64_t log = B_FALSE;
1831
1832 (void) nvlist_lookup_uint64(top[t], ZPOOL_CONFIG_IS_LOG, &log);
1833 if (log)
1834 continue;
1835 if (nvlist_exists(top[t], ZPOOL_CONFIG_ALLOCATION_BIAS))
1836 continue;
1837
1838 normal++;
1839 }
1840
1841 return (normal);
1842 }
1843
1844 /*
1845 * Get and validate the contents of the given vdev specification. This ensures
1846 * that the nvlist returned is well-formed, that all the devices exist, and that
1847 * they are not currently in use by any other known consumer. The 'poolconfig'
1848 * parameter is the current configuration of the pool when adding devices
1849 * existing pool, and is used to perform additional checks, such as changing the
1850 * replication level of the pool. It can be 'NULL' to indicate that this is a
1851 * new pool. The 'force' flag controls whether devices should be forcefully
1852 * added, even if they appear in use.
1853 */
1854 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)1855 make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
1856 boolean_t replacing, boolean_t dryrun, int argc, char **argv)
1857 {
1858 nvlist_t *newroot;
1859 nvlist_t *poolconfig = NULL;
1860 is_force = force;
1861
1862 /*
1863 * Construct the vdev specification. If this is successful, we know
1864 * that we have a valid specification, and that all devices can be
1865 * opened.
1866 */
1867 if ((newroot = construct_spec(props, argc, argv)) == NULL)
1868 return (NULL);
1869
1870 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL)) {
1871 nvlist_free(newroot);
1872 return (NULL);
1873 }
1874
1875 /*
1876 * Validate each device to make sure that it's not shared with another
1877 * subsystem. We do this even if 'force' is set, because there are some
1878 * uses (such as a dedicated dump device) that even '-f' cannot
1879 * override.
1880 */
1881 if (is_device_in_use(poolconfig, newroot, force, replacing, B_FALSE)) {
1882 nvlist_free(newroot);
1883 return (NULL);
1884 }
1885
1886 /*
1887 * Check the replication level of the given vdevs and report any errors
1888 * found. We include the existing pool spec, if any, as we need to
1889 * catch changes against the existing replication level.
1890 */
1891 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1892 nvlist_free(newroot);
1893 return (NULL);
1894 }
1895
1896 /*
1897 * On pool create the new vdev spec must have one normal vdev.
1898 */
1899 if (poolconfig == NULL && num_normal_vdevs(newroot) == 0) {
1900 vdev_error(gettext("at least one general top-level vdev must "
1901 "be specified\n"));
1902 nvlist_free(newroot);
1903 return (NULL);
1904 }
1905
1906 /*
1907 * Run through the vdev specification and label any whole disks found.
1908 */
1909 if (!dryrun && make_disks(zhp, newroot, replacing) != 0) {
1910 nvlist_free(newroot);
1911 return (NULL);
1912 }
1913
1914 return (newroot);
1915 }
1916