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