author | mmusante |
Thu, 01 Mar 2007 06:09:49 -0800 | |
changeset 3741 | 806e933bf124 |
parent 2676 | 5cee47eddab6 |
child 4074 | 58874e1ba2ef |
permissions | -rw-r--r-- |
789 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
1544 | 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 |
*/ |
|
21 |
/* |
|
3741
806e933bf124
6441384 zpool import action message is not correctly localized
mmusante
parents:
2676
diff
changeset
|
22 |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
789 | 23 |
* Use is subject to license terms. |
24 |
*/ |
|
25 |
||
26 |
#pragma ident "%Z%%M% %I% %E% SMI" |
|
27 |
||
28 |
/* |
|
29 |
* The pool configuration repository is stored in /etc/zfs/zpool.cache as a |
|
30 |
* single packed nvlist. While it would be nice to just read in this |
|
31 |
* file from userland, this wouldn't work from a local zone. So we have to have |
|
32 |
* a zpool ioctl to return the complete configuration for all pools. In the |
|
33 |
* global zone, this will be identical to reading the file and unpacking it in |
|
34 |
* userland. |
|
35 |
*/ |
|
36 |
||
37 |
#include <errno.h> |
|
38 |
#include <sys/stat.h> |
|
39 |
#include <fcntl.h> |
|
40 |
#include <stddef.h> |
|
41 |
#include <string.h> |
|
42 |
#include <unistd.h> |
|
43 |
#include <libintl.h> |
|
44 |
#include <libuutil.h> |
|
45 |
||
46 |
#include "libzfs_impl.h" |
|
47 |
||
48 |
typedef struct config_node { |
|
49 |
char *cn_name; |
|
50 |
nvlist_t *cn_config; |
|
51 |
uu_avl_node_t cn_avl; |
|
52 |
} config_node_t; |
|
53 |
||
54 |
/* ARGSUSED */ |
|
55 |
static int |
|
56 |
config_node_compare(const void *a, const void *b, void *unused) |
|
57 |
{ |
|
58 |
int ret; |
|
59 |
||
60 |
const config_node_t *ca = (config_node_t *)a; |
|
61 |
const config_node_t *cb = (config_node_t *)b; |
|
62 |
||
63 |
ret = strcmp(ca->cn_name, cb->cn_name); |
|
64 |
||
65 |
if (ret < 0) |
|
66 |
return (-1); |
|
67 |
else if (ret > 0) |
|
68 |
return (1); |
|
69 |
else |
|
70 |
return (0); |
|
71 |
} |
|
72 |
||
2082 | 73 |
void |
74 |
namespace_clear(libzfs_handle_t *hdl) |
|
75 |
{ |
|
76 |
if (hdl->libzfs_ns_avl) { |
|
77 |
uu_avl_walk_t *walk; |
|
78 |
config_node_t *cn; |
|
79 |
||
80 |
if ((walk = uu_avl_walk_start(hdl->libzfs_ns_avl, |
|
81 |
UU_WALK_ROBUST)) == NULL) |
|
82 |
return; |
|
83 |
||
84 |
while ((cn = uu_avl_walk_next(walk)) != NULL) { |
|
85 |
uu_avl_remove(hdl->libzfs_ns_avl, cn); |
|
86 |
nvlist_free(cn->cn_config); |
|
87 |
free(cn->cn_name); |
|
88 |
free(cn); |
|
89 |
} |
|
90 |
||
91 |
uu_avl_walk_end(walk); |
|
92 |
||
93 |
uu_avl_destroy(hdl->libzfs_ns_avl); |
|
94 |
hdl->libzfs_ns_avl = NULL; |
|
95 |
} |
|
96 |
||
97 |
if (hdl->libzfs_ns_avlpool) { |
|
98 |
uu_avl_pool_destroy(hdl->libzfs_ns_avlpool); |
|
99 |
hdl->libzfs_ns_avlpool = NULL; |
|
100 |
} |
|
101 |
} |
|
102 |
||
789 | 103 |
/* |
104 |
* Loads the pool namespace, or re-loads it if the cache has changed. |
|
105 |
*/ |
|
2082 | 106 |
static int |
107 |
namespace_reload(libzfs_handle_t *hdl) |
|
789 | 108 |
{ |
109 |
nvlist_t *config; |
|
110 |
config_node_t *cn; |
|
111 |
nvpair_t *elem; |
|
112 |
zfs_cmd_t zc = { 0 }; |
|
113 |
uu_avl_walk_t *walk; |
|
114 |
||
2082 | 115 |
if (hdl->libzfs_ns_gen == 0) { |
789 | 116 |
/* |
117 |
* This is the first time we've accessed the configuration |
|
118 |
* cache. Initialize the AVL tree and then fall through to the |
|
119 |
* common code. |
|
120 |
*/ |
|
2082 | 121 |
if ((hdl->libzfs_ns_avlpool = uu_avl_pool_create("config_pool", |
789 | 122 |
sizeof (config_node_t), |
123 |
offsetof(config_node_t, cn_avl), |
|
124 |
config_node_compare, UU_DEFAULT)) == NULL) |
|
2082 | 125 |
return (no_memory(hdl)); |
789 | 126 |
|
2082 | 127 |
if ((hdl->libzfs_ns_avl = uu_avl_create(hdl->libzfs_ns_avlpool, |
128 |
NULL, UU_DEFAULT)) == NULL) |
|
129 |
return (no_memory(hdl)); |
|
789 | 130 |
} |
131 |
||
2676 | 132 |
if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) |
2082 | 133 |
return (-1); |
2676 | 134 |
|
789 | 135 |
for (;;) { |
2082 | 136 |
zc.zc_cookie = hdl->libzfs_ns_gen; |
137 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_CONFIGS, &zc) != 0) { |
|
789 | 138 |
switch (errno) { |
139 |
case EEXIST: |
|
140 |
/* |
|
141 |
* The namespace hasn't changed. |
|
142 |
*/ |
|
2676 | 143 |
zcmd_free_nvlists(&zc); |
2082 | 144 |
return (0); |
789 | 145 |
|
146 |
case ENOMEM: |
|
2676 | 147 |
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { |
148 |
zcmd_free_nvlists(&zc); |
|
2082 | 149 |
return (-1); |
2676 | 150 |
} |
789 | 151 |
break; |
152 |
||
153 |
default: |
|
2676 | 154 |
zcmd_free_nvlists(&zc); |
2082 | 155 |
return (zfs_standard_error(hdl, errno, |
156 |
dgettext(TEXT_DOMAIN, "failed to read " |
|
157 |
"pool configuration"))); |
|
789 | 158 |
} |
159 |
} else { |
|
2082 | 160 |
hdl->libzfs_ns_gen = zc.zc_cookie; |
789 | 161 |
break; |
162 |
} |
|
163 |
} |
|
164 |
||
2676 | 165 |
if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) { |
166 |
zcmd_free_nvlists(&zc); |
|
167 |
return (-1); |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
168 |
} |
789 | 169 |
|
2676 | 170 |
zcmd_free_nvlists(&zc); |
789 | 171 |
|
172 |
/* |
|
173 |
* Clear out any existing configuration information. |
|
174 |
*/ |
|
2082 | 175 |
if ((walk = uu_avl_walk_start(hdl->libzfs_ns_avl, |
176 |
UU_WALK_ROBUST)) == NULL) { |
|
177 |
nvlist_free(config); |
|
178 |
return (no_memory(hdl)); |
|
179 |
} |
|
789 | 180 |
|
181 |
while ((cn = uu_avl_walk_next(walk)) != NULL) { |
|
2082 | 182 |
uu_avl_remove(hdl->libzfs_ns_avl, cn); |
789 | 183 |
nvlist_free(cn->cn_config); |
184 |
free(cn->cn_name); |
|
185 |
free(cn); |
|
186 |
} |
|
187 |
||
2082 | 188 |
uu_avl_walk_end(walk); |
189 |
||
789 | 190 |
elem = NULL; |
191 |
while ((elem = nvlist_next_nvpair(config, elem)) != NULL) { |
|
192 |
nvlist_t *child; |
|
193 |
uu_avl_index_t where; |
|
194 |
||
2082 | 195 |
if ((cn = zfs_alloc(hdl, sizeof (config_node_t))) == NULL) { |
196 |
nvlist_free(config); |
|
197 |
return (-1); |
|
198 |
} |
|
199 |
||
200 |
if ((cn->cn_name = zfs_strdup(hdl, |
|
201 |
nvpair_name(elem))) == NULL) { |
|
202 |
free(cn); |
|
2474
c001ad7e0c25
6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents:
2142
diff
changeset
|
203 |
nvlist_free(config); |
2082 | 204 |
return (-1); |
205 |
} |
|
789 | 206 |
|
207 |
verify(nvpair_value_nvlist(elem, &child) == 0); |
|
2082 | 208 |
if (nvlist_dup(child, &cn->cn_config, 0) != 0) { |
2474
c001ad7e0c25
6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents:
2142
diff
changeset
|
209 |
free(cn->cn_name); |
c001ad7e0c25
6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents:
2142
diff
changeset
|
210 |
free(cn); |
2082 | 211 |
nvlist_free(config); |
212 |
return (no_memory(hdl)); |
|
213 |
} |
|
214 |
verify(uu_avl_find(hdl->libzfs_ns_avl, cn, NULL, &where) |
|
215 |
== NULL); |
|
789 | 216 |
|
2082 | 217 |
uu_avl_insert(hdl->libzfs_ns_avl, cn, where); |
789 | 218 |
} |
219 |
||
220 |
nvlist_free(config); |
|
2082 | 221 |
return (0); |
789 | 222 |
} |
223 |
||
224 |
/* |
|
225 |
* Retrive the configuration for the given pool. The configuration is a nvlist |
|
226 |
* describing the vdevs, as well as the statistics associated with each one. |
|
227 |
*/ |
|
228 |
nvlist_t * |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
229 |
zpool_get_config(zpool_handle_t *zhp, nvlist_t **oldconfig) |
789 | 230 |
{ |
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
231 |
if (oldconfig) |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
232 |
*oldconfig = zhp->zpool_old_config; |
789 | 233 |
return (zhp->zpool_config); |
234 |
} |
|
235 |
||
236 |
/* |
|
237 |
* Refresh the vdev statistics associated with the given pool. This is used in |
|
238 |
* iostat to show configuration changes and determine the delta from the last |
|
239 |
* time the function was called. This function can fail, in case the pool has |
|
240 |
* been destroyed. |
|
241 |
*/ |
|
242 |
int |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
243 |
zpool_refresh_stats(zpool_handle_t *zhp, boolean_t *missing) |
789 | 244 |
{ |
245 |
zfs_cmd_t zc = { 0 }; |
|
246 |
int error; |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
247 |
nvlist_t *config; |
2676 | 248 |
libzfs_handle_t *hdl = zhp->zpool_hdl; |
789 | 249 |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
250 |
*missing = B_FALSE; |
789 | 251 |
(void) strcpy(zc.zc_name, zhp->zpool_name); |
252 |
||
253 |
if (zhp->zpool_config_size == 0) |
|
254 |
zhp->zpool_config_size = 1 << 16; |
|
255 |
||
2676 | 256 |
if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size) != 0) |
2082 | 257 |
return (-1); |
789 | 258 |
|
1544 | 259 |
for (;;) { |
2082 | 260 |
if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_POOL_STATS, |
261 |
&zc) == 0) { |
|
789 | 262 |
/* |
1544 | 263 |
* The real error is returned in the zc_cookie field. |
789 | 264 |
*/ |
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
265 |
error = zc.zc_cookie; |
789 | 266 |
break; |
267 |
} |
|
268 |
||
1544 | 269 |
if (errno == ENOMEM) { |
2676 | 270 |
if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { |
271 |
zcmd_free_nvlists(&zc); |
|
2082 | 272 |
return (-1); |
2676 | 273 |
} |
1544 | 274 |
} else { |
2676 | 275 |
zcmd_free_nvlists(&zc); |
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
276 |
if (errno == ENOENT || errno == EINVAL) |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
277 |
*missing = B_TRUE; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
278 |
zhp->zpool_state = POOL_STATE_UNAVAIL; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
279 |
return (0); |
789 | 280 |
} |
281 |
} |
|
282 |
||
2676 | 283 |
if (zcmd_read_dst_nvlist(hdl, &zc, &config) != 0) { |
284 |
zcmd_free_nvlists(&zc); |
|
285 |
return (-1); |
|
2082 | 286 |
} |
789 | 287 |
|
2676 | 288 |
zcmd_free_nvlists(&zc); |
289 |
||
290 |
zhp->zpool_config_size = zc.zc_nvlist_dst_size; |
|
789 | 291 |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
292 |
if (zhp->zpool_config != NULL) { |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
293 |
uint64_t oldtxg, newtxg; |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
294 |
|
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
295 |
verify(nvlist_lookup_uint64(zhp->zpool_config, |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
296 |
ZPOOL_CONFIG_POOL_TXG, &oldtxg) == 0); |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
297 |
verify(nvlist_lookup_uint64(config, |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
298 |
ZPOOL_CONFIG_POOL_TXG, &newtxg) == 0); |
789 | 299 |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
300 |
if (zhp->zpool_old_config != NULL) |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
301 |
nvlist_free(zhp->zpool_old_config); |
789 | 302 |
|
952
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
303 |
if (oldtxg != newtxg) { |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
304 |
nvlist_free(zhp->zpool_config); |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
305 |
zhp->zpool_old_config = NULL; |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
306 |
} else { |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
307 |
zhp->zpool_old_config = zhp->zpool_config; |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
308 |
} |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
309 |
} |
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
310 |
|
12ec54aa046e
6344502 placeholder bug for the remaining Makefile cleanup work for ZFS
eschrock
parents:
789
diff
changeset
|
311 |
zhp->zpool_config = config; |
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
312 |
if (error) |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
313 |
zhp->zpool_state = POOL_STATE_UNAVAIL; |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
314 |
else |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
315 |
zhp->zpool_state = POOL_STATE_ACTIVE; |
789 | 316 |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
317 |
return (0); |
789 | 318 |
} |
319 |
||
320 |
/* |
|
321 |
* Iterate over all pools in the system. |
|
322 |
*/ |
|
323 |
int |
|
2082 | 324 |
zpool_iter(libzfs_handle_t *hdl, zpool_iter_f func, void *data) |
789 | 325 |
{ |
326 |
config_node_t *cn; |
|
327 |
zpool_handle_t *zhp; |
|
328 |
int ret; |
|
329 |
||
2082 | 330 |
if (namespace_reload(hdl) != 0) |
331 |
return (-1); |
|
789 | 332 |
|
2082 | 333 |
for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL; |
334 |
cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) { |
|
789 | 335 |
|
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
336 |
if (zpool_open_silent(hdl, cn->cn_name, &zhp) != 0) |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
337 |
return (-1); |
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
338 |
|
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
339 |
if (zhp == NULL) |
789 | 340 |
continue; |
341 |
||
342 |
if ((ret = func(zhp, data)) != 0) |
|
343 |
return (ret); |
|
344 |
} |
|
345 |
||
346 |
return (0); |
|
347 |
} |
|
348 |
||
349 |
/* |
|
350 |
* Iterate over root datasets, calling the given function for each. The zfs |
|
351 |
* handle passed each time must be explicitly closed by the callback. |
|
352 |
*/ |
|
353 |
int |
|
2082 | 354 |
zfs_iter_root(libzfs_handle_t *hdl, zfs_iter_f func, void *data) |
789 | 355 |
{ |
356 |
config_node_t *cn; |
|
357 |
zfs_handle_t *zhp; |
|
358 |
int ret; |
|
359 |
||
2082 | 360 |
if (namespace_reload(hdl) != 0) |
361 |
return (-1); |
|
789 | 362 |
|
2082 | 363 |
for (cn = uu_avl_first(hdl->libzfs_ns_avl); cn != NULL; |
364 |
cn = uu_avl_next(hdl->libzfs_ns_avl, cn)) { |
|
789 | 365 |
|
2082 | 366 |
if ((zhp = make_dataset_handle(hdl, cn->cn_name)) == NULL) |
789 | 367 |
continue; |
368 |
||
369 |
if ((ret = func(zhp, data)) != 0) |
|
370 |
return (ret); |
|
371 |
} |
|
372 |
||
373 |
return (0); |
|
374 |
} |