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