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