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