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