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