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