9075 Improve ZFS pool import/load process and corrupted pool recovery
[unleashed.git] / usr / src / lib / libzfs / common / libzfs_pool.c
blob01f6b0d8accbe86bbec4f0532a434aae9dcaf0c3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2017 Datto Inc.
31 #include <ctype.h>
32 #include <errno.h>
33 #include <devid.h>
34 #include <fcntl.h>
35 #include <libintl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <libgen.h>
41 #include <sys/efi_partition.h>
42 #include <sys/vtoc.h>
43 #include <sys/zfs_ioctl.h>
44 #include <dlfcn.h>
46 #include "zfs_namecheck.h"
47 #include "zfs_prop.h"
48 #include "libzfs_impl.h"
49 #include "zfs_comutil.h"
50 #include "zfeature_common.h"
52 static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
53 static boolean_t zpool_vdev_is_interior(const char *name);
55 #define BACKUP_SLICE "s2"
57 typedef struct prop_flags {
58 int create:1; /* Validate property on creation */
59 int import:1; /* Validate property on import */
60 } prop_flags_t;
63 * ====================================================================
64 * zpool property functions
65 * ====================================================================
68 static int
69 zpool_get_all_props(zpool_handle_t *zhp)
71 zfs_cmd_t zc = { 0 };
72 libzfs_handle_t *hdl = zhp->zpool_hdl;
74 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
77 return (-1);
79 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
80 if (errno == ENOMEM) {
81 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
82 zcmd_free_nvlists(&zc);
83 return (-1);
85 } else {
86 zcmd_free_nvlists(&zc);
87 return (-1);
91 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
92 zcmd_free_nvlists(&zc);
93 return (-1);
96 zcmd_free_nvlists(&zc);
98 return (0);
101 static int
102 zpool_props_refresh(zpool_handle_t *zhp)
104 nvlist_t *old_props;
106 old_props = zhp->zpool_props;
108 if (zpool_get_all_props(zhp) != 0)
109 return (-1);
111 nvlist_free(old_props);
112 return (0);
115 static char *
116 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
117 zprop_source_t *src)
119 nvlist_t *nv, *nvl;
120 uint64_t ival;
121 char *value;
122 zprop_source_t source;
124 nvl = zhp->zpool_props;
125 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
126 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
127 source = ival;
128 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
129 } else {
130 source = ZPROP_SRC_DEFAULT;
131 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
132 value = "-";
135 if (src)
136 *src = source;
138 return (value);
141 uint64_t
142 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
144 nvlist_t *nv, *nvl;
145 uint64_t value;
146 zprop_source_t source;
148 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop == ZPOOL_PROP_GUID) &&
155 (nvlist_lookup_nvlist(zhp->zpool_config,
156 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
158 == 0)) {
159 return (value);
161 return (zpool_prop_default_numeric(prop));
164 nvl = zhp->zpool_props;
165 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
166 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
167 source = value;
168 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
169 } else {
170 source = ZPROP_SRC_DEFAULT;
171 value = zpool_prop_default_numeric(prop);
174 if (src)
175 *src = source;
177 return (value);
181 * Map VDEV STATE to printed strings.
183 const char *
184 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186 switch (state) {
187 case VDEV_STATE_CLOSED:
188 case VDEV_STATE_OFFLINE:
189 return (gettext("OFFLINE"));
190 case VDEV_STATE_REMOVED:
191 return (gettext("REMOVED"));
192 case VDEV_STATE_CANT_OPEN:
193 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
194 return (gettext("FAULTED"));
195 else if (aux == VDEV_AUX_SPLIT_POOL)
196 return (gettext("SPLIT"));
197 else
198 return (gettext("UNAVAIL"));
199 case VDEV_STATE_FAULTED:
200 return (gettext("FAULTED"));
201 case VDEV_STATE_DEGRADED:
202 return (gettext("DEGRADED"));
203 case VDEV_STATE_HEALTHY:
204 return (gettext("ONLINE"));
206 default:
207 break;
210 return (gettext("UNKNOWN"));
214 * Map POOL STATE to printed strings.
216 const char *
217 zpool_pool_state_to_name(pool_state_t state)
219 switch (state) {
220 case POOL_STATE_ACTIVE:
221 return (gettext("ACTIVE"));
222 case POOL_STATE_EXPORTED:
223 return (gettext("EXPORTED"));
224 case POOL_STATE_DESTROYED:
225 return (gettext("DESTROYED"));
226 case POOL_STATE_SPARE:
227 return (gettext("SPARE"));
228 case POOL_STATE_L2CACHE:
229 return (gettext("L2CACHE"));
230 case POOL_STATE_UNINITIALIZED:
231 return (gettext("UNINITIALIZED"));
232 case POOL_STATE_UNAVAIL:
233 return (gettext("UNAVAIL"));
234 case POOL_STATE_POTENTIALLY_ACTIVE:
235 return (gettext("POTENTIALLY_ACTIVE"));
238 return (gettext("UNKNOWN"));
242 * Get a zpool property value for 'prop' and return the value in
243 * a pre-allocated buffer.
246 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
247 zprop_source_t *srctype, boolean_t literal)
249 uint64_t intval;
250 const char *strval;
251 zprop_source_t src = ZPROP_SRC_NONE;
252 nvlist_t *nvroot;
253 vdev_stat_t *vs;
254 uint_t vsc;
256 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
257 switch (prop) {
258 case ZPOOL_PROP_NAME:
259 (void) strlcpy(buf, zpool_get_name(zhp), len);
260 break;
262 case ZPOOL_PROP_HEALTH:
263 (void) strlcpy(buf, "FAULTED", len);
264 break;
266 case ZPOOL_PROP_GUID:
267 intval = zpool_get_prop_int(zhp, prop, &src);
268 (void) snprintf(buf, len, "%llu", intval);
269 break;
271 case ZPOOL_PROP_ALTROOT:
272 case ZPOOL_PROP_CACHEFILE:
273 case ZPOOL_PROP_COMMENT:
274 if (zhp->zpool_props != NULL ||
275 zpool_get_all_props(zhp) == 0) {
276 (void) strlcpy(buf,
277 zpool_get_prop_string(zhp, prop, &src),
278 len);
279 break;
281 /* FALLTHROUGH */
282 default:
283 (void) strlcpy(buf, "-", len);
284 break;
287 if (srctype != NULL)
288 *srctype = src;
289 return (0);
292 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
293 prop != ZPOOL_PROP_NAME)
294 return (-1);
296 switch (zpool_prop_get_type(prop)) {
297 case PROP_TYPE_STRING:
298 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
299 len);
300 break;
302 case PROP_TYPE_NUMBER:
303 intval = zpool_get_prop_int(zhp, prop, &src);
305 switch (prop) {
306 case ZPOOL_PROP_SIZE:
307 case ZPOOL_PROP_ALLOCATED:
308 case ZPOOL_PROP_FREE:
309 case ZPOOL_PROP_FREEING:
310 case ZPOOL_PROP_LEAKED:
311 if (literal) {
312 (void) snprintf(buf, len, "%llu",
313 (u_longlong_t)intval);
314 } else {
315 (void) zfs_nicenum(intval, buf, len);
317 break;
318 case ZPOOL_PROP_BOOTSIZE:
319 case ZPOOL_PROP_EXPANDSZ:
320 if (intval == 0) {
321 (void) strlcpy(buf, "-", len);
322 } else if (literal) {
323 (void) snprintf(buf, len, "%llu",
324 (u_longlong_t)intval);
325 } else {
326 (void) zfs_nicenum(intval, buf, len);
328 break;
329 case ZPOOL_PROP_CAPACITY:
330 if (literal) {
331 (void) snprintf(buf, len, "%llu",
332 (u_longlong_t)intval);
333 } else {
334 (void) snprintf(buf, len, "%llu%%",
335 (u_longlong_t)intval);
337 break;
338 case ZPOOL_PROP_FRAGMENTATION:
339 if (intval == UINT64_MAX) {
340 (void) strlcpy(buf, "-", len);
341 } else {
342 (void) snprintf(buf, len, "%llu%%",
343 (u_longlong_t)intval);
345 break;
346 case ZPOOL_PROP_DEDUPRATIO:
347 (void) snprintf(buf, len, "%llu.%02llux",
348 (u_longlong_t)(intval / 100),
349 (u_longlong_t)(intval % 100));
350 break;
351 case ZPOOL_PROP_HEALTH:
352 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
353 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
354 verify(nvlist_lookup_uint64_array(nvroot,
355 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
356 == 0);
358 (void) strlcpy(buf, zpool_state_to_name(intval,
359 vs->vs_aux), len);
360 break;
361 case ZPOOL_PROP_VERSION:
362 if (intval >= SPA_VERSION_FEATURES) {
363 (void) snprintf(buf, len, "-");
364 break;
366 /* FALLTHROUGH */
367 default:
368 (void) snprintf(buf, len, "%llu", intval);
370 break;
372 case PROP_TYPE_INDEX:
373 intval = zpool_get_prop_int(zhp, prop, &src);
374 if (zpool_prop_index_to_string(prop, intval, &strval)
375 != 0)
376 return (-1);
377 (void) strlcpy(buf, strval, len);
378 break;
380 default:
381 abort();
384 if (srctype)
385 *srctype = src;
387 return (0);
391 * Check if the bootfs name has the same pool name as it is set to.
392 * Assuming bootfs is a valid dataset name.
394 static boolean_t
395 bootfs_name_valid(const char *pool, char *bootfs)
397 int len = strlen(pool);
399 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
400 return (B_FALSE);
402 if (strncmp(pool, bootfs, len) == 0 &&
403 (bootfs[len] == '/' || bootfs[len] == '\0'))
404 return (B_TRUE);
406 return (B_FALSE);
409 boolean_t
410 zpool_is_bootable(zpool_handle_t *zhp)
412 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
414 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
415 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
416 sizeof (bootfs)) != 0);
421 * Given an nvlist of zpool properties to be set, validate that they are
422 * correct, and parse any numeric properties (index, boolean, etc) if they are
423 * specified as strings.
425 static nvlist_t *
426 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
427 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
429 nvpair_t *elem;
430 nvlist_t *retprops;
431 zpool_prop_t prop;
432 char *strval;
433 uint64_t intval;
434 char *slash, *check;
435 struct stat64 statbuf;
436 zpool_handle_t *zhp;
438 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
439 (void) no_memory(hdl);
440 return (NULL);
443 elem = NULL;
444 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
445 const char *propname = nvpair_name(elem);
447 prop = zpool_name_to_prop(propname);
448 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
449 int err;
450 char *fname = strchr(propname, '@') + 1;
452 err = zfeature_lookup_name(fname, NULL);
453 if (err != 0) {
454 ASSERT3U(err, ==, ENOENT);
455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456 "invalid feature '%s'"), fname);
457 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
458 goto error;
461 if (nvpair_type(elem) != DATA_TYPE_STRING) {
462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
463 "'%s' must be a string"), propname);
464 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
465 goto error;
468 (void) nvpair_value_string(elem, &strval);
469 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
471 "property '%s' can only be set to "
472 "'enabled'"), propname);
473 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
474 goto error;
477 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
478 (void) no_memory(hdl);
479 goto error;
481 continue;
485 * Make sure this property is valid and applies to this type.
487 if (prop == ZPOOL_PROP_INVAL) {
488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489 "invalid property '%s'"), propname);
490 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
491 goto error;
494 if (zpool_prop_readonly(prop)) {
495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
496 "is readonly"), propname);
497 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
498 goto error;
501 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
502 &strval, &intval, errbuf) != 0)
503 goto error;
506 * Perform additional checking for specific properties.
508 switch (prop) {
509 case ZPOOL_PROP_VERSION:
510 if (intval < version ||
511 !SPA_VERSION_IS_SUPPORTED(intval)) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' number %d is invalid."),
514 propname, intval);
515 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
516 goto error;
518 break;
520 case ZPOOL_PROP_BOOTSIZE:
521 if (!flags.create) {
522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
523 "property '%s' can only be set during pool "
524 "creation"), propname);
525 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
526 goto error;
528 break;
530 case ZPOOL_PROP_BOOTFS:
531 if (flags.create || flags.import) {
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "property '%s' cannot be set at creation "
534 "or import time"), propname);
535 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
536 goto error;
539 if (version < SPA_VERSION_BOOTFS) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541 "pool must be upgraded to support "
542 "'%s' property"), propname);
543 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
544 goto error;
548 * bootfs property value has to be a dataset name and
549 * the dataset has to be in the same pool as it sets to.
551 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
552 strval)) {
553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
554 "is an invalid name"), strval);
555 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
556 goto error;
559 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
561 "could not open pool '%s'"), poolname);
562 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
563 goto error;
565 zpool_close(zhp);
566 break;
568 case ZPOOL_PROP_ALTROOT:
569 if (!flags.create && !flags.import) {
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571 "property '%s' can only be set during pool "
572 "creation or import"), propname);
573 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
574 goto error;
577 if (strval[0] != '/') {
578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
579 "bad alternate root '%s'"), strval);
580 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
581 goto error;
583 break;
585 case ZPOOL_PROP_CACHEFILE:
586 if (strval[0] == '\0')
587 break;
589 if (strcmp(strval, "none") == 0)
590 break;
592 if (strval[0] != '/') {
593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
594 "property '%s' must be empty, an "
595 "absolute path, or 'none'"), propname);
596 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
597 goto error;
600 slash = strrchr(strval, '/');
602 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
603 strcmp(slash, "/..") == 0) {
604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
605 "'%s' is not a valid file"), strval);
606 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
607 goto error;
610 *slash = '\0';
612 if (strval[0] != '\0' &&
613 (stat64(strval, &statbuf) != 0 ||
614 !S_ISDIR(statbuf.st_mode))) {
615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
616 "'%s' is not a valid directory"),
617 strval);
618 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
619 goto error;
622 *slash = '/';
623 break;
625 case ZPOOL_PROP_COMMENT:
626 for (check = strval; *check != '\0'; check++) {
627 if (!isprint(*check)) {
628 zfs_error_aux(hdl,
629 dgettext(TEXT_DOMAIN,
630 "comment may only have printable "
631 "characters"));
632 (void) zfs_error(hdl, EZFS_BADPROP,
633 errbuf);
634 goto error;
637 if (strlen(strval) > ZPROP_MAX_COMMENT) {
638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
639 "comment must not exceed %d characters"),
640 ZPROP_MAX_COMMENT);
641 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
642 goto error;
644 break;
645 case ZPOOL_PROP_READONLY:
646 if (!flags.import) {
647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
648 "property '%s' can only be set at "
649 "import time"), propname);
650 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
651 goto error;
653 break;
655 default:
656 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
657 "property '%s'(%d) not defined"), propname, prop);
658 break;
662 return (retprops);
663 error:
664 nvlist_free(retprops);
665 return (NULL);
669 * Set zpool property : propname=propval.
672 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
674 zfs_cmd_t zc = { 0 };
675 int ret = -1;
676 char errbuf[1024];
677 nvlist_t *nvl = NULL;
678 nvlist_t *realprops;
679 uint64_t version;
680 prop_flags_t flags = { 0 };
682 (void) snprintf(errbuf, sizeof (errbuf),
683 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
684 zhp->zpool_name);
686 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
687 return (no_memory(zhp->zpool_hdl));
689 if (nvlist_add_string(nvl, propname, propval) != 0) {
690 nvlist_free(nvl);
691 return (no_memory(zhp->zpool_hdl));
694 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
695 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
696 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
697 nvlist_free(nvl);
698 return (-1);
701 nvlist_free(nvl);
702 nvl = realprops;
705 * Execute the corresponding ioctl() to set this property.
707 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
709 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
710 nvlist_free(nvl);
711 return (-1);
714 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
716 zcmd_free_nvlists(&zc);
717 nvlist_free(nvl);
719 if (ret)
720 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
721 else
722 (void) zpool_props_refresh(zhp);
724 return (ret);
728 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
730 libzfs_handle_t *hdl = zhp->zpool_hdl;
731 zprop_list_t *entry;
732 char buf[ZFS_MAXPROPLEN];
733 nvlist_t *features = NULL;
734 zprop_list_t **last;
735 boolean_t firstexpand = (NULL == *plp);
737 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
738 return (-1);
740 last = plp;
741 while (*last != NULL)
742 last = &(*last)->pl_next;
744 if ((*plp)->pl_all)
745 features = zpool_get_features(zhp);
747 if ((*plp)->pl_all && firstexpand) {
748 for (int i = 0; i < SPA_FEATURES; i++) {
749 zprop_list_t *entry = zfs_alloc(hdl,
750 sizeof (zprop_list_t));
751 entry->pl_prop = ZPROP_INVAL;
752 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
753 spa_feature_table[i].fi_uname);
754 entry->pl_width = strlen(entry->pl_user_prop);
755 entry->pl_all = B_TRUE;
757 *last = entry;
758 last = &entry->pl_next;
762 /* add any unsupported features */
763 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
764 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
765 char *propname;
766 boolean_t found;
767 zprop_list_t *entry;
769 if (zfeature_is_supported(nvpair_name(nvp)))
770 continue;
772 propname = zfs_asprintf(hdl, "unsupported@%s",
773 nvpair_name(nvp));
776 * Before adding the property to the list make sure that no
777 * other pool already added the same property.
779 found = B_FALSE;
780 entry = *plp;
781 while (entry != NULL) {
782 if (entry->pl_user_prop != NULL &&
783 strcmp(propname, entry->pl_user_prop) == 0) {
784 found = B_TRUE;
785 break;
787 entry = entry->pl_next;
789 if (found) {
790 free(propname);
791 continue;
794 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
795 entry->pl_prop = ZPROP_INVAL;
796 entry->pl_user_prop = propname;
797 entry->pl_width = strlen(entry->pl_user_prop);
798 entry->pl_all = B_TRUE;
800 *last = entry;
801 last = &entry->pl_next;
804 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
806 if (entry->pl_fixed)
807 continue;
809 if (entry->pl_prop != ZPROP_INVAL &&
810 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
811 NULL, B_FALSE) == 0) {
812 if (strlen(buf) > entry->pl_width)
813 entry->pl_width = strlen(buf);
817 return (0);
821 * Get the state for the given feature on the given ZFS pool.
824 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
825 size_t len)
827 uint64_t refcount;
828 boolean_t found = B_FALSE;
829 nvlist_t *features = zpool_get_features(zhp);
830 boolean_t supported;
831 const char *feature = strchr(propname, '@') + 1;
833 supported = zpool_prop_feature(propname);
834 ASSERT(supported || zpool_prop_unsupported(propname));
837 * Convert from feature name to feature guid. This conversion is
838 * unecessary for unsupported@... properties because they already
839 * use guids.
841 if (supported) {
842 int ret;
843 spa_feature_t fid;
845 ret = zfeature_lookup_name(feature, &fid);
846 if (ret != 0) {
847 (void) strlcpy(buf, "-", len);
848 return (ENOTSUP);
850 feature = spa_feature_table[fid].fi_guid;
853 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
854 found = B_TRUE;
856 if (supported) {
857 if (!found) {
858 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
859 } else {
860 if (refcount == 0)
861 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
862 else
863 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
865 } else {
866 if (found) {
867 if (refcount == 0) {
868 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
869 } else {
870 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
872 } else {
873 (void) strlcpy(buf, "-", len);
874 return (ENOTSUP);
878 return (0);
882 * Don't start the slice at the default block of 34; many storage
883 * devices will use a stripe width of 128k, so start there instead.
885 #define NEW_START_BLOCK 256
888 * Validate the given pool name, optionally putting an extended error message in
889 * 'buf'.
891 boolean_t
892 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
894 namecheck_err_t why;
895 char what;
896 int ret;
898 ret = pool_namecheck(pool, &why, &what);
901 * The rules for reserved pool names were extended at a later point.
902 * But we need to support users with existing pools that may now be
903 * invalid. So we only check for this expanded set of names during a
904 * create (or import), and only in userland.
906 if (ret == 0 && !isopen &&
907 (strncmp(pool, "mirror", 6) == 0 ||
908 strncmp(pool, "raidz", 5) == 0 ||
909 strncmp(pool, "spare", 5) == 0 ||
910 strcmp(pool, "log") == 0)) {
911 if (hdl != NULL)
912 zfs_error_aux(hdl,
913 dgettext(TEXT_DOMAIN, "name is reserved"));
914 return (B_FALSE);
918 if (ret != 0) {
919 if (hdl != NULL) {
920 switch (why) {
921 case NAME_ERR_TOOLONG:
922 zfs_error_aux(hdl,
923 dgettext(TEXT_DOMAIN, "name is too long"));
924 break;
926 case NAME_ERR_INVALCHAR:
927 zfs_error_aux(hdl,
928 dgettext(TEXT_DOMAIN, "invalid character "
929 "'%c' in pool name"), what);
930 break;
932 case NAME_ERR_NOLETTER:
933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
934 "name must begin with a letter"));
935 break;
937 case NAME_ERR_RESERVED:
938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
939 "name is reserved"));
940 break;
942 case NAME_ERR_DISKLIKE:
943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
944 "pool name is reserved"));
945 break;
947 case NAME_ERR_LEADING_SLASH:
948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
949 "leading slash in name"));
950 break;
952 case NAME_ERR_EMPTY_COMPONENT:
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "empty component in name"));
955 break;
957 case NAME_ERR_TRAILING_SLASH:
958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959 "trailing slash in name"));
960 break;
962 case NAME_ERR_MULTIPLE_DELIMITERS:
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "multiple '@' and/or '#' delimiters in "
965 "name"));
966 break;
968 default:
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "(%d) not defined"), why);
971 break;
974 return (B_FALSE);
977 return (B_TRUE);
981 * Open a handle to the given pool, even if the pool is currently in the FAULTED
982 * state.
984 zpool_handle_t *
985 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
987 zpool_handle_t *zhp;
988 boolean_t missing;
991 * Make sure the pool name is valid.
993 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
994 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
995 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
996 pool);
997 return (NULL);
1000 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1001 return (NULL);
1003 zhp->zpool_hdl = hdl;
1004 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1006 if (zpool_refresh_stats(zhp, &missing) != 0) {
1007 zpool_close(zhp);
1008 return (NULL);
1011 if (missing) {
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1013 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1014 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1015 zpool_close(zhp);
1016 return (NULL);
1019 return (zhp);
1023 * Like the above, but silent on error. Used when iterating over pools (because
1024 * the configuration cache may be out of date).
1027 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1029 zpool_handle_t *zhp;
1030 boolean_t missing;
1032 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1033 return (-1);
1035 zhp->zpool_hdl = hdl;
1036 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1038 if (zpool_refresh_stats(zhp, &missing) != 0) {
1039 zpool_close(zhp);
1040 return (-1);
1043 if (missing) {
1044 zpool_close(zhp);
1045 *ret = NULL;
1046 return (0);
1049 *ret = zhp;
1050 return (0);
1054 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1055 * state.
1057 zpool_handle_t *
1058 zpool_open(libzfs_handle_t *hdl, const char *pool)
1060 zpool_handle_t *zhp;
1062 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1063 return (NULL);
1065 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1066 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1067 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1068 zpool_close(zhp);
1069 return (NULL);
1072 return (zhp);
1076 * Close the handle. Simply frees the memory associated with the handle.
1078 void
1079 zpool_close(zpool_handle_t *zhp)
1081 nvlist_free(zhp->zpool_config);
1082 nvlist_free(zhp->zpool_old_config);
1083 nvlist_free(zhp->zpool_props);
1084 free(zhp);
1088 * Return the name of the pool.
1090 const char *
1091 zpool_get_name(zpool_handle_t *zhp)
1093 return (zhp->zpool_name);
1098 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1101 zpool_get_state(zpool_handle_t *zhp)
1103 return (zhp->zpool_state);
1107 * Create the named pool, using the provided vdev list. It is assumed
1108 * that the consumer has already validated the contents of the nvlist, so we
1109 * don't have to worry about error semantics.
1112 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1113 nvlist_t *props, nvlist_t *fsprops)
1115 zfs_cmd_t zc = { 0 };
1116 nvlist_t *zc_fsprops = NULL;
1117 nvlist_t *zc_props = NULL;
1118 char msg[1024];
1119 int ret = -1;
1121 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1122 "cannot create '%s'"), pool);
1124 if (!zpool_name_valid(hdl, B_FALSE, pool))
1125 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1127 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1128 return (-1);
1130 if (props) {
1131 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1133 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1134 SPA_VERSION_1, flags, msg)) == NULL) {
1135 goto create_failed;
1139 if (fsprops) {
1140 uint64_t zoned;
1141 char *zonestr;
1143 zoned = ((nvlist_lookup_string(fsprops,
1144 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1145 strcmp(zonestr, "on") == 0);
1147 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1148 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1149 goto create_failed;
1151 if (!zc_props &&
1152 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1153 goto create_failed;
1155 if (nvlist_add_nvlist(zc_props,
1156 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1157 goto create_failed;
1161 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1162 goto create_failed;
1164 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1166 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1168 zcmd_free_nvlists(&zc);
1169 nvlist_free(zc_props);
1170 nvlist_free(zc_fsprops);
1172 switch (errno) {
1173 case EBUSY:
1175 * This can happen if the user has specified the same
1176 * device multiple times. We can't reliably detect this
1177 * until we try to add it and see we already have a
1178 * label.
1180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1181 "one or more vdevs refer to the same device"));
1182 return (zfs_error(hdl, EZFS_BADDEV, msg));
1184 case ERANGE:
1186 * This happens if the record size is smaller or larger
1187 * than the allowed size range, or not a power of 2.
1189 * NOTE: although zfs_valid_proplist is called earlier,
1190 * this case may have slipped through since the
1191 * pool does not exist yet and it is therefore
1192 * impossible to read properties e.g. max blocksize
1193 * from the pool.
1195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1196 "record size invalid"));
1197 return (zfs_error(hdl, EZFS_BADPROP, msg));
1199 case EOVERFLOW:
1201 * This occurs when one of the devices is below
1202 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1203 * device was the problem device since there's no
1204 * reliable way to determine device size from userland.
1207 char buf[64];
1209 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1212 "one or more devices is less than the "
1213 "minimum size (%s)"), buf);
1215 return (zfs_error(hdl, EZFS_BADDEV, msg));
1217 case ENOSPC:
1218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1219 "one or more devices is out of space"));
1220 return (zfs_error(hdl, EZFS_BADDEV, msg));
1222 case ENOTBLK:
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "cache device must be a disk or disk slice"));
1225 return (zfs_error(hdl, EZFS_BADDEV, msg));
1227 default:
1228 return (zpool_standard_error(hdl, errno, msg));
1232 create_failed:
1233 zcmd_free_nvlists(&zc);
1234 nvlist_free(zc_props);
1235 nvlist_free(zc_fsprops);
1236 return (ret);
1240 * Destroy the given pool. It is up to the caller to ensure that there are no
1241 * datasets left in the pool.
1244 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1246 zfs_cmd_t zc = { 0 };
1247 zfs_handle_t *zfp = NULL;
1248 libzfs_handle_t *hdl = zhp->zpool_hdl;
1249 char msg[1024];
1251 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1252 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1253 return (-1);
1255 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1256 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1258 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1259 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1260 "cannot destroy '%s'"), zhp->zpool_name);
1262 if (errno == EROFS) {
1263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1264 "one or more devices is read only"));
1265 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1266 } else {
1267 (void) zpool_standard_error(hdl, errno, msg);
1270 if (zfp)
1271 zfs_close(zfp);
1272 return (-1);
1275 if (zfp) {
1276 remove_mountpoint(zfp);
1277 zfs_close(zfp);
1280 return (0);
1284 * Add the given vdevs to the pool. The caller must have already performed the
1285 * necessary verification to ensure that the vdev specification is well-formed.
1288 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1290 zfs_cmd_t zc = { 0 };
1291 int ret;
1292 libzfs_handle_t *hdl = zhp->zpool_hdl;
1293 char msg[1024];
1294 nvlist_t **spares, **l2cache;
1295 uint_t nspares, nl2cache;
1297 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1298 "cannot add to '%s'"), zhp->zpool_name);
1300 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1301 SPA_VERSION_SPARES &&
1302 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1303 &spares, &nspares) == 0) {
1304 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1305 "upgraded to add hot spares"));
1306 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1309 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1310 SPA_VERSION_L2CACHE &&
1311 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1312 &l2cache, &nl2cache) == 0) {
1313 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1314 "upgraded to add cache devices"));
1315 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1318 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1319 return (-1);
1320 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1322 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1323 switch (errno) {
1324 case EBUSY:
1326 * This can happen if the user has specified the same
1327 * device multiple times. We can't reliably detect this
1328 * until we try to add it and see we already have a
1329 * label.
1331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1332 "one or more vdevs refer to the same device"));
1333 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1334 break;
1336 case EINVAL:
1337 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1338 "invalid config; a pool with removing/removed "
1339 "vdevs does not support adding raidz vdevs"));
1340 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1341 break;
1343 case EOVERFLOW:
1345 * This occurrs when one of the devices is below
1346 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1347 * device was the problem device since there's no
1348 * reliable way to determine device size from userland.
1351 char buf[64];
1353 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1356 "device is less than the minimum "
1357 "size (%s)"), buf);
1359 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1360 break;
1362 case ENOTSUP:
1363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1364 "pool must be upgraded to add these vdevs"));
1365 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1366 break;
1368 case EDOM:
1369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1370 "root pool can not have multiple vdevs"
1371 " or separate logs"));
1372 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1373 break;
1375 case ENOTBLK:
1376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377 "cache device must be a disk or disk slice"));
1378 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1379 break;
1381 default:
1382 (void) zpool_standard_error(hdl, errno, msg);
1385 ret = -1;
1386 } else {
1387 ret = 0;
1390 zcmd_free_nvlists(&zc);
1392 return (ret);
1396 * Exports the pool from the system. The caller must ensure that there are no
1397 * mounted datasets in the pool.
1399 static int
1400 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1401 const char *log_str)
1403 zfs_cmd_t zc = { 0 };
1404 char msg[1024];
1406 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1407 "cannot export '%s'"), zhp->zpool_name);
1409 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1410 zc.zc_cookie = force;
1411 zc.zc_guid = hardforce;
1412 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1414 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1415 switch (errno) {
1416 case EXDEV:
1417 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1418 "use '-f' to override the following errors:\n"
1419 "'%s' has an active shared spare which could be"
1420 " used by other pools once '%s' is exported."),
1421 zhp->zpool_name, zhp->zpool_name);
1422 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1423 msg));
1424 default:
1425 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1426 msg));
1430 return (0);
1434 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1436 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1440 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1442 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1445 static void
1446 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1447 nvlist_t *config)
1449 nvlist_t *nv = NULL;
1450 uint64_t rewindto;
1451 int64_t loss = -1;
1452 struct tm t;
1453 char timestr[128];
1455 if (!hdl->libzfs_printerr || config == NULL)
1456 return;
1458 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1459 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1460 return;
1463 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1464 return;
1465 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1467 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1468 strftime(timestr, 128, 0, &t) != 0) {
1469 if (dryrun) {
1470 (void) printf(dgettext(TEXT_DOMAIN,
1471 "Would be able to return %s "
1472 "to its state as of %s.\n"),
1473 name, timestr);
1474 } else {
1475 (void) printf(dgettext(TEXT_DOMAIN,
1476 "Pool %s returned to its state as of %s.\n"),
1477 name, timestr);
1479 if (loss > 120) {
1480 (void) printf(dgettext(TEXT_DOMAIN,
1481 "%s approximately %lld "),
1482 dryrun ? "Would discard" : "Discarded",
1483 (loss + 30) / 60);
1484 (void) printf(dgettext(TEXT_DOMAIN,
1485 "minutes of transactions.\n"));
1486 } else if (loss > 0) {
1487 (void) printf(dgettext(TEXT_DOMAIN,
1488 "%s approximately %lld "),
1489 dryrun ? "Would discard" : "Discarded", loss);
1490 (void) printf(dgettext(TEXT_DOMAIN,
1491 "seconds of transactions.\n"));
1496 void
1497 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1498 nvlist_t *config)
1500 nvlist_t *nv = NULL;
1501 int64_t loss = -1;
1502 uint64_t edata = UINT64_MAX;
1503 uint64_t rewindto;
1504 struct tm t;
1505 char timestr[128];
1507 if (!hdl->libzfs_printerr)
1508 return;
1510 if (reason >= 0)
1511 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1512 else
1513 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1515 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1516 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1517 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1518 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1519 goto no_info;
1521 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1522 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1523 &edata);
1525 (void) printf(dgettext(TEXT_DOMAIN,
1526 "Recovery is possible, but will result in some data loss.\n"));
1528 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1529 strftime(timestr, 128, 0, &t) != 0) {
1530 (void) printf(dgettext(TEXT_DOMAIN,
1531 "\tReturning the pool to its state as of %s\n"
1532 "\tshould correct the problem. "),
1533 timestr);
1534 } else {
1535 (void) printf(dgettext(TEXT_DOMAIN,
1536 "\tReverting the pool to an earlier state "
1537 "should correct the problem.\n\t"));
1540 if (loss > 120) {
1541 (void) printf(dgettext(TEXT_DOMAIN,
1542 "Approximately %lld minutes of data\n"
1543 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1544 } else if (loss > 0) {
1545 (void) printf(dgettext(TEXT_DOMAIN,
1546 "Approximately %lld seconds of data\n"
1547 "\tmust be discarded, irreversibly. "), loss);
1549 if (edata != 0 && edata != UINT64_MAX) {
1550 if (edata == 1) {
1551 (void) printf(dgettext(TEXT_DOMAIN,
1552 "After rewind, at least\n"
1553 "\tone persistent user-data error will remain. "));
1554 } else {
1555 (void) printf(dgettext(TEXT_DOMAIN,
1556 "After rewind, several\n"
1557 "\tpersistent user-data errors will remain. "));
1560 (void) printf(dgettext(TEXT_DOMAIN,
1561 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1562 reason >= 0 ? "clear" : "import", name);
1564 (void) printf(dgettext(TEXT_DOMAIN,
1565 "A scrub of the pool\n"
1566 "\tis strongly recommended after recovery.\n"));
1567 return;
1569 no_info:
1570 (void) printf(dgettext(TEXT_DOMAIN,
1571 "Destroy and re-create the pool from\n\ta backup source.\n"));
1575 * zpool_import() is a contracted interface. Should be kept the same
1576 * if possible.
1578 * Applications should use zpool_import_props() to import a pool with
1579 * new properties value to be set.
1582 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1583 char *altroot)
1585 nvlist_t *props = NULL;
1586 int ret;
1588 if (altroot != NULL) {
1589 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1590 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1591 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1592 newname));
1595 if (nvlist_add_string(props,
1596 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1597 nvlist_add_string(props,
1598 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1599 nvlist_free(props);
1600 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1601 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1602 newname));
1606 ret = zpool_import_props(hdl, config, newname, props,
1607 ZFS_IMPORT_NORMAL);
1608 nvlist_free(props);
1609 return (ret);
1612 static void
1613 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1614 int indent)
1616 nvlist_t **child;
1617 uint_t c, children;
1618 char *vname;
1619 uint64_t is_log = 0;
1621 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1622 &is_log);
1624 if (name != NULL)
1625 (void) printf("\t%*s%s%s\n", indent, "", name,
1626 is_log ? " [log]" : "");
1628 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1629 &child, &children) != 0)
1630 return;
1632 for (c = 0; c < children; c++) {
1633 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1634 print_vdev_tree(hdl, vname, child[c], indent + 2);
1635 free(vname);
1639 void
1640 zpool_print_unsup_feat(nvlist_t *config)
1642 nvlist_t *nvinfo, *unsup_feat;
1644 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1646 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1647 &unsup_feat) == 0);
1649 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1650 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1651 char *desc;
1653 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1654 verify(nvpair_value_string(nvp, &desc) == 0);
1656 if (strlen(desc) > 0)
1657 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1658 else
1659 (void) printf("\t%s\n", nvpair_name(nvp));
1664 * Import the given pool using the known configuration and a list of
1665 * properties to be set. The configuration should have come from
1666 * zpool_find_import(). The 'newname' parameters control whether the pool
1667 * is imported with a different name.
1670 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1671 nvlist_t *props, int flags)
1673 zfs_cmd_t zc = { 0 };
1674 zpool_rewind_policy_t policy;
1675 nvlist_t *nv = NULL;
1676 nvlist_t *nvinfo = NULL;
1677 nvlist_t *missing = NULL;
1678 char *thename;
1679 char *origname;
1680 int ret;
1681 int error = 0;
1682 char errbuf[1024];
1684 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1685 &origname) == 0);
1687 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1688 "cannot import pool '%s'"), origname);
1690 if (newname != NULL) {
1691 if (!zpool_name_valid(hdl, B_FALSE, newname))
1692 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1693 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1694 newname));
1695 thename = (char *)newname;
1696 } else {
1697 thename = origname;
1700 if (props != NULL) {
1701 uint64_t version;
1702 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1704 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1705 &version) == 0);
1707 if ((props = zpool_valid_proplist(hdl, origname,
1708 props, version, flags, errbuf)) == NULL)
1709 return (-1);
1710 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1711 nvlist_free(props);
1712 return (-1);
1714 nvlist_free(props);
1717 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1719 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1720 &zc.zc_guid) == 0);
1722 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1723 zcmd_free_nvlists(&zc);
1724 return (-1);
1726 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1727 zcmd_free_nvlists(&zc);
1728 return (-1);
1731 zc.zc_cookie = flags;
1732 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1733 errno == ENOMEM) {
1734 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1735 zcmd_free_nvlists(&zc);
1736 return (-1);
1739 if (ret != 0)
1740 error = errno;
1742 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1744 zcmd_free_nvlists(&zc);
1746 zpool_get_rewind_policy(config, &policy);
1748 if (error) {
1749 char desc[1024];
1752 * Dry-run failed, but we print out what success
1753 * looks like if we found a best txg
1755 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1756 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1757 B_TRUE, nv);
1758 nvlist_free(nv);
1759 return (-1);
1762 if (newname == NULL)
1763 (void) snprintf(desc, sizeof (desc),
1764 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1765 thename);
1766 else
1767 (void) snprintf(desc, sizeof (desc),
1768 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1769 origname, thename);
1771 switch (error) {
1772 case ENOTSUP:
1773 if (nv != NULL && nvlist_lookup_nvlist(nv,
1774 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1775 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1776 (void) printf(dgettext(TEXT_DOMAIN, "This "
1777 "pool uses the following feature(s) not "
1778 "supported by this system:\n"));
1779 zpool_print_unsup_feat(nv);
1780 if (nvlist_exists(nvinfo,
1781 ZPOOL_CONFIG_CAN_RDONLY)) {
1782 (void) printf(dgettext(TEXT_DOMAIN,
1783 "All unsupported features are only "
1784 "required for writing to the pool."
1785 "\nThe pool can be imported using "
1786 "'-o readonly=on'.\n"));
1790 * Unsupported version.
1792 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1793 break;
1795 case EINVAL:
1796 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1797 break;
1799 case EROFS:
1800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1801 "one or more devices is read only"));
1802 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1803 break;
1805 case ENXIO:
1806 if (nv && nvlist_lookup_nvlist(nv,
1807 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1808 nvlist_lookup_nvlist(nvinfo,
1809 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1810 (void) printf(dgettext(TEXT_DOMAIN,
1811 "The devices below are missing or "
1812 "corrupted, use '-m' to import the pool "
1813 "anyway:\n"));
1814 print_vdev_tree(hdl, NULL, missing, 2);
1815 (void) printf("\n");
1817 (void) zpool_standard_error(hdl, error, desc);
1818 break;
1820 case EEXIST:
1821 (void) zpool_standard_error(hdl, error, desc);
1822 break;
1823 case ENAMETOOLONG:
1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825 "new name of at least one dataset is longer than "
1826 "the maximum allowable length"));
1827 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1828 break;
1829 default:
1830 (void) zpool_standard_error(hdl, error, desc);
1831 zpool_explain_recover(hdl,
1832 newname ? origname : thename, -error, nv);
1833 break;
1836 nvlist_free(nv);
1837 ret = -1;
1838 } else {
1839 zpool_handle_t *zhp;
1842 * This should never fail, but play it safe anyway.
1844 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1845 ret = -1;
1846 else if (zhp != NULL)
1847 zpool_close(zhp);
1848 if (policy.zrp_request &
1849 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1850 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1851 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1853 nvlist_free(nv);
1854 return (0);
1857 return (ret);
1861 * Scan the pool.
1864 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1866 zfs_cmd_t zc = { 0 };
1867 char msg[1024];
1868 int err;
1869 libzfs_handle_t *hdl = zhp->zpool_hdl;
1871 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1872 zc.zc_cookie = func;
1873 zc.zc_flags = cmd;
1875 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1876 return (0);
1878 err = errno;
1880 /* ECANCELED on a scrub means we resumed a paused scrub */
1881 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1882 cmd == POOL_SCRUB_NORMAL)
1883 return (0);
1885 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1886 return (0);
1888 if (func == POOL_SCAN_SCRUB) {
1889 if (cmd == POOL_SCRUB_PAUSE) {
1890 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1891 "cannot pause scrubbing %s"), zc.zc_name);
1892 } else {
1893 assert(cmd == POOL_SCRUB_NORMAL);
1894 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1895 "cannot scrub %s"), zc.zc_name);
1897 } else if (func == POOL_SCAN_NONE) {
1898 (void) snprintf(msg, sizeof (msg),
1899 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1900 zc.zc_name);
1901 } else {
1902 assert(!"unexpected result");
1905 if (err == EBUSY) {
1906 nvlist_t *nvroot;
1907 pool_scan_stat_t *ps = NULL;
1908 uint_t psc;
1910 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1911 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1912 (void) nvlist_lookup_uint64_array(nvroot,
1913 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1914 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1915 if (cmd == POOL_SCRUB_PAUSE)
1916 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1917 else
1918 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1919 } else {
1920 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1922 } else if (err == ENOENT) {
1923 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1924 } else {
1925 return (zpool_standard_error(hdl, err, msg));
1930 * This provides a very minimal check whether a given string is likely a
1931 * c#t#d# style string. Users of this are expected to do their own
1932 * verification of the s# part.
1934 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1937 * More elaborate version for ones which may start with "/dev/dsk/"
1938 * and the like.
1940 static int
1941 ctd_check_path(char *str)
1944 * If it starts with a slash, check the last component.
1946 if (str && str[0] == '/') {
1947 char *tmp = strrchr(str, '/');
1950 * If it ends in "/old", check the second-to-last
1951 * component of the string instead.
1953 if (tmp != str && strcmp(tmp, "/old") == 0) {
1954 for (tmp--; *tmp != '/'; tmp--)
1957 str = tmp + 1;
1959 return (CTD_CHECK(str));
1963 * Find a vdev that matches the search criteria specified. We use the
1964 * the nvpair name to determine how we should look for the device.
1965 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1966 * spare; but FALSE if its an INUSE spare.
1968 static nvlist_t *
1969 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1970 boolean_t *l2cache, boolean_t *log)
1972 uint_t c, children;
1973 nvlist_t **child;
1974 nvlist_t *ret;
1975 uint64_t is_log;
1976 char *srchkey;
1977 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1979 /* Nothing to look for */
1980 if (search == NULL || pair == NULL)
1981 return (NULL);
1983 /* Obtain the key we will use to search */
1984 srchkey = nvpair_name(pair);
1986 switch (nvpair_type(pair)) {
1987 case DATA_TYPE_UINT64:
1988 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1989 uint64_t srchval, theguid;
1991 verify(nvpair_value_uint64(pair, &srchval) == 0);
1992 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1993 &theguid) == 0);
1994 if (theguid == srchval)
1995 return (nv);
1997 break;
1999 case DATA_TYPE_STRING: {
2000 char *srchval, *val;
2002 verify(nvpair_value_string(pair, &srchval) == 0);
2003 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2004 break;
2007 * Search for the requested value. Special cases:
2009 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2010 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2011 * "s1/old". The "s0" or "s1" part is hidden from the user,
2012 * but included in the string, so this matches around it.
2013 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2015 * Otherwise, all other searches are simple string compares.
2017 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2018 ctd_check_path(val)) {
2019 uint64_t wholedisk = 0;
2021 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2022 &wholedisk);
2023 if (wholedisk) {
2024 int slen = strlen(srchval);
2025 int vlen = strlen(val);
2027 if (slen != vlen - 2)
2028 break;
2031 * make_leaf_vdev() should only set
2032 * wholedisk for ZPOOL_CONFIG_PATHs which
2033 * will include "/dev/dsk/", giving plenty of
2034 * room for the indices used next.
2036 ASSERT(vlen >= 6);
2039 * strings identical except trailing "s0"
2041 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2042 strcmp(&val[vlen - 2], "s1") == 0) &&
2043 strncmp(srchval, val, slen) == 0)
2044 return (nv);
2047 * strings identical except trailing "s0/old"
2049 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2050 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2051 strcmp(&srchval[slen - 4], "/old") == 0 &&
2052 strncmp(srchval, val, slen - 4) == 0)
2053 return (nv);
2055 break;
2057 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2058 char *type, *idx, *end, *p;
2059 uint64_t id, vdev_id;
2062 * Determine our vdev type, keeping in mind
2063 * that the srchval is composed of a type and
2064 * vdev id pair (i.e. mirror-4).
2066 if ((type = strdup(srchval)) == NULL)
2067 return (NULL);
2069 if ((p = strrchr(type, '-')) == NULL) {
2070 free(type);
2071 break;
2073 idx = p + 1;
2074 *p = '\0';
2077 * If the types don't match then keep looking.
2079 if (strncmp(val, type, strlen(val)) != 0) {
2080 free(type);
2081 break;
2084 verify(zpool_vdev_is_interior(type));
2085 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2086 &id) == 0);
2088 errno = 0;
2089 vdev_id = strtoull(idx, &end, 10);
2091 free(type);
2092 if (errno != 0)
2093 return (NULL);
2096 * Now verify that we have the correct vdev id.
2098 if (vdev_id == id)
2099 return (nv);
2103 * Common case
2105 if (strcmp(srchval, val) == 0)
2106 return (nv);
2107 break;
2110 default:
2111 break;
2114 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2115 &child, &children) != 0)
2116 return (NULL);
2118 for (c = 0; c < children; c++) {
2119 if ((ret = vdev_to_nvlist_iter(child[c], search,
2120 avail_spare, l2cache, NULL)) != NULL) {
2122 * The 'is_log' value is only set for the toplevel
2123 * vdev, not the leaf vdevs. So we always lookup the
2124 * log device from the root of the vdev tree (where
2125 * 'log' is non-NULL).
2127 if (log != NULL &&
2128 nvlist_lookup_uint64(child[c],
2129 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2130 is_log) {
2131 *log = B_TRUE;
2133 return (ret);
2137 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2138 &child, &children) == 0) {
2139 for (c = 0; c < children; c++) {
2140 if ((ret = vdev_to_nvlist_iter(child[c], search,
2141 avail_spare, l2cache, NULL)) != NULL) {
2142 *avail_spare = B_TRUE;
2143 return (ret);
2148 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2149 &child, &children) == 0) {
2150 for (c = 0; c < children; c++) {
2151 if ((ret = vdev_to_nvlist_iter(child[c], search,
2152 avail_spare, l2cache, NULL)) != NULL) {
2153 *l2cache = B_TRUE;
2154 return (ret);
2159 return (NULL);
2163 * Given a physical path (minus the "/devices" prefix), find the
2164 * associated vdev.
2166 nvlist_t *
2167 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2168 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2170 nvlist_t *search, *nvroot, *ret;
2172 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2173 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2175 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2176 &nvroot) == 0);
2178 *avail_spare = B_FALSE;
2179 *l2cache = B_FALSE;
2180 if (log != NULL)
2181 *log = B_FALSE;
2182 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2183 nvlist_free(search);
2185 return (ret);
2189 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2191 static boolean_t
2192 zpool_vdev_is_interior(const char *name)
2194 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2195 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2196 strncmp(name,
2197 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2198 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2199 return (B_TRUE);
2200 return (B_FALSE);
2203 nvlist_t *
2204 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2205 boolean_t *l2cache, boolean_t *log)
2207 char buf[MAXPATHLEN];
2208 char *end;
2209 nvlist_t *nvroot, *search, *ret;
2210 uint64_t guid;
2212 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2214 guid = strtoull(path, &end, 10);
2215 if (guid != 0 && *end == '\0') {
2216 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2217 } else if (zpool_vdev_is_interior(path)) {
2218 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2219 } else if (path[0] != '/') {
2220 (void) snprintf(buf, sizeof (buf), "%s/%s", ZFS_DISK_ROOT,
2221 path);
2222 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2223 } else {
2224 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2227 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2228 &nvroot) == 0);
2230 *avail_spare = B_FALSE;
2231 *l2cache = B_FALSE;
2232 if (log != NULL)
2233 *log = B_FALSE;
2234 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2235 nvlist_free(search);
2237 return (ret);
2240 static int
2241 vdev_online(nvlist_t *nv)
2243 uint64_t ival;
2245 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2246 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2247 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2248 return (0);
2250 return (1);
2254 * Helper function for zpool_get_physpaths().
2256 static int
2257 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2258 size_t *bytes_written)
2260 size_t bytes_left, pos, rsz;
2261 char *tmppath;
2262 const char *format;
2264 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2265 &tmppath) != 0)
2266 return (EZFS_NODEVICE);
2268 pos = *bytes_written;
2269 bytes_left = physpath_size - pos;
2270 format = (pos == 0) ? "%s" : " %s";
2272 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2273 *bytes_written += rsz;
2275 if (rsz >= bytes_left) {
2276 /* if physpath was not copied properly, clear it */
2277 if (bytes_left != 0) {
2278 physpath[pos] = 0;
2280 return (EZFS_NOSPC);
2282 return (0);
2285 static int
2286 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2287 size_t *rsz, boolean_t is_spare)
2289 char *type;
2290 int ret;
2292 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2293 return (EZFS_INVALCONFIG);
2295 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2297 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2298 * For a spare vdev, we only want to boot from the active
2299 * spare device.
2301 if (is_spare) {
2302 uint64_t spare = 0;
2303 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2304 &spare);
2305 if (!spare)
2306 return (EZFS_INVALCONFIG);
2309 if (vdev_online(nv)) {
2310 if ((ret = vdev_get_one_physpath(nv, physpath,
2311 phypath_size, rsz)) != 0)
2312 return (ret);
2314 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2315 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2316 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2317 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2318 nvlist_t **child;
2319 uint_t count;
2320 int i, ret;
2322 if (nvlist_lookup_nvlist_array(nv,
2323 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2324 return (EZFS_INVALCONFIG);
2326 for (i = 0; i < count; i++) {
2327 ret = vdev_get_physpaths(child[i], physpath,
2328 phypath_size, rsz, is_spare);
2329 if (ret == EZFS_NOSPC)
2330 return (ret);
2334 return (EZFS_POOL_INVALARG);
2338 * Get phys_path for a root pool config.
2339 * Return 0 on success; non-zero on failure.
2341 static int
2342 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2344 size_t rsz;
2345 nvlist_t *vdev_root;
2346 nvlist_t **child;
2347 uint_t count;
2348 char *type;
2350 rsz = 0;
2352 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2353 &vdev_root) != 0)
2354 return (EZFS_INVALCONFIG);
2356 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2357 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2358 &child, &count) != 0)
2359 return (EZFS_INVALCONFIG);
2362 * root pool can only have a single top-level vdev.
2364 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2365 return (EZFS_POOL_INVALARG);
2367 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2368 B_FALSE);
2370 /* No online devices */
2371 if (rsz == 0)
2372 return (EZFS_NODEVICE);
2374 return (0);
2378 * Get phys_path for a root pool
2379 * Return 0 on success; non-zero on failure.
2382 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2384 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2385 phypath_size));
2389 * If the device has being dynamically expanded then we need to relabel
2390 * the disk to use the new unallocated space.
2392 static int
2393 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2395 char path[MAXPATHLEN];
2396 char errbuf[1024];
2397 int fd, error;
2398 int (*_efi_use_whole_disk)(int);
2400 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2401 "efi_use_whole_disk")) == NULL)
2402 return (-1);
2404 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2406 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2408 "relabel '%s': unable to open device"), name);
2409 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2413 * It's possible that we might encounter an error if the device
2414 * does not have any unallocated space left. If so, we simply
2415 * ignore that error and continue on.
2417 error = _efi_use_whole_disk(fd);
2418 (void) close(fd);
2419 if (error && error != VT_ENOSPC) {
2420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2421 "relabel '%s': unable to read disk capacity"), name);
2422 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2424 return (0);
2428 * Bring the specified vdev online. The 'flags' parameter is a set of the
2429 * ZFS_ONLINE_* flags.
2432 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2433 vdev_state_t *newstate)
2435 zfs_cmd_t zc = { 0 };
2436 char msg[1024];
2437 char *pathname;
2438 nvlist_t *tgt;
2439 boolean_t avail_spare, l2cache, islog;
2440 libzfs_handle_t *hdl = zhp->zpool_hdl;
2442 if (flags & ZFS_ONLINE_EXPAND) {
2443 (void) snprintf(msg, sizeof (msg),
2444 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2445 } else {
2446 (void) snprintf(msg, sizeof (msg),
2447 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2450 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2451 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2452 &islog)) == NULL)
2453 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2455 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2457 if (avail_spare)
2458 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2460 if ((flags & ZFS_ONLINE_EXPAND ||
2461 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2462 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2463 uint64_t wholedisk = 0;
2465 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2466 &wholedisk);
2469 * XXX - L2ARC 1.0 devices can't support expansion.
2471 if (l2cache) {
2472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2473 "cannot expand cache devices"));
2474 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2477 if (wholedisk) {
2478 pathname += strlen(ZFS_DISK_ROOT) + 1;
2479 (void) zpool_relabel_disk(hdl, pathname);
2483 zc.zc_cookie = VDEV_STATE_ONLINE;
2484 zc.zc_obj = flags;
2486 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2487 if (errno == EINVAL) {
2488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2489 "from this pool into a new one. Use '%s' "
2490 "instead"), "zpool detach");
2491 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2493 return (zpool_standard_error(hdl, errno, msg));
2496 *newstate = zc.zc_cookie;
2497 return (0);
2501 * Take the specified vdev offline
2504 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2506 zfs_cmd_t zc = { 0 };
2507 char msg[1024];
2508 nvlist_t *tgt;
2509 boolean_t avail_spare, l2cache;
2510 libzfs_handle_t *hdl = zhp->zpool_hdl;
2512 (void) snprintf(msg, sizeof (msg),
2513 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2515 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2516 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2517 NULL)) == NULL)
2518 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2520 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2522 if (avail_spare)
2523 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2525 zc.zc_cookie = VDEV_STATE_OFFLINE;
2526 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2528 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2529 return (0);
2531 switch (errno) {
2532 case EBUSY:
2535 * There are no other replicas of this device.
2537 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2539 case EEXIST:
2541 * The log device has unplayed logs
2543 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2545 default:
2546 return (zpool_standard_error(hdl, errno, msg));
2551 * Mark the given vdev faulted.
2554 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2556 zfs_cmd_t zc = { 0 };
2557 char msg[1024];
2558 libzfs_handle_t *hdl = zhp->zpool_hdl;
2560 (void) snprintf(msg, sizeof (msg),
2561 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2563 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2564 zc.zc_guid = guid;
2565 zc.zc_cookie = VDEV_STATE_FAULTED;
2566 zc.zc_obj = aux;
2568 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2569 return (0);
2571 switch (errno) {
2572 case EBUSY:
2575 * There are no other replicas of this device.
2577 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2579 default:
2580 return (zpool_standard_error(hdl, errno, msg));
2586 * Mark the given vdev degraded.
2589 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2591 zfs_cmd_t zc = { 0 };
2592 char msg[1024];
2593 libzfs_handle_t *hdl = zhp->zpool_hdl;
2595 (void) snprintf(msg, sizeof (msg),
2596 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2598 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2599 zc.zc_guid = guid;
2600 zc.zc_cookie = VDEV_STATE_DEGRADED;
2601 zc.zc_obj = aux;
2603 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2604 return (0);
2606 return (zpool_standard_error(hdl, errno, msg));
2610 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2611 * a hot spare.
2613 static boolean_t
2614 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2616 nvlist_t **child;
2617 uint_t c, children;
2618 char *type;
2620 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2621 &children) == 0) {
2622 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2623 &type) == 0);
2625 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2626 children == 2 && child[which] == tgt)
2627 return (B_TRUE);
2629 for (c = 0; c < children; c++)
2630 if (is_replacing_spare(child[c], tgt, which))
2631 return (B_TRUE);
2634 return (B_FALSE);
2638 * Attach new_disk (fully described by nvroot) to old_disk.
2639 * If 'replacing' is specified, the new disk will replace the old one.
2642 zpool_vdev_attach(zpool_handle_t *zhp,
2643 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2645 zfs_cmd_t zc = { 0 };
2646 char msg[1024];
2647 int ret;
2648 nvlist_t *tgt;
2649 boolean_t avail_spare, l2cache, islog;
2650 uint64_t val;
2651 char *newname;
2652 nvlist_t **child;
2653 uint_t children;
2654 nvlist_t *config_root;
2655 libzfs_handle_t *hdl = zhp->zpool_hdl;
2656 boolean_t rootpool = zpool_is_bootable(zhp);
2658 if (replacing)
2659 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2660 "cannot replace %s with %s"), old_disk, new_disk);
2661 else
2662 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2663 "cannot attach %s to %s"), new_disk, old_disk);
2665 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2666 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2667 &islog)) == NULL)
2668 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2670 if (avail_spare)
2671 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2673 if (l2cache)
2674 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2676 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2677 zc.zc_cookie = replacing;
2679 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2680 &child, &children) != 0 || children != 1) {
2681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2682 "new device must be a single disk"));
2683 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2686 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2687 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2689 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2690 return (-1);
2693 * If the target is a hot spare that has been swapped in, we can only
2694 * replace it with another hot spare.
2696 if (replacing &&
2697 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2698 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2699 NULL) == NULL || !avail_spare) &&
2700 is_replacing_spare(config_root, tgt, 1)) {
2701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2702 "can only be replaced by another hot spare"));
2703 free(newname);
2704 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2707 free(newname);
2709 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2710 return (-1);
2712 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2714 zcmd_free_nvlists(&zc);
2716 if (ret == 0) {
2717 if (rootpool) {
2719 * XXX need a better way to prevent user from
2720 * booting up a half-baked vdev.
2722 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2723 "sure to wait until resilver is done "
2724 "before rebooting.\n"));
2726 return (0);
2729 switch (errno) {
2730 case ENOTSUP:
2732 * Can't attach to or replace this type of vdev.
2734 if (replacing) {
2735 uint64_t version = zpool_get_prop_int(zhp,
2736 ZPOOL_PROP_VERSION, NULL);
2738 if (islog)
2739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2740 "cannot replace a log with a spare"));
2741 else if (version >= SPA_VERSION_MULTI_REPLACE)
2742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2743 "already in replacing/spare config; wait "
2744 "for completion or use 'zpool detach'"));
2745 else
2746 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2747 "cannot replace a replacing device"));
2748 } else {
2749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2750 "can only attach to mirrors and top-level "
2751 "disks"));
2753 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2754 break;
2756 case EINVAL:
2758 * The new device must be a single disk.
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2761 "new device must be a single disk"));
2762 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2763 break;
2765 case EBUSY:
2766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2767 "or pool has removing/removed vdevs"),
2768 new_disk);
2769 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2770 break;
2772 case EOVERFLOW:
2774 * The new device is too small.
2776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2777 "device is too small"));
2778 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2779 break;
2781 case EDOM:
2783 * The new device has a different alignment requirement.
2785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2786 "devices have different sector alignment"));
2787 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2788 break;
2790 case ENAMETOOLONG:
2792 * The resulting top-level vdev spec won't fit in the label.
2794 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2795 break;
2797 default:
2798 (void) zpool_standard_error(hdl, errno, msg);
2801 return (-1);
2805 * Detach the specified device.
2808 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2810 zfs_cmd_t zc = { 0 };
2811 char msg[1024];
2812 nvlist_t *tgt;
2813 boolean_t avail_spare, l2cache;
2814 libzfs_handle_t *hdl = zhp->zpool_hdl;
2816 (void) snprintf(msg, sizeof (msg),
2817 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2819 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2820 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2821 NULL)) == NULL)
2822 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2824 if (avail_spare)
2825 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2827 if (l2cache)
2828 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2830 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2832 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2833 return (0);
2835 switch (errno) {
2837 case ENOTSUP:
2839 * Can't detach from this type of vdev.
2841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2842 "applicable to mirror and replacing vdevs"));
2843 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2844 break;
2846 case EBUSY:
2848 * There are no other replicas of this device.
2850 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2851 break;
2853 default:
2854 (void) zpool_standard_error(hdl, errno, msg);
2857 return (-1);
2861 * Find a mirror vdev in the source nvlist.
2863 * The mchild array contains a list of disks in one of the top-level mirrors
2864 * of the source pool. The schild array contains a list of disks that the
2865 * user specified on the command line. We loop over the mchild array to
2866 * see if any entry in the schild array matches.
2868 * If a disk in the mchild array is found in the schild array, we return
2869 * the index of that entry. Otherwise we return -1.
2871 static int
2872 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2873 nvlist_t **schild, uint_t schildren)
2875 uint_t mc;
2877 for (mc = 0; mc < mchildren; mc++) {
2878 uint_t sc;
2879 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2880 mchild[mc], B_FALSE);
2882 for (sc = 0; sc < schildren; sc++) {
2883 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2884 schild[sc], B_FALSE);
2885 boolean_t result = (strcmp(mpath, spath) == 0);
2887 free(spath);
2888 if (result) {
2889 free(mpath);
2890 return (mc);
2894 free(mpath);
2897 return (-1);
2901 * Split a mirror pool. If newroot points to null, then a new nvlist
2902 * is generated and it is the responsibility of the caller to free it.
2905 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2906 nvlist_t *props, splitflags_t flags)
2908 zfs_cmd_t zc = { 0 };
2909 char msg[1024];
2910 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2911 nvlist_t **varray = NULL, *zc_props = NULL;
2912 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2913 libzfs_handle_t *hdl = zhp->zpool_hdl;
2914 uint64_t vers;
2915 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2916 int retval = 0;
2918 (void) snprintf(msg, sizeof (msg),
2919 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2921 if (!zpool_name_valid(hdl, B_FALSE, newname))
2922 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2924 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2925 (void) fprintf(stderr, gettext("Internal error: unable to "
2926 "retrieve pool configuration\n"));
2927 return (-1);
2930 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2931 == 0);
2932 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2934 if (props) {
2935 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2936 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2937 props, vers, flags, msg)) == NULL)
2938 return (-1);
2941 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2942 &children) != 0) {
2943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2944 "Source pool is missing vdev tree"));
2945 nvlist_free(zc_props);
2946 return (-1);
2949 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2950 vcount = 0;
2952 if (*newroot == NULL ||
2953 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2954 &newchild, &newchildren) != 0)
2955 newchildren = 0;
2957 for (c = 0; c < children; c++) {
2958 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2959 char *type;
2960 nvlist_t **mchild, *vdev;
2961 uint_t mchildren;
2962 int entry;
2965 * Unlike cache & spares, slogs are stored in the
2966 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2968 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2969 &is_log);
2970 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2971 &is_hole);
2972 if (is_log || is_hole) {
2974 * Create a hole vdev and put it in the config.
2976 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2977 goto out;
2978 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2979 VDEV_TYPE_HOLE) != 0)
2980 goto out;
2981 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2982 1) != 0)
2983 goto out;
2984 if (lastlog == 0)
2985 lastlog = vcount;
2986 varray[vcount++] = vdev;
2987 continue;
2989 lastlog = 0;
2990 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2991 == 0);
2992 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2993 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2994 "Source pool must be composed only of mirrors\n"));
2995 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2996 goto out;
2999 verify(nvlist_lookup_nvlist_array(child[c],
3000 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3002 /* find or add an entry for this top-level vdev */
3003 if (newchildren > 0 &&
3004 (entry = find_vdev_entry(zhp, mchild, mchildren,
3005 newchild, newchildren)) >= 0) {
3006 /* We found a disk that the user specified. */
3007 vdev = mchild[entry];
3008 ++found;
3009 } else {
3010 /* User didn't specify a disk for this vdev. */
3011 vdev = mchild[mchildren - 1];
3014 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3015 goto out;
3018 /* did we find every disk the user specified? */
3019 if (found != newchildren) {
3020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3021 "include at most one disk from each mirror"));
3022 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3023 goto out;
3026 /* Prepare the nvlist for populating. */
3027 if (*newroot == NULL) {
3028 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3029 goto out;
3030 freelist = B_TRUE;
3031 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3032 VDEV_TYPE_ROOT) != 0)
3033 goto out;
3034 } else {
3035 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3038 /* Add all the children we found */
3039 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3040 lastlog == 0 ? vcount : lastlog) != 0)
3041 goto out;
3044 * If we're just doing a dry run, exit now with success.
3046 if (flags.dryrun) {
3047 memory_err = B_FALSE;
3048 freelist = B_FALSE;
3049 goto out;
3052 /* now build up the config list & call the ioctl */
3053 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3054 goto out;
3056 if (nvlist_add_nvlist(newconfig,
3057 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3058 nvlist_add_string(newconfig,
3059 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3060 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3061 goto out;
3064 * The new pool is automatically part of the namespace unless we
3065 * explicitly export it.
3067 if (!flags.import)
3068 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3069 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3070 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3071 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3072 goto out;
3073 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3074 goto out;
3076 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3077 retval = zpool_standard_error(hdl, errno, msg);
3078 goto out;
3081 freelist = B_FALSE;
3082 memory_err = B_FALSE;
3084 out:
3085 if (varray != NULL) {
3086 int v;
3088 for (v = 0; v < vcount; v++)
3089 nvlist_free(varray[v]);
3090 free(varray);
3092 zcmd_free_nvlists(&zc);
3093 nvlist_free(zc_props);
3094 nvlist_free(newconfig);
3095 if (freelist) {
3096 nvlist_free(*newroot);
3097 *newroot = NULL;
3100 if (retval != 0)
3101 return (retval);
3103 if (memory_err)
3104 return (no_memory(hdl));
3106 return (0);
3110 * Remove the given device.
3113 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3115 zfs_cmd_t zc = { 0 };
3116 char msg[1024];
3117 nvlist_t *tgt;
3118 boolean_t avail_spare, l2cache, islog;
3119 libzfs_handle_t *hdl = zhp->zpool_hdl;
3120 uint64_t version;
3122 (void) snprintf(msg, sizeof (msg),
3123 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3125 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3126 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3127 &islog)) == NULL)
3128 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3130 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3131 if (islog && version < SPA_VERSION_HOLES) {
3132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3133 "pool must be upgraded to support log removal"));
3134 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3137 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3139 "root pool can not have removed devices, "
3140 "because GRUB does not understand them"));
3141 return (zfs_error(hdl, EINVAL, msg));
3144 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3146 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3147 return (0);
3149 switch (errno) {
3151 case EINVAL:
3152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3153 "invalid config; all top-level vdevs must "
3154 "have the same sector size and not be raidz."));
3155 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3156 break;
3158 case EBUSY:
3159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3160 "Pool busy; removal may already be in progress"));
3161 (void) zfs_error(hdl, EZFS_BUSY, msg);
3162 break;
3164 default:
3165 (void) zpool_standard_error(hdl, errno, msg);
3167 return (-1);
3171 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3173 zfs_cmd_t zc = { 0 };
3174 char msg[1024];
3175 libzfs_handle_t *hdl = zhp->zpool_hdl;
3177 (void) snprintf(msg, sizeof (msg),
3178 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3180 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3181 zc.zc_cookie = 1;
3183 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3184 return (0);
3186 return (zpool_standard_error(hdl, errno, msg));
3190 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3191 uint64_t *sizep)
3193 char msg[1024];
3194 nvlist_t *tgt;
3195 boolean_t avail_spare, l2cache, islog;
3196 libzfs_handle_t *hdl = zhp->zpool_hdl;
3198 (void) snprintf(msg, sizeof (msg),
3199 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3200 path);
3202 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3203 &islog)) == NULL)
3204 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3206 if (avail_spare || l2cache || islog) {
3207 *sizep = 0;
3208 return (0);
3211 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3213 "indirect size not available"));
3214 return (zfs_error(hdl, EINVAL, msg));
3216 return (0);
3220 * Clear the errors for the pool, or the particular device if specified.
3223 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3225 zfs_cmd_t zc = { 0 };
3226 char msg[1024];
3227 nvlist_t *tgt;
3228 zpool_rewind_policy_t policy;
3229 boolean_t avail_spare, l2cache;
3230 libzfs_handle_t *hdl = zhp->zpool_hdl;
3231 nvlist_t *nvi = NULL;
3232 int error;
3234 if (path)
3235 (void) snprintf(msg, sizeof (msg),
3236 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3237 path);
3238 else
3239 (void) snprintf(msg, sizeof (msg),
3240 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3241 zhp->zpool_name);
3243 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3244 if (path) {
3245 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3246 &l2cache, NULL)) == NULL)
3247 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3250 * Don't allow error clearing for hot spares. Do allow
3251 * error clearing for l2cache devices.
3253 if (avail_spare)
3254 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3256 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3257 &zc.zc_guid) == 0);
3260 zpool_get_rewind_policy(rewindnvl, &policy);
3261 zc.zc_cookie = policy.zrp_request;
3263 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3264 return (-1);
3266 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3267 return (-1);
3269 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3270 errno == ENOMEM) {
3271 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3272 zcmd_free_nvlists(&zc);
3273 return (-1);
3277 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3278 errno != EPERM && errno != EACCES)) {
3279 if (policy.zrp_request &
3280 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3281 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3282 zpool_rewind_exclaim(hdl, zc.zc_name,
3283 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3284 nvi);
3285 nvlist_free(nvi);
3287 zcmd_free_nvlists(&zc);
3288 return (0);
3291 zcmd_free_nvlists(&zc);
3292 return (zpool_standard_error(hdl, errno, msg));
3296 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3299 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3301 zfs_cmd_t zc = { 0 };
3302 char msg[1024];
3303 libzfs_handle_t *hdl = zhp->zpool_hdl;
3305 (void) snprintf(msg, sizeof (msg),
3306 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3307 guid);
3309 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3310 zc.zc_guid = guid;
3311 zc.zc_cookie = ZPOOL_NO_REWIND;
3313 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3314 return (0);
3316 return (zpool_standard_error(hdl, errno, msg));
3320 * Change the GUID for a pool.
3323 zpool_reguid(zpool_handle_t *zhp)
3325 char msg[1024];
3326 libzfs_handle_t *hdl = zhp->zpool_hdl;
3327 zfs_cmd_t zc = { 0 };
3329 (void) snprintf(msg, sizeof (msg),
3330 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3332 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3333 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3334 return (0);
3336 return (zpool_standard_error(hdl, errno, msg));
3340 * Reopen the pool.
3343 zpool_reopen(zpool_handle_t *zhp)
3345 zfs_cmd_t zc = { 0 };
3346 char msg[1024];
3347 libzfs_handle_t *hdl = zhp->zpool_hdl;
3349 (void) snprintf(msg, sizeof (msg),
3350 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3351 zhp->zpool_name);
3353 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3354 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3355 return (0);
3356 return (zpool_standard_error(hdl, errno, msg));
3360 * Convert from a devid string to a path.
3362 static char *
3363 devid_to_path(char *devid_str)
3365 ddi_devid_t devid;
3366 char *minor;
3367 char *path;
3368 devid_nmlist_t *list = NULL;
3369 int ret;
3371 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3372 return (NULL);
3374 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3376 devid_str_free(minor);
3377 devid_free(devid);
3379 if (ret != 0)
3380 return (NULL);
3383 * In a case the strdup() fails, we will just return NULL below.
3385 path = strdup(list[0].devname);
3387 devid_free_nmlist(list);
3389 return (path);
3393 * Convert from a path to a devid string.
3395 static char *
3396 path_to_devid(const char *path)
3398 int fd;
3399 ddi_devid_t devid;
3400 char *minor, *ret;
3402 if ((fd = open(path, O_RDONLY)) < 0)
3403 return (NULL);
3405 minor = NULL;
3406 ret = NULL;
3407 if (devid_get(fd, &devid) == 0) {
3408 if (devid_get_minor_name(fd, &minor) == 0)
3409 ret = devid_str_encode(devid, minor);
3410 if (minor != NULL)
3411 devid_str_free(minor);
3412 devid_free(devid);
3414 (void) close(fd);
3416 return (ret);
3420 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3421 * ignore any failure here, since a common case is for an unprivileged user to
3422 * type 'zpool status', and we'll display the correct information anyway.
3424 static void
3425 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3427 zfs_cmd_t zc = { 0 };
3429 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3430 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3431 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3432 &zc.zc_guid) == 0);
3434 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3438 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3439 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3440 * We also check if this is a whole disk, in which case we strip off the
3441 * trailing 's0' slice name.
3443 * This routine is also responsible for identifying when disks have been
3444 * reconfigured in a new location. The kernel will have opened the device by
3445 * devid, but the path will still refer to the old location. To catch this, we
3446 * first do a path -> devid translation (which is fast for the common case). If
3447 * the devid matches, we're done. If not, we do a reverse devid -> path
3448 * translation and issue the appropriate ioctl() to update the path of the vdev.
3449 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3450 * of these checks.
3452 char *
3453 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3454 boolean_t verbose)
3456 char *path, *devid;
3457 uint64_t value;
3458 char buf[64];
3459 vdev_stat_t *vs;
3460 uint_t vsc;
3462 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3463 &value) == 0) {
3464 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3465 &value) == 0);
3466 (void) snprintf(buf, sizeof (buf), "%llu",
3467 (u_longlong_t)value);
3468 path = buf;
3469 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3472 * If the device is dead (faulted, offline, etc) then don't
3473 * bother opening it. Otherwise we may be forcing the user to
3474 * open a misbehaving device, which can have undesirable
3475 * effects.
3477 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3478 (uint64_t **)&vs, &vsc) != 0 ||
3479 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3480 zhp != NULL &&
3481 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3483 * Determine if the current path is correct.
3485 char *newdevid = path_to_devid(path);
3487 if (newdevid == NULL ||
3488 strcmp(devid, newdevid) != 0) {
3489 char *newpath;
3491 if ((newpath = devid_to_path(devid)) != NULL) {
3493 * Update the path appropriately.
3495 set_path(zhp, nv, newpath);
3496 if (nvlist_add_string(nv,
3497 ZPOOL_CONFIG_PATH, newpath) == 0)
3498 verify(nvlist_lookup_string(nv,
3499 ZPOOL_CONFIG_PATH,
3500 &path) == 0);
3501 free(newpath);
3505 if (newdevid)
3506 devid_str_free(newdevid);
3509 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3510 path += strlen(ZFS_DISK_ROOTD);
3512 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3513 &value) == 0 && value) {
3514 int pathlen = strlen(path);
3515 char *tmp = zfs_strdup(hdl, path);
3518 * If it starts with c#, and ends with "s0" or "s1",
3519 * chop the slice off, or if it ends with "s0/old" or
3520 * "s1/old", remove the slice from the middle.
3522 if (CTD_CHECK(tmp)) {
3523 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3524 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3525 tmp[pathlen - 2] = '\0';
3526 } else if (pathlen > 6 &&
3527 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3528 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3529 (void) strcpy(&tmp[pathlen - 6],
3530 "/old");
3533 return (tmp);
3535 } else {
3536 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3539 * If it's a raidz device, we need to stick in the parity level.
3541 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3542 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3543 &value) == 0);
3544 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3545 (u_longlong_t)value);
3546 path = buf;
3550 * We identify each top-level vdev by using a <type-id>
3551 * naming convention.
3553 if (verbose) {
3554 uint64_t id;
3556 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3557 &id) == 0);
3558 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3559 (u_longlong_t)id);
3560 path = buf;
3564 return (zfs_strdup(hdl, path));
3567 static int
3568 zbookmark_mem_compare(const void *a, const void *b)
3570 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3574 * Retrieve the persistent error log, uniquify the members, and return to the
3575 * caller.
3578 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3580 zfs_cmd_t zc = { 0 };
3581 uint64_t count;
3582 zbookmark_phys_t *zb = NULL;
3583 int i;
3586 * Retrieve the raw error list from the kernel. If the number of errors
3587 * has increased, allocate more space and continue until we get the
3588 * entire list.
3590 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3591 &count) == 0);
3592 if (count == 0)
3593 return (0);
3594 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3595 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3596 return (-1);
3597 zc.zc_nvlist_dst_size = count;
3598 (void) strcpy(zc.zc_name, zhp->zpool_name);
3599 for (;;) {
3600 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3601 &zc) != 0) {
3602 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3603 if (errno == ENOMEM) {
3604 void *dst;
3606 count = zc.zc_nvlist_dst_size;
3607 dst = zfs_alloc(zhp->zpool_hdl, count *
3608 sizeof (zbookmark_phys_t));
3609 if (dst == NULL)
3610 return (-1);
3611 zc.zc_nvlist_dst = (uintptr_t)dst;
3612 } else {
3613 return (-1);
3615 } else {
3616 break;
3621 * Sort the resulting bookmarks. This is a little confusing due to the
3622 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3623 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3624 * _not_ copied as part of the process. So we point the start of our
3625 * array appropriate and decrement the total number of elements.
3627 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3628 zc.zc_nvlist_dst_size;
3629 count -= zc.zc_nvlist_dst_size;
3631 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3633 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3636 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3638 for (i = 0; i < count; i++) {
3639 nvlist_t *nv;
3641 /* ignoring zb_blkid and zb_level for now */
3642 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3643 zb[i-1].zb_object == zb[i].zb_object)
3644 continue;
3646 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3647 goto nomem;
3648 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3649 zb[i].zb_objset) != 0) {
3650 nvlist_free(nv);
3651 goto nomem;
3653 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3654 zb[i].zb_object) != 0) {
3655 nvlist_free(nv);
3656 goto nomem;
3658 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3659 nvlist_free(nv);
3660 goto nomem;
3662 nvlist_free(nv);
3665 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3666 return (0);
3668 nomem:
3669 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3670 return (no_memory(zhp->zpool_hdl));
3674 * Upgrade a ZFS pool to the latest on-disk version.
3677 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3679 zfs_cmd_t zc = { 0 };
3680 libzfs_handle_t *hdl = zhp->zpool_hdl;
3682 (void) strcpy(zc.zc_name, zhp->zpool_name);
3683 zc.zc_cookie = new_version;
3685 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3686 return (zpool_standard_error_fmt(hdl, errno,
3687 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3688 zhp->zpool_name));
3689 return (0);
3692 void
3693 zfs_save_arguments(int argc, char **argv, char *string, int len)
3695 (void) strlcpy(string, basename(argv[0]), len);
3696 for (int i = 1; i < argc; i++) {
3697 (void) strlcat(string, " ", len);
3698 (void) strlcat(string, argv[i], len);
3703 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3705 zfs_cmd_t zc = { 0 };
3706 nvlist_t *args;
3707 int err;
3709 args = fnvlist_alloc();
3710 fnvlist_add_string(args, "message", message);
3711 err = zcmd_write_src_nvlist(hdl, &zc, args);
3712 if (err == 0)
3713 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3714 nvlist_free(args);
3715 zcmd_free_nvlists(&zc);
3716 return (err);
3720 * Perform ioctl to get some command history of a pool.
3722 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3723 * logical offset of the history buffer to start reading from.
3725 * Upon return, 'off' is the next logical offset to read from and
3726 * 'len' is the actual amount of bytes read into 'buf'.
3728 static int
3729 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3731 zfs_cmd_t zc = { 0 };
3732 libzfs_handle_t *hdl = zhp->zpool_hdl;
3734 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3736 zc.zc_history = (uint64_t)(uintptr_t)buf;
3737 zc.zc_history_len = *len;
3738 zc.zc_history_offset = *off;
3740 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3741 switch (errno) {
3742 case EPERM:
3743 return (zfs_error_fmt(hdl, EZFS_PERM,
3744 dgettext(TEXT_DOMAIN,
3745 "cannot show history for pool '%s'"),
3746 zhp->zpool_name));
3747 case ENOENT:
3748 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3749 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3750 "'%s'"), zhp->zpool_name));
3751 case ENOTSUP:
3752 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3753 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3754 "'%s', pool must be upgraded"), zhp->zpool_name));
3755 default:
3756 return (zpool_standard_error_fmt(hdl, errno,
3757 dgettext(TEXT_DOMAIN,
3758 "cannot get history for '%s'"), zhp->zpool_name));
3762 *len = zc.zc_history_len;
3763 *off = zc.zc_history_offset;
3765 return (0);
3769 * Process the buffer of nvlists, unpacking and storing each nvlist record
3770 * into 'records'. 'leftover' is set to the number of bytes that weren't
3771 * processed as there wasn't a complete record.
3774 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3775 nvlist_t ***records, uint_t *numrecords)
3777 uint64_t reclen;
3778 nvlist_t *nv;
3779 int i;
3781 while (bytes_read > sizeof (reclen)) {
3783 /* get length of packed record (stored as little endian) */
3784 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3785 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3787 if (bytes_read < sizeof (reclen) + reclen)
3788 break;
3790 /* unpack record */
3791 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3792 return (ENOMEM);
3793 bytes_read -= sizeof (reclen) + reclen;
3794 buf += sizeof (reclen) + reclen;
3796 /* add record to nvlist array */
3797 (*numrecords)++;
3798 if (ISP2(*numrecords + 1)) {
3799 *records = realloc(*records,
3800 *numrecords * 2 * sizeof (nvlist_t *));
3802 (*records)[*numrecords - 1] = nv;
3805 *leftover = bytes_read;
3806 return (0);
3810 * Retrieve the command history of a pool.
3813 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3815 char *buf;
3816 int buflen = 128 * 1024;
3817 uint64_t off = 0;
3818 nvlist_t **records = NULL;
3819 uint_t numrecords = 0;
3820 int err, i;
3822 buf = malloc(buflen);
3823 if (buf == NULL)
3824 return (ENOMEM);
3825 do {
3826 uint64_t bytes_read = buflen;
3827 uint64_t leftover;
3829 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3830 break;
3832 /* if nothing else was read in, we're at EOF, just return */
3833 if (!bytes_read)
3834 break;
3836 if ((err = zpool_history_unpack(buf, bytes_read,
3837 &leftover, &records, &numrecords)) != 0)
3838 break;
3839 off -= leftover;
3840 if (leftover == bytes_read) {
3842 * no progress made, because buffer is not big enough
3843 * to hold this record; resize and retry.
3845 buflen *= 2;
3846 free(buf);
3847 buf = malloc(buflen);
3848 if (buf == NULL)
3849 return (ENOMEM);
3852 /* CONSTCOND */
3853 } while (1);
3855 free(buf);
3857 if (!err) {
3858 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3859 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3860 records, numrecords) == 0);
3862 for (i = 0; i < numrecords; i++)
3863 nvlist_free(records[i]);
3864 free(records);
3866 return (err);
3869 void
3870 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3871 char *pathname, size_t len)
3873 zfs_cmd_t zc = { 0 };
3874 boolean_t mounted = B_FALSE;
3875 char *mntpnt = NULL;
3876 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3878 if (dsobj == 0) {
3879 /* special case for the MOS */
3880 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3881 return;
3884 /* get the dataset's name */
3885 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3886 zc.zc_obj = dsobj;
3887 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3888 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3889 /* just write out a path of two object numbers */
3890 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3891 dsobj, obj);
3892 return;
3894 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3896 /* find out if the dataset is mounted */
3897 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3899 /* get the corrupted object's path */
3900 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3901 zc.zc_obj = obj;
3902 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3903 &zc) == 0) {
3904 if (mounted) {
3905 (void) snprintf(pathname, len, "%s%s", mntpnt,
3906 zc.zc_value);
3907 } else {
3908 (void) snprintf(pathname, len, "%s:%s",
3909 dsname, zc.zc_value);
3911 } else {
3912 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3914 free(mntpnt);
3918 * Read the EFI label from the config, if a label does not exist then
3919 * pass back the error to the caller. If the caller has passed a non-NULL
3920 * diskaddr argument then we set it to the starting address of the EFI
3921 * partition. If the caller has passed a non-NULL boolean argument, then
3922 * we set it to indicate if the disk does have efi system partition.
3924 static int
3925 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
3927 char *path;
3928 int fd;
3929 char diskname[MAXPATHLEN];
3930 boolean_t boot = B_FALSE;
3931 int err = -1;
3932 int slice;
3934 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3935 return (err);
3937 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3938 strrchr(path, '/'));
3939 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3940 struct dk_gpt *vtoc;
3942 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3943 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
3944 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
3945 boot = B_TRUE;
3946 if (vtoc->efi_parts[slice].p_tag == V_USR)
3947 break;
3949 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
3950 *sb = vtoc->efi_parts[slice].p_start;
3951 if (system != NULL)
3952 *system = boot;
3953 efi_free(vtoc);
3955 (void) close(fd);
3957 return (err);
3961 * determine where a partition starts on a disk in the current
3962 * configuration
3964 static diskaddr_t
3965 find_start_block(nvlist_t *config)
3967 nvlist_t **child;
3968 uint_t c, children;
3969 diskaddr_t sb = MAXOFFSET_T;
3970 uint64_t wholedisk;
3972 if (nvlist_lookup_nvlist_array(config,
3973 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3974 if (nvlist_lookup_uint64(config,
3975 ZPOOL_CONFIG_WHOLE_DISK,
3976 &wholedisk) != 0 || !wholedisk) {
3977 return (MAXOFFSET_T);
3979 if (read_efi_label(config, &sb, NULL) < 0)
3980 sb = MAXOFFSET_T;
3981 return (sb);
3984 for (c = 0; c < children; c++) {
3985 sb = find_start_block(child[c]);
3986 if (sb != MAXOFFSET_T) {
3987 return (sb);
3990 return (MAXOFFSET_T);
3994 * Label an individual disk. The name provided is the short name,
3995 * stripped of any leading /dev path.
3998 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
3999 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4001 char path[MAXPATHLEN];
4002 struct dk_gpt *vtoc;
4003 int fd;
4004 size_t resv = EFI_MIN_RESV_SIZE;
4005 uint64_t slice_size;
4006 diskaddr_t start_block;
4007 char errbuf[1024];
4009 /* prepare an error message just in case */
4010 (void) snprintf(errbuf, sizeof (errbuf),
4011 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4013 if (zhp) {
4014 nvlist_t *nvroot;
4016 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4017 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4019 if (zhp->zpool_start_block == 0)
4020 start_block = find_start_block(nvroot);
4021 else
4022 start_block = zhp->zpool_start_block;
4023 zhp->zpool_start_block = start_block;
4024 } else {
4025 /* new pool */
4026 start_block = NEW_START_BLOCK;
4029 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4030 BACKUP_SLICE);
4032 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4034 * This shouldn't happen. We've long since verified that this
4035 * is a valid device.
4037 zfs_error_aux(hdl,
4038 dgettext(TEXT_DOMAIN, "unable to open device"));
4039 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4042 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4044 * The only way this can fail is if we run out of memory, or we
4045 * were unable to read the disk's capacity
4047 if (errno == ENOMEM)
4048 (void) no_memory(hdl);
4050 (void) close(fd);
4051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4052 "unable to read disk capacity"), name);
4054 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4058 * Why we use V_USR: V_BACKUP confuses users, and is considered
4059 * disposable by some EFI utilities (since EFI doesn't have a backup
4060 * slice). V_UNASSIGNED is supposed to be used only for zero size
4061 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4062 * etc. were all pretty specific. V_USR is as close to reality as we
4063 * can get, in the absence of V_OTHER.
4065 /* first fix the partition start block */
4066 if (start_block == MAXOFFSET_T)
4067 start_block = NEW_START_BLOCK;
4070 * EFI System partition is using slice 0.
4071 * ZFS is on slice 1 and slice 8 is reserved.
4072 * We assume the GPT partition table without system
4073 * partition has zfs p_start == NEW_START_BLOCK.
4074 * If start_block != NEW_START_BLOCK, it means we have
4075 * system partition. Correct solution would be to query/cache vtoc
4076 * from existing vdev member.
4078 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4079 if (boot_size % vtoc->efi_lbasize != 0) {
4080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4081 "boot partition size must be a multiple of %d"),
4082 vtoc->efi_lbasize);
4083 (void) close(fd);
4084 efi_free(vtoc);
4085 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4088 * System partition size checks.
4089 * Note the 1MB is quite arbitrary value, since we
4090 * are creating dedicated pool, it should be enough
4091 * to hold fat + efi bootloader. May need to be
4092 * adjusted if the bootloader size will grow.
4094 if (boot_size < 1024 * 1024) {
4095 char buf[64];
4096 zfs_nicenum(boot_size, buf, sizeof (buf));
4097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4098 "Specified size %s for EFI System partition is too "
4099 "small, the minimum size is 1MB."), buf);
4100 (void) close(fd);
4101 efi_free(vtoc);
4102 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4104 /* 33MB is tested with mkfs -F pcfs */
4105 if (hdl->libzfs_printerr &&
4106 ((vtoc->efi_lbasize == 512 &&
4107 boot_size < 33 * 1024 * 1024) ||
4108 (vtoc->efi_lbasize == 4096 &&
4109 boot_size < 256 * 1024 * 1024))) {
4110 char buf[64];
4111 zfs_nicenum(boot_size, buf, sizeof (buf));
4112 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4113 "Warning: EFI System partition size %s is "
4114 "not allowing to create FAT32 file\nsystem, which "
4115 "may result in unbootable system.\n"), buf);
4117 /* Adjust zfs partition start by size of system partition. */
4118 start_block += boot_size / vtoc->efi_lbasize;
4121 if (start_block == NEW_START_BLOCK) {
4123 * Use default layout.
4124 * ZFS is on slice 0 and slice 8 is reserved.
4126 slice_size = vtoc->efi_last_u_lba + 1;
4127 slice_size -= EFI_MIN_RESV_SIZE;
4128 slice_size -= start_block;
4129 if (slice != NULL)
4130 *slice = 0;
4132 vtoc->efi_parts[0].p_start = start_block;
4133 vtoc->efi_parts[0].p_size = slice_size;
4135 vtoc->efi_parts[0].p_tag = V_USR;
4136 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4138 vtoc->efi_parts[8].p_start = slice_size + start_block;
4139 vtoc->efi_parts[8].p_size = resv;
4140 vtoc->efi_parts[8].p_tag = V_RESERVED;
4141 } else {
4142 slice_size = start_block - NEW_START_BLOCK;
4143 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4144 vtoc->efi_parts[0].p_size = slice_size;
4145 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4146 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4147 if (slice != NULL)
4148 *slice = 1;
4149 /* prepare slice 1 */
4150 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4151 slice_size -= resv;
4152 slice_size -= NEW_START_BLOCK;
4153 vtoc->efi_parts[1].p_start = start_block;
4154 vtoc->efi_parts[1].p_size = slice_size;
4155 vtoc->efi_parts[1].p_tag = V_USR;
4156 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4158 vtoc->efi_parts[8].p_start = slice_size + start_block;
4159 vtoc->efi_parts[8].p_size = resv;
4160 vtoc->efi_parts[8].p_tag = V_RESERVED;
4163 if (efi_write(fd, vtoc) != 0) {
4165 * Some block drivers (like pcata) may not support EFI
4166 * GPT labels. Print out a helpful error message dir-
4167 * ecting the user to manually label the disk and give
4168 * a specific slice.
4170 (void) close(fd);
4171 efi_free(vtoc);
4173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4174 "try using fdisk(1M) and then provide a specific slice"));
4175 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4178 (void) close(fd);
4179 efi_free(vtoc);
4180 return (0);
4183 static boolean_t
4184 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4186 char *type;
4187 nvlist_t **child;
4188 uint_t children, c;
4190 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4191 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4192 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4193 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4194 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4195 "vdev type '%s' is not supported"), type);
4196 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4197 return (B_FALSE);
4199 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4200 &child, &children) == 0) {
4201 for (c = 0; c < children; c++) {
4202 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4203 return (B_FALSE);
4206 return (B_TRUE);
4210 * Check if this zvol is allowable for use as a dump device; zero if
4211 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4213 * Allowable storage configurations include mirrors, all raidz variants, and
4214 * pools with log, cache, and spare devices. Pools which are backed by files or
4215 * have missing/hole vdevs are not suitable.
4218 zvol_check_dump_config(char *arg)
4220 zpool_handle_t *zhp = NULL;
4221 nvlist_t *config, *nvroot;
4222 char *p, *volname;
4223 nvlist_t **top;
4224 uint_t toplevels;
4225 libzfs_handle_t *hdl;
4226 char errbuf[1024];
4227 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4228 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4229 int ret = 1;
4231 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4232 return (-1);
4235 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4236 "dump is not supported on device '%s'"), arg);
4238 if ((hdl = libzfs_init()) == NULL)
4239 return (1);
4240 libzfs_print_on_error(hdl, B_TRUE);
4242 volname = arg + pathlen;
4244 /* check the configuration of the pool */
4245 if ((p = strchr(volname, '/')) == NULL) {
4246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4247 "malformed dataset name"));
4248 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4249 return (1);
4250 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4252 "dataset name is too long"));
4253 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4254 return (1);
4255 } else {
4256 (void) strncpy(poolname, volname, p - volname);
4257 poolname[p - volname] = '\0';
4260 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4262 "could not open pool '%s'"), poolname);
4263 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4264 goto out;
4266 config = zpool_get_config(zhp, NULL);
4267 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4268 &nvroot) != 0) {
4269 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4270 "could not obtain vdev configuration for '%s'"), poolname);
4271 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4272 goto out;
4275 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4276 &top, &toplevels) == 0);
4278 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4279 goto out;
4281 ret = 0;
4283 out:
4284 if (zhp)
4285 zpool_close(zhp);
4286 libzfs_fini(hdl);
4287 return (ret);