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