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