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