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