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