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