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