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