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