]> git.proxmox.com Git - mirror_zfs-debian.git/blob - lib/libzfs/libzfs_pool.c
Align parition end on 1 MiB boundary
[mirror_zfs-debian.git] / lib / libzfs / libzfs_pool.c
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 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <devid.h>
29 #include <fcntl.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <zone.h>
36 #include <sys/stat.h>
37 #include <sys/efi_partition.h>
38 #include <sys/vtoc.h>
39 #include <sys/zfs_ioctl.h>
40 #include <dlfcn.h>
41
42 #include "zfs_namecheck.h"
43 #include "zfs_prop.h"
44 #include "libzfs_impl.h"
45 #include "zfs_comutil.h"
46
47 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
48
49 typedef struct prop_flags {
50 int create:1; /* Validate property on creation */
51 int import:1; /* Validate property on import */
52 } prop_flags_t;
53
54 /*
55 * ====================================================================
56 * zpool property functions
57 * ====================================================================
58 */
59
60 static int
61 zpool_get_all_props(zpool_handle_t *zhp)
62 {
63 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
64 libzfs_handle_t *hdl = zhp->zpool_hdl;
65
66 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
67
68 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
69 return (-1);
70
71 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
72 if (errno == ENOMEM) {
73 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
74 zcmd_free_nvlists(&zc);
75 return (-1);
76 }
77 } else {
78 zcmd_free_nvlists(&zc);
79 return (-1);
80 }
81 }
82
83 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
84 zcmd_free_nvlists(&zc);
85 return (-1);
86 }
87
88 zcmd_free_nvlists(&zc);
89
90 return (0);
91 }
92
93 static int
94 zpool_props_refresh(zpool_handle_t *zhp)
95 {
96 nvlist_t *old_props;
97
98 old_props = zhp->zpool_props;
99
100 if (zpool_get_all_props(zhp) != 0)
101 return (-1);
102
103 nvlist_free(old_props);
104 return (0);
105 }
106
107 static char *
108 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
109 zprop_source_t *src)
110 {
111 nvlist_t *nv, *nvl;
112 uint64_t ival;
113 char *value;
114 zprop_source_t source;
115
116 nvl = zhp->zpool_props;
117 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
118 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
119 source = ival;
120 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
121 } else {
122 source = ZPROP_SRC_DEFAULT;
123 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
124 value = "-";
125 }
126
127 if (src)
128 *src = source;
129
130 return (value);
131 }
132
133 uint64_t
134 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
135 {
136 nvlist_t *nv, *nvl;
137 uint64_t value;
138 zprop_source_t source;
139
140 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
141 /*
142 * zpool_get_all_props() has most likely failed because
143 * the pool is faulted, but if all we need is the top level
144 * vdev's guid then get it from the zhp config nvlist.
145 */
146 if ((prop == ZPOOL_PROP_GUID) &&
147 (nvlist_lookup_nvlist(zhp->zpool_config,
148 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
149 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
150 == 0)) {
151 return (value);
152 }
153 return (zpool_prop_default_numeric(prop));
154 }
155
156 nvl = zhp->zpool_props;
157 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
158 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
159 source = value;
160 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
161 } else {
162 source = ZPROP_SRC_DEFAULT;
163 value = zpool_prop_default_numeric(prop);
164 }
165
166 if (src)
167 *src = source;
168
169 return (value);
170 }
171
172 /*
173 * Map VDEV STATE to printed strings.
174 */
175 char *
176 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
177 {
178 switch (state) {
179 default:
180 break;
181 case VDEV_STATE_CLOSED:
182 case VDEV_STATE_OFFLINE:
183 return (gettext("OFFLINE"));
184 case VDEV_STATE_REMOVED:
185 return (gettext("REMOVED"));
186 case VDEV_STATE_CANT_OPEN:
187 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
188 return (gettext("FAULTED"));
189 else if (aux == VDEV_AUX_SPLIT_POOL)
190 return (gettext("SPLIT"));
191 else
192 return (gettext("UNAVAIL"));
193 case VDEV_STATE_FAULTED:
194 return (gettext("FAULTED"));
195 case VDEV_STATE_DEGRADED:
196 return (gettext("DEGRADED"));
197 case VDEV_STATE_HEALTHY:
198 return (gettext("ONLINE"));
199 }
200
201 return (gettext("UNKNOWN"));
202 }
203
204 /*
205 * Get a zpool property value for 'prop' and return the value in
206 * a pre-allocated buffer.
207 */
208 int
209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
210 zprop_source_t *srctype)
211 {
212 uint64_t intval;
213 const char *strval;
214 zprop_source_t src = ZPROP_SRC_NONE;
215 nvlist_t *nvroot;
216 vdev_stat_t *vs;
217 uint_t vsc;
218
219 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
220 switch (prop) {
221 case ZPOOL_PROP_NAME:
222 (void) strlcpy(buf, zpool_get_name(zhp), len);
223 break;
224
225 case ZPOOL_PROP_HEALTH:
226 (void) strlcpy(buf, "FAULTED", len);
227 break;
228
229 case ZPOOL_PROP_GUID:
230 intval = zpool_get_prop_int(zhp, prop, &src);
231 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
232 break;
233
234 case ZPOOL_PROP_ALTROOT:
235 case ZPOOL_PROP_CACHEFILE:
236 if (zhp->zpool_props != NULL ||
237 zpool_get_all_props(zhp) == 0) {
238 (void) strlcpy(buf,
239 zpool_get_prop_string(zhp, prop, &src),
240 len);
241 if (srctype != NULL)
242 *srctype = src;
243 return (0);
244 }
245 /* FALLTHROUGH */
246 default:
247 (void) strlcpy(buf, "-", len);
248 break;
249 }
250
251 if (srctype != NULL)
252 *srctype = src;
253 return (0);
254 }
255
256 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
257 prop != ZPOOL_PROP_NAME)
258 return (-1);
259
260 switch (zpool_prop_get_type(prop)) {
261 case PROP_TYPE_STRING:
262 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
263 len);
264 break;
265
266 case PROP_TYPE_NUMBER:
267 intval = zpool_get_prop_int(zhp, prop, &src);
268
269 switch (prop) {
270 case ZPOOL_PROP_SIZE:
271 case ZPOOL_PROP_ALLOCATED:
272 case ZPOOL_PROP_FREE:
273 case ZPOOL_PROP_ASHIFT:
274 (void) zfs_nicenum(intval, buf, len);
275 break;
276
277 case ZPOOL_PROP_CAPACITY:
278 (void) snprintf(buf, len, "%llu%%",
279 (u_longlong_t)intval);
280 break;
281
282 case ZPOOL_PROP_DEDUPRATIO:
283 (void) snprintf(buf, len, "%llu.%02llux",
284 (u_longlong_t)(intval / 100),
285 (u_longlong_t)(intval % 100));
286 break;
287
288 case ZPOOL_PROP_HEALTH:
289 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
290 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
291 verify(nvlist_lookup_uint64_array(nvroot,
292 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
293 == 0);
294
295 (void) strlcpy(buf, zpool_state_to_name(intval,
296 vs->vs_aux), len);
297 break;
298 default:
299 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
300 }
301 break;
302
303 case PROP_TYPE_INDEX:
304 intval = zpool_get_prop_int(zhp, prop, &src);
305 if (zpool_prop_index_to_string(prop, intval, &strval)
306 != 0)
307 return (-1);
308 (void) strlcpy(buf, strval, len);
309 break;
310
311 default:
312 abort();
313 }
314
315 if (srctype)
316 *srctype = src;
317
318 return (0);
319 }
320
321 /*
322 * Check if the bootfs name has the same pool name as it is set to.
323 * Assuming bootfs is a valid dataset name.
324 */
325 static boolean_t
326 bootfs_name_valid(const char *pool, char *bootfs)
327 {
328 int len = strlen(pool);
329
330 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
331 return (B_FALSE);
332
333 if (strncmp(pool, bootfs, len) == 0 &&
334 (bootfs[len] == '/' || bootfs[len] == '\0'))
335 return (B_TRUE);
336
337 return (B_FALSE);
338 }
339
340 /*
341 * Inspect the configuration to determine if any of the devices contain
342 * an EFI label.
343 */
344 static boolean_t
345 pool_uses_efi(nvlist_t *config)
346 {
347 nvlist_t **child;
348 uint_t c, children;
349
350 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
351 &child, &children) != 0)
352 return (read_efi_label(config, NULL) >= 0);
353
354 for (c = 0; c < children; c++) {
355 if (pool_uses_efi(child[c]))
356 return (B_TRUE);
357 }
358 return (B_FALSE);
359 }
360
361 static boolean_t
362 pool_is_bootable(zpool_handle_t *zhp)
363 {
364 char bootfs[ZPOOL_MAXNAMELEN];
365
366 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
367 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
368 sizeof (bootfs)) != 0);
369 }
370
371
372 /*
373 * Given an nvlist of zpool properties to be set, validate that they are
374 * correct, and parse any numeric properties (index, boolean, etc) if they are
375 * specified as strings.
376 */
377 static nvlist_t *
378 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
379 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
380 {
381 nvpair_t *elem;
382 nvlist_t *retprops;
383 zpool_prop_t prop;
384 char *strval;
385 uint64_t intval;
386 char *slash;
387 struct stat64 statbuf;
388 zpool_handle_t *zhp;
389 nvlist_t *nvroot;
390
391 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
392 (void) no_memory(hdl);
393 return (NULL);
394 }
395
396 elem = NULL;
397 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
398 const char *propname = nvpair_name(elem);
399
400 /*
401 * Make sure this property is valid and applies to this type.
402 */
403 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405 "invalid property '%s'"), propname);
406 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
407 goto error;
408 }
409
410 if (zpool_prop_readonly(prop)) {
411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
412 "is readonly"), propname);
413 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
414 goto error;
415 }
416
417 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
418 &strval, &intval, errbuf) != 0)
419 goto error;
420
421 /*
422 * Perform additional checking for specific properties.
423 */
424 switch (prop) {
425 default:
426 break;
427 case ZPOOL_PROP_VERSION:
428 if (intval < version || intval > SPA_VERSION) {
429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
430 "property '%s' number %d is invalid."),
431 propname, intval);
432 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
433 goto error;
434 }
435 break;
436
437 case ZPOOL_PROP_ASHIFT:
438 if (!flags.create) {
439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440 "property '%s' can only be set at "
441 "creation time"), propname);
442 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
443 goto error;
444 }
445
446 if (intval != 0 && (intval < 9 || intval > 13)) {
447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
448 "property '%s' number %d is invalid."),
449 propname, intval);
450 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
451 goto error;
452 }
453 break;
454
455 case ZPOOL_PROP_BOOTFS:
456 if (flags.create || flags.import) {
457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
458 "property '%s' cannot be set at creation "
459 "or import time"), propname);
460 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
461 goto error;
462 }
463
464 if (version < SPA_VERSION_BOOTFS) {
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 "pool must be upgraded to support "
467 "'%s' property"), propname);
468 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
469 goto error;
470 }
471
472 /*
473 * bootfs property value has to be a dataset name and
474 * the dataset has to be in the same pool as it sets to.
475 */
476 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
477 strval)) {
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
479 "is an invalid name"), strval);
480 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
481 goto error;
482 }
483
484 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
486 "could not open pool '%s'"), poolname);
487 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
488 goto error;
489 }
490 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
491 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
492
493 #if defined(__sun__) || defined(__sun)
494 /*
495 * bootfs property cannot be set on a disk which has
496 * been EFI labeled.
497 */
498 if (pool_uses_efi(nvroot)) {
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
500 "property '%s' not supported on "
501 "EFI labeled devices"), propname);
502 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
503 zpool_close(zhp);
504 goto error;
505 }
506 #endif
507 zpool_close(zhp);
508 break;
509
510 case ZPOOL_PROP_ALTROOT:
511 if (!flags.create && !flags.import) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' can only be set during pool "
514 "creation or import"), propname);
515 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
516 goto error;
517 }
518
519 if (strval[0] != '/') {
520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521 "bad alternate root '%s'"), strval);
522 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
523 goto error;
524 }
525 break;
526
527 case ZPOOL_PROP_CACHEFILE:
528 if (strval[0] == '\0')
529 break;
530
531 if (strcmp(strval, "none") == 0)
532 break;
533
534 if (strval[0] != '/') {
535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536 "property '%s' must be empty, an "
537 "absolute path, or 'none'"), propname);
538 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
539 goto error;
540 }
541
542 slash = strrchr(strval, '/');
543
544 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
545 strcmp(slash, "/..") == 0) {
546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
547 "'%s' is not a valid file"), strval);
548 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
549 goto error;
550 }
551
552 *slash = '\0';
553
554 if (strval[0] != '\0' &&
555 (stat64(strval, &statbuf) != 0 ||
556 !S_ISDIR(statbuf.st_mode))) {
557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558 "'%s' is not a valid directory"),
559 strval);
560 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
561 goto error;
562 }
563
564 *slash = '/';
565 break;
566
567 case ZPOOL_PROP_READONLY:
568 if (!flags.import) {
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "property '%s' can only be set at "
571 "import time"), propname);
572 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
573 goto error;
574 }
575 break;
576 }
577 }
578
579 return (retprops);
580 error:
581 nvlist_free(retprops);
582 return (NULL);
583 }
584
585 /*
586 * Set zpool property : propname=propval.
587 */
588 int
589 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
590 {
591 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
592 int ret = -1;
593 char errbuf[1024];
594 nvlist_t *nvl = NULL;
595 nvlist_t *realprops;
596 uint64_t version;
597 prop_flags_t flags = { 0 };
598
599 (void) snprintf(errbuf, sizeof (errbuf),
600 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
601 zhp->zpool_name);
602
603 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
604 return (no_memory(zhp->zpool_hdl));
605
606 if (nvlist_add_string(nvl, propname, propval) != 0) {
607 nvlist_free(nvl);
608 return (no_memory(zhp->zpool_hdl));
609 }
610
611 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
612 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
613 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
614 nvlist_free(nvl);
615 return (-1);
616 }
617
618 nvlist_free(nvl);
619 nvl = realprops;
620
621 /*
622 * Execute the corresponding ioctl() to set this property.
623 */
624 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
625
626 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
627 nvlist_free(nvl);
628 return (-1);
629 }
630
631 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
632
633 zcmd_free_nvlists(&zc);
634 nvlist_free(nvl);
635
636 if (ret)
637 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
638 else
639 (void) zpool_props_refresh(zhp);
640
641 return (ret);
642 }
643
644 int
645 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
646 {
647 libzfs_handle_t *hdl = zhp->zpool_hdl;
648 zprop_list_t *entry;
649 char buf[ZFS_MAXPROPLEN];
650
651 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
652 return (-1);
653
654 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
655
656 if (entry->pl_fixed)
657 continue;
658
659 if (entry->pl_prop != ZPROP_INVAL &&
660 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
661 NULL) == 0) {
662 if (strlen(buf) > entry->pl_width)
663 entry->pl_width = strlen(buf);
664 }
665 }
666
667 return (0);
668 }
669
670
671 /*
672 * Don't start the slice at the default block of 34; many storage
673 * devices will use a stripe width of 128k, other vendors prefer a 1m
674 * alignment. It is best to play it safe and ensure a 1m alignment
675 * given 512B blocks. When the block size is larger by a power of 2
676 * we will still be 1m aligned. Some devices are sensitive to the
677 * partition ending alignment as well.
678 */
679 #define NEW_START_BLOCK 2048
680 #define PARTITION_END_ALIGNMENT 2048
681
682 /*
683 * Validate the given pool name, optionally putting an extended error message in
684 * 'buf'.
685 */
686 boolean_t
687 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
688 {
689 namecheck_err_t why;
690 char what;
691 int ret;
692
693 ret = pool_namecheck(pool, &why, &what);
694
695 /*
696 * The rules for reserved pool names were extended at a later point.
697 * But we need to support users with existing pools that may now be
698 * invalid. So we only check for this expanded set of names during a
699 * create (or import), and only in userland.
700 */
701 if (ret == 0 && !isopen &&
702 (strncmp(pool, "mirror", 6) == 0 ||
703 strncmp(pool, "raidz", 5) == 0 ||
704 strncmp(pool, "spare", 5) == 0 ||
705 strcmp(pool, "log") == 0)) {
706 if (hdl != NULL)
707 zfs_error_aux(hdl,
708 dgettext(TEXT_DOMAIN, "name is reserved"));
709 return (B_FALSE);
710 }
711
712
713 if (ret != 0) {
714 if (hdl != NULL) {
715 switch (why) {
716 case NAME_ERR_TOOLONG:
717 zfs_error_aux(hdl,
718 dgettext(TEXT_DOMAIN, "name is too long"));
719 break;
720
721 case NAME_ERR_INVALCHAR:
722 zfs_error_aux(hdl,
723 dgettext(TEXT_DOMAIN, "invalid character "
724 "'%c' in pool name"), what);
725 break;
726
727 case NAME_ERR_NOLETTER:
728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
729 "name must begin with a letter"));
730 break;
731
732 case NAME_ERR_RESERVED:
733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
734 "name is reserved"));
735 break;
736
737 case NAME_ERR_DISKLIKE:
738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
739 "pool name is reserved"));
740 break;
741
742 case NAME_ERR_LEADING_SLASH:
743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
744 "leading slash in name"));
745 break;
746
747 case NAME_ERR_EMPTY_COMPONENT:
748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
749 "empty component in name"));
750 break;
751
752 case NAME_ERR_TRAILING_SLASH:
753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
754 "trailing slash in name"));
755 break;
756
757 case NAME_ERR_MULTIPLE_AT:
758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
759 "multiple '@' delimiters in name"));
760 break;
761 case NAME_ERR_NO_AT:
762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
763 "permission set is missing '@'"));
764 break;
765 }
766 }
767 return (B_FALSE);
768 }
769
770 return (B_TRUE);
771 }
772
773 /*
774 * Open a handle to the given pool, even if the pool is currently in the FAULTED
775 * state.
776 */
777 zpool_handle_t *
778 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
779 {
780 zpool_handle_t *zhp;
781 boolean_t missing;
782
783 /*
784 * Make sure the pool name is valid.
785 */
786 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
787 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
788 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
789 pool);
790 return (NULL);
791 }
792
793 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
794 return (NULL);
795
796 zhp->zpool_hdl = hdl;
797 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
798
799 if (zpool_refresh_stats(zhp, &missing) != 0) {
800 zpool_close(zhp);
801 return (NULL);
802 }
803
804 if (missing) {
805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
806 (void) zfs_error_fmt(hdl, EZFS_NOENT,
807 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
808 zpool_close(zhp);
809 return (NULL);
810 }
811
812 return (zhp);
813 }
814
815 /*
816 * Like the above, but silent on error. Used when iterating over pools (because
817 * the configuration cache may be out of date).
818 */
819 int
820 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
821 {
822 zpool_handle_t *zhp;
823 boolean_t missing;
824
825 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
826 return (-1);
827
828 zhp->zpool_hdl = hdl;
829 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
830
831 if (zpool_refresh_stats(zhp, &missing) != 0) {
832 zpool_close(zhp);
833 return (-1);
834 }
835
836 if (missing) {
837 zpool_close(zhp);
838 *ret = NULL;
839 return (0);
840 }
841
842 *ret = zhp;
843 return (0);
844 }
845
846 /*
847 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
848 * state.
849 */
850 zpool_handle_t *
851 zpool_open(libzfs_handle_t *hdl, const char *pool)
852 {
853 zpool_handle_t *zhp;
854
855 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
856 return (NULL);
857
858 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
859 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
860 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
861 zpool_close(zhp);
862 return (NULL);
863 }
864
865 return (zhp);
866 }
867
868 /*
869 * Close the handle. Simply frees the memory associated with the handle.
870 */
871 void
872 zpool_close(zpool_handle_t *zhp)
873 {
874 if (zhp->zpool_config)
875 nvlist_free(zhp->zpool_config);
876 if (zhp->zpool_old_config)
877 nvlist_free(zhp->zpool_old_config);
878 if (zhp->zpool_props)
879 nvlist_free(zhp->zpool_props);
880 free(zhp);
881 }
882
883 /*
884 * Return the name of the pool.
885 */
886 const char *
887 zpool_get_name(zpool_handle_t *zhp)
888 {
889 return (zhp->zpool_name);
890 }
891
892
893 /*
894 * Return the state of the pool (ACTIVE or UNAVAILABLE)
895 */
896 int
897 zpool_get_state(zpool_handle_t *zhp)
898 {
899 return (zhp->zpool_state);
900 }
901
902 /*
903 * Create the named pool, using the provided vdev list. It is assumed
904 * that the consumer has already validated the contents of the nvlist, so we
905 * don't have to worry about error semantics.
906 */
907 int
908 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
909 nvlist_t *props, nvlist_t *fsprops)
910 {
911 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
912 nvlist_t *zc_fsprops = NULL;
913 nvlist_t *zc_props = NULL;
914 char msg[1024];
915 char *altroot;
916 int ret = -1;
917
918 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
919 "cannot create '%s'"), pool);
920
921 if (!zpool_name_valid(hdl, B_FALSE, pool))
922 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
923
924 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
925 return (-1);
926
927 if (props) {
928 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
929
930 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
931 SPA_VERSION_1, flags, msg)) == NULL) {
932 goto create_failed;
933 }
934 }
935
936 if (fsprops) {
937 uint64_t zoned;
938 char *zonestr;
939
940 zoned = ((nvlist_lookup_string(fsprops,
941 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
942 strcmp(zonestr, "on") == 0);
943
944 if ((zc_fsprops = zfs_valid_proplist(hdl,
945 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
946 goto create_failed;
947 }
948 if (!zc_props &&
949 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
950 goto create_failed;
951 }
952 if (nvlist_add_nvlist(zc_props,
953 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
954 goto create_failed;
955 }
956 }
957
958 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
959 goto create_failed;
960
961 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
962
963 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
964
965 zcmd_free_nvlists(&zc);
966 nvlist_free(zc_props);
967 nvlist_free(zc_fsprops);
968
969 switch (errno) {
970 case EBUSY:
971 /*
972 * This can happen if the user has specified the same
973 * device multiple times. We can't reliably detect this
974 * until we try to add it and see we already have a
975 * label. This can also happen under if the device is
976 * part of an active md or lvm device.
977 */
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "one or more vdevs refer to the same device, or one of\n"
980 "the devices is part of an active md or lvm device"));
981 return (zfs_error(hdl, EZFS_BADDEV, msg));
982
983 case EOVERFLOW:
984 /*
985 * This occurs when one of the devices is below
986 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
987 * device was the problem device since there's no
988 * reliable way to determine device size from userland.
989 */
990 {
991 char buf[64];
992
993 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
994
995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
996 "one or more devices is less than the "
997 "minimum size (%s)"), buf);
998 }
999 return (zfs_error(hdl, EZFS_BADDEV, msg));
1000
1001 case ENOSPC:
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "one or more devices is out of space"));
1004 return (zfs_error(hdl, EZFS_BADDEV, msg));
1005
1006 case ENOTBLK:
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "cache device must be a disk or disk slice"));
1009 return (zfs_error(hdl, EZFS_BADDEV, msg));
1010
1011 default:
1012 return (zpool_standard_error(hdl, errno, msg));
1013 }
1014 }
1015
1016 /*
1017 * If this is an alternate root pool, then we automatically set the
1018 * mountpoint of the root dataset to be '/'.
1019 */
1020 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1021 &altroot) == 0) {
1022 zfs_handle_t *zhp;
1023
1024 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1025 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1026 "/") == 0);
1027
1028 zfs_close(zhp);
1029 }
1030
1031 create_failed:
1032 zcmd_free_nvlists(&zc);
1033 nvlist_free(zc_props);
1034 nvlist_free(zc_fsprops);
1035 return (ret);
1036 }
1037
1038 /*
1039 * Destroy the given pool. It is up to the caller to ensure that there are no
1040 * datasets left in the pool.
1041 */
1042 int
1043 zpool_destroy(zpool_handle_t *zhp)
1044 {
1045 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1046 zfs_handle_t *zfp = NULL;
1047 libzfs_handle_t *hdl = zhp->zpool_hdl;
1048 char msg[1024];
1049
1050 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1051 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1052 return (-1);
1053
1054 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1055
1056 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1057 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1058 "cannot destroy '%s'"), zhp->zpool_name);
1059
1060 if (errno == EROFS) {
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "one or more devices is read only"));
1063 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1064 } else {
1065 (void) zpool_standard_error(hdl, errno, msg);
1066 }
1067
1068 if (zfp)
1069 zfs_close(zfp);
1070 return (-1);
1071 }
1072
1073 if (zfp) {
1074 remove_mountpoint(zfp);
1075 zfs_close(zfp);
1076 }
1077
1078 return (0);
1079 }
1080
1081 /*
1082 * Add the given vdevs to the pool. The caller must have already performed the
1083 * necessary verification to ensure that the vdev specification is well-formed.
1084 */
1085 int
1086 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1087 {
1088 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1089 int ret;
1090 libzfs_handle_t *hdl = zhp->zpool_hdl;
1091 char msg[1024];
1092 nvlist_t **spares, **l2cache;
1093 uint_t nspares, nl2cache;
1094
1095 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1096 "cannot add to '%s'"), zhp->zpool_name);
1097
1098 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1099 SPA_VERSION_SPARES &&
1100 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1101 &spares, &nspares) == 0) {
1102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1103 "upgraded to add hot spares"));
1104 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1105 }
1106
1107 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1108 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1109 uint64_t s;
1110
1111 for (s = 0; s < nspares; s++) {
1112 char *path;
1113
1114 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1115 &path) == 0 && pool_uses_efi(spares[s])) {
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "device '%s' contains an EFI label and "
1118 "cannot be used on root pools."),
1119 zpool_vdev_name(hdl, NULL, spares[s],
1120 B_FALSE));
1121 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1122 }
1123 }
1124 }
1125
1126 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1127 SPA_VERSION_L2CACHE &&
1128 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1129 &l2cache, &nl2cache) == 0) {
1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1131 "upgraded to add cache devices"));
1132 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1133 }
1134
1135 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1136 return (-1);
1137 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1138
1139 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1140 switch (errno) {
1141 case EBUSY:
1142 /*
1143 * This can happen if the user has specified the same
1144 * device multiple times. We can't reliably detect this
1145 * until we try to add it and see we already have a
1146 * label.
1147 */
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "one or more vdevs refer to the same device"));
1150 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1151 break;
1152
1153 case EOVERFLOW:
1154 /*
1155 * This occurrs when one of the devices is below
1156 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1157 * device was the problem device since there's no
1158 * reliable way to determine device size from userland.
1159 */
1160 {
1161 char buf[64];
1162
1163 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1164
1165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1166 "device is less than the minimum "
1167 "size (%s)"), buf);
1168 }
1169 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1170 break;
1171
1172 case ENOTSUP:
1173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1174 "pool must be upgraded to add these vdevs"));
1175 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1176 break;
1177
1178 case EDOM:
1179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1180 "root pool can not have multiple vdevs"
1181 " or separate logs"));
1182 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1183 break;
1184
1185 case ENOTBLK:
1186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1187 "cache device must be a disk or disk slice"));
1188 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1189 break;
1190
1191 default:
1192 (void) zpool_standard_error(hdl, errno, msg);
1193 }
1194
1195 ret = -1;
1196 } else {
1197 ret = 0;
1198 }
1199
1200 zcmd_free_nvlists(&zc);
1201
1202 return (ret);
1203 }
1204
1205 /*
1206 * Exports the pool from the system. The caller must ensure that there are no
1207 * mounted datasets in the pool.
1208 */
1209 int
1210 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1211 {
1212 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1213 char msg[1024];
1214
1215 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1216 "cannot export '%s'"), zhp->zpool_name);
1217
1218 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1219 zc.zc_cookie = force;
1220 zc.zc_guid = hardforce;
1221
1222 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1223 switch (errno) {
1224 case EXDEV:
1225 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1226 "use '-f' to override the following errors:\n"
1227 "'%s' has an active shared spare which could be"
1228 " used by other pools once '%s' is exported."),
1229 zhp->zpool_name, zhp->zpool_name);
1230 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1231 msg));
1232 default:
1233 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1234 msg));
1235 }
1236 }
1237
1238 return (0);
1239 }
1240
1241 int
1242 zpool_export(zpool_handle_t *zhp, boolean_t force)
1243 {
1244 return (zpool_export_common(zhp, force, B_FALSE));
1245 }
1246
1247 int
1248 zpool_export_force(zpool_handle_t *zhp)
1249 {
1250 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1251 }
1252
1253 static void
1254 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1255 nvlist_t *config)
1256 {
1257 nvlist_t *nv = NULL;
1258 uint64_t rewindto;
1259 int64_t loss = -1;
1260 struct tm t;
1261 char timestr[128];
1262
1263 if (!hdl->libzfs_printerr || config == NULL)
1264 return;
1265
1266 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1267 return;
1268
1269 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1270 return;
1271 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1272
1273 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1274 strftime(timestr, 128, "%c", &t) != 0) {
1275 if (dryrun) {
1276 (void) printf(dgettext(TEXT_DOMAIN,
1277 "Would be able to return %s "
1278 "to its state as of %s.\n"),
1279 name, timestr);
1280 } else {
1281 (void) printf(dgettext(TEXT_DOMAIN,
1282 "Pool %s returned to its state as of %s.\n"),
1283 name, timestr);
1284 }
1285 if (loss > 120) {
1286 (void) printf(dgettext(TEXT_DOMAIN,
1287 "%s approximately %lld "),
1288 dryrun ? "Would discard" : "Discarded",
1289 ((longlong_t)loss + 30) / 60);
1290 (void) printf(dgettext(TEXT_DOMAIN,
1291 "minutes of transactions.\n"));
1292 } else if (loss > 0) {
1293 (void) printf(dgettext(TEXT_DOMAIN,
1294 "%s approximately %lld "),
1295 dryrun ? "Would discard" : "Discarded",
1296 (longlong_t)loss);
1297 (void) printf(dgettext(TEXT_DOMAIN,
1298 "seconds of transactions.\n"));
1299 }
1300 }
1301 }
1302
1303 void
1304 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1305 nvlist_t *config)
1306 {
1307 nvlist_t *nv = NULL;
1308 int64_t loss = -1;
1309 uint64_t edata = UINT64_MAX;
1310 uint64_t rewindto;
1311 struct tm t;
1312 char timestr[128];
1313
1314 if (!hdl->libzfs_printerr)
1315 return;
1316
1317 if (reason >= 0)
1318 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1319 else
1320 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1321
1322 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1323 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1324 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1325 goto no_info;
1326
1327 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1328 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1329 &edata);
1330
1331 (void) printf(dgettext(TEXT_DOMAIN,
1332 "Recovery is possible, but will result in some data loss.\n"));
1333
1334 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1335 strftime(timestr, 128, "%c", &t) != 0) {
1336 (void) printf(dgettext(TEXT_DOMAIN,
1337 "\tReturning the pool to its state as of %s\n"
1338 "\tshould correct the problem. "),
1339 timestr);
1340 } else {
1341 (void) printf(dgettext(TEXT_DOMAIN,
1342 "\tReverting the pool to an earlier state "
1343 "should correct the problem.\n\t"));
1344 }
1345
1346 if (loss > 120) {
1347 (void) printf(dgettext(TEXT_DOMAIN,
1348 "Approximately %lld minutes of data\n"
1349 "\tmust be discarded, irreversibly. "),
1350 ((longlong_t)loss + 30) / 60);
1351 } else if (loss > 0) {
1352 (void) printf(dgettext(TEXT_DOMAIN,
1353 "Approximately %lld seconds of data\n"
1354 "\tmust be discarded, irreversibly. "),
1355 (longlong_t)loss);
1356 }
1357 if (edata != 0 && edata != UINT64_MAX) {
1358 if (edata == 1) {
1359 (void) printf(dgettext(TEXT_DOMAIN,
1360 "After rewind, at least\n"
1361 "\tone persistent user-data error will remain. "));
1362 } else {
1363 (void) printf(dgettext(TEXT_DOMAIN,
1364 "After rewind, several\n"
1365 "\tpersistent user-data errors will remain. "));
1366 }
1367 }
1368 (void) printf(dgettext(TEXT_DOMAIN,
1369 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1370 reason >= 0 ? "clear" : "import", name);
1371
1372 (void) printf(dgettext(TEXT_DOMAIN,
1373 "A scrub of the pool\n"
1374 "\tis strongly recommended after recovery.\n"));
1375 return;
1376
1377 no_info:
1378 (void) printf(dgettext(TEXT_DOMAIN,
1379 "Destroy and re-create the pool from\n\ta backup source.\n"));
1380 }
1381
1382 /*
1383 * zpool_import() is a contracted interface. Should be kept the same
1384 * if possible.
1385 *
1386 * Applications should use zpool_import_props() to import a pool with
1387 * new properties value to be set.
1388 */
1389 int
1390 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1391 char *altroot)
1392 {
1393 nvlist_t *props = NULL;
1394 int ret;
1395
1396 if (altroot != NULL) {
1397 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1398 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1399 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1400 newname));
1401 }
1402
1403 if (nvlist_add_string(props,
1404 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1405 nvlist_add_string(props,
1406 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1407 nvlist_free(props);
1408 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1409 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1410 newname));
1411 }
1412 }
1413
1414 ret = zpool_import_props(hdl, config, newname, props,
1415 ZFS_IMPORT_NORMAL);
1416 if (props)
1417 nvlist_free(props);
1418 return (ret);
1419 }
1420
1421 static void
1422 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1423 int indent)
1424 {
1425 nvlist_t **child;
1426 uint_t c, children;
1427 char *vname;
1428 uint64_t is_log = 0;
1429
1430 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1431 &is_log);
1432
1433 if (name != NULL)
1434 (void) printf("\t%*s%s%s\n", indent, "", name,
1435 is_log ? " [log]" : "");
1436
1437 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1438 &child, &children) != 0)
1439 return;
1440
1441 for (c = 0; c < children; c++) {
1442 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1443 print_vdev_tree(hdl, vname, child[c], indent + 2);
1444 free(vname);
1445 }
1446 }
1447
1448 /*
1449 * Import the given pool using the known configuration and a list of
1450 * properties to be set. The configuration should have come from
1451 * zpool_find_import(). The 'newname' parameters control whether the pool
1452 * is imported with a different name.
1453 */
1454 int
1455 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1456 nvlist_t *props, int flags)
1457 {
1458 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1459 zpool_rewind_policy_t policy;
1460 nvlist_t *nv = NULL;
1461 nvlist_t *nvinfo = NULL;
1462 nvlist_t *missing = NULL;
1463 char *thename;
1464 char *origname;
1465 int ret;
1466 int error = 0;
1467 char errbuf[1024];
1468
1469 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1470 &origname) == 0);
1471
1472 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1473 "cannot import pool '%s'"), origname);
1474
1475 if (newname != NULL) {
1476 if (!zpool_name_valid(hdl, B_FALSE, newname))
1477 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1478 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1479 newname));
1480 thename = (char *)newname;
1481 } else {
1482 thename = origname;
1483 }
1484
1485 if (props) {
1486 uint64_t version;
1487 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1488
1489 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1490 &version) == 0);
1491
1492 if ((props = zpool_valid_proplist(hdl, origname,
1493 props, version, flags, errbuf)) == NULL) {
1494 return (-1);
1495 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1496 nvlist_free(props);
1497 return (-1);
1498 }
1499 }
1500
1501 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1502
1503 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1504 &zc.zc_guid) == 0);
1505
1506 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1507 nvlist_free(props);
1508 return (-1);
1509 }
1510 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1511 nvlist_free(props);
1512 return (-1);
1513 }
1514
1515 zc.zc_cookie = flags;
1516 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1517 errno == ENOMEM) {
1518 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1519 zcmd_free_nvlists(&zc);
1520 return (-1);
1521 }
1522 }
1523 if (ret != 0)
1524 error = errno;
1525
1526 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1527 zpool_get_rewind_policy(config, &policy);
1528
1529 if (error) {
1530 char desc[1024];
1531
1532 /*
1533 * Dry-run failed, but we print out what success
1534 * looks like if we found a best txg
1535 */
1536 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1537 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1538 B_TRUE, nv);
1539 nvlist_free(nv);
1540 return (-1);
1541 }
1542
1543 if (newname == NULL)
1544 (void) snprintf(desc, sizeof (desc),
1545 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1546 thename);
1547 else
1548 (void) snprintf(desc, sizeof (desc),
1549 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1550 origname, thename);
1551
1552 switch (error) {
1553 case ENOTSUP:
1554 /*
1555 * Unsupported version.
1556 */
1557 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1558 break;
1559
1560 case EINVAL:
1561 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1562 break;
1563
1564 case EROFS:
1565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566 "one or more devices is read only"));
1567 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1568 break;
1569
1570 case ENXIO:
1571 if (nv && nvlist_lookup_nvlist(nv,
1572 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1573 nvlist_lookup_nvlist(nvinfo,
1574 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1575 (void) printf(dgettext(TEXT_DOMAIN,
1576 "The devices below are missing, use "
1577 "'-m' to import the pool anyway:\n"));
1578 print_vdev_tree(hdl, NULL, missing, 2);
1579 (void) printf("\n");
1580 }
1581 (void) zpool_standard_error(hdl, error, desc);
1582 break;
1583
1584 case EEXIST:
1585 (void) zpool_standard_error(hdl, error, desc);
1586 break;
1587
1588 default:
1589 (void) zpool_standard_error(hdl, error, desc);
1590 zpool_explain_recover(hdl,
1591 newname ? origname : thename, -error, nv);
1592 break;
1593 }
1594
1595 nvlist_free(nv);
1596 ret = -1;
1597 } else {
1598 zpool_handle_t *zhp;
1599
1600 /*
1601 * This should never fail, but play it safe anyway.
1602 */
1603 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1604 ret = -1;
1605 else if (zhp != NULL)
1606 zpool_close(zhp);
1607 if (policy.zrp_request &
1608 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1609 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1610 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1611 }
1612 nvlist_free(nv);
1613 return (0);
1614 }
1615
1616 zcmd_free_nvlists(&zc);
1617 nvlist_free(props);
1618
1619 return (ret);
1620 }
1621
1622 /*
1623 * Scan the pool.
1624 */
1625 int
1626 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1627 {
1628 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1629 char msg[1024];
1630 libzfs_handle_t *hdl = zhp->zpool_hdl;
1631
1632 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1633 zc.zc_cookie = func;
1634
1635 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1636 (errno == ENOENT && func != POOL_SCAN_NONE))
1637 return (0);
1638
1639 if (func == POOL_SCAN_SCRUB) {
1640 (void) snprintf(msg, sizeof (msg),
1641 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1642 } else if (func == POOL_SCAN_NONE) {
1643 (void) snprintf(msg, sizeof (msg),
1644 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1645 zc.zc_name);
1646 } else {
1647 assert(!"unexpected result");
1648 }
1649
1650 if (errno == EBUSY) {
1651 nvlist_t *nvroot;
1652 pool_scan_stat_t *ps = NULL;
1653 uint_t psc;
1654
1655 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1656 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1657 (void) nvlist_lookup_uint64_array(nvroot,
1658 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1659 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1660 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1661 else
1662 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1663 } else if (errno == ENOENT) {
1664 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1665 } else {
1666 return (zpool_standard_error(hdl, errno, msg));
1667 }
1668 }
1669
1670 /*
1671 * Find a vdev that matches the search criteria specified. We use the
1672 * the nvpair name to determine how we should look for the device.
1673 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1674 * spare; but FALSE if its an INUSE spare.
1675 */
1676 static nvlist_t *
1677 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1678 boolean_t *l2cache, boolean_t *log)
1679 {
1680 uint_t c, children;
1681 nvlist_t **child;
1682 nvlist_t *ret;
1683 uint64_t is_log;
1684 char *srchkey;
1685 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1686
1687 /* Nothing to look for */
1688 if (search == NULL || pair == NULL)
1689 return (NULL);
1690
1691 /* Obtain the key we will use to search */
1692 srchkey = nvpair_name(pair);
1693
1694 switch (nvpair_type(pair)) {
1695 case DATA_TYPE_UINT64:
1696 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1697 uint64_t srchval, theguid;
1698
1699 verify(nvpair_value_uint64(pair, &srchval) == 0);
1700 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1701 &theguid) == 0);
1702 if (theguid == srchval)
1703 return (nv);
1704 }
1705 break;
1706
1707 case DATA_TYPE_STRING: {
1708 char *srchval, *val;
1709
1710 verify(nvpair_value_string(pair, &srchval) == 0);
1711 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1712 break;
1713
1714 /*
1715 * Search for the requested value. Special cases:
1716 *
1717 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1718 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1719 * from the user, but included in the string, so this matches around
1720 * it.
1721 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1722 *
1723 * Otherwise, all other searches are simple string compares.
1724 */
1725 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1726 uint64_t wholedisk = 0;
1727
1728 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1729 &wholedisk);
1730 if (wholedisk) {
1731 char buf[MAXPATHLEN];
1732
1733 zfs_append_partition(srchval, buf, sizeof (buf));
1734 if (strcmp(val, buf) == 0)
1735 return (nv);
1736
1737 break;
1738 }
1739 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1740 char *type, *idx, *end, *p;
1741 uint64_t id, vdev_id;
1742
1743 /*
1744 * Determine our vdev type, keeping in mind
1745 * that the srchval is composed of a type and
1746 * vdev id pair (i.e. mirror-4).
1747 */
1748 if ((type = strdup(srchval)) == NULL)
1749 return (NULL);
1750
1751 if ((p = strrchr(type, '-')) == NULL) {
1752 free(type);
1753 break;
1754 }
1755 idx = p + 1;
1756 *p = '\0';
1757
1758 /*
1759 * If the types don't match then keep looking.
1760 */
1761 if (strncmp(val, type, strlen(val)) != 0) {
1762 free(type);
1763 break;
1764 }
1765
1766 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1767 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1768 strncmp(type, VDEV_TYPE_MIRROR,
1769 strlen(VDEV_TYPE_MIRROR)) == 0);
1770 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1771 &id) == 0);
1772
1773 errno = 0;
1774 vdev_id = strtoull(idx, &end, 10);
1775
1776 free(type);
1777 if (errno != 0)
1778 return (NULL);
1779
1780 /*
1781 * Now verify that we have the correct vdev id.
1782 */
1783 if (vdev_id == id)
1784 return (nv);
1785 }
1786
1787 /*
1788 * Common case
1789 */
1790 if (strcmp(srchval, val) == 0)
1791 return (nv);
1792 break;
1793 }
1794
1795 default:
1796 break;
1797 }
1798
1799 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1800 &child, &children) != 0)
1801 return (NULL);
1802
1803 for (c = 0; c < children; c++) {
1804 if ((ret = vdev_to_nvlist_iter(child[c], search,
1805 avail_spare, l2cache, NULL)) != NULL) {
1806 /*
1807 * The 'is_log' value is only set for the toplevel
1808 * vdev, not the leaf vdevs. So we always lookup the
1809 * log device from the root of the vdev tree (where
1810 * 'log' is non-NULL).
1811 */
1812 if (log != NULL &&
1813 nvlist_lookup_uint64(child[c],
1814 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1815 is_log) {
1816 *log = B_TRUE;
1817 }
1818 return (ret);
1819 }
1820 }
1821
1822 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1823 &child, &children) == 0) {
1824 for (c = 0; c < children; c++) {
1825 if ((ret = vdev_to_nvlist_iter(child[c], search,
1826 avail_spare, l2cache, NULL)) != NULL) {
1827 *avail_spare = B_TRUE;
1828 return (ret);
1829 }
1830 }
1831 }
1832
1833 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1834 &child, &children) == 0) {
1835 for (c = 0; c < children; c++) {
1836 if ((ret = vdev_to_nvlist_iter(child[c], search,
1837 avail_spare, l2cache, NULL)) != NULL) {
1838 *l2cache = B_TRUE;
1839 return (ret);
1840 }
1841 }
1842 }
1843
1844 return (NULL);
1845 }
1846
1847 /*
1848 * Given a physical path (minus the "/devices" prefix), find the
1849 * associated vdev.
1850 */
1851 nvlist_t *
1852 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1853 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1854 {
1855 nvlist_t *search, *nvroot, *ret;
1856
1857 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1858 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1859
1860 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1861 &nvroot) == 0);
1862
1863 *avail_spare = B_FALSE;
1864 *l2cache = B_FALSE;
1865 if (log != NULL)
1866 *log = B_FALSE;
1867 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1868 nvlist_free(search);
1869
1870 return (ret);
1871 }
1872
1873 /*
1874 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1875 */
1876 boolean_t
1877 zpool_vdev_is_interior(const char *name)
1878 {
1879 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1880 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1881 return (B_TRUE);
1882 return (B_FALSE);
1883 }
1884
1885 nvlist_t *
1886 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1887 boolean_t *l2cache, boolean_t *log)
1888 {
1889 char buf[MAXPATHLEN];
1890 char *end;
1891 nvlist_t *nvroot, *search, *ret;
1892 uint64_t guid;
1893
1894 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1895
1896 guid = strtoull(path, &end, 10);
1897 if (guid != 0 && *end == '\0') {
1898 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1899 } else if (zpool_vdev_is_interior(path)) {
1900 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1901 } else if (path[0] != '/') {
1902 if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
1903 nvlist_free(search);
1904 return (NULL);
1905 }
1906 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1907 } else {
1908 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1909 }
1910
1911 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1912 &nvroot) == 0);
1913
1914 *avail_spare = B_FALSE;
1915 *l2cache = B_FALSE;
1916 if (log != NULL)
1917 *log = B_FALSE;
1918 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1919 nvlist_free(search);
1920
1921 return (ret);
1922 }
1923
1924 static int
1925 vdev_online(nvlist_t *nv)
1926 {
1927 uint64_t ival;
1928
1929 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1930 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1931 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1932 return (0);
1933
1934 return (1);
1935 }
1936
1937 /*
1938 * Helper function for zpool_get_physpaths().
1939 */
1940 static int
1941 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1942 size_t *bytes_written)
1943 {
1944 size_t bytes_left, pos, rsz;
1945 char *tmppath;
1946 const char *format;
1947
1948 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1949 &tmppath) != 0)
1950 return (EZFS_NODEVICE);
1951
1952 pos = *bytes_written;
1953 bytes_left = physpath_size - pos;
1954 format = (pos == 0) ? "%s" : " %s";
1955
1956 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1957 *bytes_written += rsz;
1958
1959 if (rsz >= bytes_left) {
1960 /* if physpath was not copied properly, clear it */
1961 if (bytes_left != 0) {
1962 physpath[pos] = 0;
1963 }
1964 return (EZFS_NOSPC);
1965 }
1966 return (0);
1967 }
1968
1969 static int
1970 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1971 size_t *rsz, boolean_t is_spare)
1972 {
1973 char *type;
1974 int ret;
1975
1976 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1977 return (EZFS_INVALCONFIG);
1978
1979 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1980 /*
1981 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1982 * For a spare vdev, we only want to boot from the active
1983 * spare device.
1984 */
1985 if (is_spare) {
1986 uint64_t spare = 0;
1987 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1988 &spare);
1989 if (!spare)
1990 return (EZFS_INVALCONFIG);
1991 }
1992
1993 if (vdev_online(nv)) {
1994 if ((ret = vdev_get_one_physpath(nv, physpath,
1995 phypath_size, rsz)) != 0)
1996 return (ret);
1997 }
1998 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1999 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2000 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2001 nvlist_t **child;
2002 uint_t count;
2003 int i, ret;
2004
2005 if (nvlist_lookup_nvlist_array(nv,
2006 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2007 return (EZFS_INVALCONFIG);
2008
2009 for (i = 0; i < count; i++) {
2010 ret = vdev_get_physpaths(child[i], physpath,
2011 phypath_size, rsz, is_spare);
2012 if (ret == EZFS_NOSPC)
2013 return (ret);
2014 }
2015 }
2016
2017 return (EZFS_POOL_INVALARG);
2018 }
2019
2020 /*
2021 * Get phys_path for a root pool config.
2022 * Return 0 on success; non-zero on failure.
2023 */
2024 static int
2025 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2026 {
2027 size_t rsz;
2028 nvlist_t *vdev_root;
2029 nvlist_t **child;
2030 uint_t count;
2031 char *type;
2032
2033 rsz = 0;
2034
2035 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2036 &vdev_root) != 0)
2037 return (EZFS_INVALCONFIG);
2038
2039 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2040 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2041 &child, &count) != 0)
2042 return (EZFS_INVALCONFIG);
2043
2044 /*
2045 * root pool can not have EFI labeled disks and can only have
2046 * a single top-level vdev.
2047 */
2048 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2049 pool_uses_efi(vdev_root))
2050 return (EZFS_POOL_INVALARG);
2051
2052 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2053 B_FALSE);
2054
2055 /* No online devices */
2056 if (rsz == 0)
2057 return (EZFS_NODEVICE);
2058
2059 return (0);
2060 }
2061
2062 /*
2063 * Get phys_path for a root pool
2064 * Return 0 on success; non-zero on failure.
2065 */
2066 int
2067 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2068 {
2069 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2070 phypath_size));
2071 }
2072
2073 /*
2074 * If the device has being dynamically expanded then we need to relabel
2075 * the disk to use the new unallocated space.
2076 */
2077 static int
2078 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path)
2079 {
2080 char errbuf[1024];
2081 int fd, error;
2082
2083 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2085 "relabel '%s': unable to open device"), path);
2086 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2087 }
2088
2089 /*
2090 * It's possible that we might encounter an error if the device
2091 * does not have any unallocated space left. If so, we simply
2092 * ignore that error and continue on.
2093 */
2094 error = efi_use_whole_disk(fd);
2095 (void) close(fd);
2096 if (error && error != VT_ENOSPC) {
2097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2098 "relabel '%s': unable to read disk capacity"), path);
2099 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2100 }
2101 return (0);
2102 }
2103
2104 /*
2105 * Bring the specified vdev online. The 'flags' parameter is a set of the
2106 * ZFS_ONLINE_* flags.
2107 */
2108 int
2109 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2110 vdev_state_t *newstate)
2111 {
2112 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2113 char msg[1024];
2114 nvlist_t *tgt;
2115 boolean_t avail_spare, l2cache, islog;
2116 libzfs_handle_t *hdl = zhp->zpool_hdl;
2117
2118 if (flags & ZFS_ONLINE_EXPAND) {
2119 (void) snprintf(msg, sizeof (msg),
2120 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2121 } else {
2122 (void) snprintf(msg, sizeof (msg),
2123 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2124 }
2125
2126 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2127 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2128 &islog)) == NULL)
2129 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2130
2131 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2132
2133 if (avail_spare)
2134 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2135
2136 if (flags & ZFS_ONLINE_EXPAND ||
2137 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2138 char *pathname = NULL;
2139 uint64_t wholedisk = 0;
2140
2141 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2142 &wholedisk);
2143 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2144 &pathname) == 0);
2145
2146 /*
2147 * XXX - L2ARC 1.0 devices can't support expansion.
2148 */
2149 if (l2cache) {
2150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2151 "cannot expand cache devices"));
2152 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2153 }
2154
2155 if (wholedisk) {
2156 pathname += strlen(DISK_ROOT) + 1;
2157 (void) zpool_relabel_disk(hdl, pathname);
2158 }
2159 }
2160
2161 zc.zc_cookie = VDEV_STATE_ONLINE;
2162 zc.zc_obj = flags;
2163
2164 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2165 if (errno == EINVAL) {
2166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2167 "from this pool into a new one. Use '%s' "
2168 "instead"), "zpool detach");
2169 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2170 }
2171 return (zpool_standard_error(hdl, errno, msg));
2172 }
2173
2174 *newstate = zc.zc_cookie;
2175 return (0);
2176 }
2177
2178 /*
2179 * Take the specified vdev offline
2180 */
2181 int
2182 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2183 {
2184 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2185 char msg[1024];
2186 nvlist_t *tgt;
2187 boolean_t avail_spare, l2cache;
2188 libzfs_handle_t *hdl = zhp->zpool_hdl;
2189
2190 (void) snprintf(msg, sizeof (msg),
2191 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2192
2193 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2194 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2195 NULL)) == NULL)
2196 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2197
2198 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2199
2200 if (avail_spare)
2201 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2202
2203 zc.zc_cookie = VDEV_STATE_OFFLINE;
2204 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2205
2206 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2207 return (0);
2208
2209 switch (errno) {
2210 case EBUSY:
2211
2212 /*
2213 * There are no other replicas of this device.
2214 */
2215 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2216
2217 case EEXIST:
2218 /*
2219 * The log device has unplayed logs
2220 */
2221 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2222
2223 default:
2224 return (zpool_standard_error(hdl, errno, msg));
2225 }
2226 }
2227
2228 /*
2229 * Mark the given vdev faulted.
2230 */
2231 int
2232 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2233 {
2234 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2235 char msg[1024];
2236 libzfs_handle_t *hdl = zhp->zpool_hdl;
2237
2238 (void) snprintf(msg, sizeof (msg),
2239 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2240
2241 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2242 zc.zc_guid = guid;
2243 zc.zc_cookie = VDEV_STATE_FAULTED;
2244 zc.zc_obj = aux;
2245
2246 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2247 return (0);
2248
2249 switch (errno) {
2250 case EBUSY:
2251
2252 /*
2253 * There are no other replicas of this device.
2254 */
2255 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2256
2257 default:
2258 return (zpool_standard_error(hdl, errno, msg));
2259 }
2260
2261 }
2262
2263 /*
2264 * Mark the given vdev degraded.
2265 */
2266 int
2267 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2268 {
2269 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2270 char msg[1024];
2271 libzfs_handle_t *hdl = zhp->zpool_hdl;
2272
2273 (void) snprintf(msg, sizeof (msg),
2274 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2275
2276 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2277 zc.zc_guid = guid;
2278 zc.zc_cookie = VDEV_STATE_DEGRADED;
2279 zc.zc_obj = aux;
2280
2281 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2282 return (0);
2283
2284 return (zpool_standard_error(hdl, errno, msg));
2285 }
2286
2287 /*
2288 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2289 * a hot spare.
2290 */
2291 static boolean_t
2292 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2293 {
2294 nvlist_t **child;
2295 uint_t c, children;
2296 char *type;
2297
2298 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2299 &children) == 0) {
2300 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2301 &type) == 0);
2302
2303 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2304 children == 2 && child[which] == tgt)
2305 return (B_TRUE);
2306
2307 for (c = 0; c < children; c++)
2308 if (is_replacing_spare(child[c], tgt, which))
2309 return (B_TRUE);
2310 }
2311
2312 return (B_FALSE);
2313 }
2314
2315 /*
2316 * Attach new_disk (fully described by nvroot) to old_disk.
2317 * If 'replacing' is specified, the new disk will replace the old one.
2318 */
2319 int
2320 zpool_vdev_attach(zpool_handle_t *zhp,
2321 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2322 {
2323 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2324 char msg[1024];
2325 int ret;
2326 nvlist_t *tgt;
2327 boolean_t avail_spare, l2cache, islog;
2328 uint64_t val;
2329 char *newname;
2330 nvlist_t **child;
2331 uint_t children;
2332 nvlist_t *config_root;
2333 libzfs_handle_t *hdl = zhp->zpool_hdl;
2334 boolean_t rootpool = pool_is_bootable(zhp);
2335
2336 if (replacing)
2337 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2338 "cannot replace %s with %s"), old_disk, new_disk);
2339 else
2340 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2341 "cannot attach %s to %s"), new_disk, old_disk);
2342
2343 /*
2344 * If this is a root pool, make sure that we're not attaching an
2345 * EFI labeled device.
2346 */
2347 if (rootpool && pool_uses_efi(nvroot)) {
2348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2349 "EFI labeled devices are not supported on root pools."));
2350 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2351 }
2352
2353 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2354 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2355 &islog)) == 0)
2356 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2357
2358 if (avail_spare)
2359 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2360
2361 if (l2cache)
2362 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2363
2364 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2365 zc.zc_cookie = replacing;
2366
2367 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2368 &child, &children) != 0 || children != 1) {
2369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2370 "new device must be a single disk"));
2371 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2372 }
2373
2374 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2375 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2376
2377 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2378 return (-1);
2379
2380 /*
2381 * If the target is a hot spare that has been swapped in, we can only
2382 * replace it with another hot spare.
2383 */
2384 if (replacing &&
2385 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2386 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2387 NULL) == NULL || !avail_spare) &&
2388 is_replacing_spare(config_root, tgt, 1)) {
2389 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2390 "can only be replaced by another hot spare"));
2391 free(newname);
2392 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2393 }
2394
2395 free(newname);
2396
2397 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2398 return (-1);
2399
2400 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2401
2402 zcmd_free_nvlists(&zc);
2403
2404 if (ret == 0) {
2405 if (rootpool) {
2406 /*
2407 * XXX need a better way to prevent user from
2408 * booting up a half-baked vdev.
2409 */
2410 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2411 "sure to wait until resilver is done "
2412 "before rebooting.\n"));
2413 }
2414 return (0);
2415 }
2416
2417 switch (errno) {
2418 case ENOTSUP:
2419 /*
2420 * Can't attach to or replace this type of vdev.
2421 */
2422 if (replacing) {
2423 uint64_t version = zpool_get_prop_int(zhp,
2424 ZPOOL_PROP_VERSION, NULL);
2425
2426 if (islog)
2427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2428 "cannot replace a log with a spare"));
2429 else if (version >= SPA_VERSION_MULTI_REPLACE)
2430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2431 "already in replacing/spare config; wait "
2432 "for completion or use 'zpool detach'"));
2433 else
2434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2435 "cannot replace a replacing device"));
2436 } else {
2437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2438 "can only attach to mirrors and top-level "
2439 "disks"));
2440 }
2441 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2442 break;
2443
2444 case EINVAL:
2445 /*
2446 * The new device must be a single disk.
2447 */
2448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2449 "new device must be a single disk"));
2450 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2451 break;
2452
2453 case EBUSY:
2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2455 new_disk);
2456 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2457 break;
2458
2459 case EOVERFLOW:
2460 /*
2461 * The new device is too small.
2462 */
2463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2464 "device is too small"));
2465 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2466 break;
2467
2468 case EDOM:
2469 /*
2470 * The new device has a different alignment requirement.
2471 */
2472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2473 "devices have different sector alignment"));
2474 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2475 break;
2476
2477 case ENAMETOOLONG:
2478 /*
2479 * The resulting top-level vdev spec won't fit in the label.
2480 */
2481 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2482 break;
2483
2484 default:
2485 (void) zpool_standard_error(hdl, errno, msg);
2486 }
2487
2488 return (-1);
2489 }
2490
2491 /*
2492 * Detach the specified device.
2493 */
2494 int
2495 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2496 {
2497 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2498 char msg[1024];
2499 nvlist_t *tgt;
2500 boolean_t avail_spare, l2cache;
2501 libzfs_handle_t *hdl = zhp->zpool_hdl;
2502
2503 (void) snprintf(msg, sizeof (msg),
2504 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2505
2506 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2507 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2508 NULL)) == 0)
2509 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2510
2511 if (avail_spare)
2512 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2513
2514 if (l2cache)
2515 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2516
2517 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2518
2519 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2520 return (0);
2521
2522 switch (errno) {
2523
2524 case ENOTSUP:
2525 /*
2526 * Can't detach from this type of vdev.
2527 */
2528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2529 "applicable to mirror and replacing vdevs"));
2530 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2531 break;
2532
2533 case EBUSY:
2534 /*
2535 * There are no other replicas of this device.
2536 */
2537 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2538 break;
2539
2540 default:
2541 (void) zpool_standard_error(hdl, errno, msg);
2542 }
2543
2544 return (-1);
2545 }
2546
2547 /*
2548 * Find a mirror vdev in the source nvlist.
2549 *
2550 * The mchild array contains a list of disks in one of the top-level mirrors
2551 * of the source pool. The schild array contains a list of disks that the
2552 * user specified on the command line. We loop over the mchild array to
2553 * see if any entry in the schild array matches.
2554 *
2555 * If a disk in the mchild array is found in the schild array, we return
2556 * the index of that entry. Otherwise we return -1.
2557 */
2558 static int
2559 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2560 nvlist_t **schild, uint_t schildren)
2561 {
2562 uint_t mc;
2563
2564 for (mc = 0; mc < mchildren; mc++) {
2565 uint_t sc;
2566 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2567 mchild[mc], B_FALSE);
2568
2569 for (sc = 0; sc < schildren; sc++) {
2570 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2571 schild[sc], B_FALSE);
2572 boolean_t result = (strcmp(mpath, spath) == 0);
2573
2574 free(spath);
2575 if (result) {
2576 free(mpath);
2577 return (mc);
2578 }
2579 }
2580
2581 free(mpath);
2582 }
2583
2584 return (-1);
2585 }
2586
2587 /*
2588 * Split a mirror pool. If newroot points to null, then a new nvlist
2589 * is generated and it is the responsibility of the caller to free it.
2590 */
2591 int
2592 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2593 nvlist_t *props, splitflags_t flags)
2594 {
2595 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2596 char msg[1024];
2597 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2598 nvlist_t **varray = NULL, *zc_props = NULL;
2599 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2600 libzfs_handle_t *hdl = zhp->zpool_hdl;
2601 uint64_t vers;
2602 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2603 int retval = 0;
2604
2605 (void) snprintf(msg, sizeof (msg),
2606 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2607
2608 if (!zpool_name_valid(hdl, B_FALSE, newname))
2609 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2610
2611 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2612 (void) fprintf(stderr, gettext("Internal error: unable to "
2613 "retrieve pool configuration\n"));
2614 return (-1);
2615 }
2616
2617 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2618 == 0);
2619 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2620
2621 if (props) {
2622 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2623 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2624 props, vers, flags, msg)) == NULL)
2625 return (-1);
2626 }
2627
2628 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2629 &children) != 0) {
2630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2631 "Source pool is missing vdev tree"));
2632 if (zc_props)
2633 nvlist_free(zc_props);
2634 return (-1);
2635 }
2636
2637 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2638 vcount = 0;
2639
2640 if (*newroot == NULL ||
2641 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2642 &newchild, &newchildren) != 0)
2643 newchildren = 0;
2644
2645 for (c = 0; c < children; c++) {
2646 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2647 char *type;
2648 nvlist_t **mchild, *vdev;
2649 uint_t mchildren;
2650 int entry;
2651
2652 /*
2653 * Unlike cache & spares, slogs are stored in the
2654 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2655 */
2656 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2657 &is_log);
2658 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2659 &is_hole);
2660 if (is_log || is_hole) {
2661 /*
2662 * Create a hole vdev and put it in the config.
2663 */
2664 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2665 goto out;
2666 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2667 VDEV_TYPE_HOLE) != 0)
2668 goto out;
2669 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2670 1) != 0)
2671 goto out;
2672 if (lastlog == 0)
2673 lastlog = vcount;
2674 varray[vcount++] = vdev;
2675 continue;
2676 }
2677 lastlog = 0;
2678 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2679 == 0);
2680 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2682 "Source pool must be composed only of mirrors\n"));
2683 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2684 goto out;
2685 }
2686
2687 verify(nvlist_lookup_nvlist_array(child[c],
2688 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2689
2690 /* find or add an entry for this top-level vdev */
2691 if (newchildren > 0 &&
2692 (entry = find_vdev_entry(zhp, mchild, mchildren,
2693 newchild, newchildren)) >= 0) {
2694 /* We found a disk that the user specified. */
2695 vdev = mchild[entry];
2696 ++found;
2697 } else {
2698 /* User didn't specify a disk for this vdev. */
2699 vdev = mchild[mchildren - 1];
2700 }
2701
2702 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2703 goto out;
2704 }
2705
2706 /* did we find every disk the user specified? */
2707 if (found != newchildren) {
2708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2709 "include at most one disk from each mirror"));
2710 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2711 goto out;
2712 }
2713
2714 /* Prepare the nvlist for populating. */
2715 if (*newroot == NULL) {
2716 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2717 goto out;
2718 freelist = B_TRUE;
2719 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2720 VDEV_TYPE_ROOT) != 0)
2721 goto out;
2722 } else {
2723 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2724 }
2725
2726 /* Add all the children we found */
2727 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2728 lastlog == 0 ? vcount : lastlog) != 0)
2729 goto out;
2730
2731 /*
2732 * If we're just doing a dry run, exit now with success.
2733 */
2734 if (flags.dryrun) {
2735 memory_err = B_FALSE;
2736 freelist = B_FALSE;
2737 goto out;
2738 }
2739
2740 /* now build up the config list & call the ioctl */
2741 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2742 goto out;
2743
2744 if (nvlist_add_nvlist(newconfig,
2745 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2746 nvlist_add_string(newconfig,
2747 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2748 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2749 goto out;
2750
2751 /*
2752 * The new pool is automatically part of the namespace unless we
2753 * explicitly export it.
2754 */
2755 if (!flags.import)
2756 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2757 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2758 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2759 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2760 goto out;
2761 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2762 goto out;
2763
2764 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2765 retval = zpool_standard_error(hdl, errno, msg);
2766 goto out;
2767 }
2768
2769 freelist = B_FALSE;
2770 memory_err = B_FALSE;
2771
2772 out:
2773 if (varray != NULL) {
2774 int v;
2775
2776 for (v = 0; v < vcount; v++)
2777 nvlist_free(varray[v]);
2778 free(varray);
2779 }
2780 zcmd_free_nvlists(&zc);
2781 if (zc_props)
2782 nvlist_free(zc_props);
2783 if (newconfig)
2784 nvlist_free(newconfig);
2785 if (freelist) {
2786 nvlist_free(*newroot);
2787 *newroot = NULL;
2788 }
2789
2790 if (retval != 0)
2791 return (retval);
2792
2793 if (memory_err)
2794 return (no_memory(hdl));
2795
2796 return (0);
2797 }
2798
2799 /*
2800 * Remove the given device. Currently, this is supported only for hot spares
2801 * and level 2 cache devices.
2802 */
2803 int
2804 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2805 {
2806 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2807 char msg[1024];
2808 nvlist_t *tgt;
2809 boolean_t avail_spare, l2cache, islog;
2810 libzfs_handle_t *hdl = zhp->zpool_hdl;
2811 uint64_t version;
2812
2813 (void) snprintf(msg, sizeof (msg),
2814 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2815
2816 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2817 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2818 &islog)) == 0)
2819 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2820 /*
2821 * XXX - this should just go away.
2822 */
2823 if (!avail_spare && !l2cache && !islog) {
2824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2825 "only inactive hot spares, cache, top-level, "
2826 "or log devices can be removed"));
2827 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2828 }
2829
2830 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2831 if (islog && version < SPA_VERSION_HOLES) {
2832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2833 "pool must be upgrade to support log removal"));
2834 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2835 }
2836
2837 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2838
2839 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2840 return (0);
2841
2842 return (zpool_standard_error(hdl, errno, msg));
2843 }
2844
2845 /*
2846 * Clear the errors for the pool, or the particular device if specified.
2847 */
2848 int
2849 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2850 {
2851 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2852 char msg[1024];
2853 nvlist_t *tgt;
2854 zpool_rewind_policy_t policy;
2855 boolean_t avail_spare, l2cache;
2856 libzfs_handle_t *hdl = zhp->zpool_hdl;
2857 nvlist_t *nvi = NULL;
2858 int error;
2859
2860 if (path)
2861 (void) snprintf(msg, sizeof (msg),
2862 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2863 path);
2864 else
2865 (void) snprintf(msg, sizeof (msg),
2866 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2867 zhp->zpool_name);
2868
2869 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2870 if (path) {
2871 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2872 &l2cache, NULL)) == 0)
2873 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2874
2875 /*
2876 * Don't allow error clearing for hot spares. Do allow
2877 * error clearing for l2cache devices.
2878 */
2879 if (avail_spare)
2880 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2881
2882 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2883 &zc.zc_guid) == 0);
2884 }
2885
2886 zpool_get_rewind_policy(rewindnvl, &policy);
2887 zc.zc_cookie = policy.zrp_request;
2888
2889 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2890 return (-1);
2891
2892 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2893 return (-1);
2894
2895 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2896 errno == ENOMEM) {
2897 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2898 zcmd_free_nvlists(&zc);
2899 return (-1);
2900 }
2901 }
2902
2903 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2904 errno != EPERM && errno != EACCES)) {
2905 if (policy.zrp_request &
2906 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2907 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2908 zpool_rewind_exclaim(hdl, zc.zc_name,
2909 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2910 nvi);
2911 nvlist_free(nvi);
2912 }
2913 zcmd_free_nvlists(&zc);
2914 return (0);
2915 }
2916
2917 zcmd_free_nvlists(&zc);
2918 return (zpool_standard_error(hdl, errno, msg));
2919 }
2920
2921 /*
2922 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2923 */
2924 int
2925 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2926 {
2927 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2928 char msg[1024];
2929 libzfs_handle_t *hdl = zhp->zpool_hdl;
2930
2931 (void) snprintf(msg, sizeof (msg),
2932 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2933 (u_longlong_t)guid);
2934
2935 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2936 zc.zc_guid = guid;
2937 zc.zc_cookie = ZPOOL_NO_REWIND;
2938
2939 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2940 return (0);
2941
2942 return (zpool_standard_error(hdl, errno, msg));
2943 }
2944
2945 /*
2946 * Convert from a devid string to a path.
2947 */
2948 static char *
2949 devid_to_path(char *devid_str)
2950 {
2951 ddi_devid_t devid;
2952 char *minor;
2953 char *path;
2954 devid_nmlist_t *list = NULL;
2955 int ret;
2956
2957 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2958 return (NULL);
2959
2960 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2961
2962 devid_str_free(minor);
2963 devid_free(devid);
2964
2965 if (ret != 0)
2966 return (NULL);
2967
2968 if ((path = strdup(list[0].devname)) == NULL)
2969 return (NULL);
2970
2971 devid_free_nmlist(list);
2972
2973 return (path);
2974 }
2975
2976 /*
2977 * Convert from a path to a devid string.
2978 */
2979 static char *
2980 path_to_devid(const char *path)
2981 {
2982 int fd;
2983 ddi_devid_t devid;
2984 char *minor, *ret;
2985
2986 if ((fd = open(path, O_RDONLY)) < 0)
2987 return (NULL);
2988
2989 minor = NULL;
2990 ret = NULL;
2991 if (devid_get(fd, &devid) == 0) {
2992 if (devid_get_minor_name(fd, &minor) == 0)
2993 ret = devid_str_encode(devid, minor);
2994 if (minor != NULL)
2995 devid_str_free(minor);
2996 devid_free(devid);
2997 }
2998 (void) close(fd);
2999
3000 return (ret);
3001 }
3002
3003 /*
3004 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3005 * ignore any failure here, since a common case is for an unprivileged user to
3006 * type 'zpool status', and we'll display the correct information anyway.
3007 */
3008 static void
3009 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3010 {
3011 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3012
3013 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3014 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3015 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3016 &zc.zc_guid) == 0);
3017
3018 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3019 }
3020
3021 /*
3022 * Remove partition suffix from a vdev path. Partition suffixes may take three
3023 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3024 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3025 * third case only occurs when preceded by a string matching the regular
3026 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3027 */
3028 static char *
3029 strip_partition(libzfs_handle_t *hdl, char *path)
3030 {
3031 char *tmp = zfs_strdup(hdl, path);
3032 char *part = NULL, *d = NULL;
3033
3034 if ((part = strstr(tmp, "-part")) && part != tmp) {
3035 d = part + 5;
3036 } else if ((part = strrchr(tmp, 'p')) &&
3037 part > tmp + 1 && isdigit(*(part-1))) {
3038 d = part + 1;
3039 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3040 for (d = &tmp[2]; isalpha(*d); part = ++d);
3041 }
3042 if (part && d && *d != '\0') {
3043 for (; isdigit(*d); d++);
3044 if (*d == '\0')
3045 *part = '\0';
3046 }
3047 return (tmp);
3048 }
3049
3050 #define PATH_BUF_LEN 64
3051
3052 /*
3053 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3054 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3055 * We also check if this is a whole disk, in which case we strip off the
3056 * trailing 's0' slice name.
3057 *
3058 * This routine is also responsible for identifying when disks have been
3059 * reconfigured in a new location. The kernel will have opened the device by
3060 * devid, but the path will still refer to the old location. To catch this, we
3061 * first do a path -> devid translation (which is fast for the common case). If
3062 * the devid matches, we're done. If not, we do a reverse devid -> path
3063 * translation and issue the appropriate ioctl() to update the path of the vdev.
3064 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3065 * of these checks.
3066 */
3067 char *
3068 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3069 boolean_t verbose)
3070 {
3071 char *path, *devid, *type;
3072 uint64_t value;
3073 char buf[PATH_BUF_LEN];
3074 vdev_stat_t *vs;
3075 uint_t vsc;
3076
3077 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3078 &value) == 0) {
3079 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3080 &value) == 0);
3081 (void) snprintf(buf, sizeof (buf), "%llu",
3082 (u_longlong_t)value);
3083 path = buf;
3084 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3085 /*
3086 * If the device is dead (faulted, offline, etc) then don't
3087 * bother opening it. Otherwise we may be forcing the user to
3088 * open a misbehaving device, which can have undesirable
3089 * effects.
3090 */
3091 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3092 (uint64_t **)&vs, &vsc) != 0 ||
3093 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3094 zhp != NULL &&
3095 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3096 /*
3097 * Determine if the current path is correct.
3098 */
3099 char *newdevid = path_to_devid(path);
3100
3101 if (newdevid == NULL ||
3102 strcmp(devid, newdevid) != 0) {
3103 char *newpath;
3104
3105 if ((newpath = devid_to_path(devid)) != NULL) {
3106 /*
3107 * Update the path appropriately.
3108 */
3109 set_path(zhp, nv, newpath);
3110 if (nvlist_add_string(nv,
3111 ZPOOL_CONFIG_PATH, newpath) == 0)
3112 verify(nvlist_lookup_string(nv,
3113 ZPOOL_CONFIG_PATH,
3114 &path) == 0);
3115 free(newpath);
3116 }
3117 }
3118
3119 if (newdevid)
3120 devid_str_free(newdevid);
3121 }
3122
3123 /*
3124 * For a block device only use the name.
3125 */
3126 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3127 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3128 path = strrchr(path, '/');
3129 path++;
3130 }
3131
3132 /*
3133 * Remove the partition from the path it this is a whole disk.
3134 */
3135 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3136 &value) == 0 && value) {
3137 return strip_partition(hdl, path);
3138 }
3139 } else {
3140 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3141
3142 /*
3143 * If it's a raidz device, we need to stick in the parity level.
3144 */
3145 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3146 char tmpbuf[PATH_BUF_LEN];
3147
3148 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3149 &value) == 0);
3150 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%llu", path,
3151 (u_longlong_t)value);
3152 path = tmpbuf;
3153 }
3154
3155 /*
3156 * We identify each top-level vdev by using a <type-id>
3157 * naming convention.
3158 */
3159 if (verbose) {
3160 uint64_t id;
3161
3162 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3163 &id) == 0);
3164 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3165 (u_longlong_t)id);
3166 path = buf;
3167 }
3168 }
3169
3170 return (zfs_strdup(hdl, path));
3171 }
3172
3173 static int
3174 zbookmark_compare(const void *a, const void *b)
3175 {
3176 return (memcmp(a, b, sizeof (zbookmark_t)));
3177 }
3178
3179 /*
3180 * Retrieve the persistent error log, uniquify the members, and return to the
3181 * caller.
3182 */
3183 int
3184 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3185 {
3186 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3187 uint64_t count;
3188 zbookmark_t *zb = NULL;
3189 int i;
3190
3191 /*
3192 * Retrieve the raw error list from the kernel. If the number of errors
3193 * has increased, allocate more space and continue until we get the
3194 * entire list.
3195 */
3196 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3197 &count) == 0);
3198 if (count == 0)
3199 return (0);
3200 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3201 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3202 return (-1);
3203 zc.zc_nvlist_dst_size = count;
3204 (void) strcpy(zc.zc_name, zhp->zpool_name);
3205 for (;;) {
3206 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3207 &zc) != 0) {
3208 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3209 if (errno == ENOMEM) {
3210 count = zc.zc_nvlist_dst_size;
3211 if ((zc.zc_nvlist_dst = (uintptr_t)
3212 zfs_alloc(zhp->zpool_hdl, count *
3213 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3214 return (-1);
3215 } else {
3216 return (-1);
3217 }
3218 } else {
3219 break;
3220 }
3221 }
3222
3223 /*
3224 * Sort the resulting bookmarks. This is a little confusing due to the
3225 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3226 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3227 * _not_ copied as part of the process. So we point the start of our
3228 * array appropriate and decrement the total number of elements.
3229 */
3230 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3231 zc.zc_nvlist_dst_size;
3232 count -= zc.zc_nvlist_dst_size;
3233
3234 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3235
3236 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3237
3238 /*
3239 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3240 */
3241 for (i = 0; i < count; i++) {
3242 nvlist_t *nv;
3243
3244 /* ignoring zb_blkid and zb_level for now */
3245 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3246 zb[i-1].zb_object == zb[i].zb_object)
3247 continue;
3248
3249 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3250 goto nomem;
3251 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3252 zb[i].zb_objset) != 0) {
3253 nvlist_free(nv);
3254 goto nomem;
3255 }
3256 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3257 zb[i].zb_object) != 0) {
3258 nvlist_free(nv);
3259 goto nomem;
3260 }
3261 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3262 nvlist_free(nv);
3263 goto nomem;
3264 }
3265 nvlist_free(nv);
3266 }
3267
3268 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3269 return (0);
3270
3271 nomem:
3272 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3273 return (no_memory(zhp->zpool_hdl));
3274 }
3275
3276 /*
3277 * Upgrade a ZFS pool to the latest on-disk version.
3278 */
3279 int
3280 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3281 {
3282 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3283 libzfs_handle_t *hdl = zhp->zpool_hdl;
3284
3285 (void) strcpy(zc.zc_name, zhp->zpool_name);
3286 zc.zc_cookie = new_version;
3287
3288 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3289 return (zpool_standard_error_fmt(hdl, errno,
3290 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3291 zhp->zpool_name));
3292 return (0);
3293 }
3294
3295 void
3296 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3297 char *history_str)
3298 {
3299 int i;
3300
3301 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3302 for (i = 1; i < argc; i++) {
3303 if (strlen(history_str) + 1 + strlen(argv[i]) >
3304 HIS_MAX_RECORD_LEN)
3305 break;
3306 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3307 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3308 }
3309 }
3310
3311 /*
3312 * Stage command history for logging.
3313 */
3314 int
3315 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3316 {
3317 if (history_str == NULL)
3318 return (EINVAL);
3319
3320 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3321 return (EINVAL);
3322
3323 if (hdl->libzfs_log_str != NULL)
3324 free(hdl->libzfs_log_str);
3325
3326 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3327 return (no_memory(hdl));
3328
3329 return (0);
3330 }
3331
3332 /*
3333 * Perform ioctl to get some command history of a pool.
3334 *
3335 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3336 * logical offset of the history buffer to start reading from.
3337 *
3338 * Upon return, 'off' is the next logical offset to read from and
3339 * 'len' is the actual amount of bytes read into 'buf'.
3340 */
3341 static int
3342 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3343 {
3344 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3345 libzfs_handle_t *hdl = zhp->zpool_hdl;
3346
3347 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3348
3349 zc.zc_history = (uint64_t)(uintptr_t)buf;
3350 zc.zc_history_len = *len;
3351 zc.zc_history_offset = *off;
3352
3353 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3354 switch (errno) {
3355 case EPERM:
3356 return (zfs_error_fmt(hdl, EZFS_PERM,
3357 dgettext(TEXT_DOMAIN,
3358 "cannot show history for pool '%s'"),
3359 zhp->zpool_name));
3360 case ENOENT:
3361 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3362 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3363 "'%s'"), zhp->zpool_name));
3364 case ENOTSUP:
3365 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3366 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3367 "'%s', pool must be upgraded"), zhp->zpool_name));
3368 default:
3369 return (zpool_standard_error_fmt(hdl, errno,
3370 dgettext(TEXT_DOMAIN,
3371 "cannot get history for '%s'"), zhp->zpool_name));
3372 }
3373 }
3374
3375 *len = zc.zc_history_len;
3376 *off = zc.zc_history_offset;
3377
3378 return (0);
3379 }
3380
3381 /*
3382 * Process the buffer of nvlists, unpacking and storing each nvlist record
3383 * into 'records'. 'leftover' is set to the number of bytes that weren't
3384 * processed as there wasn't a complete record.
3385 */
3386 int
3387 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3388 nvlist_t ***records, uint_t *numrecords)
3389 {
3390 uint64_t reclen;
3391 nvlist_t *nv;
3392 int i;
3393
3394 while (bytes_read > sizeof (reclen)) {
3395
3396 /* get length of packed record (stored as little endian) */
3397 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3398 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3399
3400 if (bytes_read < sizeof (reclen) + reclen)
3401 break;
3402
3403 /* unpack record */
3404 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3405 return (ENOMEM);
3406 bytes_read -= sizeof (reclen) + reclen;
3407 buf += sizeof (reclen) + reclen;
3408
3409 /* add record to nvlist array */
3410 (*numrecords)++;
3411 if (ISP2(*numrecords + 1)) {
3412 *records = realloc(*records,
3413 *numrecords * 2 * sizeof (nvlist_t *));
3414 }
3415 (*records)[*numrecords - 1] = nv;
3416 }
3417
3418 *leftover = bytes_read;
3419 return (0);
3420 }
3421
3422 #define HIS_BUF_LEN (128*1024)
3423
3424 /*
3425 * Retrieve the command history of a pool.
3426 */
3427 int
3428 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3429 {
3430 char buf[HIS_BUF_LEN];
3431 uint64_t off = 0;
3432 nvlist_t **records = NULL;
3433 uint_t numrecords = 0;
3434 int err, i;
3435
3436 do {
3437 uint64_t bytes_read = sizeof (buf);
3438 uint64_t leftover;
3439
3440 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3441 break;
3442
3443 /* if nothing else was read in, we're at EOF, just return */
3444 if (!bytes_read)
3445 break;
3446
3447 if ((err = zpool_history_unpack(buf, bytes_read,
3448 &leftover, &records, &numrecords)) != 0)
3449 break;
3450 off -= leftover;
3451
3452 /* CONSTCOND */
3453 } while (1);
3454
3455 if (!err) {
3456 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3457 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3458 records, numrecords) == 0);
3459 }
3460 for (i = 0; i < numrecords; i++)
3461 nvlist_free(records[i]);
3462 free(records);
3463
3464 return (err);
3465 }
3466
3467 /*
3468 * Retrieve the next event. If there is a new event available 'nvp' will
3469 * contain a newly allocated nvlist and 'dropped' will be set to the number
3470 * of missed events since the last call to this function. When 'nvp' is
3471 * set to NULL it indicates no new events are available. In either case
3472 * the function returns 0 and it is up to the caller to free 'nvp'. In
3473 * the case of a fatal error the function will return a non-zero value.
3474 * When the function is called in blocking mode it will not return until
3475 * a new event is available.
3476 */
3477 int
3478 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3479 int *dropped, int block, int cleanup_fd)
3480 {
3481 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3482 int error = 0;
3483
3484 *nvp = NULL;
3485 *dropped = 0;
3486 zc.zc_cleanup_fd = cleanup_fd;
3487
3488 if (!block)
3489 zc.zc_guid = ZEVENT_NONBLOCK;
3490
3491 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3492 return (-1);
3493
3494 retry:
3495 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3496 switch (errno) {
3497 case ESHUTDOWN:
3498 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3499 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3500 goto out;
3501 case ENOENT:
3502 /* Blocking error case should not occur */
3503 if (block)
3504 error = zpool_standard_error_fmt(hdl, errno,
3505 dgettext(TEXT_DOMAIN, "cannot get event"));
3506
3507 goto out;
3508 case ENOMEM:
3509 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3510 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3511 dgettext(TEXT_DOMAIN, "cannot get event"));
3512 goto out;
3513 } else {
3514 goto retry;
3515 }
3516 default:
3517 error = zpool_standard_error_fmt(hdl, errno,
3518 dgettext(TEXT_DOMAIN, "cannot get event"));
3519 goto out;
3520 }
3521 }
3522
3523 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3524 if (error != 0)
3525 goto out;
3526
3527 *dropped = (int)zc.zc_cookie;
3528 out:
3529 zcmd_free_nvlists(&zc);
3530
3531 return (error);
3532 }
3533
3534 /*
3535 * Clear all events.
3536 */
3537 int
3538 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3539 {
3540 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3541 char msg[1024];
3542
3543 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3544 "cannot clear events"));
3545
3546 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3547 return (zpool_standard_error_fmt(hdl, errno, msg));
3548
3549 if (count != NULL)
3550 *count = (int)zc.zc_cookie; /* # of events cleared */
3551
3552 return (0);
3553 }
3554
3555 void
3556 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3557 char *pathname, size_t len)
3558 {
3559 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3560 boolean_t mounted = B_FALSE;
3561 char *mntpnt = NULL;
3562 char dsname[MAXNAMELEN];
3563
3564 if (dsobj == 0) {
3565 /* special case for the MOS */
3566 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3567 return;
3568 }
3569
3570 /* get the dataset's name */
3571 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3572 zc.zc_obj = dsobj;
3573 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3574 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3575 /* just write out a path of two object numbers */
3576 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3577 (longlong_t)dsobj, (longlong_t)obj);
3578 return;
3579 }
3580 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3581
3582 /* find out if the dataset is mounted */
3583 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3584
3585 /* get the corrupted object's path */
3586 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3587 zc.zc_obj = obj;
3588 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3589 &zc) == 0) {
3590 if (mounted) {
3591 (void) snprintf(pathname, len, "%s%s", mntpnt,
3592 zc.zc_value);
3593 } else {
3594 (void) snprintf(pathname, len, "%s:%s",
3595 dsname, zc.zc_value);
3596 }
3597 } else {
3598 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3599 }
3600 free(mntpnt);
3601 }
3602
3603 /*
3604 * Read the EFI label from the config, if a label does not exist then
3605 * pass back the error to the caller. If the caller has passed a non-NULL
3606 * diskaddr argument then we set it to the starting address of the EFI
3607 * partition.
3608 */
3609 static int
3610 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3611 {
3612 char *path;
3613 int fd;
3614 char diskname[MAXPATHLEN];
3615 int err = -1;
3616
3617 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3618 return (err);
3619
3620 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3621 strrchr(path, '/'));
3622 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3623 struct dk_gpt *vtoc;
3624
3625 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3626 if (sb != NULL)
3627 *sb = vtoc->efi_parts[0].p_start;
3628 efi_free(vtoc);
3629 }
3630 (void) close(fd);
3631 }
3632 return (err);
3633 }
3634
3635 /*
3636 * determine where a partition starts on a disk in the current
3637 * configuration
3638 */
3639 static diskaddr_t
3640 find_start_block(nvlist_t *config)
3641 {
3642 nvlist_t **child;
3643 uint_t c, children;
3644 diskaddr_t sb = MAXOFFSET_T;
3645 uint64_t wholedisk;
3646
3647 if (nvlist_lookup_nvlist_array(config,
3648 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3649 if (nvlist_lookup_uint64(config,
3650 ZPOOL_CONFIG_WHOLE_DISK,
3651 &wholedisk) != 0 || !wholedisk) {
3652 return (MAXOFFSET_T);
3653 }
3654 if (read_efi_label(config, &sb) < 0)
3655 sb = MAXOFFSET_T;
3656 return (sb);
3657 }
3658
3659 for (c = 0; c < children; c++) {
3660 sb = find_start_block(child[c]);
3661 if (sb != MAXOFFSET_T) {
3662 return (sb);
3663 }
3664 }
3665 return (MAXOFFSET_T);
3666 }
3667
3668 int
3669 zpool_label_disk_wait(char *path, int timeout)
3670 {
3671 struct stat64 statbuf;
3672 int i;
3673
3674 /*
3675 * Wait timeout miliseconds for a newly created device to be available
3676 * from the given path. There is a small window when a /dev/ device
3677 * will exist and the udev link will not, so we must wait for the
3678 * symlink. Depending on the udev rules this may take a few seconds.
3679 */
3680 for (i = 0; i < timeout; i++) {
3681 usleep(1000);
3682
3683 errno = 0;
3684 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3685 return (0);
3686 }
3687
3688 return (ENOENT);
3689 }
3690
3691 int
3692 zpool_label_disk_check(char *path)
3693 {
3694 struct dk_gpt *vtoc;
3695 int fd, err;
3696
3697 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3698 return errno;
3699
3700 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3701 (void) close(fd);
3702 return err;
3703 }
3704
3705 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3706 efi_free(vtoc);
3707 (void) close(fd);
3708 return EIDRM;
3709 }
3710
3711 efi_free(vtoc);
3712 (void) close(fd);
3713 return 0;
3714 }
3715
3716 /*
3717 * Label an individual disk. The name provided is the short name,
3718 * stripped of any leading /dev path.
3719 */
3720 int
3721 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3722 {
3723 char path[MAXPATHLEN];
3724 struct dk_gpt *vtoc;
3725 int rval, fd;
3726 size_t resv = EFI_MIN_RESV_SIZE;
3727 uint64_t slice_size;
3728 diskaddr_t start_block;
3729 char errbuf[1024];
3730
3731 /* prepare an error message just in case */
3732 (void) snprintf(errbuf, sizeof (errbuf),
3733 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3734
3735 if (zhp) {
3736 nvlist_t *nvroot;
3737
3738 if (pool_is_bootable(zhp)) {
3739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3740 "EFI labeled devices are not supported on root "
3741 "pools."));
3742 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3743 }
3744
3745 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3746 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3747
3748 if (zhp->zpool_start_block == 0)
3749 start_block = find_start_block(nvroot);
3750 else
3751 start_block = zhp->zpool_start_block;
3752 zhp->zpool_start_block = start_block;
3753 } else {
3754 /* new pool */
3755 start_block = NEW_START_BLOCK;
3756 }
3757
3758 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3759 BACKUP_SLICE);
3760
3761 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3762 /*
3763 * This shouldn't happen. We've long since verified that this
3764 * is a valid device.
3765 */
3766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3767 "unable to open device '%s': %d"), path, errno);
3768 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3769 }
3770
3771 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3772 /*
3773 * The only way this can fail is if we run out of memory, or we
3774 * were unable to read the disk's capacity
3775 */
3776 if (errno == ENOMEM)
3777 (void) no_memory(hdl);
3778
3779 (void) close(fd);
3780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3781 "unable to read disk capacity"), name);
3782
3783 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3784 }
3785
3786 slice_size = vtoc->efi_last_u_lba + 1;
3787 slice_size -= EFI_MIN_RESV_SIZE;
3788 if (start_block == MAXOFFSET_T)
3789 start_block = NEW_START_BLOCK;
3790 slice_size -= start_block;
3791 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3792
3793 vtoc->efi_parts[0].p_start = start_block;
3794 vtoc->efi_parts[0].p_size = slice_size;
3795
3796 /*
3797 * Why we use V_USR: V_BACKUP confuses users, and is considered
3798 * disposable by some EFI utilities (since EFI doesn't have a backup
3799 * slice). V_UNASSIGNED is supposed to be used only for zero size
3800 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3801 * etc. were all pretty specific. V_USR is as close to reality as we
3802 * can get, in the absence of V_OTHER.
3803 */
3804 vtoc->efi_parts[0].p_tag = V_USR;
3805 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3806
3807 vtoc->efi_parts[8].p_start = slice_size + start_block;
3808 vtoc->efi_parts[8].p_size = resv;
3809 vtoc->efi_parts[8].p_tag = V_RESERVED;
3810
3811 if ((rval = efi_write(fd, vtoc)) != 0) {
3812 /*
3813 * Some block drivers (like pcata) may not support EFI
3814 * GPT labels. Print out a helpful error message dir-
3815 * ecting the user to manually label the disk and give
3816 * a specific slice.
3817 */
3818 (void) close(fd);
3819 efi_free(vtoc);
3820
3821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3822 "parted(8) and then provide a specific slice: %d"), rval);
3823 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3824 }
3825
3826 (void) close(fd);
3827 efi_free(vtoc);
3828
3829 /* Wait for the first expected slice to appear. */
3830 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3831 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3832 rval = zpool_label_disk_wait(path, 3000);
3833 if (rval) {
3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3835 "detect device partitions on '%s': %d"), path, rval);
3836 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3837 }
3838
3839 /* We can't be to paranoid. Read the label back and verify it. */
3840 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3841 rval = zpool_label_disk_check(path);
3842 if (rval) {
3843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3844 "EFI label on '%s' is damaged. Ensure\nthis device "
3845 "is not in in use, and is functioning properly: %d"),
3846 path, rval);
3847 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3848 }
3849
3850 return 0;
3851 }