]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zpool/zpool_vdev.c
Some nvlist allocations in hold processing need to use KM_PUSHPAGE.
[mirror_zfs.git] / cmd / zpool / zpool_vdev.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f
BB
26/*
27 * Functions to convert between a list of vdevs and an nvlist representing the
28 * configuration. Each entry in the list can be one of:
29 *
30 * Device vdevs
31 * disk=(path=..., devid=...)
32 * file=(path=...)
33 *
34 * Group vdevs
35 * raidz[1|2]=(...)
36 * mirror=(...)
37 *
38 * Hot spares
39 *
40 * While the underlying implementation supports it, group vdevs cannot contain
41 * other group vdevs. All userland verification of devices is contained within
42 * this file. If successful, the nvlist returned can be passed directly to the
43 * kernel; we've done as much verification as possible in userland.
44 *
45 * Hot spares are a special case, and passed down as an array of disk vdevs, at
46 * the same level as the root of the vdev tree.
47 *
48 * The only function exported by this file is 'make_root_vdev'. The
49 * function performs several passes:
50 *
51 * 1. Construct the vdev specification. Performs syntax validation and
52 * makes sure each device is valid.
d603ed6c 53 * 2. Check for devices in use. Using libblkid to make sure that no
34dc7c2f
BB
54 * devices are also in use. Some can be overridden using the 'force'
55 * flag, others cannot.
56 * 3. Check for replication errors if the 'force' flag is not specified.
57 * validates that the replication level is consistent across the
58 * entire pool.
59 * 4. Call libzfs to label any whole disks with an EFI label.
60 */
61
62#include <assert.h>
d603ed6c 63#include <ctype.h>
34dc7c2f
BB
64#include <devid.h>
65#include <errno.h>
66#include <fcntl.h>
34dc7c2f
BB
67#include <libintl.h>
68#include <libnvpair.h>
45d1cae3 69#include <limits.h>
bff32e09
RY
70#include <scsi/scsi.h>
71#include <scsi/sg.h>
34dc7c2f
BB
72#include <stdio.h>
73#include <string.h>
74#include <unistd.h>
75#include <sys/efi_partition.h>
76#include <sys/stat.h>
77#include <sys/vtoc.h>
78#include <sys/mntent.h>
d603ed6c
BB
79#include <uuid/uuid.h>
80#ifdef HAVE_LIBBLKID
81#include <blkid/blkid.h>
82#else
83#define blkid_cache void *
84#endif /* HAVE_LIBBLKID */
34dc7c2f
BB
85
86#include "zpool_util.h"
bff32e09 87#include <sys/zfs_context.h>
34dc7c2f 88
34dc7c2f
BB
89/*
90 * For any given vdev specification, we can have multiple errors. The
91 * vdev_error() function keeps track of whether we have seen an error yet, and
92 * prints out a header if its the first error we've seen.
93 */
94boolean_t error_seen;
95boolean_t is_force;
96
bff32e09
RY
97typedef struct vdev_disk_db_entry
98{
99 char id[24];
100 int sector_size;
101} vdev_disk_db_entry_t;
102
103/*
104 * Database of block devices that lie about physical sector sizes. The
105 * identification string must be precisely 24 characters to avoid false
106 * negatives
107 */
108static vdev_disk_db_entry_t vdev_disk_database[] = {
3549721c
RY
109 {"ATA C400-MTFDDAC064M", 8192},
110 {"ATA C400-MTFDDAC128M", 8192},
111 {"ATA C400-MTFDDAC256M", 8192},
112 {"ATA C400-MTFDDAC512M", 8192},
bff32e09 113 {"ATA Corsair Force 3 ", 8192},
3549721c 114 {"ATA Corsair Force GS", 8192},
bff32e09 115 {"ATA INTEL SSDSA2CT04", 8192},
3549721c
RY
116 {"ATA INTEL SSDSA2CW04", 8192},
117 {"ATA INTEL SSDSA2CW08", 8192},
118 {"ATA INTEL SSDSA2CW12", 8192},
bff32e09 119 {"ATA INTEL SSDSA2CW16", 8192},
3549721c
RY
120 {"ATA INTEL SSDSA2CW30", 8192},
121 {"ATA INTEL SSDSA2CW60", 8192},
122 {"ATA INTEL SSDSC2BA10", 8192},
123 {"ATA INTEL SSDSC2BA20", 8192},
124 {"ATA INTEL SSDSC2BA40", 8192},
125 {"ATA INTEL SSDSC2BA80", 8192},
126 {"ATA INTEL SSDSC2CT06", 8192},
127 {"ATA INTEL SSDSC2CT12", 8192},
bff32e09 128 {"ATA INTEL SSDSC2CT18", 8192},
3549721c
RY
129 {"ATA INTEL SSDSC2CT24", 8192},
130 {"ATA INTEL SSDSC2CW06", 8192},
bff32e09 131 {"ATA INTEL SSDSC2CW12", 8192},
3549721c
RY
132 {"ATA INTEL SSDSC2CW18", 8192},
133 {"ATA INTEL SSDSC2CW24", 8192},
134 {"ATA INTEL SSDSC2CW48", 8192},
bff32e09 135 {"ATA KINGSTON SH100S3", 8192},
3549721c 136 {"ATA KINGSTON SH103S3", 8192},
bff32e09
RY
137 {"ATA M4-CT064M4SSD2 ", 8192},
138 {"ATA M4-CT128M4SSD2 ", 8192},
139 {"ATA M4-CT256M4SSD2 ", 8192},
140 {"ATA M4-CT512M4SSD2 ", 8192},
141 {"ATA OCZ-AGILITY2 ", 8192},
142 {"ATA OCZ-VERTEX2 3.5 ", 8192},
143 {"ATA OCZ-VERTEX3 ", 8192},
144 {"ATA OCZ-VERTEX3 LT ", 8192},
145 {"ATA OCZ-VERTEX3 MI ", 8192},
3549721c
RY
146 {"ATA OCZ-VERTEX4 ", 8192},
147 {"ATA SAMSUNG MZ7WD120", 8192},
148 {"ATA SAMSUNG MZ7WD240", 8192},
149 {"ATA SAMSUNG MZ7WD480", 8192},
150 {"ATA SAMSUNG MZ7WD960", 8192},
bff32e09
RY
151 {"ATA SAMSUNG SSD 830 ", 8192},
152 {"ATA Samsung SSD 840 ", 8192},
3549721c
RY
153 {"ATA TOSHIBA THNSNH06", 8192},
154 {"ATA TOSHIBA THNSNH12", 8192},
155 {"ATA TOSHIBA THNSNH25", 8192},
156 {"ATA TOSHIBA THNSNH51", 8192},
bff32e09
RY
157 {"ATA INTEL SSDSA2M040", 4096},
158 {"ATA INTEL SSDSA2M080", 4096},
159 {"ATA INTEL SSDSA2M160", 4096},
3549721c
RY
160 {"ATA OCZ CORE_SSD ", 4096},
161 {"ATA SAMSUNG MCCOE32G", 4096},
162 {"ATA SAMSUNG MCCOE64G", 4096},
bff32e09
RY
163 /* Imported from Open Solaris*/
164 {"ATA MARVELL SD88SA02", 4096},
165 /* Advanced format Hard drives */
166 {"ATA Hitachi HDS5C303", 4096},
167 {"ATA SAMSUNG HD204UI ", 4096},
168 {"ATA ST2000DL004 HD20", 4096},
169 {"ATA WDC WD10EARS-00M", 4096},
170 {"ATA WDC WD10EARS-00S", 4096},
171 {"ATA WDC WD10EARS-00Z", 4096},
172 {"ATA WDC WD15EARS-00M", 4096},
173 {"ATA WDC WD15EARS-00S", 4096},
174 {"ATA WDC WD15EARS-00Z", 4096},
175 {"ATA WDC WD20EARS-00M", 4096},
176 {"ATA WDC WD20EARS-00S", 4096},
177 {"ATA WDC WD20EARS-00Z", 4096},
178 /* Virtual disks: Assume zvols with default volblocksize */
179#if 0
180 {"ATA QEMU HARDDISK ", 8192},
181 {"IET VIRTUAL-DISK ", 8192},
182 {"OI COMSTAR ", 8192},
183#endif
184};
185
186static const int vdev_disk_database_size =
187 sizeof (vdev_disk_database) / sizeof (vdev_disk_database[0]);
188
189#define INQ_REPLY_LEN 96
190#define INQ_CMD_LEN 6
191
192static boolean_t
193check_sector_size_database(char *path, int *sector_size)
194{
195 unsigned char inq_buff[INQ_REPLY_LEN];
196 unsigned char sense_buffer[32];
197 unsigned char inq_cmd_blk[INQ_CMD_LEN] =
198 {INQUIRY, 0, 0, 0, INQ_REPLY_LEN, 0};
199 sg_io_hdr_t io_hdr;
200 int error;
201 int fd;
202 int i;
203
204 /* Prepare INQUIRY command */
205 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
206 io_hdr.interface_id = 'S';
207 io_hdr.cmd_len = sizeof(inq_cmd_blk);
208 io_hdr.mx_sb_len = sizeof(sense_buffer);
209 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
210 io_hdr.dxfer_len = INQ_REPLY_LEN;
211 io_hdr.dxferp = inq_buff;
212 io_hdr.cmdp = inq_cmd_blk;
213 io_hdr.sbp = sense_buffer;
214 io_hdr.timeout = 10; /* 10 milliseconds is ample time */
215
216 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
217 return (B_FALSE);
218
219 error = ioctl(fd, SG_IO, (unsigned long) &io_hdr);
220
221 (void) close(fd);
222
223 if (error < 0)
224 return (B_FALSE);
225
226 if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
227 return (B_FALSE);
228
229 for (i = 0; i < vdev_disk_database_size; i++) {
230 if (memcmp(inq_buff + 8, vdev_disk_database[i].id, 24))
231 continue;
232
233 *sector_size = vdev_disk_database[i].sector_size;
234 return (B_TRUE);
235 }
236
237 return (B_FALSE);
238}
239
34dc7c2f
BB
240/*PRINTFLIKE1*/
241static void
242vdev_error(const char *fmt, ...)
243{
244 va_list ap;
245
246 if (!error_seen) {
247 (void) fprintf(stderr, gettext("invalid vdev specification\n"));
248 if (!is_force)
249 (void) fprintf(stderr, gettext("use '-f' to override "
250 "the following errors:\n"));
251 else
252 (void) fprintf(stderr, gettext("the following errors "
253 "must be manually repaired:\n"));
254 error_seen = B_TRUE;
255 }
256
257 va_start(ap, fmt);
258 (void) vfprintf(stderr, fmt, ap);
259 va_end(ap);
260}
261
34dc7c2f
BB
262/*
263 * Check that a file is valid. All we can do in this case is check that it's
264 * not in use by another pool, and not in use by swap.
265 */
266static int
267check_file(const char *file, boolean_t force, boolean_t isspare)
268{
269 char *name;
270 int fd;
271 int ret = 0;
34dc7c2f
BB
272 pool_state_t state;
273 boolean_t inuse;
274
34dc7c2f
BB
275 if ((fd = open(file, O_RDONLY)) < 0)
276 return (0);
277
278 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) == 0 && inuse) {
279 const char *desc;
280
281 switch (state) {
282 case POOL_STATE_ACTIVE:
283 desc = gettext("active");
284 break;
285
286 case POOL_STATE_EXPORTED:
287 desc = gettext("exported");
288 break;
289
290 case POOL_STATE_POTENTIALLY_ACTIVE:
291 desc = gettext("potentially active");
292 break;
293
294 default:
295 desc = gettext("unknown");
296 break;
297 }
298
299 /*
300 * Allow hot spares to be shared between pools.
301 */
302 if (state == POOL_STATE_SPARE && isspare)
303 return (0);
304
305 if (state == POOL_STATE_ACTIVE ||
306 state == POOL_STATE_SPARE || !force) {
307 switch (state) {
308 case POOL_STATE_SPARE:
309 vdev_error(gettext("%s is reserved as a hot "
310 "spare for pool %s\n"), file, name);
311 break;
312 default:
313 vdev_error(gettext("%s is part of %s pool "
314 "'%s'\n"), file, desc, name);
315 break;
316 }
317 ret = -1;
318 }
319
320 free(name);
321 }
322
323 (void) close(fd);
324 return (ret);
325}
326
d603ed6c
BB
327static void
328check_error(int err)
329{
330 (void) fprintf(stderr, gettext("warning: device in use checking "
331 "failed: %s\n"), strerror(err));
332}
333
334static int
335check_slice(const char *path, blkid_cache cache, int force, boolean_t isspare)
336{
d603ed6c
BB
337 int err;
338#ifdef HAVE_LIBBLKID
339 char *value;
d603ed6c 340
d603ed6c
BB
341 /* No valid type detected device is safe to use */
342 value = blkid_get_tag_value(cache, "TYPE", path);
343 if (value == NULL)
344 return (0);
345
346 /*
347 * If libblkid detects a ZFS device, we check the device
348 * using check_file() to see if it's safe. The one safe
349 * case is a spare device shared between multiple pools.
350 */
351 if (strcmp(value, "zfs") == 0) {
352 err = check_file(path, force, isspare);
353 } else {
354 if (force) {
355 err = 0;
356 } else {
357 err = -1;
358 vdev_error(gettext("%s contains a filesystem of "
359 "type '%s'\n"), path, value);
360 }
361 }
362
363 free(value);
364#else
365 err = check_file(path, force, isspare);
366#endif /* HAVE_LIBBLKID */
367
368 return (err);
369}
370
371/*
372 * Validate a whole disk. Iterate over all slices on the disk and make sure
373 * that none is in use by calling check_slice().
374 */
375static int
376check_disk(const char *path, blkid_cache cache, int force,
377 boolean_t isspare, boolean_t iswholedisk)
378{
379 struct dk_gpt *vtoc;
380 char slice_path[MAXPATHLEN];
381 int err = 0;
382 int fd, i;
383
384 /* This is not a wholedisk we only check the given partition */
385 if (!iswholedisk)
386 return check_slice(path, cache, force, isspare);
387
388 /*
389 * When the device is a whole disk try to read the efi partition
390 * label. If this is successful we safely check the all of the
391 * partitions. However, when it fails it may simply be because
392 * the disk is partitioned via the MBR. Since we currently can
393 * not easily decode the MBR return a failure and prompt to the
394 * user to use force option since we cannot check the partitions.
395 */
8128bd89 396 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0) {
d603ed6c
BB
397 check_error(errno);
398 return -1;
399 }
400
401 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
402 (void) close(fd);
403
404 if (force) {
405 return 0;
406 } else {
407 vdev_error(gettext("%s does not contain an EFI "
408 "label but it may contain partition\n"
409 "information in the MBR.\n"), path);
410 return -1;
411 }
412 }
413
414 /*
415 * The primary efi partition label is damaged however the secondary
416 * label at the end of the device is intact. Rather than use this
417 * label we should play it safe and treat this as a non efi device.
418 */
419 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
420 efi_free(vtoc);
421 (void) close(fd);
422
423 if (force) {
424 /* Partitions will no be created using the backup */
425 return 0;
426 } else {
427 vdev_error(gettext("%s contains a corrupt primary "
428 "EFI label.\n"), path);
429 return -1;
430 }
431 }
432
433 for (i = 0; i < vtoc->efi_nparts; i++) {
434
435 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED ||
436 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
437 continue;
438
439 if (strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0)
440 (void) snprintf(slice_path, sizeof (slice_path),
441 "%s%s%d", path, "-part", i+1);
442 else
443 (void) snprintf(slice_path, sizeof (slice_path),
444 "%s%s%d", path, isdigit(path[strlen(path)-1]) ?
445 "p" : "", i+1);
446
447 err = check_slice(slice_path, cache, force, isspare);
448 if (err)
449 break;
450 }
451
452 efi_free(vtoc);
453 (void) close(fd);
454
8128bd89 455 return (err);
d603ed6c
BB
456}
457
458static int
459check_device(const char *path, boolean_t force,
460 boolean_t isspare, boolean_t iswholedisk)
461{
462 static blkid_cache cache = NULL;
463
464#ifdef HAVE_LIBBLKID
465 /*
466 * There is no easy way to add a correct blkid_put_cache() call,
467 * memory will be reclaimed when the command exits.
468 */
469 if (cache == NULL) {
470 int err;
471
472 if ((err = blkid_get_cache(&cache, NULL)) != 0) {
473 check_error(err);
474 return -1;
475 }
476
477 if ((err = blkid_probe_all(cache)) != 0) {
478 blkid_put_cache(cache);
479 check_error(err);
480 return -1;
481 }
482 }
483#endif /* HAVE_LIBBLKID */
484
485 return check_disk(path, cache, force, isspare, iswholedisk);
486}
34dc7c2f
BB
487
488/*
489 * By "whole disk" we mean an entire physical disk (something we can
490 * label, toggle the write cache on, etc.) as opposed to the full
491 * capacity of a pseudo-device such as lofi or did. We act as if we
492 * are labeling the disk, which should be a pretty good test of whether
493 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
494 * it isn't.
495 */
496static boolean_t
d603ed6c 497is_whole_disk(const char *path)
34dc7c2f
BB
498{
499 struct dk_gpt *label;
500 int fd;
34dc7c2f 501
8128bd89 502 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
34dc7c2f
BB
503 return (B_FALSE);
504 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
505 (void) close(fd);
506 return (B_FALSE);
507 }
508 efi_free(label);
509 (void) close(fd);
510 return (B_TRUE);
511}
512
d603ed6c
BB
513/*
514 * This may be a shorthand device path or it could be total gibberish.
eac47204
BB
515 * Check to see if it is a known device available in zfs_vdev_paths.
516 * As part of this check, see if we've been given an entire disk
517 * (minus the slice number).
d603ed6c
BB
518 */
519static int
520is_shorthand_path(const char *arg, char *path,
521 struct stat64 *statbuf, boolean_t *wholedisk)
522{
eac47204
BB
523 int error;
524
525 error = zfs_resolve_shortname(arg, path, MAXPATHLEN);
526 if (error == 0) {
d603ed6c 527 *wholedisk = is_whole_disk(path);
79e7242a 528 if (*wholedisk || (stat64(path, statbuf) == 0))
d603ed6c
BB
529 return (0);
530 }
531
532 strlcpy(path, arg, sizeof(path));
533 memset(statbuf, 0, sizeof(*statbuf));
534 *wholedisk = B_FALSE;
535
eac47204 536 return (error);
d603ed6c
BB
537}
538
8128bd89
BB
539/*
540 * Determine if the given path is a hot spare within the given configuration.
541 * If no configuration is given we rely solely on the label.
542 */
543static boolean_t
544is_spare(nvlist_t *config, const char *path)
545{
546 int fd;
547 pool_state_t state;
548 char *name = NULL;
549 nvlist_t *label;
550 uint64_t guid, spareguid;
551 nvlist_t *nvroot;
552 nvlist_t **spares;
553 uint_t i, nspares;
554 boolean_t inuse;
555
556 if ((fd = open(path, O_RDONLY)) < 0)
557 return (B_FALSE);
558
559 if (zpool_in_use(g_zfs, fd, &state, &name, &inuse) != 0 ||
560 !inuse ||
561 state != POOL_STATE_SPARE ||
562 zpool_read_label(fd, &label) != 0) {
563 free(name);
564 (void) close(fd);
565 return (B_FALSE);
566 }
567 free(name);
568 (void) close(fd);
569
570 if (config == NULL)
571 return (B_TRUE);
572
573 verify(nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) == 0);
574 nvlist_free(label);
575
576 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
577 &nvroot) == 0);
578 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
579 &spares, &nspares) == 0) {
580 for (i = 0; i < nspares; i++) {
581 verify(nvlist_lookup_uint64(spares[i],
582 ZPOOL_CONFIG_GUID, &spareguid) == 0);
583 if (spareguid == guid)
584 return (B_TRUE);
585 }
586 }
587
588 return (B_FALSE);
589}
590
34dc7c2f
BB
591/*
592 * Create a leaf vdev. Determine if this is a file or a device. If it's a
593 * device, fill in the device id to make a complete nvlist. Valid forms for a
594 * leaf vdev are:
595 *
eac47204
BB
596 * /dev/xxx Complete disk path
597 * /xxx Full path to file
598 * xxx Shorthand for <zfs_vdev_paths>/xxx
34dc7c2f
BB
599 */
600static nvlist_t *
df30f566 601make_leaf_vdev(nvlist_t *props, const char *arg, uint64_t is_log)
34dc7c2f
BB
602{
603 char path[MAXPATHLEN];
604 struct stat64 statbuf;
605 nvlist_t *vdev = NULL;
606 char *type = NULL;
607 boolean_t wholedisk = B_FALSE;
bff32e09 608 uint64_t ashift = 0;
d603ed6c 609 int err;
34dc7c2f
BB
610
611 /*
612 * Determine what type of vdev this is, and put the full path into
613 * 'path'. We detect whether this is a device of file afterwards by
614 * checking the st_mode of the file.
615 */
616 if (arg[0] == '/') {
617 /*
618 * Complete device or file path. Exact type is determined by
d603ed6c
BB
619 * examining the file descriptor afterwards. Symbolic links
620 * are resolved to their real paths for the is_whole_disk()
621 * and S_ISBLK/S_ISREG type checks. However, we are careful
622 * to store the given path as ZPOOL_CONFIG_PATH to ensure we
623 * can leverage udev's persistent device labels.
34dc7c2f 624 */
d603ed6c 625 if (realpath(arg, path) == NULL) {
34dc7c2f 626 (void) fprintf(stderr,
d603ed6c 627 gettext("cannot resolve path '%s'\n"), arg);
34dc7c2f
BB
628 return (NULL);
629 }
630
34dc7c2f
BB
631 wholedisk = is_whole_disk(path);
632 if (!wholedisk && (stat64(path, &statbuf) != 0)) {
d603ed6c
BB
633 (void) fprintf(stderr,
634 gettext("cannot open '%s': %s\n"),
635 path, strerror(errno));
636 return (NULL);
637 }
638
639 /* After is_whole_disk() check restore original passed path */
640 strlcpy(path, arg, MAXPATHLEN);
641 } else {
642 err = is_shorthand_path(arg, path, &statbuf, &wholedisk);
643 if (err != 0) {
34dc7c2f
BB
644 /*
645 * If we got ENOENT, then the user gave us
646 * gibberish, so try to direct them with a
647 * reasonable error message. Otherwise,
648 * regurgitate strerror() since it's the best we
649 * can do.
650 */
d603ed6c 651 if (err == ENOENT) {
34dc7c2f
BB
652 (void) fprintf(stderr,
653 gettext("cannot open '%s': no such "
654 "device in %s\n"), arg, DISK_ROOT);
655 (void) fprintf(stderr,
656 gettext("must be a full path or "
657 "shorthand device name\n"));
658 return (NULL);
659 } else {
660 (void) fprintf(stderr,
661 gettext("cannot open '%s': %s\n"),
662 path, strerror(errno));
663 return (NULL);
664 }
665 }
666 }
667
668 /*
669 * Determine whether this is a device or a file.
670 */
671 if (wholedisk || S_ISBLK(statbuf.st_mode)) {
672 type = VDEV_TYPE_DISK;
673 } else if (S_ISREG(statbuf.st_mode)) {
674 type = VDEV_TYPE_FILE;
675 } else {
676 (void) fprintf(stderr, gettext("cannot use '%s': must be a "
677 "block device or regular file\n"), path);
678 return (NULL);
679 }
680
681 /*
682 * Finally, we have the complete device or file, and we know that it is
683 * acceptable to use. Construct the nvlist to describe this vdev. All
684 * vdevs have a 'path' element, and devices also have a 'devid' element.
685 */
686 verify(nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) == 0);
687 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_PATH, path) == 0);
688 verify(nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE, type) == 0);
689 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_LOG, is_log) == 0);
690 if (strcmp(type, VDEV_TYPE_DISK) == 0)
691 verify(nvlist_add_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
692 (uint64_t)wholedisk) == 0);
693
bff32e09
RY
694 /*
695 * Override defaults if custom properties are provided.
696 */
df30f566 697 if (props != NULL) {
df30f566
CK
698 char *value = NULL;
699
700 if (nvlist_lookup_string(props,
701 zpool_prop_to_name(ZPOOL_PROP_ASHIFT), &value) == 0)
702 zfs_nicestrtonum(NULL, value, &ashift);
bff32e09 703 }
df30f566 704
bff32e09
RY
705 /*
706 * If the device is known to incorrectly report its physical sector
707 * size explicitly provide the known correct value.
708 */
709 if (ashift == 0) {
710 int sector_size;
711
712 if (check_sector_size_database(path, &sector_size) == B_TRUE)
713 ashift = highbit(sector_size) - 1;
df30f566
CK
714 }
715
bff32e09
RY
716 if (ashift > 0)
717 nvlist_add_uint64(vdev, ZPOOL_CONFIG_ASHIFT, ashift);
718
34dc7c2f
BB
719 return (vdev);
720}
721
722/*
723 * Go through and verify the replication level of the pool is consistent.
724 * Performs the following checks:
725 *
726 * For the new spec, verifies that devices in mirrors and raidz are the
727 * same size.
728 *
729 * If the current configuration already has inconsistent replication
730 * levels, ignore any other potential problems in the new spec.
731 *
732 * Otherwise, make sure that the current spec (if there is one) and the new
733 * spec have consistent replication levels.
734 */
735typedef struct replication_level {
736 char *zprl_type;
737 uint64_t zprl_children;
738 uint64_t zprl_parity;
739} replication_level_t;
740
741#define ZPOOL_FUZZ (16 * 1024 * 1024)
742
743/*
744 * Given a list of toplevel vdevs, return the current replication level. If
745 * the config is inconsistent, then NULL is returned. If 'fatal' is set, then
746 * an error message will be displayed for each self-inconsistent vdev.
747 */
748static replication_level_t *
749get_replication(nvlist_t *nvroot, boolean_t fatal)
750{
751 nvlist_t **top;
752 uint_t t, toplevels;
753 nvlist_t **child;
754 uint_t c, children;
755 nvlist_t *nv;
756 char *type;
d4ed6673 757 replication_level_t lastrep = { 0 }, rep, *ret;
34dc7c2f
BB
758 boolean_t dontreport;
759
760 ret = safe_malloc(sizeof (replication_level_t));
761
762 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
763 &top, &toplevels) == 0);
764
765 lastrep.zprl_type = NULL;
766 for (t = 0; t < toplevels; t++) {
767 uint64_t is_log = B_FALSE;
768
769 nv = top[t];
770
771 /*
772 * For separate logs we ignore the top level vdev replication
773 * constraints.
774 */
775 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
776 if (is_log)
777 continue;
778
779 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE,
780 &type) == 0);
781 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
782 &child, &children) != 0) {
783 /*
784 * This is a 'file' or 'disk' vdev.
785 */
786 rep.zprl_type = type;
787 rep.zprl_children = 1;
788 rep.zprl_parity = 0;
789 } else {
790 uint64_t vdev_size;
791
792 /*
793 * This is a mirror or RAID-Z vdev. Go through and make
794 * sure the contents are all the same (files vs. disks),
795 * keeping track of the number of elements in the
796 * process.
797 *
798 * We also check that the size of each vdev (if it can
799 * be determined) is the same.
800 */
801 rep.zprl_type = type;
802 rep.zprl_children = 0;
803
804 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
805 verify(nvlist_lookup_uint64(nv,
806 ZPOOL_CONFIG_NPARITY,
807 &rep.zprl_parity) == 0);
808 assert(rep.zprl_parity != 0);
809 } else {
810 rep.zprl_parity = 0;
811 }
812
813 /*
814 * The 'dontreport' variable indicates that we've
815 * already reported an error for this spec, so don't
816 * bother doing it again.
817 */
818 type = NULL;
819 dontreport = 0;
820 vdev_size = -1ULL;
821 for (c = 0; c < children; c++) {
822 nvlist_t *cnv = child[c];
823 char *path;
824 struct stat64 statbuf;
825 uint64_t size = -1ULL;
826 char *childtype;
827 int fd, err;
828
829 rep.zprl_children++;
830
831 verify(nvlist_lookup_string(cnv,
832 ZPOOL_CONFIG_TYPE, &childtype) == 0);
833
834 /*
835 * If this is a replacing or spare vdev, then
836 * get the real first child of the vdev.
837 */
838 if (strcmp(childtype,
839 VDEV_TYPE_REPLACING) == 0 ||
840 strcmp(childtype, VDEV_TYPE_SPARE) == 0) {
841 nvlist_t **rchild;
842 uint_t rchildren;
843
844 verify(nvlist_lookup_nvlist_array(cnv,
845 ZPOOL_CONFIG_CHILDREN, &rchild,
846 &rchildren) == 0);
847 assert(rchildren == 2);
848 cnv = rchild[0];
849
850 verify(nvlist_lookup_string(cnv,
851 ZPOOL_CONFIG_TYPE,
852 &childtype) == 0);
853 }
854
855 verify(nvlist_lookup_string(cnv,
856 ZPOOL_CONFIG_PATH, &path) == 0);
857
858 /*
859 * If we have a raidz/mirror that combines disks
860 * with files, report it as an error.
861 */
862 if (!dontreport && type != NULL &&
863 strcmp(type, childtype) != 0) {
864 if (ret != NULL)
865 free(ret);
866 ret = NULL;
867 if (fatal)
868 vdev_error(gettext(
869 "mismatched replication "
870 "level: %s contains both "
871 "files and devices\n"),
872 rep.zprl_type);
873 else
874 return (NULL);
875 dontreport = B_TRUE;
876 }
877
878 /*
879 * According to stat(2), the value of 'st_size'
880 * is undefined for block devices and character
881 * devices. But there is no effective way to
882 * determine the real size in userland.
883 *
884 * Instead, we'll take advantage of an
885 * implementation detail of spec_size(). If the
886 * device is currently open, then we (should)
887 * return a valid size.
888 *
889 * If we still don't get a valid size (indicated
890 * by a size of 0 or MAXOFFSET_T), then ignore
891 * this device altogether.
892 */
893 if ((fd = open(path, O_RDONLY)) >= 0) {
894 err = fstat64(fd, &statbuf);
895 (void) close(fd);
896 } else {
897 err = stat64(path, &statbuf);
898 }
899
900 if (err != 0 ||
901 statbuf.st_size == 0 ||
902 statbuf.st_size == MAXOFFSET_T)
903 continue;
904
905 size = statbuf.st_size;
906
907 /*
908 * Also make sure that devices and
909 * slices have a consistent size. If
910 * they differ by a significant amount
911 * (~16MB) then report an error.
912 */
913 if (!dontreport &&
914 (vdev_size != -1ULL &&
915 (labs(size - vdev_size) >
916 ZPOOL_FUZZ))) {
917 if (ret != NULL)
918 free(ret);
919 ret = NULL;
920 if (fatal)
921 vdev_error(gettext(
922 "%s contains devices of "
923 "different sizes\n"),
924 rep.zprl_type);
925 else
926 return (NULL);
927 dontreport = B_TRUE;
928 }
929
930 type = childtype;
931 vdev_size = size;
932 }
933 }
934
935 /*
936 * At this point, we have the replication of the last toplevel
937 * vdev in 'rep'. Compare it to 'lastrep' to see if its
938 * different.
939 */
940 if (lastrep.zprl_type != NULL) {
941 if (strcmp(lastrep.zprl_type, rep.zprl_type) != 0) {
942 if (ret != NULL)
943 free(ret);
944 ret = NULL;
945 if (fatal)
946 vdev_error(gettext(
947 "mismatched replication level: "
948 "both %s and %s vdevs are "
949 "present\n"),
950 lastrep.zprl_type, rep.zprl_type);
951 else
952 return (NULL);
953 } else if (lastrep.zprl_parity != rep.zprl_parity) {
954 if (ret)
955 free(ret);
956 ret = NULL;
957 if (fatal)
958 vdev_error(gettext(
959 "mismatched replication level: "
960 "both %llu and %llu device parity "
961 "%s vdevs are present\n"),
962 lastrep.zprl_parity,
963 rep.zprl_parity,
964 rep.zprl_type);
965 else
966 return (NULL);
967 } else if (lastrep.zprl_children != rep.zprl_children) {
968 if (ret)
969 free(ret);
970 ret = NULL;
971 if (fatal)
972 vdev_error(gettext(
973 "mismatched replication level: "
974 "both %llu-way and %llu-way %s "
975 "vdevs are present\n"),
976 lastrep.zprl_children,
977 rep.zprl_children,
978 rep.zprl_type);
979 else
980 return (NULL);
981 }
982 }
983 lastrep = rep;
984 }
985
986 if (ret != NULL)
987 *ret = rep;
988
989 return (ret);
990}
991
992/*
993 * Check the replication level of the vdev spec against the current pool. Calls
994 * get_replication() to make sure the new spec is self-consistent. If the pool
995 * has a consistent replication level, then we ignore any errors. Otherwise,
996 * report any difference between the two.
997 */
998static int
999check_replication(nvlist_t *config, nvlist_t *newroot)
1000{
1001 nvlist_t **child;
1002 uint_t children;
1003 replication_level_t *current = NULL, *new;
1004 int ret;
1005
1006 /*
1007 * If we have a current pool configuration, check to see if it's
1008 * self-consistent. If not, simply return success.
1009 */
1010 if (config != NULL) {
1011 nvlist_t *nvroot;
1012
1013 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1014 &nvroot) == 0);
1015 if ((current = get_replication(nvroot, B_FALSE)) == NULL)
1016 return (0);
1017 }
1018 /*
1019 * for spares there may be no children, and therefore no
1020 * replication level to check
1021 */
1022 if ((nvlist_lookup_nvlist_array(newroot, ZPOOL_CONFIG_CHILDREN,
1023 &child, &children) != 0) || (children == 0)) {
1024 free(current);
1025 return (0);
1026 }
1027
1028 /*
1029 * If all we have is logs then there's no replication level to check.
1030 */
1031 if (num_logs(newroot) == children) {
1032 free(current);
1033 return (0);
1034 }
1035
1036 /*
1037 * Get the replication level of the new vdev spec, reporting any
1038 * inconsistencies found.
1039 */
1040 if ((new = get_replication(newroot, B_TRUE)) == NULL) {
1041 free(current);
1042 return (-1);
1043 }
1044
1045 /*
1046 * Check to see if the new vdev spec matches the replication level of
1047 * the current pool.
1048 */
1049 ret = 0;
1050 if (current != NULL) {
1051 if (strcmp(current->zprl_type, new->zprl_type) != 0) {
1052 vdev_error(gettext(
1053 "mismatched replication level: pool uses %s "
1054 "and new vdev is %s\n"),
1055 current->zprl_type, new->zprl_type);
1056 ret = -1;
1057 } else if (current->zprl_parity != new->zprl_parity) {
1058 vdev_error(gettext(
1059 "mismatched replication level: pool uses %llu "
1060 "device parity and new vdev uses %llu\n"),
1061 current->zprl_parity, new->zprl_parity);
1062 ret = -1;
1063 } else if (current->zprl_children != new->zprl_children) {
1064 vdev_error(gettext(
1065 "mismatched replication level: pool uses %llu-way "
1066 "%s and new vdev uses %llu-way %s\n"),
1067 current->zprl_children, current->zprl_type,
1068 new->zprl_children, new->zprl_type);
1069 ret = -1;
1070 }
1071 }
1072
1073 free(new);
1074 if (current != NULL)
1075 free(current);
1076
1077 return (ret);
1078}
1079
d603ed6c
BB
1080static int
1081zero_label(char *path)
1082{
1083 const int size = 4096;
1084 char buf[size];
1085 int err, fd;
1086
1087 if ((fd = open(path, O_WRONLY|O_EXCL)) < 0) {
1088 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
1089 path, strerror(errno));
1090 return (-1);
1091 }
1092
1093 memset(buf, 0, size);
1094 err = write(fd, buf, size);
1095 (void) fdatasync(fd);
1096 (void) close(fd);
1097
1098 if (err == -1) {
1099 (void) fprintf(stderr, gettext("cannot zero first %d bytes "
1100 "of '%s': %s\n"), size, path, strerror(errno));
1101 return (-1);
1102 }
1103
1104 if (err != size) {
1105 (void) fprintf(stderr, gettext("could only zero %d/%d bytes "
1106 "of '%s'\n"), err, size, path);
1107 return (-1);
1108 }
1109
1110 return 0;
1111}
1112
34dc7c2f
BB
1113/*
1114 * Go through and find any whole disks in the vdev specification, labelling them
1115 * as appropriate. When constructing the vdev spec, we were unable to open this
1116 * device in order to provide a devid. Now that we have labelled the disk and
1117 * know that slice 0 is valid, we can construct the devid now.
1118 *
1119 * If the disk was already labeled with an EFI label, we will have gotten the
1120 * devid already (because we were able to open the whole disk). Otherwise, we
1121 * need to get the devid after we label the disk.
1122 */
1123static int
1124make_disks(zpool_handle_t *zhp, nvlist_t *nv)
1125{
1126 nvlist_t **child;
1127 uint_t c, children;
8128bd89 1128 char *type, *path;
d877ac6b
NB
1129 char devpath[MAXPATHLEN];
1130 char udevpath[MAXPATHLEN];
34dc7c2f 1131 uint64_t wholedisk;
d877ac6b 1132 struct stat64 statbuf;
8128bd89
BB
1133 int is_exclusive = 0;
1134 int fd;
34dc7c2f 1135 int ret;
34dc7c2f
BB
1136
1137 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1138
1139 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1140 &child, &children) != 0) {
1141
1142 if (strcmp(type, VDEV_TYPE_DISK) != 0)
1143 return (0);
1144
1145 /*
d603ed6c
BB
1146 * We have a disk device. If this is a whole disk write
1147 * out the efi partition table, otherwise write zero's to
1148 * the first 4k of the partition. This is to ensure that
1149 * libblkid will not misidentify the partition due to a
1150 * magic value left by the previous filesystem.
34dc7c2f 1151 */
d603ed6c
BB
1152 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1153 verify(!nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1154 &wholedisk));
1155
1156 if (!wholedisk) {
8128bd89
BB
1157 (void) zero_label(path);
1158 return (0);
d603ed6c
BB
1159 }
1160
d877ac6b 1161 if (realpath(path, devpath) == NULL) {
d603ed6c
BB
1162 ret = errno;
1163 (void) fprintf(stderr,
1164 gettext("cannot resolve path '%s'\n"), path);
1165 return (ret);
1166 }
34dc7c2f 1167
d877ac6b
NB
1168 /*
1169 * Remove any previously existing symlink from a udev path to
1170 * the device before labeling the disk. This makes
1171 * zpool_label_disk_wait() truly wait for the new link to show
1172 * up instead of returning if it finds an old link still in
1173 * place. Otherwise there is a window between when udev
1174 * deletes and recreates the link during which access attempts
1175 * will fail with ENOENT.
1176 */
eac47204
BB
1177 strncpy(udevpath, path, MAXPATHLEN);
1178 (void) zfs_append_partition(udevpath, MAXPATHLEN);
1179
8128bd89
BB
1180 fd = open(devpath, O_RDWR|O_EXCL);
1181 if (fd == -1) {
1182 if (errno == EBUSY)
1183 is_exclusive = 1;
1184 } else {
1185 (void) close(fd);
1186 }
34dc7c2f
BB
1187
1188 /*
8128bd89
BB
1189 * If the partition exists, contains a valid spare label,
1190 * and is opened exclusively there is no need to partition
1191 * it. Hot spares have already been partitioned and are
1192 * held open exclusively by the kernel as a safety measure.
1193 *
1194 * If the provided path is for a /dev/disk/ device its
1195 * symbolic link will be removed, partition table created,
1196 * and then block until udev creates the new link.
34dc7c2f 1197 */
8128bd89
BB
1198 if (!is_exclusive || !is_spare(NULL, udevpath)) {
1199 ret = strncmp(udevpath,UDISK_ROOT,strlen(UDISK_ROOT));
1200 if (ret == 0) {
1201 ret = lstat64(udevpath, &statbuf);
1202 if (ret == 0 && S_ISLNK(statbuf.st_mode))
1203 (void) unlink(udevpath);
1204 }
1205
1206 if (zpool_label_disk(g_zfs, zhp,
1207 strrchr(devpath, '/') + 1) == -1)
1208 return (-1);
1209
11cb9d77 1210 ret = zpool_label_disk_wait(udevpath, DISK_LABEL_WAIT);
8128bd89
BB
1211 if (ret) {
1212 (void) fprintf(stderr, gettext("cannot "
1213 "resolve path '%s': %d\n"), udevpath, ret);
1214 return (-1);
1215 }
1216
1217 (void) zero_label(udevpath);
34dc7c2f
BB
1218 }
1219
34dc7c2f 1220 /*
eac47204 1221 * Update the path to refer to the partition. The presence of
34dc7c2f 1222 * the 'whole_disk' field indicates to the CLI that we should
eac47204 1223 * chop off the partition number when displaying the device in
34dc7c2f
BB
1224 * future output.
1225 */
d877ac6b 1226 verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);
34dc7c2f 1227
34dc7c2f
BB
1228 return (0);
1229 }
1230
1231 for (c = 0; c < children; c++)
1232 if ((ret = make_disks(zhp, child[c])) != 0)
1233 return (ret);
1234
1235 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1236 &child, &children) == 0)
1237 for (c = 0; c < children; c++)
1238 if ((ret = make_disks(zhp, child[c])) != 0)
1239 return (ret);
1240
1241 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1242 &child, &children) == 0)
1243 for (c = 0; c < children; c++)
1244 if ((ret = make_disks(zhp, child[c])) != 0)
1245 return (ret);
1246
1247 return (0);
1248}
1249
34dc7c2f
BB
1250/*
1251 * Go through and find any devices that are in use. We rely on libdiskmgt for
1252 * the majority of this task.
1253 */
1254static int
428870ff
BB
1255check_in_use(nvlist_t *config, nvlist_t *nv, boolean_t force,
1256 boolean_t replacing, boolean_t isspare)
34dc7c2f
BB
1257{
1258 nvlist_t **child;
1259 uint_t c, children;
1260 char *type, *path;
d603ed6c 1261 int ret = 0;
34dc7c2f 1262 char buf[MAXPATHLEN];
d603ed6c 1263 uint64_t wholedisk = B_FALSE;
34dc7c2f
BB
1264
1265 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
1266
1267 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1268 &child, &children) != 0) {
1269
d603ed6c
BB
1270 verify(!nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path));
1271 if (strcmp(type, VDEV_TYPE_DISK) == 0)
1272 verify(!nvlist_lookup_uint64(nv,
1273 ZPOOL_CONFIG_WHOLE_DISK, &wholedisk));
34dc7c2f
BB
1274
1275 /*
1276 * As a generic check, we look to see if this is a replace of a
1277 * hot spare within the same pool. If so, we allow it
d603ed6c 1278 * regardless of what libblkid or zpool_in_use() says.
34dc7c2f 1279 */
428870ff 1280 if (replacing) {
8128bd89
BB
1281 (void) strlcpy(buf, path, sizeof (buf));
1282 if (wholedisk) {
1283 ret = zfs_append_partition(buf, sizeof (buf));
1284 if (ret == -1)
1285 return (-1);
1286 }
428870ff 1287
34dc7c2f
BB
1288 if (is_spare(config, buf))
1289 return (0);
1290 }
1291
1292 if (strcmp(type, VDEV_TYPE_DISK) == 0)
d603ed6c 1293 ret = check_device(path, force, isspare, wholedisk);
34dc7c2f
BB
1294
1295 if (strcmp(type, VDEV_TYPE_FILE) == 0)
1296 ret = check_file(path, force, isspare);
1297
1298 return (ret);
1299 }
1300
1301 for (c = 0; c < children; c++)
1302 if ((ret = check_in_use(config, child[c], force,
428870ff 1303 replacing, B_FALSE)) != 0)
34dc7c2f
BB
1304 return (ret);
1305
1306 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1307 &child, &children) == 0)
1308 for (c = 0; c < children; c++)
1309 if ((ret = check_in_use(config, child[c], force,
428870ff 1310 replacing, B_TRUE)) != 0)
34dc7c2f
BB
1311 return (ret);
1312
1313 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1314 &child, &children) == 0)
1315 for (c = 0; c < children; c++)
1316 if ((ret = check_in_use(config, child[c], force,
428870ff 1317 replacing, B_FALSE)) != 0)
34dc7c2f
BB
1318 return (ret);
1319
1320 return (0);
1321}
1322
1323static const char *
45d1cae3 1324is_grouping(const char *type, int *mindev, int *maxdev)
34dc7c2f 1325{
45d1cae3
BB
1326 if (strncmp(type, "raidz", 5) == 0) {
1327 const char *p = type + 5;
1328 char *end;
1329 long nparity;
1330
1331 if (*p == '\0') {
1332 nparity = 1;
1333 } else if (*p == '0') {
1334 return (NULL); /* no zero prefixes allowed */
1335 } else {
1336 errno = 0;
1337 nparity = strtol(p, &end, 10);
1338 if (errno != 0 || nparity < 1 || nparity >= 255 ||
1339 *end != '\0')
1340 return (NULL);
1341 }
34dc7c2f 1342
34dc7c2f 1343 if (mindev != NULL)
45d1cae3
BB
1344 *mindev = nparity + 1;
1345 if (maxdev != NULL)
1346 *maxdev = 255;
34dc7c2f
BB
1347 return (VDEV_TYPE_RAIDZ);
1348 }
1349
45d1cae3
BB
1350 if (maxdev != NULL)
1351 *maxdev = INT_MAX;
1352
34dc7c2f
BB
1353 if (strcmp(type, "mirror") == 0) {
1354 if (mindev != NULL)
1355 *mindev = 2;
1356 return (VDEV_TYPE_MIRROR);
1357 }
1358
1359 if (strcmp(type, "spare") == 0) {
1360 if (mindev != NULL)
1361 *mindev = 1;
1362 return (VDEV_TYPE_SPARE);
1363 }
1364
1365 if (strcmp(type, "log") == 0) {
1366 if (mindev != NULL)
1367 *mindev = 1;
1368 return (VDEV_TYPE_LOG);
1369 }
1370
1371 if (strcmp(type, "cache") == 0) {
1372 if (mindev != NULL)
1373 *mindev = 1;
1374 return (VDEV_TYPE_L2CACHE);
1375 }
1376
1377 return (NULL);
1378}
1379
1380/*
1381 * Construct a syntactically valid vdev specification,
1382 * and ensure that all devices and files exist and can be opened.
1383 * Note: we don't bother freeing anything in the error paths
1384 * because the program is just going to exit anyway.
1385 */
1386nvlist_t *
df30f566 1387construct_spec(nvlist_t *props, int argc, char **argv)
34dc7c2f
BB
1388{
1389 nvlist_t *nvroot, *nv, **top, **spares, **l2cache;
45d1cae3 1390 int t, toplevels, mindev, maxdev, nspares, nlogs, nl2cache;
34dc7c2f
BB
1391 const char *type;
1392 uint64_t is_log;
1393 boolean_t seen_logs;
1394
1395 top = NULL;
1396 toplevels = 0;
1397 spares = NULL;
1398 l2cache = NULL;
1399 nspares = 0;
1400 nlogs = 0;
1401 nl2cache = 0;
1402 is_log = B_FALSE;
1403 seen_logs = B_FALSE;
1404
1405 while (argc > 0) {
1406 nv = NULL;
1407
1408 /*
1409 * If it's a mirror or raidz, the subsequent arguments are
1410 * its leaves -- until we encounter the next mirror or raidz.
1411 */
45d1cae3 1412 if ((type = is_grouping(argv[0], &mindev, &maxdev)) != NULL) {
34dc7c2f
BB
1413 nvlist_t **child = NULL;
1414 int c, children = 0;
1415
1416 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1417 if (spares != NULL) {
1418 (void) fprintf(stderr,
1419 gettext("invalid vdev "
1420 "specification: 'spare' can be "
1421 "specified only once\n"));
1422 return (NULL);
1423 }
1424 is_log = B_FALSE;
1425 }
1426
1427 if (strcmp(type, VDEV_TYPE_LOG) == 0) {
1428 if (seen_logs) {
1429 (void) fprintf(stderr,
1430 gettext("invalid vdev "
1431 "specification: 'log' can be "
1432 "specified only once\n"));
1433 return (NULL);
1434 }
1435 seen_logs = B_TRUE;
1436 is_log = B_TRUE;
1437 argc--;
1438 argv++;
1439 /*
1440 * A log is not a real grouping device.
1441 * We just set is_log and continue.
1442 */
1443 continue;
1444 }
1445
1446 if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1447 if (l2cache != NULL) {
1448 (void) fprintf(stderr,
1449 gettext("invalid vdev "
1450 "specification: 'cache' can be "
1451 "specified only once\n"));
1452 return (NULL);
1453 }
1454 is_log = B_FALSE;
1455 }
1456
1457 if (is_log) {
1458 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
1459 (void) fprintf(stderr,
1460 gettext("invalid vdev "
1461 "specification: unsupported 'log' "
1462 "device: %s\n"), type);
1463 return (NULL);
1464 }
1465 nlogs++;
1466 }
1467
1468 for (c = 1; c < argc; c++) {
45d1cae3 1469 if (is_grouping(argv[c], NULL, NULL) != NULL)
34dc7c2f
BB
1470 break;
1471 children++;
1472 child = realloc(child,
1473 children * sizeof (nvlist_t *));
1474 if (child == NULL)
1475 zpool_no_memory();
df30f566 1476 if ((nv = make_leaf_vdev(props, argv[c], B_FALSE))
34dc7c2f
BB
1477 == NULL)
1478 return (NULL);
1479 child[children - 1] = nv;
1480 }
1481
1482 if (children < mindev) {
1483 (void) fprintf(stderr, gettext("invalid vdev "
1484 "specification: %s requires at least %d "
1485 "devices\n"), argv[0], mindev);
1486 return (NULL);
1487 }
1488
45d1cae3
BB
1489 if (children > maxdev) {
1490 (void) fprintf(stderr, gettext("invalid vdev "
1491 "specification: %s supports no more than "
1492 "%d devices\n"), argv[0], maxdev);
1493 return (NULL);
1494 }
1495
34dc7c2f
BB
1496 argc -= c;
1497 argv += c;
1498
1499 if (strcmp(type, VDEV_TYPE_SPARE) == 0) {
1500 spares = child;
1501 nspares = children;
1502 continue;
1503 } else if (strcmp(type, VDEV_TYPE_L2CACHE) == 0) {
1504 l2cache = child;
1505 nl2cache = children;
1506 continue;
1507 } else {
1508 verify(nvlist_alloc(&nv, NV_UNIQUE_NAME,
1509 0) == 0);
1510 verify(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
1511 type) == 0);
1512 verify(nvlist_add_uint64(nv,
1513 ZPOOL_CONFIG_IS_LOG, is_log) == 0);
1514 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
1515 verify(nvlist_add_uint64(nv,
1516 ZPOOL_CONFIG_NPARITY,
1517 mindev - 1) == 0);
1518 }
1519 verify(nvlist_add_nvlist_array(nv,
1520 ZPOOL_CONFIG_CHILDREN, child,
1521 children) == 0);
1522
1523 for (c = 0; c < children; c++)
1524 nvlist_free(child[c]);
1525 free(child);
1526 }
1527 } else {
1528 /*
1529 * We have a device. Pass off to make_leaf_vdev() to
1530 * construct the appropriate nvlist describing the vdev.
1531 */
df30f566 1532 if ((nv = make_leaf_vdev(props, argv[0], is_log)) == NULL)
34dc7c2f
BB
1533 return (NULL);
1534 if (is_log)
1535 nlogs++;
1536 argc--;
1537 argv++;
1538 }
1539
1540 toplevels++;
1541 top = realloc(top, toplevels * sizeof (nvlist_t *));
1542 if (top == NULL)
1543 zpool_no_memory();
1544 top[toplevels - 1] = nv;
1545 }
1546
1547 if (toplevels == 0 && nspares == 0 && nl2cache == 0) {
1548 (void) fprintf(stderr, gettext("invalid vdev "
1549 "specification: at least one toplevel vdev must be "
1550 "specified\n"));
1551 return (NULL);
1552 }
1553
1554 if (seen_logs && nlogs == 0) {
1555 (void) fprintf(stderr, gettext("invalid vdev specification: "
1556 "log requires at least 1 device\n"));
1557 return (NULL);
1558 }
1559
1560 /*
1561 * Finally, create nvroot and add all top-level vdevs to it.
1562 */
1563 verify(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) == 0);
1564 verify(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
1565 VDEV_TYPE_ROOT) == 0);
1566 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1567 top, toplevels) == 0);
1568 if (nspares != 0)
1569 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1570 spares, nspares) == 0);
1571 if (nl2cache != 0)
1572 verify(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1573 l2cache, nl2cache) == 0);
1574
1575 for (t = 0; t < toplevels; t++)
1576 nvlist_free(top[t]);
1577 for (t = 0; t < nspares; t++)
1578 nvlist_free(spares[t]);
1579 for (t = 0; t < nl2cache; t++)
1580 nvlist_free(l2cache[t]);
1581 if (spares)
1582 free(spares);
1583 if (l2cache)
1584 free(l2cache);
1585 free(top);
1586
1587 return (nvroot);
1588}
1589
428870ff
BB
1590nvlist_t *
1591split_mirror_vdev(zpool_handle_t *zhp, char *newname, nvlist_t *props,
1592 splitflags_t flags, int argc, char **argv)
1593{
1594 nvlist_t *newroot = NULL, **child;
1595 uint_t c, children;
1596
1597 if (argc > 0) {
df30f566 1598 if ((newroot = construct_spec(props, argc, argv)) == NULL) {
428870ff
BB
1599 (void) fprintf(stderr, gettext("Unable to build a "
1600 "pool from the specified devices\n"));
1601 return (NULL);
1602 }
1603
1604 if (!flags.dryrun && make_disks(zhp, newroot) != 0) {
1605 nvlist_free(newroot);
1606 return (NULL);
1607 }
1608
1609 /* avoid any tricks in the spec */
1610 verify(nvlist_lookup_nvlist_array(newroot,
1611 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
1612 for (c = 0; c < children; c++) {
1613 char *path;
1614 const char *type;
1615 int min, max;
1616
1617 verify(nvlist_lookup_string(child[c],
1618 ZPOOL_CONFIG_PATH, &path) == 0);
1619 if ((type = is_grouping(path, &min, &max)) != NULL) {
1620 (void) fprintf(stderr, gettext("Cannot use "
1621 "'%s' as a device for splitting\n"), type);
1622 nvlist_free(newroot);
1623 return (NULL);
1624 }
1625 }
1626 }
1627
1628 if (zpool_vdev_split(zhp, newname, &newroot, props, flags) != 0) {
1629 if (newroot != NULL)
1630 nvlist_free(newroot);
1631 return (NULL);
1632 }
1633
1634 return (newroot);
1635}
34dc7c2f
BB
1636
1637/*
1638 * Get and validate the contents of the given vdev specification. This ensures
1639 * that the nvlist returned is well-formed, that all the devices exist, and that
1640 * they are not currently in use by any other known consumer. The 'poolconfig'
1641 * parameter is the current configuration of the pool when adding devices
1642 * existing pool, and is used to perform additional checks, such as changing the
1643 * replication level of the pool. It can be 'NULL' to indicate that this is a
1644 * new pool. The 'force' flag controls whether devices should be forcefully
1645 * added, even if they appear in use.
1646 */
1647nvlist_t *
df30f566 1648make_root_vdev(zpool_handle_t *zhp, nvlist_t *props, int force, int check_rep,
428870ff 1649 boolean_t replacing, boolean_t dryrun, int argc, char **argv)
34dc7c2f
BB
1650{
1651 nvlist_t *newroot;
1652 nvlist_t *poolconfig = NULL;
1653 is_force = force;
1654
1655 /*
1656 * Construct the vdev specification. If this is successful, we know
1657 * that we have a valid specification, and that all devices can be
1658 * opened.
1659 */
df30f566 1660 if ((newroot = construct_spec(props, argc, argv)) == NULL)
34dc7c2f
BB
1661 return (NULL);
1662
1663 if (zhp && ((poolconfig = zpool_get_config(zhp, NULL)) == NULL))
1664 return (NULL);
1665
1666 /*
1667 * Validate each device to make sure that its not shared with another
1668 * subsystem. We do this even if 'force' is set, because there are some
1669 * uses (such as a dedicated dump device) that even '-f' cannot
1670 * override.
1671 */
428870ff 1672 if (check_in_use(poolconfig, newroot, force, replacing, B_FALSE) != 0) {
34dc7c2f
BB
1673 nvlist_free(newroot);
1674 return (NULL);
1675 }
1676
1677 /*
1678 * Check the replication level of the given vdevs and report any errors
1679 * found. We include the existing pool spec, if any, as we need to
1680 * catch changes against the existing replication level.
1681 */
1682 if (check_rep && check_replication(poolconfig, newroot) != 0) {
1683 nvlist_free(newroot);
1684 return (NULL);
1685 }
1686
1687 /*
1688 * Run through the vdev specification and label any whole disks found.
1689 */
b128c09f 1690 if (!dryrun && make_disks(zhp, newroot) != 0) {
34dc7c2f
BB
1691 nvlist_free(newroot);
1692 return (NULL);
1693 }
1694
1695 return (newroot);
1696}