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