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