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