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