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