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