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.
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.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
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:
31 * disk=(path=..., devid=...)
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.
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.
48 * The only function exported by this file is 'make_root_vdev'. The
49 * function performs several passes:
51 * 1. Construct the vdev specification. Performs syntax validation and
52 * makes sure each device is valid.
53 * 2. Check for devices in use. Using libblkid to make sure that no
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
59 * 4. Call libzfs to label any whole disks with an EFI label.
68 #include <libnvpair.h>
70 #include <scsi/scsi.h>
75 #include <sys/efi_partition.h>
78 #include <sys/mntent.h>
79 #include <uuid/uuid.h>
81 #include <blkid/blkid.h>
83 #define blkid_cache void *
84 #endif /* HAVE_LIBBLKID */
86 #include "zpool_util.h"
87 #include <sys/zfs_context.h>
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.
97 typedef struct vdev_disk_db_entry
101 } vdev_disk_db_entry_t
;
104 * Database of block devices that lie about physical sector sizes. The
105 * identification string must be precisely 24 characters to avoid false
108 static vdev_disk_db_entry_t vdev_disk_database
[] = {
109 {"ATA C400-MTFDDAC064M", 8192},
110 {"ATA C400-MTFDDAC128M", 8192},
111 {"ATA C400-MTFDDAC256M", 8192},
112 {"ATA C400-MTFDDAC512M", 8192},
113 {"ATA Corsair Force 3 ", 8192},
114 {"ATA Corsair Force GS", 8192},
115 {"ATA INTEL SSDSA2CT04", 8192},
116 {"ATA INTEL SSDSA2CW04", 8192},
117 {"ATA INTEL SSDSA2CW08", 8192},
118 {"ATA INTEL SSDSA2CW12", 8192},
119 {"ATA INTEL SSDSA2CW16", 8192},
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},
128 {"ATA INTEL SSDSC2CT18", 8192},
129 {"ATA INTEL SSDSC2CT24", 8192},
130 {"ATA INTEL SSDSC2CW06", 8192},
131 {"ATA INTEL SSDSC2CW12", 8192},
132 {"ATA INTEL SSDSC2CW18", 8192},
133 {"ATA INTEL SSDSC2CW24", 8192},
134 {"ATA INTEL SSDSC2CW48", 8192},
135 {"ATA KINGSTON SH100S3", 8192},
136 {"ATA KINGSTON SH103S3", 8192},
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},
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},
151 {"ATA SAMSUNG SSD 830 ", 8192},
152 {"ATA Samsung SSD 840 ", 8192},
153 {"ATA TOSHIBA THNSNH06", 8192},
154 {"ATA TOSHIBA THNSNH12", 8192},
155 {"ATA TOSHIBA THNSNH25", 8192},
156 {"ATA TOSHIBA THNSNH51", 8192},
157 {"ATA INTEL SSDSA2M040", 4096},
158 {"ATA INTEL SSDSA2M080", 4096},
159 {"ATA INTEL SSDSA2M160", 4096},
160 {"ATA OCZ CORE_SSD ", 4096},
161 {"ATA SAMSUNG MCCOE32G", 4096},
162 {"ATA SAMSUNG MCCOE64G", 4096},
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 */
180 {"ATA QEMU HARDDISK ", 8192},
181 {"IET VIRTUAL-DISK ", 8192},
182 {"OI COMSTAR ", 8192},
186 static const int vdev_disk_database_size
=
187 sizeof (vdev_disk_database
) / sizeof (vdev_disk_database
[0]);
189 #define INQ_REPLY_LEN 96
190 #define INQ_CMD_LEN 6
193 check_sector_size_database(char *path
, int *sector_size
)
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};
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 */
216 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
219 error
= ioctl(fd
, SG_IO
, (unsigned long) &io_hdr
);
226 if ((io_hdr
.info
& SG_INFO_OK_MASK
) != SG_INFO_OK
)
229 for (i
= 0; i
< vdev_disk_database_size
; i
++) {
230 if (memcmp(inq_buff
+ 8, vdev_disk_database
[i
].id
, 24))
233 *sector_size
= vdev_disk_database
[i
].sector_size
;
242 vdev_error(const char *fmt
, ...)
247 (void) fprintf(stderr
, gettext("invalid vdev specification\n"));
249 (void) fprintf(stderr
, gettext("use '-f' to override "
250 "the following errors:\n"));
252 (void) fprintf(stderr
, gettext("the following errors "
253 "must be manually repaired:\n"));
258 (void) vfprintf(stderr
, fmt
, ap
);
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.
267 check_file(const char *file
, boolean_t force
, boolean_t isspare
)
275 if ((fd
= open(file
, O_RDONLY
)) < 0)
278 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) == 0 && inuse
) {
282 case POOL_STATE_ACTIVE
:
283 desc
= gettext("active");
286 case POOL_STATE_EXPORTED
:
287 desc
= gettext("exported");
290 case POOL_STATE_POTENTIALLY_ACTIVE
:
291 desc
= gettext("potentially active");
295 desc
= gettext("unknown");
300 * Allow hot spares to be shared between pools.
302 if (state
== POOL_STATE_SPARE
&& isspare
)
305 if (state
== POOL_STATE_ACTIVE
||
306 state
== POOL_STATE_SPARE
|| !force
) {
308 case POOL_STATE_SPARE
:
309 vdev_error(gettext("%s is reserved as a hot "
310 "spare for pool %s\n"), file
, name
);
313 vdev_error(gettext("%s is part of %s pool "
314 "'%s'\n"), file
, desc
, name
);
330 (void) fprintf(stderr
, gettext("warning: device in use checking "
331 "failed: %s\n"), strerror(err
));
335 check_slice(const char *path
, blkid_cache cache
, int force
, boolean_t isspare
)
341 /* No valid type detected device is safe to use */
342 value
= blkid_get_tag_value(cache
, "TYPE", path
);
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.
351 if (strcmp(value
, "zfs") == 0) {
352 err
= check_file(path
, force
, isspare
);
358 vdev_error(gettext("%s contains a filesystem of "
359 "type '%s'\n"), path
, value
);
365 err
= check_file(path
, force
, isspare
);
366 #endif /* HAVE_LIBBLKID */
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().
376 check_disk(const char *path
, blkid_cache cache
, int force
,
377 boolean_t isspare
, boolean_t iswholedisk
)
380 char slice_path
[MAXPATHLEN
];
384 /* This is not a wholedisk we only check the given partition */
386 return check_slice(path
, cache
, force
, isspare
);
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.
396 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0) {
401 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
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
);
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.
419 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
424 /* Partitions will no be created using the backup */
427 vdev_error(gettext("%s contains a corrupt primary "
428 "EFI label.\n"), path
);
433 for (i
= 0; i
< vtoc
->efi_nparts
; i
++) {
435 if (vtoc
->efi_parts
[i
].p_tag
== V_UNASSIGNED
||
436 uuid_is_null((uchar_t
*)&vtoc
->efi_parts
[i
].p_guid
))
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);
443 (void) snprintf(slice_path
, sizeof (slice_path
),
444 "%s%s%d", path
, isdigit(path
[strlen(path
)-1]) ?
447 err
= check_slice(slice_path
, cache
, force
, isspare
);
459 check_device(const char *path
, boolean_t force
,
460 boolean_t isspare
, boolean_t iswholedisk
)
462 static blkid_cache cache
= NULL
;
466 * There is no easy way to add a correct blkid_put_cache() call,
467 * memory will be reclaimed when the command exits.
472 if ((err
= blkid_get_cache(&cache
, NULL
)) != 0) {
477 if ((err
= blkid_probe_all(cache
)) != 0) {
478 blkid_put_cache(cache
);
483 #endif /* HAVE_LIBBLKID */
485 return check_disk(path
, cache
, force
, isspare
, iswholedisk
);
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
497 is_whole_disk(const char *path
)
499 struct dk_gpt
*label
;
502 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
504 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
514 * This may be a shorthand device path or it could be total gibberish.
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).
520 is_shorthand_path(const char *arg
, char *path
,
521 struct stat64
*statbuf
, boolean_t
*wholedisk
)
525 error
= zfs_resolve_shortname(arg
, path
, MAXPATHLEN
);
527 *wholedisk
= is_whole_disk(path
);
528 if (*wholedisk
|| (stat64(path
, statbuf
) == 0))
532 strlcpy(path
, arg
, sizeof(path
));
533 memset(statbuf
, 0, sizeof(*statbuf
));
534 *wholedisk
= B_FALSE
;
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.
544 is_spare(nvlist_t
*config
, const char *path
)
550 uint64_t guid
, spareguid
;
556 if ((fd
= open(path
, O_RDONLY
)) < 0)
559 if (zpool_in_use(g_zfs
, fd
, &state
, &name
, &inuse
) != 0 ||
561 state
!= POOL_STATE_SPARE
||
562 zpool_read_label(fd
, &label
) != 0) {
573 verify(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
576 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
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
)
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
596 * /dev/xxx Complete disk path
597 * /xxx Full path to file
598 * xxx Shorthand for <zfs_vdev_paths>/xxx
601 make_leaf_vdev(nvlist_t
*props
, const char *arg
, uint64_t is_log
)
603 char path
[MAXPATHLEN
];
604 struct stat64 statbuf
;
605 nvlist_t
*vdev
= NULL
;
607 boolean_t wholedisk
= B_FALSE
;
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.
618 * Complete device or file path. Exact type is determined by
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.
625 if (realpath(arg
, path
) == NULL
) {
626 (void) fprintf(stderr
,
627 gettext("cannot resolve path '%s'\n"), arg
);
631 wholedisk
= is_whole_disk(path
);
632 if (!wholedisk
&& (stat64(path
, &statbuf
) != 0)) {
633 (void) fprintf(stderr
,
634 gettext("cannot open '%s': %s\n"),
635 path
, strerror(errno
));
639 /* After is_whole_disk() check restore original passed path */
640 strlcpy(path
, arg
, MAXPATHLEN
);
642 err
= is_shorthand_path(arg
, path
, &statbuf
, &wholedisk
);
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
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"));
660 (void) fprintf(stderr
,
661 gettext("cannot open '%s': %s\n"),
662 path
, strerror(errno
));
669 * Determine whether this is a device or a file.
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
;
676 (void) fprintf(stderr
, gettext("cannot use '%s': must be a "
677 "block device or regular file\n"), path
);
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.
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);
695 * Override defaults if custom properties are provided.
700 if (nvlist_lookup_string(props
,
701 zpool_prop_to_name(ZPOOL_PROP_ASHIFT
), &value
) == 0)
702 zfs_nicestrtonum(NULL
, value
, &ashift
);
706 * If the device is known to incorrectly report its physical sector
707 * size explicitly provide the known correct value.
712 if (check_sector_size_database(path
, §or_size
) == B_TRUE
)
713 ashift
= highbit(sector_size
) - 1;
717 nvlist_add_uint64(vdev
, ZPOOL_CONFIG_ASHIFT
, ashift
);
723 * Go through and verify the replication level of the pool is consistent.
724 * Performs the following checks:
726 * For the new spec, verifies that devices in mirrors and raidz are the
729 * If the current configuration already has inconsistent replication
730 * levels, ignore any other potential problems in the new spec.
732 * Otherwise, make sure that the current spec (if there is one) and the new
733 * spec have consistent replication levels.
735 typedef struct replication_level
{
737 uint64_t zprl_children
;
738 uint64_t zprl_parity
;
739 } replication_level_t
;
741 #define ZPOOL_FUZZ (16 * 1024 * 1024)
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.
748 static replication_level_t
*
749 get_replication(nvlist_t
*nvroot
, boolean_t fatal
)
757 replication_level_t lastrep
= { 0 }, rep
, *ret
;
758 boolean_t dontreport
;
760 ret
= safe_malloc(sizeof (replication_level_t
));
762 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
763 &top
, &toplevels
) == 0);
765 lastrep
.zprl_type
= NULL
;
766 for (t
= 0; t
< toplevels
; t
++) {
767 uint64_t is_log
= B_FALSE
;
772 * For separate logs we ignore the top level vdev replication
775 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
779 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
,
781 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
782 &child
, &children
) != 0) {
784 * This is a 'file' or 'disk' vdev.
786 rep
.zprl_type
= type
;
787 rep
.zprl_children
= 1;
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
798 * We also check that the size of each vdev (if it can
799 * be determined) is the same.
801 rep
.zprl_type
= type
;
802 rep
.zprl_children
= 0;
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);
814 * The 'dontreport' variable indicates that we've
815 * already reported an error for this spec, so don't
816 * bother doing it again.
821 for (c
= 0; c
< children
; c
++) {
822 nvlist_t
*cnv
= child
[c
];
824 struct stat64 statbuf
;
825 uint64_t size
= -1ULL;
831 verify(nvlist_lookup_string(cnv
,
832 ZPOOL_CONFIG_TYPE
, &childtype
) == 0);
835 * If this is a replacing or spare vdev, then
836 * get the real first child of the vdev.
838 if (strcmp(childtype
,
839 VDEV_TYPE_REPLACING
) == 0 ||
840 strcmp(childtype
, VDEV_TYPE_SPARE
) == 0) {
844 verify(nvlist_lookup_nvlist_array(cnv
,
845 ZPOOL_CONFIG_CHILDREN
, &rchild
,
847 assert(rchildren
== 2);
850 verify(nvlist_lookup_string(cnv
,
855 verify(nvlist_lookup_string(cnv
,
856 ZPOOL_CONFIG_PATH
, &path
) == 0);
859 * If we have a raidz/mirror that combines disks
860 * with files, report it as an error.
862 if (!dontreport
&& type
!= NULL
&&
863 strcmp(type
, childtype
) != 0) {
869 "mismatched replication "
870 "level: %s contains both "
871 "files and devices\n"),
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.
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.
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.
893 if ((fd
= open(path
, O_RDONLY
)) >= 0) {
894 err
= fstat64(fd
, &statbuf
);
897 err
= stat64(path
, &statbuf
);
901 statbuf
.st_size
== 0 ||
902 statbuf
.st_size
== MAXOFFSET_T
)
905 size
= statbuf
.st_size
;
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.
914 (vdev_size
!= -1ULL &&
915 (labs(size
- vdev_size
) >
922 "%s contains devices of "
923 "different sizes\n"),
936 * At this point, we have the replication of the last toplevel
937 * vdev in 'rep'. Compare it to 'lastrep' to see if its
940 if (lastrep
.zprl_type
!= NULL
) {
941 if (strcmp(lastrep
.zprl_type
, rep
.zprl_type
) != 0) {
947 "mismatched replication level: "
948 "both %s and %s vdevs are "
950 lastrep
.zprl_type
, rep
.zprl_type
);
953 } else if (lastrep
.zprl_parity
!= rep
.zprl_parity
) {
959 "mismatched replication level: "
960 "both %llu and %llu device parity "
961 "%s vdevs are present\n"),
967 } else if (lastrep
.zprl_children
!= rep
.zprl_children
) {
973 "mismatched replication level: "
974 "both %llu-way and %llu-way %s "
975 "vdevs are present\n"),
976 lastrep
.zprl_children
,
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.
999 check_replication(nvlist_t
*config
, nvlist_t
*newroot
)
1003 replication_level_t
*current
= NULL
, *new;
1007 * If we have a current pool configuration, check to see if it's
1008 * self-consistent. If not, simply return success.
1010 if (config
!= NULL
) {
1013 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
1015 if ((current
= get_replication(nvroot
, B_FALSE
)) == NULL
)
1019 * for spares there may be no children, and therefore no
1020 * replication level to check
1022 if ((nvlist_lookup_nvlist_array(newroot
, ZPOOL_CONFIG_CHILDREN
,
1023 &child
, &children
) != 0) || (children
== 0)) {
1029 * If all we have is logs then there's no replication level to check.
1031 if (num_logs(newroot
) == children
) {
1037 * Get the replication level of the new vdev spec, reporting any
1038 * inconsistencies found.
1040 if ((new = get_replication(newroot
, B_TRUE
)) == NULL
) {
1046 * Check to see if the new vdev spec matches the replication level of
1050 if (current
!= NULL
) {
1051 if (strcmp(current
->zprl_type
, new->zprl_type
) != 0) {
1053 "mismatched replication level: pool uses %s "
1054 "and new vdev is %s\n"),
1055 current
->zprl_type
, new->zprl_type
);
1057 } else if (current
->zprl_parity
!= new->zprl_parity
) {
1059 "mismatched replication level: pool uses %llu "
1060 "device parity and new vdev uses %llu\n"),
1061 current
->zprl_parity
, new->zprl_parity
);
1063 } else if (current
->zprl_children
!= new->zprl_children
) {
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
);
1074 if (current
!= NULL
)
1081 zero_label(char *path
)
1083 const int size
= 4096;
1087 if ((fd
= open(path
, O_WRONLY
|O_EXCL
)) < 0) {
1088 (void) fprintf(stderr
, gettext("cannot open '%s': %s\n"),
1089 path
, strerror(errno
));
1093 memset(buf
, 0, size
);
1094 err
= write(fd
, buf
, size
);
1095 (void) fdatasync(fd
);
1099 (void) fprintf(stderr
, gettext("cannot zero first %d bytes "
1100 "of '%s': %s\n"), size
, path
, strerror(errno
));
1105 (void) fprintf(stderr
, gettext("could only zero %d/%d bytes "
1106 "of '%s'\n"), err
, size
, path
);
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.
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.
1124 make_disks(zpool_handle_t
*zhp
, nvlist_t
*nv
)
1129 char devpath
[MAXPATHLEN
];
1130 char udevpath
[MAXPATHLEN
];
1132 struct stat64 statbuf
;
1133 int is_exclusive
= 0;
1137 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1139 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1140 &child
, &children
) != 0) {
1142 if (strcmp(type
, VDEV_TYPE_DISK
) != 0)
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.
1152 verify(!nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
));
1153 verify(!nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1157 (void) zero_label(path
);
1161 if (realpath(path
, devpath
) == NULL
) {
1163 (void) fprintf(stderr
,
1164 gettext("cannot resolve path '%s'\n"), path
);
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.
1177 strncpy(udevpath
, path
, MAXPATHLEN
);
1178 (void) zfs_append_partition(udevpath
, MAXPATHLEN
);
1180 fd
= open(devpath
, O_RDWR
|O_EXCL
);
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.
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.
1198 if (!is_exclusive
|| !is_spare(NULL
, udevpath
)) {
1199 ret
= strncmp(udevpath
,UDISK_ROOT
,strlen(UDISK_ROOT
));
1201 ret
= lstat64(udevpath
, &statbuf
);
1202 if (ret
== 0 && S_ISLNK(statbuf
.st_mode
))
1203 (void) unlink(udevpath
);
1206 if (zpool_label_disk(g_zfs
, zhp
,
1207 strrchr(devpath
, '/') + 1) == -1)
1210 ret
= zpool_label_disk_wait(udevpath
, DISK_LABEL_WAIT
);
1212 (void) fprintf(stderr
, gettext("cannot "
1213 "resolve path '%s': %d\n"), udevpath
, ret
);
1217 (void) zero_label(udevpath
);
1221 * Update the path to refer to the partition. The presence of
1222 * the 'whole_disk' field indicates to the CLI that we should
1223 * chop off the partition number when displaying the device in
1226 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_PATH
, udevpath
) == 0);
1231 for (c
= 0; c
< children
; c
++)
1232 if ((ret
= make_disks(zhp
, child
[c
])) != 0)
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)
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)
1251 * Go through and find any devices that are in use. We rely on libdiskmgt for
1252 * the majority of this task.
1255 check_in_use(nvlist_t
*config
, nvlist_t
*nv
, boolean_t force
,
1256 boolean_t replacing
, boolean_t isspare
)
1262 char buf
[MAXPATHLEN
];
1263 uint64_t wholedisk
= B_FALSE
;
1265 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
1267 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1268 &child
, &children
) != 0) {
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
));
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
1278 * regardless of what libblkid or zpool_in_use() says.
1281 (void) strlcpy(buf
, path
, sizeof (buf
));
1283 ret
= zfs_append_partition(buf
, sizeof (buf
));
1288 if (is_spare(config
, buf
))
1292 if (strcmp(type
, VDEV_TYPE_DISK
) == 0)
1293 ret
= check_device(path
, force
, isspare
, wholedisk
);
1295 if (strcmp(type
, VDEV_TYPE_FILE
) == 0)
1296 ret
= check_file(path
, force
, isspare
);
1301 for (c
= 0; c
< children
; c
++)
1302 if ((ret
= check_in_use(config
, child
[c
], force
,
1303 replacing
, B_FALSE
)) != 0)
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
,
1310 replacing
, B_TRUE
)) != 0)
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
,
1317 replacing
, B_FALSE
)) != 0)
1324 is_grouping(const char *type
, int *mindev
, int *maxdev
)
1326 if (strncmp(type
, "raidz", 5) == 0) {
1327 const char *p
= type
+ 5;
1333 } else if (*p
== '0') {
1334 return (NULL
); /* no zero prefixes allowed */
1337 nparity
= strtol(p
, &end
, 10);
1338 if (errno
!= 0 || nparity
< 1 || nparity
>= 255 ||
1344 *mindev
= nparity
+ 1;
1347 return (VDEV_TYPE_RAIDZ
);
1353 if (strcmp(type
, "mirror") == 0) {
1356 return (VDEV_TYPE_MIRROR
);
1359 if (strcmp(type
, "spare") == 0) {
1362 return (VDEV_TYPE_SPARE
);
1365 if (strcmp(type
, "log") == 0) {
1368 return (VDEV_TYPE_LOG
);
1371 if (strcmp(type
, "cache") == 0) {
1374 return (VDEV_TYPE_L2CACHE
);
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.
1387 construct_spec(nvlist_t
*props
, int argc
, char **argv
)
1389 nvlist_t
*nvroot
, *nv
, **top
, **spares
, **l2cache
;
1390 int t
, toplevels
, mindev
, maxdev
, nspares
, nlogs
, nl2cache
;
1393 boolean_t seen_logs
;
1403 seen_logs
= B_FALSE
;
1409 * If it's a mirror or raidz, the subsequent arguments are
1410 * its leaves -- until we encounter the next mirror or raidz.
1412 if ((type
= is_grouping(argv
[0], &mindev
, &maxdev
)) != NULL
) {
1413 nvlist_t
**child
= NULL
;
1414 int c
, children
= 0;
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"));
1427 if (strcmp(type
, VDEV_TYPE_LOG
) == 0) {
1429 (void) fprintf(stderr
,
1430 gettext("invalid vdev "
1431 "specification: 'log' can be "
1432 "specified only once\n"));
1440 * A log is not a real grouping device.
1441 * We just set is_log and continue.
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"));
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
);
1468 for (c
= 1; c
< argc
; c
++) {
1469 if (is_grouping(argv
[c
], NULL
, NULL
) != NULL
)
1472 child
= realloc(child
,
1473 children
* sizeof (nvlist_t
*));
1476 if ((nv
= make_leaf_vdev(props
, argv
[c
], B_FALSE
))
1479 child
[children
- 1] = nv
;
1482 if (children
< mindev
) {
1483 (void) fprintf(stderr
, gettext("invalid vdev "
1484 "specification: %s requires at least %d "
1485 "devices\n"), argv
[0], mindev
);
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
);
1499 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0) {
1503 } else if (strcmp(type
, VDEV_TYPE_L2CACHE
) == 0) {
1505 nl2cache
= children
;
1508 verify(nvlist_alloc(&nv
, NV_UNIQUE_NAME
,
1510 verify(nvlist_add_string(nv
, ZPOOL_CONFIG_TYPE
,
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
,
1519 verify(nvlist_add_nvlist_array(nv
,
1520 ZPOOL_CONFIG_CHILDREN
, child
,
1523 for (c
= 0; c
< children
; c
++)
1524 nvlist_free(child
[c
]);
1529 * We have a device. Pass off to make_leaf_vdev() to
1530 * construct the appropriate nvlist describing the vdev.
1532 if ((nv
= make_leaf_vdev(props
, argv
[0], is_log
)) == NULL
)
1541 top
= realloc(top
, toplevels
* sizeof (nvlist_t
*));
1544 top
[toplevels
- 1] = nv
;
1547 if (toplevels
== 0 && nspares
== 0 && nl2cache
== 0) {
1548 (void) fprintf(stderr
, gettext("invalid vdev "
1549 "specification: at least one toplevel vdev must be "
1554 if (seen_logs
&& nlogs
== 0) {
1555 (void) fprintf(stderr
, gettext("invalid vdev specification: "
1556 "log requires at least 1 device\n"));
1561 * Finally, create nvroot and add all top-level vdevs to it.
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);
1569 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1570 spares
, nspares
) == 0);
1572 verify(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1573 l2cache
, nl2cache
) == 0);
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
]);
1591 split_mirror_vdev(zpool_handle_t
*zhp
, char *newname
, nvlist_t
*props
,
1592 splitflags_t flags
, int argc
, char **argv
)
1594 nvlist_t
*newroot
= NULL
, **child
;
1598 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
) {
1599 (void) fprintf(stderr
, gettext("Unable to build a "
1600 "pool from the specified devices\n"));
1604 if (!flags
.dryrun
&& make_disks(zhp
, newroot
) != 0) {
1605 nvlist_free(newroot
);
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
++) {
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
);
1628 if (zpool_vdev_split(zhp
, newname
, &newroot
, props
, flags
) != 0) {
1629 if (newroot
!= NULL
)
1630 nvlist_free(newroot
);
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.
1648 make_root_vdev(zpool_handle_t
*zhp
, nvlist_t
*props
, int force
, int check_rep
,
1649 boolean_t replacing
, boolean_t dryrun
, int argc
, char **argv
)
1652 nvlist_t
*poolconfig
= NULL
;
1656 * Construct the vdev specification. If this is successful, we know
1657 * that we have a valid specification, and that all devices can be
1660 if ((newroot
= construct_spec(props
, argc
, argv
)) == NULL
)
1663 if (zhp
&& ((poolconfig
= zpool_get_config(zhp
, NULL
)) == NULL
))
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
1672 if (check_in_use(poolconfig
, newroot
, force
, replacing
, B_FALSE
) != 0) {
1673 nvlist_free(newroot
);
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.
1682 if (check_rep
&& check_replication(poolconfig
, newroot
) != 0) {
1683 nvlist_free(newroot
);
1688 * Run through the vdev specification and label any whole disks found.
1690 if (!dryrun
&& make_disks(zhp
, newroot
) != 0) {
1691 nvlist_free(newroot
);