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