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