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