]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_pool.c
MFC r209962, r211970-r211972, r212050, r212605, r212611
[FreeBSD/stable/8.git] / cddl / contrib / opensolaris / lib / libzfs / common / libzfs_pool.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <devid.h>
33 #include <dirent.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 <zone.h>
41 #include <sys/zfs_ioctl.h>
42 #include <sys/zio.h>
43 #include <strings.h>
44 #include <umem.h>
45
46 #include "zfs_namecheck.h"
47 #include "zfs_prop.h"
48 #include "libzfs_impl.h"
49
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51
52 #if defined(__i386) || defined(__amd64)
53 #define BOOTCMD "installgrub(1M)"
54 #else
55 #define BOOTCMD "installboot(1M)"
56 #endif
57
58 /*
59  * ====================================================================
60  *   zpool property functions
61  * ====================================================================
62  */
63
64 static int
65 zpool_get_all_props(zpool_handle_t *zhp)
66 {
67         zfs_cmd_t zc = { 0 };
68         libzfs_handle_t *hdl = zhp->zpool_hdl;
69
70         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71
72         if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73                 return (-1);
74
75         while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
76                 if (errno == ENOMEM) {
77                         if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
78                                 zcmd_free_nvlists(&zc);
79                                 return (-1);
80                         }
81                 } else {
82                         zcmd_free_nvlists(&zc);
83                         return (-1);
84                 }
85         }
86
87         if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
88                 zcmd_free_nvlists(&zc);
89                 return (-1);
90         }
91
92         zcmd_free_nvlists(&zc);
93
94         return (0);
95 }
96
97 static int
98 zpool_props_refresh(zpool_handle_t *zhp)
99 {
100         nvlist_t *old_props;
101
102         old_props = zhp->zpool_props;
103
104         if (zpool_get_all_props(zhp) != 0)
105                 return (-1);
106
107         nvlist_free(old_props);
108         return (0);
109 }
110
111 static char *
112 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
113     zprop_source_t *src)
114 {
115         nvlist_t *nv, *nvl;
116         uint64_t ival;
117         char *value;
118         zprop_source_t source;
119
120         nvl = zhp->zpool_props;
121         if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
122                 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
123                 source = ival;
124                 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
125         } else {
126                 source = ZPROP_SRC_DEFAULT;
127                 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
128                         value = "-";
129         }
130
131         if (src)
132                 *src = source;
133
134         return (value);
135 }
136
137 uint64_t
138 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
139 {
140         nvlist_t *nv, *nvl;
141         uint64_t value;
142         zprop_source_t source;
143
144         if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
145                 /*
146                  * zpool_get_all_props() has most likely failed because
147                  * the pool is faulted, but if all we need is the top level
148                  * vdev's guid then get it from the zhp config nvlist.
149                  */
150                 if ((prop == ZPOOL_PROP_GUID) &&
151                     (nvlist_lookup_nvlist(zhp->zpool_config,
152                     ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
153                     (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
154                     == 0)) {
155                         return (value);
156                 }
157                 return (zpool_prop_default_numeric(prop));
158         }
159
160         nvl = zhp->zpool_props;
161         if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
162                 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
163                 source = value;
164                 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
165         } else {
166                 source = ZPROP_SRC_DEFAULT;
167                 value = zpool_prop_default_numeric(prop);
168         }
169
170         if (src)
171                 *src = source;
172
173         return (value);
174 }
175
176 /*
177  * Map VDEV STATE to printed strings.
178  */
179 char *
180 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
181 {
182         switch (state) {
183         case VDEV_STATE_CLOSED:
184         case VDEV_STATE_OFFLINE:
185                 return (gettext("OFFLINE"));
186         case VDEV_STATE_REMOVED:
187                 return (gettext("REMOVED"));
188         case VDEV_STATE_CANT_OPEN:
189                 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190                         return (gettext("FAULTED"));
191                 else
192                         return (gettext("UNAVAIL"));
193         case VDEV_STATE_FAULTED:
194                 return (gettext("FAULTED"));
195         case VDEV_STATE_DEGRADED:
196                 return (gettext("DEGRADED"));
197         case VDEV_STATE_HEALTHY:
198                 return (gettext("ONLINE"));
199         }
200
201         return (gettext("UNKNOWN"));
202 }
203
204 /*
205  * Get a zpool property value for 'prop' and return the value in
206  * a pre-allocated buffer.
207  */
208 int
209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
210     zprop_source_t *srctype)
211 {
212         uint64_t intval;
213         const char *strval;
214         zprop_source_t src = ZPROP_SRC_NONE;
215         nvlist_t *nvroot;
216         vdev_stat_t *vs;
217         uint_t vsc;
218
219         if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
220                 switch (prop) {
221                 case ZPOOL_PROP_NAME:
222                         (void) strlcpy(buf, zpool_get_name(zhp), len);
223                         break;
224
225                 case ZPOOL_PROP_HEALTH:
226                         (void) strlcpy(buf, "FAULTED", len);
227                         break;
228
229                 case ZPOOL_PROP_GUID:
230                         intval = zpool_get_prop_int(zhp, prop, &src);
231                         (void) snprintf(buf, len, "%llu", intval);
232                         break;
233
234                 case ZPOOL_PROP_ALTROOT:
235                 case ZPOOL_PROP_CACHEFILE:
236                         if (zhp->zpool_props != NULL ||
237                             zpool_get_all_props(zhp) == 0) {
238                                 (void) strlcpy(buf,
239                                     zpool_get_prop_string(zhp, prop, &src),
240                                     len);
241                                 if (srctype != NULL)
242                                         *srctype = src;
243                                 return (0);
244                         }
245                         /* FALLTHROUGH */
246                 default:
247                         (void) strlcpy(buf, "-", len);
248                         break;
249                 }
250
251                 if (srctype != NULL)
252                         *srctype = src;
253                 return (0);
254         }
255
256         if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
257             prop != ZPOOL_PROP_NAME)
258                 return (-1);
259
260         switch (zpool_prop_get_type(prop)) {
261         case PROP_TYPE_STRING:
262                 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
263                     len);
264                 break;
265
266         case PROP_TYPE_NUMBER:
267                 intval = zpool_get_prop_int(zhp, prop, &src);
268
269                 switch (prop) {
270                 case ZPOOL_PROP_SIZE:
271                 case ZPOOL_PROP_USED:
272                 case ZPOOL_PROP_AVAILABLE:
273                         (void) zfs_nicenum(intval, buf, len);
274                         break;
275
276                 case ZPOOL_PROP_CAPACITY:
277                         (void) snprintf(buf, len, "%llu%%",
278                             (u_longlong_t)intval);
279                         break;
280
281                 case ZPOOL_PROP_HEALTH:
282                         verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
283                             ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
284                         verify(nvlist_lookup_uint64_array(nvroot,
285                             ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
286
287                         (void) strlcpy(buf, zpool_state_to_name(intval,
288                             vs->vs_aux), len);
289                         break;
290                 default:
291                         (void) snprintf(buf, len, "%llu", intval);
292                 }
293                 break;
294
295         case PROP_TYPE_INDEX:
296                 intval = zpool_get_prop_int(zhp, prop, &src);
297                 if (zpool_prop_index_to_string(prop, intval, &strval)
298                     != 0)
299                         return (-1);
300                 (void) strlcpy(buf, strval, len);
301                 break;
302
303         default:
304                 abort();
305         }
306
307         if (srctype)
308                 *srctype = src;
309
310         return (0);
311 }
312
313 static boolean_t
314 pool_is_bootable(zpool_handle_t *zhp)
315 {
316         char bootfs[ZPOOL_MAXNAMELEN];
317
318         return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
319             sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
320             sizeof (bootfs)) != 0);
321 }
322
323
324 /*
325  * Check if the bootfs name has the same pool name as it is set to.
326  * Assuming bootfs is a valid dataset name.
327  */
328 static boolean_t
329 bootfs_name_valid(const char *pool, char *bootfs)
330 {
331         int len = strlen(pool);
332
333         if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
334                 return (B_FALSE);
335
336         if (strncmp(pool, bootfs, len) == 0 &&
337             (bootfs[len] == '/' || bootfs[len] == '\0'))
338                 return (B_TRUE);
339
340         return (B_FALSE);
341 }
342
343 /*
344  * Inspect the configuration to determine if any of the devices contain
345  * an EFI label.
346  */
347 static boolean_t
348 pool_uses_efi(nvlist_t *config)
349 {
350 #ifdef sun
351         nvlist_t **child;
352         uint_t c, children;
353
354         if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
355             &child, &children) != 0)
356                 return (read_efi_label(config, NULL) >= 0);
357
358         for (c = 0; c < children; c++) {
359                 if (pool_uses_efi(child[c]))
360                         return (B_TRUE);
361         }
362 #endif  /* sun */
363         return (B_FALSE);
364 }
365
366 /*
367  * Given an nvlist of zpool properties to be set, validate that they are
368  * correct, and parse any numeric properties (index, boolean, etc) if they are
369  * specified as strings.
370  */
371 static nvlist_t *
372 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
373     nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
374 {
375         nvpair_t *elem;
376         nvlist_t *retprops;
377         zpool_prop_t prop;
378         char *strval;
379         uint64_t intval;
380         char *slash;
381         struct stat64 statbuf;
382         zpool_handle_t *zhp;
383         nvlist_t *nvroot;
384
385         if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
386                 (void) no_memory(hdl);
387                 return (NULL);
388         }
389
390         elem = NULL;
391         while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
392                 const char *propname = nvpair_name(elem);
393
394                 /*
395                  * Make sure this property is valid and applies to this type.
396                  */
397                 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
398                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399                             "invalid property '%s'"), propname);
400                         (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
401                         goto error;
402                 }
403
404                 if (zpool_prop_readonly(prop)) {
405                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
406                             "is readonly"), propname);
407                         (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
408                         goto error;
409                 }
410
411                 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
412                     &strval, &intval, errbuf) != 0)
413                         goto error;
414
415                 /*
416                  * Perform additional checking for specific properties.
417                  */
418                 switch (prop) {
419                 case ZPOOL_PROP_VERSION:
420                         if (intval < version || intval > SPA_VERSION) {
421                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
422                                     "property '%s' number %d is invalid."),
423                                     propname, intval);
424                                 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
425                                 goto error;
426                         }
427                         break;
428
429                 case ZPOOL_PROP_BOOTFS:
430                         if (create_or_import) {
431                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
432                                     "property '%s' cannot be set at creation "
433                                     "or import time"), propname);
434                                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
435                                 goto error;
436                         }
437
438                         if (version < SPA_VERSION_BOOTFS) {
439                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440                                     "pool must be upgraded to support "
441                                     "'%s' property"), propname);
442                                 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
443                                 goto error;
444                         }
445
446                         /*
447                          * bootfs property value has to be a dataset name and
448                          * the dataset has to be in the same pool as it sets to.
449                          */
450                         if (strval[0] != '\0' && !bootfs_name_valid(poolname,
451                             strval)) {
452                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
453                                     "is an invalid name"), strval);
454                                 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
455                                 goto error;
456                         }
457
458                         if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
459                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460                                     "could not open pool '%s'"), poolname);
461                                 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
462                                 goto error;
463                         }
464                         verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
465                             ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
466
467 #if defined(sun)
468                         /*
469                          * bootfs property cannot be set on a disk which has
470                          * been EFI labeled.
471                          */
472                         if (pool_uses_efi(nvroot)) {
473                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474                                     "property '%s' not supported on "
475                                     "EFI labeled devices"), propname);
476                                 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
477                                 zpool_close(zhp);
478                                 goto error;
479                         }
480 #endif
481                         zpool_close(zhp);
482                         break;
483
484                 case ZPOOL_PROP_ALTROOT:
485                         if (!create_or_import) {
486                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
487                                     "property '%s' can only be set during pool "
488                                     "creation or import"), propname);
489                                 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
490                                 goto error;
491                         }
492
493                         if (strval[0] != '/') {
494                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495                                     "bad alternate root '%s'"), strval);
496                                 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
497                                 goto error;
498                         }
499                         break;
500
501                 case ZPOOL_PROP_CACHEFILE:
502                         if (strval[0] == '\0')
503                                 break;
504
505                         if (strcmp(strval, "none") == 0)
506                                 break;
507
508                         if (strval[0] != '/') {
509                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
510                                     "property '%s' must be empty, an "
511                                     "absolute path, or 'none'"), propname);
512                                 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
513                                 goto error;
514                         }
515
516                         slash = strrchr(strval, '/');
517
518                         if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
519                             strcmp(slash, "/..") == 0) {
520                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521                                     "'%s' is not a valid file"), strval);
522                                 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
523                                 goto error;
524                         }
525
526                         *slash = '\0';
527
528                         if (strval[0] != '\0' &&
529                             (stat64(strval, &statbuf) != 0 ||
530                             !S_ISDIR(statbuf.st_mode))) {
531                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532                                     "'%s' is not a valid directory"),
533                                     strval);
534                                 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
535                                 goto error;
536                         }
537
538                         *slash = '/';
539                         break;
540                 }
541         }
542
543         return (retprops);
544 error:
545         nvlist_free(retprops);
546         return (NULL);
547 }
548
549 /*
550  * Set zpool property : propname=propval.
551  */
552 int
553 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
554 {
555         zfs_cmd_t zc = { 0 };
556         int ret = -1;
557         char errbuf[1024];
558         nvlist_t *nvl = NULL;
559         nvlist_t *realprops;
560         uint64_t version;
561
562         (void) snprintf(errbuf, sizeof (errbuf),
563             dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
564             zhp->zpool_name);
565
566         if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
567                 return (no_memory(zhp->zpool_hdl));
568
569         if (nvlist_add_string(nvl, propname, propval) != 0) {
570                 nvlist_free(nvl);
571                 return (no_memory(zhp->zpool_hdl));
572         }
573
574         version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
575         if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
576             zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
577                 nvlist_free(nvl);
578                 return (-1);
579         }
580
581         nvlist_free(nvl);
582         nvl = realprops;
583
584         /*
585          * Execute the corresponding ioctl() to set this property.
586          */
587         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
588
589         if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
590                 nvlist_free(nvl);
591                 return (-1);
592         }
593
594         ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
595
596         zcmd_free_nvlists(&zc);
597         nvlist_free(nvl);
598
599         if (ret)
600                 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
601         else
602                 (void) zpool_props_refresh(zhp);
603
604         return (ret);
605 }
606
607 int
608 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
609 {
610         libzfs_handle_t *hdl = zhp->zpool_hdl;
611         zprop_list_t *entry;
612         char buf[ZFS_MAXPROPLEN];
613
614         if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
615                 return (-1);
616
617         for (entry = *plp; entry != NULL; entry = entry->pl_next) {
618
619                 if (entry->pl_fixed)
620                         continue;
621
622                 if (entry->pl_prop != ZPROP_INVAL &&
623                     zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
624                     NULL) == 0) {
625                         if (strlen(buf) > entry->pl_width)
626                                 entry->pl_width = strlen(buf);
627                 }
628         }
629
630         return (0);
631 }
632
633
634 /*
635  * Validate the given pool name, optionally putting an extended error message in
636  * 'buf'.
637  */
638 boolean_t
639 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
640 {
641         namecheck_err_t why;
642         char what;
643         int ret;
644
645         ret = pool_namecheck(pool, &why, &what);
646
647         /*
648          * The rules for reserved pool names were extended at a later point.
649          * But we need to support users with existing pools that may now be
650          * invalid.  So we only check for this expanded set of names during a
651          * create (or import), and only in userland.
652          */
653         if (ret == 0 && !isopen &&
654             (strncmp(pool, "mirror", 6) == 0 ||
655             strncmp(pool, "raidz", 5) == 0 ||
656             strncmp(pool, "spare", 5) == 0 ||
657             strcmp(pool, "log") == 0)) {
658                 if (hdl != NULL)
659                         zfs_error_aux(hdl,
660                             dgettext(TEXT_DOMAIN, "name is reserved"));
661                 return (B_FALSE);
662         }
663
664
665         if (ret != 0) {
666                 if (hdl != NULL) {
667                         switch (why) {
668                         case NAME_ERR_TOOLONG:
669                                 zfs_error_aux(hdl,
670                                     dgettext(TEXT_DOMAIN, "name is too long"));
671                                 break;
672
673                         case NAME_ERR_INVALCHAR:
674                                 zfs_error_aux(hdl,
675                                     dgettext(TEXT_DOMAIN, "invalid character "
676                                     "'%c' in pool name"), what);
677                                 break;
678
679                         case NAME_ERR_NOLETTER:
680                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
681                                     "name must begin with a letter"));
682                                 break;
683
684                         case NAME_ERR_RESERVED:
685                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
686                                     "name is reserved"));
687                                 break;
688
689                         case NAME_ERR_DISKLIKE:
690                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
691                                     "pool name is reserved"));
692                                 break;
693
694                         case NAME_ERR_LEADING_SLASH:
695                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
696                                     "leading slash in name"));
697                                 break;
698
699                         case NAME_ERR_EMPTY_COMPONENT:
700                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
701                                     "empty component in name"));
702                                 break;
703
704                         case NAME_ERR_TRAILING_SLASH:
705                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
706                                     "trailing slash in name"));
707                                 break;
708
709                         case NAME_ERR_MULTIPLE_AT:
710                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711                                     "multiple '@' delimiters in name"));
712                                 break;
713
714                         }
715                 }
716                 return (B_FALSE);
717         }
718
719         return (B_TRUE);
720 }
721
722 /*
723  * Open a handle to the given pool, even if the pool is currently in the FAULTED
724  * state.
725  */
726 zpool_handle_t *
727 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
728 {
729         zpool_handle_t *zhp;
730         boolean_t missing;
731
732         /*
733          * Make sure the pool name is valid.
734          */
735         if (!zpool_name_valid(hdl, B_TRUE, pool)) {
736                 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
737                     dgettext(TEXT_DOMAIN, "cannot open '%s'"),
738                     pool);
739                 return (NULL);
740         }
741
742         if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
743                 return (NULL);
744
745         zhp->zpool_hdl = hdl;
746         (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
747
748         if (zpool_refresh_stats(zhp, &missing) != 0) {
749                 zpool_close(zhp);
750                 return (NULL);
751         }
752
753         if (missing) {
754                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
755                 (void) zfs_error_fmt(hdl, EZFS_NOENT,
756                     dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
757                 zpool_close(zhp);
758                 return (NULL);
759         }
760
761         return (zhp);
762 }
763
764 /*
765  * Like the above, but silent on error.  Used when iterating over pools (because
766  * the configuration cache may be out of date).
767  */
768 int
769 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
770 {
771         zpool_handle_t *zhp;
772         boolean_t missing;
773
774         if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
775                 return (-1);
776
777         zhp->zpool_hdl = hdl;
778         (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
779
780         if (zpool_refresh_stats(zhp, &missing) != 0) {
781                 zpool_close(zhp);
782                 return (-1);
783         }
784
785         if (missing) {
786                 zpool_close(zhp);
787                 *ret = NULL;
788                 return (0);
789         }
790
791         *ret = zhp;
792         return (0);
793 }
794
795 /*
796  * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
797  * state.
798  */
799 zpool_handle_t *
800 zpool_open(libzfs_handle_t *hdl, const char *pool)
801 {
802         zpool_handle_t *zhp;
803
804         if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
805                 return (NULL);
806
807         if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
808                 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
809                     dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
810                 zpool_close(zhp);
811                 return (NULL);
812         }
813
814         return (zhp);
815 }
816
817 /*
818  * Close the handle.  Simply frees the memory associated with the handle.
819  */
820 void
821 zpool_close(zpool_handle_t *zhp)
822 {
823         if (zhp->zpool_config)
824                 nvlist_free(zhp->zpool_config);
825         if (zhp->zpool_old_config)
826                 nvlist_free(zhp->zpool_old_config);
827         if (zhp->zpool_props)
828                 nvlist_free(zhp->zpool_props);
829         free(zhp);
830 }
831
832 /*
833  * Return the name of the pool.
834  */
835 const char *
836 zpool_get_name(zpool_handle_t *zhp)
837 {
838         return (zhp->zpool_name);
839 }
840
841
842 /*
843  * Return the state of the pool (ACTIVE or UNAVAILABLE)
844  */
845 int
846 zpool_get_state(zpool_handle_t *zhp)
847 {
848         return (zhp->zpool_state);
849 }
850
851 /*
852  * Create the named pool, using the provided vdev list.  It is assumed
853  * that the consumer has already validated the contents of the nvlist, so we
854  * don't have to worry about error semantics.
855  */
856 int
857 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
858     nvlist_t *props, nvlist_t *fsprops)
859 {
860         zfs_cmd_t zc = { 0 };
861         nvlist_t *zc_fsprops = NULL;
862         nvlist_t *zc_props = NULL;
863         char msg[1024];
864         char *altroot;
865         int ret = -1;
866
867         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
868             "cannot create '%s'"), pool);
869
870         if (!zpool_name_valid(hdl, B_FALSE, pool))
871                 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
872
873         if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
874                 return (-1);
875
876         if (props) {
877                 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
878                     SPA_VERSION_1, B_TRUE, msg)) == NULL) {
879                         goto create_failed;
880                 }
881         }
882
883         if (fsprops) {
884                 uint64_t zoned;
885                 char *zonestr;
886
887                 zoned = ((nvlist_lookup_string(fsprops,
888                     zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
889                     strcmp(zonestr, "on") == 0);
890
891                 if ((zc_fsprops = zfs_valid_proplist(hdl,
892                     ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
893                         goto create_failed;
894                 }
895                 if (!zc_props &&
896                     (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
897                         goto create_failed;
898                 }
899                 if (nvlist_add_nvlist(zc_props,
900                     ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
901                         goto create_failed;
902                 }
903         }
904
905         if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
906                 goto create_failed;
907
908         (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
909
910         if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
911
912                 zcmd_free_nvlists(&zc);
913                 nvlist_free(zc_props);
914                 nvlist_free(zc_fsprops);
915
916                 switch (errno) {
917                 case EBUSY:
918                         /*
919                          * This can happen if the user has specified the same
920                          * device multiple times.  We can't reliably detect this
921                          * until we try to add it and see we already have a
922                          * label.
923                          */
924                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
925                             "one or more vdevs refer to the same device"));
926                         return (zfs_error(hdl, EZFS_BADDEV, msg));
927
928                 case EOVERFLOW:
929                         /*
930                          * This occurs when one of the devices is below
931                          * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
932                          * device was the problem device since there's no
933                          * reliable way to determine device size from userland.
934                          */
935                         {
936                                 char buf[64];
937
938                                 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
939
940                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
941                                     "one or more devices is less than the "
942                                     "minimum size (%s)"), buf);
943                         }
944                         return (zfs_error(hdl, EZFS_BADDEV, msg));
945
946                 case ENOSPC:
947                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
948                             "one or more devices is out of space"));
949                         return (zfs_error(hdl, EZFS_BADDEV, msg));
950
951                 case ENOTBLK:
952                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953                             "cache device must be a disk or disk slice"));
954                         return (zfs_error(hdl, EZFS_BADDEV, msg));
955
956                 default:
957                         return (zpool_standard_error(hdl, errno, msg));
958                 }
959         }
960
961         /*
962          * If this is an alternate root pool, then we automatically set the
963          * mountpoint of the root dataset to be '/'.
964          */
965         if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
966             &altroot) == 0) {
967                 zfs_handle_t *zhp;
968
969                 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
970                 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
971                     "/") == 0);
972
973                 zfs_close(zhp);
974         }
975
976 create_failed:
977         zcmd_free_nvlists(&zc);
978         nvlist_free(zc_props);
979         nvlist_free(zc_fsprops);
980         return (ret);
981 }
982
983 /*
984  * Destroy the given pool.  It is up to the caller to ensure that there are no
985  * datasets left in the pool.
986  */
987 int
988 zpool_destroy(zpool_handle_t *zhp)
989 {
990         zfs_cmd_t zc = { 0 };
991         zfs_handle_t *zfp = NULL;
992         libzfs_handle_t *hdl = zhp->zpool_hdl;
993         char msg[1024];
994
995         if (zhp->zpool_state == POOL_STATE_ACTIVE &&
996             (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
997             ZFS_TYPE_FILESYSTEM)) == NULL)
998                 return (-1);
999
1000         if (zpool_remove_zvol_links(zhp) != 0)
1001                 return (-1);
1002
1003         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1004
1005         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1006                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1007                     "cannot destroy '%s'"), zhp->zpool_name);
1008
1009                 if (errno == EROFS) {
1010                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011                             "one or more devices is read only"));
1012                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1013                 } else {
1014                         (void) zpool_standard_error(hdl, errno, msg);
1015                 }
1016
1017                 if (zfp)
1018                         zfs_close(zfp);
1019                 return (-1);
1020         }
1021
1022         if (zfp) {
1023                 remove_mountpoint(zfp);
1024                 zfs_close(zfp);
1025         }
1026
1027         return (0);
1028 }
1029
1030 /*
1031  * Add the given vdevs to the pool.  The caller must have already performed the
1032  * necessary verification to ensure that the vdev specification is well-formed.
1033  */
1034 int
1035 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1036 {
1037         zfs_cmd_t zc = { 0 };
1038         int ret;
1039         libzfs_handle_t *hdl = zhp->zpool_hdl;
1040         char msg[1024];
1041         nvlist_t **spares, **l2cache;
1042         uint_t nspares, nl2cache;
1043
1044         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1045             "cannot add to '%s'"), zhp->zpool_name);
1046
1047         if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1048             SPA_VERSION_SPARES &&
1049             nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1050             &spares, &nspares) == 0) {
1051                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1052                     "upgraded to add hot spares"));
1053                 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1054         }
1055
1056         if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1057             ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1058                 uint64_t s;
1059
1060                 for (s = 0; s < nspares; s++) {
1061                         char *path;
1062
1063                         if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1064                             &path) == 0 && pool_uses_efi(spares[s])) {
1065                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1066                                     "device '%s' contains an EFI label and "
1067                                     "cannot be used on root pools."),
1068                                     zpool_vdev_name(hdl, NULL, spares[s]));
1069                                 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1070                         }
1071                 }
1072         }
1073
1074         if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1075             SPA_VERSION_L2CACHE &&
1076             nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1077             &l2cache, &nl2cache) == 0) {
1078                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1079                     "upgraded to add cache devices"));
1080                 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1081         }
1082
1083         if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1084                 return (-1);
1085         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1086
1087         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1088                 switch (errno) {
1089                 case EBUSY:
1090                         /*
1091                          * This can happen if the user has specified the same
1092                          * device multiple times.  We can't reliably detect this
1093                          * until we try to add it and see we already have a
1094                          * label.
1095                          */
1096                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1097                             "one or more vdevs refer to the same device"));
1098                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1099                         break;
1100
1101                 case EOVERFLOW:
1102                         /*
1103                          * This occurrs when one of the devices is below
1104                          * SPA_MINDEVSIZE.  Unfortunately, we can't detect which
1105                          * device was the problem device since there's no
1106                          * reliable way to determine device size from userland.
1107                          */
1108                         {
1109                                 char buf[64];
1110
1111                                 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1112
1113                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1114                                     "device is less than the minimum "
1115                                     "size (%s)"), buf);
1116                         }
1117                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1118                         break;
1119
1120                 case ENOTSUP:
1121                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1122                             "pool must be upgraded to add these vdevs"));
1123                         (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1124                         break;
1125
1126                 case EDOM:
1127                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1128                             "root pool can not have multiple vdevs"
1129                             " or separate logs"));
1130                         (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1131                         break;
1132
1133                 case ENOTBLK:
1134                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1135                             "cache device must be a disk or disk slice"));
1136                         (void) zfs_error(hdl, EZFS_BADDEV, msg);
1137                         break;
1138
1139                 default:
1140                         (void) zpool_standard_error(hdl, errno, msg);
1141                 }
1142
1143                 ret = -1;
1144         } else {
1145                 ret = 0;
1146         }
1147
1148         zcmd_free_nvlists(&zc);
1149
1150         return (ret);
1151 }
1152
1153 /*
1154  * Exports the pool from the system.  The caller must ensure that there are no
1155  * mounted datasets in the pool.
1156  */
1157 int
1158 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1159 {
1160         zfs_cmd_t zc = { 0 };
1161         char msg[1024];
1162
1163         if (zpool_remove_zvol_links(zhp) != 0)
1164                 return (-1);
1165
1166         (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1167             "cannot export '%s'"), zhp->zpool_name);
1168
1169         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1170         zc.zc_cookie = force;
1171         zc.zc_guid = hardforce;
1172
1173         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1174                 switch (errno) {
1175                 case EXDEV:
1176                         zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1177                             "use '-f' to override the following errors:\n"
1178                             "'%s' has an active shared spare which could be"
1179                             " used by other pools once '%s' is exported."),
1180                             zhp->zpool_name, zhp->zpool_name);
1181                         return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1182                             msg));
1183                 default:
1184                         return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1185                             msg));
1186                 }
1187         }
1188
1189         return (0);
1190 }
1191
1192 int
1193 zpool_export(zpool_handle_t *zhp, boolean_t force)
1194 {
1195         return (zpool_export_common(zhp, force, B_FALSE));
1196 }
1197
1198 int
1199 zpool_export_force(zpool_handle_t *zhp)
1200 {
1201         return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1202 }
1203
1204 /*
1205  * zpool_import() is a contracted interface. Should be kept the same
1206  * if possible.
1207  *
1208  * Applications should use zpool_import_props() to import a pool with
1209  * new properties value to be set.
1210  */
1211 int
1212 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1213     char *altroot)
1214 {
1215         nvlist_t *props = NULL;
1216         int ret;
1217
1218         if (altroot != NULL) {
1219                 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1220                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1221                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1222                             newname));
1223                 }
1224
1225                 if (nvlist_add_string(props,
1226                     zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1227                     nvlist_add_string(props,
1228                     zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1229                         nvlist_free(props);
1230                         return (zfs_error_fmt(hdl, EZFS_NOMEM,
1231                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1232                             newname));
1233                 }
1234         }
1235
1236         ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1237         if (props)
1238                 nvlist_free(props);
1239         return (ret);
1240 }
1241
1242 /*
1243  * Import the given pool using the known configuration and a list of
1244  * properties to be set. The configuration should have come from
1245  * zpool_find_import(). The 'newname' parameters control whether the pool
1246  * is imported with a different name.
1247  */
1248 int
1249 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1250     nvlist_t *props, boolean_t importfaulted)
1251 {
1252         zfs_cmd_t zc = { 0 };
1253         char *thename;
1254         char *origname;
1255         int ret;
1256         char errbuf[1024];
1257
1258         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1259             &origname) == 0);
1260
1261         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1262             "cannot import pool '%s'"), origname);
1263
1264         if (newname != NULL) {
1265                 if (!zpool_name_valid(hdl, B_FALSE, newname))
1266                         return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1267                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1268                             newname));
1269                 thename = (char *)newname;
1270         } else {
1271                 thename = origname;
1272         }
1273
1274         if (props) {
1275                 uint64_t version;
1276
1277                 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1278                     &version) == 0);
1279
1280                 if ((props = zpool_valid_proplist(hdl, origname,
1281                     props, version, B_TRUE, errbuf)) == NULL) {
1282                         return (-1);
1283                 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1284                         nvlist_free(props);
1285                         return (-1);
1286                 }
1287         }
1288
1289         (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1290
1291         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1292             &zc.zc_guid) == 0);
1293
1294         if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1295                 nvlist_free(props);
1296                 return (-1);
1297         }
1298
1299         zc.zc_cookie = (uint64_t)importfaulted;
1300         ret = 0;
1301         if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1302                 char desc[1024];
1303                 if (newname == NULL)
1304                         (void) snprintf(desc, sizeof (desc),
1305                             dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1306                             thename);
1307                 else
1308                         (void) snprintf(desc, sizeof (desc),
1309                             dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1310                             origname, thename);
1311
1312                 switch (errno) {
1313                 case ENOTSUP:
1314                         /*
1315                          * Unsupported version.
1316                          */
1317                         (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1318                         break;
1319
1320                 case EINVAL:
1321                         (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1322                         break;
1323
1324                 default:
1325                         (void) zpool_standard_error(hdl, errno, desc);
1326                 }
1327
1328                 ret = -1;
1329         } else {
1330                 zpool_handle_t *zhp;
1331
1332                 /*
1333                  * This should never fail, but play it safe anyway.
1334                  */
1335                 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1336                         ret = -1;
1337                 } else if (zhp != NULL) {
1338                         ret = zpool_create_zvol_links(zhp);
1339                         zpool_close(zhp);
1340                 }
1341
1342         }
1343
1344         zcmd_free_nvlists(&zc);
1345         nvlist_free(props);
1346
1347         return (ret);
1348 }
1349
1350 /*
1351  * Scrub the pool.
1352  */
1353 int
1354 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1355 {
1356         zfs_cmd_t zc = { 0 };
1357         char msg[1024];
1358         libzfs_handle_t *hdl = zhp->zpool_hdl;
1359
1360         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1361         zc.zc_cookie = type;
1362
1363         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1364                 return (0);
1365
1366         (void) snprintf(msg, sizeof (msg),
1367             dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1368
1369         if (errno == EBUSY)
1370                 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1371         else
1372                 return (zpool_standard_error(hdl, errno, msg));
1373 }
1374
1375 /*
1376  * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1377  * spare; but FALSE if its an INUSE spare.
1378  */
1379 static nvlist_t *
1380 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1381     boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1382 {
1383         uint_t c, children;
1384         nvlist_t **child;
1385         uint64_t theguid, present;
1386         char *path;
1387         uint64_t wholedisk = 0;
1388         nvlist_t *ret;
1389         uint64_t is_log;
1390
1391         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1392
1393         if (search == NULL &&
1394             nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1395                 /*
1396                  * If the device has never been present since import, the only
1397                  * reliable way to match the vdev is by GUID.
1398                  */
1399                 if (theguid == guid)
1400                         return (nv);
1401         } else if (search != NULL &&
1402             nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1403                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1404                     &wholedisk);
1405                 if (wholedisk) {
1406                         /*
1407                          * For whole disks, the internal path has 's0', but the
1408                          * path passed in by the user doesn't.
1409                          */
1410                         if (strlen(search) == strlen(path) - 2 &&
1411                             strncmp(search, path, strlen(search)) == 0)
1412                                 return (nv);
1413                 } else if (strcmp(search, path) == 0) {
1414                         return (nv);
1415                 }
1416         }
1417
1418         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1419             &child, &children) != 0)
1420                 return (NULL);
1421
1422         for (c = 0; c < children; c++) {
1423                 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1424                     avail_spare, l2cache, NULL)) != NULL) {
1425                         /*
1426                          * The 'is_log' value is only set for the toplevel
1427                          * vdev, not the leaf vdevs.  So we always lookup the
1428                          * log device from the root of the vdev tree (where
1429                          * 'log' is non-NULL).
1430                          */
1431                         if (log != NULL &&
1432                             nvlist_lookup_uint64(child[c],
1433                             ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1434                             is_log) {
1435                                 *log = B_TRUE;
1436                         }
1437                         return (ret);
1438                 }
1439         }
1440
1441         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1442             &child, &children) == 0) {
1443                 for (c = 0; c < children; c++) {
1444                         if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1445                             avail_spare, l2cache, NULL)) != NULL) {
1446                                 *avail_spare = B_TRUE;
1447                                 return (ret);
1448                         }
1449                 }
1450         }
1451
1452         if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1453             &child, &children) == 0) {
1454                 for (c = 0; c < children; c++) {
1455                         if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1456                             avail_spare, l2cache, NULL)) != NULL) {
1457                                 *l2cache = B_TRUE;
1458                                 return (ret);
1459                         }
1460                 }
1461         }
1462
1463         return (NULL);
1464 }
1465
1466 nvlist_t *
1467 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1468     boolean_t *l2cache, boolean_t *log)
1469 {
1470         char buf[MAXPATHLEN];
1471         const char *search;
1472         char *end;
1473         nvlist_t *nvroot;
1474         uint64_t guid;
1475
1476         guid = strtoull(path, &end, 10);
1477         if (guid != 0 && *end == '\0') {
1478                 search = NULL;
1479         } else if (path[0] != '/') {
1480                 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1481                 search = buf;
1482         } else {
1483                 search = path;
1484         }
1485
1486         verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1487             &nvroot) == 0);
1488
1489         *avail_spare = B_FALSE;
1490         *l2cache = B_FALSE;
1491         if (log != NULL)
1492                 *log = B_FALSE;
1493         return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1494             l2cache, log));
1495 }
1496
1497 static int
1498 vdev_online(nvlist_t *nv)
1499 {
1500         uint64_t ival;
1501
1502         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1503             nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1504             nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1505                 return (0);
1506
1507         return (1);
1508 }
1509
1510 /*
1511  * Get phys_path for a root pool
1512  * Return 0 on success; non-zeron on failure.
1513  */
1514 int
1515 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1516 {
1517         nvlist_t *vdev_root;
1518         nvlist_t **child;
1519         uint_t count;
1520         int i;
1521
1522         /*
1523          * Make sure this is a root pool, as phys_path doesn't mean
1524          * anything to a non-root pool.
1525          */
1526         if (!pool_is_bootable(zhp))
1527                 return (-1);
1528
1529         verify(nvlist_lookup_nvlist(zhp->zpool_config,
1530             ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1531
1532         if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1533             &child, &count) != 0)
1534                 return (-2);
1535
1536         for (i = 0; i < count; i++) {
1537                 nvlist_t **child2;
1538                 uint_t count2;
1539                 char *type;
1540                 char *tmppath;
1541                 int j;
1542
1543                 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1544                     != 0)
1545                         return (-3);
1546
1547                 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1548                         if (!vdev_online(child[i]))
1549                                 return (-8);
1550                         verify(nvlist_lookup_string(child[i],
1551                             ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1552                         (void) strncpy(physpath, tmppath, strlen(tmppath));
1553                 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1554                         if (nvlist_lookup_nvlist_array(child[i],
1555                             ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1556                                 return (-4);
1557
1558                         for (j = 0; j < count2; j++) {
1559                                 if (!vdev_online(child2[j]))
1560                                         return (-8);
1561                                 if (nvlist_lookup_string(child2[j],
1562                                     ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1563                                         return (-5);
1564
1565                                 if ((strlen(physpath) + strlen(tmppath)) >
1566                                     MAXNAMELEN)
1567                                         return (-6);
1568
1569                                 if (strlen(physpath) == 0) {
1570                                         (void) strncpy(physpath, tmppath,
1571                                             strlen(tmppath));
1572                                 } else {
1573                                         (void) strcat(physpath, " ");
1574                                         (void) strcat(physpath, tmppath);
1575                                 }
1576                         }
1577                 } else {
1578                         return (-7);
1579                 }
1580         }
1581
1582         return (0);
1583 }
1584
1585 /*
1586  * Returns TRUE if the given guid corresponds to the given type.
1587  * This is used to check for hot spares (INUSE or not), and level 2 cache
1588  * devices.
1589  */
1590 static boolean_t
1591 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1592 {
1593         uint64_t target_guid;
1594         nvlist_t *nvroot;
1595         nvlist_t **list;
1596         uint_t count;
1597         int i;
1598
1599         verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1600             &nvroot) == 0);
1601         if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1602                 for (i = 0; i < count; i++) {
1603                         verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1604                             &target_guid) == 0);
1605                         if (guid == target_guid)
1606                                 return (B_TRUE);
1607                 }
1608         }
1609
1610         return (B_FALSE);
1611 }
1612
1613 /*
1614  * Bring the specified vdev online.   The 'flags' parameter is a set of the
1615  * ZFS_ONLINE_* flags.
1616  */
1617 int
1618 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1619     vdev_state_t *newstate)
1620 {
1621         zfs_cmd_t zc = { 0 };
1622         char msg[1024];
1623         nvlist_t *tgt;
1624         boolean_t avail_spare, l2cache;
1625         libzfs_handle_t *hdl = zhp->zpool_hdl;
1626
1627         (void) snprintf(msg, sizeof (msg),
1628             dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1629
1630         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1631         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1632             NULL)) == NULL)
1633                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1634
1635         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1636
1637         if (avail_spare ||
1638             is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1639                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1640
1641         zc.zc_cookie = VDEV_STATE_ONLINE;
1642         zc.zc_obj = flags;
1643
1644         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1645                 return (zpool_standard_error(hdl, errno, msg));
1646
1647         *newstate = zc.zc_cookie;
1648         return (0);
1649 }
1650
1651 /*
1652  * Take the specified vdev offline
1653  */
1654 int
1655 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1656 {
1657         zfs_cmd_t zc = { 0 };
1658         char msg[1024];
1659         nvlist_t *tgt;
1660         boolean_t avail_spare, l2cache;
1661         libzfs_handle_t *hdl = zhp->zpool_hdl;
1662
1663         (void) snprintf(msg, sizeof (msg),
1664             dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1665
1666         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1667         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1668             NULL)) == NULL)
1669                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1670
1671         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1672
1673         if (avail_spare ||
1674             is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1675                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1676
1677         zc.zc_cookie = VDEV_STATE_OFFLINE;
1678         zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1679
1680         if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1681                 return (0);
1682
1683         switch (errno) {
1684         case EBUSY:
1685
1686                 /*
1687                  * There are no other replicas of this device.
1688                  */
1689                 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1690
1691         default:
1692                 return (zpool_standard_error(hdl, errno, msg));
1693         }
1694 }
1695
1696 /*
1697  * Mark the given vdev faulted.
1698  */
1699 int
1700 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1701 {
1702         zfs_cmd_t zc = { 0 };
1703         char msg[1024];
1704         libzfs_handle_t *hdl = zhp->zpool_hdl;
1705
1706         (void) snprintf(msg, sizeof (msg),
1707             dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1708
1709         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1710         zc.zc_guid = guid;
1711         zc.zc_cookie = VDEV_STATE_FAULTED;
1712
1713         if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1714                 return (0);
1715
1716         switch (errno) {
1717         case EBUSY:
1718
1719                 /*
1720                  * There are no other replicas of this device.
1721                  */
1722                 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1723
1724         default:
1725                 return (zpool_standard_error(hdl, errno, msg));
1726         }
1727
1728 }
1729
1730 /*
1731  * Mark the given vdev degraded.
1732  */
1733 int
1734 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1735 {
1736         zfs_cmd_t zc = { 0 };
1737         char msg[1024];
1738         libzfs_handle_t *hdl = zhp->zpool_hdl;
1739
1740         (void) snprintf(msg, sizeof (msg),
1741             dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1742
1743         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1744         zc.zc_guid = guid;
1745         zc.zc_cookie = VDEV_STATE_DEGRADED;
1746
1747         if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1748                 return (0);
1749
1750         return (zpool_standard_error(hdl, errno, msg));
1751 }
1752
1753 /*
1754  * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1755  * a hot spare.
1756  */
1757 static boolean_t
1758 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1759 {
1760         nvlist_t **child;
1761         uint_t c, children;
1762         char *type;
1763
1764         if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1765             &children) == 0) {
1766                 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1767                     &type) == 0);
1768
1769                 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1770                     children == 2 && child[which] == tgt)
1771                         return (B_TRUE);
1772
1773                 for (c = 0; c < children; c++)
1774                         if (is_replacing_spare(child[c], tgt, which))
1775                                 return (B_TRUE);
1776         }
1777
1778         return (B_FALSE);
1779 }
1780
1781 /*
1782  * Attach new_disk (fully described by nvroot) to old_disk.
1783  * If 'replacing' is specified, the new disk will replace the old one.
1784  */
1785 int
1786 zpool_vdev_attach(zpool_handle_t *zhp,
1787     const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1788 {
1789         zfs_cmd_t zc = { 0 };
1790         char msg[1024];
1791         int ret;
1792         nvlist_t *tgt;
1793         boolean_t avail_spare, l2cache, islog;
1794         uint64_t val;
1795         char *path, *newname;
1796         nvlist_t **child;
1797         uint_t children;
1798         nvlist_t *config_root;
1799         libzfs_handle_t *hdl = zhp->zpool_hdl;
1800         boolean_t rootpool = pool_is_bootable(zhp);
1801
1802         if (replacing)
1803                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1804                     "cannot replace %s with %s"), old_disk, new_disk);
1805         else
1806                 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1807                     "cannot attach %s to %s"), new_disk, old_disk);
1808
1809         /*
1810          * If this is a root pool, make sure that we're not attaching an
1811          * EFI labeled device.
1812          */
1813         if (rootpool && pool_uses_efi(nvroot)) {
1814                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1815                     "EFI labeled devices are not supported on root pools."));
1816                 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1817         }
1818
1819         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1820         if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1821             &islog)) == 0)
1822                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1823
1824         if (avail_spare)
1825                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1826
1827         if (l2cache)
1828                 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1829
1830         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1831         zc.zc_cookie = replacing;
1832
1833         if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1834             &child, &children) != 0 || children != 1) {
1835                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1836                     "new device must be a single disk"));
1837                 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1838         }
1839
1840         verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1841             ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1842
1843         if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1844                 return (-1);
1845
1846         /*
1847          * If the target is a hot spare that has been swapped in, we can only
1848          * replace it with another hot spare.
1849          */
1850         if (replacing &&
1851             nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1852             (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1853             NULL) == NULL || !avail_spare) &&
1854             is_replacing_spare(config_root, tgt, 1)) {
1855                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1856                     "can only be replaced by another hot spare"));
1857                 free(newname);
1858                 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1859         }
1860
1861         /*
1862          * If we are attempting to replace a spare, it canot be applied to an
1863          * already spared device.
1864          */
1865         if (replacing &&
1866             nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1867             zpool_find_vdev(zhp, newname, &avail_spare,
1868             &l2cache, NULL) != NULL && avail_spare &&
1869             is_replacing_spare(config_root, tgt, 0)) {
1870                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1871                     "device has already been replaced with a spare"));
1872                 free(newname);
1873                 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1874         }
1875
1876         free(newname);
1877
1878         if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1879                 return (-1);
1880
1881         ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1882
1883         zcmd_free_nvlists(&zc);
1884
1885         if (ret == 0) {
1886                 if (rootpool) {
1887                         /*
1888                          * XXX - This should be removed once we can
1889                          * automatically install the bootblocks on the
1890                          * newly attached disk.
1891                          */
1892                         (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
1893                             "be sure to invoke %s to make '%s' bootable.\n"),
1894                             BOOTCMD, new_disk);
1895                 }
1896                 return (0);
1897         }
1898
1899         switch (errno) {
1900         case ENOTSUP:
1901                 /*
1902                  * Can't attach to or replace this type of vdev.
1903                  */
1904                 if (replacing) {
1905                         if (islog)
1906                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1907                                     "cannot replace a log with a spare"));
1908                         else
1909                                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1910                                     "cannot replace a replacing device"));
1911                 } else {
1912                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1913                             "can only attach to mirrors and top-level "
1914                             "disks"));
1915                 }
1916                 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1917                 break;
1918
1919         case EINVAL:
1920                 /*
1921                  * The new device must be a single disk.
1922                  */
1923                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1924                     "new device must be a single disk"));
1925                 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1926                 break;
1927
1928         case EBUSY:
1929                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1930                     new_disk);
1931                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1932                 break;
1933
1934         case EOVERFLOW:
1935                 /*
1936                  * The new device is too small.
1937                  */
1938                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1939                     "device is too small"));
1940                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1941                 break;
1942
1943         case EDOM:
1944                 /*
1945                  * The new device has a different alignment requirement.
1946                  */
1947                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1948                     "devices have different sector alignment"));
1949                 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1950                 break;
1951
1952         case ENAMETOOLONG:
1953                 /*
1954                  * The resulting top-level vdev spec won't fit in the label.
1955                  */
1956                 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1957                 break;
1958
1959         default:
1960                 (void) zpool_standard_error(hdl, errno, msg);
1961         }
1962
1963         return (-1);
1964 }
1965
1966 /*
1967  * Detach the specified device.
1968  */
1969 int
1970 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1971 {
1972         zfs_cmd_t zc = { 0 };
1973         char msg[1024];
1974         nvlist_t *tgt;
1975         boolean_t avail_spare, l2cache;
1976         libzfs_handle_t *hdl = zhp->zpool_hdl;
1977
1978         (void) snprintf(msg, sizeof (msg),
1979             dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1980
1981         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1982         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1983             NULL)) == 0)
1984                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1985
1986         if (avail_spare)
1987                 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1988
1989         if (l2cache)
1990                 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1991
1992         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1993
1994         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1995                 return (0);
1996
1997         switch (errno) {
1998
1999         case ENOTSUP:
2000                 /*
2001                  * Can't detach from this type of vdev.
2002                  */
2003                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2004                     "applicable to mirror and replacing vdevs"));
2005                 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2006                 break;
2007
2008         case EBUSY:
2009                 /*
2010                  * There are no other replicas of this device.
2011                  */
2012                 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2013                 break;
2014
2015         default:
2016                 (void) zpool_standard_error(hdl, errno, msg);
2017         }
2018
2019         return (-1);
2020 }
2021
2022 /*
2023  * Remove the given device.  Currently, this is supported only for hot spares
2024  * and level 2 cache devices.
2025  */
2026 int
2027 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2028 {
2029         zfs_cmd_t zc = { 0 };
2030         char msg[1024];
2031         nvlist_t *tgt;
2032         boolean_t avail_spare, l2cache;
2033         libzfs_handle_t *hdl = zhp->zpool_hdl;
2034
2035         (void) snprintf(msg, sizeof (msg),
2036             dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2037
2038         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2039         if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2040             NULL)) == 0)
2041                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2042
2043         if (!avail_spare && !l2cache) {
2044                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2045                     "only inactive hot spares or cache devices "
2046                     "can be removed"));
2047                 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2048         }
2049
2050         verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2051
2052         if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2053                 return (0);
2054
2055         return (zpool_standard_error(hdl, errno, msg));
2056 }
2057
2058 /*
2059  * Clear the errors for the pool, or the particular device if specified.
2060  */
2061 int
2062 zpool_clear(zpool_handle_t *zhp, const char *path)
2063 {
2064         zfs_cmd_t zc = { 0 };
2065         char msg[1024];
2066         nvlist_t *tgt;
2067         boolean_t avail_spare, l2cache;
2068         libzfs_handle_t *hdl = zhp->zpool_hdl;
2069
2070         if (path)
2071                 (void) snprintf(msg, sizeof (msg),
2072                     dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2073                     path);
2074         else
2075                 (void) snprintf(msg, sizeof (msg),
2076                     dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2077                     zhp->zpool_name);
2078
2079         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2080         if (path) {
2081                 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2082                     &l2cache, NULL)) == 0)
2083                         return (zfs_error(hdl, EZFS_NODEVICE, msg));
2084
2085                 /*
2086                  * Don't allow error clearing for hot spares.  Do allow
2087                  * error clearing for l2cache devices.
2088                  */
2089                 if (avail_spare)
2090                         return (zfs_error(hdl, EZFS_ISSPARE, msg));
2091
2092                 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2093                     &zc.zc_guid) == 0);
2094         }
2095
2096         if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2097                 return (0);
2098
2099         return (zpool_standard_error(hdl, errno, msg));
2100 }
2101
2102 /*
2103  * Similar to zpool_clear(), but takes a GUID (used by fmd).
2104  */
2105 int
2106 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2107 {
2108         zfs_cmd_t zc = { 0 };
2109         char msg[1024];
2110         libzfs_handle_t *hdl = zhp->zpool_hdl;
2111
2112         (void) snprintf(msg, sizeof (msg),
2113             dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2114             guid);
2115
2116         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2117         zc.zc_guid = guid;
2118
2119         if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2120                 return (0);
2121
2122         return (zpool_standard_error(hdl, errno, msg));
2123 }
2124
2125 /*
2126  * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2127  * hierarchy.
2128  */
2129 int
2130 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2131     void *data)
2132 {
2133         libzfs_handle_t *hdl = zhp->zpool_hdl;
2134         char (*paths)[MAXPATHLEN];
2135         char path[MAXPATHLEN];
2136         size_t size = 4;
2137         int curr, fd, base, ret = 0;
2138         DIR *dirp;
2139         struct dirent *dp;
2140         struct stat st;
2141
2142         if ((base = open(ZVOL_FULL_DEV_DIR, O_RDONLY)) < 0)
2143                 return (errno == ENOENT ? 0 : -1);
2144
2145         snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2146             zhp->zpool_name);
2147         if (stat(path, &st) != 0) {
2148                 int err = errno;
2149                 (void) close(base);
2150                 return (err == ENOENT ? 0 : -1);
2151         }
2152
2153         /*
2154          * Oddly this wasn't a directory -- ignore that failure since we
2155          * know there are no links lower in the (non-existant) hierarchy.
2156          */
2157         if (!S_ISDIR(st.st_mode)) {
2158                 (void) close(base);
2159                 return (0);
2160         }
2161
2162         if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2163                 (void) close(base);
2164                 return (-1);
2165         }
2166
2167         (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2168         curr = 0;
2169
2170         while (curr >= 0) {
2171                 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2172                     paths[curr]);
2173                 if (lstat(path, &st) != 0)
2174                         goto err;
2175
2176                 if (S_ISDIR(st.st_mode)) {
2177                         if ((dirp = opendir(path)) == NULL) {
2178                                 goto err;
2179                         }
2180
2181                         while ((dp = readdir(dirp)) != NULL) {
2182                                 if (dp->d_name[0] == '.')
2183                                         continue;
2184
2185                                 if (curr + 1 == size) {
2186                                         paths = zfs_realloc(hdl, paths,
2187                                             size * sizeof (paths[0]),
2188                                             size * 2 * sizeof (paths[0]));
2189                                         if (paths == NULL) {
2190                                                 (void) closedir(dirp);
2191                                                 goto err;
2192                                         }
2193
2194                                         size *= 2;
2195                                 }
2196
2197                                 (void) strlcpy(paths[curr + 1], paths[curr],
2198                                     sizeof (paths[curr + 1]));
2199                                 (void) strlcat(paths[curr], "/",
2200                                     sizeof (paths[curr]));
2201                                 (void) strlcat(paths[curr], dp->d_name,
2202                                     sizeof (paths[curr]));
2203                                 curr++;
2204                         }
2205
2206                         (void) closedir(dirp);
2207
2208                 } else {
2209                         if ((ret = cb(paths[curr], data)) != 0)
2210                                 break;
2211                 }
2212
2213                 curr--;
2214         }
2215
2216         free(paths);
2217         (void) close(base);
2218
2219         return (ret);
2220
2221 err:
2222         free(paths);
2223         (void) close(base);
2224         return (-1);
2225 }
2226
2227 typedef struct zvol_cb {
2228         zpool_handle_t *zcb_pool;
2229         boolean_t zcb_create;
2230 } zvol_cb_t;
2231
2232 /*ARGSUSED*/
2233 static int
2234 do_zvol_create(zfs_handle_t *zhp, void *data)
2235 {
2236         int ret = 0;
2237
2238         if (ZFS_IS_VOLUME(zhp)) {
2239                 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2240                 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2241         }
2242
2243         if (ret == 0)
2244                 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2245
2246         zfs_close(zhp);
2247
2248         return (ret);
2249 }
2250
2251 /*
2252  * Iterate over all zvols in the pool and make any necessary minor nodes.
2253  */
2254 int
2255 zpool_create_zvol_links(zpool_handle_t *zhp)
2256 {
2257         zfs_handle_t *zfp;
2258         int ret;
2259
2260         /*
2261          * If the pool is unavailable, just return success.
2262          */
2263         if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2264             zhp->zpool_name)) == NULL)
2265                 return (0);
2266
2267         ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2268
2269         zfs_close(zfp);
2270         return (ret);
2271 }
2272
2273 static int
2274 do_zvol_remove(const char *dataset, void *data)
2275 {
2276         zpool_handle_t *zhp = data;
2277
2278         return (zvol_remove_link(zhp->zpool_hdl, dataset));
2279 }
2280
2281 /*
2282  * Iterate over all zvols in the pool and remove any minor nodes.  We iterate
2283  * by examining the /dev links so that a corrupted pool doesn't impede this
2284  * operation.
2285  */
2286 int
2287 zpool_remove_zvol_links(zpool_handle_t *zhp)
2288 {
2289         return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2290 }
2291
2292 /*
2293  * Convert from a devid string to a path.
2294  */
2295 static char *
2296 devid_to_path(char *devid_str)
2297 {
2298         ddi_devid_t devid;
2299         char *minor;
2300         char *path;
2301         devid_nmlist_t *list = NULL;
2302         int ret;
2303
2304         if (devid_str_decode(devid_str, &devid, &minor) != 0)
2305                 return (NULL);
2306
2307         ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2308
2309         devid_str_free(minor);
2310         devid_free(devid);
2311
2312         if (ret != 0)
2313                 return (NULL);
2314
2315         if ((path = strdup(list[0].devname)) == NULL)
2316                 return (NULL);
2317
2318         devid_free_nmlist(list);
2319
2320         return (path);
2321 }
2322
2323 /*
2324  * Convert from a path to a devid string.
2325  */
2326 static char *
2327 path_to_devid(const char *path)
2328 {
2329         int fd;
2330         ddi_devid_t devid;
2331         char *minor, *ret;
2332
2333         if ((fd = open(path, O_RDONLY)) < 0)
2334                 return (NULL);
2335
2336         minor = NULL;
2337         ret = NULL;
2338         if (devid_get(fd, &devid) == 0) {
2339                 if (devid_get_minor_name(fd, &minor) == 0)
2340                         ret = devid_str_encode(devid, minor);
2341                 if (minor != NULL)
2342                         devid_str_free(minor);
2343                 devid_free(devid);
2344         }
2345         (void) close(fd);
2346
2347         return (ret);
2348 }
2349
2350 /*
2351  * Issue the necessary ioctl() to update the stored path value for the vdev.  We
2352  * ignore any failure here, since a common case is for an unprivileged user to
2353  * type 'zpool status', and we'll display the correct information anyway.
2354  */
2355 static void
2356 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2357 {
2358         zfs_cmd_t zc = { 0 };
2359
2360         (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2361         (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2362         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2363             &zc.zc_guid) == 0);
2364
2365         (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2366 }
2367
2368 /*
2369  * Given a vdev, return the name to display in iostat.  If the vdev has a path,
2370  * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2371  * We also check if this is a whole disk, in which case we strip off the
2372  * trailing 's0' slice name.
2373  *
2374  * This routine is also responsible for identifying when disks have been
2375  * reconfigured in a new location.  The kernel will have opened the device by
2376  * devid, but the path will still refer to the old location.  To catch this, we
2377  * first do a path -> devid translation (which is fast for the common case).  If
2378  * the devid matches, we're done.  If not, we do a reverse devid -> path
2379  * translation and issue the appropriate ioctl() to update the path of the vdev.
2380  * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2381  * of these checks.
2382  */
2383 char *
2384 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2385 {
2386         char *path, *devid;
2387         uint64_t value;
2388         char buf[64];
2389         vdev_stat_t *vs;
2390         uint_t vsc;
2391
2392         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2393             &value) == 0) {
2394                 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2395                     &value) == 0);
2396                 (void) snprintf(buf, sizeof (buf), "%llu",
2397                     (u_longlong_t)value);
2398                 path = buf;
2399         } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2400
2401                 /*
2402                  * If the device is dead (faulted, offline, etc) then don't
2403                  * bother opening it.  Otherwise we may be forcing the user to
2404                  * open a misbehaving device, which can have undesirable
2405                  * effects.
2406                  */
2407                 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2408                     (uint64_t **)&vs, &vsc) != 0 ||
2409                     vs->vs_state >= VDEV_STATE_DEGRADED) &&
2410                     zhp != NULL &&
2411                     nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2412                         /*
2413                          * Determine if the current path is correct.
2414                          */
2415                         char *newdevid = path_to_devid(path);
2416
2417                         if (newdevid == NULL ||
2418                             strcmp(devid, newdevid) != 0) {
2419                                 char *newpath;
2420
2421                                 if ((newpath = devid_to_path(devid)) != NULL) {
2422                                         /*
2423                                          * Update the path appropriately.
2424                                          */
2425                                         set_path(zhp, nv, newpath);
2426                                         if (nvlist_add_string(nv,
2427                                             ZPOOL_CONFIG_PATH, newpath) == 0)
2428                                                 verify(nvlist_lookup_string(nv,
2429                                                     ZPOOL_CONFIG_PATH,
2430                                                     &path) == 0);
2431                                         free(newpath);
2432                                 }
2433                         }
2434
2435                         if (newdevid)
2436                                 devid_str_free(newdevid);
2437                 }
2438
2439                 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2440                         path += sizeof(_PATH_DEV) - 1;
2441
2442                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2443                     &value) == 0 && value) {
2444                         char *tmp = zfs_strdup(hdl, path);
2445                         if (tmp == NULL)
2446                                 return (NULL);
2447                         tmp[strlen(path) - 2] = '\0';
2448                         return (tmp);
2449                 }
2450         } else {
2451                 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2452
2453                 /*
2454                  * If it's a raidz device, we need to stick in the parity level.
2455                  */
2456                 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2457                         verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2458                             &value) == 0);
2459                         (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2460                             (u_longlong_t)value);
2461                         path = buf;
2462                 }
2463         }
2464
2465         return (zfs_strdup(hdl, path));
2466 }
2467
2468 static int
2469 zbookmark_compare(const void *a, const void *b)
2470 {
2471         return (memcmp(a, b, sizeof (zbookmark_t)));
2472 }
2473
2474 /*
2475  * Retrieve the persistent error log, uniquify the members, and return to the
2476  * caller.
2477  */
2478 int
2479 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2480 {
2481         zfs_cmd_t zc = { 0 };
2482         uint64_t count;
2483         zbookmark_t *zb = NULL;
2484         int i;
2485
2486         /*
2487          * Retrieve the raw error list from the kernel.  If the number of errors
2488          * has increased, allocate more space and continue until we get the
2489          * entire list.
2490          */
2491         verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2492             &count) == 0);
2493         if (count == 0)
2494                 return (0);
2495         if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2496             count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2497                 return (-1);
2498         zc.zc_nvlist_dst_size = count;
2499         (void) strcpy(zc.zc_name, zhp->zpool_name);
2500         for (;;) {
2501                 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2502                     &zc) != 0) {
2503                         free((void *)(uintptr_t)zc.zc_nvlist_dst);
2504                         if (errno == ENOMEM) {
2505                                 count = zc.zc_nvlist_dst_size;
2506                                 if ((zc.zc_nvlist_dst = (uintptr_t)
2507                                     zfs_alloc(zhp->zpool_hdl, count *
2508                                     sizeof (zbookmark_t))) == (uintptr_t)NULL)
2509                                         return (-1);
2510                         } else {
2511                                 return (-1);
2512                         }
2513                 } else {
2514                         break;
2515                 }
2516         }
2517
2518         /*
2519          * Sort the resulting bookmarks.  This is a little confusing due to the
2520          * implementation of ZFS_IOC_ERROR_LOG.  The bookmarks are copied last
2521          * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2522          * _not_ copied as part of the process.  So we point the start of our
2523          * array appropriate and decrement the total number of elements.
2524          */
2525         zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2526             zc.zc_nvlist_dst_size;
2527         count -= zc.zc_nvlist_dst_size;
2528
2529         qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2530
2531         verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2532
2533         /*
2534          * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2535          */
2536         for (i = 0; i < count; i++) {
2537                 nvlist_t *nv;
2538
2539                 /* ignoring zb_blkid and zb_level for now */
2540                 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2541                     zb[i-1].zb_object == zb[i].zb_object)
2542                         continue;
2543
2544                 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2545                         goto nomem;
2546                 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2547                     zb[i].zb_objset) != 0) {
2548                         nvlist_free(nv);
2549                         goto nomem;
2550                 }
2551                 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2552                     zb[i].zb_object) != 0) {
2553                         nvlist_free(nv);
2554                         goto nomem;
2555                 }
2556                 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2557                         nvlist_free(nv);
2558                         goto nomem;
2559                 }
2560                 nvlist_free(nv);
2561         }
2562
2563         free((void *)(uintptr_t)zc.zc_nvlist_dst);
2564         return (0);
2565
2566 nomem:
2567         free((void *)(uintptr_t)zc.zc_nvlist_dst);
2568         return (no_memory(zhp->zpool_hdl));
2569 }
2570
2571 /*
2572  * Upgrade a ZFS pool to the latest on-disk version.
2573  */
2574 int
2575 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2576 {
2577         zfs_cmd_t zc = { 0 };
2578         libzfs_handle_t *hdl = zhp->zpool_hdl;
2579
2580         (void) strcpy(zc.zc_name, zhp->zpool_name);
2581         zc.zc_cookie = new_version;
2582
2583         if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2584                 return (zpool_standard_error_fmt(hdl, errno,
2585                     dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2586                     zhp->zpool_name));
2587         return (0);
2588 }
2589
2590 void
2591 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2592     char *history_str)
2593 {
2594         int i;
2595
2596         (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2597         for (i = 1; i < argc; i++) {
2598                 if (strlen(history_str) + 1 + strlen(argv[i]) >
2599                     HIS_MAX_RECORD_LEN)
2600                         break;
2601                 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2602                 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2603         }
2604 }
2605
2606 /*
2607  * Stage command history for logging.
2608  */
2609 int
2610 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2611 {
2612         if (history_str == NULL)
2613                 return (EINVAL);
2614
2615         if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2616                 return (EINVAL);
2617
2618         if (hdl->libzfs_log_str != NULL)
2619                 free(hdl->libzfs_log_str);
2620
2621         if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2622                 return (no_memory(hdl));
2623
2624         return (0);
2625 }
2626
2627 /*
2628  * Perform ioctl to get some command history of a pool.
2629  *
2630  * 'buf' is the buffer to fill up to 'len' bytes.  'off' is the
2631  * logical offset of the history buffer to start reading from.
2632  *
2633  * Upon return, 'off' is the next logical offset to read from and
2634  * 'len' is the actual amount of bytes read into 'buf'.
2635  */
2636 static int
2637 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2638 {
2639         zfs_cmd_t zc = { 0 };
2640         libzfs_handle_t *hdl = zhp->zpool_hdl;
2641
2642         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2643
2644         zc.zc_history = (uint64_t)(uintptr_t)buf;
2645         zc.zc_history_len = *len;
2646         zc.zc_history_offset = *off;
2647
2648         if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2649                 switch (errno) {
2650                 case EPERM:
2651                         return (zfs_error_fmt(hdl, EZFS_PERM,
2652                             dgettext(TEXT_DOMAIN,
2653                             "cannot show history for pool '%s'"),
2654                             zhp->zpool_name));
2655                 case ENOENT:
2656                         return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2657                             dgettext(TEXT_DOMAIN, "cannot get history for pool "
2658                             "'%s'"), zhp->zpool_name));
2659                 case ENOTSUP:
2660                         return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2661                             dgettext(TEXT_DOMAIN, "cannot get history for pool "
2662                             "'%s', pool must be upgraded"), zhp->zpool_name));
2663                 default:
2664                         return (zpool_standard_error_fmt(hdl, errno,
2665                             dgettext(TEXT_DOMAIN,
2666                             "cannot get history for '%s'"), zhp->zpool_name));
2667                 }
2668         }
2669
2670         *len = zc.zc_history_len;
2671         *off = zc.zc_history_offset;
2672
2673         return (0);
2674 }
2675
2676 /*
2677  * Process the buffer of nvlists, unpacking and storing each nvlist record
2678  * into 'records'.  'leftover' is set to the number of bytes that weren't
2679  * processed as there wasn't a complete record.
2680  */
2681 static int
2682 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2683     nvlist_t ***records, uint_t *numrecords)
2684 {
2685         uint64_t reclen;
2686         nvlist_t *nv;
2687         int i;
2688
2689         while (bytes_read > sizeof (reclen)) {
2690
2691                 /* get length of packed record (stored as little endian) */
2692                 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2693                         reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2694
2695                 if (bytes_read < sizeof (reclen) + reclen)
2696                         break;
2697
2698                 /* unpack record */
2699                 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2700                         return (ENOMEM);
2701                 bytes_read -= sizeof (reclen) + reclen;
2702                 buf += sizeof (reclen) + reclen;
2703
2704                 /* add record to nvlist array */
2705                 (*numrecords)++;
2706                 if (ISP2(*numrecords + 1)) {
2707                         *records = realloc(*records,
2708                             *numrecords * 2 * sizeof (nvlist_t *));
2709                 }
2710                 (*records)[*numrecords - 1] = nv;
2711         }
2712
2713         *leftover = bytes_read;
2714         return (0);
2715 }
2716
2717 #define HIS_BUF_LEN     (128*1024)
2718
2719 /*
2720  * Retrieve the command history of a pool.
2721  */
2722 int
2723 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2724 {
2725         char buf[HIS_BUF_LEN];
2726         uint64_t off = 0;
2727         nvlist_t **records = NULL;
2728         uint_t numrecords = 0;
2729         int err, i;
2730
2731         do {
2732                 uint64_t bytes_read = sizeof (buf);
2733                 uint64_t leftover;
2734
2735                 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2736                         break;
2737
2738                 /* if nothing else was read in, we're at EOF, just return */
2739                 if (!bytes_read)
2740                         break;
2741
2742                 if ((err = zpool_history_unpack(buf, bytes_read,
2743                     &leftover, &records, &numrecords)) != 0)
2744                         break;
2745                 off -= leftover;
2746
2747                 /* CONSTCOND */
2748         } while (1);
2749
2750         if (!err) {
2751                 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2752                 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2753                     records, numrecords) == 0);
2754         }
2755         for (i = 0; i < numrecords; i++)
2756                 nvlist_free(records[i]);
2757         free(records);
2758
2759         return (err);
2760 }
2761
2762 void
2763 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2764     char *pathname, size_t len)
2765 {
2766         zfs_cmd_t zc = { 0 };
2767         boolean_t mounted = B_FALSE;
2768         char *mntpnt = NULL;
2769         char dsname[MAXNAMELEN];
2770
2771         if (dsobj == 0) {
2772                 /* special case for the MOS */
2773                 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2774                 return;
2775         }
2776
2777         /* get the dataset's name */
2778         (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2779         zc.zc_obj = dsobj;
2780         if (ioctl(zhp->zpool_hdl->libzfs_fd,
2781             ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2782                 /* just write out a path of two object numbers */
2783                 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2784                     dsobj, obj);
2785                 return;
2786         }
2787         (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2788
2789         /* find out if the dataset is mounted */
2790         mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2791
2792         /* get the corrupted object's path */
2793         (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2794         zc.zc_obj = obj;
2795         if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2796             &zc) == 0) {
2797                 if (mounted) {
2798                         (void) snprintf(pathname, len, "%s%s", mntpnt,
2799                             zc.zc_value);
2800                 } else {
2801                         (void) snprintf(pathname, len, "%s:%s",
2802                             dsname, zc.zc_value);
2803                 }
2804         } else {
2805                 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2806         }
2807         free(mntpnt);
2808 }
2809
2810 #define RDISK_ROOT      "/dev/rdsk"
2811 #define BACKUP_SLICE    "s2"
2812 /*
2813  * Don't start the slice at the default block of 34; many storage
2814  * devices will use a stripe width of 128k, so start there instead.
2815  */
2816 #define NEW_START_BLOCK 256
2817
2818 #if defined(sun)
2819 /*
2820  * Read the EFI label from the config, if a label does not exist then
2821  * pass back the error to the caller. If the caller has passed a non-NULL
2822  * diskaddr argument then we set it to the starting address of the EFI
2823  * partition.
2824  */
2825 static int
2826 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2827 {
2828         char *path;
2829         int fd;
2830         char diskname[MAXPATHLEN];
2831         int err = -1;
2832
2833         if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2834                 return (err);
2835
2836         (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2837             strrchr(path, '/'));
2838         if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2839                 struct dk_gpt *vtoc;
2840
2841                 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2842                         if (sb != NULL)
2843                                 *sb = vtoc->efi_parts[0].p_start;
2844                         efi_free(vtoc);
2845                 }
2846                 (void) close(fd);
2847         }
2848         return (err);
2849 }
2850
2851 /*
2852  * determine where a partition starts on a disk in the current
2853  * configuration
2854  */
2855 static diskaddr_t
2856 find_start_block(nvlist_t *config)
2857 {
2858         nvlist_t **child;
2859         uint_t c, children;
2860         diskaddr_t sb = MAXOFFSET_T;
2861         uint64_t wholedisk;
2862
2863         if (nvlist_lookup_nvlist_array(config,
2864             ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2865                 if (nvlist_lookup_uint64(config,
2866                     ZPOOL_CONFIG_WHOLE_DISK,
2867                     &wholedisk) != 0 || !wholedisk) {
2868                         return (MAXOFFSET_T);
2869                 }
2870                 if (read_efi_label(config, &sb) < 0)
2871                         sb = MAXOFFSET_T;
2872                 return (sb);
2873         }
2874
2875         for (c = 0; c < children; c++) {
2876                 sb = find_start_block(child[c]);
2877                 if (sb != MAXOFFSET_T) {
2878                         return (sb);
2879                 }
2880         }
2881         return (MAXOFFSET_T);
2882 }
2883 #endif /* sun */
2884
2885 /*
2886  * Label an individual disk.  The name provided is the short name,
2887  * stripped of any leading /dev path.
2888  */
2889 int
2890 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2891 {
2892 #if defined(sun)
2893         char path[MAXPATHLEN];
2894         struct dk_gpt *vtoc;
2895         int fd;
2896         size_t resv = EFI_MIN_RESV_SIZE;
2897         uint64_t slice_size;
2898         diskaddr_t start_block;
2899         char errbuf[1024];
2900
2901         /* prepare an error message just in case */
2902         (void) snprintf(errbuf, sizeof (errbuf),
2903             dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2904
2905         if (zhp) {
2906                 nvlist_t *nvroot;
2907
2908                 if (pool_is_bootable(zhp)) {
2909                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2910                             "EFI labeled devices are not supported on root "
2911                             "pools."));
2912                         return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2913                 }
2914
2915                 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2916                     ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2917
2918                 if (zhp->zpool_start_block == 0)
2919                         start_block = find_start_block(nvroot);
2920                 else
2921                         start_block = zhp->zpool_start_block;
2922                 zhp->zpool_start_block = start_block;
2923         } else {
2924                 /* new pool */
2925                 start_block = NEW_START_BLOCK;
2926         }
2927
2928         (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2929             BACKUP_SLICE);
2930
2931         if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2932                 /*
2933                  * This shouldn't happen.  We've long since verified that this
2934                  * is a valid device.
2935                  */
2936                 zfs_error_aux(hdl,
2937                     dgettext(TEXT_DOMAIN, "unable to open device"));
2938                 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2939         }
2940
2941         if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2942                 /*
2943                  * The only way this can fail is if we run out of memory, or we
2944                  * were unable to read the disk's capacity
2945                  */
2946                 if (errno == ENOMEM)
2947                         (void) no_memory(hdl);
2948
2949                 (void) close(fd);
2950                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2951                     "unable to read disk capacity"), name);
2952
2953                 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2954         }
2955
2956         slice_size = vtoc->efi_last_u_lba + 1;
2957         slice_size -= EFI_MIN_RESV_SIZE;
2958         if (start_block == MAXOFFSET_T)
2959                 start_block = NEW_START_BLOCK;
2960         slice_size -= start_block;
2961
2962         vtoc->efi_parts[0].p_start = start_block;
2963         vtoc->efi_parts[0].p_size = slice_size;
2964
2965         /*
2966          * Why we use V_USR: V_BACKUP confuses users, and is considered
2967          * disposable by some EFI utilities (since EFI doesn't have a backup
2968          * slice).  V_UNASSIGNED is supposed to be used only for zero size
2969          * partitions, and efi_write() will fail if we use it.  V_ROOT, V_BOOT,
2970          * etc. were all pretty specific.  V_USR is as close to reality as we
2971          * can get, in the absence of V_OTHER.
2972          */
2973         vtoc->efi_parts[0].p_tag = V_USR;
2974         (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2975
2976         vtoc->efi_parts[8].p_start = slice_size + start_block;
2977         vtoc->efi_parts[8].p_size = resv;
2978         vtoc->efi_parts[8].p_tag = V_RESERVED;
2979
2980         if (efi_write(fd, vtoc) != 0) {
2981                 /*
2982                  * Some block drivers (like pcata) may not support EFI
2983                  * GPT labels.  Print out a helpful error message dir-
2984                  * ecting the user to manually label the disk and give
2985                  * a specific slice.
2986                  */
2987                 (void) close(fd);
2988                 efi_free(vtoc);
2989
2990                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2991                     "try using fdisk(1M) and then provide a specific slice"));
2992                 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
2993         }
2994
2995         (void) close(fd);
2996         efi_free(vtoc);
2997 #endif /* sun */
2998         return (0);
2999 }
3000
3001 static boolean_t
3002 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3003 {
3004         char *type;
3005         nvlist_t **child;
3006         uint_t children, c;
3007
3008         verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3009         if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3010             strcmp(type, VDEV_TYPE_FILE) == 0 ||
3011             strcmp(type, VDEV_TYPE_LOG) == 0 ||
3012             strcmp(type, VDEV_TYPE_MISSING) == 0) {
3013                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3014                     "vdev type '%s' is not supported"), type);
3015                 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3016                 return (B_FALSE);
3017         }
3018         if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3019             &child, &children) == 0) {
3020                 for (c = 0; c < children; c++) {
3021                         if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3022                                 return (B_FALSE);
3023                 }
3024         }
3025         return (B_TRUE);
3026 }
3027
3028 /*
3029  * check if this zvol is allowable for use as a dump device; zero if
3030  * it is, > 0 if it isn't, < 0 if it isn't a zvol
3031  */
3032 int
3033 zvol_check_dump_config(char *arg)
3034 {
3035         zpool_handle_t *zhp = NULL;
3036         nvlist_t *config, *nvroot;
3037         char *p, *volname;
3038         nvlist_t **top;
3039         uint_t toplevels;
3040         libzfs_handle_t *hdl;
3041         char errbuf[1024];
3042         char poolname[ZPOOL_MAXNAMELEN];
3043         int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3044         int ret = 1;
3045
3046         if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3047                 return (-1);
3048         }
3049
3050         (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3051             "dump is not supported on device '%s'"), arg);
3052
3053         if ((hdl = libzfs_init()) == NULL)
3054                 return (1);
3055         libzfs_print_on_error(hdl, B_TRUE);
3056
3057         volname = arg + pathlen;
3058
3059         /* check the configuration of the pool */
3060         if ((p = strchr(volname, '/')) == NULL) {
3061                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3062                     "malformed dataset name"));
3063                 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3064                 return (1);
3065         } else if (p - volname >= ZFS_MAXNAMELEN) {
3066                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3067                     "dataset name is too long"));
3068                 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3069                 return (1);
3070         } else {
3071                 (void) strncpy(poolname, volname, p - volname);
3072                 poolname[p - volname] = '\0';
3073         }
3074
3075         if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3076                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3077                     "could not open pool '%s'"), poolname);
3078                 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3079                 goto out;
3080         }
3081         config = zpool_get_config(zhp, NULL);
3082         if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3083             &nvroot) != 0) {
3084                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085                     "could not obtain vdev configuration for  '%s'"), poolname);
3086                 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3087                 goto out;
3088         }
3089
3090         verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3091             &top, &toplevels) == 0);
3092         if (toplevels != 1) {
3093                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3094                     "'%s' has multiple top level vdevs"), poolname);
3095                 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3096                 goto out;
3097         }
3098
3099         if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
3100                 goto out;
3101         }
3102         ret = 0;
3103
3104 out:
3105         if (zhp)
3106                 zpool_close(zhp);
3107         libzfs_fini(hdl);
3108         return (ret);
3109 }