]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_pool.c
MFV r329502: 7614 zfs device evacuation/removal
[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, use "
1813                                     "'-m' to import the pool anyway:\n"));
1814                                 print_vdev_tree(hdl, NULL, missing, 2);
1815                                 (void) printf("\n");
1816                         }
1817                         (void) zpool_standard_error(hdl, error, desc);
1818                         break;
1819
1820                 case EEXIST:
1821                         (void) zpool_standard_error(hdl, error, desc);
1822                         break;
1823                 case ENAMETOOLONG:
1824                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825                             "new name of at least one dataset is longer than "
1826                             "the maximum allowable length"));
1827                         (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1828                         break;
1829                 default:
1830                         (void) zpool_standard_error(hdl, error, desc);
1831                         zpool_explain_recover(hdl,
1832                             newname ? origname : thename, -error, nv);
1833                         break;
1834                 }
1835
1836                 nvlist_free(nv);
1837                 ret = -1;
1838         } else {
1839                 zpool_handle_t *zhp;
1840
1841                 /*
1842                  * This should never fail, but play it safe anyway.
1843                  */
1844                 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1845                         ret = -1;
1846                 else if (zhp != NULL)
1847                         zpool_close(zhp);
1848                 if (policy.zrp_request &
1849                     (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1850                         zpool_rewind_exclaim(hdl, newname ? origname : thename,
1851                             ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1852                 }
1853                 nvlist_free(nv);
1854                 return (0);
1855         }
1856
1857         return (ret);
1858 }
1859
1860 /*
1861  * Scan the pool.
1862  */
1863 int
1864 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1865 {
1866         zfs_cmd_t zc = { 0 };
1867         char msg[1024];
1868         int err;
1869         libzfs_handle_t *hdl = zhp->zpool_hdl;
1870
1871         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1872         zc.zc_cookie = func;
1873         zc.zc_flags = cmd;
1874
1875         if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1876                 return (0);
1877
1878         err = errno;
1879
1880         /* ECANCELED on a scrub means we resumed a paused scrub */
1881         if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1882             cmd == POOL_SCRUB_NORMAL)
1883                 return (0);
1884
1885         if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1886                 return (0);
1887
1888         if (func == POOL_SCAN_SCRUB) {
1889                 if (cmd == POOL_SCRUB_PAUSE) {
1890                         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1891                             "cannot pause scrubbing %s"), zc.zc_name);
1892                 } else {
1893                         assert(cmd == POOL_SCRUB_NORMAL);
1894                         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1895                             "cannot scrub %s"), zc.zc_name);
1896                 }
1897         } else if (func == POOL_SCAN_NONE) {
1898                 (void) snprintf(msg, sizeof (msg),
1899                     dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1900                     zc.zc_name);
1901         } else {
1902                 assert(!"unexpected result");
1903         }
1904
1905         if (err == EBUSY) {
1906                 nvlist_t *nvroot;
1907                 pool_scan_stat_t *ps = NULL;
1908                 uint_t psc;
1909
1910                 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1911                     ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1912                 (void) nvlist_lookup_uint64_array(nvroot,
1913                     ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1914                 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1915                         if (cmd == POOL_SCRUB_PAUSE)
1916                                 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1917                         else
1918                                 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1919                 } else {
1920                         return (zfs_error(hdl, EZFS_RESILVERING, msg));
1921                 }
1922         } else if (err == ENOENT) {
1923                 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1924         } else {
1925                 return (zpool_standard_error(hdl, err, msg));
1926         }
1927 }
1928
1929 #ifdef illumos
1930 /*
1931  * This provides a very minimal check whether a given string is likely a
1932  * c#t#d# style string.  Users of this are expected to do their own
1933  * verification of the s# part.
1934  */
1935 #define CTD_CHECK(str)  (str && str[0] == 'c' && isdigit(str[1]))
1936
1937 /*
1938  * More elaborate version for ones which may start with "/dev/dsk/"
1939  * and the like.
1940  */
1941 static int
1942 ctd_check_path(char *str)
1943 {
1944         /*
1945          * If it starts with a slash, check the last component.
1946          */
1947         if (str && str[0] == '/') {
1948                 char *tmp = strrchr(str, '/');
1949
1950                 /*
1951                  * If it ends in "/old", check the second-to-last
1952                  * component of the string instead.
1953                  */
1954                 if (tmp != str && strcmp(tmp, "/old") == 0) {
1955                         for (tmp--; *tmp != '/'; tmp--)
1956                                 ;
1957                 }
1958                 str = tmp + 1;
1959         }
1960         return (CTD_CHECK(str));
1961 }
1962 #endif
1963
1964 /*
1965  * Find a vdev that matches the search criteria specified. We use the
1966  * the nvpair name to determine how we should look for the device.
1967  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1968  * spare; but FALSE if its an INUSE spare.
1969  */
1970 static nvlist_t *
1971 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1972     boolean_t *l2cache, boolean_t *log)
1973 {
1974         uint_t c, children;
1975         nvlist_t **child;
1976         nvlist_t *ret;
1977         uint64_t is_log;
1978         char *srchkey;
1979         nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1980
1981         /* Nothing to look for */
1982         if (search == NULL || pair == NULL)
1983                 return (NULL);
1984
1985         /* Obtain the key we will use to search */
1986         srchkey = nvpair_name(pair);
1987
1988         switch (nvpair_type(pair)) {
1989         case DATA_TYPE_UINT64:
1990                 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1991                         uint64_t srchval, theguid;
1992
1993                         verify(nvpair_value_uint64(pair, &srchval) == 0);
1994                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1995                             &theguid) == 0);
1996                         if (theguid == srchval)
1997                                 return (nv);
1998                 }
1999                 break;
2000
2001         case DATA_TYPE_STRING: {
2002                 char *srchval, *val;
2003
2004                 verify(nvpair_value_string(pair, &srchval) == 0);
2005                 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2006                         break;
2007
2008                 /*
2009                  * Search for the requested value. Special cases:
2010                  *
2011                  * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2012                  *   UEFI boot, these end in "s0" or "s0/old" or "s1" or
2013                  *   "s1/old".   The "s0" or "s1" part is hidden from the user,
2014                  *   but included in the string, so this matches around it.
2015                  * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2016                  *
2017                  * Otherwise, all other searches are simple string compares.
2018                  */
2019 #ifdef illumos
2020                 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2021                     ctd_check_path(val)) {
2022                         uint64_t wholedisk = 0;
2023
2024                         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2025                             &wholedisk);
2026                         if (wholedisk) {
2027                                 int slen = strlen(srchval);
2028                                 int vlen = strlen(val);
2029
2030                                 if (slen != vlen - 2)
2031                                         break;
2032
2033                                 /*
2034                                  * make_leaf_vdev() should only set
2035                                  * wholedisk for ZPOOL_CONFIG_PATHs which
2036                                  * will include "/dev/dsk/", giving plenty of
2037                                  * room for the indices used next.
2038                                  */
2039                                 ASSERT(vlen >= 6);
2040
2041                                 /*
2042                                  * strings identical except trailing "s0"
2043                                  */
2044                                 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2045                                     strcmp(&val[vlen - 2], "s1") == 0) &&
2046                                     strncmp(srchval, val, slen) == 0)
2047                                         return (nv);
2048
2049                                 /*
2050                                  * strings identical except trailing "s0/old"
2051                                  */
2052                                 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2053                                     strcmp(&val[vlen - 6], "s1/old") == 0) &&
2054                                     strcmp(&srchval[slen - 4], "/old") == 0 &&
2055                                     strncmp(srchval, val, slen - 4) == 0)
2056                                         return (nv);
2057
2058                                 break;
2059                         }
2060                 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2061 #else
2062                 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2063 #endif
2064                         char *type, *idx, *end, *p;
2065                         uint64_t id, vdev_id;
2066
2067                         /*
2068                          * Determine our vdev type, keeping in mind
2069                          * that the srchval is composed of a type and
2070                          * vdev id pair (i.e. mirror-4).
2071                          */
2072                         if ((type = strdup(srchval)) == NULL)
2073                                 return (NULL);
2074
2075                         if ((p = strrchr(type, '-')) == NULL) {
2076                                 free(type);
2077                                 break;
2078                         }
2079                         idx = p + 1;
2080                         *p = '\0';
2081
2082                         /*
2083                          * If the types don't match then keep looking.
2084                          */
2085                         if (strncmp(val, type, strlen(val)) != 0) {
2086                                 free(type);
2087                                 break;
2088                         }
2089
2090                         verify(zpool_vdev_is_interior(type));
2091                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2092                             &id) == 0);
2093
2094                         errno = 0;
2095                         vdev_id = strtoull(idx, &end, 10);
2096
2097                         free(type);
2098                         if (errno != 0)
2099                                 return (NULL);
2100
2101                         /*
2102                          * Now verify that we have the correct vdev id.
2103                          */
2104                         if (vdev_id == id)
2105                                 return (nv);
2106                 }
2107
2108                 /*
2109                  * Common case
2110                  */
2111                 if (strcmp(srchval, val) == 0)
2112                         return (nv);
2113                 break;
2114         }
2115
2116         default:
2117                 break;
2118         }
2119
2120         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2121             &child, &children) != 0)
2122                 return (NULL);
2123
2124         for (c = 0; c < children; c++) {
2125                 if ((ret = vdev_to_nvlist_iter(child[c], search,
2126                     avail_spare, l2cache, NULL)) != NULL) {
2127                         /*
2128                          * The 'is_log' value is only set for the toplevel
2129                          * vdev, not the leaf vdevs.  So we always lookup the
2130                          * log device from the root of the vdev tree (where
2131                          * 'log' is non-NULL).
2132                          */
2133                         if (log != NULL &&
2134                             nvlist_lookup_uint64(child[c],
2135                             ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2136                             is_log) {
2137                                 *log = B_TRUE;
2138                         }
2139                         return (ret);
2140                 }
2141         }
2142
2143         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2144             &child, &children) == 0) {
2145                 for (c = 0; c < children; c++) {
2146                         if ((ret = vdev_to_nvlist_iter(child[c], search,
2147                             avail_spare, l2cache, NULL)) != NULL) {
2148                                 *avail_spare = B_TRUE;
2149                                 return (ret);
2150                         }
2151                 }
2152         }
2153
2154         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2155             &child, &children) == 0) {
2156                 for (c = 0; c < children; c++) {
2157                         if ((ret = vdev_to_nvlist_iter(child[c], search,
2158                             avail_spare, l2cache, NULL)) != NULL) {
2159                                 *l2cache = B_TRUE;
2160                                 return (ret);
2161                         }
2162                 }
2163         }
2164
2165         return (NULL);
2166 }
2167
2168 /*
2169  * Given a physical path (minus the "/devices" prefix), find the
2170  * associated vdev.
2171  */
2172 nvlist_t *
2173 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2174     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2175 {
2176         nvlist_t *search, *nvroot, *ret;
2177
2178         verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2179         verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2180
2181         verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2182             &nvroot) == 0);
2183
2184         *avail_spare = B_FALSE;
2185         *l2cache = B_FALSE;
2186         if (log != NULL)
2187                 *log = B_FALSE;
2188         ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2189         nvlist_free(search);
2190
2191         return (ret);
2192 }
2193
2194 /*
2195  * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2196  */
2197 static boolean_t
2198 zpool_vdev_is_interior(const char *name)
2199 {
2200         if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2201             strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2202             strncmp(name,
2203             VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2204             strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2205                 return (B_TRUE);
2206         return (B_FALSE);
2207 }
2208
2209 nvlist_t *
2210 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2211     boolean_t *l2cache, boolean_t *log)
2212 {
2213         char buf[MAXPATHLEN];
2214         char *end;
2215         nvlist_t *nvroot, *search, *ret;
2216         uint64_t guid;
2217
2218         verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2219
2220         guid = strtoull(path, &end, 10);
2221         if (guid != 0 && *end == '\0') {
2222                 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2223         } else if (zpool_vdev_is_interior(path)) {
2224                 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2225         } else if (path[0] != '/') {
2226                 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2227                 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2228         } else {
2229                 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2230         }
2231
2232         verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2233             &nvroot) == 0);
2234
2235         *avail_spare = B_FALSE;
2236         *l2cache = B_FALSE;
2237         if (log != NULL)
2238                 *log = B_FALSE;
2239         ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2240         nvlist_free(search);
2241
2242         return (ret);
2243 }
2244
2245 static int
2246 vdev_online(nvlist_t *nv)
2247 {
2248         uint64_t ival;
2249
2250         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2251             nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2252             nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2253                 return (0);
2254
2255         return (1);
2256 }
2257
2258 /*
2259  * Helper function for zpool_get_physpaths().
2260  */
2261 static int
2262 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2263     size_t *bytes_written)
2264 {
2265         size_t bytes_left, pos, rsz;
2266         char *tmppath;
2267         const char *format;
2268
2269         if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2270             &tmppath) != 0)
2271                 return (EZFS_NODEVICE);
2272
2273         pos = *bytes_written;
2274         bytes_left = physpath_size - pos;
2275         format = (pos == 0) ? "%s" : " %s";
2276
2277         rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2278         *bytes_written += rsz;
2279
2280         if (rsz >= bytes_left) {
2281                 /* if physpath was not copied properly, clear it */
2282                 if (bytes_left != 0) {
2283                         physpath[pos] = 0;
2284                 }
2285                 return (EZFS_NOSPC);
2286         }
2287         return (0);
2288 }
2289
2290 static int
2291 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2292     size_t *rsz, boolean_t is_spare)
2293 {
2294         char *type;
2295         int ret;
2296
2297         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2298                 return (EZFS_INVALCONFIG);
2299
2300         if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2301                 /*
2302                  * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2303                  * For a spare vdev, we only want to boot from the active
2304                  * spare device.
2305                  */
2306                 if (is_spare) {
2307                         uint64_t spare = 0;
2308                         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2309                             &spare);
2310                         if (!spare)
2311                                 return (EZFS_INVALCONFIG);
2312                 }
2313
2314                 if (vdev_online(nv)) {
2315                         if ((ret = vdev_get_one_physpath(nv, physpath,
2316                             phypath_size, rsz)) != 0)
2317                                 return (ret);
2318                 }
2319         } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2320             strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2321             strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2322             (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2323                 nvlist_t **child;
2324                 uint_t count;
2325                 int i, ret;
2326
2327                 if (nvlist_lookup_nvlist_array(nv,
2328                     ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2329                         return (EZFS_INVALCONFIG);
2330
2331                 for (i = 0; i < count; i++) {
2332                         ret = vdev_get_physpaths(child[i], physpath,
2333                             phypath_size, rsz, is_spare);
2334                         if (ret == EZFS_NOSPC)
2335                                 return (ret);
2336                 }
2337         }
2338
2339         return (EZFS_POOL_INVALARG);
2340 }
2341
2342 /*
2343  * Get phys_path for a root pool config.
2344  * Return 0 on success; non-zero on failure.
2345  */
2346 static int
2347 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2348 {
2349         size_t rsz;
2350         nvlist_t *vdev_root;
2351         nvlist_t **child;
2352         uint_t count;
2353         char *type;
2354
2355         rsz = 0;
2356
2357         if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2358             &vdev_root) != 0)
2359                 return (EZFS_INVALCONFIG);
2360
2361         if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2362             nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2363             &child, &count) != 0)
2364                 return (EZFS_INVALCONFIG);
2365
2366         /*
2367          * root pool can only have a single top-level vdev.
2368          */
2369         if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2370                 return (EZFS_POOL_INVALARG);
2371
2372         (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2373             B_FALSE);
2374
2375         /* No online devices */
2376         if (rsz == 0)
2377                 return (EZFS_NODEVICE);
2378
2379         return (0);
2380 }
2381
2382 /*
2383  * Get phys_path for a root pool
2384  * Return 0 on success; non-zero on failure.
2385  */
2386 int
2387 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2388 {
2389         return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2390             phypath_size));
2391 }
2392
2393 /*
2394  * If the device has being dynamically expanded then we need to relabel
2395  * the disk to use the new unallocated space.
2396  */
2397 static int
2398 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2399 {
2400 #ifdef illumos
2401         char path[MAXPATHLEN];
2402         char errbuf[1024];
2403         int fd, error;
2404         int (*_efi_use_whole_disk)(int);
2405
2406         if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2407             "efi_use_whole_disk")) == NULL)
2408                 return (-1);
2409
2410         (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2411
2412         if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2413                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2414                     "relabel '%s': unable to open device"), name);
2415                 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2416         }
2417
2418         /*
2419          * It's possible that we might encounter an error if the device
2420          * does not have any unallocated space left. If so, we simply
2421          * ignore that error and continue on.
2422          */
2423         error = _efi_use_whole_disk(fd);
2424         (void) close(fd);
2425         if (error && error != VT_ENOSPC) {
2426                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2427                     "relabel '%s': unable to read disk capacity"), name);
2428                 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2429         }
2430 #endif  /* illumos */
2431         return (0);
2432 }
2433
2434 /*
2435  * Bring the specified vdev online.   The 'flags' parameter is a set of the
2436  * ZFS_ONLINE_* flags.
2437  */
2438 int
2439 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2440     vdev_state_t *newstate)
2441 {
2442         zfs_cmd_t zc = { 0 };
2443         char msg[1024];
2444         char *pathname;
2445         nvlist_t *tgt;
2446         boolean_t avail_spare, l2cache, islog;
2447         libzfs_handle_t *hdl = zhp->zpool_hdl;
2448
2449         if (flags & ZFS_ONLINE_EXPAND) {
2450                 (void) snprintf(msg, sizeof (msg),
2451                     dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2452         } else {
2453                 (void) snprintf(msg, sizeof (msg),
2454                     dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2455         }
2456
2457         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2458         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2459             &islog)) == NULL)
2460                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2461
2462         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2463
2464         if (avail_spare)
2465                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2466
2467         if ((flags & ZFS_ONLINE_EXPAND ||
2468             zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2469             nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2470                 uint64_t wholedisk = 0;
2471
2472                 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2473                     &wholedisk);
2474
2475                 /*
2476                  * XXX - L2ARC 1.0 devices can't support expansion.
2477                  */
2478                 if (l2cache) {
2479                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2480                             "cannot expand cache devices"));
2481                         return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2482                 }
2483
2484                 if (wholedisk) {
2485                         pathname += strlen(ZFS_DISK_ROOT) + 1;
2486                         (void) zpool_relabel_disk(hdl, pathname);
2487                 }
2488         }
2489
2490         zc.zc_cookie = VDEV_STATE_ONLINE;
2491         zc.zc_obj = flags;
2492
2493         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2494                 if (errno == EINVAL) {
2495                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2496                             "from this pool into a new one.  Use '%s' "
2497                             "instead"), "zpool detach");
2498                         return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2499                 }
2500                 return (zpool_standard_error(hdl, errno, msg));
2501         }
2502
2503         *newstate = zc.zc_cookie;
2504         return (0);
2505 }
2506
2507 /*
2508  * Take the specified vdev offline
2509  */
2510 int
2511 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2512 {
2513         zfs_cmd_t zc = { 0 };
2514         char msg[1024];
2515         nvlist_t *tgt;
2516         boolean_t avail_spare, l2cache;
2517         libzfs_handle_t *hdl = zhp->zpool_hdl;
2518
2519         (void) snprintf(msg, sizeof (msg),
2520             dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2521
2522         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2523         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2524             NULL)) == NULL)
2525                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2526
2527         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2528
2529         if (avail_spare)
2530                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2531
2532         zc.zc_cookie = VDEV_STATE_OFFLINE;
2533         zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2534
2535         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2536                 return (0);
2537
2538         switch (errno) {
2539         case EBUSY:
2540
2541                 /*
2542                  * There are no other replicas of this device.
2543                  */
2544                 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2545
2546         case EEXIST:
2547                 /*
2548                  * The log device has unplayed logs
2549                  */
2550                 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2551
2552         default:
2553                 return (zpool_standard_error(hdl, errno, msg));
2554         }
2555 }
2556
2557 /*
2558  * Mark the given vdev faulted.
2559  */
2560 int
2561 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2562 {
2563         zfs_cmd_t zc = { 0 };
2564         char msg[1024];
2565         libzfs_handle_t *hdl = zhp->zpool_hdl;
2566
2567         (void) snprintf(msg, sizeof (msg),
2568             dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2569
2570         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2571         zc.zc_guid = guid;
2572         zc.zc_cookie = VDEV_STATE_FAULTED;
2573         zc.zc_obj = aux;
2574
2575         if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2576                 return (0);
2577
2578         switch (errno) {
2579         case EBUSY:
2580
2581                 /*
2582                  * There are no other replicas of this device.
2583                  */
2584                 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2585
2586         default:
2587                 return (zpool_standard_error(hdl, errno, msg));
2588         }
2589
2590 }
2591
2592 /*
2593  * Mark the given vdev degraded.
2594  */
2595 int
2596 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2597 {
2598         zfs_cmd_t zc = { 0 };
2599         char msg[1024];
2600         libzfs_handle_t *hdl = zhp->zpool_hdl;
2601
2602         (void) snprintf(msg, sizeof (msg),
2603             dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2604
2605         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2606         zc.zc_guid = guid;
2607         zc.zc_cookie = VDEV_STATE_DEGRADED;
2608         zc.zc_obj = aux;
2609
2610         if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2611                 return (0);
2612
2613         return (zpool_standard_error(hdl, errno, msg));
2614 }
2615
2616 /*
2617  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2618  * a hot spare.
2619  */
2620 static boolean_t
2621 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2622 {
2623         nvlist_t **child;
2624         uint_t c, children;
2625         char *type;
2626
2627         if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2628             &children) == 0) {
2629                 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2630                     &type) == 0);
2631
2632                 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2633                     children == 2 && child[which] == tgt)
2634                         return (B_TRUE);
2635
2636                 for (c = 0; c < children; c++)
2637                         if (is_replacing_spare(child[c], tgt, which))
2638                                 return (B_TRUE);
2639         }
2640
2641         return (B_FALSE);
2642 }
2643
2644 /*
2645  * Attach new_disk (fully described by nvroot) to old_disk.
2646  * If 'replacing' is specified, the new disk will replace the old one.
2647  */
2648 int
2649 zpool_vdev_attach(zpool_handle_t *zhp,
2650     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2651 {
2652         zfs_cmd_t zc = { 0 };
2653         char msg[1024];
2654         int ret;
2655         nvlist_t *tgt;
2656         boolean_t avail_spare, l2cache, islog;
2657         uint64_t val;
2658         char *newname;
2659         nvlist_t **child;
2660         uint_t children;
2661         nvlist_t *config_root;
2662         libzfs_handle_t *hdl = zhp->zpool_hdl;
2663         boolean_t rootpool = zpool_is_bootable(zhp);
2664
2665         if (replacing)
2666                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2667                     "cannot replace %s with %s"), old_disk, new_disk);
2668         else
2669                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2670                     "cannot attach %s to %s"), new_disk, old_disk);
2671
2672         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2673         if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2674             &islog)) == NULL)
2675                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2676
2677         if (avail_spare)
2678                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2679
2680         if (l2cache)
2681                 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2682
2683         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2684         zc.zc_cookie = replacing;
2685
2686         if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2687             &child, &children) != 0 || children != 1) {
2688                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2689                     "new device must be a single disk"));
2690                 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2691         }
2692
2693         verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2694             ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2695
2696         if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2697                 return (-1);
2698
2699         /*
2700          * If the target is a hot spare that has been swapped in, we can only
2701          * replace it with another hot spare.
2702          */
2703         if (replacing &&
2704             nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2705             (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2706             NULL) == NULL || !avail_spare) &&
2707             is_replacing_spare(config_root, tgt, 1)) {
2708                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2709                     "can only be replaced by another hot spare"));
2710                 free(newname);
2711                 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2712         }
2713
2714         free(newname);
2715
2716         if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2717                 return (-1);
2718
2719         ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2720
2721         zcmd_free_nvlists(&zc);
2722
2723         if (ret == 0) {
2724                 if (rootpool) {
2725                         /*
2726                          * XXX need a better way to prevent user from
2727                          * booting up a half-baked vdev.
2728                          */
2729                         (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2730                             "sure to wait until resilver is done "
2731                             "before rebooting.\n"));
2732                         (void) fprintf(stderr, "\n");
2733                         (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2734                             "you boot from pool '%s', you may need to update\n"
2735                             "boot code on newly attached disk '%s'.\n\n"
2736                             "Assuming you use GPT partitioning and 'da0' is "
2737                             "your new boot disk\n"
2738                             "you may use the following command:\n\n"
2739                             "\tgpart bootcode -b /boot/pmbr -p "
2740                             "/boot/gptzfsboot -i 1 da0\n\n"),
2741                             zhp->zpool_name, new_disk);
2742                 }
2743                 return (0);
2744         }
2745
2746         switch (errno) {
2747         case ENOTSUP:
2748                 /*
2749                  * Can't attach to or replace this type of vdev.
2750                  */
2751                 if (replacing) {
2752                         uint64_t version = zpool_get_prop_int(zhp,
2753                             ZPOOL_PROP_VERSION, NULL);
2754
2755                         if (islog)
2756                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2757                                     "cannot replace a log with a spare"));
2758                         else if (version >= SPA_VERSION_MULTI_REPLACE)
2759                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2760                                     "already in replacing/spare config; wait "
2761                                     "for completion or use 'zpool detach'"));
2762                         else
2763                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2764                                     "cannot replace a replacing device"));
2765                 } else {
2766                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2767                             "can only attach to mirrors and top-level "
2768                             "disks"));
2769                 }
2770                 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2771                 break;
2772
2773         case EINVAL:
2774                 /*
2775                  * The new device must be a single disk.
2776                  */
2777                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2778                     "new device must be a single disk"));
2779                 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2780                 break;
2781
2782         case EBUSY:
2783                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2784                     "or pool has removing/removed vdevs"),
2785                     new_disk);
2786                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2787                 break;
2788
2789         case EOVERFLOW:
2790                 /*
2791                  * The new device is too small.
2792                  */
2793                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794                     "device is too small"));
2795                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2796                 break;
2797
2798         case EDOM:
2799                 /*
2800                  * The new device has a different alignment requirement.
2801                  */
2802                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803                     "devices have different sector alignment"));
2804                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2805                 break;
2806
2807         case ENAMETOOLONG:
2808                 /*
2809                  * The resulting top-level vdev spec won't fit in the label.
2810                  */
2811                 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2812                 break;
2813
2814         default:
2815                 (void) zpool_standard_error(hdl, errno, msg);
2816         }
2817
2818         return (-1);
2819 }
2820
2821 /*
2822  * Detach the specified device.
2823  */
2824 int
2825 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2826 {
2827         zfs_cmd_t zc = { 0 };
2828         char msg[1024];
2829         nvlist_t *tgt;
2830         boolean_t avail_spare, l2cache;
2831         libzfs_handle_t *hdl = zhp->zpool_hdl;
2832
2833         (void) snprintf(msg, sizeof (msg),
2834             dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2835
2836         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2837         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2838             NULL)) == NULL)
2839                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2840
2841         if (avail_spare)
2842                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2843
2844         if (l2cache)
2845                 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2846
2847         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2848
2849         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2850                 return (0);
2851
2852         switch (errno) {
2853
2854         case ENOTSUP:
2855                 /*
2856                  * Can't detach from this type of vdev.
2857                  */
2858                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2859                     "applicable to mirror and replacing vdevs"));
2860                 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2861                 break;
2862
2863         case EBUSY:
2864                 /*
2865                  * There are no other replicas of this device.
2866                  */
2867                 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2868                 break;
2869
2870         default:
2871                 (void) zpool_standard_error(hdl, errno, msg);
2872         }
2873
2874         return (-1);
2875 }
2876
2877 /*
2878  * Find a mirror vdev in the source nvlist.
2879  *
2880  * The mchild array contains a list of disks in one of the top-level mirrors
2881  * of the source pool.  The schild array contains a list of disks that the
2882  * user specified on the command line.  We loop over the mchild array to
2883  * see if any entry in the schild array matches.
2884  *
2885  * If a disk in the mchild array is found in the schild array, we return
2886  * the index of that entry.  Otherwise we return -1.
2887  */
2888 static int
2889 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2890     nvlist_t **schild, uint_t schildren)
2891 {
2892         uint_t mc;
2893
2894         for (mc = 0; mc < mchildren; mc++) {
2895                 uint_t sc;
2896                 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2897                     mchild[mc], B_FALSE);
2898
2899                 for (sc = 0; sc < schildren; sc++) {
2900                         char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2901                             schild[sc], B_FALSE);
2902                         boolean_t result = (strcmp(mpath, spath) == 0);
2903
2904                         free(spath);
2905                         if (result) {
2906                                 free(mpath);
2907                                 return (mc);
2908                         }
2909                 }
2910
2911                 free(mpath);
2912         }
2913
2914         return (-1);
2915 }
2916
2917 /*
2918  * Split a mirror pool.  If newroot points to null, then a new nvlist
2919  * is generated and it is the responsibility of the caller to free it.
2920  */
2921 int
2922 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2923     nvlist_t *props, splitflags_t flags)
2924 {
2925         zfs_cmd_t zc = { 0 };
2926         char msg[1024];
2927         nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2928         nvlist_t **varray = NULL, *zc_props = NULL;
2929         uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2930         libzfs_handle_t *hdl = zhp->zpool_hdl;
2931         uint64_t vers;
2932         boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2933         int retval = 0;
2934
2935         (void) snprintf(msg, sizeof (msg),
2936             dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2937
2938         if (!zpool_name_valid(hdl, B_FALSE, newname))
2939                 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2940
2941         if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2942                 (void) fprintf(stderr, gettext("Internal error: unable to "
2943                     "retrieve pool configuration\n"));
2944                 return (-1);
2945         }
2946
2947         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2948             == 0);
2949         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2950
2951         if (props) {
2952                 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2953                 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2954                     props, vers, flags, msg)) == NULL)
2955                         return (-1);
2956         }
2957
2958         if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2959             &children) != 0) {
2960                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2961                     "Source pool is missing vdev tree"));
2962                 nvlist_free(zc_props);
2963                 return (-1);
2964         }
2965
2966         varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2967         vcount = 0;
2968
2969         if (*newroot == NULL ||
2970             nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2971             &newchild, &newchildren) != 0)
2972                 newchildren = 0;
2973
2974         for (c = 0; c < children; c++) {
2975                 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2976                 char *type;
2977                 nvlist_t **mchild, *vdev;
2978                 uint_t mchildren;
2979                 int entry;
2980
2981                 /*
2982                  * Unlike cache & spares, slogs are stored in the
2983                  * ZPOOL_CONFIG_CHILDREN array.  We filter them out here.
2984                  */
2985                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2986                     &is_log);
2987                 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2988                     &is_hole);
2989                 if (is_log || is_hole) {
2990                         /*
2991                          * Create a hole vdev and put it in the config.
2992                          */
2993                         if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2994                                 goto out;
2995                         if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2996                             VDEV_TYPE_HOLE) != 0)
2997                                 goto out;
2998                         if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2999                             1) != 0)
3000                                 goto out;
3001                         if (lastlog == 0)
3002                                 lastlog = vcount;
3003                         varray[vcount++] = vdev;
3004                         continue;
3005                 }
3006                 lastlog = 0;
3007                 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3008                     == 0);
3009                 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3010                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3011                             "Source pool must be composed only of mirrors\n"));
3012                         retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3013                         goto out;
3014                 }
3015
3016                 verify(nvlist_lookup_nvlist_array(child[c],
3017                     ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3018
3019                 /* find or add an entry for this top-level vdev */
3020                 if (newchildren > 0 &&
3021                     (entry = find_vdev_entry(zhp, mchild, mchildren,
3022                     newchild, newchildren)) >= 0) {
3023                         /* We found a disk that the user specified. */
3024                         vdev = mchild[entry];
3025                         ++found;
3026                 } else {
3027                         /* User didn't specify a disk for this vdev. */
3028                         vdev = mchild[mchildren - 1];
3029                 }
3030
3031                 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3032                         goto out;
3033         }
3034
3035         /* did we find every disk the user specified? */
3036         if (found != newchildren) {
3037                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3038                     "include at most one disk from each mirror"));
3039                 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3040                 goto out;
3041         }
3042
3043         /* Prepare the nvlist for populating. */
3044         if (*newroot == NULL) {
3045                 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3046                         goto out;
3047                 freelist = B_TRUE;
3048                 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3049                     VDEV_TYPE_ROOT) != 0)
3050                         goto out;
3051         } else {
3052                 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3053         }
3054
3055         /* Add all the children we found */
3056         if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3057             lastlog == 0 ? vcount : lastlog) != 0)
3058                 goto out;
3059
3060         /*
3061          * If we're just doing a dry run, exit now with success.
3062          */
3063         if (flags.dryrun) {
3064                 memory_err = B_FALSE;
3065                 freelist = B_FALSE;
3066                 goto out;
3067         }
3068
3069         /* now build up the config list & call the ioctl */
3070         if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3071                 goto out;
3072
3073         if (nvlist_add_nvlist(newconfig,
3074             ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3075             nvlist_add_string(newconfig,
3076             ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3077             nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3078                 goto out;
3079
3080         /*
3081          * The new pool is automatically part of the namespace unless we
3082          * explicitly export it.
3083          */
3084         if (!flags.import)
3085                 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3086         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3087         (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3088         if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3089                 goto out;
3090         if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3091                 goto out;
3092
3093         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3094                 retval = zpool_standard_error(hdl, errno, msg);
3095                 goto out;
3096         }
3097
3098         freelist = B_FALSE;
3099         memory_err = B_FALSE;
3100
3101 out:
3102         if (varray != NULL) {
3103                 int v;
3104
3105                 for (v = 0; v < vcount; v++)
3106                         nvlist_free(varray[v]);
3107                 free(varray);
3108         }
3109         zcmd_free_nvlists(&zc);
3110         nvlist_free(zc_props);
3111         nvlist_free(newconfig);
3112         if (freelist) {
3113                 nvlist_free(*newroot);
3114                 *newroot = NULL;
3115         }
3116
3117         if (retval != 0)
3118                 return (retval);
3119
3120         if (memory_err)
3121                 return (no_memory(hdl));
3122
3123         return (0);
3124 }
3125
3126 /*
3127  * Remove the given device.
3128  */
3129 int
3130 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3131 {
3132         zfs_cmd_t zc = { 0 };
3133         char msg[1024];
3134         nvlist_t *tgt;
3135         boolean_t avail_spare, l2cache, islog;
3136         libzfs_handle_t *hdl = zhp->zpool_hdl;
3137         uint64_t version;
3138
3139         (void) snprintf(msg, sizeof (msg),
3140             dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3141
3142         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3143         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3144             &islog)) == NULL)
3145                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3146
3147         version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3148         if (islog && version < SPA_VERSION_HOLES) {
3149                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3150                     "pool must be upgraded to support log removal"));
3151                 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3152         }
3153
3154         if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3155                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3156                     "root pool can not have removed devices, "
3157                     "because GRUB does not understand them"));
3158                 return (zfs_error(hdl, EINVAL, msg));
3159         }
3160
3161         zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3162
3163         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3164                 return (0);
3165
3166         switch (errno) {
3167
3168         case EINVAL:
3169                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3170                     "invalid config; all top-level vdevs must "
3171                     "have the same sector size and not be raidz."));
3172                 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3173                 break;
3174
3175         case EBUSY:
3176                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3177                     "Pool busy; removal may already be in progress"));
3178                 (void) zfs_error(hdl, EZFS_BUSY, msg);
3179                 break;
3180
3181         default:
3182                 (void) zpool_standard_error(hdl, errno, msg);
3183         }
3184         return (-1);
3185 }
3186
3187 int
3188 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3189 {
3190         zfs_cmd_t zc = { 0 };
3191         char msg[1024];
3192         libzfs_handle_t *hdl = zhp->zpool_hdl;
3193
3194         (void) snprintf(msg, sizeof (msg),
3195             dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3196
3197         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3198         zc.zc_cookie = 1;
3199
3200         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3201                 return (0);
3202
3203         return (zpool_standard_error(hdl, errno, msg));
3204 }
3205
3206 int
3207 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3208     uint64_t *sizep)
3209 {
3210         char msg[1024];
3211         nvlist_t *tgt;
3212         boolean_t avail_spare, l2cache, islog;
3213         libzfs_handle_t *hdl = zhp->zpool_hdl;
3214
3215         (void) snprintf(msg, sizeof (msg),
3216             dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3217             path);
3218
3219         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3220             &islog)) == NULL)
3221                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3222
3223         if (avail_spare || l2cache || islog) {
3224                 *sizep = 0;
3225                 return (0);
3226         }
3227
3228         if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3229                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3230                     "indirect size not available"));
3231                 return (zfs_error(hdl, EINVAL, msg));
3232         }
3233         return (0);
3234 }
3235
3236 /*
3237  * Clear the errors for the pool, or the particular device if specified.
3238  */
3239 int
3240 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3241 {
3242         zfs_cmd_t zc = { 0 };
3243         char msg[1024];
3244         nvlist_t *tgt;
3245         zpool_rewind_policy_t policy;
3246         boolean_t avail_spare, l2cache;
3247         libzfs_handle_t *hdl = zhp->zpool_hdl;
3248         nvlist_t *nvi = NULL;
3249         int error;
3250
3251         if (path)
3252                 (void) snprintf(msg, sizeof (msg),
3253                     dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3254                     path);
3255         else
3256                 (void) snprintf(msg, sizeof (msg),
3257                     dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3258                     zhp->zpool_name);
3259
3260         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3261         if (path) {
3262                 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3263                     &l2cache, NULL)) == NULL)
3264                         return (zfs_error(hdl, EZFS_NODEVICE, msg));
3265
3266                 /*
3267                  * Don't allow error clearing for hot spares.  Do allow
3268                  * error clearing for l2cache devices.
3269                  */
3270                 if (avail_spare)
3271                         return (zfs_error(hdl, EZFS_ISSPARE, msg));
3272
3273                 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3274                     &zc.zc_guid) == 0);
3275         }
3276
3277         zpool_get_rewind_policy(rewindnvl, &policy);
3278         zc.zc_cookie = policy.zrp_request;
3279
3280         if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3281                 return (-1);
3282
3283         if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3284                 return (-1);
3285
3286         while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3287             errno == ENOMEM) {
3288                 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3289                         zcmd_free_nvlists(&zc);
3290                         return (-1);
3291                 }
3292         }
3293
3294         if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3295             errno != EPERM && errno != EACCES)) {
3296                 if (policy.zrp_request &
3297                     (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3298                         (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3299                         zpool_rewind_exclaim(hdl, zc.zc_name,
3300                             ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3301                             nvi);
3302                         nvlist_free(nvi);
3303                 }
3304                 zcmd_free_nvlists(&zc);
3305                 return (0);
3306         }
3307
3308         zcmd_free_nvlists(&zc);
3309         return (zpool_standard_error(hdl, errno, msg));
3310 }
3311
3312 /*
3313  * Similar to zpool_clear(), but takes a GUID (used by fmd).
3314  */
3315 int
3316 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3317 {
3318         zfs_cmd_t zc = { 0 };
3319         char msg[1024];
3320         libzfs_handle_t *hdl = zhp->zpool_hdl;
3321
3322         (void) snprintf(msg, sizeof (msg),
3323             dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3324             guid);
3325
3326         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3327         zc.zc_guid = guid;
3328         zc.zc_cookie = ZPOOL_NO_REWIND;
3329
3330         if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3331                 return (0);
3332
3333         return (zpool_standard_error(hdl, errno, msg));
3334 }
3335
3336 /*
3337  * Change the GUID for a pool.
3338  */
3339 int
3340 zpool_reguid(zpool_handle_t *zhp)
3341 {
3342         char msg[1024];
3343         libzfs_handle_t *hdl = zhp->zpool_hdl;
3344         zfs_cmd_t zc = { 0 };
3345
3346         (void) snprintf(msg, sizeof (msg),
3347             dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3348
3349         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3350         if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3351                 return (0);
3352
3353         return (zpool_standard_error(hdl, errno, msg));
3354 }
3355
3356 /*
3357  * Reopen the pool.
3358  */
3359 int
3360 zpool_reopen(zpool_handle_t *zhp)
3361 {
3362         zfs_cmd_t zc = { 0 };
3363         char msg[1024];
3364         libzfs_handle_t *hdl = zhp->zpool_hdl;
3365
3366         (void) snprintf(msg, sizeof (msg),
3367             dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3368             zhp->zpool_name);
3369
3370         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3371         if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3372                 return (0);
3373         return (zpool_standard_error(hdl, errno, msg));
3374 }
3375
3376 /*
3377  * Convert from a devid string to a path.
3378  */
3379 static char *
3380 devid_to_path(char *devid_str)
3381 {
3382         ddi_devid_t devid;
3383         char *minor;
3384         char *path;
3385         devid_nmlist_t *list = NULL;
3386         int ret;
3387
3388         if (devid_str_decode(devid_str, &devid, &minor) != 0)
3389                 return (NULL);
3390
3391         ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3392
3393         devid_str_free(minor);
3394         devid_free(devid);
3395
3396         if (ret != 0)
3397                 return (NULL);
3398
3399         /*
3400          * In a case the strdup() fails, we will just return NULL below.
3401          */
3402         path = strdup(list[0].devname);
3403
3404         devid_free_nmlist(list);
3405
3406         return (path);
3407 }
3408
3409 /*
3410  * Convert from a path to a devid string.
3411  */
3412 static char *
3413 path_to_devid(const char *path)
3414 {
3415 #ifdef have_devid
3416         int fd;
3417         ddi_devid_t devid;
3418         char *minor, *ret;
3419
3420         if ((fd = open(path, O_RDONLY)) < 0)
3421                 return (NULL);
3422
3423         minor = NULL;
3424         ret = NULL;
3425         if (devid_get(fd, &devid) == 0) {
3426                 if (devid_get_minor_name(fd, &minor) == 0)
3427                         ret = devid_str_encode(devid, minor);
3428                 if (minor != NULL)
3429                         devid_str_free(minor);
3430                 devid_free(devid);
3431         }
3432         (void) close(fd);
3433
3434         return (ret);
3435 #else
3436         return (NULL);
3437 #endif
3438 }
3439
3440 /*
3441  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
3442  * ignore any failure here, since a common case is for an unprivileged user to
3443  * type 'zpool status', and we'll display the correct information anyway.
3444  */
3445 static void
3446 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3447 {
3448         zfs_cmd_t zc = { 0 };
3449
3450         (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3451         (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3452         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3453             &zc.zc_guid) == 0);
3454
3455         (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3456 }
3457
3458 /*
3459  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
3460  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3461  * We also check if this is a whole disk, in which case we strip off the
3462  * trailing 's0' slice name.
3463  *
3464  * This routine is also responsible for identifying when disks have been
3465  * reconfigured in a new location.  The kernel will have opened the device by
3466  * devid, but the path will still refer to the old location.  To catch this, we
3467  * first do a path -> devid translation (which is fast for the common case).  If
3468  * the devid matches, we're done.  If not, we do a reverse devid -> path
3469  * translation and issue the appropriate ioctl() to update the path of the vdev.
3470  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3471  * of these checks.
3472  */
3473 char *
3474 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3475     boolean_t verbose)
3476 {
3477         char *path, *devid;
3478         uint64_t value;
3479         char buf[64];
3480         vdev_stat_t *vs;
3481         uint_t vsc;
3482         int have_stats;
3483         int have_path;
3484
3485         have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3486             (uint64_t **)&vs, &vsc) == 0;
3487         have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3488
3489         /*
3490          * If the device is not currently present, assume it will not
3491          * come back at the same device path.  Display the device by GUID.
3492          */
3493         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3494             have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3495                 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3496                     &value) == 0);
3497                 (void) snprintf(buf, sizeof (buf), "%llu",
3498                     (u_longlong_t)value);
3499                 path = buf;
3500         } else if (have_path) {
3501
3502                 /*
3503                  * If the device is dead (faulted, offline, etc) then don't
3504                  * bother opening it.  Otherwise we may be forcing the user to
3505                  * open a misbehaving device, which can have undesirable
3506                  * effects.
3507                  */
3508                 if ((have_stats == 0 ||
3509                     vs->vs_state >= VDEV_STATE_DEGRADED) &&
3510                     zhp != NULL &&
3511                     nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3512                         /*
3513                          * Determine if the current path is correct.
3514                          */
3515                         char *newdevid = path_to_devid(path);
3516
3517                         if (newdevid == NULL ||
3518                             strcmp(devid, newdevid) != 0) {
3519                                 char *newpath;
3520
3521                                 if ((newpath = devid_to_path(devid)) != NULL) {
3522                                         /*
3523                                          * Update the path appropriately.
3524                                          */
3525                                         set_path(zhp, nv, newpath);
3526                                         if (nvlist_add_string(nv,
3527                                             ZPOOL_CONFIG_PATH, newpath) == 0)
3528                                                 verify(nvlist_lookup_string(nv,
3529                                                     ZPOOL_CONFIG_PATH,
3530                                                     &path) == 0);
3531                                         free(newpath);
3532                                 }
3533                         }
3534
3535                         if (newdevid)
3536                                 devid_str_free(newdevid);
3537                 }
3538
3539 #ifdef illumos
3540                 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3541                         path += strlen(ZFS_DISK_ROOTD);
3542
3543                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3544                     &value) == 0 && value) {
3545                         int pathlen = strlen(path);
3546                         char *tmp = zfs_strdup(hdl, path);
3547
3548                         /*
3549                          * If it starts with c#, and ends with "s0" or "s1",
3550                          * chop the slice off, or if it ends with "s0/old" or
3551                          * "s1/old", remove the slice from the middle.
3552                          */
3553                         if (CTD_CHECK(tmp)) {
3554                                 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3555                                     strcmp(&tmp[pathlen - 2], "s1") == 0) {
3556                                         tmp[pathlen - 2] = '\0';
3557                                 } else if (pathlen > 6 &&
3558                                     (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3559                                     strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3560                                         (void) strcpy(&tmp[pathlen - 6],
3561                                             "/old");
3562                                 }
3563                         }
3564                         return (tmp);
3565                 }
3566 #else   /* !illumos */
3567                 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3568                         path += sizeof(_PATH_DEV) - 1;
3569 #endif  /* illumos */
3570         } else {
3571                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3572
3573                 /*
3574                  * If it's a raidz device, we need to stick in the parity level.
3575                  */
3576                 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3577                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3578                             &value) == 0);
3579                         (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3580                             (u_longlong_t)value);
3581                         path = buf;
3582                 }
3583
3584                 /*
3585                  * We identify each top-level vdev by using a <type-id>
3586                  * naming convention.
3587                  */
3588                 if (verbose) {
3589                         uint64_t id;
3590
3591                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3592                             &id) == 0);
3593                         (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3594                             (u_longlong_t)id);
3595                         path = buf;
3596                 }
3597         }
3598
3599         return (zfs_strdup(hdl, path));
3600 }
3601
3602 static int
3603 zbookmark_mem_compare(const void *a, const void *b)
3604 {
3605         return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3606 }
3607
3608 /*
3609  * Retrieve the persistent error log, uniquify the members, and return to the
3610  * caller.
3611  */
3612 int
3613 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3614 {
3615         zfs_cmd_t zc = { 0 };
3616         uint64_t count;
3617         zbookmark_phys_t *zb = NULL;
3618         int i;
3619
3620         /*
3621          * Retrieve the raw error list from the kernel.  If the number of errors
3622          * has increased, allocate more space and continue until we get the
3623          * entire list.
3624          */
3625         verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3626             &count) == 0);
3627         if (count == 0)
3628                 return (0);
3629         if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3630             count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3631                 return (-1);
3632         zc.zc_nvlist_dst_size = count;
3633         (void) strcpy(zc.zc_name, zhp->zpool_name);
3634         for (;;) {
3635                 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3636                     &zc) != 0) {
3637                         free((void *)(uintptr_t)zc.zc_nvlist_dst);
3638                         if (errno == ENOMEM) {
3639                                 void *dst;
3640
3641                                 count = zc.zc_nvlist_dst_size;
3642                                 dst = zfs_alloc(zhp->zpool_hdl, count *
3643                                     sizeof (zbookmark_phys_t));
3644                                 if (dst == NULL)
3645                                         return (-1);
3646                                 zc.zc_nvlist_dst = (uintptr_t)dst;
3647                         } else {
3648                                 return (-1);
3649                         }
3650                 } else {
3651                         break;
3652                 }
3653         }
3654
3655         /*
3656          * Sort the resulting bookmarks.  This is a little confusing due to the
3657          * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
3658          * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3659          * _not_ copied as part of the process.  So we point the start of our
3660          * array appropriate and decrement the total number of elements.
3661          */
3662         zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3663             zc.zc_nvlist_dst_size;
3664         count -= zc.zc_nvlist_dst_size;
3665
3666         qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3667
3668         verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3669
3670         /*
3671          * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3672          */
3673         for (i = 0; i < count; i++) {
3674                 nvlist_t *nv;
3675
3676                 /* ignoring zb_blkid and zb_level for now */
3677                 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3678                     zb[i-1].zb_object == zb[i].zb_object)
3679                         continue;
3680
3681                 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3682                         goto nomem;
3683                 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3684                     zb[i].zb_objset) != 0) {
3685                         nvlist_free(nv);
3686                         goto nomem;
3687                 }
3688                 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3689                     zb[i].zb_object) != 0) {
3690                         nvlist_free(nv);
3691                         goto nomem;
3692                 }
3693                 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3694                         nvlist_free(nv);
3695                         goto nomem;
3696                 }
3697                 nvlist_free(nv);
3698         }
3699
3700         free((void *)(uintptr_t)zc.zc_nvlist_dst);
3701         return (0);
3702
3703 nomem:
3704         free((void *)(uintptr_t)zc.zc_nvlist_dst);
3705         return (no_memory(zhp->zpool_hdl));
3706 }
3707
3708 /*
3709  * Upgrade a ZFS pool to the latest on-disk version.
3710  */
3711 int
3712 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3713 {
3714         zfs_cmd_t zc = { 0 };
3715         libzfs_handle_t *hdl = zhp->zpool_hdl;
3716
3717         (void) strcpy(zc.zc_name, zhp->zpool_name);
3718         zc.zc_cookie = new_version;
3719
3720         if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3721                 return (zpool_standard_error_fmt(hdl, errno,
3722                     dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3723                     zhp->zpool_name));
3724         return (0);
3725 }
3726
3727 void
3728 zfs_save_arguments(int argc, char **argv, char *string, int len)
3729 {
3730         (void) strlcpy(string, basename(argv[0]), len);
3731         for (int i = 1; i < argc; i++) {
3732                 (void) strlcat(string, " ", len);
3733                 (void) strlcat(string, argv[i], len);
3734         }
3735 }
3736
3737 int
3738 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3739 {
3740         zfs_cmd_t zc = { 0 };
3741         nvlist_t *args;
3742         int err;
3743
3744         args = fnvlist_alloc();
3745         fnvlist_add_string(args, "message", message);
3746         err = zcmd_write_src_nvlist(hdl, &zc, args);
3747         if (err == 0)
3748                 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3749         nvlist_free(args);
3750         zcmd_free_nvlists(&zc);
3751         return (err);
3752 }
3753
3754 /*
3755  * Perform ioctl to get some command history of a pool.
3756  *
3757  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
3758  * logical offset of the history buffer to start reading from.
3759  *
3760  * Upon return, 'off' is the next logical offset to read from and
3761  * 'len' is the actual amount of bytes read into 'buf'.
3762  */
3763 static int
3764 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3765 {
3766         zfs_cmd_t zc = { 0 };
3767         libzfs_handle_t *hdl = zhp->zpool_hdl;
3768
3769         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3770
3771         zc.zc_history = (uint64_t)(uintptr_t)buf;
3772         zc.zc_history_len = *len;
3773         zc.zc_history_offset = *off;
3774
3775         if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3776                 switch (errno) {
3777                 case EPERM:
3778                         return (zfs_error_fmt(hdl, EZFS_PERM,
3779                             dgettext(TEXT_DOMAIN,
3780                             "cannot show history for pool '%s'"),
3781                             zhp->zpool_name));
3782                 case ENOENT:
3783                         return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3784                             dgettext(TEXT_DOMAIN, "cannot get history for pool "
3785                             "'%s'"), zhp->zpool_name));
3786                 case ENOTSUP:
3787                         return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3788                             dgettext(TEXT_DOMAIN, "cannot get history for pool "
3789                             "'%s', pool must be upgraded"), zhp->zpool_name));
3790                 default:
3791                         return (zpool_standard_error_fmt(hdl, errno,
3792                             dgettext(TEXT_DOMAIN,
3793                             "cannot get history for '%s'"), zhp->zpool_name));
3794                 }
3795         }
3796
3797         *len = zc.zc_history_len;
3798         *off = zc.zc_history_offset;
3799
3800         return (0);
3801 }
3802
3803 /*
3804  * Process the buffer of nvlists, unpacking and storing each nvlist record
3805  * into 'records'.  'leftover' is set to the number of bytes that weren't
3806  * processed as there wasn't a complete record.
3807  */
3808 int
3809 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3810     nvlist_t ***records, uint_t *numrecords)
3811 {
3812         uint64_t reclen;
3813         nvlist_t *nv;
3814         int i;
3815
3816         while (bytes_read > sizeof (reclen)) {
3817
3818                 /* get length of packed record (stored as little endian) */
3819                 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3820                         reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3821
3822                 if (bytes_read < sizeof (reclen) + reclen)
3823                         break;
3824
3825                 /* unpack record */
3826                 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3827                         return (ENOMEM);
3828                 bytes_read -= sizeof (reclen) + reclen;
3829                 buf += sizeof (reclen) + reclen;
3830
3831                 /* add record to nvlist array */
3832                 (*numrecords)++;
3833                 if (ISP2(*numrecords + 1)) {
3834                         *records = realloc(*records,
3835                             *numrecords * 2 * sizeof (nvlist_t *));
3836                 }
3837                 (*records)[*numrecords - 1] = nv;
3838         }
3839
3840         *leftover = bytes_read;
3841         return (0);
3842 }
3843
3844 /* from spa_history.c: spa_history_create_obj() */
3845 #define HIS_BUF_LEN_DEF (128 << 10)
3846 #define HIS_BUF_LEN_MAX (1 << 30)
3847
3848 /*
3849  * Retrieve the command history of a pool.
3850  */
3851 int
3852 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3853 {
3854         char *buf;
3855         uint64_t buflen = HIS_BUF_LEN_DEF;
3856         uint64_t off = 0;
3857         nvlist_t **records = NULL;
3858         uint_t numrecords = 0;
3859         int err, i;
3860
3861         buf = malloc(buflen);
3862         if (buf == NULL)
3863                 return (ENOMEM);
3864         do {
3865                 uint64_t bytes_read = buflen;
3866                 uint64_t leftover;
3867
3868                 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3869                         break;
3870
3871                 /* if nothing else was read in, we're at EOF, just return */
3872                 if (bytes_read == 0)
3873                         break;
3874
3875                 if ((err = zpool_history_unpack(buf, bytes_read,
3876                     &leftover, &records, &numrecords)) != 0)
3877                         break;
3878                 off -= leftover;
3879                 if (leftover == bytes_read) {
3880                         /*
3881                          * no progress made, because buffer is not big enough
3882                          * to hold this record; resize and retry.
3883                          */
3884                         buflen *= 2;
3885                         free(buf);
3886                         buf = NULL;
3887                         if ((buflen >= HIS_BUF_LEN_MAX) ||
3888                             ((buf = malloc(buflen)) == NULL)) {
3889                                 err = ENOMEM;
3890                                 break;
3891                         }
3892                 }
3893
3894                 /* CONSTCOND */
3895         } while (1);
3896
3897         free(buf);
3898
3899         if (!err) {
3900                 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3901                 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3902                     records, numrecords) == 0);
3903         }
3904         for (i = 0; i < numrecords; i++)
3905                 nvlist_free(records[i]);
3906         free(records);
3907
3908         return (err);
3909 }
3910
3911 void
3912 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3913     char *pathname, size_t len)
3914 {
3915         zfs_cmd_t zc = { 0 };
3916         boolean_t mounted = B_FALSE;
3917         char *mntpnt = NULL;
3918         char dsname[ZFS_MAX_DATASET_NAME_LEN];
3919
3920         if (dsobj == 0) {
3921                 /* special case for the MOS */
3922                 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3923                 return;
3924         }
3925
3926         /* get the dataset's name */
3927         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3928         zc.zc_obj = dsobj;
3929         if (ioctl(zhp->zpool_hdl->libzfs_fd,
3930             ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3931                 /* just write out a path of two object numbers */
3932                 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3933                     dsobj, obj);
3934                 return;
3935         }
3936         (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3937
3938         /* find out if the dataset is mounted */
3939         mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3940
3941         /* get the corrupted object's path */
3942         (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3943         zc.zc_obj = obj;
3944         if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3945             &zc) == 0) {
3946                 if (mounted) {
3947                         (void) snprintf(pathname, len, "%s%s", mntpnt,
3948                             zc.zc_value);
3949                 } else {
3950                         (void) snprintf(pathname, len, "%s:%s",
3951                             dsname, zc.zc_value);
3952                 }
3953         } else {
3954                 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3955         }
3956         free(mntpnt);
3957 }
3958
3959 #ifdef illumos
3960 /*
3961  * Read the EFI label from the config, if a label does not exist then
3962  * pass back the error to the caller. If the caller has passed a non-NULL
3963  * diskaddr argument then we set it to the starting address of the EFI
3964  * partition. If the caller has passed a non-NULL boolean argument, then
3965  * we set it to indicate if the disk does have efi system partition.
3966  */
3967 static int
3968 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
3969 {
3970         char *path;
3971         int fd;
3972         char diskname[MAXPATHLEN];
3973         boolean_t boot = B_FALSE;
3974         int err = -1;
3975         int slice;
3976
3977         if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3978                 return (err);
3979
3980         (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3981             strrchr(path, '/'));
3982         if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3983                 struct dk_gpt *vtoc;
3984
3985                 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3986                         for (slice = 0; slice < vtoc->efi_nparts; slice++) {
3987                                 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
3988                                         boot = B_TRUE;
3989                                 if (vtoc->efi_parts[slice].p_tag == V_USR)
3990                                         break;
3991                         }
3992                         if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
3993                                 *sb = vtoc->efi_parts[slice].p_start;
3994                         if (system != NULL)
3995                                 *system = boot;
3996                         efi_free(vtoc);
3997                 }
3998                 (void) close(fd);
3999         }
4000         return (err);
4001 }
4002
4003 /*
4004  * determine where a partition starts on a disk in the current
4005  * configuration
4006  */
4007 static diskaddr_t
4008 find_start_block(nvlist_t *config)
4009 {
4010         nvlist_t **child;
4011         uint_t c, children;
4012         diskaddr_t sb = MAXOFFSET_T;
4013         uint64_t wholedisk;
4014
4015         if (nvlist_lookup_nvlist_array(config,
4016             ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4017                 if (nvlist_lookup_uint64(config,
4018                     ZPOOL_CONFIG_WHOLE_DISK,
4019                     &wholedisk) != 0 || !wholedisk) {
4020                         return (MAXOFFSET_T);
4021                 }
4022                 if (read_efi_label(config, &sb, NULL) < 0)
4023                         sb = MAXOFFSET_T;
4024                 return (sb);
4025         }
4026
4027         for (c = 0; c < children; c++) {
4028                 sb = find_start_block(child[c]);
4029                 if (sb != MAXOFFSET_T) {
4030                         return (sb);
4031                 }
4032         }
4033         return (MAXOFFSET_T);
4034 }
4035 #endif /* illumos */
4036
4037 /*
4038  * Label an individual disk.  The name provided is the short name,
4039  * stripped of any leading /dev path.
4040  */
4041 int
4042 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4043     zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4044 {
4045 #ifdef illumos
4046         char path[MAXPATHLEN];
4047         struct dk_gpt *vtoc;
4048         int fd;
4049         size_t resv = EFI_MIN_RESV_SIZE;
4050         uint64_t slice_size;
4051         diskaddr_t start_block;
4052         char errbuf[1024];
4053
4054         /* prepare an error message just in case */
4055         (void) snprintf(errbuf, sizeof (errbuf),
4056             dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4057
4058         if (zhp) {
4059                 nvlist_t *nvroot;
4060
4061                 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4062                     ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4063
4064                 if (zhp->zpool_start_block == 0)
4065                         start_block = find_start_block(nvroot);
4066                 else
4067                         start_block = zhp->zpool_start_block;
4068                 zhp->zpool_start_block = start_block;
4069         } else {
4070                 /* new pool */
4071                 start_block = NEW_START_BLOCK;
4072         }
4073
4074         (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4075             BACKUP_SLICE);
4076
4077         if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4078                 /*
4079                  * This shouldn't happen.  We've long since verified that this
4080                  * is a valid device.
4081                  */
4082                 zfs_error_aux(hdl,
4083                     dgettext(TEXT_DOMAIN, "unable to open device"));
4084                 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4085         }
4086
4087         if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4088                 /*
4089                  * The only way this can fail is if we run out of memory, or we
4090                  * were unable to read the disk's capacity
4091                  */
4092                 if (errno == ENOMEM)
4093                         (void) no_memory(hdl);
4094
4095                 (void) close(fd);
4096                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4097                     "unable to read disk capacity"), name);
4098
4099                 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4100         }
4101
4102         /*
4103          * Why we use V_USR: V_BACKUP confuses users, and is considered
4104          * disposable by some EFI utilities (since EFI doesn't have a backup
4105          * slice).  V_UNASSIGNED is supposed to be used only for zero size
4106          * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
4107          * etc. were all pretty specific.  V_USR is as close to reality as we
4108          * can get, in the absence of V_OTHER.
4109          */
4110         /* first fix the partition start block */
4111         if (start_block == MAXOFFSET_T)
4112                 start_block = NEW_START_BLOCK;
4113
4114         /*
4115          * EFI System partition is using slice 0.
4116          * ZFS is on slice 1 and slice 8 is reserved.
4117          * We assume the GPT partition table without system
4118          * partition has zfs p_start == NEW_START_BLOCK.
4119          * If start_block != NEW_START_BLOCK, it means we have
4120          * system partition. Correct solution would be to query/cache vtoc
4121          * from existing vdev member.
4122          */
4123         if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4124                 if (boot_size % vtoc->efi_lbasize != 0) {
4125                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4126                             "boot partition size must be a multiple of %d"),
4127                             vtoc->efi_lbasize);
4128                         (void) close(fd);
4129                         efi_free(vtoc);
4130                         return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4131                 }
4132                 /*
4133                  * System partition size checks.
4134                  * Note the 1MB is quite arbitrary value, since we
4135                  * are creating dedicated pool, it should be enough
4136                  * to hold fat + efi bootloader. May need to be
4137                  * adjusted if the bootloader size will grow.
4138                  */
4139                 if (boot_size < 1024 * 1024) {
4140                         char buf[64];
4141                         zfs_nicenum(boot_size, buf, sizeof (buf));
4142                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4143                             "Specified size %s for EFI System partition is too "
4144                             "small, the minimum size is 1MB."), buf);
4145                         (void) close(fd);
4146                         efi_free(vtoc);
4147                         return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4148                 }
4149                 /* 33MB is tested with mkfs -F pcfs */
4150                 if (hdl->libzfs_printerr &&
4151                     ((vtoc->efi_lbasize == 512 &&
4152                     boot_size < 33 * 1024 * 1024) ||
4153                     (vtoc->efi_lbasize == 4096 &&
4154                     boot_size < 256 * 1024 * 1024)))  {
4155                         char buf[64];
4156                         zfs_nicenum(boot_size, buf, sizeof (buf));
4157                         (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4158                             "Warning: EFI System partition size %s is "
4159                             "not allowing to create FAT32 file\nsystem, which "
4160                             "may result in unbootable system.\n"), buf);
4161                 }
4162                 /* Adjust zfs partition start by size of system partition. */
4163                 start_block += boot_size / vtoc->efi_lbasize;
4164         }
4165
4166         if (start_block == NEW_START_BLOCK) {
4167                 /*
4168                  * Use default layout.
4169                  * ZFS is on slice 0 and slice 8 is reserved.
4170                  */
4171                 slice_size = vtoc->efi_last_u_lba + 1;
4172                 slice_size -= EFI_MIN_RESV_SIZE;
4173                 slice_size -= start_block;
4174                 if (slice != NULL)
4175                         *slice = 0;
4176
4177                 vtoc->efi_parts[0].p_start = start_block;
4178                 vtoc->efi_parts[0].p_size = slice_size;
4179
4180                 vtoc->efi_parts[0].p_tag = V_USR;
4181                 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4182
4183                 vtoc->efi_parts[8].p_start = slice_size + start_block;
4184                 vtoc->efi_parts[8].p_size = resv;
4185                 vtoc->efi_parts[8].p_tag = V_RESERVED;
4186         } else {
4187                 slice_size = start_block - NEW_START_BLOCK;
4188                 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4189                 vtoc->efi_parts[0].p_size = slice_size;
4190                 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4191                 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4192                 if (slice != NULL)
4193                         *slice = 1;
4194                 /* prepare slice 1 */
4195                 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4196                 slice_size -= resv;
4197                 slice_size -= NEW_START_BLOCK;
4198                 vtoc->efi_parts[1].p_start = start_block;
4199                 vtoc->efi_parts[1].p_size = slice_size;
4200                 vtoc->efi_parts[1].p_tag = V_USR;
4201                 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4202
4203                 vtoc->efi_parts[8].p_start = slice_size + start_block;
4204                 vtoc->efi_parts[8].p_size = resv;
4205                 vtoc->efi_parts[8].p_tag = V_RESERVED;
4206         }
4207
4208         if (efi_write(fd, vtoc) != 0) {
4209                 /*
4210                  * Some block drivers (like pcata) may not support EFI
4211                  * GPT labels.  Print out a helpful error message dir-
4212                  * ecting the user to manually label the disk and give
4213                  * a specific slice.
4214                  */
4215                 (void) close(fd);
4216                 efi_free(vtoc);
4217
4218                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4219                     "try using fdisk(1M) and then provide a specific slice"));
4220                 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4221         }
4222
4223         (void) close(fd);
4224         efi_free(vtoc);
4225 #endif /* illumos */
4226         return (0);
4227 }
4228
4229 static boolean_t
4230 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4231 {
4232         char *type;
4233         nvlist_t **child;
4234         uint_t children, c;
4235
4236         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4237         if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4238             strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4239             strcmp(type, VDEV_TYPE_MISSING) == 0) {
4240                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4241                     "vdev type '%s' is not supported"), type);
4242                 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4243                 return (B_FALSE);
4244         }
4245         if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4246             &child, &children) == 0) {
4247                 for (c = 0; c < children; c++) {
4248                         if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4249                                 return (B_FALSE);
4250                 }
4251         }
4252         return (B_TRUE);
4253 }
4254
4255 /*
4256  * Check if this zvol is allowable for use as a dump device; zero if
4257  * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4258  *
4259  * Allowable storage configurations include mirrors, all raidz variants, and
4260  * pools with log, cache, and spare devices.  Pools which are backed by files or
4261  * have missing/hole vdevs are not suitable.
4262  */
4263 int
4264 zvol_check_dump_config(char *arg)
4265 {
4266         zpool_handle_t *zhp = NULL;
4267         nvlist_t *config, *nvroot;
4268         char *p, *volname;
4269         nvlist_t **top;
4270         uint_t toplevels;
4271         libzfs_handle_t *hdl;
4272         char errbuf[1024];
4273         char poolname[ZFS_MAX_DATASET_NAME_LEN];
4274         int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4275         int ret = 1;
4276
4277         if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4278                 return (-1);
4279         }
4280
4281         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4282             "dump is not supported on device '%s'"), arg);
4283
4284         if ((hdl = libzfs_init()) == NULL)
4285                 return (1);
4286         libzfs_print_on_error(hdl, B_TRUE);
4287
4288         volname = arg + pathlen;
4289
4290         /* check the configuration of the pool */
4291         if ((p = strchr(volname, '/')) == NULL) {
4292                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4293                     "malformed dataset name"));
4294                 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4295                 return (1);
4296         } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4297                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4298                     "dataset name is too long"));
4299                 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4300                 return (1);
4301         } else {
4302                 (void) strncpy(poolname, volname, p - volname);
4303                 poolname[p - volname] = '\0';
4304         }
4305
4306         if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4307                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4308                     "could not open pool '%s'"), poolname);
4309                 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4310                 goto out;
4311         }
4312         config = zpool_get_config(zhp, NULL);
4313         if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4314             &nvroot) != 0) {
4315                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4316                     "could not obtain vdev configuration for  '%s'"), poolname);
4317                 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4318                 goto out;
4319         }
4320
4321         verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4322             &top, &toplevels) == 0);
4323
4324         if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4325                 goto out;
4326         }
4327         ret = 0;
4328
4329 out:
4330         if (zhp)
4331                 zpool_close(zhp);
4332         libzfs_fini(hdl);
4333         return (ret);
4334 }
4335
4336 int
4337 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4338     const char *command)
4339 {
4340         zfs_cmd_t zc = { 0 };
4341         nvlist_t *args;
4342         char *packed;
4343         size_t size;
4344         int error;
4345
4346         args = fnvlist_alloc();
4347         fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4348         fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4349         fnvlist_add_string(args, "command", command);
4350         error = zcmd_write_src_nvlist(hdl, &zc, args);
4351         if (error == 0)
4352                 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4353         zcmd_free_nvlists(&zc);
4354         nvlist_free(args);
4355         return (error);
4356 }