author | nd150628 |
Tue, 03 Oct 2006 15:01:10 -0700 | |
changeset 2856 | 6f4d5ee1906a |
parent 2676 | 5cee47eddab6 |
child 2926 | acfcfefbc60d |
permissions | -rw-r--r-- |
789 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
1485 | 5 |
* Common Development and Distribution License (the "License"). |
6 |
* You may not use this file except in compliance with the License. |
|
789 | 7 |
* |
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
2082 | 21 |
|
789 | 22 |
/* |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
23 |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
789 | 24 |
* Use is subject to license terms. |
25 |
*/ |
|
26 |
||
27 |
#pragma ident "%Z%%M% %I% %E% SMI" |
|
28 |
||
29 |
#include <assert.h> |
|
30 |
#include <ctype.h> |
|
31 |
#include <errno.h> |
|
32 |
#include <devid.h> |
|
33 |
#include <fcntl.h> |
|
34 |
#include <libintl.h> |
|
35 |
#include <stdio.h> |
|
36 |
#include <stdlib.h> |
|
37 |
#include <string.h> |
|
38 |
#include <unistd.h> |
|
39 |
#include <sys/zfs_ioctl.h> |
|
1544 | 40 |
#include <sys/zio.h> |
789 | 41 |
|
42 |
#include "zfs_namecheck.h" |
|
43 |
#include "libzfs_impl.h" |
|
44 |
||
45 |
/* |
|
46 |
* Validate the given pool name, optionally putting an extended error message in |
|
47 |
* 'buf'. |
|
48 |
*/ |
|
2082 | 49 |
static boolean_t |
50 |
zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool) |
|
789 | 51 |
{ |
52 |
namecheck_err_t why; |
|
53 |
char what; |
|
1773
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
54 |
int ret; |
789 | 55 |
|
1773
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
56 |
ret = pool_namecheck(pool, &why, &what); |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
57 |
|
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
58 |
/* |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
59 |
* The rules for reserved pool names were extended at a later point. |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
60 |
* But we need to support users with existing pools that may now be |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
61 |
* invalid. So we only check for this expanded set of names during a |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
62 |
* create (or import), and only in userland. |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
63 |
*/ |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
64 |
if (ret == 0 && !isopen && |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
65 |
(strncmp(pool, "mirror", 6) == 0 || |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
66 |
strncmp(pool, "raidz", 5) == 0 || |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
67 |
strncmp(pool, "spare", 5) == 0)) { |
2082 | 68 |
zfs_error_aux(hdl, |
69 |
dgettext(TEXT_DOMAIN, "name is reserved")); |
|
70 |
return (B_FALSE); |
|
1773
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
71 |
} |
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
72 |
|
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
73 |
|
d6e4f2855c14
6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents:
1760
diff
changeset
|
74 |
if (ret != 0) { |
2082 | 75 |
if (hdl != NULL) { |
789 | 76 |
switch (why) { |
1003 | 77 |
case NAME_ERR_TOOLONG: |
2082 | 78 |
zfs_error_aux(hdl, |
1003 | 79 |
dgettext(TEXT_DOMAIN, "name is too long")); |
80 |
break; |
|
81 |
||
789 | 82 |
case NAME_ERR_INVALCHAR: |
2082 | 83 |
zfs_error_aux(hdl, |
789 | 84 |
dgettext(TEXT_DOMAIN, "invalid character " |
85 |
"'%c' in pool name"), what); |
|
86 |
break; |
|
87 |
||
88 |
case NAME_ERR_NOLETTER: |
|
2082 | 89 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
90 |
"name must begin with a letter")); |
|
789 | 91 |
break; |
92 |
||
93 |
case NAME_ERR_RESERVED: |
|
2082 | 94 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
95 |
"name is reserved")); |
|
789 | 96 |
break; |
97 |
||
98 |
case NAME_ERR_DISKLIKE: |
|
2082 | 99 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
100 |
"pool name is reserved")); |
|
789 | 101 |
break; |
2856 | 102 |
|
103 |
case NAME_ERR_LEADING_SLASH: |
|
104 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
105 |
"leading slash in name")); |
|
106 |
break; |
|
107 |
||
108 |
case NAME_ERR_EMPTY_COMPONENT: |
|
109 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
110 |
"empty component in name")); |
|
111 |
break; |
|
112 |
||
113 |
case NAME_ERR_TRAILING_SLASH: |
|
114 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
115 |
"trailing slash in name")); |
|
116 |
break; |
|
117 |
||
118 |
case NAME_ERR_MULTIPLE_AT: |
|
119 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
120 |
"multiple '@' delimiters in name")); |
|
121 |
break; |
|
122 |
||
789 | 123 |
} |
124 |
} |
|
2082 | 125 |
return (B_FALSE); |
789 | 126 |
} |
127 |
||
2082 | 128 |
return (B_TRUE); |
789 | 129 |
} |
130 |
||
131 |
/* |
|
132 |
* Set the pool-wide health based on the vdev state of the root vdev. |
|
133 |
*/ |
|
2082 | 134 |
int |
789 | 135 |
set_pool_health(nvlist_t *config) |
136 |
{ |
|
137 |
nvlist_t *nvroot; |
|
138 |
vdev_stat_t *vs; |
|
139 |
uint_t vsc; |
|
140 |
char *health; |
|
141 |
||
142 |
verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, |
|
143 |
&nvroot) == 0); |
|
144 |
verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS, |
|
145 |
(uint64_t **)&vs, &vsc) == 0); |
|
146 |
||
147 |
switch (vs->vs_state) { |
|
148 |
||
149 |
case VDEV_STATE_CLOSED: |
|
150 |
case VDEV_STATE_CANT_OPEN: |
|
151 |
case VDEV_STATE_OFFLINE: |
|
152 |
health = dgettext(TEXT_DOMAIN, "FAULTED"); |
|
153 |
break; |
|
154 |
||
155 |
case VDEV_STATE_DEGRADED: |
|
156 |
health = dgettext(TEXT_DOMAIN, "DEGRADED"); |
|
157 |
break; |
|
158 |
||
159 |
case VDEV_STATE_HEALTHY: |
|
160 |
health = dgettext(TEXT_DOMAIN, "ONLINE"); |
|
161 |
break; |
|
162 |
||
163 |
default: |
|
2082 | 164 |
abort(); |
789 | 165 |
} |
166 |
||
2082 | 167 |
return (nvlist_add_string(config, ZPOOL_CONFIG_POOL_HEALTH, health)); |
789 | 168 |
} |
169 |
||
170 |
/* |
|
171 |
* Open a handle to the given pool, even if the pool is currently in the FAULTED |
|
172 |
* state. |
|
173 |
*/ |
|
174 |
zpool_handle_t * |
|
2082 | 175 |
zpool_open_canfail(libzfs_handle_t *hdl, const char *pool) |
789 | 176 |
{ |
177 |
zpool_handle_t *zhp; |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
178 |
boolean_t missing; |
789 | 179 |
|
180 |
/* |
|
181 |
* Make sure the pool name is valid. |
|
182 |
*/ |
|
2082 | 183 |
if (!zpool_name_valid(hdl, B_TRUE, pool)) { |
184 |
(void) zfs_error(hdl, EZFS_INVALIDNAME, |
|
185 |
dgettext(TEXT_DOMAIN, "cannot open '%s'"), |
|
186 |
pool); |
|
789 | 187 |
return (NULL); |
188 |
} |
|
189 |
||
2082 | 190 |
if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) |
191 |
return (NULL); |
|
789 | 192 |
|
2082 | 193 |
zhp->zpool_hdl = hdl; |
789 | 194 |
(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); |
195 |
||
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
196 |
if (zpool_refresh_stats(zhp, &missing) != 0) { |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
197 |
zpool_close(zhp); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
198 |
return (NULL); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
199 |
} |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
200 |
|
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
201 |
if (missing) { |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
202 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
203 |
"no such pool")); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
204 |
(void) zfs_error(hdl, EZFS_NOENT, |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
205 |
dgettext(TEXT_DOMAIN, "cannot open '%s'"), |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
206 |
pool); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
207 |
zpool_close(zhp); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
208 |
return (NULL); |
789 | 209 |
} |
210 |
||
211 |
return (zhp); |
|
212 |
} |
|
213 |
||
214 |
/* |
|
215 |
* Like the above, but silent on error. Used when iterating over pools (because |
|
216 |
* the configuration cache may be out of date). |
|
217 |
*/ |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
218 |
int |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
219 |
zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret) |
789 | 220 |
{ |
221 |
zpool_handle_t *zhp; |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
222 |
boolean_t missing; |
789 | 223 |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
224 |
if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL) |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
225 |
return (-1); |
789 | 226 |
|
2082 | 227 |
zhp->zpool_hdl = hdl; |
789 | 228 |
(void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name)); |
229 |
||
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
230 |
if (zpool_refresh_stats(zhp, &missing) != 0) { |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
231 |
zpool_close(zhp); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
232 |
return (-1); |
789 | 233 |
} |
234 |
||
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
235 |
if (missing) { |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
236 |
zpool_close(zhp); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
237 |
*ret = NULL; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
238 |
return (0); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
239 |
} |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
240 |
|
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
241 |
*ret = zhp; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
242 |
return (0); |
789 | 243 |
} |
244 |
||
245 |
/* |
|
246 |
* Similar to zpool_open_canfail(), but refuses to open pools in the faulted |
|
247 |
* state. |
|
248 |
*/ |
|
249 |
zpool_handle_t * |
|
2082 | 250 |
zpool_open(libzfs_handle_t *hdl, const char *pool) |
789 | 251 |
{ |
252 |
zpool_handle_t *zhp; |
|
253 |
||
2082 | 254 |
if ((zhp = zpool_open_canfail(hdl, pool)) == NULL) |
789 | 255 |
return (NULL); |
256 |
||
257 |
if (zhp->zpool_state == POOL_STATE_UNAVAIL) { |
|
2082 | 258 |
(void) zfs_error(hdl, EZFS_POOLUNAVAIL, |
259 |
dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name); |
|
789 | 260 |
zpool_close(zhp); |
261 |
return (NULL); |
|
262 |
} |
|
263 |
||
264 |
return (zhp); |
|
265 |
} |
|
266 |
||
267 |
/* |
|
268 |
* Close the handle. Simply frees the memory associated with the handle. |
|
269 |
*/ |
|
270 |
void |
|
271 |
zpool_close(zpool_handle_t *zhp) |
|
272 |
{ |
|
273 |
if (zhp->zpool_config) |
|
274 |
nvlist_free(zhp->zpool_config); |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
275 |
if (zhp->zpool_old_config) |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
276 |
nvlist_free(zhp->zpool_old_config); |
1544 | 277 |
if (zhp->zpool_error_log) { |
278 |
int i; |
|
279 |
for (i = 0; i < zhp->zpool_error_count; i++) |
|
2082 | 280 |
nvlist_free(zhp->zpool_error_log[i]); |
1544 | 281 |
free(zhp->zpool_error_log); |
282 |
} |
|
789 | 283 |
free(zhp); |
284 |
} |
|
285 |
||
286 |
/* |
|
287 |
* Return the name of the pool. |
|
288 |
*/ |
|
289 |
const char * |
|
290 |
zpool_get_name(zpool_handle_t *zhp) |
|
291 |
{ |
|
292 |
return (zhp->zpool_name); |
|
293 |
} |
|
294 |
||
295 |
/* |
|
296 |
* Return the GUID of the pool. |
|
297 |
*/ |
|
298 |
uint64_t |
|
299 |
zpool_get_guid(zpool_handle_t *zhp) |
|
300 |
{ |
|
301 |
uint64_t guid; |
|
302 |
||
303 |
verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID, |
|
304 |
&guid) == 0); |
|
305 |
return (guid); |
|
306 |
} |
|
307 |
||
308 |
/* |
|
2082 | 309 |
* Return the version of the pool. |
310 |
*/ |
|
311 |
uint64_t |
|
312 |
zpool_get_version(zpool_handle_t *zhp) |
|
313 |
{ |
|
314 |
uint64_t version; |
|
315 |
||
316 |
verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_VERSION, |
|
317 |
&version) == 0); |
|
318 |
||
319 |
return (version); |
|
320 |
} |
|
321 |
||
322 |
/* |
|
789 | 323 |
* Return the amount of space currently consumed by the pool. |
324 |
*/ |
|
325 |
uint64_t |
|
326 |
zpool_get_space_used(zpool_handle_t *zhp) |
|
327 |
{ |
|
328 |
nvlist_t *nvroot; |
|
329 |
vdev_stat_t *vs; |
|
330 |
uint_t vsc; |
|
331 |
||
332 |
verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, |
|
333 |
&nvroot) == 0); |
|
334 |
verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS, |
|
335 |
(uint64_t **)&vs, &vsc) == 0); |
|
336 |
||
337 |
return (vs->vs_alloc); |
|
338 |
} |
|
339 |
||
340 |
/* |
|
341 |
* Return the total space in the pool. |
|
342 |
*/ |
|
343 |
uint64_t |
|
344 |
zpool_get_space_total(zpool_handle_t *zhp) |
|
345 |
{ |
|
346 |
nvlist_t *nvroot; |
|
347 |
vdev_stat_t *vs; |
|
348 |
uint_t vsc; |
|
349 |
||
350 |
verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, |
|
351 |
&nvroot) == 0); |
|
352 |
verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS, |
|
353 |
(uint64_t **)&vs, &vsc) == 0); |
|
354 |
||
355 |
return (vs->vs_space); |
|
356 |
} |
|
357 |
||
358 |
/* |
|
359 |
* Return the alternate root for this pool, if any. |
|
360 |
*/ |
|
361 |
int |
|
362 |
zpool_get_root(zpool_handle_t *zhp, char *buf, size_t buflen) |
|
363 |
{ |
|
364 |
zfs_cmd_t zc = { 0 }; |
|
365 |
||
366 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
2082 | 367 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 || |
2676 | 368 |
zc.zc_value[0] == '\0') |
789 | 369 |
return (-1); |
370 |
||
2676 | 371 |
(void) strlcpy(buf, zc.zc_value, buflen); |
789 | 372 |
|
373 |
return (0); |
|
374 |
} |
|
375 |
||
376 |
/* |
|
377 |
* Return the state of the pool (ACTIVE or UNAVAILABLE) |
|
378 |
*/ |
|
379 |
int |
|
380 |
zpool_get_state(zpool_handle_t *zhp) |
|
381 |
{ |
|
382 |
return (zhp->zpool_state); |
|
383 |
} |
|
384 |
||
385 |
/* |
|
386 |
* Create the named pool, using the provided vdev list. It is assumed |
|
387 |
* that the consumer has already validated the contents of the nvlist, so we |
|
388 |
* don't have to worry about error semantics. |
|
389 |
*/ |
|
390 |
int |
|
2082 | 391 |
zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot, |
392 |
const char *altroot) |
|
789 | 393 |
{ |
394 |
zfs_cmd_t zc = { 0 }; |
|
2082 | 395 |
char msg[1024]; |
396 |
||
397 |
(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, |
|
398 |
"cannot create '%s'"), pool); |
|
789 | 399 |
|
2082 | 400 |
if (!zpool_name_valid(hdl, B_FALSE, pool)) |
401 |
return (zfs_error(hdl, EZFS_INVALIDNAME, msg)); |
|
402 |
||
403 |
if (altroot != NULL && altroot[0] != '/') |
|
404 |
return (zfs_error(hdl, EZFS_BADPATH, |
|
405 |
dgettext(TEXT_DOMAIN, "bad alternate root '%s'"), altroot)); |
|
789 | 406 |
|
2676 | 407 |
if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0) |
789 | 408 |
return (-1); |
409 |
||
410 |
(void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name)); |
|
411 |
||
412 |
if (altroot != NULL) |
|
2676 | 413 |
(void) strlcpy(zc.zc_value, altroot, sizeof (zc.zc_value)); |
789 | 414 |
|
2082 | 415 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CREATE, &zc) != 0) { |
2676 | 416 |
zcmd_free_nvlists(&zc); |
2082 | 417 |
|
789 | 418 |
switch (errno) { |
419 |
case EBUSY: |
|
420 |
/* |
|
421 |
* This can happen if the user has specified the same |
|
422 |
* device multiple times. We can't reliably detect this |
|
423 |
* until we try to add it and see we already have a |
|
424 |
* label. |
|
425 |
*/ |
|
2082 | 426 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
427 |
"one or more vdevs refer to the same device")); |
|
428 |
return (zfs_error(hdl, EZFS_BADDEV, msg)); |
|
789 | 429 |
|
430 |
case EOVERFLOW: |
|
431 |
/* |
|
2082 | 432 |
* This occurs when one of the devices is below |
789 | 433 |
* SPA_MINDEVSIZE. Unfortunately, we can't detect which |
434 |
* device was the problem device since there's no |
|
435 |
* reliable way to determine device size from userland. |
|
436 |
*/ |
|
437 |
{ |
|
438 |
char buf[64]; |
|
439 |
||
440 |
zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); |
|
441 |
||
2082 | 442 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
443 |
"one or more devices is less than the " |
|
444 |
"minimum size (%s)"), buf); |
|
789 | 445 |
} |
2082 | 446 |
return (zfs_error(hdl, EZFS_BADDEV, msg)); |
789 | 447 |
|
448 |
case ENOSPC: |
|
2082 | 449 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
450 |
"one or more devices is out of space")); |
|
451 |
return (zfs_error(hdl, EZFS_BADDEV, msg)); |
|
789 | 452 |
|
453 |
default: |
|
2082 | 454 |
return (zpool_standard_error(hdl, errno, msg)); |
789 | 455 |
} |
456 |
} |
|
457 |
||
2676 | 458 |
zcmd_free_nvlists(&zc); |
789 | 459 |
|
460 |
/* |
|
461 |
* If this is an alternate root pool, then we automatically set the |
|
2676 | 462 |
* mountpoint of the root dataset to be '/'. |
789 | 463 |
*/ |
464 |
if (altroot != NULL) { |
|
465 |
zfs_handle_t *zhp; |
|
466 |
||
2082 | 467 |
verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_ANY)) != NULL); |
2676 | 468 |
verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), |
469 |
"/") == 0); |
|
789 | 470 |
|
471 |
zfs_close(zhp); |
|
472 |
} |
|
473 |
||
474 |
return (0); |
|
475 |
} |
|
476 |
||
477 |
/* |
|
478 |
* Destroy the given pool. It is up to the caller to ensure that there are no |
|
479 |
* datasets left in the pool. |
|
480 |
*/ |
|
481 |
int |
|
482 |
zpool_destroy(zpool_handle_t *zhp) |
|
483 |
{ |
|
484 |
zfs_cmd_t zc = { 0 }; |
|
485 |
zfs_handle_t *zfp = NULL; |
|
2082 | 486 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
487 |
char msg[1024]; |
|
789 | 488 |
|
489 |
if (zhp->zpool_state == POOL_STATE_ACTIVE && |
|
2082 | 490 |
(zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, |
491 |
ZFS_TYPE_FILESYSTEM)) == NULL) |
|
789 | 492 |
return (-1); |
493 |
||
2856 | 494 |
if (zpool_remove_zvol_links(zhp) != 0) |
789 | 495 |
return (-1); |
496 |
||
497 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
498 |
||
2082 | 499 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_DESTROY, &zc) != 0) { |
500 |
(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, |
|
501 |
"cannot destroy '%s'"), zhp->zpool_name); |
|
789 | 502 |
|
2082 | 503 |
if (errno == EROFS) { |
504 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
505 |
"one or more devices is read only")); |
|
506 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
|
507 |
} else { |
|
508 |
(void) zpool_standard_error(hdl, errno, msg); |
|
789 | 509 |
} |
510 |
||
511 |
if (zfp) |
|
512 |
zfs_close(zfp); |
|
513 |
return (-1); |
|
514 |
} |
|
515 |
||
516 |
if (zfp) { |
|
517 |
remove_mountpoint(zfp); |
|
518 |
zfs_close(zfp); |
|
519 |
} |
|
520 |
||
521 |
return (0); |
|
522 |
} |
|
523 |
||
524 |
/* |
|
525 |
* Add the given vdevs to the pool. The caller must have already performed the |
|
526 |
* necessary verification to ensure that the vdev specification is well-formed. |
|
527 |
*/ |
|
528 |
int |
|
529 |
zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot) |
|
530 |
{ |
|
2676 | 531 |
zfs_cmd_t zc = { 0 }; |
2082 | 532 |
int ret; |
533 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
|
534 |
char msg[1024]; |
|
535 |
nvlist_t **spares; |
|
536 |
uint_t nspares; |
|
537 |
||
538 |
(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, |
|
539 |
"cannot add to '%s'"), zhp->zpool_name); |
|
540 |
||
541 |
if (zpool_get_version(zhp) < ZFS_VERSION_SPARES && |
|
542 |
nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
|
543 |
&spares, &nspares) == 0) { |
|
544 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be " |
|
545 |
"upgraded to add hot spares")); |
|
546 |
return (zfs_error(hdl, EZFS_BADVERSION, msg)); |
|
547 |
} |
|
789 | 548 |
|
2676 | 549 |
if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0) |
2082 | 550 |
return (-1); |
789 | 551 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
552 |
||
2082 | 553 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ADD, &zc) != 0) { |
789 | 554 |
switch (errno) { |
555 |
case EBUSY: |
|
556 |
/* |
|
557 |
* This can happen if the user has specified the same |
|
558 |
* device multiple times. We can't reliably detect this |
|
559 |
* until we try to add it and see we already have a |
|
560 |
* label. |
|
561 |
*/ |
|
2082 | 562 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
563 |
"one or more vdevs refer to the same device")); |
|
564 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
|
789 | 565 |
break; |
566 |
||
567 |
case EOVERFLOW: |
|
568 |
/* |
|
569 |
* This occurrs when one of the devices is below |
|
570 |
* SPA_MINDEVSIZE. Unfortunately, we can't detect which |
|
571 |
* device was the problem device since there's no |
|
572 |
* reliable way to determine device size from userland. |
|
573 |
*/ |
|
574 |
{ |
|
575 |
char buf[64]; |
|
576 |
||
577 |
zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf)); |
|
578 |
||
2082 | 579 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
580 |
"device is less than the minimum " |
|
581 |
"size (%s)"), buf); |
|
789 | 582 |
} |
2082 | 583 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
584 |
break; |
|
585 |
||
586 |
case ENOTSUP: |
|
587 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
588 |
"pool must be upgraded to add raidz2 vdevs")); |
|
589 |
(void) zfs_error(hdl, EZFS_BADVERSION, msg); |
|
789 | 590 |
break; |
591 |
||
592 |
default: |
|
2082 | 593 |
(void) zpool_standard_error(hdl, errno, msg); |
789 | 594 |
} |
595 |
||
2082 | 596 |
ret = -1; |
597 |
} else { |
|
598 |
ret = 0; |
|
789 | 599 |
} |
600 |
||
2676 | 601 |
zcmd_free_nvlists(&zc); |
789 | 602 |
|
2082 | 603 |
return (ret); |
789 | 604 |
} |
605 |
||
606 |
/* |
|
607 |
* Exports the pool from the system. The caller must ensure that there are no |
|
608 |
* mounted datasets in the pool. |
|
609 |
*/ |
|
610 |
int |
|
611 |
zpool_export(zpool_handle_t *zhp) |
|
612 |
{ |
|
613 |
zfs_cmd_t zc = { 0 }; |
|
614 |
||
615 |
if (zpool_remove_zvol_links(zhp) != 0) |
|
616 |
return (-1); |
|
617 |
||
618 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
619 |
||
2082 | 620 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_EXPORT, &zc) != 0) |
621 |
return (zpool_standard_error(zhp->zpool_hdl, errno, |
|
622 |
dgettext(TEXT_DOMAIN, "cannot export '%s'"), |
|
623 |
zhp->zpool_name)); |
|
789 | 624 |
|
625 |
return (0); |
|
626 |
} |
|
627 |
||
628 |
/* |
|
629 |
* Import the given pool using the known configuration. The configuration |
|
630 |
* should have come from zpool_find_import(). The 'newname' and 'altroot' |
|
631 |
* parameters control whether the pool is imported with a different name or with |
|
632 |
* an alternate root, respectively. |
|
633 |
*/ |
|
634 |
int |
|
2082 | 635 |
zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname, |
636 |
const char *altroot) |
|
789 | 637 |
{ |
2676 | 638 |
zfs_cmd_t zc = { 0 }; |
789 | 639 |
char *thename; |
640 |
char *origname; |
|
641 |
int ret; |
|
642 |
||
643 |
verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, |
|
644 |
&origname) == 0); |
|
645 |
||
646 |
if (newname != NULL) { |
|
2082 | 647 |
if (!zpool_name_valid(hdl, B_FALSE, newname)) |
648 |
return (zfs_error(hdl, EZFS_INVALIDNAME, |
|
649 |
dgettext(TEXT_DOMAIN, "cannot import '%s'"), |
|
650 |
newname)); |
|
789 | 651 |
thename = (char *)newname; |
652 |
} else { |
|
653 |
thename = origname; |
|
654 |
} |
|
655 |
||
2082 | 656 |
if (altroot != NULL && altroot[0] != '/') |
657 |
return (zfs_error(hdl, EZFS_BADPATH, |
|
658 |
dgettext(TEXT_DOMAIN, "bad alternate root '%s'"), |
|
659 |
altroot)); |
|
789 | 660 |
|
661 |
(void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name)); |
|
662 |
||
663 |
if (altroot != NULL) |
|
2676 | 664 |
(void) strlcpy(zc.zc_value, altroot, sizeof (zc.zc_value)); |
789 | 665 |
else |
2676 | 666 |
zc.zc_value[0] = '\0'; |
789 | 667 |
|
668 |
verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, |
|
1544 | 669 |
&zc.zc_guid) == 0); |
789 | 670 |
|
2676 | 671 |
if (zcmd_write_src_nvlist(hdl, &zc, config, NULL) != 0) |
2082 | 672 |
return (-1); |
789 | 673 |
|
674 |
ret = 0; |
|
2082 | 675 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_IMPORT, &zc) != 0) { |
789 | 676 |
char desc[1024]; |
677 |
if (newname == NULL) |
|
678 |
(void) snprintf(desc, sizeof (desc), |
|
679 |
dgettext(TEXT_DOMAIN, "cannot import '%s'"), |
|
680 |
thename); |
|
681 |
else |
|
682 |
(void) snprintf(desc, sizeof (desc), |
|
683 |
dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"), |
|
684 |
origname, thename); |
|
685 |
||
686 |
switch (errno) { |
|
1544 | 687 |
case ENOTSUP: |
688 |
/* |
|
689 |
* Unsupported version. |
|
690 |
*/ |
|
2082 | 691 |
(void) zfs_error(hdl, EZFS_BADVERSION, desc); |
1544 | 692 |
break; |
693 |
||
2174
73de7a781492
6433717 offline devices should not be marked persistently unavailble
eschrock
parents:
2142
diff
changeset
|
694 |
case EINVAL: |
73de7a781492
6433717 offline devices should not be marked persistently unavailble
eschrock
parents:
2142
diff
changeset
|
695 |
(void) zfs_error(hdl, EZFS_INVALCONFIG, desc); |
73de7a781492
6433717 offline devices should not be marked persistently unavailble
eschrock
parents:
2142
diff
changeset
|
696 |
break; |
73de7a781492
6433717 offline devices should not be marked persistently unavailble
eschrock
parents:
2142
diff
changeset
|
697 |
|
789 | 698 |
default: |
2082 | 699 |
(void) zpool_standard_error(hdl, errno, desc); |
789 | 700 |
} |
701 |
||
702 |
ret = -1; |
|
703 |
} else { |
|
704 |
zpool_handle_t *zhp; |
|
705 |
/* |
|
706 |
* This should never fail, but play it safe anyway. |
|
707 |
*/ |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
708 |
if (zpool_open_silent(hdl, thename, &zhp) != 0) { |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
709 |
ret = -1; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
710 |
} else if (zhp != NULL) { |
789 | 711 |
ret = zpool_create_zvol_links(zhp); |
712 |
zpool_close(zhp); |
|
713 |
} |
|
714 |
} |
|
715 |
||
2676 | 716 |
zcmd_free_nvlists(&zc); |
789 | 717 |
return (ret); |
718 |
} |
|
719 |
||
720 |
/* |
|
721 |
* Scrub the pool. |
|
722 |
*/ |
|
723 |
int |
|
724 |
zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type) |
|
725 |
{ |
|
726 |
zfs_cmd_t zc = { 0 }; |
|
727 |
char msg[1024]; |
|
2082 | 728 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
789 | 729 |
|
730 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
731 |
zc.zc_cookie = type; |
|
732 |
||
2082 | 733 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_SCRUB, &zc) == 0) |
789 | 734 |
return (0); |
735 |
||
736 |
(void) snprintf(msg, sizeof (msg), |
|
737 |
dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name); |
|
738 |
||
2082 | 739 |
if (errno == EBUSY) |
740 |
return (zfs_error(hdl, EZFS_RESILVERING, msg)); |
|
741 |
else |
|
742 |
return (zpool_standard_error(hdl, errno, msg)); |
|
789 | 743 |
} |
744 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
745 |
/* |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
746 |
* 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
747 |
* spare; but FALSE if its an INUSE spare. |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
748 |
*/ |
2082 | 749 |
static nvlist_t * |
750 |
vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid, |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
751 |
boolean_t *avail_spare) |
1544 | 752 |
{ |
753 |
uint_t c, children; |
|
754 |
nvlist_t **child; |
|
2082 | 755 |
uint64_t theguid, present; |
1544 | 756 |
char *path; |
757 |
uint64_t wholedisk = 0; |
|
2082 | 758 |
nvlist_t *ret; |
1544 | 759 |
|
2082 | 760 |
verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0); |
1544 | 761 |
|
762 |
if (search == NULL && |
|
763 |
nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) { |
|
764 |
/* |
|
765 |
* If the device has never been present since import, the only |
|
766 |
* reliable way to match the vdev is by GUID. |
|
767 |
*/ |
|
2082 | 768 |
if (theguid == guid) |
769 |
return (nv); |
|
1544 | 770 |
} else if (search != NULL && |
771 |
nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { |
|
772 |
(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, |
|
773 |
&wholedisk); |
|
774 |
if (wholedisk) { |
|
775 |
/* |
|
776 |
* For whole disks, the internal path has 's0', but the |
|
777 |
* path passed in by the user doesn't. |
|
778 |
*/ |
|
779 |
if (strlen(search) == strlen(path) - 2 && |
|
780 |
strncmp(search, path, strlen(search)) == 0) |
|
2082 | 781 |
return (nv); |
1544 | 782 |
} else if (strcmp(search, path) == 0) { |
2082 | 783 |
return (nv); |
1544 | 784 |
} |
785 |
} |
|
786 |
||
787 |
if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
|
788 |
&child, &children) != 0) |
|
2082 | 789 |
return (NULL); |
1544 | 790 |
|
791 |
for (c = 0; c < children; c++) |
|
2082 | 792 |
if ((ret = vdev_to_nvlist_iter(child[c], search, guid, |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
793 |
avail_spare)) != NULL) |
1544 | 794 |
return (ret); |
795 |
||
2082 | 796 |
if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES, |
797 |
&child, &children) == 0) { |
|
798 |
for (c = 0; c < children; c++) { |
|
799 |
if ((ret = vdev_to_nvlist_iter(child[c], search, guid, |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
800 |
avail_spare)) != NULL) { |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
801 |
*avail_spare = B_TRUE; |
2082 | 802 |
return (ret); |
803 |
} |
|
804 |
} |
|
805 |
} |
|
806 |
||
807 |
return (NULL); |
|
1544 | 808 |
} |
809 |
||
2082 | 810 |
nvlist_t * |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
811 |
zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare) |
1544 | 812 |
{ |
813 |
char buf[MAXPATHLEN]; |
|
814 |
const char *search; |
|
815 |
char *end; |
|
816 |
nvlist_t *nvroot; |
|
817 |
uint64_t guid; |
|
818 |
||
1613
aca102a242d3
6396628 zpool offline does not recognize c0d0 patten recently
eschrock
parents:
1544
diff
changeset
|
819 |
guid = strtoull(path, &end, 10); |
1544 | 820 |
if (guid != 0 && *end == '\0') { |
821 |
search = NULL; |
|
822 |
} else if (path[0] != '/') { |
|
823 |
(void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path); |
|
824 |
search = buf; |
|
825 |
} else { |
|
826 |
search = path; |
|
827 |
} |
|
828 |
||
829 |
verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, |
|
830 |
&nvroot) == 0); |
|
831 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
832 |
*avail_spare = B_FALSE; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
833 |
return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare)); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
834 |
} |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
835 |
|
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
836 |
/* |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
837 |
* Returns TRUE if the given guid corresponds to a spare (INUSE or not). |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
838 |
*/ |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
839 |
static boolean_t |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
840 |
is_spare(zpool_handle_t *zhp, uint64_t guid) |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
841 |
{ |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
842 |
uint64_t spare_guid; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
843 |
nvlist_t *nvroot; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
844 |
nvlist_t **spares; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
845 |
uint_t nspares; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
846 |
int i; |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
847 |
|
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
848 |
verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
849 |
&nvroot) == 0); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
850 |
if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
851 |
&spares, &nspares) == 0) { |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
852 |
for (i = 0; i < nspares; i++) { |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
853 |
verify(nvlist_lookup_uint64(spares[i], |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
854 |
ZPOOL_CONFIG_GUID, &spare_guid) == 0); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
855 |
if (guid == spare_guid) |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
856 |
return (B_TRUE); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
857 |
} |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
858 |
} |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
859 |
|
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
860 |
return (B_FALSE); |
1544 | 861 |
} |
862 |
||
789 | 863 |
/* |
864 |
* Bring the specified vdev online |
|
865 |
*/ |
|
866 |
int |
|
867 |
zpool_vdev_online(zpool_handle_t *zhp, const char *path) |
|
868 |
{ |
|
869 |
zfs_cmd_t zc = { 0 }; |
|
870 |
char msg[1024]; |
|
2082 | 871 |
nvlist_t *tgt; |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
872 |
boolean_t avail_spare; |
2082 | 873 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
789 | 874 |
|
1544 | 875 |
(void) snprintf(msg, sizeof (msg), |
876 |
dgettext(TEXT_DOMAIN, "cannot online %s"), path); |
|
789 | 877 |
|
1544 | 878 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
879 |
if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == NULL) |
2082 | 880 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
789 | 881 |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
882 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
883 |
|
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
884 |
if (avail_spare || is_spare(zhp, zc.zc_guid) == B_TRUE) |
2082 | 885 |
return (zfs_error(hdl, EZFS_ISSPARE, msg)); |
886 |
||
887 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ONLINE, &zc) == 0) |
|
1544 | 888 |
return (0); |
789 | 889 |
|
2082 | 890 |
return (zpool_standard_error(hdl, errno, msg)); |
789 | 891 |
} |
892 |
||
893 |
/* |
|
894 |
* Take the specified vdev offline |
|
895 |
*/ |
|
896 |
int |
|
1485 | 897 |
zpool_vdev_offline(zpool_handle_t *zhp, const char *path, int istmp) |
789 | 898 |
{ |
899 |
zfs_cmd_t zc = { 0 }; |
|
900 |
char msg[1024]; |
|
2082 | 901 |
nvlist_t *tgt; |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
902 |
boolean_t avail_spare; |
2082 | 903 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
789 | 904 |
|
1544 | 905 |
(void) snprintf(msg, sizeof (msg), |
906 |
dgettext(TEXT_DOMAIN, "cannot offline %s"), path); |
|
907 |
||
789 | 908 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
909 |
if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == NULL) |
2082 | 910 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
911 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
912 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
913 |
|
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
914 |
if (avail_spare || is_spare(zhp, zc.zc_guid) == B_TRUE) |
2082 | 915 |
return (zfs_error(hdl, EZFS_ISSPARE, msg)); |
916 |
||
1485 | 917 |
zc.zc_cookie = istmp; |
918 |
||
2082 | 919 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_OFFLINE, &zc) == 0) |
789 | 920 |
return (0); |
921 |
||
922 |
switch (errno) { |
|
2082 | 923 |
case EBUSY: |
789 | 924 |
|
925 |
/* |
|
926 |
* There are no other replicas of this device. |
|
927 |
*/ |
|
2082 | 928 |
return (zfs_error(hdl, EZFS_NOREPLICAS, msg)); |
929 |
||
930 |
default: |
|
931 |
return (zpool_standard_error(hdl, errno, msg)); |
|
932 |
} |
|
933 |
} |
|
789 | 934 |
|
2082 | 935 |
/* |
936 |
* Returns TRUE if the given nvlist is a vdev that was originally swapped in as |
|
937 |
* a hot spare. |
|
938 |
*/ |
|
939 |
static boolean_t |
|
940 |
is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which) |
|
941 |
{ |
|
942 |
nvlist_t **child; |
|
943 |
uint_t c, children; |
|
944 |
char *type; |
|
945 |
||
946 |
if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child, |
|
947 |
&children) == 0) { |
|
948 |
verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE, |
|
949 |
&type) == 0); |
|
950 |
||
951 |
if (strcmp(type, VDEV_TYPE_SPARE) == 0 && |
|
952 |
children == 2 && child[which] == tgt) |
|
953 |
return (B_TRUE); |
|
954 |
||
955 |
for (c = 0; c < children; c++) |
|
956 |
if (is_replacing_spare(child[c], tgt, which)) |
|
957 |
return (B_TRUE); |
|
789 | 958 |
} |
2082 | 959 |
|
960 |
return (B_FALSE); |
|
789 | 961 |
} |
962 |
||
963 |
/* |
|
964 |
* Attach new_disk (fully described by nvroot) to old_disk. |
|
965 |
* If 'replacing' is specified, tne new disk will replace the old one. |
|
966 |
*/ |
|
967 |
int |
|
968 |
zpool_vdev_attach(zpool_handle_t *zhp, |
|
969 |
const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing) |
|
970 |
{ |
|
971 |
zfs_cmd_t zc = { 0 }; |
|
972 |
char msg[1024]; |
|
973 |
int ret; |
|
2082 | 974 |
nvlist_t *tgt; |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
975 |
boolean_t avail_spare; |
2082 | 976 |
uint64_t val; |
977 |
char *path; |
|
978 |
nvlist_t **child; |
|
979 |
uint_t children; |
|
980 |
nvlist_t *config_root; |
|
981 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
|
789 | 982 |
|
1544 | 983 |
if (replacing) |
984 |
(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, |
|
985 |
"cannot replace %s with %s"), old_disk, new_disk); |
|
986 |
else |
|
987 |
(void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN, |
|
988 |
"cannot attach %s to %s"), new_disk, old_disk); |
|
989 |
||
789 | 990 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
991 |
if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare)) == 0) |
2082 | 992 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
993 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
994 |
if (avail_spare) |
2082 | 995 |
return (zfs_error(hdl, EZFS_ISSPARE, msg)); |
996 |
||
997 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); |
|
998 |
zc.zc_cookie = replacing; |
|
999 |
||
1000 |
if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, |
|
1001 |
&child, &children) != 0 || children != 1) { |
|
1002 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
1003 |
"new device must be a single disk")); |
|
1004 |
return (zfs_error(hdl, EZFS_INVALCONFIG, msg)); |
|
1544 | 1005 |
} |
2082 | 1006 |
|
1007 |
verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), |
|
1008 |
ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0); |
|
1009 |
||
1010 |
/* |
|
1011 |
* If the target is a hot spare that has been swapped in, we can only |
|
1012 |
* replace it with another hot spare. |
|
1013 |
*/ |
|
1014 |
if (replacing && |
|
1015 |
nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 && |
|
1016 |
nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1017 |
(zpool_find_vdev(zhp, path, &avail_spare) == NULL || |
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1018 |
!avail_spare) && is_replacing_spare(config_root, tgt, 1)) { |
2082 | 1019 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1020 |
"can only be replaced by another hot spare")); |
|
1021 |
return (zfs_error(hdl, EZFS_BADTARGET, msg)); |
|
1022 |
} |
|
1023 |
||
1024 |
/* |
|
1025 |
* If we are attempting to replace a spare, it canot be applied to an |
|
1026 |
* already spared device. |
|
1027 |
*/ |
|
1028 |
if (replacing && |
|
1029 |
nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 && |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1030 |
zpool_find_vdev(zhp, path, &avail_spare) != NULL && avail_spare && |
2082 | 1031 |
is_replacing_spare(config_root, tgt, 0)) { |
1032 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
1033 |
"device has already been replaced with a spare")); |
|
1034 |
return (zfs_error(hdl, EZFS_BADTARGET, msg)); |
|
1035 |
} |
|
789 | 1036 |
|
2676 | 1037 |
if (zcmd_write_src_nvlist(hdl, &zc, nvroot, NULL) != 0) |
2082 | 1038 |
return (-1); |
789 | 1039 |
|
2082 | 1040 |
ret = ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_ATTACH, &zc); |
789 | 1041 |
|
2676 | 1042 |
zcmd_free_nvlists(&zc); |
789 | 1043 |
|
1044 |
if (ret == 0) |
|
1045 |
return (0); |
|
1046 |
||
1047 |
switch (errno) { |
|
1544 | 1048 |
case ENOTSUP: |
789 | 1049 |
/* |
1050 |
* Can't attach to or replace this type of vdev. |
|
1051 |
*/ |
|
1052 |
if (replacing) |
|
2082 | 1053 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1054 |
"cannot replace a replacing device")); |
|
789 | 1055 |
else |
2082 | 1056 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1057 |
"can only attach to mirrors and top-level " |
|
1058 |
"disks")); |
|
1059 |
(void) zfs_error(hdl, EZFS_BADTARGET, msg); |
|
789 | 1060 |
break; |
1061 |
||
1544 | 1062 |
case EINVAL: |
789 | 1063 |
/* |
1064 |
* The new device must be a single disk. |
|
1065 |
*/ |
|
2082 | 1066 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1067 |
"new device must be a single disk")); |
|
1068 |
(void) zfs_error(hdl, EZFS_INVALCONFIG, msg); |
|
789 | 1069 |
break; |
1070 |
||
1544 | 1071 |
case EBUSY: |
2082 | 1072 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"), |
1073 |
new_disk); |
|
1074 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
|
789 | 1075 |
break; |
1076 |
||
1544 | 1077 |
case EOVERFLOW: |
789 | 1078 |
/* |
1079 |
* The new device is too small. |
|
1080 |
*/ |
|
2082 | 1081 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1082 |
"device is too small")); |
|
1083 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
|
789 | 1084 |
break; |
1085 |
||
1544 | 1086 |
case EDOM: |
789 | 1087 |
/* |
1088 |
* The new device has a different alignment requirement. |
|
1089 |
*/ |
|
2082 | 1090 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1091 |
"devices have different sector alignment")); |
|
1092 |
(void) zfs_error(hdl, EZFS_BADDEV, msg); |
|
789 | 1093 |
break; |
1094 |
||
1544 | 1095 |
case ENAMETOOLONG: |
789 | 1096 |
/* |
1097 |
* The resulting top-level vdev spec won't fit in the label. |
|
1098 |
*/ |
|
2082 | 1099 |
(void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg); |
789 | 1100 |
break; |
1101 |
||
1544 | 1102 |
default: |
2082 | 1103 |
(void) zpool_standard_error(hdl, errno, msg); |
789 | 1104 |
} |
1105 |
||
2082 | 1106 |
return (-1); |
789 | 1107 |
} |
1108 |
||
1109 |
/* |
|
1110 |
* Detach the specified device. |
|
1111 |
*/ |
|
1112 |
int |
|
1113 |
zpool_vdev_detach(zpool_handle_t *zhp, const char *path) |
|
1114 |
{ |
|
1115 |
zfs_cmd_t zc = { 0 }; |
|
1116 |
char msg[1024]; |
|
2082 | 1117 |
nvlist_t *tgt; |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1118 |
boolean_t avail_spare; |
2082 | 1119 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
789 | 1120 |
|
1544 | 1121 |
(void) snprintf(msg, sizeof (msg), |
1122 |
dgettext(TEXT_DOMAIN, "cannot detach %s"), path); |
|
1123 |
||
789 | 1124 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1125 |
if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0) |
2082 | 1126 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
789 | 1127 |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1128 |
if (avail_spare) |
2082 | 1129 |
return (zfs_error(hdl, EZFS_ISSPARE, msg)); |
1130 |
||
1131 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); |
|
1132 |
||
1133 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_DETACH, &zc) == 0) |
|
789 | 1134 |
return (0); |
1135 |
||
1136 |
switch (errno) { |
|
1137 |
||
1544 | 1138 |
case ENOTSUP: |
789 | 1139 |
/* |
1140 |
* Can't detach from this type of vdev. |
|
1141 |
*/ |
|
2082 | 1142 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only " |
1143 |
"applicable to mirror and replacing vdevs")); |
|
1144 |
(void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg); |
|
789 | 1145 |
break; |
1146 |
||
1544 | 1147 |
case EBUSY: |
789 | 1148 |
/* |
1149 |
* There are no other replicas of this device. |
|
1150 |
*/ |
|
2082 | 1151 |
(void) zfs_error(hdl, EZFS_NOREPLICAS, msg); |
789 | 1152 |
break; |
1153 |
||
1544 | 1154 |
default: |
2082 | 1155 |
(void) zpool_standard_error(hdl, errno, msg); |
1544 | 1156 |
} |
1157 |
||
2082 | 1158 |
return (-1); |
1159 |
} |
|
1160 |
||
1161 |
/* |
|
1162 |
* Remove the given device. Currently, this is supported only for hot spares. |
|
1163 |
*/ |
|
1164 |
int |
|
1165 |
zpool_vdev_remove(zpool_handle_t *zhp, const char *path) |
|
1166 |
{ |
|
1167 |
zfs_cmd_t zc = { 0 }; |
|
1168 |
char msg[1024]; |
|
1169 |
nvlist_t *tgt; |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1170 |
boolean_t avail_spare; |
2082 | 1171 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
1172 |
||
1173 |
(void) snprintf(msg, sizeof (msg), |
|
1174 |
dgettext(TEXT_DOMAIN, "cannot remove %s"), path); |
|
1175 |
||
1176 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1177 |
if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0) |
2082 | 1178 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
1179 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1180 |
if (!avail_spare) { |
2082 | 1181 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1182 |
"only hot spares can be removed")); |
|
1183 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
|
1184 |
} |
|
1185 |
||
1186 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0); |
|
1187 |
||
1188 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_REMOVE, &zc) == 0) |
|
1189 |
return (0); |
|
1190 |
||
1191 |
return (zpool_standard_error(hdl, errno, msg)); |
|
1544 | 1192 |
} |
1193 |
||
1194 |
/* |
|
1195 |
* Clear the errors for the pool, or the particular device if specified. |
|
1196 |
*/ |
|
1197 |
int |
|
1198 |
zpool_clear(zpool_handle_t *zhp, const char *path) |
|
1199 |
{ |
|
1200 |
zfs_cmd_t zc = { 0 }; |
|
1201 |
char msg[1024]; |
|
2082 | 1202 |
nvlist_t *tgt; |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1203 |
boolean_t avail_spare; |
2082 | 1204 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
1544 | 1205 |
|
1206 |
if (path) |
|
1207 |
(void) snprintf(msg, sizeof (msg), |
|
1208 |
dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), |
|
2676 | 1209 |
path); |
1544 | 1210 |
else |
1211 |
(void) snprintf(msg, sizeof (msg), |
|
1212 |
dgettext(TEXT_DOMAIN, "cannot clear errors for %s"), |
|
1213 |
zhp->zpool_name); |
|
1214 |
||
1215 |
(void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
|
2082 | 1216 |
if (path) { |
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1217 |
if ((tgt = zpool_find_vdev(zhp, path, &avail_spare)) == 0) |
2082 | 1218 |
return (zfs_error(hdl, EZFS_NODEVICE, msg)); |
1219 |
||
2468
b5038d012f66
6433208 should not be able to offline/online a spare
ek110237
parents:
2174
diff
changeset
|
1220 |
if (avail_spare) |
2082 | 1221 |
return (zfs_error(hdl, EZFS_ISSPARE, msg)); |
1222 |
||
1223 |
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, |
|
1224 |
&zc.zc_guid) == 0); |
|
1544 | 1225 |
} |
1226 |
||
2082 | 1227 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0) |
1544 | 1228 |
return (0); |
1229 |
||
2082 | 1230 |
return (zpool_standard_error(hdl, errno, msg)); |
789 | 1231 |
} |
1232 |
||
1233 |
static int |
|
1234 |
do_zvol(zfs_handle_t *zhp, void *data) |
|
1235 |
{ |
|
1236 |
int linktype = (int)(uintptr_t)data; |
|
1237 |
int ret; |
|
1238 |
||
1239 |
/* |
|
1240 |
* We check for volblocksize intead of ZFS_TYPE_VOLUME so that we |
|
1241 |
* correctly handle snapshots of volumes. |
|
1242 |
*/ |
|
2676 | 1243 |
if (ZFS_IS_VOLUME(zhp)) { |
789 | 1244 |
if (linktype) |
2082 | 1245 |
ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); |
789 | 1246 |
else |
2082 | 1247 |
ret = zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); |
789 | 1248 |
} |
1249 |
||
1250 |
ret = zfs_iter_children(zhp, do_zvol, data); |
|
1251 |
||
1252 |
zfs_close(zhp); |
|
1253 |
return (ret); |
|
1254 |
} |
|
1255 |
||
1256 |
/* |
|
1257 |
* Iterate over all zvols in the pool and make any necessary minor nodes. |
|
1258 |
*/ |
|
1259 |
int |
|
1260 |
zpool_create_zvol_links(zpool_handle_t *zhp) |
|
1261 |
{ |
|
1262 |
zfs_handle_t *zfp; |
|
1263 |
int ret; |
|
1264 |
||
1265 |
/* |
|
1266 |
* If the pool is unavailable, just return success. |
|
1267 |
*/ |
|
2082 | 1268 |
if ((zfp = make_dataset_handle(zhp->zpool_hdl, |
1269 |
zhp->zpool_name)) == NULL) |
|
789 | 1270 |
return (0); |
1271 |
||
2082 | 1272 |
ret = zfs_iter_children(zfp, do_zvol, (void *)B_TRUE); |
789 | 1273 |
|
1274 |
zfs_close(zfp); |
|
1275 |
return (ret); |
|
1276 |
} |
|
1277 |
||
1278 |
/* |
|
1279 |
* Iterate over all zvols in the poool and remove any minor nodes. |
|
1280 |
*/ |
|
1281 |
int |
|
1282 |
zpool_remove_zvol_links(zpool_handle_t *zhp) |
|
1283 |
{ |
|
1284 |
zfs_handle_t *zfp; |
|
1285 |
int ret; |
|
1286 |
||
1287 |
/* |
|
1288 |
* If the pool is unavailable, just return success. |
|
1289 |
*/ |
|
2082 | 1290 |
if ((zfp = make_dataset_handle(zhp->zpool_hdl, |
1291 |
zhp->zpool_name)) == NULL) |
|
789 | 1292 |
return (0); |
1293 |
||
2082 | 1294 |
ret = zfs_iter_children(zfp, do_zvol, (void *)B_FALSE); |
789 | 1295 |
|
1296 |
zfs_close(zfp); |
|
1297 |
return (ret); |
|
1298 |
} |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1299 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1300 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1301 |
* Convert from a devid string to a path. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1302 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1303 |
static char * |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1304 |
devid_to_path(char *devid_str) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1305 |
{ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1306 |
ddi_devid_t devid; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1307 |
char *minor; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1308 |
char *path; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1309 |
devid_nmlist_t *list = NULL; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1310 |
int ret; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1311 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1312 |
if (devid_str_decode(devid_str, &devid, &minor) != 0) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1313 |
return (NULL); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1314 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1315 |
ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1316 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1317 |
devid_str_free(minor); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1318 |
devid_free(devid); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1319 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1320 |
if (ret != 0) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1321 |
return (NULL); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1322 |
|
2082 | 1323 |
if ((path = strdup(list[0].devname)) == NULL) |
1324 |
return (NULL); |
|
1325 |
||
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1326 |
devid_free_nmlist(list); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1327 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1328 |
return (path); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1329 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1330 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1331 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1332 |
* Convert from a path to a devid string. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1333 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1334 |
static char * |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1335 |
path_to_devid(const char *path) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1336 |
{ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1337 |
int fd; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1338 |
ddi_devid_t devid; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1339 |
char *minor, *ret; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1340 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1341 |
if ((fd = open(path, O_RDONLY)) < 0) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1342 |
return (NULL); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1343 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1344 |
minor = NULL; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1345 |
ret = NULL; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1346 |
if (devid_get(fd, &devid) == 0) { |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1347 |
if (devid_get_minor_name(fd, &minor) == 0) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1348 |
ret = devid_str_encode(devid, minor); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1349 |
if (minor != NULL) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1350 |
devid_str_free(minor); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1351 |
devid_free(devid); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1352 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1353 |
(void) close(fd); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1354 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1355 |
return (ret); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1356 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1357 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1358 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1359 |
* Issue the necessary ioctl() to update the stored path value for the vdev. We |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1360 |
* ignore any failure here, since a common case is for an unprivileged user to |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1361 |
* type 'zpool status', and we'll display the correct information anyway. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1362 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1363 |
static void |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1364 |
set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1365 |
{ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1366 |
zfs_cmd_t zc = { 0 }; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1367 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1368 |
(void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name)); |
2676 | 1369 |
(void) strncpy(zc.zc_value, path, sizeof (zc.zc_value)); |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1370 |
verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, |
1544 | 1371 |
&zc.zc_guid) == 0); |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1372 |
|
2082 | 1373 |
(void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc); |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1374 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1375 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1376 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1377 |
* Given a vdev, return the name to display in iostat. If the vdev has a path, |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1378 |
* we use that, stripping off any leading "/dev/dsk/"; if not, we use the type. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1379 |
* We also check if this is a whole disk, in which case we strip off the |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1380 |
* trailing 's0' slice name. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1381 |
* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1382 |
* This routine is also responsible for identifying when disks have been |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1383 |
* reconfigured in a new location. The kernel will have opened the device by |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1384 |
* devid, but the path will still refer to the old location. To catch this, we |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1385 |
* first do a path -> devid translation (which is fast for the common case). If |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1386 |
* the devid matches, we're done. If not, we do a reverse devid -> path |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1387 |
* translation and issue the appropriate ioctl() to update the path of the vdev. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1388 |
* If 'zhp' is NULL, then this is an exported pool, and we don't need to do any |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1389 |
* of these checks. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1390 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1391 |
char * |
2082 | 1392 |
zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv) |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1393 |
{ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1394 |
char *path, *devid; |
1544 | 1395 |
uint64_t value; |
1396 |
char buf[64]; |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1397 |
|
1544 | 1398 |
if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, |
1399 |
&value) == 0) { |
|
1400 |
verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, |
|
1401 |
&value) == 0); |
|
2856 | 1402 |
(void) snprintf(buf, sizeof (buf), "%llu", |
1403 |
(u_longlong_t)value); |
|
1544 | 1404 |
path = buf; |
1405 |
} else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) { |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1406 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1407 |
if (zhp != NULL && |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1408 |
nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) { |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1409 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1410 |
* Determine if the current path is correct. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1411 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1412 |
char *newdevid = path_to_devid(path); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1413 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1414 |
if (newdevid == NULL || |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1415 |
strcmp(devid, newdevid) != 0) { |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1416 |
char *newpath; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1417 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1418 |
if ((newpath = devid_to_path(devid)) != NULL) { |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1419 |
/* |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1420 |
* Update the path appropriately. |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1421 |
*/ |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1422 |
set_path(zhp, nv, newpath); |
2082 | 1423 |
if (nvlist_add_string(nv, |
1424 |
ZPOOL_CONFIG_PATH, newpath) == 0) |
|
1425 |
verify(nvlist_lookup_string(nv, |
|
1426 |
ZPOOL_CONFIG_PATH, |
|
1427 |
&path) == 0); |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1428 |
free(newpath); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1429 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1430 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1431 |
|
2082 | 1432 |
if (newdevid) |
1433 |
devid_str_free(newdevid); |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1434 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1435 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1436 |
if (strncmp(path, "/dev/dsk/", 9) == 0) |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1437 |
path += 9; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1438 |
|
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1439 |
if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, |
1544 | 1440 |
&value) == 0 && value) { |
2082 | 1441 |
char *tmp = zfs_strdup(hdl, path); |
1442 |
if (tmp == NULL) |
|
1443 |
return (NULL); |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1444 |
tmp[strlen(path) - 2] = '\0'; |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1445 |
return (tmp); |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1446 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1447 |
} else { |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1448 |
verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0); |
2082 | 1449 |
|
1450 |
/* |
|
1451 |
* If it's a raidz device, we need to stick in the parity level. |
|
1452 |
*/ |
|
1453 |
if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) { |
|
1454 |
verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY, |
|
1455 |
&value) == 0); |
|
1456 |
(void) snprintf(buf, sizeof (buf), "%s%llu", path, |
|
2856 | 1457 |
(u_longlong_t)value); |
2082 | 1458 |
path = buf; |
1459 |
} |
|
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1460 |
} |
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1461 |
|
2082 | 1462 |
return (zfs_strdup(hdl, path)); |
1354
81359ee1ee63
6362672 import gets confused about overlapping slices
eschrock
parents:
1003
diff
changeset
|
1463 |
} |
1544 | 1464 |
|
1465 |
static int |
|
1466 |
zbookmark_compare(const void *a, const void *b) |
|
1467 |
{ |
|
1468 |
return (memcmp(a, b, sizeof (zbookmark_t))); |
|
1469 |
} |
|
1470 |
||
1471 |
/* |
|
1472 |
* Retrieve the persistent error log, uniquify the members, and return to the |
|
1473 |
* caller. |
|
1474 |
*/ |
|
1475 |
int |
|
1476 |
zpool_get_errlog(zpool_handle_t *zhp, nvlist_t ***list, size_t *nelem) |
|
1477 |
{ |
|
1478 |
zfs_cmd_t zc = { 0 }; |
|
1479 |
uint64_t count; |
|
2676 | 1480 |
zbookmark_t *zb = NULL; |
1481 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
|
1544 | 1482 |
int i, j; |
1483 |
||
1484 |
if (zhp->zpool_error_log != NULL) { |
|
1485 |
*list = zhp->zpool_error_log; |
|
1486 |
*nelem = zhp->zpool_error_count; |
|
1487 |
return (0); |
|
1488 |
} |
|
1489 |
||
1490 |
/* |
|
1491 |
* Retrieve the raw error list from the kernel. If the number of errors |
|
1492 |
* has increased, allocate more space and continue until we get the |
|
1493 |
* entire list. |
|
1494 |
*/ |
|
1495 |
verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT, |
|
1496 |
&count) == 0); |
|
2676 | 1497 |
if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl, |
2856 | 1498 |
count * sizeof (zbookmark_t))) == (uintptr_t)NULL) |
2082 | 1499 |
return (-1); |
2676 | 1500 |
zc.zc_nvlist_dst_size = count; |
1544 | 1501 |
(void) strcpy(zc.zc_name, zhp->zpool_name); |
1502 |
for (;;) { |
|
2082 | 1503 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG, |
1504 |
&zc) != 0) { |
|
2676 | 1505 |
free((void *)(uintptr_t)zc.zc_nvlist_dst); |
1544 | 1506 |
if (errno == ENOMEM) { |
2676 | 1507 |
if ((zc.zc_nvlist_dst = (uintptr_t) |
2082 | 1508 |
zfs_alloc(zhp->zpool_hdl, |
2856 | 1509 |
zc.zc_nvlist_dst_size)) == (uintptr_t)NULL) |
2082 | 1510 |
return (-1); |
1544 | 1511 |
} else { |
1512 |
return (-1); |
|
1513 |
} |
|
1514 |
} else { |
|
1515 |
break; |
|
1516 |
} |
|
1517 |
} |
|
1518 |
||
1519 |
/* |
|
1520 |
* Sort the resulting bookmarks. This is a little confusing due to the |
|
1521 |
* implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last |
|
2676 | 1522 |
* to first, and 'zc_nvlist_dst_size' indicates the number of boomarks |
1544 | 1523 |
* _not_ copied as part of the process. So we point the start of our |
1524 |
* array appropriate and decrement the total number of elements. |
|
1525 |
*/ |
|
2676 | 1526 |
zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) + |
1527 |
zc.zc_nvlist_dst_size; |
|
1528 |
count -= zc.zc_nvlist_dst_size; |
|
1529 |
zc.zc_nvlist_dst = 0ULL; |
|
1544 | 1530 |
|
1531 |
qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare); |
|
1532 |
||
1533 |
/* |
|
1534 |
* Count the number of unique elements |
|
1535 |
*/ |
|
1536 |
j = 0; |
|
1537 |
for (i = 0; i < count; i++) { |
|
1538 |
if (i > 0 && memcmp(&zb[i - 1], &zb[i], |
|
1539 |
sizeof (zbookmark_t)) == 0) |
|
1540 |
continue; |
|
1541 |
j++; |
|
1542 |
} |
|
1543 |
||
1544 |
/* |
|
1545 |
* If the user has only requested the number of items, return it now |
|
1546 |
* without bothering with the extra work. |
|
1547 |
*/ |
|
1548 |
if (list == NULL) { |
|
1549 |
*nelem = j; |
|
2676 | 1550 |
free((void *)(uintptr_t)zc.zc_nvlist_dst); |
1544 | 1551 |
return (0); |
1552 |
} |
|
1553 |
||
1554 |
zhp->zpool_error_count = j; |
|
1555 |
||
1556 |
/* |
|
1557 |
* Allocate an array of nvlists to hold the results |
|
1558 |
*/ |
|
2082 | 1559 |
if ((zhp->zpool_error_log = zfs_alloc(zhp->zpool_hdl, |
1560 |
j * sizeof (nvlist_t *))) == NULL) { |
|
2676 | 1561 |
free((void *)(uintptr_t)zc.zc_nvlist_dst); |
2082 | 1562 |
return (-1); |
1563 |
} |
|
1544 | 1564 |
|
1565 |
/* |
|
1566 |
* Fill in the results with names from the kernel. |
|
1567 |
*/ |
|
1568 |
j = 0; |
|
1569 |
for (i = 0; i < count; i++) { |
|
1570 |
char buf[64]; |
|
1571 |
nvlist_t *nv; |
|
1572 |
||
1573 |
if (i > 0 && memcmp(&zb[i - 1], &zb[i], |
|
1574 |
sizeof (zbookmark_t)) == 0) |
|
1575 |
continue; |
|
1576 |
||
2676 | 1577 |
if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) |
2082 | 1578 |
goto nomem; |
1544 | 1579 |
|
1580 |
zc.zc_bookmark = zb[i]; |
|
2676 | 1581 |
for (;;) { |
1582 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, |
|
1583 |
ZFS_IOC_BOOKMARK_NAME, &zc) != 0) { |
|
1584 |
if (errno == ENOMEM) { |
|
1585 |
if (zcmd_expand_dst_nvlist(hdl, &zc) |
|
1586 |
!= 0) { |
|
1587 |
zcmd_free_nvlists(&zc); |
|
1588 |
goto nomem; |
|
1589 |
} |
|
1590 |
||
1591 |
continue; |
|
1592 |
} else { |
|
1593 |
if (nvlist_alloc(&nv, NV_UNIQUE_NAME, |
|
1594 |
0) != 0) |
|
1595 |
goto nomem; |
|
1596 |
||
1597 |
zhp->zpool_error_log[j] = nv; |
|
1598 |
(void) snprintf(buf, sizeof (buf), |
|
2856 | 1599 |
"%llx", (longlong_t) |
1600 |
zb[i].zb_objset); |
|
2676 | 1601 |
if (nvlist_add_string(nv, |
1602 |
ZPOOL_ERR_DATASET, buf) != 0) |
|
1603 |
goto nomem; |
|
1604 |
(void) snprintf(buf, sizeof (buf), |
|
2856 | 1605 |
"%llx", (longlong_t) |
1606 |
zb[i].zb_object); |
|
2676 | 1607 |
if (nvlist_add_string(nv, |
1608 |
ZPOOL_ERR_OBJECT, buf) != 0) |
|
1609 |
goto nomem; |
|
1610 |
(void) snprintf(buf, sizeof (buf), |
|
1611 |
"lvl=%u blkid=%llu", |
|
1612 |
(int)zb[i].zb_level, |
|
1613 |
(long long)zb[i].zb_blkid); |
|
1614 |
if (nvlist_add_string(nv, |
|
1615 |
ZPOOL_ERR_RANGE, buf) != 0) |
|
1616 |
goto nomem; |
|
1617 |
} |
|
1618 |
} else { |
|
1619 |
if (zcmd_read_dst_nvlist(hdl, &zc, |
|
1620 |
&zhp->zpool_error_log[j]) != 0) { |
|
1621 |
zcmd_free_nvlists(&zc); |
|
1622 |
goto nomem; |
|
1623 |
} |
|
1624 |
} |
|
1625 |
||
1626 |
break; |
|
1544 | 1627 |
} |
1628 |
||
2676 | 1629 |
zcmd_free_nvlists(&zc); |
1630 |
||
1544 | 1631 |
j++; |
1632 |
} |
|
1633 |
||
1634 |
*list = zhp->zpool_error_log; |
|
1635 |
*nelem = zhp->zpool_error_count; |
|
2676 | 1636 |
free(zb); |
1544 | 1637 |
|
1638 |
return (0); |
|
2082 | 1639 |
|
1640 |
nomem: |
|
2676 | 1641 |
free(zb); |
1642 |
free((void *)(uintptr_t)zc.zc_nvlist_dst); |
|
1643 |
for (i = 0; i < zhp->zpool_error_count; i++) |
|
1644 |
nvlist_free(zhp->zpool_error_log[i]); |
|
2082 | 1645 |
free(zhp->zpool_error_log); |
1646 |
zhp->zpool_error_log = NULL; |
|
1647 |
return (no_memory(zhp->zpool_hdl)); |
|
1544 | 1648 |
} |
1760 | 1649 |
|
1650 |
/* |
|
1651 |
* Upgrade a ZFS pool to the latest on-disk version. |
|
1652 |
*/ |
|
1653 |
int |
|
1654 |
zpool_upgrade(zpool_handle_t *zhp) |
|
1655 |
{ |
|
1656 |
zfs_cmd_t zc = { 0 }; |
|
2082 | 1657 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
1760 | 1658 |
|
1659 |
(void) strcpy(zc.zc_name, zhp->zpool_name); |
|
2082 | 1660 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_UPGRADE, &zc) != 0) |
1661 |
return (zpool_standard_error(hdl, errno, |
|
1662 |
dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"), |
|
1663 |
zhp->zpool_name)); |
|
1760 | 1664 |
|
1665 |
return (0); |
|
1666 |
} |