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