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