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