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