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