]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - cddl/contrib/opensolaris/cmd/zpool/zpool_vdev.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / cddl / contrib / opensolaris / cmd / zpool / zpool_vdev.c
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 http://www.opensolaris.org/os/licensing.
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 /*
28  * Functions to convert between a list of vdevs and an nvlist representing the
29  * configuration.  Each entry in the list can be one of:
30  *
31  *      Device vdevs
32  *              disk=(path=..., devid=...)
33  *              file=(path=...)
34  *
35  *      Group vdevs
36  *              raidz[1|2]=(...)
37  *              mirror=(...)
38  *
39  *      Hot spares
40  *
41  * While the underlying implementation supports it, group vdevs cannot contain
42  * other group vdevs.  All userland verification of devices is contained within
43  * this file.  If successful, the nvlist returned can be passed directly to the
44  * kernel; we've done as much verification as possible in userland.
45  *
46  * Hot spares are a special case, and passed down as an array of disk vdevs, at
47  * the same level as the root of the vdev tree.
48  *
49  * The only function exported by this file is 'make_root_vdev'.  The
50  * function performs several passes:
51  *
52  *      1. Construct the vdev specification.  Performs syntax validation and
53  *         makes sure each device is valid.
54  *      2. Check for devices in use.  Using libdiskmgt, makes sure that no
55  *         devices are also in use.  Some can be overridden using the 'force'
56  *         flag, others cannot.
57  *      3. Check for replication errors if the 'force' flag is not specified.
58  *         validates that the replication level is consistent across the
59  *         entire pool.
60  *      4. Call libzfs to label any whole disks with an EFI label.
61  */
62
63 #include <assert.h>
64 #include <devid.h>
65 #include <errno.h>
66 #include <fcntl.h>
67 #include <libintl.h>
68 #include <libnvpair.h>
69 #include <stdio.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <paths.h>
73 #include <sys/stat.h>
74 #include <sys/disk.h>
75 #include <sys/mntent.h>
76 #include <libgeom.h>
77
78 #include "zpool_util.h"
79
80 /*
81  * For any given vdev specification, we can have multiple errors.  The
82  * vdev_error() function keeps track of whether we have seen an error yet, and
83  * prints out a header if its the first error we've seen.
84  */
85 boolean_t error_seen;
86 boolean_t is_force;
87
88 /*PRINTFLIKE1*/
89 static void
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 static int
115 check_file(const char *file, boolean_t force, boolean_t isspare)
116 {
117         char  *name;
118         int fd;
119         int ret = 0;
120         int err;
121         pool_state_t state;
122         boolean_t inuse;
123
124 #if 0
125         if (dm_inuse_swap(file, &err)) {
126                 if (err)
127                         libdiskmgt_error(err);
128                 else
129                         vdev_error(gettext("%s is currently used by swap. "
130                             "Please see swap(1M).\n"), file);
131                 return (-1);
132         }
133 #endif
134
135         if ((fd = open(file, O_RDONLY)) < 0)
136                 return (0);
137
138         if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
139                 const char *desc;
140
141                 switch (state) {
142                 case POOL_STATE_ACTIVE:
143                         desc = gettext("active");
144                         break;
145
146                 case POOL_STATE_EXPORTED:
147                         desc = gettext("exported");
148                         break;
149
150                 case POOL_STATE_POTENTIALLY_ACTIVE:
151                         desc = gettext("potentially active");
152                         break;
153
154                 default:
155                         desc = gettext("unknown");
156                         break;
157                 }
158
159                 /*
160                  * Allow hot spares to be shared between pools.
161                  */
162                 if (state == POOL_STATE_SPARE && isspare)
163                         return (0);
164
165                 if (state == POOL_STATE_ACTIVE ||
166                     state == POOL_STATE_SPARE || !force) {
167                         switch (state) {
168                         case POOL_STATE_SPARE:
169                                 vdev_error(gettext("%s is reserved as a hot "
170                                     "spare for pool %s\n"), file, name);
171                                 break;
172                         default:
173                                 vdev_error(gettext("%s is part of %s pool "
174                                     "'%s'\n"), file, desc, name);
175                                 break;
176                         }
177                         ret = -1;
178                 }
179
180                 free(name);
181         }
182
183         (void) close(fd);
184         return (ret);
185 }
186
187 static int
188 check_provider(const char *name, boolean_t force, boolean_t isspare)
189 {
190         char path[MAXPATHLEN];
191
192         if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) != 0)
193                 snprintf(path, sizeof(path), "%s%s", _PATH_DEV, name);
194         else
195                 strlcpy(path, name, sizeof(path));
196
197         return (check_file(path, force, isspare));
198 }
199
200 /*
201  * By "whole disk" we mean an entire physical disk (something we can
202  * label, toggle the write cache on, etc.) as opposed to the full
203  * capacity of a pseudo-device such as lofi or did.  We act as if we
204  * are labeling the disk, which should be a pretty good test of whether
205  * it's a viable device or not.  Returns B_TRUE if it is and B_FALSE if
206  * it isn't.
207  */
208 static boolean_t
209 is_whole_disk(const char *name)
210 {
211         int fd;
212
213         fd = g_open(name, 0);
214         if (fd >= 0) {
215                 g_close(fd);
216                 return (B_TRUE);
217         }
218         return (B_FALSE);
219 }
220
221 /*
222  * Create a leaf vdev.  Determine if this is a GEOM provider.
223  * Valid forms for a leaf vdev are:
224  *
225  *      /dev/xxx        Complete path to a GEOM provider
226  *      xxx             Shorthand for /dev/xxx
227  */
228 static nvlist_t *
229 make_leaf_vdev(const char *arg, uint64_t is_log)
230 {
231         char path[MAXPATHLEN];
232         struct stat64 statbuf;
233         nvlist_t *vdev = NULL;
234         char *type = NULL;
235         boolean_t wholedisk = B_FALSE;
236
237         /*
238          * Determine what type of vdev this is, and put the full path into
239          * 'path'.  We detect whether this is a device of file afterwards by
240          * checking the st_mode of the file.
241          */
242         if (arg[0] == '/') {
243                 /*
244                  * Complete device or file path.  Exact type is determined by
245                  * examining the file descriptor afterwards.
246                  */
247                 wholedisk = is_whole_disk(arg);
248                 if (!wholedisk && (stat64(arg, &statbuf) != 0)) {
249                         (void) fprintf(stderr,
250                             gettext("cannot open '%s': %s\n"),
251                             arg, strerror(errno));
252                         return (NULL);
253                 }
254
255                 (void) strlcpy(path, arg, sizeof (path));
256         } else {
257                 /*
258                  * This may be a short path for a device, or it could be total
259                  * gibberish.  Check to see if it's a known device in
260                  * /dev/dsk/.  As part of this check, see if we've been given a
261                  * an entire disk (minus the slice number).
262                  */
263                 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
264                         strlcpy(path, arg, sizeof (path));
265                 else
266                         snprintf(path, sizeof (path), "%s%s", _PATH_DEV, arg);
267                 wholedisk = is_whole_disk(path);
268                 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
269                         /*
270                          * If we got ENOENT, then the user gave us
271                          * gibberish, so try to direct them with a
272                          * reasonable error message.  Otherwise,
273                          * regurgitate strerror() since it's the best we
274                          * can do.
275                          */
276                         if (errno == ENOENT) {
277                                 (void) fprintf(stderr,
278                                     gettext("cannot open '%s': no such "
279                                     "GEOM provider\n"), arg);
280                                 (void) fprintf(stderr,
281                                     gettext("must be a full path or "
282                                     "shorthand device name\n"));
283                                 return (NULL);
284                         } else {
285                                 (void) fprintf(stderr,
286                                     gettext("cannot open '%s': %s\n"),
287                                     path, strerror(errno));
288                                 return (NULL);
289                         }
290                 }
291         }
292
293         /*
294          * Determine whether this is a device or a file.
295          */
296         if (wholedisk) {
297                 type = VDEV_TYPE_DISK;
298         } else if (S_ISREG(statbuf.st_mode)) {
299                 type = VDEV_TYPE_FILE;
300         } else {
301                 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
302                     "GEOM provider or regular file\n"), path);
303                 return (NULL);
304         }
305
306         /*
307          * Finally, we have the complete device or file, and we know that it is
308          * acceptable to use.  Construct the nvlist to describe this vdev.  All
309          * vdevs have a 'path' element, and devices also have a 'devid' element.
310          */
311         verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
312         verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
313         verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
314         verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
315         if (strcmp(type, VDEV_TYPE_DISK) == 0)
316                 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
317                     (uint64_t)B_FALSE) == 0);
318
319         /*
320          * For a whole disk, defer getting its devid until after labeling it.
321          */
322         if (1 || (S_ISBLK(statbuf.st_mode) && !wholedisk)) {
323                 /*
324                  * Get the devid for the device.
325                  */
326                 int fd;
327                 ddi_devid_t devid;
328                 char *minor = NULL, *devid_str = NULL;
329
330                 if ((fd = open(path, O_RDONLY)) < 0) {
331                         (void) fprintf(stderr, gettext("cannot open '%s': "
332                             "%s\n"), path, strerror(errno));
333                         nvlist_free(vdev);
334                         return (NULL);
335                 }
336
337                 if (devid_get(fd, &devid) == 0) {
338                         if (devid_get_minor_name(fd, &minor) == 0 &&
339                             (devid_str = devid_str_encode(devid, minor)) !=
340                             NULL) {
341                                 verify(nvlist_add_string(vdev,
342                                     ZPOOL_CONFIG_DEVID, devid_str) == 0);
343                         }
344                         if (devid_str != NULL)
345                                 devid_str_free(devid_str);
346                         if (minor != NULL)
347                                 devid_str_free(minor);
348                         devid_free(devid);
349                 }
350
351                 (void) close(fd);
352         }
353
354         return (vdev);
355 }
356
357 /*
358  * Go through and verify the replication level of the pool is consistent.
359  * Performs the following checks:
360  *
361  *      For the new spec, verifies that devices in mirrors and raidz are the
362  *      same size.
363  *
364  *      If the current configuration already has inconsistent replication
365  *      levels, ignore any other potential problems in the new spec.
366  *
367  *      Otherwise, make sure that the current spec (if there is one) and the new
368  *      spec have consistent replication levels.
369  */
370 typedef struct replication_level {
371         char *zprl_type;
372         uint64_t zprl_children;
373         uint64_t zprl_parity;
374 } replication_level_t;
375
376 #define ZPOOL_FUZZ      (16 * 1024 * 1024)
377
378 /*
379  * Given a list of toplevel vdevs, return the current replication level.  If
380  * the config is inconsistent, then NULL is returned.  If 'fatal' is set, then
381  * an error message will be displayed for each self-inconsistent vdev.
382  */
383 static replication_level_t *
384 get_replication(nvlist_t *nvroot, boolean_t fatal)
385 {
386         nvlist_t **top;
387         uint_t t, toplevels;
388         nvlist_t **child;
389         uint_t c, children;
390         nvlist_t *nv;
391         char *type;
392         replication_level_t lastrep, rep, *ret;
393         boolean_t dontreport;
394
395         ret = safe_malloc(sizeof (replication_level_t));
396
397         verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
398             &top, &toplevels) == 0);
399
400         lastrep.zprl_type = NULL;
401         for (t = 0; t < toplevels; t++) {
402                 uint64_t is_log = B_FALSE;
403
404                 nv = top[t];
405
406                 /*
407                  * For separate logs we ignore the top level vdev replication
408                  * constraints.
409                  */
410                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
411                 if (is_log)
412                         continue;
413
414                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
415                     &type) == 0);
416                 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
417                     &child, &children) != 0) {
418                         /*
419                          * This is a 'file' or 'disk' vdev.
420                          */
421                         rep.zprl_type = type;
422                         rep.zprl_children = 1;
423                         rep.zprl_parity = 0;
424                 } else {
425                         uint64_t vdev_size;
426
427                         /*
428                          * This is a mirror or RAID-Z vdev.  Go through and make
429                          * sure the contents are all the same (files vs. disks),
430                          * keeping track of the number of elements in the
431                          * process.
432                          *
433                          * We also check that the size of each vdev (if it can
434                          * be determined) is the same.
435                          */
436                         rep.zprl_type = type;
437                         rep.zprl_children = 0;
438
439                         if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
440                                 verify(nvlist_lookup_uint64(nv,
441                                     ZPOOL_CONFIG_NPARITY,
442                                     &rep.zprl_parity) == 0);
443                                 assert(rep.zprl_parity != 0);
444                         } else {
445                                 rep.zprl_parity = 0;
446                         }
447
448                         /*
449                          * The 'dontreport' variable indicates that we've
450                          * already reported an error for this spec, so don't
451                          * bother doing it again.
452                          */
453                         type = NULL;
454                         dontreport = 0;
455                         vdev_size = -1ULL;
456                         for (c = 0; c < children; c++) {
457                                 nvlist_t *cnv = child[c];
458                                 char *path;
459                                 struct stat64 statbuf;
460                                 uint64_t size = -1ULL;
461                                 char *childtype;
462                                 int fd, err;
463
464                                 rep.zprl_children++;
465
466                                 verify(nvlist_lookup_string(cnv,
467                                     ZPOOL_CONFIG_TYPE, &childtype) == 0);
468
469                                 /*
470                                  * If this is a replacing or spare vdev, then
471                                  * get the real first child of the vdev.
472                                  */
473                                 if (strcmp(childtype,
474                                     VDEV_TYPE_REPLACING) == 0 ||
475                                     strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
476                                         nvlist_t **rchild;
477                                         uint_t rchildren;
478
479                                         verify(nvlist_lookup_nvlist_array(cnv,
480                                             ZPOOL_CONFIG_CHILDREN, &rchild,
481                                             &rchildren) == 0);
482                                         assert(rchildren == 2);
483                                         cnv = rchild[0];
484
485                                         verify(nvlist_lookup_string(cnv,
486                                             ZPOOL_CONFIG_TYPE,
487                                             &childtype) == 0);
488                                 }
489
490                                 verify(nvlist_lookup_string(cnv,
491                                     ZPOOL_CONFIG_PATH, &path) == 0);
492
493                                 /*
494                                  * If we have a raidz/mirror that combines disks
495                                  * with files, report it as an error.
496                                  */
497                                 if (!dontreport && type != NULL &&
498                                     strcmp(type, childtype) != 0) {
499                                         if (ret != NULL)
500                                                 free(ret);
501                                         ret = NULL;
502                                         if (fatal)
503                                                 vdev_error(gettext(
504                                                     "mismatched replication "
505                                                     "level: %s contains both "
506                                                     "files and devices\n"),
507                                                     rep.zprl_type);
508                                         else
509                                                 return (NULL);
510                                         dontreport = B_TRUE;
511                                 }
512
513                                 /*
514                                  * According to stat(2), the value of 'st_size'
515                                  * is undefined for block devices and character
516                                  * devices.  But there is no effective way to
517                                  * determine the real size in userland.
518                                  *
519                                  * Instead, we'll take advantage of an
520                                  * implementation detail of spec_size().  If the
521                                  * device is currently open, then we (should)
522                                  * return a valid size.
523                                  *
524                                  * If we still don't get a valid size (indicated
525                                  * by a size of 0 or MAXOFFSET_T), then ignore
526                                  * this device altogether.
527                                  */
528                                 if ((fd = open(path, O_RDONLY)) >= 0) {
529                                         err = fstat64(fd, &statbuf);
530                                         if (err == 0 &&
531                                             S_ISCHR(statbuf.st_mode)) {
532                                                 err = ioctl(fd, DIOCGMEDIASIZE,
533                                                     &statbuf.st_size);
534                                         }
535                                         (void) close(fd);
536                                 } else {
537                                         err = stat64(path, &statbuf);
538                                 }
539                                 if (err != 0 || statbuf.st_size == 0)
540                                         continue;
541
542                                 size = statbuf.st_size;
543
544                                 /*
545                                  * Also make sure that devices and
546                                  * slices have a consistent size.  If
547                                  * they differ by a significant amount
548                                  * (~16MB) then report an error.
549                                  */
550                                 if (!dontreport &&
551                                     (vdev_size != -1ULL &&
552                                     (labs(size - vdev_size) >
553                                     ZPOOL_FUZZ))) {
554                                         if (ret != NULL)
555                                                 free(ret);
556                                         ret = NULL;
557                                         if (fatal)
558                                                 vdev_error(gettext(
559                                                     "%s contains devices of "
560                                                     "different sizes\n"),
561                                                     rep.zprl_type);
562                                         else
563                                                 return (NULL);
564                                         dontreport = B_TRUE;
565                                 }
566
567                                 type = childtype;
568                                 vdev_size = size;
569                         }
570                 }
571
572                 /*
573                  * At this point, we have the replication of the last toplevel
574                  * vdev in 'rep'.  Compare it to 'lastrep' to see if its
575                  * different.
576                  */
577                 if (lastrep.zprl_type != NULL) {
578                         if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
579                                 if (ret != NULL)
580                                         free(ret);
581                                 ret = NULL;
582                                 if (fatal)
583                                         vdev_error(gettext(
584                                             "mismatched replication level: "
585                                             "both %s and %s vdevs are "
586                                             "present\n"),
587                                             lastrep.zprl_type, rep.zprl_type);
588                                 else
589                                         return (NULL);
590                         } else if (lastrep.zprl_parity != rep.zprl_parity) {
591                                 if (ret)
592                                         free(ret);
593                                 ret = NULL;
594                                 if (fatal)
595                                         vdev_error(gettext(
596                                             "mismatched replication level: "
597                                             "both %llu and %llu device parity "
598                                             "%s vdevs are present\n"),
599                                             lastrep.zprl_parity,
600                                             rep.zprl_parity,
601                                             rep.zprl_type);
602                                 else
603                                         return (NULL);
604                         } else if (lastrep.zprl_children != rep.zprl_children) {
605                                 if (ret)
606                                         free(ret);
607                                 ret = NULL;
608                                 if (fatal)
609                                         vdev_error(gettext(
610                                             "mismatched replication level: "
611                                             "both %llu-way and %llu-way %s "
612                                             "vdevs are present\n"),
613                                             lastrep.zprl_children,
614                                             rep.zprl_children,
615                                             rep.zprl_type);
616                                 else
617                                         return (NULL);
618                         }
619                 }
620                 lastrep = rep;
621         }
622
623         if (ret != NULL)
624                 *ret = rep;
625
626         return (ret);
627 }
628
629 /*
630  * Check the replication level of the vdev spec against the current pool.  Calls
631  * get_replication() to make sure the new spec is self-consistent.  If the pool
632  * has a consistent replication level, then we ignore any errors.  Otherwise,
633  * report any difference between the two.
634  */
635 static int
636 check_replication(nvlist_t *config, nvlist_t *newroot)
637 {
638         nvlist_t **child;
639         uint_t  children;
640         replication_level_t *current = NULL, *new;
641         int ret;
642
643         /*
644          * If we have a current pool configuration, check to see if it's
645          * self-consistent.  If not, simply return success.
646          */
647         if (config != NULL) {
648                 nvlist_t *nvroot;
649
650                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
651                     &nvroot) == 0);
652                 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
653                         return (0);
654         }
655         /*
656          * for spares there may be no children, and therefore no
657          * replication level to check
658          */
659         if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
660             &child, &children) != 0) || (children == 0)) {
661                 free(current);
662                 return (0);
663         }
664
665         /*
666          * If all we have is logs then there's no replication level to check.
667          */
668         if (num_logs(newroot) == children) {
669                 free(current);
670                 return (0);
671         }
672
673         /*
674          * Get the replication level of the new vdev spec, reporting any
675          * inconsistencies found.
676          */
677         if ((new = get_replication(newroot, B_TRUE)) == NULL) {
678                 free(current);
679                 return (-1);
680         }
681
682         /*
683          * Check to see if the new vdev spec matches the replication level of
684          * the current pool.
685          */
686         ret = 0;
687         if (current != NULL) {
688                 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
689                         vdev_error(gettext(
690                             "mismatched replication level: pool uses %s "
691                             "and new vdev is %s\n"),
692                             current->zprl_type, new->zprl_type);
693                         ret = -1;
694                 } else if (current->zprl_parity != new->zprl_parity) {
695                         vdev_error(gettext(
696                             "mismatched replication level: pool uses %llu "
697                             "device parity and new vdev uses %llu\n"),
698                             current->zprl_parity, new->zprl_parity);
699                         ret = -1;
700                 } else if (current->zprl_children != new->zprl_children) {
701                         vdev_error(gettext(
702                             "mismatched replication level: pool uses %llu-way "
703                             "%s and new vdev uses %llu-way %s\n"),
704                             current->zprl_children, current->zprl_type,
705                             new->zprl_children, new->zprl_type);
706                         ret = -1;
707                 }
708         }
709
710         free(new);
711         if (current != NULL)
712                 free(current);
713
714         return (ret);
715 }
716
717 /*
718  * Determine if the given path is a hot spare within the given configuration.
719  */
720 static boolean_t
721 is_spare(nvlist_t *config, const char *path)
722 {
723         int fd;
724         pool_state_t state;
725         char *name = NULL;
726         nvlist_t *label;
727         uint64_t guid, spareguid;
728         nvlist_t *nvroot;
729         nvlist_t **spares;
730         uint_t i, nspares;
731         boolean_t inuse;
732
733         if ((fd = open(path, O_RDONLY)) < 0)
734                 return (B_FALSE);
735
736         if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
737             !inuse ||
738             state != POOL_STATE_SPARE ||
739             zpool_read_label(fd, &label) != 0) {
740                 free(name);
741                 (void) close(fd);
742                 return (B_FALSE);
743         }
744         free(name);
745
746         (void) close(fd);
747         verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
748         nvlist_free(label);
749
750         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
751             &nvroot) == 0);
752         if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
753             &spares, &nspares) == 0) {
754                 for (i = 0; i < nspares; i++) {
755                         verify(nvlist_lookup_uint64(spares[i],
756                             ZPOOL_CONFIG_GUID, &spareguid) == 0);
757                         if (spareguid == guid)
758                                 return (B_TRUE);
759                 }
760         }
761
762         return (B_FALSE);
763 }
764
765 /*
766  * Go through and find any devices that are in use.  We rely on libdiskmgt for
767  * the majority of this task.
768  */
769 static int
770 check_in_use(nvlist_t *config, nvlist_t *nv, int force, int isreplacing,
771     int isspare)
772 {
773         nvlist_t **child;
774         uint_t c, children;
775         char *type, *path;
776         int ret;
777         char buf[MAXPATHLEN];
778         uint64_t wholedisk;
779
780         verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
781
782         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
783             &child, &children) != 0) {
784
785                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0);
786
787                 /*
788                  * As a generic check, we look to see if this is a replace of a
789                  * hot spare within the same pool.  If so, we allow it
790                  * regardless of what libdiskmgt or zpool_in_use() says.
791                  */
792                 if (isreplacing) {
793                         (void) strlcpy(buf, path, sizeof (buf));
794                         if (is_spare(config, buf))
795                                 return (0);
796                 }
797
798                 if (strcmp(type, VDEV_TYPE_DISK) == 0)
799                         ret = check_provider(path, force, isspare);
800
801                 if (strcmp(type, VDEV_TYPE_FILE) == 0)
802                         ret = check_file(path, force, isspare);
803
804                 return (ret);
805         }
806
807         for (c = 0; c < children; c++)
808                 if ((ret = check_in_use(config, child[c], force,
809                     isreplacing, B_FALSE)) != 0)
810                         return (ret);
811
812         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
813             &child, &children) == 0)
814                 for (c = 0; c < children; c++)
815                         if ((ret = check_in_use(config, child[c], force,
816                             isreplacing, B_TRUE)) != 0)
817                                 return (ret);
818
819         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
820             &child, &children) == 0)
821                 for (c = 0; c < children; c++)
822                         if ((ret = check_in_use(config, child[c], force,
823                             isreplacing, B_FALSE)) != 0)
824                                 return (ret);
825
826         return (0);
827 }
828
829 static const char *
830 is_grouping(const char *type, int *mindev)
831 {
832         if (strcmp(type, "raidz") == 0 || strcmp(type, "raidz1") == 0) {
833                 if (mindev != NULL)
834                         *mindev = 2;
835                 return (VDEV_TYPE_RAIDZ);
836         }
837
838         if (strcmp(type, "raidz2") == 0) {
839                 if (mindev != NULL)
840                         *mindev = 3;
841                 return (VDEV_TYPE_RAIDZ);
842         }
843
844         if (strcmp(type, "mirror") == 0) {
845                 if (mindev != NULL)
846                         *mindev = 2;
847                 return (VDEV_TYPE_MIRROR);
848         }
849
850         if (strcmp(type, "spare") == 0) {
851                 if (mindev != NULL)
852                         *mindev = 1;
853                 return (VDEV_TYPE_SPARE);
854         }
855
856         if (strcmp(type, "log") == 0) {
857                 if (mindev != NULL)
858                         *mindev = 1;
859                 return (VDEV_TYPE_LOG);
860         }
861
862         if (strcmp(type, "cache") == 0) {
863                 if (mindev != NULL)
864                         *mindev = 1;
865                 return (VDEV_TYPE_L2CACHE);
866         }
867
868         return (NULL);
869 }
870
871 /*
872  * Construct a syntactically valid vdev specification,
873  * and ensure that all devices and files exist and can be opened.
874  * Note: we don't bother freeing anything in the error paths
875  * because the program is just going to exit anyway.
876  */
877 nvlist_t *
878 construct_spec(int argc, char **argv)
879 {
880         nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
881         int t, toplevels, mindev, nspares, nlogs, nl2cache;
882         const char *type;
883         uint64_t is_log;
884         boolean_t seen_logs;
885
886         top = NULL;
887         toplevels = 0;
888         spares = NULL;
889         l2cache = NULL;
890         nspares = 0;
891         nlogs = 0;
892         nl2cache = 0;
893         is_log = B_FALSE;
894         seen_logs = B_FALSE;
895
896         while (argc > 0) {
897                 nv = NULL;
898
899                 /*
900                  * If it's a mirror or raidz, the subsequent arguments are
901                  * its leaves -- until we encounter the next mirror or raidz.
902                  */
903                 if ((type = is_grouping(argv[0], &mindev)) != NULL) {
904                         nvlist_t **child = NULL;
905                         int c, children = 0;
906
907                         if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
908                                 if (spares != NULL) {
909                                         (void) fprintf(stderr,
910                                             gettext("invalid vdev "
911                                             "specification: 'spare' can be "
912                                             "specified only once\n"));
913                                         return (NULL);
914                                 }
915                                 is_log = B_FALSE;
916                         }
917
918                         if (strcmp(type, VDEV_TYPE_LOG) == 0) {
919                                 if (seen_logs) {
920                                         (void) fprintf(stderr,
921                                             gettext("invalid vdev "
922                                             "specification: 'log' can be "
923                                             "specified only once\n"));
924                                         return (NULL);
925                                 }
926                                 seen_logs = B_TRUE;
927                                 is_log = B_TRUE;
928                                 argc--;
929                                 argv++;
930                                 /*
931                                  * A log is not a real grouping device.
932                                  * We just set is_log and continue.
933                                  */
934                                 continue;
935                         }
936
937                         if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
938                                 if (l2cache != NULL) {
939                                         (void) fprintf(stderr,
940                                             gettext("invalid vdev "
941                                             "specification: 'cache' can be "
942                                             "specified only once\n"));
943                                         return (NULL);
944                                 }
945                                 is_log = B_FALSE;
946                         }
947
948                         if (is_log) {
949                                 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
950                                         (void) fprintf(stderr,
951                                             gettext("invalid vdev "
952                                             "specification: unsupported 'log' "
953                                             "device: %s\n"), type);
954                                         return (NULL);
955                                 }
956                                 nlogs++;
957                         }
958
959                         for (c = 1; c < argc; c++) {
960                                 if (is_grouping(argv[c], NULL) != NULL)
961                                         break;
962                                 children++;
963                                 child = realloc(child,
964                                     children * sizeof (nvlist_t *));
965                                 if (child == NULL)
966                                         zpool_no_memory();
967                                 if ((nv = make_leaf_vdev(argv[c], B_FALSE))
968                                     == NULL)
969                                         return (NULL);
970                                 child[children - 1] = nv;
971                         }
972
973                         if (children < mindev) {
974                                 (void) fprintf(stderr, gettext("invalid vdev "
975                                     "specification: %s requires at least %d "
976                                     "devices\n"), argv[0], mindev);
977                                 return (NULL);
978                         }
979
980                         argc -= c;
981                         argv += c;
982
983                         if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
984                                 spares = child;
985                                 nspares = children;
986                                 continue;
987                         } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
988                                 l2cache = child;
989                                 nl2cache = children;
990                                 continue;
991                         } else {
992                                 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
993                                     0) == 0);
994                                 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
995                                     type) == 0);
996                                 verify(nvlist_add_uint64(nv,
997                                     ZPOOL_CONFIG_IS_LOG, is_log) == 0);
998                                 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
999                                         verify(nvlist_add_uint64(nv,
1000                                             ZPOOL_CONFIG_NPARITY,
1001                                             mindev - 1) == 0);
1002                                 }
1003                                 verify(nvlist_add_nvlist_array(nv,
1004                                     ZPOOL_CONFIG_CHILDREN, child,
1005                                     children) == 0);
1006
1007                                 for (c = 0; c < children; c++)
1008                                         nvlist_free(child[c]);
1009                                 free(child);
1010                         }
1011                 } else {
1012                         /*
1013                          * We have a device.  Pass off to make_leaf_vdev() to
1014                          * construct the appropriate nvlist describing the vdev.
1015                          */
1016                         if ((nv = make_leaf_vdev(argv[0], is_log)) == NULL)
1017                                 return (NULL);
1018                         if (is_log)
1019                                 nlogs++;
1020                         argc--;
1021                         argv++;
1022                 }
1023
1024                 toplevels++;
1025                 top = realloc(top, toplevels * sizeof (nvlist_t *));
1026                 if (top == NULL)
1027                         zpool_no_memory();
1028                 top[toplevels - 1] = nv;
1029         }
1030
1031         if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1032                 (void) fprintf(stderr, gettext("invalid vdev "
1033                     "specification: at least one toplevel vdev must be "
1034                     "specified\n"));
1035                 return (NULL);
1036         }
1037
1038         if (seen_logs && nlogs == 0) {
1039                 (void) fprintf(stderr, gettext("invalid vdev specification: "
1040                     "log requires at least 1 device\n"));
1041                 return (NULL);
1042         }
1043
1044         /*
1045          * Finally, create nvroot and add all top-level vdevs to it.
1046          */
1047         verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1048         verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1049             VDEV_TYPE_ROOT) == 0);
1050         verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1051             top, toplevels) == 0);
1052         if (nspares != 0)
1053                 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1054                     spares, nspares) == 0);
1055         if (nl2cache != 0)
1056                 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1057                     l2cache, nl2cache) == 0);
1058
1059         for (t = 0; t < toplevels; t++)
1060                 nvlist_free(top[t]);
1061         for (t = 0; t < nspares; t++)
1062                 nvlist_free(spares[t]);
1063         for (t = 0; t < nl2cache; t++)
1064                 nvlist_free(l2cache[t]);
1065         if (spares)
1066                 free(spares);
1067         if (l2cache)
1068                 free(l2cache);
1069         free(top);
1070
1071         return (nvroot);
1072 }
1073
1074
1075 /*
1076  * Get and validate the contents of the given vdev specification.  This ensures
1077  * that the nvlist returned is well-formed, that all the devices exist, and that
1078  * they are not currently in use by any other known consumer.  The 'poolconfig'
1079  * parameter is the current configuration of the pool when adding devices
1080  * existing pool, and is used to perform additional checks, such as changing the
1081  * replication level of the pool.  It can be 'NULL' to indicate that this is a
1082  * new pool.  The 'force' flag controls whether devices should be forcefully
1083  * added, even if they appear in use.
1084  */
1085 nvlist_t *
1086 make_root_vdev(zpool_handle_t *zhp, int force, int check_rep,
1087     boolean_t isreplacing, boolean_t dryrun, int argc, char **argv)
1088 {
1089         nvlist_t *newroot;
1090         nvlist_t *poolconfig = NULL;
1091         is_force = force;
1092
1093         /*
1094          * Construct the vdev specification.  If this is successful, we know
1095          * that we have a valid specification, and that all devices can be
1096          * opened.
1097          */
1098         if ((newroot = construct_spec(argc, argv)) == NULL)
1099                 return (NULL);
1100
1101         if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1102                 return (NULL);
1103
1104         /*
1105          * Validate each device to make sure that its not shared with another
1106          * subsystem.  We do this even if 'force' is set, because there are some
1107          * uses (such as a dedicated dump device) that even '-f' cannot
1108          * override.
1109          */
1110         if (check_in_use(poolconfig, newroot, force, isreplacing,
1111             B_FALSE) != 0) {
1112                 nvlist_free(newroot);
1113                 return (NULL);
1114         }
1115
1116         /*
1117          * Check the replication level of the given vdevs and report any errors
1118          * found.  We include the existing pool spec, if any, as we need to
1119          * catch changes against the existing replication level.
1120          */
1121         if (check_rep && check_replication(poolconfig, newroot) != 0) {
1122                 nvlist_free(newroot);
1123                 return (NULL);
1124         }
1125
1126         return (newroot);
1127 }