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