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