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