1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2011, 2024 by Delphix. All rights reserved.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2018 Datto Inc.
29 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
30 * Copyright (c) 2017, Intel Corporation.
31 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
32 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
33 * Copyright (c) 2021, 2023, Klara Inc.
34 * Copyright (c) 2025 Hewlett Packard Enterprise Development LP.
35 */
36
37 #include <errno.h>
38 #include <libintl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <libgen.h>
44 #include <zone.h>
45 #include <sys/stat.h>
46 #include <sys/efi_partition.h>
47 #include <sys/systeminfo.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_sysfs.h>
50 #include <sys/vdev_disk.h>
51 #include <sys/types.h>
52 #include <dlfcn.h>
53 #include <libzutil.h>
54 #include <fcntl.h>
55
56 #include "zfs_namecheck.h"
57 #include "zfs_prop.h"
58 #include "libzfs_impl.h"
59 #include "zfs_comutil.h"
60 #include "zfeature_common.h"
61
62 static boolean_t zpool_vdev_is_interior(const char *name);
63
64 typedef struct prop_flags {
65 unsigned int create:1; /* Validate property on creation */
66 unsigned int import:1; /* Validate property on import */
67 unsigned int vdevprop:1; /* Validate property as a VDEV property */
68 } prop_flags_t;
69
70 /*
71 * ====================================================================
72 * zpool property functions
73 * ====================================================================
74 */
75
76 static int
zpool_get_all_props(zpool_handle_t * zhp)77 zpool_get_all_props(zpool_handle_t *zhp)
78 {
79 zfs_cmd_t zc = {"\0"};
80 libzfs_handle_t *hdl = zhp->zpool_hdl;
81
82 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
83
84 if (zhp->zpool_n_propnames > 0) {
85 nvlist_t *innvl = fnvlist_alloc();
86 fnvlist_add_string_array(innvl, ZPOOL_GET_PROPS_NAMES,
87 zhp->zpool_propnames, zhp->zpool_n_propnames);
88 zcmd_write_src_nvlist(hdl, &zc, innvl);
89 fnvlist_free(innvl);
90 }
91
92 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
93
94 while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
95 if (errno == ENOMEM)
96 zcmd_expand_dst_nvlist(hdl, &zc);
97 else {
98 zcmd_free_nvlists(&zc);
99 return (-1);
100 }
101 }
102
103 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
104 zcmd_free_nvlists(&zc);
105 return (-1);
106 }
107
108 zcmd_free_nvlists(&zc);
109
110 return (0);
111 }
112
113 int
zpool_props_refresh(zpool_handle_t * zhp)114 zpool_props_refresh(zpool_handle_t *zhp)
115 {
116 nvlist_t *old_props;
117
118 old_props = zhp->zpool_props;
119
120 if (zpool_get_all_props(zhp) != 0)
121 return (-1);
122
123 nvlist_free(old_props);
124 return (0);
125 }
126
127 static const char *
zpool_get_prop_string(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)128 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
129 zprop_source_t *src)
130 {
131 nvlist_t *nv, *nvl;
132 const char *value;
133 zprop_source_t source;
134
135 nvl = zhp->zpool_props;
136 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
137 source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
138 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
139 } else {
140 source = ZPROP_SRC_DEFAULT;
141 if ((value = zpool_prop_default_string(prop)) == NULL)
142 value = "-";
143 }
144
145 if (src)
146 *src = source;
147
148 return (value);
149 }
150
151 uint64_t
zpool_get_prop_int(zpool_handle_t * zhp,zpool_prop_t prop,zprop_source_t * src)152 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
153 {
154 nvlist_t *nv, *nvl;
155 uint64_t value;
156 zprop_source_t source;
157
158 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
159 /*
160 * zpool_get_all_props() has most likely failed because
161 * the pool is faulted, but if all we need is the top level
162 * vdev's guid then get it from the zhp config nvlist.
163 */
164 if ((prop == ZPOOL_PROP_GUID) &&
165 (nvlist_lookup_nvlist(zhp->zpool_config,
166 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
167 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
168 == 0)) {
169 return (value);
170 }
171 return (zpool_prop_default_numeric(prop));
172 }
173
174 nvl = zhp->zpool_props;
175 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
176 source = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
177 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
178 } else {
179 source = ZPROP_SRC_DEFAULT;
180 value = zpool_prop_default_numeric(prop);
181 }
182
183 if (src)
184 *src = source;
185
186 return (value);
187 }
188
189 /*
190 * Map VDEV STATE to printed strings.
191 */
192 const char *
zpool_state_to_name(vdev_state_t state,vdev_aux_t aux)193 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
194 {
195 switch (state) {
196 case VDEV_STATE_CLOSED:
197 case VDEV_STATE_OFFLINE:
198 return (gettext("OFFLINE"));
199 case VDEV_STATE_REMOVED:
200 return (gettext("REMOVED"));
201 case VDEV_STATE_CANT_OPEN:
202 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
203 return (gettext("FAULTED"));
204 else if (aux == VDEV_AUX_SPLIT_POOL)
205 return (gettext("SPLIT"));
206 else
207 return (gettext("UNAVAIL"));
208 case VDEV_STATE_FAULTED:
209 return (gettext("FAULTED"));
210 case VDEV_STATE_DEGRADED:
211 return (gettext("DEGRADED"));
212 case VDEV_STATE_HEALTHY:
213 return (gettext("ONLINE"));
214
215 default:
216 break;
217 }
218
219 return (gettext("UNKNOWN"));
220 }
221
222 /*
223 * Map POOL STATE to printed strings.
224 */
225 const char *
zpool_pool_state_to_name(pool_state_t state)226 zpool_pool_state_to_name(pool_state_t state)
227 {
228 switch (state) {
229 default:
230 break;
231 case POOL_STATE_ACTIVE:
232 return (gettext("ACTIVE"));
233 case POOL_STATE_EXPORTED:
234 return (gettext("EXPORTED"));
235 case POOL_STATE_DESTROYED:
236 return (gettext("DESTROYED"));
237 case POOL_STATE_SPARE:
238 return (gettext("SPARE"));
239 case POOL_STATE_L2CACHE:
240 return (gettext("L2CACHE"));
241 case POOL_STATE_UNINITIALIZED:
242 return (gettext("UNINITIALIZED"));
243 case POOL_STATE_UNAVAIL:
244 return (gettext("UNAVAIL"));
245 case POOL_STATE_POTENTIALLY_ACTIVE:
246 return (gettext("POTENTIALLY_ACTIVE"));
247 }
248
249 return (gettext("UNKNOWN"));
250 }
251
252 /*
253 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
254 * "SUSPENDED", etc).
255 */
256 const char *
zpool_get_state_str(zpool_handle_t * zhp)257 zpool_get_state_str(zpool_handle_t *zhp)
258 {
259 zpool_errata_t errata;
260 zpool_status_t status;
261 const char *str;
262
263 status = zpool_get_status(zhp, NULL, &errata);
264
265 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
266 str = gettext("FAULTED");
267 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
268 status == ZPOOL_STATUS_IO_FAILURE_CONTINUE ||
269 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
270 str = gettext("SUSPENDED");
271 } else {
272 nvlist_t *nvroot = fnvlist_lookup_nvlist(
273 zpool_get_config(zhp, NULL), ZPOOL_CONFIG_VDEV_TREE);
274 uint_t vsc;
275 vdev_stat_t *vs = (vdev_stat_t *)fnvlist_lookup_uint64_array(
276 nvroot, ZPOOL_CONFIG_VDEV_STATS, &vsc);
277 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
278 }
279 return (str);
280 }
281
282 /*
283 * Get a zpool property value for 'prop' and return the value in
284 * a pre-allocated buffer.
285 */
286 int
zpool_get_prop(zpool_handle_t * zhp,zpool_prop_t prop,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)287 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
288 size_t len, zprop_source_t *srctype, boolean_t literal)
289 {
290 uint64_t intval;
291 const char *strval;
292 zprop_source_t src = ZPROP_SRC_NONE;
293
294 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
295 switch (prop) {
296 case ZPOOL_PROP_NAME:
297 (void) strlcpy(buf, zpool_get_name(zhp), len);
298 break;
299
300 case ZPOOL_PROP_HEALTH:
301 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
302 break;
303
304 case ZPOOL_PROP_GUID:
305 intval = zpool_get_prop_int(zhp, prop, &src);
306 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
307 break;
308
309 case ZPOOL_PROP_ALTROOT:
310 case ZPOOL_PROP_CACHEFILE:
311 case ZPOOL_PROP_COMMENT:
312 case ZPOOL_PROP_COMPATIBILITY:
313 if (zhp->zpool_props != NULL ||
314 zpool_get_all_props(zhp) == 0) {
315 (void) strlcpy(buf,
316 zpool_get_prop_string(zhp, prop, &src),
317 len);
318 break;
319 }
320 zfs_fallthrough;
321 default:
322 (void) strlcpy(buf, "-", len);
323 break;
324 }
325
326 if (srctype != NULL)
327 *srctype = src;
328 return (0);
329 }
330
331 /*
332 * ZPOOL_PROP_DEDUPCACHED can be fetched by name only using
333 * the ZPOOL_GET_PROPS_NAMES mechanism
334 */
335 if (prop == ZPOOL_PROP_DEDUPCACHED) {
336 zpool_add_propname(zhp, ZPOOL_DEDUPCACHED_PROP_NAME);
337 (void) zpool_props_refresh(zhp);
338 }
339
340 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
341 prop != ZPOOL_PROP_NAME)
342 return (-1);
343
344 switch (zpool_prop_get_type(prop)) {
345 case PROP_TYPE_STRING:
346 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
347 len);
348 break;
349
350 case PROP_TYPE_NUMBER:
351 intval = zpool_get_prop_int(zhp, prop, &src);
352
353 switch (prop) {
354 case ZPOOL_PROP_DEDUP_TABLE_QUOTA:
355 /*
356 * If dedup quota is 0, we translate this into 'none'
357 * (unless literal is set). And if it is UINT64_MAX
358 * we translate that as 'automatic' (limit to size of
359 * the dedicated dedup VDEV. Otherwise, fall throught
360 * into the regular number formating.
361 */
362 if (intval == 0) {
363 (void) strlcpy(buf, literal ? "0" : "none",
364 len);
365 break;
366 } else if (intval == UINT64_MAX) {
367 (void) strlcpy(buf, "auto", len);
368 break;
369 }
370 zfs_fallthrough;
371
372 case ZPOOL_PROP_SIZE:
373 case ZPOOL_PROP_ALLOCATED:
374 case ZPOOL_PROP_FREE:
375 case ZPOOL_PROP_FREEING:
376 case ZPOOL_PROP_LEAKED:
377 case ZPOOL_PROP_ASHIFT:
378 case ZPOOL_PROP_MAXBLOCKSIZE:
379 case ZPOOL_PROP_MAXDNODESIZE:
380 case ZPOOL_PROP_BCLONESAVED:
381 case ZPOOL_PROP_BCLONEUSED:
382 case ZPOOL_PROP_DEDUP_TABLE_SIZE:
383 case ZPOOL_PROP_DEDUPUSED:
384 case ZPOOL_PROP_DEDUPSAVED:
385 case ZPOOL_PROP_DEDUPCACHED:
386 if (literal)
387 (void) snprintf(buf, len, "%llu",
388 (u_longlong_t)intval);
389 else
390 (void) zfs_nicenum(intval, buf, len);
391 break;
392
393 case ZPOOL_PROP_EXPANDSZ:
394 case ZPOOL_PROP_CHECKPOINT:
395 if (intval == 0) {
396 (void) strlcpy(buf, "-", len);
397 } else if (literal) {
398 (void) snprintf(buf, len, "%llu",
399 (u_longlong_t)intval);
400 } else {
401 (void) zfs_nicebytes(intval, buf, len);
402 }
403 break;
404
405 case ZPOOL_PROP_CAPACITY:
406 if (literal) {
407 (void) snprintf(buf, len, "%llu",
408 (u_longlong_t)intval);
409 } else {
410 (void) snprintf(buf, len, "%llu%%",
411 (u_longlong_t)intval);
412 }
413 break;
414
415 case ZPOOL_PROP_FRAGMENTATION:
416 if (intval == UINT64_MAX) {
417 (void) strlcpy(buf, "-", len);
418 } else if (literal) {
419 (void) snprintf(buf, len, "%llu",
420 (u_longlong_t)intval);
421 } else {
422 (void) snprintf(buf, len, "%llu%%",
423 (u_longlong_t)intval);
424 }
425 break;
426
427 case ZPOOL_PROP_BCLONERATIO:
428 case ZPOOL_PROP_DEDUPRATIO:
429 if (literal)
430 (void) snprintf(buf, len, "%llu.%02llu",
431 (u_longlong_t)(intval / 100),
432 (u_longlong_t)(intval % 100));
433 else
434 (void) snprintf(buf, len, "%llu.%02llux",
435 (u_longlong_t)(intval / 100),
436 (u_longlong_t)(intval % 100));
437 break;
438
439 case ZPOOL_PROP_HEALTH:
440 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
441 break;
442 case ZPOOL_PROP_VERSION:
443 if (intval >= SPA_VERSION_FEATURES) {
444 (void) snprintf(buf, len, "-");
445 break;
446 }
447 zfs_fallthrough;
448 default:
449 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
450 }
451 break;
452
453 case PROP_TYPE_INDEX:
454 intval = zpool_get_prop_int(zhp, prop, &src);
455 if (zpool_prop_index_to_string(prop, intval, &strval)
456 != 0)
457 return (-1);
458 (void) strlcpy(buf, strval, len);
459 break;
460
461 default:
462 abort();
463 }
464
465 if (srctype)
466 *srctype = src;
467
468 return (0);
469 }
470
471 /*
472 * Get a zpool property value for 'propname' and return the value in
473 * a pre-allocated buffer.
474 */
475 int
zpool_get_userprop(zpool_handle_t * zhp,const char * propname,char * buf,size_t len,zprop_source_t * srctype)476 zpool_get_userprop(zpool_handle_t *zhp, const char *propname, char *buf,
477 size_t len, zprop_source_t *srctype)
478 {
479 nvlist_t *nv;
480 uint64_t ival;
481 const char *value;
482 zprop_source_t source = ZPROP_SRC_LOCAL;
483
484 if (zhp->zpool_props == NULL)
485 zpool_get_all_props(zhp);
486
487 if (nvlist_lookup_nvlist(zhp->zpool_props, propname, &nv) == 0) {
488 if (nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0)
489 source = ival;
490 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
491 } else {
492 source = ZPROP_SRC_DEFAULT;
493 value = "-";
494 }
495
496 if (srctype)
497 *srctype = source;
498
499 (void) strlcpy(buf, value, len);
500
501 return (0);
502 }
503
504 /*
505 * Check if the bootfs name has the same pool name as it is set to.
506 * Assuming bootfs is a valid dataset name.
507 */
508 static boolean_t
bootfs_name_valid(const char * pool,const char * bootfs)509 bootfs_name_valid(const char *pool, const char *bootfs)
510 {
511 int len = strlen(pool);
512 if (bootfs[0] == '\0')
513 return (B_TRUE);
514
515 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
516 return (B_FALSE);
517
518 if (strncmp(pool, bootfs, len) == 0 &&
519 (bootfs[len] == '/' || bootfs[len] == '\0'))
520 return (B_TRUE);
521
522 return (B_FALSE);
523 }
524
525 /*
526 * Given an nvlist of zpool properties to be set, validate that they are
527 * correct, and parse any numeric properties (index, boolean, etc) if they are
528 * specified as strings.
529 */
530 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)531 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
532 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
533 {
534 nvpair_t *elem;
535 nvlist_t *retprops;
536 zpool_prop_t prop;
537 const char *strval;
538 uint64_t intval;
539 const char *check;
540 struct stat64 statbuf;
541 zpool_handle_t *zhp;
542 char *parent, *slash;
543 char report[1024];
544
545 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
546 (void) no_memory(hdl);
547 return (NULL);
548 }
549
550 elem = NULL;
551 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
552 const char *propname = nvpair_name(elem);
553
554 if (flags.vdevprop && zpool_prop_vdev(propname)) {
555 vdev_prop_t vprop = vdev_name_to_prop(propname);
556
557 if (vdev_prop_readonly(vprop)) {
558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
559 "is readonly"), propname);
560 (void) zfs_error(hdl, EZFS_PROPREADONLY,
561 errbuf);
562 goto error;
563 }
564
565 if (zprop_parse_value(hdl, elem, vprop, ZFS_TYPE_VDEV,
566 retprops, &strval, &intval, errbuf) != 0)
567 goto error;
568
569 continue;
570 } else if (flags.vdevprop && vdev_prop_user(propname)) {
571 if (nvlist_add_nvpair(retprops, elem) != 0) {
572 (void) no_memory(hdl);
573 goto error;
574 }
575 continue;
576 } else if (flags.vdevprop) {
577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
578 "invalid property: '%s'"), propname);
579 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
580 goto error;
581 }
582
583 prop = zpool_name_to_prop(propname);
584 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
585 int err;
586 char *fname = strchr(propname, '@') + 1;
587
588 err = zfeature_lookup_name(fname, NULL);
589 if (err != 0) {
590 ASSERT3U(err, ==, ENOENT);
591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
592 "feature '%s' unsupported by kernel"),
593 fname);
594 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
595 goto error;
596 }
597
598 if (nvpair_type(elem) != DATA_TYPE_STRING) {
599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
600 "'%s' must be a string"), propname);
601 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
602 goto error;
603 }
604
605 (void) nvpair_value_string(elem, &strval);
606 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
607 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
609 "property '%s' can only be set to "
610 "'enabled' or 'disabled'"), propname);
611 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
612 goto error;
613 }
614
615 if (!flags.create &&
616 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
618 "property '%s' can only be set to "
619 "'disabled' at creation time"), propname);
620 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
621 goto error;
622 }
623
624 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
625 (void) no_memory(hdl);
626 goto error;
627 }
628 continue;
629 } else if (prop == ZPOOL_PROP_INVAL &&
630 zfs_prop_user(propname)) {
631 /*
632 * This is a user property: make sure it's a
633 * string, and that it's less than ZAP_MAXNAMELEN.
634 */
635 if (nvpair_type(elem) != DATA_TYPE_STRING) {
636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
637 "'%s' must be a string"), propname);
638 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
639 goto error;
640 }
641
642 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
644 "property name '%s' is too long"),
645 propname);
646 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
647 goto error;
648 }
649
650 (void) nvpair_value_string(elem, &strval);
651
652 if (strlen(strval) >= ZFS_MAXPROPLEN) {
653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
654 "property value '%s' is too long"),
655 strval);
656 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
657 goto error;
658 }
659
660 if (nvlist_add_string(retprops, propname,
661 strval) != 0) {
662 (void) no_memory(hdl);
663 goto error;
664 }
665
666 continue;
667 }
668
669 /*
670 * Make sure this property is valid and applies to this type.
671 */
672 if (prop == ZPOOL_PROP_INVAL) {
673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
674 "invalid property '%s'"), propname);
675 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
676 goto error;
677 }
678
679 if (zpool_prop_readonly(prop)) {
680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
681 "is readonly"), propname);
682 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
683 goto error;
684 }
685
686 if (!flags.create && zpool_prop_setonce(prop)) {
687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
688 "property '%s' can only be set at "
689 "creation time"), propname);
690 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
691 goto error;
692 }
693
694 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
695 &strval, &intval, errbuf) != 0)
696 goto error;
697
698 /*
699 * Perform additional checking for specific properties.
700 */
701 switch (prop) {
702 case ZPOOL_PROP_VERSION:
703 if (intval < version ||
704 !SPA_VERSION_IS_SUPPORTED(intval)) {
705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
706 "property '%s' number %llu is invalid."),
707 propname, (unsigned long long)intval);
708 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
709 goto error;
710 }
711 break;
712
713 case ZPOOL_PROP_ASHIFT:
714 if (intval != 0 &&
715 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
717 "property '%s' number %llu is invalid, "
718 "only values between %" PRId32 " and %"
719 PRId32 " are allowed."),
720 propname, (unsigned long long)intval,
721 ASHIFT_MIN, ASHIFT_MAX);
722 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
723 goto error;
724 }
725 break;
726
727 case ZPOOL_PROP_BOOTFS:
728 if (flags.create || flags.import) {
729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
730 "property '%s' cannot be set at creation "
731 "or import time"), propname);
732 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
733 goto error;
734 }
735
736 if (version < SPA_VERSION_BOOTFS) {
737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
738 "pool must be upgraded to support "
739 "'%s' property"), propname);
740 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
741 goto error;
742 }
743
744 /*
745 * bootfs property value has to be a dataset name and
746 * the dataset has to be in the same pool as it sets to.
747 */
748 if (!bootfs_name_valid(poolname, strval)) {
749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
750 "is an invalid name"), strval);
751 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
752 goto error;
753 }
754
755 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
757 "could not open pool '%s'"), poolname);
758 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
759 goto error;
760 }
761 zpool_close(zhp);
762 break;
763
764 case ZPOOL_PROP_ALTROOT:
765 if (!flags.create && !flags.import) {
766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
767 "property '%s' can only be set during pool "
768 "creation or import"), propname);
769 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
770 goto error;
771 }
772
773 if (strval[0] != '/') {
774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
775 "bad alternate root '%s'"), strval);
776 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
777 goto error;
778 }
779 break;
780
781 case ZPOOL_PROP_CACHEFILE:
782 if (strval[0] == '\0')
783 break;
784
785 if (strcmp(strval, "none") == 0)
786 break;
787
788 if (strval[0] != '/') {
789 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
790 "property '%s' must be empty, an "
791 "absolute path, or 'none'"), propname);
792 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
793 goto error;
794 }
795
796 parent = strdup(strval);
797 if (parent == NULL) {
798 (void) zfs_error(hdl, EZFS_NOMEM, errbuf);
799 goto error;
800 }
801 slash = strrchr(parent, '/');
802
803 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
804 strcmp(slash, "/..") == 0) {
805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
806 "'%s' is not a valid file"), parent);
807 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
808 free(parent);
809 goto error;
810 }
811
812 *slash = '\0';
813
814 if (parent[0] != '\0' &&
815 (stat64(parent, &statbuf) != 0 ||
816 !S_ISDIR(statbuf.st_mode))) {
817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
818 "'%s' is not a valid directory"),
819 parent);
820 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
821 free(parent);
822 goto error;
823 }
824 free(parent);
825
826 break;
827
828 case ZPOOL_PROP_COMPATIBILITY:
829 switch (zpool_load_compat(strval, NULL, report, 1024)) {
830 case ZPOOL_COMPATIBILITY_OK:
831 case ZPOOL_COMPATIBILITY_WARNTOKEN:
832 break;
833 case ZPOOL_COMPATIBILITY_BADFILE:
834 case ZPOOL_COMPATIBILITY_BADTOKEN:
835 case ZPOOL_COMPATIBILITY_NOFILES:
836 zfs_error_aux(hdl, "%s", report);
837 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
838 goto error;
839 }
840 break;
841
842 case ZPOOL_PROP_COMMENT:
843 for (check = strval; *check != '\0'; check++) {
844 if (!isprint(*check)) {
845 zfs_error_aux(hdl,
846 dgettext(TEXT_DOMAIN,
847 "comment may only have printable "
848 "characters"));
849 (void) zfs_error(hdl, EZFS_BADPROP,
850 errbuf);
851 goto error;
852 }
853 }
854 if (strlen(strval) > ZPROP_MAX_COMMENT) {
855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
856 "comment must not exceed %d characters"),
857 ZPROP_MAX_COMMENT);
858 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
859 goto error;
860 }
861 break;
862 case ZPOOL_PROP_READONLY:
863 if (!flags.import) {
864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
865 "property '%s' can only be set at "
866 "import time"), propname);
867 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
868 goto error;
869 }
870 break;
871 case ZPOOL_PROP_MULTIHOST:
872 if (get_system_hostid() == 0) {
873 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
874 "requires a non-zero system hostid"));
875 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
876 goto error;
877 }
878 break;
879 case ZPOOL_PROP_DEDUPDITTO:
880 printf("Note: property '%s' no longer has "
881 "any effect\n", propname);
882 break;
883
884 default:
885 break;
886 }
887 }
888
889 return (retprops);
890 error:
891 nvlist_free(retprops);
892 return (NULL);
893 }
894
895 /*
896 * Set zpool property : propname=propval.
897 */
898 int
zpool_set_prop(zpool_handle_t * zhp,const char * propname,const char * propval)899 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
900 {
901 zfs_cmd_t zc = {"\0"};
902 int ret;
903 char errbuf[ERRBUFLEN];
904 nvlist_t *nvl = NULL;
905 nvlist_t *realprops;
906 uint64_t version;
907 prop_flags_t flags = { 0 };
908
909 (void) snprintf(errbuf, sizeof (errbuf),
910 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
911 zhp->zpool_name);
912
913 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
914 return (no_memory(zhp->zpool_hdl));
915
916 if (nvlist_add_string(nvl, propname, propval) != 0) {
917 nvlist_free(nvl);
918 return (no_memory(zhp->zpool_hdl));
919 }
920
921 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
922 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
923 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
924 nvlist_free(nvl);
925 return (-1);
926 }
927
928 nvlist_free(nvl);
929 nvl = realprops;
930
931 /*
932 * Execute the corresponding ioctl() to set this property.
933 */
934 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
935
936 zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl);
937
938 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
939
940 zcmd_free_nvlists(&zc);
941 nvlist_free(nvl);
942
943 if (ret)
944 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
945 else
946 (void) zpool_props_refresh(zhp);
947
948 return (ret);
949 }
950
951 int
zpool_expand_proplist(zpool_handle_t * zhp,zprop_list_t ** plp,zfs_type_t type,boolean_t literal)952 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp,
953 zfs_type_t type, boolean_t literal)
954 {
955 libzfs_handle_t *hdl = zhp->zpool_hdl;
956 zprop_list_t *entry;
957 char buf[ZFS_MAXPROPLEN];
958 nvlist_t *features = NULL;
959 nvpair_t *nvp;
960 zprop_list_t **last;
961 boolean_t firstexpand = (NULL == *plp);
962 int i;
963
964 if (zprop_expand_list(hdl, plp, type) != 0)
965 return (-1);
966
967 if (type == ZFS_TYPE_VDEV)
968 return (0);
969
970 last = plp;
971 while (*last != NULL)
972 last = &(*last)->pl_next;
973
974 if ((*plp)->pl_all)
975 features = zpool_get_features(zhp);
976
977 if ((*plp)->pl_all && firstexpand) {
978 /* Handle userprops in the all properties case */
979 if (zhp->zpool_props == NULL && zpool_props_refresh(zhp))
980 return (-1);
981
982 nvp = NULL;
983 while ((nvp = nvlist_next_nvpair(zhp->zpool_props, nvp)) !=
984 NULL) {
985 const char *propname = nvpair_name(nvp);
986
987 if (!zfs_prop_user(propname))
988 continue;
989
990 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
991 entry->pl_prop = ZPROP_USERPROP;
992 entry->pl_user_prop = zfs_strdup(hdl, propname);
993 entry->pl_width = strlen(entry->pl_user_prop);
994 entry->pl_all = B_TRUE;
995
996 *last = entry;
997 last = &entry->pl_next;
998 }
999
1000 for (i = 0; i < SPA_FEATURES; i++) {
1001 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
1002 entry->pl_prop = ZPROP_USERPROP;
1003 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
1004 spa_feature_table[i].fi_uname);
1005 entry->pl_width = strlen(entry->pl_user_prop);
1006 entry->pl_all = B_TRUE;
1007
1008 *last = entry;
1009 last = &entry->pl_next;
1010 }
1011 }
1012
1013 /* add any unsupported features */
1014 for (nvp = nvlist_next_nvpair(features, NULL);
1015 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
1016 char *propname;
1017 boolean_t found;
1018
1019 if (zfeature_is_supported(nvpair_name(nvp)))
1020 continue;
1021
1022 propname = zfs_asprintf(hdl, "unsupported@%s",
1023 nvpair_name(nvp));
1024
1025 /*
1026 * Before adding the property to the list make sure that no
1027 * other pool already added the same property.
1028 */
1029 found = B_FALSE;
1030 entry = *plp;
1031 while (entry != NULL) {
1032 if (entry->pl_user_prop != NULL &&
1033 strcmp(propname, entry->pl_user_prop) == 0) {
1034 found = B_TRUE;
1035 break;
1036 }
1037 entry = entry->pl_next;
1038 }
1039 if (found) {
1040 free(propname);
1041 continue;
1042 }
1043
1044 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
1045 entry->pl_prop = ZPROP_USERPROP;
1046 entry->pl_user_prop = propname;
1047 entry->pl_width = strlen(entry->pl_user_prop);
1048 entry->pl_all = B_TRUE;
1049
1050 *last = entry;
1051 last = &entry->pl_next;
1052 }
1053
1054 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
1055 if (entry->pl_fixed && !literal)
1056 continue;
1057
1058 if (entry->pl_prop != ZPROP_USERPROP &&
1059 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
1060 NULL, literal) == 0) {
1061 if (strlen(buf) > entry->pl_width)
1062 entry->pl_width = strlen(buf);
1063 } else if (entry->pl_prop == ZPROP_INVAL &&
1064 zfs_prop_user(entry->pl_user_prop) &&
1065 zpool_get_userprop(zhp, entry->pl_user_prop, buf,
1066 sizeof (buf), NULL) == 0) {
1067 if (strlen(buf) > entry->pl_width)
1068 entry->pl_width = strlen(buf);
1069 }
1070 }
1071
1072 return (0);
1073 }
1074
1075 int
vdev_expand_proplist(zpool_handle_t * zhp,const char * vdevname,zprop_list_t ** plp)1076 vdev_expand_proplist(zpool_handle_t *zhp, const char *vdevname,
1077 zprop_list_t **plp)
1078 {
1079 zprop_list_t *entry;
1080 char buf[ZFS_MAXPROPLEN];
1081 const char *strval = NULL;
1082 int err = 0;
1083 nvpair_t *elem = NULL;
1084 nvlist_t *vprops = NULL;
1085 nvlist_t *propval = NULL;
1086 const char *propname;
1087 vdev_prop_t prop;
1088 zprop_list_t **last;
1089
1090 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
1091 if (entry->pl_fixed)
1092 continue;
1093
1094 if (zpool_get_vdev_prop(zhp, vdevname, entry->pl_prop,
1095 entry->pl_user_prop, buf, sizeof (buf), NULL,
1096 B_FALSE) == 0) {
1097 if (strlen(buf) > entry->pl_width)
1098 entry->pl_width = strlen(buf);
1099 }
1100 if (entry->pl_prop == VDEV_PROP_NAME &&
1101 strlen(vdevname) > entry->pl_width)
1102 entry->pl_width = strlen(vdevname);
1103 }
1104
1105 /* Handle the all properties case */
1106 last = plp;
1107 if (*last != NULL && (*last)->pl_all == B_TRUE) {
1108 while (*last != NULL)
1109 last = &(*last)->pl_next;
1110
1111 err = zpool_get_all_vdev_props(zhp, vdevname, &vprops);
1112 if (err != 0)
1113 return (err);
1114
1115 while ((elem = nvlist_next_nvpair(vprops, elem)) != NULL) {
1116 propname = nvpair_name(elem);
1117
1118 /* Skip properties that are not user defined */
1119 if ((prop = vdev_name_to_prop(propname)) !=
1120 VDEV_PROP_USERPROP)
1121 continue;
1122
1123 if (nvpair_value_nvlist(elem, &propval) != 0)
1124 continue;
1125
1126 strval = fnvlist_lookup_string(propval, ZPROP_VALUE);
1127
1128 entry = zfs_alloc(zhp->zpool_hdl,
1129 sizeof (zprop_list_t));
1130 entry->pl_prop = prop;
1131 entry->pl_user_prop = zfs_strdup(zhp->zpool_hdl,
1132 propname);
1133 entry->pl_width = strlen(strval);
1134 entry->pl_all = B_TRUE;
1135 *last = entry;
1136 last = &entry->pl_next;
1137 }
1138 }
1139
1140 return (0);
1141 }
1142
1143 /*
1144 * Get the state for the given feature on the given ZFS pool.
1145 */
1146 int
zpool_prop_get_feature(zpool_handle_t * zhp,const char * propname,char * buf,size_t len)1147 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
1148 size_t len)
1149 {
1150 uint64_t refcount;
1151 boolean_t found = B_FALSE;
1152 nvlist_t *features = zpool_get_features(zhp);
1153 boolean_t supported;
1154 const char *feature = strchr(propname, '@') + 1;
1155
1156 supported = zpool_prop_feature(propname);
1157 ASSERT(supported || zpool_prop_unsupported(propname));
1158
1159 /*
1160 * Convert from feature name to feature guid. This conversion is
1161 * unnecessary for unsupported@... properties because they already
1162 * use guids.
1163 */
1164 if (supported) {
1165 int ret;
1166 spa_feature_t fid;
1167
1168 ret = zfeature_lookup_name(feature, &fid);
1169 if (ret != 0) {
1170 (void) strlcpy(buf, "-", len);
1171 return (ENOTSUP);
1172 }
1173 feature = spa_feature_table[fid].fi_guid;
1174 }
1175
1176 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
1177 found = B_TRUE;
1178
1179 if (supported) {
1180 if (!found) {
1181 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
1182 } else {
1183 if (refcount == 0)
1184 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
1185 else
1186 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
1187 }
1188 } else {
1189 if (found) {
1190 if (refcount == 0) {
1191 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
1192 } else {
1193 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
1194 }
1195 } else {
1196 (void) strlcpy(buf, "-", len);
1197 return (ENOTSUP);
1198 }
1199 }
1200
1201 return (0);
1202 }
1203
1204 /*
1205 * Validate the given pool name, optionally putting an extended error message in
1206 * 'buf'.
1207 */
1208 boolean_t
zpool_name_valid(libzfs_handle_t * hdl,boolean_t isopen,const char * pool)1209 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
1210 {
1211 namecheck_err_t why;
1212 char what;
1213 int ret;
1214
1215 ret = pool_namecheck(pool, &why, &what);
1216
1217 /*
1218 * The rules for reserved pool names were extended at a later point.
1219 * But we need to support users with existing pools that may now be
1220 * invalid. So we only check for this expanded set of names during a
1221 * create (or import), and only in userland.
1222 */
1223 if (ret == 0 && !isopen &&
1224 (strncmp(pool, "mirror", 6) == 0 ||
1225 strncmp(pool, "raidz", 5) == 0 ||
1226 strncmp(pool, "draid", 5) == 0 ||
1227 strncmp(pool, "spare", 5) == 0 ||
1228 strcmp(pool, "log") == 0)) {
1229 if (hdl != NULL)
1230 zfs_error_aux(hdl,
1231 dgettext(TEXT_DOMAIN, "name is reserved"));
1232 return (B_FALSE);
1233 }
1234
1235
1236 if (ret != 0) {
1237 if (hdl != NULL) {
1238 switch (why) {
1239 case NAME_ERR_TOOLONG:
1240 zfs_error_aux(hdl,
1241 dgettext(TEXT_DOMAIN, "name is too long"));
1242 break;
1243
1244 case NAME_ERR_INVALCHAR:
1245 zfs_error_aux(hdl,
1246 dgettext(TEXT_DOMAIN, "invalid character "
1247 "'%c' in pool name"), what);
1248 break;
1249
1250 case NAME_ERR_NOLETTER:
1251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1252 "name must begin with a letter"));
1253 break;
1254
1255 case NAME_ERR_RESERVED:
1256 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1257 "name is reserved"));
1258 break;
1259
1260 case NAME_ERR_DISKLIKE:
1261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262 "pool name is reserved"));
1263 break;
1264
1265 case NAME_ERR_LEADING_SLASH:
1266 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1267 "leading slash in name"));
1268 break;
1269
1270 case NAME_ERR_EMPTY_COMPONENT:
1271 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1272 "empty component in name"));
1273 break;
1274
1275 case NAME_ERR_TRAILING_SLASH:
1276 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277 "trailing slash in name"));
1278 break;
1279
1280 case NAME_ERR_MULTIPLE_DELIMITERS:
1281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1282 "multiple '@' and/or '#' delimiters in "
1283 "name"));
1284 break;
1285
1286 case NAME_ERR_NO_AT:
1287 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1288 "permission set is missing '@'"));
1289 break;
1290
1291 default:
1292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1293 "(%d) not defined"), why);
1294 break;
1295 }
1296 }
1297 return (B_FALSE);
1298 }
1299
1300 return (B_TRUE);
1301 }
1302
1303 /*
1304 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1305 * state.
1306 */
1307 zpool_handle_t *
zpool_open_canfail(libzfs_handle_t * hdl,const char * pool)1308 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1309 {
1310 zpool_handle_t *zhp;
1311 boolean_t missing;
1312
1313 /*
1314 * Make sure the pool name is valid.
1315 */
1316 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1317 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1318 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1319 pool);
1320 return (NULL);
1321 }
1322
1323 zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
1324
1325 zhp->zpool_hdl = hdl;
1326 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1327
1328 if (zpool_refresh_stats(zhp, &missing) != 0) {
1329 zpool_close(zhp);
1330 return (NULL);
1331 }
1332
1333 if (missing) {
1334 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1335 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1336 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1337 zpool_close(zhp);
1338 return (NULL);
1339 }
1340
1341 return (zhp);
1342 }
1343
1344 /*
1345 * Like the above, but silent on error. Used when iterating over pools (because
1346 * the configuration cache may be out of date).
1347 */
1348 int
zpool_open_silent(libzfs_handle_t * hdl,const char * pool,zpool_handle_t ** ret)1349 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1350 {
1351 zpool_handle_t *zhp;
1352 boolean_t missing;
1353
1354 zhp = zfs_alloc(hdl, sizeof (zpool_handle_t));
1355
1356 zhp->zpool_hdl = hdl;
1357 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1358
1359 if (zpool_refresh_stats(zhp, &missing) != 0) {
1360 zpool_close(zhp);
1361 return (-1);
1362 }
1363
1364 if (missing) {
1365 zpool_close(zhp);
1366 *ret = NULL;
1367 return (0);
1368 }
1369
1370 *ret = zhp;
1371 return (0);
1372 }
1373
1374 /*
1375 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1376 * state.
1377 */
1378 zpool_handle_t *
zpool_open(libzfs_handle_t * hdl,const char * pool)1379 zpool_open(libzfs_handle_t *hdl, const char *pool)
1380 {
1381 zpool_handle_t *zhp;
1382
1383 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1384 return (NULL);
1385
1386 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1387 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1388 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1389 zpool_close(zhp);
1390 return (NULL);
1391 }
1392
1393 return (zhp);
1394 }
1395
1396 /*
1397 * Close the handle. Simply frees the memory associated with the handle.
1398 */
1399 void
zpool_close(zpool_handle_t * zhp)1400 zpool_close(zpool_handle_t *zhp)
1401 {
1402 nvlist_free(zhp->zpool_config);
1403 nvlist_free(zhp->zpool_old_config);
1404 nvlist_free(zhp->zpool_props);
1405 free(zhp);
1406 }
1407
1408 /*
1409 * Return the name of the pool.
1410 */
1411 const char *
zpool_get_name(zpool_handle_t * zhp)1412 zpool_get_name(zpool_handle_t *zhp)
1413 {
1414 return (zhp->zpool_name);
1415 }
1416
1417
1418 /*
1419 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1420 */
1421 int
zpool_get_state(zpool_handle_t * zhp)1422 zpool_get_state(zpool_handle_t *zhp)
1423 {
1424 return (zhp->zpool_state);
1425 }
1426
1427 /*
1428 * Check if vdev list contains a dRAID vdev
1429 */
1430 static boolean_t
zpool_has_draid_vdev(nvlist_t * nvroot)1431 zpool_has_draid_vdev(nvlist_t *nvroot)
1432 {
1433 nvlist_t **child;
1434 uint_t children;
1435
1436 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1437 &child, &children) == 0) {
1438 for (uint_t c = 0; c < children; c++) {
1439 const char *type;
1440
1441 if (nvlist_lookup_string(child[c],
1442 ZPOOL_CONFIG_TYPE, &type) == 0 &&
1443 strcmp(type, VDEV_TYPE_DRAID) == 0) {
1444 return (B_TRUE);
1445 }
1446 }
1447 }
1448 return (B_FALSE);
1449 }
1450
1451 /*
1452 * Output a dRAID top-level vdev name in to the provided buffer.
1453 */
1454 static char *
zpool_draid_name(char * name,int len,uint64_t data,uint64_t parity,uint64_t spares,uint64_t children)1455 zpool_draid_name(char *name, int len, uint64_t data, uint64_t parity,
1456 uint64_t spares, uint64_t children)
1457 {
1458 snprintf(name, len, "%s%llu:%llud:%lluc:%llus",
1459 VDEV_TYPE_DRAID, (u_longlong_t)parity, (u_longlong_t)data,
1460 (u_longlong_t)children, (u_longlong_t)spares);
1461
1462 return (name);
1463 }
1464
1465 /*
1466 * Return B_TRUE if the provided name is a dRAID spare name.
1467 */
1468 boolean_t
zpool_is_draid_spare(const char * name)1469 zpool_is_draid_spare(const char *name)
1470 {
1471 uint64_t spare_id, parity, vdev_id;
1472
1473 if (sscanf(name, VDEV_TYPE_DRAID "%llu-%llu-%llu",
1474 (u_longlong_t *)&parity, (u_longlong_t *)&vdev_id,
1475 (u_longlong_t *)&spare_id) == 3) {
1476 return (B_TRUE);
1477 }
1478
1479 return (B_FALSE);
1480 }
1481
1482 /*
1483 * Create the named pool, using the provided vdev list. It is assumed
1484 * that the consumer has already validated the contents of the nvlist, so we
1485 * don't have to worry about error semantics.
1486 */
1487 int
zpool_create(libzfs_handle_t * hdl,const char * pool,nvlist_t * nvroot,nvlist_t * props,nvlist_t * fsprops)1488 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1489 nvlist_t *props, nvlist_t *fsprops)
1490 {
1491 zfs_cmd_t zc = {"\0"};
1492 nvlist_t *zc_fsprops = NULL;
1493 nvlist_t *zc_props = NULL;
1494 nvlist_t *hidden_args = NULL;
1495 uint8_t *wkeydata = NULL;
1496 uint_t wkeylen = 0;
1497 char errbuf[ERRBUFLEN];
1498 int ret = -1;
1499
1500 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1501 "cannot create '%s'"), pool);
1502
1503 if (!zpool_name_valid(hdl, B_FALSE, pool))
1504 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1505
1506 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
1507
1508 if (props) {
1509 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1510
1511 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1512 SPA_VERSION_1, flags, errbuf)) == NULL) {
1513 goto create_failed;
1514 }
1515 }
1516
1517 if (fsprops) {
1518 uint64_t zoned;
1519 const char *zonestr;
1520
1521 zoned = ((nvlist_lookup_string(fsprops,
1522 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1523 strcmp(zonestr, "on") == 0);
1524
1525 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1526 fsprops, zoned, NULL, NULL, B_TRUE, errbuf)) == NULL) {
1527 goto create_failed;
1528 }
1529
1530 if (!zc_props &&
1531 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1532 goto create_failed;
1533 }
1534 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1535 &wkeydata, &wkeylen) != 0) {
1536 zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
1537 goto create_failed;
1538 }
1539 if (nvlist_add_nvlist(zc_props,
1540 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1541 goto create_failed;
1542 }
1543 if (wkeydata != NULL) {
1544 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1545 goto create_failed;
1546
1547 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1548 wkeydata, wkeylen) != 0)
1549 goto create_failed;
1550
1551 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1552 hidden_args) != 0)
1553 goto create_failed;
1554 }
1555 }
1556
1557 if (zc_props)
1558 zcmd_write_src_nvlist(hdl, &zc, zc_props);
1559
1560 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1561
1562 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1563
1564 zcmd_free_nvlists(&zc);
1565 nvlist_free(zc_props);
1566 nvlist_free(zc_fsprops);
1567 nvlist_free(hidden_args);
1568 if (wkeydata != NULL)
1569 free(wkeydata);
1570
1571 switch (errno) {
1572 case EBUSY:
1573 /*
1574 * This can happen if the user has specified the same
1575 * device multiple times. We can't reliably detect this
1576 * until we try to add it and see we already have a
1577 * label. This can also happen under if the device is
1578 * part of an active md or lvm device.
1579 */
1580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1581 "one or more vdevs refer to the same device, or "
1582 "one of\nthe devices is part of an active md or "
1583 "lvm device"));
1584 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1585
1586 case ERANGE:
1587 /*
1588 * This happens if the record size is smaller or larger
1589 * than the allowed size range, or not a power of 2.
1590 *
1591 * NOTE: although zfs_valid_proplist is called earlier,
1592 * this case may have slipped through since the
1593 * pool does not exist yet and it is therefore
1594 * impossible to read properties e.g. max blocksize
1595 * from the pool.
1596 */
1597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1598 "record size invalid"));
1599 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1600
1601 case EOVERFLOW:
1602 /*
1603 * This occurs when one of the devices is below
1604 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1605 * device was the problem device since there's no
1606 * reliable way to determine device size from userland.
1607 */
1608 {
1609 char buf[64];
1610
1611 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1612 sizeof (buf));
1613
1614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1615 "one or more devices is less than the "
1616 "minimum size (%s)"), buf);
1617 }
1618 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1619
1620 case ENOSPC:
1621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1622 "one or more devices is out of space"));
1623 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1624
1625 case EINVAL:
1626 if (zpool_has_draid_vdev(nvroot) &&
1627 zfeature_lookup_name("draid", NULL) != 0) {
1628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1629 "dRAID vdevs are unsupported by the "
1630 "kernel"));
1631 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1632 } else {
1633 return (zpool_standard_error(hdl, errno,
1634 errbuf));
1635 }
1636
1637 case ENXIO:
1638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1639 "one or more devices could not be opened"));
1640 return (zfs_error(hdl, EZFS_BADDEV, errbuf));
1641
1642 default:
1643 return (zpool_standard_error(hdl, errno, errbuf));
1644 }
1645 }
1646
1647 create_failed:
1648 zcmd_free_nvlists(&zc);
1649 nvlist_free(zc_props);
1650 nvlist_free(zc_fsprops);
1651 nvlist_free(hidden_args);
1652 if (wkeydata != NULL)
1653 free(wkeydata);
1654 return (ret);
1655 }
1656
1657 /*
1658 * Destroy the given pool. It is up to the caller to ensure that there are no
1659 * datasets left in the pool.
1660 */
1661 int
zpool_destroy(zpool_handle_t * zhp,const char * log_str)1662 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1663 {
1664 zfs_cmd_t zc = {"\0"};
1665 zfs_handle_t *zfp = NULL;
1666 libzfs_handle_t *hdl = zhp->zpool_hdl;
1667 char errbuf[ERRBUFLEN];
1668
1669 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1670 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1671 return (-1);
1672
1673 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1674 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1675
1676 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1677 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1678 "cannot destroy '%s'"), zhp->zpool_name);
1679
1680 if (errno == EROFS) {
1681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1682 "one or more devices is read only"));
1683 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1684 } else {
1685 (void) zpool_standard_error(hdl, errno, errbuf);
1686 }
1687
1688 if (zfp)
1689 zfs_close(zfp);
1690 return (-1);
1691 }
1692
1693 if (zfp) {
1694 remove_mountpoint(zfp);
1695 zfs_close(zfp);
1696 }
1697
1698 return (0);
1699 }
1700
1701 /*
1702 * Create a checkpoint in the given pool.
1703 */
1704 int
zpool_checkpoint(zpool_handle_t * zhp)1705 zpool_checkpoint(zpool_handle_t *zhp)
1706 {
1707 libzfs_handle_t *hdl = zhp->zpool_hdl;
1708 char errbuf[ERRBUFLEN];
1709 int error;
1710
1711 error = lzc_pool_checkpoint(zhp->zpool_name);
1712 if (error != 0) {
1713 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1714 "cannot checkpoint '%s'"), zhp->zpool_name);
1715 (void) zpool_standard_error(hdl, error, errbuf);
1716 return (-1);
1717 }
1718
1719 return (0);
1720 }
1721
1722 /*
1723 * Discard the checkpoint from the given pool.
1724 */
1725 int
zpool_discard_checkpoint(zpool_handle_t * zhp)1726 zpool_discard_checkpoint(zpool_handle_t *zhp)
1727 {
1728 libzfs_handle_t *hdl = zhp->zpool_hdl;
1729 char errbuf[ERRBUFLEN];
1730 int error;
1731
1732 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1733 if (error != 0) {
1734 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1735 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1736 (void) zpool_standard_error(hdl, error, errbuf);
1737 return (-1);
1738 }
1739
1740 return (0);
1741 }
1742
1743 /*
1744 * Load data type for the given pool.
1745 */
1746 int
zpool_prefetch(zpool_handle_t * zhp,zpool_prefetch_type_t type)1747 zpool_prefetch(zpool_handle_t *zhp, zpool_prefetch_type_t type)
1748 {
1749 libzfs_handle_t *hdl = zhp->zpool_hdl;
1750 char msg[1024];
1751 int error;
1752
1753 error = lzc_pool_prefetch(zhp->zpool_name, type);
1754 if (error != 0) {
1755 const char *typename = "unknown";
1756 if (type == ZPOOL_PREFETCH_DDT)
1757 typename = "ddt";
1758 else if (type == ZPOOL_PREFETCH_BRT)
1759 typename = "brt";
1760 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1761 "cannot prefetch %s in '%s'"), typename, zhp->zpool_name);
1762 (void) zpool_standard_error(hdl, error, msg);
1763 return (-1);
1764 }
1765
1766 return (0);
1767 }
1768
1769 /*
1770 * Add the given vdevs to the pool. The caller must have already performed the
1771 * necessary verification to ensure that the vdev specification is well-formed.
1772 */
1773 int
zpool_add(zpool_handle_t * zhp,nvlist_t * nvroot,boolean_t check_ashift)1774 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot, boolean_t check_ashift)
1775 {
1776 zfs_cmd_t zc = {"\0"};
1777 int ret;
1778 libzfs_handle_t *hdl = zhp->zpool_hdl;
1779 char errbuf[ERRBUFLEN];
1780 nvlist_t **spares, **l2cache;
1781 uint_t nspares, nl2cache;
1782
1783 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1784 "cannot add to '%s'"), zhp->zpool_name);
1785
1786 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1787 SPA_VERSION_SPARES &&
1788 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1789 &spares, &nspares) == 0) {
1790 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1791 "upgraded to add hot spares"));
1792 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
1793 }
1794
1795 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1796 SPA_VERSION_L2CACHE &&
1797 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1798 &l2cache, &nl2cache) == 0) {
1799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1800 "upgraded to add cache devices"));
1801 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
1802 }
1803
1804 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
1805 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1806 zc.zc_flags = check_ashift;
1807
1808 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1809 switch (errno) {
1810 case EBUSY:
1811 /*
1812 * This can happen if the user has specified the same
1813 * device multiple times. We can't reliably detect this
1814 * until we try to add it and see we already have a
1815 * label.
1816 */
1817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1818 "one or more vdevs refer to the same device"));
1819 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1820 break;
1821
1822 case EINVAL:
1823
1824 if (zpool_has_draid_vdev(nvroot) &&
1825 zfeature_lookup_name("draid", NULL) != 0) {
1826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1827 "dRAID vdevs are unsupported by the "
1828 "kernel"));
1829 } else {
1830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1831 "invalid config; a pool with removing/"
1832 "removed vdevs does not support adding "
1833 "raidz or dRAID vdevs"));
1834 }
1835
1836 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1837 break;
1838
1839 case EOVERFLOW:
1840 /*
1841 * This occurs when one of the devices is below
1842 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1843 * device was the problem device since there's no
1844 * reliable way to determine device size from userland.
1845 */
1846 {
1847 char buf[64];
1848
1849 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1850 sizeof (buf));
1851
1852 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1853 "device is less than the minimum "
1854 "size (%s)"), buf);
1855 }
1856 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
1857 break;
1858
1859 case ENOTSUP:
1860 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1861 "pool must be upgraded to add these vdevs"));
1862 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1863 break;
1864
1865 default:
1866 (void) zpool_standard_error(hdl, errno, errbuf);
1867 }
1868
1869 ret = -1;
1870 } else {
1871 ret = 0;
1872 }
1873
1874 zcmd_free_nvlists(&zc);
1875
1876 return (ret);
1877 }
1878
1879 /*
1880 * Exports the pool from the system. The caller must ensure that there are no
1881 * mounted datasets in the pool.
1882 */
1883 static int
zpool_export_common(zpool_handle_t * zhp,boolean_t force,boolean_t hardforce,const char * log_str)1884 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1885 const char *log_str)
1886 {
1887 zfs_cmd_t zc = {"\0"};
1888
1889 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1890 zc.zc_cookie = force;
1891 zc.zc_guid = hardforce;
1892 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1893
1894 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1895 switch (errno) {
1896 case EXDEV:
1897 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1898 "use '-f' to override the following errors:\n"
1899 "'%s' has an active shared spare which could be"
1900 " used by other pools once '%s' is exported."),
1901 zhp->zpool_name, zhp->zpool_name);
1902 return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1903 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1904 zhp->zpool_name));
1905 default:
1906 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1907 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1908 zhp->zpool_name));
1909 }
1910 }
1911
1912 return (0);
1913 }
1914
1915 int
zpool_export(zpool_handle_t * zhp,boolean_t force,const char * log_str)1916 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1917 {
1918 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1919 }
1920
1921 int
zpool_export_force(zpool_handle_t * zhp,const char * log_str)1922 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1923 {
1924 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1925 }
1926
1927 static void
zpool_rewind_exclaim(libzfs_handle_t * hdl,const char * name,boolean_t dryrun,nvlist_t * config)1928 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1929 nvlist_t *config)
1930 {
1931 nvlist_t *nv = NULL;
1932 uint64_t rewindto;
1933 int64_t loss = -1;
1934 struct tm t;
1935 char timestr[128];
1936
1937 if (!hdl->libzfs_printerr || config == NULL)
1938 return;
1939
1940 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1941 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1942 return;
1943 }
1944
1945 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1946 return;
1947 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1948
1949 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1950 ctime_r((time_t *)&rewindto, timestr) != NULL) {
1951 timestr[24] = 0;
1952 if (dryrun) {
1953 (void) printf(dgettext(TEXT_DOMAIN,
1954 "Would be able to return %s "
1955 "to its state as of %s.\n"),
1956 name, timestr);
1957 } else {
1958 (void) printf(dgettext(TEXT_DOMAIN,
1959 "Pool %s returned to its state as of %s.\n"),
1960 name, timestr);
1961 }
1962 if (loss > 120) {
1963 (void) printf(dgettext(TEXT_DOMAIN,
1964 "%s approximately %lld "),
1965 dryrun ? "Would discard" : "Discarded",
1966 ((longlong_t)loss + 30) / 60);
1967 (void) printf(dgettext(TEXT_DOMAIN,
1968 "minutes of transactions.\n"));
1969 } else if (loss > 0) {
1970 (void) printf(dgettext(TEXT_DOMAIN,
1971 "%s approximately %lld "),
1972 dryrun ? "Would discard" : "Discarded",
1973 (longlong_t)loss);
1974 (void) printf(dgettext(TEXT_DOMAIN,
1975 "seconds of transactions.\n"));
1976 }
1977 }
1978 }
1979
1980 void
zpool_explain_recover(libzfs_handle_t * hdl,const char * name,int reason,nvlist_t * config,char * buf,size_t size)1981 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1982 nvlist_t *config, char *buf, size_t size)
1983 {
1984 nvlist_t *nv = NULL;
1985 int64_t loss = -1;
1986 uint64_t edata = UINT64_MAX;
1987 uint64_t rewindto;
1988 struct tm t;
1989 char timestr[128], temp[1024];
1990
1991 if (!hdl->libzfs_printerr)
1992 return;
1993
1994 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1995 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1996 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1997 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1998 goto no_info;
1999
2000 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
2001 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
2002 &edata);
2003
2004 (void) snprintf(buf, size, dgettext(TEXT_DOMAIN,
2005 "Recovery is possible, but will result in some data loss.\n"));
2006
2007 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
2008 ctime_r((time_t *)&rewindto, timestr) != NULL) {
2009 timestr[24] = 0;
2010 (void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2011 "\tReturning the pool to its state as of %s\n"
2012 "\tshould correct the problem. "), timestr);
2013 (void) strlcat(buf, temp, size);
2014 } else {
2015 (void) strlcat(buf, dgettext(TEXT_DOMAIN,
2016 "\tReverting the pool to an earlier state "
2017 "should correct the problem.\n\t"), size);
2018 }
2019
2020 if (loss > 120) {
2021 (void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2022 "Approximately %lld minutes of data\n"
2023 "\tmust be discarded, irreversibly. "),
2024 ((longlong_t)loss + 30) / 60);
2025 (void) strlcat(buf, temp, size);
2026 } else if (loss > 0) {
2027 (void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2028 "Approximately %lld seconds of data\n"
2029 "\tmust be discarded, irreversibly. "),
2030 (longlong_t)loss);
2031 (void) strlcat(buf, temp, size);
2032 }
2033 if (edata != 0 && edata != UINT64_MAX) {
2034 if (edata == 1) {
2035 (void) strlcat(buf, dgettext(TEXT_DOMAIN,
2036 "After rewind, at least\n"
2037 "\tone persistent user-data error will remain. "),
2038 size);
2039 } else {
2040 (void) strlcat(buf, dgettext(TEXT_DOMAIN,
2041 "After rewind, several\n"
2042 "\tpersistent user-data errors will remain. "),
2043 size);
2044 }
2045 }
2046 (void) snprintf(temp, 1024, dgettext(TEXT_DOMAIN,
2047 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
2048 reason >= 0 ? "clear" : "import", name);
2049 (void) strlcat(buf, temp, size);
2050
2051 (void) strlcat(buf, dgettext(TEXT_DOMAIN,
2052 "A scrub of the pool\n"
2053 "\tis strongly recommended after recovery.\n"), size);
2054 return;
2055
2056 no_info:
2057 (void) strlcat(buf, dgettext(TEXT_DOMAIN,
2058 "Ensure all pool devices are present and accessible, then "
2059 "retry the import.\n\tIf the problem persists, destroy and "
2060 "re-create the pool from a backup source.\n"), size);
2061 }
2062
2063 /*
2064 * zpool_import() is a contracted interface. Should be kept the same
2065 * if possible.
2066 *
2067 * Applications should use zpool_import_props() to import a pool with
2068 * new properties value to be set.
2069 */
2070 int
zpool_import(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,char * altroot)2071 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
2072 char *altroot)
2073 {
2074 nvlist_t *props = NULL;
2075 int ret;
2076
2077 if (altroot != NULL) {
2078 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2079 return (zfs_error_fmt(hdl, EZFS_NOMEM,
2080 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2081 newname));
2082 }
2083
2084 if (nvlist_add_string(props,
2085 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
2086 nvlist_add_string(props,
2087 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
2088 nvlist_free(props);
2089 return (zfs_error_fmt(hdl, EZFS_NOMEM,
2090 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2091 newname));
2092 }
2093 }
2094
2095 ret = zpool_import_props(hdl, config, newname, props,
2096 ZFS_IMPORT_NORMAL);
2097 nvlist_free(props);
2098 return (ret);
2099 }
2100
2101 static void
print_vdev_tree(libzfs_handle_t * hdl,const char * name,nvlist_t * nv,int indent)2102 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
2103 int indent)
2104 {
2105 nvlist_t **child;
2106 uint_t c, children;
2107 char *vname;
2108 uint64_t is_log = 0;
2109
2110 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
2111 &is_log);
2112
2113 if (name != NULL)
2114 (void) printf("\t%*s%s%s\n", indent, "", name,
2115 is_log ? " [log]" : "");
2116
2117 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2118 &child, &children) != 0)
2119 return;
2120
2121 for (c = 0; c < children; c++) {
2122 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
2123 print_vdev_tree(hdl, vname, child[c], indent + 2);
2124 free(vname);
2125 }
2126 }
2127
2128 void
zpool_collect_unsup_feat(nvlist_t * config,char * buf,size_t size)2129 zpool_collect_unsup_feat(nvlist_t *config, char *buf, size_t size)
2130 {
2131 nvlist_t *nvinfo, *unsup_feat;
2132 char temp[512];
2133
2134 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
2135 unsup_feat = fnvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT);
2136
2137 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL);
2138 nvp != NULL; nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
2139 const char *desc = fnvpair_value_string(nvp);
2140 if (strlen(desc) > 0) {
2141 (void) snprintf(temp, 512, "\t%s (%s)\n",
2142 nvpair_name(nvp), desc);
2143 (void) strlcat(buf, temp, size);
2144 } else {
2145 (void) snprintf(temp, 512, "\t%s\n", nvpair_name(nvp));
2146 (void) strlcat(buf, temp, size);
2147 }
2148 }
2149 }
2150
2151 /*
2152 * Import the given pool using the known configuration and a list of
2153 * properties to be set. The configuration should have come from
2154 * zpool_find_import(). The 'newname' parameters control whether the pool
2155 * is imported with a different name.
2156 */
2157 int
zpool_import_props(libzfs_handle_t * hdl,nvlist_t * config,const char * newname,nvlist_t * props,int flags)2158 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
2159 nvlist_t *props, int flags)
2160 {
2161 zfs_cmd_t zc = {"\0"};
2162 zpool_load_policy_t policy;
2163 nvlist_t *nv = NULL;
2164 nvlist_t *nvinfo = NULL;
2165 nvlist_t *missing = NULL;
2166 const char *thename;
2167 const char *origname;
2168 int ret;
2169 int error = 0;
2170 char buf[2048];
2171 char errbuf[ERRBUFLEN];
2172
2173 origname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
2174
2175 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2176 "cannot import pool '%s'"), origname);
2177
2178 if (newname != NULL) {
2179 if (!zpool_name_valid(hdl, B_FALSE, newname))
2180 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2181 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2182 newname));
2183 thename = newname;
2184 } else {
2185 thename = origname;
2186 }
2187
2188 if (props != NULL) {
2189 uint64_t version;
2190 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2191
2192 version = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
2193
2194 if ((props = zpool_valid_proplist(hdl, origname,
2195 props, version, flags, errbuf)) == NULL)
2196 return (-1);
2197 zcmd_write_src_nvlist(hdl, &zc, props);
2198 nvlist_free(props);
2199 }
2200
2201 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
2202
2203 zc.zc_guid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
2204
2205 zcmd_write_conf_nvlist(hdl, &zc, config);
2206 zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2);
2207
2208 zc.zc_cookie = flags;
2209 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
2210 errno == ENOMEM)
2211 zcmd_expand_dst_nvlist(hdl, &zc);
2212 if (ret != 0)
2213 error = errno;
2214
2215 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
2216
2217 zcmd_free_nvlists(&zc);
2218
2219 zpool_get_load_policy(config, &policy);
2220
2221 if (getenv("ZFS_LOAD_INFO_DEBUG") && nv != NULL &&
2222 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2223 dump_nvlist(nvinfo, 4);
2224 }
2225
2226 if (error) {
2227 char desc[1024];
2228 char aux[256];
2229
2230 /*
2231 * Dry-run failed, but we print out what success
2232 * looks like if we found a best txg
2233 */
2234 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
2235 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2236 B_TRUE, nv);
2237 nvlist_free(nv);
2238 return (-1);
2239 }
2240
2241 if (newname == NULL)
2242 (void) snprintf(desc, sizeof (desc),
2243 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2244 thename);
2245 else
2246 (void) snprintf(desc, sizeof (desc),
2247 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2248 origname, thename);
2249
2250 switch (error) {
2251 case ENOTSUP:
2252 if (nv != NULL && nvlist_lookup_nvlist(nv,
2253 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2254 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2255 (void) printf(dgettext(TEXT_DOMAIN, "This "
2256 "pool uses the following feature(s) not "
2257 "supported by this system:\n"));
2258 memset(buf, 0, 2048);
2259 zpool_collect_unsup_feat(nv, buf, 2048);
2260 (void) printf("%s", buf);
2261 if (nvlist_exists(nvinfo,
2262 ZPOOL_CONFIG_CAN_RDONLY)) {
2263 (void) printf(dgettext(TEXT_DOMAIN,
2264 "All unsupported features are only "
2265 "required for writing to the pool."
2266 "\nThe pool can be imported using "
2267 "'-o readonly=on'.\n"));
2268 }
2269 }
2270 /*
2271 * Unsupported version.
2272 */
2273 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
2274 break;
2275
2276 case EREMOTEIO:
2277 if (nv != NULL && nvlist_lookup_nvlist(nv,
2278 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2279 const char *hostname = "<unknown>";
2280 uint64_t hostid = 0;
2281 mmp_state_t mmp_state;
2282
2283 mmp_state = fnvlist_lookup_uint64(nvinfo,
2284 ZPOOL_CONFIG_MMP_STATE);
2285
2286 if (nvlist_exists(nvinfo,
2287 ZPOOL_CONFIG_MMP_HOSTNAME))
2288 hostname = fnvlist_lookup_string(nvinfo,
2289 ZPOOL_CONFIG_MMP_HOSTNAME);
2290
2291 if (nvlist_exists(nvinfo,
2292 ZPOOL_CONFIG_MMP_HOSTID))
2293 hostid = fnvlist_lookup_uint64(nvinfo,
2294 ZPOOL_CONFIG_MMP_HOSTID);
2295
2296 if (mmp_state == MMP_STATE_ACTIVE) {
2297 (void) snprintf(aux, sizeof (aux),
2298 dgettext(TEXT_DOMAIN, "pool is imp"
2299 "orted on host '%s' (hostid=%lx).\n"
2300 "Export the pool on the other "
2301 "system, then run 'zpool import'."),
2302 hostname, (unsigned long) hostid);
2303 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2304 (void) snprintf(aux, sizeof (aux),
2305 dgettext(TEXT_DOMAIN, "pool has "
2306 "the multihost property on and "
2307 "the\nsystem's hostid is not set. "
2308 "Set a unique system hostid with "
2309 "the zgenhostid(8) command.\n"));
2310 }
2311
2312 (void) zfs_error_aux(hdl, "%s", aux);
2313 }
2314 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2315 break;
2316
2317 case EINVAL:
2318 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2319 break;
2320
2321 case EROFS:
2322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2323 "one or more devices is read only"));
2324 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2325 break;
2326
2327 case ENXIO:
2328 if (nv && nvlist_lookup_nvlist(nv,
2329 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2330 nvlist_lookup_nvlist(nvinfo,
2331 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2332 (void) printf(dgettext(TEXT_DOMAIN,
2333 "The devices below are missing or "
2334 "corrupted, use '-m' to import the pool "
2335 "anyway:\n"));
2336 print_vdev_tree(hdl, NULL, missing, 2);
2337 (void) printf("\n");
2338 }
2339 (void) zpool_standard_error(hdl, error, desc);
2340 break;
2341
2342 case EEXIST:
2343 (void) zpool_standard_error(hdl, error, desc);
2344 break;
2345
2346 case EBUSY:
2347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2348 "one or more devices are already in use\n"));
2349 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2350 break;
2351 case ENAMETOOLONG:
2352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2353 "new name of at least one dataset is longer than "
2354 "the maximum allowable length"));
2355 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2356 break;
2357 default:
2358 (void) zpool_standard_error(hdl, error, desc);
2359 memset(buf, 0, 2048);
2360 zpool_explain_recover(hdl,
2361 newname ? origname : thename, -error, nv,
2362 buf, 2048);
2363 (void) printf("\t%s", buf);
2364 break;
2365 }
2366
2367 nvlist_free(nv);
2368 ret = -1;
2369 } else {
2370 zpool_handle_t *zhp;
2371
2372 /*
2373 * This should never fail, but play it safe anyway.
2374 */
2375 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2376 ret = -1;
2377 else if (zhp != NULL)
2378 zpool_close(zhp);
2379 if (policy.zlp_rewind &
2380 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2381 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2382 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2383 }
2384 nvlist_free(nv);
2385 }
2386
2387 return (ret);
2388 }
2389
2390 /*
2391 * Translate vdev names to guids. If a vdev_path is determined to be
2392 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2393 * are added to it.
2394 */
2395 static int
zpool_translate_vdev_guids(zpool_handle_t * zhp,nvlist_t * vds,nvlist_t * vdev_guids,nvlist_t * guids_to_paths,nvlist_t ** vd_errlist)2396 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2397 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2398 {
2399 nvlist_t *errlist = NULL;
2400 int error = 0;
2401
2402 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2403 elem = nvlist_next_nvpair(vds, elem)) {
2404 boolean_t spare, cache;
2405
2406 const char *vd_path = nvpair_name(elem);
2407 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2408 NULL);
2409
2410 if ((tgt == NULL) || cache || spare) {
2411 if (errlist == NULL) {
2412 errlist = fnvlist_alloc();
2413 error = EINVAL;
2414 }
2415
2416 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2417 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2418 fnvlist_add_int64(errlist, vd_path, err);
2419 continue;
2420 }
2421
2422 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2423 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2424
2425 char msg[MAXNAMELEN];
2426 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2427 fnvlist_add_string(guids_to_paths, msg, vd_path);
2428 }
2429
2430 if (error != 0) {
2431 verify(errlist != NULL);
2432 if (vd_errlist != NULL)
2433 *vd_errlist = errlist;
2434 else
2435 fnvlist_free(errlist);
2436 }
2437
2438 return (error);
2439 }
2440
2441 static int
xlate_init_err(int err)2442 xlate_init_err(int err)
2443 {
2444 switch (err) {
2445 case ENODEV:
2446 return (EZFS_NODEVICE);
2447 case EINVAL:
2448 case EROFS:
2449 return (EZFS_BADDEV);
2450 case EBUSY:
2451 return (EZFS_INITIALIZING);
2452 case ESRCH:
2453 return (EZFS_NO_INITIALIZE);
2454 }
2455 return (err);
2456 }
2457
2458 int
zpool_initialize_one(zpool_handle_t * zhp,void * data)2459 zpool_initialize_one(zpool_handle_t *zhp, void *data)
2460 {
2461 int error;
2462 libzfs_handle_t *hdl = zpool_get_handle(zhp);
2463 const char *pool_name = zpool_get_name(zhp);
2464 if (zpool_open_silent(hdl, pool_name, &zhp) != 0)
2465 return (-1);
2466 initialize_cbdata_t *cb = data;
2467 nvlist_t *vdevs = fnvlist_alloc();
2468
2469 nvlist_t *config = zpool_get_config(zhp, NULL);
2470 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
2471 ZPOOL_CONFIG_VDEV_TREE);
2472 zpool_collect_leaves(zhp, nvroot, vdevs);
2473 if (cb->wait)
2474 error = zpool_initialize_wait(zhp, cb->cmd_type, vdevs);
2475 else
2476 error = zpool_initialize(zhp, cb->cmd_type, vdevs);
2477 fnvlist_free(vdevs);
2478
2479 return (error);
2480 }
2481
2482 /*
2483 * Begin, suspend, cancel, or uninit (clear) the initialization (initializing
2484 * of all free blocks) for the given vdevs in the given pool.
2485 */
2486 static int
zpool_initialize_impl(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds,boolean_t wait)2487 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2488 nvlist_t *vds, boolean_t wait)
2489 {
2490 int err;
2491
2492 nvlist_t *vdev_guids = fnvlist_alloc();
2493 nvlist_t *guids_to_paths = fnvlist_alloc();
2494 nvlist_t *vd_errlist = NULL;
2495 nvlist_t *errlist;
2496 nvpair_t *elem;
2497
2498 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2499 guids_to_paths, &vd_errlist);
2500
2501 if (err != 0) {
2502 verify(vd_errlist != NULL);
2503 goto list_errors;
2504 }
2505
2506 err = lzc_initialize(zhp->zpool_name, cmd_type,
2507 vdev_guids, &errlist);
2508
2509 if (err != 0) {
2510 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2511 ZPOOL_INITIALIZE_VDEVS, &vd_errlist) == 0) {
2512 goto list_errors;
2513 }
2514
2515 if (err == EINVAL && cmd_type == POOL_INITIALIZE_UNINIT) {
2516 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2517 "uninitialize is not supported by kernel"));
2518 }
2519
2520 (void) zpool_standard_error(zhp->zpool_hdl, err,
2521 dgettext(TEXT_DOMAIN, "operation failed"));
2522 goto out;
2523 }
2524
2525 if (wait) {
2526 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2527 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2528
2529 uint64_t guid = fnvpair_value_uint64(elem);
2530
2531 err = lzc_wait_tag(zhp->zpool_name,
2532 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2533 if (err != 0) {
2534 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2535 err, dgettext(TEXT_DOMAIN, "error "
2536 "waiting for '%s' to initialize"),
2537 nvpair_name(elem));
2538
2539 goto out;
2540 }
2541 }
2542 }
2543 goto out;
2544
2545 list_errors:
2546 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2547 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2548 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2549 const char *path;
2550
2551 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2552 &path) != 0)
2553 path = nvpair_name(elem);
2554
2555 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2556 "cannot initialize '%s'", path);
2557 }
2558
2559 out:
2560 fnvlist_free(vdev_guids);
2561 fnvlist_free(guids_to_paths);
2562
2563 if (vd_errlist != NULL)
2564 fnvlist_free(vd_errlist);
2565
2566 return (err == 0 ? 0 : -1);
2567 }
2568
2569 int
zpool_initialize(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds)2570 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2571 nvlist_t *vds)
2572 {
2573 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2574 }
2575
2576 int
zpool_initialize_wait(zpool_handle_t * zhp,pool_initialize_func_t cmd_type,nvlist_t * vds)2577 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2578 nvlist_t *vds)
2579 {
2580 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2581 }
2582
2583 static int
xlate_trim_err(int err)2584 xlate_trim_err(int err)
2585 {
2586 switch (err) {
2587 case ENODEV:
2588 return (EZFS_NODEVICE);
2589 case EINVAL:
2590 case EROFS:
2591 return (EZFS_BADDEV);
2592 case EBUSY:
2593 return (EZFS_TRIMMING);
2594 case ESRCH:
2595 return (EZFS_NO_TRIM);
2596 case EOPNOTSUPP:
2597 return (EZFS_TRIM_NOTSUP);
2598 }
2599 return (err);
2600 }
2601
2602 void
zpool_collect_leaves(zpool_handle_t * zhp,nvlist_t * nvroot,nvlist_t * res)2603 zpool_collect_leaves(zpool_handle_t *zhp, nvlist_t *nvroot, nvlist_t *res)
2604 {
2605 libzfs_handle_t *hdl = zhp->zpool_hdl;
2606 uint_t children = 0;
2607 nvlist_t **child;
2608 uint_t i;
2609
2610 (void) nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2611 &child, &children);
2612
2613 if (children == 0) {
2614 char *path = zpool_vdev_name(hdl, zhp, nvroot,
2615 VDEV_NAME_PATH);
2616
2617 if (strcmp(path, VDEV_TYPE_INDIRECT) != 0 &&
2618 strcmp(path, VDEV_TYPE_HOLE) != 0)
2619 fnvlist_add_boolean(res, path);
2620
2621 free(path);
2622 return;
2623 }
2624
2625 for (i = 0; i < children; i++) {
2626 zpool_collect_leaves(zhp, child[i], res);
2627 }
2628 }
2629
2630 int
zpool_trim_one(zpool_handle_t * zhp,void * data)2631 zpool_trim_one(zpool_handle_t *zhp, void *data)
2632 {
2633 int error;
2634 libzfs_handle_t *hdl = zpool_get_handle(zhp);
2635 const char *pool_name = zpool_get_name(zhp);
2636 if (zpool_open_silent(hdl, pool_name, &zhp) != 0)
2637 return (-1);
2638
2639 trim_cbdata_t *cb = data;
2640 nvlist_t *vdevs = fnvlist_alloc();
2641
2642 /* no individual leaf vdevs specified, so add them all */
2643 nvlist_t *config = zpool_get_config(zhp, NULL);
2644 nvlist_t *nvroot = fnvlist_lookup_nvlist(config,
2645 ZPOOL_CONFIG_VDEV_TREE);
2646
2647 zpool_collect_leaves(zhp, nvroot, vdevs);
2648 error = zpool_trim(zhp, cb->cmd_type, vdevs, &cb->trim_flags);
2649 fnvlist_free(vdevs);
2650
2651 return (error);
2652 }
2653
2654 static int
zpool_trim_wait(zpool_handle_t * zhp,nvlist_t * vdev_guids)2655 zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2656 {
2657 int err;
2658 nvpair_t *elem;
2659
2660 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2661 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2662
2663 uint64_t guid = fnvpair_value_uint64(elem);
2664
2665 err = lzc_wait_tag(zhp->zpool_name,
2666 ZPOOL_WAIT_TRIM, guid, NULL);
2667 if (err != 0) {
2668 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2669 err, dgettext(TEXT_DOMAIN, "error "
2670 "waiting to trim '%s'"), nvpair_name(elem));
2671
2672 return (err);
2673 }
2674 }
2675 return (0);
2676 }
2677
2678 /*
2679 * Check errlist and report any errors, omitting ones which should be
2680 * suppressed. Returns B_TRUE if any errors were reported.
2681 */
2682 static boolean_t
check_trim_errs(zpool_handle_t * zhp,trimflags_t * trim_flags,nvlist_t * guids_to_paths,nvlist_t * vds,nvlist_t * errlist)2683 check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2684 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
2685 {
2686 nvpair_t *elem;
2687 boolean_t reported_errs = B_FALSE;
2688 int num_vds = 0;
2689 int num_suppressed_errs = 0;
2690
2691 for (elem = nvlist_next_nvpair(vds, NULL);
2692 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2693 num_vds++;
2694 }
2695
2696 for (elem = nvlist_next_nvpair(errlist, NULL);
2697 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
2698 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2699 const char *path;
2700
2701 /*
2702 * If only the pool was specified, and it was not a secure
2703 * trim then suppress warnings for individual vdevs which
2704 * do not support trimming.
2705 */
2706 if (vd_error == EZFS_TRIM_NOTSUP &&
2707 trim_flags->fullpool &&
2708 !trim_flags->secure) {
2709 num_suppressed_errs++;
2710 continue;
2711 }
2712
2713 reported_errs = B_TRUE;
2714 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2715 &path) != 0)
2716 path = nvpair_name(elem);
2717
2718 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2719 "cannot trim '%s'", path);
2720 }
2721
2722 if (num_suppressed_errs == num_vds) {
2723 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2724 "no devices in pool support trim operations"));
2725 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2726 dgettext(TEXT_DOMAIN, "cannot trim")));
2727 reported_errs = B_TRUE;
2728 }
2729
2730 return (reported_errs);
2731 }
2732
2733 /*
2734 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2735 * the given vdevs in the given pool.
2736 */
2737 int
zpool_trim(zpool_handle_t * zhp,pool_trim_func_t cmd_type,nvlist_t * vds,trimflags_t * trim_flags)2738 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2739 trimflags_t *trim_flags)
2740 {
2741 int err;
2742 int retval = 0;
2743
2744 nvlist_t *vdev_guids = fnvlist_alloc();
2745 nvlist_t *guids_to_paths = fnvlist_alloc();
2746 nvlist_t *errlist = NULL;
2747
2748 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2749 guids_to_paths, &errlist);
2750 if (err != 0) {
2751 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2752 retval = -1;
2753 goto out;
2754 }
2755
2756 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2757 trim_flags->secure, vdev_guids, &errlist);
2758 if (err != 0) {
2759 nvlist_t *vd_errlist;
2760 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2761 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2762 if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2763 vds, vd_errlist)) {
2764 retval = -1;
2765 goto out;
2766 }
2767 } else {
2768 char errbuf[ERRBUFLEN];
2769
2770 (void) snprintf(errbuf, sizeof (errbuf),
2771 dgettext(TEXT_DOMAIN, "operation failed"));
2772 zpool_standard_error(zhp->zpool_hdl, err, errbuf);
2773 retval = -1;
2774 goto out;
2775 }
2776 }
2777
2778
2779 if (trim_flags->wait)
2780 retval = zpool_trim_wait(zhp, vdev_guids);
2781
2782 out:
2783 if (errlist != NULL)
2784 fnvlist_free(errlist);
2785 fnvlist_free(vdev_guids);
2786 fnvlist_free(guids_to_paths);
2787 return (retval);
2788 }
2789
2790 /*
2791 * Scan the pool.
2792 */
2793 int
zpool_scan(zpool_handle_t * zhp,pool_scan_func_t func,pool_scrub_cmd_t cmd)2794 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd) {
2795 return (zpool_scan_range(zhp, func, cmd, 0, 0));
2796 }
2797
2798 int
zpool_scan_range(zpool_handle_t * zhp,pool_scan_func_t func,pool_scrub_cmd_t cmd,time_t date_start,time_t date_end)2799 zpool_scan_range(zpool_handle_t *zhp, pool_scan_func_t func,
2800 pool_scrub_cmd_t cmd, time_t date_start, time_t date_end)
2801 {
2802 char errbuf[ERRBUFLEN];
2803 int err;
2804 libzfs_handle_t *hdl = zhp->zpool_hdl;
2805
2806 nvlist_t *args = fnvlist_alloc();
2807 fnvlist_add_uint64(args, "scan_type", (uint64_t)func);
2808 fnvlist_add_uint64(args, "scan_command", (uint64_t)cmd);
2809 fnvlist_add_uint64(args, "scan_date_start", (uint64_t)date_start);
2810 fnvlist_add_uint64(args, "scan_date_end", (uint64_t)date_end);
2811
2812 err = lzc_scrub(ZFS_IOC_POOL_SCRUB, zhp->zpool_name, args, NULL);
2813 fnvlist_free(args);
2814
2815 if (err == 0) {
2816 return (0);
2817 } else if (err == ZFS_ERR_IOC_CMD_UNAVAIL) {
2818 zfs_cmd_t zc = {"\0"};
2819 (void) strlcpy(zc.zc_name, zhp->zpool_name,
2820 sizeof (zc.zc_name));
2821 zc.zc_cookie = func;
2822 zc.zc_flags = cmd;
2823
2824 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2825 return (0);
2826 }
2827
2828 /*
2829 * An ECANCELED on a scrub means one of the following:
2830 * 1. we resumed a paused scrub.
2831 * 2. we resumed a paused error scrub.
2832 * 3. Error scrub is not run because of no error log.
2833 *
2834 * Note that we no longer return ECANCELED in case 1 or 2. However, in
2835 * order to prevent problems where we have a newer userland than
2836 * kernel, we keep this check in place. That prevents erroneous
2837 * failures when an older kernel returns ECANCELED in those cases.
2838 */
2839 if (err == ECANCELED && (func == POOL_SCAN_SCRUB ||
2840 func == POOL_SCAN_ERRORSCRUB) && cmd == POOL_SCRUB_NORMAL)
2841 return (0);
2842 /*
2843 * The following cases have been handled here:
2844 * 1. Paused a scrub/error scrub if there is none in progress.
2845 */
2846 if (err == ENOENT && func != POOL_SCAN_NONE && cmd ==
2847 POOL_SCRUB_PAUSE) {
2848 return (0);
2849 }
2850
2851 ASSERT3U(func, >=, POOL_SCAN_NONE);
2852 ASSERT3U(func, <, POOL_SCAN_FUNCS);
2853
2854 if (func == POOL_SCAN_SCRUB || func == POOL_SCAN_ERRORSCRUB) {
2855 if (cmd == POOL_SCRUB_PAUSE) {
2856 (void) snprintf(errbuf, sizeof (errbuf),
2857 dgettext(TEXT_DOMAIN, "cannot pause scrubbing %s"),
2858 zhp->zpool_name);
2859 } else {
2860 assert(cmd == POOL_SCRUB_NORMAL);
2861 (void) snprintf(errbuf, sizeof (errbuf),
2862 dgettext(TEXT_DOMAIN, "cannot scrub %s"),
2863 zhp->zpool_name);
2864 }
2865 } else if (func == POOL_SCAN_RESILVER) {
2866 assert(cmd == POOL_SCRUB_NORMAL);
2867 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2868 "cannot restart resilver on %s"), zhp->zpool_name);
2869 } else if (func == POOL_SCAN_NONE) {
2870 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2871 "cannot cancel scrubbing %s"), zhp->zpool_name);
2872 } else {
2873 assert(!"unexpected result");
2874 }
2875
2876 /*
2877 * With EBUSY, six cases are possible:
2878 *
2879 * Current state Requested
2880 * 1. Normal Scrub Running Normal Scrub or Error Scrub
2881 * 2. Normal Scrub Paused Error Scrub
2882 * 3. Normal Scrub Paused Pause Normal Scrub
2883 * 4. Error Scrub Running Normal Scrub or Error Scrub
2884 * 5. Error Scrub Paused Pause Error Scrub
2885 * 6. Resilvering Anything else
2886 */
2887 if (err == EBUSY) {
2888 nvlist_t *nvroot;
2889 pool_scan_stat_t *ps = NULL;
2890 uint_t psc;
2891
2892 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
2893 ZPOOL_CONFIG_VDEV_TREE);
2894 (void) nvlist_lookup_uint64_array(nvroot,
2895 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2896 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2897 ps->pss_state == DSS_SCANNING) {
2898 if (ps->pss_pass_scrub_pause == 0) {
2899 /* handles case 1 */
2900 assert(cmd == POOL_SCRUB_NORMAL);
2901 return (zfs_error(hdl, EZFS_SCRUBBING,
2902 errbuf));
2903 } else {
2904 if (func == POOL_SCAN_ERRORSCRUB) {
2905 /* handles case 2 */
2906 ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL);
2907 return (zfs_error(hdl,
2908 EZFS_SCRUB_PAUSED_TO_CANCEL,
2909 errbuf));
2910 } else {
2911 /* handles case 3 */
2912 ASSERT3U(func, ==, POOL_SCAN_SCRUB);
2913 ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE);
2914 return (zfs_error(hdl,
2915 EZFS_SCRUB_PAUSED, errbuf));
2916 }
2917 }
2918 } else if (ps &&
2919 ps->pss_error_scrub_func == POOL_SCAN_ERRORSCRUB &&
2920 ps->pss_error_scrub_state == DSS_ERRORSCRUBBING) {
2921 if (ps->pss_pass_error_scrub_pause == 0) {
2922 /* handles case 4 */
2923 ASSERT3U(cmd, ==, POOL_SCRUB_NORMAL);
2924 return (zfs_error(hdl, EZFS_ERRORSCRUBBING,
2925 errbuf));
2926 } else {
2927 /* handles case 5 */
2928 ASSERT3U(func, ==, POOL_SCAN_ERRORSCRUB);
2929 ASSERT3U(cmd, ==, POOL_SCRUB_PAUSE);
2930 return (zfs_error(hdl, EZFS_ERRORSCRUB_PAUSED,
2931 errbuf));
2932 }
2933 } else {
2934 /* handles case 6 */
2935 return (zfs_error(hdl, EZFS_RESILVERING, errbuf));
2936 }
2937 } else if (err == ENOENT) {
2938 return (zfs_error(hdl, EZFS_NO_SCRUB, errbuf));
2939 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2940 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, errbuf));
2941 } else {
2942 return (zpool_standard_error(hdl, err, errbuf));
2943 }
2944 }
2945
2946 /*
2947 * Find a vdev that matches the search criteria specified. We use the
2948 * the nvpair name to determine how we should look for the device.
2949 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2950 * spare; but FALSE if its an INUSE spare.
2951 *
2952 * If 'return_parent' is set, then return the *parent* of the vdev you're
2953 * searching for rather than the vdev itself.
2954 */
2955 static nvlist_t *
vdev_to_nvlist_iter(nvlist_t * nv,nvlist_t * search,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log,boolean_t return_parent)2956 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2957 boolean_t *l2cache, boolean_t *log, boolean_t return_parent)
2958 {
2959 uint_t c, children;
2960 nvlist_t **child;
2961 nvlist_t *ret;
2962 uint64_t is_log;
2963 const char *srchkey;
2964 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2965 const char *tmp = NULL;
2966 boolean_t is_root;
2967
2968 /* Nothing to look for */
2969 if (search == NULL || pair == NULL)
2970 return (NULL);
2971
2972 /* Obtain the key we will use to search */
2973 srchkey = nvpair_name(pair);
2974
2975 nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &tmp);
2976 if (strcmp(tmp, "root") == 0)
2977 is_root = B_TRUE;
2978 else
2979 is_root = B_FALSE;
2980
2981 switch (nvpair_type(pair)) {
2982 case DATA_TYPE_UINT64:
2983 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2984 uint64_t srchval = fnvpair_value_uint64(pair);
2985 uint64_t theguid = fnvlist_lookup_uint64(nv,
2986 ZPOOL_CONFIG_GUID);
2987 if (theguid == srchval)
2988 return (nv);
2989 }
2990 break;
2991
2992 case DATA_TYPE_STRING: {
2993 const char *srchval, *val;
2994
2995 srchval = fnvpair_value_string(pair);
2996 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2997 break;
2998
2999 /*
3000 * Search for the requested value. Special cases:
3001 *
3002 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
3003 * "-part1", or "p1". The suffix is hidden from the user,
3004 * but included in the string, so this matches around it.
3005 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
3006 * is used to check all possible expanded paths.
3007 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
3008 *
3009 * Otherwise, all other searches are simple string compares.
3010 */
3011 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
3012 uint64_t wholedisk = 0;
3013
3014 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3015 &wholedisk);
3016 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
3017 return (nv);
3018
3019 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0) {
3020 char *type, *idx, *end, *p;
3021 uint64_t id, vdev_id;
3022
3023 /*
3024 * Determine our vdev type, keeping in mind
3025 * that the srchval is composed of a type and
3026 * vdev id pair (i.e. mirror-4).
3027 */
3028 if ((type = strdup(srchval)) == NULL)
3029 return (NULL);
3030
3031 if ((p = strrchr(type, '-')) == NULL) {
3032 free(type);
3033 break;
3034 }
3035 idx = p + 1;
3036 *p = '\0';
3037
3038 /*
3039 * draid names are presented like: draid2:4d:6c:0s
3040 * We match them up to the first ':' so we can still
3041 * do the parity check below, but the other params
3042 * are ignored.
3043 */
3044 if ((p = strchr(type, ':')) != NULL) {
3045 if (strncmp(type, VDEV_TYPE_DRAID,
3046 strlen(VDEV_TYPE_DRAID)) == 0)
3047 *p = '\0';
3048 }
3049
3050 /*
3051 * If the types don't match then keep looking.
3052 */
3053 if (strncmp(val, type, strlen(val)) != 0) {
3054 free(type);
3055 break;
3056 }
3057
3058 verify(zpool_vdev_is_interior(type));
3059
3060 id = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID);
3061 errno = 0;
3062 vdev_id = strtoull(idx, &end, 10);
3063
3064 /*
3065 * If we are looking for a raidz and a parity is
3066 * specified, make sure it matches.
3067 */
3068 int rzlen = strlen(VDEV_TYPE_RAIDZ);
3069 assert(rzlen == strlen(VDEV_TYPE_DRAID));
3070 int typlen = strlen(type);
3071 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
3072 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
3073 typlen != rzlen) {
3074 uint64_t vdev_parity;
3075 int parity = *(type + rzlen) - '0';
3076
3077 if (parity <= 0 || parity > 3 ||
3078 (typlen - rzlen) != 1) {
3079 /*
3080 * Nonsense parity specified, can
3081 * never match
3082 */
3083 free(type);
3084 return (NULL);
3085 }
3086 vdev_parity = fnvlist_lookup_uint64(nv,
3087 ZPOOL_CONFIG_NPARITY);
3088 if ((int)vdev_parity != parity) {
3089 free(type);
3090 break;
3091 }
3092 }
3093
3094 free(type);
3095 if (errno != 0)
3096 return (NULL);
3097
3098 /*
3099 * Now verify that we have the correct vdev id.
3100 */
3101 if (vdev_id == id)
3102 return (nv);
3103 }
3104
3105 /*
3106 * Common case
3107 */
3108 if (strcmp(srchval, val) == 0)
3109 return (nv);
3110 break;
3111 }
3112
3113 default:
3114 break;
3115 }
3116
3117 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
3118 &child, &children) != 0)
3119 return (NULL);
3120
3121 for (c = 0; c < children; c++) {
3122 if ((ret = vdev_to_nvlist_iter(child[c], search,
3123 avail_spare, l2cache, NULL, return_parent)) != NULL) {
3124 /*
3125 * The 'is_log' value is only set for the toplevel
3126 * vdev, not the leaf vdevs. So we always lookup the
3127 * log device from the root of the vdev tree (where
3128 * 'log' is non-NULL).
3129 */
3130 if (log != NULL &&
3131 nvlist_lookup_uint64(child[c],
3132 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
3133 is_log) {
3134 *log = B_TRUE;
3135 }
3136 return (ret && return_parent && !is_root ? nv : ret);
3137 }
3138 }
3139
3140 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
3141 &child, &children) == 0) {
3142 for (c = 0; c < children; c++) {
3143 if ((ret = vdev_to_nvlist_iter(child[c], search,
3144 avail_spare, l2cache, NULL, return_parent))
3145 != NULL) {
3146 *avail_spare = B_TRUE;
3147 return (ret && return_parent &&
3148 !is_root ? nv : ret);
3149 }
3150 }
3151 }
3152
3153 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
3154 &child, &children) == 0) {
3155 for (c = 0; c < children; c++) {
3156 if ((ret = vdev_to_nvlist_iter(child[c], search,
3157 avail_spare, l2cache, NULL, return_parent))
3158 != NULL) {
3159 *l2cache = B_TRUE;
3160 return (ret && return_parent &&
3161 !is_root ? nv : ret);
3162 }
3163 }
3164 }
3165
3166 return (NULL);
3167 }
3168
3169 /*
3170 * Given a physical path or guid, find the associated vdev.
3171 */
3172 nvlist_t *
zpool_find_vdev_by_physpath(zpool_handle_t * zhp,const char * ppath,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3173 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
3174 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
3175 {
3176 nvlist_t *search, *nvroot, *ret;
3177 uint64_t guid;
3178 char *end;
3179
3180 search = fnvlist_alloc();
3181
3182 guid = strtoull(ppath, &end, 0);
3183 if (guid != 0 && *end == '\0') {
3184 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
3185 } else {
3186 fnvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath);
3187 }
3188
3189 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
3190 ZPOOL_CONFIG_VDEV_TREE);
3191
3192 *avail_spare = B_FALSE;
3193 *l2cache = B_FALSE;
3194 if (log != NULL)
3195 *log = B_FALSE;
3196 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log,
3197 B_FALSE);
3198 fnvlist_free(search);
3199
3200 return (ret);
3201 }
3202
3203 /*
3204 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
3205 */
3206 static boolean_t
zpool_vdev_is_interior(const char * name)3207 zpool_vdev_is_interior(const char *name)
3208 {
3209 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
3210 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
3211 strncmp(name,
3212 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
3213 strncmp(name, VDEV_TYPE_ROOT, strlen(VDEV_TYPE_ROOT)) == 0 ||
3214 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
3215 return (B_TRUE);
3216
3217 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
3218 !zpool_is_draid_spare(name))
3219 return (B_TRUE);
3220
3221 return (B_FALSE);
3222 }
3223
3224 /*
3225 * Lookup the nvlist for a given vdev or vdev's parent (depending on
3226 * if 'return_parent' is set).
3227 */
3228 static nvlist_t *
__zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log,boolean_t return_parent)3229 __zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
3230 boolean_t *l2cache, boolean_t *log, boolean_t return_parent)
3231 {
3232 char *end;
3233 nvlist_t *nvroot, *search, *ret;
3234 uint64_t guid;
3235 boolean_t __avail_spare, __l2cache, __log;
3236
3237 search = fnvlist_alloc();
3238
3239 guid = strtoull(path, &end, 0);
3240 if (guid != 0 && *end == '\0') {
3241 fnvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid);
3242 } else if (zpool_vdev_is_interior(path)) {
3243 fnvlist_add_string(search, ZPOOL_CONFIG_TYPE, path);
3244 } else {
3245 fnvlist_add_string(search, ZPOOL_CONFIG_PATH, path);
3246 }
3247
3248 nvroot = fnvlist_lookup_nvlist(zhp->zpool_config,
3249 ZPOOL_CONFIG_VDEV_TREE);
3250
3251 /*
3252 * User can pass NULL for avail_spare, l2cache, and log, but
3253 * we still need to provide variables to vdev_to_nvlist_iter(), so
3254 * just point them to junk variables here.
3255 */
3256 if (!avail_spare)
3257 avail_spare = &__avail_spare;
3258 if (!l2cache)
3259 l2cache = &__l2cache;
3260 if (!log)
3261 log = &__log;
3262
3263 *avail_spare = B_FALSE;
3264 *l2cache = B_FALSE;
3265 if (log != NULL)
3266 *log = B_FALSE;
3267 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log,
3268 return_parent);
3269 fnvlist_free(search);
3270
3271 return (ret);
3272 }
3273
3274 nvlist_t *
zpool_find_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3275 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
3276 boolean_t *l2cache, boolean_t *log)
3277 {
3278 return (__zpool_find_vdev(zhp, path, avail_spare, l2cache, log,
3279 B_FALSE));
3280 }
3281
3282 /* Given a vdev path, return its parent's nvlist */
3283 nvlist_t *
zpool_find_parent_vdev(zpool_handle_t * zhp,const char * path,boolean_t * avail_spare,boolean_t * l2cache,boolean_t * log)3284 zpool_find_parent_vdev(zpool_handle_t *zhp, const char *path,
3285 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
3286 {
3287 return (__zpool_find_vdev(zhp, path, avail_spare, l2cache, log,
3288 B_TRUE));
3289 }
3290
3291 /*
3292 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3293 *
3294 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3295 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3296 * ignore them.
3297 */
3298 static uint64_t
zpool_vdev_path_to_guid_impl(zpool_handle_t * zhp,const char * path,boolean_t * is_spare,boolean_t * is_l2cache,boolean_t * is_log)3299 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
3300 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
3301 {
3302 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
3303 nvlist_t *tgt;
3304
3305 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
3306 &log)) == NULL)
3307 return (0);
3308
3309 if (is_spare != NULL)
3310 *is_spare = spare;
3311 if (is_l2cache != NULL)
3312 *is_l2cache = l2cache;
3313 if (is_log != NULL)
3314 *is_log = log;
3315
3316 return (fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID));
3317 }
3318
3319 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3320 uint64_t
zpool_vdev_path_to_guid(zpool_handle_t * zhp,const char * path)3321 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
3322 {
3323 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
3324 }
3325
3326 /*
3327 * Bring the specified vdev online. The 'flags' parameter is a set of the
3328 * ZFS_ONLINE_* flags.
3329 */
3330 int
zpool_vdev_online(zpool_handle_t * zhp,const char * path,int flags,vdev_state_t * newstate)3331 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
3332 vdev_state_t *newstate)
3333 {
3334 zfs_cmd_t zc = {"\0"};
3335 char errbuf[ERRBUFLEN];
3336 nvlist_t *tgt;
3337 boolean_t avail_spare, l2cache, islog;
3338 libzfs_handle_t *hdl = zhp->zpool_hdl;
3339
3340 if (flags & ZFS_ONLINE_EXPAND) {
3341 (void) snprintf(errbuf, sizeof (errbuf),
3342 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
3343 } else {
3344 (void) snprintf(errbuf, sizeof (errbuf),
3345 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
3346 }
3347
3348 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3349 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3350 &islog)) == NULL)
3351 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3352
3353 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3354
3355 if (!(flags & ZFS_ONLINE_SPARE) && avail_spare)
3356 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3357
3358 #ifndef __FreeBSD__
3359 const char *pathname;
3360 if ((flags & ZFS_ONLINE_EXPAND ||
3361 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
3362 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
3363 uint64_t wholedisk = 0;
3364
3365 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
3366 &wholedisk);
3367
3368 /*
3369 * XXX - L2ARC 1.0 devices can't support expansion.
3370 */
3371 if (l2cache) {
3372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3373 "cannot expand cache devices"));
3374 return (zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf));
3375 }
3376
3377 if (wholedisk) {
3378 const char *fullpath = path;
3379 char buf[MAXPATHLEN];
3380 int error;
3381
3382 if (path[0] != '/') {
3383 error = zfs_resolve_shortname(path, buf,
3384 sizeof (buf));
3385 if (error != 0)
3386 return (zfs_error(hdl, EZFS_NODEVICE,
3387 errbuf));
3388
3389 fullpath = buf;
3390 }
3391
3392 error = zpool_relabel_disk(hdl, fullpath, errbuf);
3393 if (error != 0)
3394 return (error);
3395 }
3396 }
3397 #endif
3398
3399 zc.zc_cookie = VDEV_STATE_ONLINE;
3400 zc.zc_obj = flags;
3401
3402 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
3403 if (errno == EINVAL) {
3404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3405 "from this pool into a new one. Use '%s' "
3406 "instead"), "zpool detach");
3407 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, errbuf));
3408 }
3409 return (zpool_standard_error(hdl, errno, errbuf));
3410 }
3411
3412 *newstate = zc.zc_cookie;
3413 return (0);
3414 }
3415
3416 /*
3417 * Take the specified vdev offline
3418 */
3419 int
zpool_vdev_offline(zpool_handle_t * zhp,const char * path,boolean_t istmp)3420 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3421 {
3422 zfs_cmd_t zc = {"\0"};
3423 char errbuf[ERRBUFLEN];
3424 nvlist_t *tgt;
3425 boolean_t avail_spare, l2cache;
3426 libzfs_handle_t *hdl = zhp->zpool_hdl;
3427
3428 (void) snprintf(errbuf, sizeof (errbuf),
3429 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3430
3431 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3432 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3433 NULL)) == NULL)
3434 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3435
3436 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3437
3438 if (avail_spare)
3439 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3440
3441 zc.zc_cookie = VDEV_STATE_OFFLINE;
3442 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3443
3444 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3445 return (0);
3446
3447 switch (errno) {
3448 case EBUSY:
3449
3450 /*
3451 * There are no other replicas of this device.
3452 */
3453 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
3454
3455 case EEXIST:
3456 /*
3457 * The log device has unplayed logs
3458 */
3459 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, errbuf));
3460
3461 default:
3462 return (zpool_standard_error(hdl, errno, errbuf));
3463 }
3464 }
3465
3466 /*
3467 * Remove the specified vdev asynchronously from the configuration, so
3468 * that it may come ONLINE if reinserted. This is called from zed on
3469 * Udev remove event.
3470 * Note: We also have a similar function zpool_vdev_remove() that
3471 * removes the vdev from the pool.
3472 */
3473 int
zpool_vdev_remove_wanted(zpool_handle_t * zhp,const char * path)3474 zpool_vdev_remove_wanted(zpool_handle_t *zhp, const char *path)
3475 {
3476 zfs_cmd_t zc = {"\0"};
3477 char errbuf[ERRBUFLEN];
3478 nvlist_t *tgt;
3479 boolean_t avail_spare, l2cache;
3480 libzfs_handle_t *hdl = zhp->zpool_hdl;
3481
3482 (void) snprintf(errbuf, sizeof (errbuf),
3483 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3484
3485 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3486 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3487 NULL)) == NULL)
3488 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3489
3490 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3491
3492 zc.zc_cookie = VDEV_STATE_REMOVED;
3493
3494 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3495 return (0);
3496
3497 return (zpool_standard_error(hdl, errno, errbuf));
3498 }
3499
3500 /*
3501 * Mark the given vdev faulted.
3502 */
3503 int
zpool_vdev_fault(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3504 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3505 {
3506 zfs_cmd_t zc = {"\0"};
3507 char errbuf[ERRBUFLEN];
3508 libzfs_handle_t *hdl = zhp->zpool_hdl;
3509
3510 (void) snprintf(errbuf, sizeof (errbuf),
3511 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3512
3513 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3514 zc.zc_guid = guid;
3515 zc.zc_cookie = VDEV_STATE_FAULTED;
3516 zc.zc_obj = aux;
3517
3518 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3519 return (0);
3520
3521 switch (errno) {
3522 case EBUSY:
3523
3524 /*
3525 * There are no other replicas of this device.
3526 */
3527 return (zfs_error(hdl, EZFS_NOREPLICAS, errbuf));
3528
3529 default:
3530 return (zpool_standard_error(hdl, errno, errbuf));
3531 }
3532
3533 }
3534
3535 /*
3536 * Generic set vdev state function
3537 */
3538 static int
zpool_vdev_set_state(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux,vdev_state_t state)3539 zpool_vdev_set_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux,
3540 vdev_state_t state)
3541 {
3542 zfs_cmd_t zc = {"\0"};
3543 char errbuf[ERRBUFLEN];
3544 libzfs_handle_t *hdl = zhp->zpool_hdl;
3545
3546 (void) snprintf(errbuf, sizeof (errbuf),
3547 dgettext(TEXT_DOMAIN, "cannot set %s %llu"),
3548 zpool_state_to_name(state, aux), (u_longlong_t)guid);
3549
3550 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3551 zc.zc_guid = guid;
3552 zc.zc_cookie = state;
3553 zc.zc_obj = aux;
3554
3555 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3556 return (0);
3557
3558 return (zpool_standard_error(hdl, errno, errbuf));
3559 }
3560
3561 /*
3562 * Mark the given vdev degraded.
3563 */
3564 int
zpool_vdev_degrade(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3565 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3566 {
3567 return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_DEGRADED));
3568 }
3569
3570 /*
3571 * Mark the given vdev as in a removed state (as if the device does not exist).
3572 *
3573 * This is different than zpool_vdev_remove() which does a removal of a device
3574 * from the pool (but the device does exist).
3575 */
3576 int
zpool_vdev_set_removed_state(zpool_handle_t * zhp,uint64_t guid,vdev_aux_t aux)3577 zpool_vdev_set_removed_state(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3578 {
3579 return (zpool_vdev_set_state(zhp, guid, aux, VDEV_STATE_REMOVED));
3580 }
3581
3582 /*
3583 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3584 * a hot spare.
3585 */
3586 static boolean_t
is_replacing_spare(nvlist_t * search,nvlist_t * tgt,int which)3587 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3588 {
3589 nvlist_t **child;
3590 uint_t c, children;
3591
3592 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3593 &children) == 0) {
3594 const char *type = fnvlist_lookup_string(search,
3595 ZPOOL_CONFIG_TYPE);
3596 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
3597 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
3598 children == 2 && child[which] == tgt)
3599 return (B_TRUE);
3600
3601 for (c = 0; c < children; c++)
3602 if (is_replacing_spare(child[c], tgt, which))
3603 return (B_TRUE);
3604 }
3605
3606 return (B_FALSE);
3607 }
3608
3609 /*
3610 * Attach new_disk (fully described by nvroot) to old_disk.
3611 * If 'replacing' is specified, the new disk will replace the old one.
3612 */
3613 int
zpool_vdev_attach(zpool_handle_t * zhp,const char * old_disk,const char * new_disk,nvlist_t * nvroot,int replacing,boolean_t rebuild)3614 zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3615 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
3616 {
3617 zfs_cmd_t zc = {"\0"};
3618 char errbuf[ERRBUFLEN];
3619 int ret;
3620 nvlist_t *tgt;
3621 boolean_t avail_spare, l2cache, islog;
3622 uint64_t val;
3623 char *newname;
3624 const char *type;
3625 nvlist_t **child;
3626 uint_t children;
3627 nvlist_t *config_root;
3628 libzfs_handle_t *hdl = zhp->zpool_hdl;
3629
3630 if (replacing)
3631 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3632 "cannot replace %s with %s"), old_disk, new_disk);
3633 else
3634 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3635 "cannot attach %s to %s"), new_disk, old_disk);
3636
3637 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3638 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3639 &islog)) == NULL)
3640 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3641
3642 if (avail_spare)
3643 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3644
3645 if (l2cache)
3646 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
3647
3648 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3649 zc.zc_cookie = replacing;
3650 zc.zc_simple = rebuild;
3651
3652 if (rebuild &&
3653 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3655 "the loaded zfs module doesn't support device rebuilds"));
3656 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3657 }
3658
3659 type = fnvlist_lookup_string(tgt, ZPOOL_CONFIG_TYPE);
3660 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 &&
3661 zfeature_lookup_guid("org.openzfs:raidz_expansion", NULL) != 0) {
3662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3663 "the loaded zfs module doesn't support raidz expansion"));
3664 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3665 }
3666
3667 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3668 &child, &children) != 0 || children != 1) {
3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3670 "new device must be a single disk"));
3671 return (zfs_error(hdl, EZFS_INVALCONFIG, errbuf));
3672 }
3673
3674 config_root = fnvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3675 ZPOOL_CONFIG_VDEV_TREE);
3676
3677 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3678 return (-1);
3679
3680 /*
3681 * If the target is a hot spare that has been swapped in, we can only
3682 * replace it with another hot spare.
3683 */
3684 if (replacing &&
3685 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3686 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3687 NULL) == NULL || !avail_spare) &&
3688 is_replacing_spare(config_root, tgt, 1)) {
3689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3690 "can only be replaced by another hot spare"));
3691 free(newname);
3692 return (zfs_error(hdl, EZFS_BADTARGET, errbuf));
3693 }
3694
3695 free(newname);
3696
3697 zcmd_write_conf_nvlist(hdl, &zc, nvroot);
3698
3699 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3700
3701 zcmd_free_nvlists(&zc);
3702
3703 if (ret == 0)
3704 return (0);
3705
3706 switch (errno) {
3707 case ENOTSUP:
3708 /*
3709 * Can't attach to or replace this type of vdev.
3710 */
3711 if (replacing) {
3712 uint64_t version = zpool_get_prop_int(zhp,
3713 ZPOOL_PROP_VERSION, NULL);
3714
3715 if (islog) {
3716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3717 "cannot replace a log with a spare"));
3718 } else if (rebuild) {
3719 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3720 "only mirror and dRAID vdevs support "
3721 "sequential reconstruction"));
3722 } else if (zpool_is_draid_spare(new_disk)) {
3723 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3724 "dRAID spares can only replace child "
3725 "devices in their parent's dRAID vdev"));
3726 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
3727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3728 "already in replacing/spare config; wait "
3729 "for completion or use 'zpool detach'"));
3730 } else {
3731 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3732 "cannot replace a replacing device"));
3733 }
3734 } else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3736 "raidz_expansion feature must be enabled "
3737 "in order to attach a device to raidz"));
3738 } else {
3739 char status[64] = {0};
3740 zpool_prop_get_feature(zhp,
3741 "feature@device_rebuild", status, 63);
3742 if (rebuild &&
3743 strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) {
3744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3745 "device_rebuild feature must be enabled "
3746 "in order to use sequential "
3747 "reconstruction"));
3748 } else {
3749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3750 "can only attach to mirrors and top-level "
3751 "disks"));
3752 }
3753 }
3754 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3755 break;
3756
3757 case EINVAL:
3758 /*
3759 * The new device must be a single disk.
3760 */
3761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3762 "new device must be a single disk"));
3763 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3764 break;
3765
3766 case EBUSY:
3767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
3768 new_disk);
3769 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3770 break;
3771
3772 case EOVERFLOW:
3773 /*
3774 * The new device is too small.
3775 */
3776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3777 "device is too small"));
3778 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3779 break;
3780
3781 case EDOM:
3782 /*
3783 * The new device has a different optimal sector size.
3784 */
3785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3786 "new device has a different optimal sector size; use the "
3787 "option '-o ashift=N' to override the optimal size"));
3788 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3789 break;
3790
3791 case ENAMETOOLONG:
3792 /*
3793 * The resulting top-level vdev spec won't fit in the label.
3794 */
3795 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3796 break;
3797
3798 case ENXIO:
3799 /*
3800 * The existing raidz vdev has offline children
3801 */
3802 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3804 "raidz vdev has devices that are are offline or "
3805 "being replaced"));
3806 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3807 break;
3808 } else {
3809 (void) zpool_standard_error(hdl, errno, errbuf);
3810 }
3811 break;
3812
3813 case EADDRINUSE:
3814 /*
3815 * The boot reserved area is already being used (FreeBSD)
3816 */
3817 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
3818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3819 "the reserved boot area needed for the expansion "
3820 "is already being used by a boot loader"));
3821 (void) zfs_error(hdl, EZFS_BADDEV, errbuf);
3822 } else {
3823 (void) zpool_standard_error(hdl, errno, errbuf);
3824 }
3825 break;
3826
3827 case ZFS_ERR_ASHIFT_MISMATCH:
3828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3829 "The new device cannot have a higher alignment requirement "
3830 "than the top-level vdev."));
3831 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3832 break;
3833 default:
3834 (void) zpool_standard_error(hdl, errno, errbuf);
3835 }
3836
3837 return (-1);
3838 }
3839
3840 /*
3841 * Detach the specified device.
3842 */
3843 int
zpool_vdev_detach(zpool_handle_t * zhp,const char * path)3844 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3845 {
3846 zfs_cmd_t zc = {"\0"};
3847 char errbuf[ERRBUFLEN];
3848 nvlist_t *tgt;
3849 boolean_t avail_spare, l2cache;
3850 libzfs_handle_t *hdl = zhp->zpool_hdl;
3851
3852 (void) snprintf(errbuf, sizeof (errbuf),
3853 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3854
3855 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3856 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3857 NULL)) == NULL)
3858 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
3859
3860 if (avail_spare)
3861 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
3862
3863 if (l2cache)
3864 return (zfs_error(hdl, EZFS_ISL2CACHE, errbuf));
3865
3866 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3867
3868 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3869 return (0);
3870
3871 switch (errno) {
3872
3873 case ENOTSUP:
3874 /*
3875 * Can't detach from this type of vdev.
3876 */
3877 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3878 "applicable to mirror and replacing vdevs"));
3879 (void) zfs_error(hdl, EZFS_BADTARGET, errbuf);
3880 break;
3881
3882 case EBUSY:
3883 /*
3884 * There are no other replicas of this device.
3885 */
3886 (void) zfs_error(hdl, EZFS_NOREPLICAS, errbuf);
3887 break;
3888
3889 default:
3890 (void) zpool_standard_error(hdl, errno, errbuf);
3891 }
3892
3893 return (-1);
3894 }
3895
3896 /*
3897 * Find a mirror vdev in the source nvlist.
3898 *
3899 * The mchild array contains a list of disks in one of the top-level mirrors
3900 * of the source pool. The schild array contains a list of disks that the
3901 * user specified on the command line. We loop over the mchild array to
3902 * see if any entry in the schild array matches.
3903 *
3904 * If a disk in the mchild array is found in the schild array, we return
3905 * the index of that entry. Otherwise we return -1.
3906 */
3907 static int
find_vdev_entry(zpool_handle_t * zhp,nvlist_t ** mchild,uint_t mchildren,nvlist_t ** schild,uint_t schildren)3908 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3909 nvlist_t **schild, uint_t schildren)
3910 {
3911 uint_t mc;
3912
3913 for (mc = 0; mc < mchildren; mc++) {
3914 uint_t sc;
3915 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3916 mchild[mc], 0);
3917
3918 for (sc = 0; sc < schildren; sc++) {
3919 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3920 schild[sc], 0);
3921 boolean_t result = (strcmp(mpath, spath) == 0);
3922
3923 free(spath);
3924 if (result) {
3925 free(mpath);
3926 return (mc);
3927 }
3928 }
3929
3930 free(mpath);
3931 }
3932
3933 return (-1);
3934 }
3935
3936 /*
3937 * Split a mirror pool. If newroot points to null, then a new nvlist
3938 * is generated and it is the responsibility of the caller to free it.
3939 */
3940 int
zpool_vdev_split(zpool_handle_t * zhp,char * newname,nvlist_t ** newroot,nvlist_t * props,splitflags_t flags)3941 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3942 nvlist_t *props, splitflags_t flags)
3943 {
3944 zfs_cmd_t zc = {"\0"};
3945 char errbuf[ERRBUFLEN];
3946 const char *bias;
3947 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3948 nvlist_t **varray = NULL, *zc_props = NULL;
3949 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3950 libzfs_handle_t *hdl = zhp->zpool_hdl;
3951 uint64_t vers, readonly = B_FALSE;
3952 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3953 int retval = 0;
3954
3955 (void) snprintf(errbuf, sizeof (errbuf),
3956 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3957
3958 if (!zpool_name_valid(hdl, B_FALSE, newname))
3959 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3960
3961 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3962 (void) fprintf(stderr, gettext("Internal error: unable to "
3963 "retrieve pool configuration\n"));
3964 return (-1);
3965 }
3966
3967 tree = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
3968 vers = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION);
3969
3970 if (props) {
3971 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3972 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3973 props, vers, flags, errbuf)) == NULL)
3974 return (-1);
3975 (void) nvlist_lookup_uint64(zc_props,
3976 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3977 if (readonly) {
3978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3979 "property %s can only be set at import time"),
3980 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3981 return (-1);
3982 }
3983 }
3984
3985 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3986 &children) != 0) {
3987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3988 "Source pool is missing vdev tree"));
3989 nvlist_free(zc_props);
3990 return (-1);
3991 }
3992
3993 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3994 vcount = 0;
3995
3996 if (*newroot == NULL ||
3997 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3998 &newchild, &newchildren) != 0)
3999 newchildren = 0;
4000
4001 for (c = 0; c < children; c++) {
4002 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
4003 boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
4004 const char *type;
4005 nvlist_t **mchild, *vdev;
4006 uint_t mchildren;
4007 int entry;
4008
4009 /*
4010 * Unlike cache & spares, slogs are stored in the
4011 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
4012 */
4013 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
4014 &is_log);
4015 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4016 &is_hole);
4017 if (is_log || is_hole) {
4018 /*
4019 * Create a hole vdev and put it in the config.
4020 */
4021 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
4022 goto out;
4023 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
4024 VDEV_TYPE_HOLE) != 0)
4025 goto out;
4026 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
4027 1) != 0)
4028 goto out;
4029 if (lastlog == 0)
4030 lastlog = vcount;
4031 varray[vcount++] = vdev;
4032 continue;
4033 }
4034 lastlog = 0;
4035 type = fnvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE);
4036
4037 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
4038 vdev = child[c];
4039 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
4040 goto out;
4041 continue;
4042 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
4043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4044 "Source pool must be composed only of mirrors\n"));
4045 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4046 goto out;
4047 }
4048
4049 if (nvlist_lookup_string(child[c],
4050 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
4051 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
4052 is_special = B_TRUE;
4053 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
4054 is_dedup = B_TRUE;
4055 }
4056 verify(nvlist_lookup_nvlist_array(child[c],
4057 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
4058
4059 /* find or add an entry for this top-level vdev */
4060 if (newchildren > 0 &&
4061 (entry = find_vdev_entry(zhp, mchild, mchildren,
4062 newchild, newchildren)) >= 0) {
4063 /* We found a disk that the user specified. */
4064 vdev = mchild[entry];
4065 ++found;
4066 } else {
4067 /* User didn't specify a disk for this vdev. */
4068 vdev = mchild[mchildren - 1];
4069 }
4070
4071 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
4072 goto out;
4073
4074 if (flags.dryrun != 0) {
4075 if (is_dedup == B_TRUE) {
4076 if (nvlist_add_string(varray[vcount - 1],
4077 ZPOOL_CONFIG_ALLOCATION_BIAS,
4078 VDEV_ALLOC_BIAS_DEDUP) != 0)
4079 goto out;
4080 } else if (is_special == B_TRUE) {
4081 if (nvlist_add_string(varray[vcount - 1],
4082 ZPOOL_CONFIG_ALLOCATION_BIAS,
4083 VDEV_ALLOC_BIAS_SPECIAL) != 0)
4084 goto out;
4085 }
4086 }
4087 }
4088
4089 /* did we find every disk the user specified? */
4090 if (found != newchildren) {
4091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
4092 "include at most one disk from each mirror"));
4093 retval = zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4094 goto out;
4095 }
4096
4097 /* Prepare the nvlist for populating. */
4098 if (*newroot == NULL) {
4099 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
4100 goto out;
4101 freelist = B_TRUE;
4102 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
4103 VDEV_TYPE_ROOT) != 0)
4104 goto out;
4105 } else {
4106 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
4107 }
4108
4109 /* Add all the children we found */
4110 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
4111 (const nvlist_t **)varray, lastlog == 0 ? vcount : lastlog) != 0)
4112 goto out;
4113
4114 /*
4115 * If we're just doing a dry run, exit now with success.
4116 */
4117 if (flags.dryrun) {
4118 memory_err = B_FALSE;
4119 freelist = B_FALSE;
4120 goto out;
4121 }
4122
4123 /* now build up the config list & call the ioctl */
4124 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
4125 goto out;
4126
4127 if (nvlist_add_nvlist(newconfig,
4128 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
4129 nvlist_add_string(newconfig,
4130 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
4131 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
4132 goto out;
4133
4134 /*
4135 * The new pool is automatically part of the namespace unless we
4136 * explicitly export it.
4137 */
4138 if (!flags.import)
4139 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
4140 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4141 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
4142 zcmd_write_conf_nvlist(hdl, &zc, newconfig);
4143 if (zc_props != NULL)
4144 zcmd_write_src_nvlist(hdl, &zc, zc_props);
4145
4146 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
4147 retval = zpool_standard_error(hdl, errno, errbuf);
4148 goto out;
4149 }
4150
4151 freelist = B_FALSE;
4152 memory_err = B_FALSE;
4153
4154 out:
4155 if (varray != NULL) {
4156 int v;
4157
4158 for (v = 0; v < vcount; v++)
4159 nvlist_free(varray[v]);
4160 free(varray);
4161 }
4162 zcmd_free_nvlists(&zc);
4163 nvlist_free(zc_props);
4164 nvlist_free(newconfig);
4165 if (freelist) {
4166 nvlist_free(*newroot);
4167 *newroot = NULL;
4168 }
4169
4170 if (retval != 0)
4171 return (retval);
4172
4173 if (memory_err)
4174 return (no_memory(hdl));
4175
4176 return (0);
4177 }
4178
4179 /*
4180 * Remove the given device.
4181 */
4182 int
zpool_vdev_remove(zpool_handle_t * zhp,const char * path)4183 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
4184 {
4185 zfs_cmd_t zc = {"\0"};
4186 char errbuf[ERRBUFLEN];
4187 nvlist_t *tgt;
4188 boolean_t avail_spare, l2cache, islog;
4189 libzfs_handle_t *hdl = zhp->zpool_hdl;
4190 uint64_t version;
4191
4192 (void) snprintf(errbuf, sizeof (errbuf),
4193 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
4194
4195 if (zpool_is_draid_spare(path)) {
4196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4197 "dRAID spares cannot be removed"));
4198 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4199 }
4200
4201 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4202 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
4203 &islog)) == NULL)
4204 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4205
4206 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
4207 if (islog && version < SPA_VERSION_HOLES) {
4208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4209 "pool must be upgraded to support log removal"));
4210 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4211 }
4212
4213 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
4214
4215 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
4216 return (0);
4217
4218 switch (errno) {
4219
4220 case EALREADY:
4221 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4222 "removal for this vdev is already in progress."));
4223 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4224 break;
4225
4226 case EINVAL:
4227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4228 "invalid config; all top-level vdevs must "
4229 "have the same sector size and not be raidz."));
4230 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4231 break;
4232
4233 case EBUSY:
4234 if (islog) {
4235 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4236 "Mount encrypted datasets to replay logs."));
4237 } else {
4238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4239 "Pool busy; removal may already be in progress"));
4240 }
4241 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4242 break;
4243
4244 case EACCES:
4245 if (islog) {
4246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4247 "Mount encrypted datasets to replay logs."));
4248 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4249 } else {
4250 (void) zpool_standard_error(hdl, errno, errbuf);
4251 }
4252 break;
4253
4254 default:
4255 (void) zpool_standard_error(hdl, errno, errbuf);
4256 }
4257 return (-1);
4258 }
4259
4260 int
zpool_vdev_remove_cancel(zpool_handle_t * zhp)4261 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
4262 {
4263 zfs_cmd_t zc = {{0}};
4264 char errbuf[ERRBUFLEN];
4265 libzfs_handle_t *hdl = zhp->zpool_hdl;
4266
4267 (void) snprintf(errbuf, sizeof (errbuf),
4268 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
4269
4270 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4271 zc.zc_cookie = 1;
4272
4273 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
4274 return (0);
4275
4276 return (zpool_standard_error(hdl, errno, errbuf));
4277 }
4278
4279 int
zpool_vdev_indirect_size(zpool_handle_t * zhp,const char * path,uint64_t * sizep)4280 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
4281 uint64_t *sizep)
4282 {
4283 char errbuf[ERRBUFLEN];
4284 nvlist_t *tgt;
4285 boolean_t avail_spare, l2cache, islog;
4286 libzfs_handle_t *hdl = zhp->zpool_hdl;
4287
4288 (void) snprintf(errbuf, sizeof (errbuf),
4289 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
4290 path);
4291
4292 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
4293 &islog)) == NULL)
4294 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4295
4296 if (avail_spare || l2cache || islog) {
4297 *sizep = 0;
4298 return (0);
4299 }
4300
4301 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
4302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4303 "indirect size not available"));
4304 return (zfs_error(hdl, EINVAL, errbuf));
4305 }
4306 return (0);
4307 }
4308
4309 /*
4310 * Clear the errors for the pool, or the particular device if specified.
4311 */
4312 int
zpool_clear(zpool_handle_t * zhp,const char * path,nvlist_t * rewindnvl)4313 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
4314 {
4315 zfs_cmd_t zc = {"\0"};
4316 char errbuf[ERRBUFLEN];
4317 nvlist_t *tgt;
4318 zpool_load_policy_t policy;
4319 boolean_t avail_spare, l2cache;
4320 libzfs_handle_t *hdl = zhp->zpool_hdl;
4321 nvlist_t *nvi = NULL;
4322 int error;
4323
4324 if (path)
4325 (void) snprintf(errbuf, sizeof (errbuf),
4326 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4327 path);
4328 else
4329 (void) snprintf(errbuf, sizeof (errbuf),
4330 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4331 zhp->zpool_name);
4332
4333 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4334 if (path) {
4335 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
4336 &l2cache, NULL)) == NULL)
4337 return (zfs_error(hdl, EZFS_NODEVICE, errbuf));
4338
4339 /*
4340 * Don't allow error clearing for hot spares. Do allow
4341 * error clearing for l2cache devices.
4342 */
4343 if (avail_spare)
4344 return (zfs_error(hdl, EZFS_ISSPARE, errbuf));
4345
4346 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
4347 }
4348
4349 zpool_get_load_policy(rewindnvl, &policy);
4350 zc.zc_cookie = policy.zlp_rewind;
4351
4352 zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2);
4353 zcmd_write_src_nvlist(hdl, &zc, rewindnvl);
4354
4355 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
4356 errno == ENOMEM)
4357 zcmd_expand_dst_nvlist(hdl, &zc);
4358
4359 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
4360 errno != EPERM && errno != EACCES)) {
4361 if (policy.zlp_rewind &
4362 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
4363 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
4364 zpool_rewind_exclaim(hdl, zc.zc_name,
4365 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
4366 nvi);
4367 nvlist_free(nvi);
4368 }
4369 zcmd_free_nvlists(&zc);
4370 return (0);
4371 }
4372
4373 zcmd_free_nvlists(&zc);
4374 return (zpool_standard_error(hdl, errno, errbuf));
4375 }
4376
4377 /*
4378 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4379 */
4380 int
zpool_vdev_clear(zpool_handle_t * zhp,uint64_t guid)4381 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
4382 {
4383 zfs_cmd_t zc = {"\0"};
4384 char errbuf[ERRBUFLEN];
4385 libzfs_handle_t *hdl = zhp->zpool_hdl;
4386
4387 (void) snprintf(errbuf, sizeof (errbuf),
4388 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
4389 (u_longlong_t)guid);
4390
4391 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4392 zc.zc_guid = guid;
4393 zc.zc_cookie = ZPOOL_NO_REWIND;
4394
4395 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
4396 return (0);
4397
4398 return (zpool_standard_error(hdl, errno, errbuf));
4399 }
4400
4401 /*
4402 * Change the GUID for a pool.
4403 *
4404 * Similar to zpool_reguid(), but may take a GUID.
4405 *
4406 * If the guid argument is NULL, then no GUID is passed in the nvlist to the
4407 * ioctl().
4408 */
4409 int
zpool_set_guid(zpool_handle_t * zhp,const uint64_t * guid)4410 zpool_set_guid(zpool_handle_t *zhp, const uint64_t *guid)
4411 {
4412 char errbuf[ERRBUFLEN];
4413 libzfs_handle_t *hdl = zhp->zpool_hdl;
4414 nvlist_t *nvl = NULL;
4415 zfs_cmd_t zc = {"\0"};
4416 int error;
4417
4418 if (guid != NULL) {
4419 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
4420 return (no_memory(hdl));
4421
4422 if (nvlist_add_uint64(nvl, ZPOOL_REGUID_GUID, *guid) != 0) {
4423 nvlist_free(nvl);
4424 return (no_memory(hdl));
4425 }
4426
4427 zcmd_write_src_nvlist(hdl, &zc, nvl);
4428 }
4429
4430 (void) snprintf(errbuf, sizeof (errbuf),
4431 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
4432
4433 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4434 error = zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc);
4435 if (error) {
4436 return (zpool_standard_error(hdl, errno, errbuf));
4437 }
4438 if (guid != NULL) {
4439 zcmd_free_nvlists(&zc);
4440 nvlist_free(nvl);
4441 }
4442 return (0);
4443 }
4444
4445 /*
4446 * Change the GUID for a pool.
4447 */
4448 int
zpool_reguid(zpool_handle_t * zhp)4449 zpool_reguid(zpool_handle_t *zhp)
4450 {
4451 return (zpool_set_guid(zhp, NULL));
4452 }
4453
4454 /*
4455 * Reopen the pool.
4456 */
4457 int
zpool_reopen_one(zpool_handle_t * zhp,void * data)4458 zpool_reopen_one(zpool_handle_t *zhp, void *data)
4459 {
4460 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4461 const char *pool_name = zpool_get_name(zhp);
4462 boolean_t *scrub_restart = data;
4463 int error;
4464
4465 error = lzc_reopen(pool_name, *scrub_restart);
4466 if (error) {
4467 return (zpool_standard_error_fmt(hdl, error,
4468 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
4469 }
4470
4471 return (0);
4472 }
4473
4474 /* call into libzfs_core to execute the sync IOCTL per pool */
4475 int
zpool_sync_one(zpool_handle_t * zhp,void * data)4476 zpool_sync_one(zpool_handle_t *zhp, void *data)
4477 {
4478 int ret;
4479 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4480 const char *pool_name = zpool_get_name(zhp);
4481 boolean_t *force = data;
4482 nvlist_t *innvl = fnvlist_alloc();
4483
4484 fnvlist_add_boolean_value(innvl, "force", *force);
4485 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
4486 nvlist_free(innvl);
4487 return (zpool_standard_error_fmt(hdl, ret,
4488 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
4489 }
4490 nvlist_free(innvl);
4491
4492 return (0);
4493 }
4494
4495 #define PATH_BUF_LEN 64
4496
4497 /*
4498 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4499 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4500 * We also check if this is a whole disk, in which case we strip off the
4501 * trailing 's0' slice name.
4502 *
4503 * This routine is also responsible for identifying when disks have been
4504 * reconfigured in a new location. The kernel will have opened the device by
4505 * devid, but the path will still refer to the old location. To catch this, we
4506 * first do a path -> devid translation (which is fast for the common case). If
4507 * the devid matches, we're done. If not, we do a reverse devid -> path
4508 * translation and issue the appropriate ioctl() to update the path of the vdev.
4509 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4510 * of these checks.
4511 */
4512 char *
zpool_vdev_name(libzfs_handle_t * hdl,zpool_handle_t * zhp,nvlist_t * nv,int name_flags)4513 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
4514 int name_flags)
4515 {
4516 const char *type, *tpath;
4517 const char *path;
4518 uint64_t value;
4519 char buf[PATH_BUF_LEN];
4520 char tmpbuf[PATH_BUF_LEN * 2];
4521
4522 /*
4523 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4524 * zpool name that will be displayed to the user.
4525 */
4526 type = fnvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE);
4527 if (zhp != NULL && strcmp(type, "root") == 0)
4528 return (zfs_strdup(hdl, zpool_get_name(zhp)));
4529
4530 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4531 name_flags |= VDEV_NAME_PATH;
4532 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4533 name_flags |= VDEV_NAME_GUID;
4534 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4535 name_flags |= VDEV_NAME_FOLLOW_LINKS;
4536
4537 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4538 name_flags & VDEV_NAME_GUID) {
4539 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
4540 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
4541 path = buf;
4542 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tpath) == 0) {
4543 path = tpath;
4544
4545 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4546 char *rp = realpath(path, NULL);
4547 if (rp) {
4548 strlcpy(buf, rp, sizeof (buf));
4549 path = buf;
4550 free(rp);
4551 }
4552 }
4553
4554 /*
4555 * For a block device only use the name.
4556 */
4557 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4558 !(name_flags & VDEV_NAME_PATH)) {
4559 path = zfs_strip_path(path);
4560 }
4561
4562 /*
4563 * Remove the partition from the path if this is a whole disk.
4564 */
4565 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
4566 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4567 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4568 return (zfs_strip_partition(path));
4569 }
4570 } else {
4571 path = type;
4572
4573 /*
4574 * If it's a raidz device, we need to stick in the parity level.
4575 */
4576 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4577 value = fnvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY);
4578 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
4579 (u_longlong_t)value);
4580 path = buf;
4581 }
4582
4583 /*
4584 * If it's a dRAID device, we add parity, groups, and spares.
4585 */
4586 if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
4587 uint64_t ndata, nparity, nspares;
4588 nvlist_t **child;
4589 uint_t children;
4590
4591 verify(nvlist_lookup_nvlist_array(nv,
4592 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
4593 nparity = fnvlist_lookup_uint64(nv,
4594 ZPOOL_CONFIG_NPARITY);
4595 ndata = fnvlist_lookup_uint64(nv,
4596 ZPOOL_CONFIG_DRAID_NDATA);
4597 nspares = fnvlist_lookup_uint64(nv,
4598 ZPOOL_CONFIG_DRAID_NSPARES);
4599
4600 path = zpool_draid_name(buf, sizeof (buf), ndata,
4601 nparity, nspares, children);
4602 }
4603
4604 /*
4605 * We identify each top-level vdev by using a <type-id>
4606 * naming convention.
4607 */
4608 if (name_flags & VDEV_NAME_TYPE_ID) {
4609 uint64_t id = fnvlist_lookup_uint64(nv,
4610 ZPOOL_CONFIG_ID);
4611 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4612 path, (u_longlong_t)id);
4613 path = tmpbuf;
4614 }
4615 }
4616
4617 return (zfs_strdup(hdl, path));
4618 }
4619
4620 static int
zbookmark_mem_compare(const void * a,const void * b)4621 zbookmark_mem_compare(const void *a, const void *b)
4622 {
4623 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4624 }
4625
4626 void
zpool_add_propname(zpool_handle_t * zhp,const char * propname)4627 zpool_add_propname(zpool_handle_t *zhp, const char *propname)
4628 {
4629 assert(zhp->zpool_n_propnames < ZHP_MAX_PROPNAMES);
4630 zhp->zpool_propnames[zhp->zpool_n_propnames] = propname;
4631 zhp->zpool_n_propnames++;
4632 }
4633
4634 /*
4635 * Retrieve the persistent error log, uniquify the members, and return to the
4636 * caller.
4637 */
4638 int
zpool_get_errlog(zpool_handle_t * zhp,nvlist_t ** nverrlistp)4639 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4640 {
4641 zfs_cmd_t zc = {"\0"};
4642 libzfs_handle_t *hdl = zhp->zpool_hdl;
4643 zbookmark_phys_t *buf;
4644 uint64_t buflen = 10000; /* approx. 1MB of RAM */
4645
4646 if (fnvlist_lookup_uint64(zhp->zpool_config,
4647 ZPOOL_CONFIG_ERRCOUNT) == 0)
4648 return (0);
4649
4650 /*
4651 * Retrieve the raw error list from the kernel. If it doesn't fit,
4652 * allocate a larger buffer and retry.
4653 */
4654 (void) strcpy(zc.zc_name, zhp->zpool_name);
4655 for (;;) {
4656 buf = zfs_alloc(zhp->zpool_hdl,
4657 buflen * sizeof (zbookmark_phys_t));
4658 zc.zc_nvlist_dst = (uintptr_t)buf;
4659 zc.zc_nvlist_dst_size = buflen;
4660 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
4661 &zc) != 0) {
4662 free(buf);
4663 if (errno == ENOMEM) {
4664 buflen *= 2;
4665 } else {
4666 return (zpool_standard_error_fmt(hdl, errno,
4667 dgettext(TEXT_DOMAIN, "errors: List of "
4668 "errors unavailable")));
4669 }
4670 } else {
4671 break;
4672 }
4673 }
4674
4675 /*
4676 * Sort the resulting bookmarks. This is a little confusing due to the
4677 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4678 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4679 * _not_ copied as part of the process. So we point the start of our
4680 * array appropriate and decrement the total number of elements.
4681 */
4682 zbookmark_phys_t *zb = buf + zc.zc_nvlist_dst_size;
4683 uint64_t zblen = buflen - zc.zc_nvlist_dst_size;
4684
4685 qsort(zb, zblen, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4686
4687 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4688
4689 /*
4690 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4691 */
4692 for (uint64_t i = 0; i < zblen; i++) {
4693 nvlist_t *nv;
4694
4695 /* ignoring zb_blkid and zb_level for now */
4696 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4697 zb[i-1].zb_object == zb[i].zb_object)
4698 continue;
4699
4700 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4701 goto nomem;
4702 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4703 zb[i].zb_objset) != 0) {
4704 nvlist_free(nv);
4705 goto nomem;
4706 }
4707 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4708 zb[i].zb_object) != 0) {
4709 nvlist_free(nv);
4710 goto nomem;
4711 }
4712 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4713 nvlist_free(nv);
4714 goto nomem;
4715 }
4716 nvlist_free(nv);
4717 }
4718
4719 free(buf);
4720 return (0);
4721
4722 nomem:
4723 free(buf);
4724 return (no_memory(zhp->zpool_hdl));
4725 }
4726
4727 /*
4728 * Upgrade a ZFS pool to the latest on-disk version.
4729 */
4730 int
zpool_upgrade(zpool_handle_t * zhp,uint64_t new_version)4731 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4732 {
4733 zfs_cmd_t zc = {"\0"};
4734 libzfs_handle_t *hdl = zhp->zpool_hdl;
4735
4736 (void) strcpy(zc.zc_name, zhp->zpool_name);
4737 zc.zc_cookie = new_version;
4738
4739 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4740 return (zpool_standard_error_fmt(hdl, errno,
4741 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4742 zhp->zpool_name));
4743 return (0);
4744 }
4745
4746 void
zfs_save_arguments(int argc,char ** argv,char * string,int len)4747 zfs_save_arguments(int argc, char **argv, char *string, int len)
4748 {
4749 int i;
4750
4751 (void) strlcpy(string, zfs_basename(argv[0]), len);
4752 for (i = 1; i < argc; i++) {
4753 (void) strlcat(string, " ", len);
4754 (void) strlcat(string, argv[i], len);
4755 }
4756 }
4757
4758 int
zpool_log_history(libzfs_handle_t * hdl,const char * message)4759 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4760 {
4761 zfs_cmd_t zc = {"\0"};
4762 nvlist_t *args;
4763
4764 args = fnvlist_alloc();
4765 fnvlist_add_string(args, "message", message);
4766 zcmd_write_src_nvlist(hdl, &zc, args);
4767 int err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4768 nvlist_free(args);
4769 zcmd_free_nvlists(&zc);
4770 return (err);
4771 }
4772
4773 /*
4774 * Perform ioctl to get some command history of a pool.
4775 *
4776 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4777 * logical offset of the history buffer to start reading from.
4778 *
4779 * Upon return, 'off' is the next logical offset to read from and
4780 * 'len' is the actual amount of bytes read into 'buf'.
4781 */
4782 static int
get_history(zpool_handle_t * zhp,char * buf,uint64_t * off,uint64_t * len)4783 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4784 {
4785 zfs_cmd_t zc = {"\0"};
4786 libzfs_handle_t *hdl = zhp->zpool_hdl;
4787
4788 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4789
4790 zc.zc_history = (uint64_t)(uintptr_t)buf;
4791 zc.zc_history_len = *len;
4792 zc.zc_history_offset = *off;
4793
4794 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4795 switch (errno) {
4796 case EPERM:
4797 return (zfs_error_fmt(hdl, EZFS_PERM,
4798 dgettext(TEXT_DOMAIN,
4799 "cannot show history for pool '%s'"),
4800 zhp->zpool_name));
4801 case ENOENT:
4802 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4803 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4804 "'%s'"), zhp->zpool_name));
4805 case ENOTSUP:
4806 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4807 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4808 "'%s', pool must be upgraded"), zhp->zpool_name));
4809 default:
4810 return (zpool_standard_error_fmt(hdl, errno,
4811 dgettext(TEXT_DOMAIN,
4812 "cannot get history for '%s'"), zhp->zpool_name));
4813 }
4814 }
4815
4816 *len = zc.zc_history_len;
4817 *off = zc.zc_history_offset;
4818
4819 return (0);
4820 }
4821
4822 /*
4823 * Retrieve the command history of a pool.
4824 */
4825 int
zpool_get_history(zpool_handle_t * zhp,nvlist_t ** nvhisp,uint64_t * off,boolean_t * eof)4826 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4827 boolean_t *eof)
4828 {
4829 libzfs_handle_t *hdl = zhp->zpool_hdl;
4830 char *buf;
4831 int buflen = 128 * 1024;
4832 nvlist_t **records = NULL;
4833 uint_t numrecords = 0;
4834 int err = 0, i;
4835 uint64_t start = *off;
4836
4837 buf = zfs_alloc(hdl, buflen);
4838
4839 /* process about 1MiB a time */
4840 while (*off - start < 1024 * 1024) {
4841 uint64_t bytes_read = buflen;
4842 uint64_t leftover;
4843
4844 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4845 break;
4846
4847 /* if nothing else was read in, we're at EOF, just return */
4848 if (!bytes_read) {
4849 *eof = B_TRUE;
4850 break;
4851 }
4852
4853 if ((err = zpool_history_unpack(buf, bytes_read,
4854 &leftover, &records, &numrecords)) != 0) {
4855 zpool_standard_error_fmt(hdl, err,
4856 dgettext(TEXT_DOMAIN,
4857 "cannot get history for '%s'"), zhp->zpool_name);
4858 break;
4859 }
4860 *off -= leftover;
4861 if (leftover == bytes_read) {
4862 /*
4863 * no progress made, because buffer is not big enough
4864 * to hold this record; resize and retry.
4865 */
4866 buflen *= 2;
4867 free(buf);
4868 buf = zfs_alloc(hdl, buflen);
4869 }
4870 }
4871
4872 free(buf);
4873
4874 if (!err) {
4875 *nvhisp = fnvlist_alloc();
4876 fnvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4877 (const nvlist_t **)records, numrecords);
4878 }
4879 for (i = 0; i < numrecords; i++)
4880 nvlist_free(records[i]);
4881 free(records);
4882
4883 return (err);
4884 }
4885
4886 /*
4887 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4888 * If there is a new event available 'nvp' will contain a newly allocated
4889 * nvlist and 'dropped' will be set to the number of missed events since
4890 * the last call to this function. When 'nvp' is set to NULL it indicates
4891 * no new events are available. In either case the function returns 0 and
4892 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4893 * function will return a non-zero value. When the function is called in
4894 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4895 * it will not return until a new event is available.
4896 */
4897 int
zpool_events_next(libzfs_handle_t * hdl,nvlist_t ** nvp,int * dropped,unsigned flags,int zevent_fd)4898 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4899 int *dropped, unsigned flags, int zevent_fd)
4900 {
4901 zfs_cmd_t zc = {"\0"};
4902 int error = 0;
4903
4904 *nvp = NULL;
4905 *dropped = 0;
4906 zc.zc_cleanup_fd = zevent_fd;
4907
4908 if (flags & ZEVENT_NONBLOCK)
4909 zc.zc_guid = ZEVENT_NONBLOCK;
4910
4911 zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE);
4912
4913 retry:
4914 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4915 switch (errno) {
4916 case ESHUTDOWN:
4917 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4918 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4919 goto out;
4920 case ENOENT:
4921 /* Blocking error case should not occur */
4922 if (!(flags & ZEVENT_NONBLOCK))
4923 error = zpool_standard_error_fmt(hdl, errno,
4924 dgettext(TEXT_DOMAIN, "cannot get event"));
4925
4926 goto out;
4927 case ENOMEM:
4928 zcmd_expand_dst_nvlist(hdl, &zc);
4929 goto retry;
4930 default:
4931 error = zpool_standard_error_fmt(hdl, errno,
4932 dgettext(TEXT_DOMAIN, "cannot get event"));
4933 goto out;
4934 }
4935 }
4936
4937 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4938 if (error != 0)
4939 goto out;
4940
4941 *dropped = (int)zc.zc_cookie;
4942 out:
4943 zcmd_free_nvlists(&zc);
4944
4945 return (error);
4946 }
4947
4948 /*
4949 * Clear all events.
4950 */
4951 int
zpool_events_clear(libzfs_handle_t * hdl,int * count)4952 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4953 {
4954 zfs_cmd_t zc = {"\0"};
4955
4956 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4957 return (zpool_standard_error(hdl, errno,
4958 dgettext(TEXT_DOMAIN, "cannot clear events")));
4959
4960 if (count != NULL)
4961 *count = (int)zc.zc_cookie; /* # of events cleared */
4962
4963 return (0);
4964 }
4965
4966 /*
4967 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4968 * the passed zevent_fd file handle. On success zero is returned,
4969 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4970 */
4971 int
zpool_events_seek(libzfs_handle_t * hdl,uint64_t eid,int zevent_fd)4972 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4973 {
4974 zfs_cmd_t zc = {"\0"};
4975 int error = 0;
4976
4977 zc.zc_guid = eid;
4978 zc.zc_cleanup_fd = zevent_fd;
4979
4980 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4981 switch (errno) {
4982 case ENOENT:
4983 error = zfs_error_fmt(hdl, EZFS_NOENT,
4984 dgettext(TEXT_DOMAIN, "cannot get event"));
4985 break;
4986
4987 case ENOMEM:
4988 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4989 dgettext(TEXT_DOMAIN, "cannot get event"));
4990 break;
4991
4992 default:
4993 error = zpool_standard_error_fmt(hdl, errno,
4994 dgettext(TEXT_DOMAIN, "cannot get event"));
4995 break;
4996 }
4997 }
4998
4999 return (error);
5000 }
5001
5002 static void
zpool_obj_to_path_impl(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len,boolean_t always_unmounted)5003 zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
5004 char *pathname, size_t len, boolean_t always_unmounted)
5005 {
5006 zfs_cmd_t zc = {"\0"};
5007 boolean_t mounted = B_FALSE;
5008 char *mntpnt = NULL;
5009 char dsname[ZFS_MAX_DATASET_NAME_LEN];
5010
5011 if (dsobj == 0) {
5012 /* special case for the MOS */
5013 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
5014 (longlong_t)obj);
5015 return;
5016 }
5017
5018 /* get the dataset's name */
5019 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
5020 zc.zc_obj = dsobj;
5021 if (zfs_ioctl(zhp->zpool_hdl,
5022 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
5023 /* just write out a path of two object numbers */
5024 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
5025 (longlong_t)dsobj, (longlong_t)obj);
5026 return;
5027 }
5028 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
5029
5030 /* find out if the dataset is mounted */
5031 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
5032 &mntpnt);
5033
5034 /* get the corrupted object's path */
5035 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
5036 zc.zc_obj = obj;
5037 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
5038 &zc) == 0) {
5039 if (mounted) {
5040 (void) snprintf(pathname, len, "%s%s", mntpnt,
5041 zc.zc_value);
5042 } else {
5043 (void) snprintf(pathname, len, "%s:%s",
5044 dsname, zc.zc_value);
5045 }
5046 } else {
5047 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
5048 (longlong_t)obj);
5049 }
5050 free(mntpnt);
5051 }
5052
5053 void
zpool_obj_to_path(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)5054 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
5055 char *pathname, size_t len)
5056 {
5057 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
5058 }
5059
5060 void
zpool_obj_to_path_ds(zpool_handle_t * zhp,uint64_t dsobj,uint64_t obj,char * pathname,size_t len)5061 zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
5062 char *pathname, size_t len)
5063 {
5064 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
5065 }
5066 /*
5067 * Wait while the specified activity is in progress in the pool.
5068 */
5069 int
zpool_wait(zpool_handle_t * zhp,zpool_wait_activity_t activity)5070 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
5071 {
5072 boolean_t missing;
5073
5074 int error = zpool_wait_status(zhp, activity, &missing, NULL);
5075
5076 if (missing) {
5077 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
5078 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
5079 zhp->zpool_name);
5080 return (ENOENT);
5081 } else {
5082 return (error);
5083 }
5084 }
5085
5086 /*
5087 * Wait for the given activity and return the status of the wait (whether or not
5088 * any waiting was done) in the 'waited' parameter. Non-existent pools are
5089 * reported via the 'missing' parameter, rather than by printing an error
5090 * message. This is convenient when this function is called in a loop over a
5091 * long period of time (as it is, for example, by zpool's wait cmd). In that
5092 * scenario, a pool being exported or destroyed should be considered a normal
5093 * event, so we don't want to print an error when we find that the pool doesn't
5094 * exist.
5095 */
5096 int
zpool_wait_status(zpool_handle_t * zhp,zpool_wait_activity_t activity,boolean_t * missing,boolean_t * waited)5097 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
5098 boolean_t *missing, boolean_t *waited)
5099 {
5100 int error = lzc_wait(zhp->zpool_name, activity, waited);
5101 *missing = (error == ENOENT);
5102 if (*missing)
5103 return (0);
5104
5105 if (error != 0) {
5106 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5107 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
5108 zhp->zpool_name);
5109 }
5110
5111 return (error);
5112 }
5113
5114 int
zpool_set_bootenv(zpool_handle_t * zhp,const nvlist_t * envmap)5115 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
5116 {
5117 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
5118 if (error != 0) {
5119 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5120 dgettext(TEXT_DOMAIN,
5121 "error setting bootenv in pool '%s'"), zhp->zpool_name);
5122 }
5123
5124 return (error);
5125 }
5126
5127 int
zpool_get_bootenv(zpool_handle_t * zhp,nvlist_t ** nvlp)5128 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
5129 {
5130 nvlist_t *nvl;
5131 int error;
5132
5133 nvl = NULL;
5134 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
5135 if (error != 0) {
5136 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
5137 dgettext(TEXT_DOMAIN,
5138 "error getting bootenv in pool '%s'"), zhp->zpool_name);
5139 } else {
5140 *nvlp = nvl;
5141 }
5142
5143 return (error);
5144 }
5145
5146 /*
5147 * Attempt to read and parse feature file(s) (from "compatibility" property).
5148 * Files contain zpool feature names, comma or whitespace-separated.
5149 * Comments (# character to next newline) are discarded.
5150 *
5151 * Arguments:
5152 * compatibility : string containing feature filenames
5153 * features : either NULL or pointer to array of boolean
5154 * report : either NULL or pointer to string buffer
5155 * rlen : length of "report" buffer
5156 *
5157 * compatibility is NULL (unset), "", "off", "legacy", or list of
5158 * comma-separated filenames. filenames should either be absolute,
5159 * or relative to:
5160 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
5161 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
5162 * (Unset), "" or "off" => enable all features
5163 * "legacy" => disable all features
5164 *
5165 * Any feature names read from files which match unames in spa_feature_table
5166 * will have the corresponding boolean set in the features array (if non-NULL).
5167 * If more than one feature set specified, only features present in *all* of
5168 * them will be set.
5169 *
5170 * "report" if not NULL will be populated with a suitable status message.
5171 *
5172 * Return values:
5173 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
5174 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
5175 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
5176 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
5177 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
5178 */
5179 zpool_compat_status_t
zpool_load_compat(const char * compat,boolean_t * features,char * report,size_t rlen)5180 zpool_load_compat(const char *compat, boolean_t *features, char *report,
5181 size_t rlen)
5182 {
5183 int sdirfd, ddirfd, featfd;
5184 struct stat fs;
5185 char *fc;
5186 char *ps, *ls, *ws;
5187 char *file, *line, *word;
5188
5189 char l_compat[ZFS_MAXPROPLEN];
5190
5191 boolean_t ret_nofiles = B_TRUE;
5192 boolean_t ret_badfile = B_FALSE;
5193 boolean_t ret_badtoken = B_FALSE;
5194 boolean_t ret_warntoken = B_FALSE;
5195
5196 /* special cases (unset), "" and "off" => enable all features */
5197 if (compat == NULL || compat[0] == '\0' ||
5198 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
5199 if (features != NULL) {
5200 for (uint_t i = 0; i < SPA_FEATURES; i++)
5201 features[i] = B_TRUE;
5202 }
5203 if (report != NULL)
5204 strlcpy(report, gettext("all features enabled"), rlen);
5205 return (ZPOOL_COMPATIBILITY_OK);
5206 }
5207
5208 /* Final special case "legacy" => disable all features */
5209 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
5210 if (features != NULL)
5211 for (uint_t i = 0; i < SPA_FEATURES; i++)
5212 features[i] = B_FALSE;
5213 if (report != NULL)
5214 strlcpy(report, gettext("all features disabled"), rlen);
5215 return (ZPOOL_COMPATIBILITY_OK);
5216 }
5217
5218 /*
5219 * Start with all true; will be ANDed with results from each file
5220 */
5221 if (features != NULL)
5222 for (uint_t i = 0; i < SPA_FEATURES; i++)
5223 features[i] = B_TRUE;
5224
5225 char err_badfile[ZFS_MAXPROPLEN] = "";
5226 char err_badtoken[ZFS_MAXPROPLEN] = "";
5227
5228 /*
5229 * We ignore errors from the directory open()
5230 * as they're only needed if the filename is relative
5231 * which will be checked during the openat().
5232 */
5233
5234 /* O_PATH safer than O_RDONLY if system allows it */
5235 #if defined(O_PATH)
5236 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
5237 #else
5238 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
5239 #endif
5240
5241 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
5242 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
5243
5244 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
5245
5246 for (file = strtok_r(l_compat, ",", &ps);
5247 file != NULL;
5248 file = strtok_r(NULL, ",", &ps)) {
5249
5250 boolean_t l_features[SPA_FEATURES];
5251
5252 enum { Z_SYSCONF, Z_DATA } source;
5253
5254 /* try sysconfdir first, then datadir */
5255 source = Z_SYSCONF;
5256 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
5257 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
5258 source = Z_DATA;
5259 }
5260
5261 /* File readable and correct size? */
5262 if (featfd < 0 ||
5263 fstat(featfd, &fs) < 0 ||
5264 fs.st_size < 1 ||
5265 fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
5266 (void) close(featfd);
5267 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
5268 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
5269 ret_badfile = B_TRUE;
5270 continue;
5271 }
5272
5273 /* Prefault the file if system allows */
5274 #if defined(MAP_POPULATE)
5275 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
5276 #elif defined(MAP_PREFAULT_READ)
5277 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
5278 #else
5279 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
5280 #endif
5281
5282 /* private mmap() so we can strtok safely */
5283 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
5284 ZC_MMAP_FLAGS, featfd, 0);
5285 (void) close(featfd);
5286
5287 /* map ok, and last character == newline? */
5288 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
5289 (void) munmap((void *) fc, fs.st_size);
5290 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
5291 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
5292 ret_badfile = B_TRUE;
5293 continue;
5294 }
5295
5296 ret_nofiles = B_FALSE;
5297
5298 for (uint_t i = 0; i < SPA_FEATURES; i++)
5299 l_features[i] = B_FALSE;
5300
5301 /* replace final newline with NULL to ensure string ends */
5302 fc[fs.st_size - 1] = '\0';
5303
5304 for (line = strtok_r(fc, "\n", &ls);
5305 line != NULL;
5306 line = strtok_r(NULL, "\n", &ls)) {
5307 /* discard comments */
5308 char *r = strchr(line, '#');
5309 if (r != NULL)
5310 *r = '\0';
5311
5312 for (word = strtok_r(line, ", \t", &ws);
5313 word != NULL;
5314 word = strtok_r(NULL, ", \t", &ws)) {
5315 /* Find matching feature name */
5316 uint_t f;
5317 for (f = 0; f < SPA_FEATURES; f++) {
5318 zfeature_info_t *fi =
5319 &spa_feature_table[f];
5320 if (strcmp(word, fi->fi_uname) == 0) {
5321 l_features[f] = B_TRUE;
5322 break;
5323 }
5324 }
5325 if (f < SPA_FEATURES)
5326 continue;
5327
5328 /* found an unrecognized word */
5329 /* lightly sanitize it */
5330 if (strlen(word) > 32)
5331 word[32] = '\0';
5332 for (char *c = word; *c != '\0'; c++)
5333 if (!isprint(*c))
5334 *c = '?';
5335
5336 strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
5337 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
5338 if (source == Z_SYSCONF)
5339 ret_badtoken = B_TRUE;
5340 else
5341 ret_warntoken = B_TRUE;
5342 }
5343 }
5344 (void) munmap((void *) fc, fs.st_size);
5345
5346 if (features != NULL)
5347 for (uint_t i = 0; i < SPA_FEATURES; i++)
5348 features[i] &= l_features[i];
5349 }
5350 (void) close(sdirfd);
5351 (void) close(ddirfd);
5352
5353 /* Return the most serious error */
5354 if (ret_badfile) {
5355 if (report != NULL)
5356 snprintf(report, rlen, gettext("could not read/"
5357 "parse feature file(s): %s"), err_badfile);
5358 return (ZPOOL_COMPATIBILITY_BADFILE);
5359 }
5360 if (ret_nofiles) {
5361 if (report != NULL)
5362 strlcpy(report,
5363 gettext("no valid compatibility files specified"),
5364 rlen);
5365 return (ZPOOL_COMPATIBILITY_NOFILES);
5366 }
5367 if (ret_badtoken) {
5368 if (report != NULL)
5369 snprintf(report, rlen, gettext("invalid feature "
5370 "name(s) in local compatibility files: %s"),
5371 err_badtoken);
5372 return (ZPOOL_COMPATIBILITY_BADTOKEN);
5373 }
5374 if (ret_warntoken) {
5375 if (report != NULL)
5376 snprintf(report, rlen, gettext("unrecognized feature "
5377 "name(s) in distribution compatibility files: %s"),
5378 err_badtoken);
5379 return (ZPOOL_COMPATIBILITY_WARNTOKEN);
5380 }
5381 if (report != NULL)
5382 strlcpy(report, gettext("compatibility set ok"), rlen);
5383 return (ZPOOL_COMPATIBILITY_OK);
5384 }
5385
5386 static int
zpool_vdev_guid(zpool_handle_t * zhp,const char * vdevname,uint64_t * vdev_guid)5387 zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid)
5388 {
5389 nvlist_t *tgt;
5390 boolean_t avail_spare, l2cache;
5391
5392 verify(zhp != NULL);
5393 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5394 char errbuf[ERRBUFLEN];
5395 (void) snprintf(errbuf, sizeof (errbuf),
5396 dgettext(TEXT_DOMAIN, "pool is in an unavailable state"));
5397 return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf));
5398 }
5399
5400 if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache,
5401 NULL)) == NULL) {
5402 char errbuf[ERRBUFLEN];
5403 (void) snprintf(errbuf, sizeof (errbuf),
5404 dgettext(TEXT_DOMAIN, "can not find %s in %s"),
5405 vdevname, zhp->zpool_name);
5406 return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf));
5407 }
5408
5409 *vdev_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
5410 return (0);
5411 }
5412
5413 /*
5414 * Get a vdev property value for 'prop' and return the value in
5415 * a pre-allocated buffer.
5416 */
5417 int
zpool_get_vdev_prop_value(nvlist_t * nvprop,vdev_prop_t prop,char * prop_name,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)5418 zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name,
5419 char *buf, size_t len, zprop_source_t *srctype, boolean_t literal)
5420 {
5421 nvlist_t *nv;
5422 const char *strval;
5423 uint64_t intval;
5424 zprop_source_t src = ZPROP_SRC_NONE;
5425
5426 if (prop == VDEV_PROP_USERPROP) {
5427 /* user property, prop_name must contain the property name */
5428 assert(prop_name != NULL);
5429 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5430 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5431 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
5432 } else {
5433 /* user prop not found */
5434 src = ZPROP_SRC_DEFAULT;
5435 strval = "-";
5436 }
5437 (void) strlcpy(buf, strval, len);
5438 if (srctype)
5439 *srctype = src;
5440 return (0);
5441 }
5442
5443 if (prop_name == NULL)
5444 prop_name = (char *)vdev_prop_to_name(prop);
5445
5446 switch (vdev_prop_get_type(prop)) {
5447 case PROP_TYPE_STRING:
5448 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5449 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5450 strval = fnvlist_lookup_string(nv, ZPROP_VALUE);
5451 } else {
5452 src = ZPROP_SRC_DEFAULT;
5453 if ((strval = vdev_prop_default_string(prop)) == NULL)
5454 strval = "-";
5455 }
5456 (void) strlcpy(buf, strval, len);
5457 break;
5458
5459 case PROP_TYPE_NUMBER:
5460 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5461 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5462 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
5463 } else {
5464 src = ZPROP_SRC_DEFAULT;
5465 intval = vdev_prop_default_numeric(prop);
5466 }
5467
5468 switch (prop) {
5469 case VDEV_PROP_ASIZE:
5470 case VDEV_PROP_PSIZE:
5471 case VDEV_PROP_SIZE:
5472 case VDEV_PROP_BOOTSIZE:
5473 case VDEV_PROP_ALLOCATED:
5474 case VDEV_PROP_FREE:
5475 case VDEV_PROP_READ_ERRORS:
5476 case VDEV_PROP_WRITE_ERRORS:
5477 case VDEV_PROP_CHECKSUM_ERRORS:
5478 case VDEV_PROP_INITIALIZE_ERRORS:
5479 case VDEV_PROP_TRIM_ERRORS:
5480 case VDEV_PROP_SLOW_IOS:
5481 case VDEV_PROP_OPS_NULL:
5482 case VDEV_PROP_OPS_READ:
5483 case VDEV_PROP_OPS_WRITE:
5484 case VDEV_PROP_OPS_FREE:
5485 case VDEV_PROP_OPS_CLAIM:
5486 case VDEV_PROP_OPS_TRIM:
5487 case VDEV_PROP_BYTES_NULL:
5488 case VDEV_PROP_BYTES_READ:
5489 case VDEV_PROP_BYTES_WRITE:
5490 case VDEV_PROP_BYTES_FREE:
5491 case VDEV_PROP_BYTES_CLAIM:
5492 case VDEV_PROP_BYTES_TRIM:
5493 if (literal) {
5494 (void) snprintf(buf, len, "%llu",
5495 (u_longlong_t)intval);
5496 } else {
5497 (void) zfs_nicenum(intval, buf, len);
5498 }
5499 break;
5500 case VDEV_PROP_EXPANDSZ:
5501 if (intval == 0) {
5502 (void) strlcpy(buf, "-", len);
5503 } else if (literal) {
5504 (void) snprintf(buf, len, "%llu",
5505 (u_longlong_t)intval);
5506 } else {
5507 (void) zfs_nicenum(intval, buf, len);
5508 }
5509 break;
5510 case VDEV_PROP_CAPACITY:
5511 if (literal) {
5512 (void) snprintf(buf, len, "%llu",
5513 (u_longlong_t)intval);
5514 } else {
5515 (void) snprintf(buf, len, "%llu%%",
5516 (u_longlong_t)intval);
5517 }
5518 break;
5519 case VDEV_PROP_CHECKSUM_N:
5520 case VDEV_PROP_CHECKSUM_T:
5521 case VDEV_PROP_IO_N:
5522 case VDEV_PROP_IO_T:
5523 case VDEV_PROP_SLOW_IO_N:
5524 case VDEV_PROP_SLOW_IO_T:
5525 if (intval == UINT64_MAX) {
5526 (void) strlcpy(buf, "-", len);
5527 } else {
5528 (void) snprintf(buf, len, "%llu",
5529 (u_longlong_t)intval);
5530 }
5531 break;
5532 case VDEV_PROP_FRAGMENTATION:
5533 if (intval == UINT64_MAX) {
5534 (void) strlcpy(buf, "-", len);
5535 } else {
5536 (void) snprintf(buf, len, "%llu%%",
5537 (u_longlong_t)intval);
5538 }
5539 break;
5540 case VDEV_PROP_STATE:
5541 if (literal) {
5542 (void) snprintf(buf, len, "%llu",
5543 (u_longlong_t)intval);
5544 } else {
5545 (void) strlcpy(buf, zpool_state_to_name(intval,
5546 VDEV_AUX_NONE), len);
5547 }
5548 break;
5549 default:
5550 (void) snprintf(buf, len, "%llu",
5551 (u_longlong_t)intval);
5552 }
5553 break;
5554
5555 case PROP_TYPE_INDEX:
5556 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5557 src = fnvlist_lookup_uint64(nv, ZPROP_SOURCE);
5558 intval = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
5559 } else {
5560 /* 'trim_support' only valid for leaf vdevs */
5561 if (prop == VDEV_PROP_TRIM_SUPPORT) {
5562 (void) strlcpy(buf, "-", len);
5563 break;
5564 }
5565 src = ZPROP_SRC_DEFAULT;
5566 intval = vdev_prop_default_numeric(prop);
5567 /* Only use if provided by the RAIDZ VDEV above */
5568 if (prop == VDEV_PROP_RAIDZ_EXPANDING)
5569 return (ENOENT);
5570 if (prop == VDEV_PROP_SIT_OUT)
5571 return (ENOENT);
5572 }
5573 if (vdev_prop_index_to_string(prop, intval,
5574 (const char **)&strval) != 0)
5575 return (-1);
5576 (void) strlcpy(buf, strval, len);
5577 break;
5578
5579 default:
5580 abort();
5581 }
5582
5583 if (srctype)
5584 *srctype = src;
5585
5586 return (0);
5587 }
5588
5589 /*
5590 * Get a vdev property value for 'prop_name' and return the value in
5591 * a pre-allocated buffer.
5592 */
5593 int
zpool_get_vdev_prop(zpool_handle_t * zhp,const char * vdevname,vdev_prop_t prop,char * prop_name,char * buf,size_t len,zprop_source_t * srctype,boolean_t literal)5594 zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop,
5595 char *prop_name, char *buf, size_t len, zprop_source_t *srctype,
5596 boolean_t literal)
5597 {
5598 nvlist_t *reqnvl, *reqprops;
5599 nvlist_t *retprops = NULL;
5600 uint64_t vdev_guid = 0;
5601 int ret;
5602
5603 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5604 return (ret);
5605
5606 if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0)
5607 return (no_memory(zhp->zpool_hdl));
5608 if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0)
5609 return (no_memory(zhp->zpool_hdl));
5610
5611 fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5612
5613 if (prop != VDEV_PROP_USERPROP) {
5614 /* prop_name overrides prop value */
5615 if (prop_name != NULL)
5616 prop = vdev_name_to_prop(prop_name);
5617 else
5618 prop_name = (char *)vdev_prop_to_name(prop);
5619 assert(prop < VDEV_NUM_PROPS);
5620 }
5621
5622 assert(prop_name != NULL);
5623 if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) {
5624 nvlist_free(reqnvl);
5625 nvlist_free(reqprops);
5626 return (no_memory(zhp->zpool_hdl));
5627 }
5628
5629 fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops);
5630
5631 ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops);
5632
5633 if (ret == 0) {
5634 ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf,
5635 len, srctype, literal);
5636 } else {
5637 char errbuf[ERRBUFLEN];
5638 (void) snprintf(errbuf, sizeof (errbuf),
5639 dgettext(TEXT_DOMAIN, "cannot get vdev property %s from"
5640 " %s in %s"), prop_name, vdevname, zhp->zpool_name);
5641 (void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf);
5642 }
5643
5644 nvlist_free(reqnvl);
5645 nvlist_free(reqprops);
5646 nvlist_free(retprops);
5647
5648 return (ret);
5649 }
5650
5651 /*
5652 * Get all vdev properties
5653 */
5654 int
zpool_get_all_vdev_props(zpool_handle_t * zhp,const char * vdevname,nvlist_t ** outnvl)5655 zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname,
5656 nvlist_t **outnvl)
5657 {
5658 nvlist_t *nvl = NULL;
5659 uint64_t vdev_guid = 0;
5660 int ret;
5661
5662 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5663 return (ret);
5664
5665 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5666 return (no_memory(zhp->zpool_hdl));
5667
5668 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5669
5670 ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl);
5671
5672 nvlist_free(nvl);
5673
5674 if (ret) {
5675 char errbuf[ERRBUFLEN];
5676 (void) snprintf(errbuf, sizeof (errbuf),
5677 dgettext(TEXT_DOMAIN, "cannot get vdev properties for"
5678 " %s in %s"), vdevname, zhp->zpool_name);
5679 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5680 }
5681
5682 return (ret);
5683 }
5684
5685 /*
5686 * Set vdev property
5687 */
5688 int
zpool_set_vdev_prop(zpool_handle_t * zhp,const char * vdevname,const char * propname,const char * propval)5689 zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname,
5690 const char *propname, const char *propval)
5691 {
5692 int ret;
5693 nvlist_t *nvl = NULL;
5694 nvlist_t *outnvl = NULL;
5695 nvlist_t *props;
5696 nvlist_t *realprops;
5697 prop_flags_t flags = { 0 };
5698 uint64_t version;
5699 uint64_t vdev_guid;
5700
5701 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5702 return (ret);
5703
5704 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5705 return (no_memory(zhp->zpool_hdl));
5706 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5707 return (no_memory(zhp->zpool_hdl));
5708
5709 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid);
5710
5711 if (nvlist_add_string(props, propname, propval) != 0) {
5712 nvlist_free(props);
5713 return (no_memory(zhp->zpool_hdl));
5714 }
5715
5716 char errbuf[ERRBUFLEN];
5717 (void) snprintf(errbuf, sizeof (errbuf),
5718 dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"),
5719 propname, vdevname, zhp->zpool_name);
5720
5721 flags.vdevprop = 1;
5722 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5723 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5724 zhp->zpool_name, props, version, flags, errbuf)) == NULL) {
5725 nvlist_free(props);
5726 nvlist_free(nvl);
5727 return (-1);
5728 }
5729
5730 nvlist_free(props);
5731 props = realprops;
5732
5733 fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props);
5734
5735 ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl);
5736
5737 nvlist_free(props);
5738 nvlist_free(nvl);
5739 nvlist_free(outnvl);
5740
5741 if (ret) {
5742 if (errno == ENOTSUP) {
5743 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
5744 "property not supported for this vdev"));
5745 (void) zfs_error(zhp->zpool_hdl, EZFS_PROPTYPE, errbuf);
5746 } else {
5747 (void) zpool_standard_error(zhp->zpool_hdl, errno,
5748 errbuf);
5749 }
5750 }
5751
5752 return (ret);
5753 }
5754
5755 /*
5756 * Prune older entries from the DDT to reclaim space under the quota
5757 */
5758 int
zpool_ddt_prune(zpool_handle_t * zhp,zpool_ddt_prune_unit_t unit,uint64_t amount)5759 zpool_ddt_prune(zpool_handle_t *zhp, zpool_ddt_prune_unit_t unit,
5760 uint64_t amount)
5761 {
5762 int error = lzc_ddt_prune(zhp->zpool_name, unit, amount);
5763 if (error != 0) {
5764 libzfs_handle_t *hdl = zhp->zpool_hdl;
5765 char errbuf[ERRBUFLEN];
5766
5767 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5768 "cannot prune dedup table on '%s'"), zhp->zpool_name);
5769
5770 if (error == EALREADY) {
5771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5772 "a prune operation is already in progress"));
5773 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
5774 } else {
5775 (void) zpool_standard_error(hdl, errno, errbuf);
5776 }
5777 return (-1);
5778 }
5779
5780 return (0);
5781 }
5782