]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzfs/libzfs_import.c
ZVOLs should not be allowed to have children
[mirror_zfs.git] / lib / libzfs / libzfs_import.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
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 /*
22 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright 2015 RackTop Systems.
26 * Copyright (c) 2016, Intel Corporation.
27 */
28
29 #include <devid.h>
30 #include <errno.h>
31 #include <libintl.h>
32 #include <libgen.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <sys/vdev_impl.h>
39 #include <libzfs.h>
40 #include <libzfs_impl.h>
41 #include <libzutil.h>
42
43 /*
44 * Returns true if the named pool matches the given GUID.
45 */
46 static int
47 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
48 boolean_t *isactive)
49 {
50 zpool_handle_t *zhp;
51 uint64_t theguid;
52
53 if (zpool_open_silent(hdl, name, &zhp) != 0)
54 return (-1);
55
56 if (zhp == NULL) {
57 *isactive = B_FALSE;
58 return (0);
59 }
60
61 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
62 &theguid) == 0);
63
64 zpool_close(zhp);
65
66 *isactive = (theguid == guid);
67 return (0);
68 }
69
70 static nvlist_t *
71 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
72 {
73 nvlist_t *nvl;
74 zfs_cmd_t zc = {"\0"};
75 int err, dstbuf_size;
76
77 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
78 return (NULL);
79
80 dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
81
82 if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
83 zcmd_free_nvlists(&zc);
84 return (NULL);
85 }
86
87 while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
88 &zc)) != 0 && errno == ENOMEM) {
89 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
90 zcmd_free_nvlists(&zc);
91 return (NULL);
92 }
93 }
94
95 if (err) {
96 zcmd_free_nvlists(&zc);
97 return (NULL);
98 }
99
100 if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
101 zcmd_free_nvlists(&zc);
102 return (NULL);
103 }
104
105 zcmd_free_nvlists(&zc);
106 return (nvl);
107 }
108
109 static nvlist_t *
110 refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
111 {
112 return (refresh_config((libzfs_handle_t *)handle, tryconfig));
113 }
114
115
116 static int
117 pool_active_libzfs(void *handle, const char *name, uint64_t guid,
118 boolean_t *isactive)
119 {
120 return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
121 }
122
123 const pool_config_ops_t libzfs_config_ops = {
124 .pco_refresh_config = refresh_config_libzfs,
125 .pco_pool_active = pool_active_libzfs,
126 };
127
128 /*
129 * Return the offset of the given label.
130 */
131 static uint64_t
132 label_offset(uint64_t size, int l)
133 {
134 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
135 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
136 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
137 }
138
139 /*
140 * Given a file descriptor, clear (zero) the label information. This function
141 * is used in the appliance stack as part of the ZFS sysevent module and
142 * to implement the "zpool labelclear" command.
143 */
144 int
145 zpool_clear_label(int fd)
146 {
147 struct stat64 statbuf;
148 int l;
149 vdev_label_t *label;
150 uint64_t size;
151
152 if (fstat64_blk(fd, &statbuf) == -1)
153 return (0);
154 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
155
156 if ((label = calloc(1, sizeof (vdev_label_t))) == NULL)
157 return (-1);
158
159 for (l = 0; l < VDEV_LABELS; l++) {
160 if (pwrite64(fd, label, sizeof (vdev_label_t),
161 label_offset(size, l)) != sizeof (vdev_label_t)) {
162 free(label);
163 return (-1);
164 }
165 }
166
167 free(label);
168 return (0);
169 }
170
171 static boolean_t
172 find_guid(nvlist_t *nv, uint64_t guid)
173 {
174 uint64_t tmp;
175 nvlist_t **child;
176 uint_t c, children;
177
178 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
179 if (tmp == guid)
180 return (B_TRUE);
181
182 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
183 &child, &children) == 0) {
184 for (c = 0; c < children; c++)
185 if (find_guid(child[c], guid))
186 return (B_TRUE);
187 }
188
189 return (B_FALSE);
190 }
191
192 typedef struct aux_cbdata {
193 const char *cb_type;
194 uint64_t cb_guid;
195 zpool_handle_t *cb_zhp;
196 } aux_cbdata_t;
197
198 static int
199 find_aux(zpool_handle_t *zhp, void *data)
200 {
201 aux_cbdata_t *cbp = data;
202 nvlist_t **list;
203 uint_t i, count;
204 uint64_t guid;
205 nvlist_t *nvroot;
206
207 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
208 &nvroot) == 0);
209
210 if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
211 &list, &count) == 0) {
212 for (i = 0; i < count; i++) {
213 verify(nvlist_lookup_uint64(list[i],
214 ZPOOL_CONFIG_GUID, &guid) == 0);
215 if (guid == cbp->cb_guid) {
216 cbp->cb_zhp = zhp;
217 return (1);
218 }
219 }
220 }
221
222 zpool_close(zhp);
223 return (0);
224 }
225
226 /*
227 * Determines if the pool is in use. If so, it returns true and the state of
228 * the pool as well as the name of the pool. Name string is allocated and
229 * must be freed by the caller.
230 */
231 int
232 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
233 boolean_t *inuse)
234 {
235 nvlist_t *config;
236 char *name;
237 boolean_t ret;
238 uint64_t guid, vdev_guid;
239 zpool_handle_t *zhp;
240 nvlist_t *pool_config;
241 uint64_t stateval, isspare;
242 aux_cbdata_t cb = { 0 };
243 boolean_t isactive;
244
245 *inuse = B_FALSE;
246
247 if (zpool_read_label(fd, &config, NULL) != 0) {
248 (void) no_memory(hdl);
249 return (-1);
250 }
251
252 if (config == NULL)
253 return (0);
254
255 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
256 &stateval) == 0);
257 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
258 &vdev_guid) == 0);
259
260 if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
261 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
262 &name) == 0);
263 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
264 &guid) == 0);
265 }
266
267 switch (stateval) {
268 case POOL_STATE_EXPORTED:
269 /*
270 * A pool with an exported state may in fact be imported
271 * read-only, so check the in-core state to see if it's
272 * active and imported read-only. If it is, set
273 * its state to active.
274 */
275 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
276 (zhp = zpool_open_canfail(hdl, name)) != NULL) {
277 if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
278 stateval = POOL_STATE_ACTIVE;
279
280 /*
281 * All we needed the zpool handle for is the
282 * readonly prop check.
283 */
284 zpool_close(zhp);
285 }
286
287 ret = B_TRUE;
288 break;
289
290 case POOL_STATE_ACTIVE:
291 /*
292 * For an active pool, we have to determine if it's really part
293 * of a currently active pool (in which case the pool will exist
294 * and the guid will be the same), or whether it's part of an
295 * active pool that was disconnected without being explicitly
296 * exported.
297 */
298 if (pool_active(hdl, name, guid, &isactive) != 0) {
299 nvlist_free(config);
300 return (-1);
301 }
302
303 if (isactive) {
304 /*
305 * Because the device may have been removed while
306 * offlined, we only report it as active if the vdev is
307 * still present in the config. Otherwise, pretend like
308 * it's not in use.
309 */
310 if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
311 (pool_config = zpool_get_config(zhp, NULL))
312 != NULL) {
313 nvlist_t *nvroot;
314
315 verify(nvlist_lookup_nvlist(pool_config,
316 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
317 ret = find_guid(nvroot, vdev_guid);
318 } else {
319 ret = B_FALSE;
320 }
321
322 /*
323 * If this is an active spare within another pool, we
324 * treat it like an unused hot spare. This allows the
325 * user to create a pool with a hot spare that currently
326 * in use within another pool. Since we return B_TRUE,
327 * libdiskmgt will continue to prevent generic consumers
328 * from using the device.
329 */
330 if (ret && nvlist_lookup_uint64(config,
331 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
332 stateval = POOL_STATE_SPARE;
333
334 if (zhp != NULL)
335 zpool_close(zhp);
336 } else {
337 stateval = POOL_STATE_POTENTIALLY_ACTIVE;
338 ret = B_TRUE;
339 }
340 break;
341
342 case POOL_STATE_SPARE:
343 /*
344 * For a hot spare, it can be either definitively in use, or
345 * potentially active. To determine if it's in use, we iterate
346 * over all pools in the system and search for one with a spare
347 * with a matching guid.
348 *
349 * Due to the shared nature of spares, we don't actually report
350 * the potentially active case as in use. This means the user
351 * can freely create pools on the hot spares of exported pools,
352 * but to do otherwise makes the resulting code complicated, and
353 * we end up having to deal with this case anyway.
354 */
355 cb.cb_zhp = NULL;
356 cb.cb_guid = vdev_guid;
357 cb.cb_type = ZPOOL_CONFIG_SPARES;
358 if (zpool_iter(hdl, find_aux, &cb) == 1) {
359 name = (char *)zpool_get_name(cb.cb_zhp);
360 ret = B_TRUE;
361 } else {
362 ret = B_FALSE;
363 }
364 break;
365
366 case POOL_STATE_L2CACHE:
367
368 /*
369 * Check if any pool is currently using this l2cache device.
370 */
371 cb.cb_zhp = NULL;
372 cb.cb_guid = vdev_guid;
373 cb.cb_type = ZPOOL_CONFIG_L2CACHE;
374 if (zpool_iter(hdl, find_aux, &cb) == 1) {
375 name = (char *)zpool_get_name(cb.cb_zhp);
376 ret = B_TRUE;
377 } else {
378 ret = B_FALSE;
379 }
380 break;
381
382 default:
383 ret = B_FALSE;
384 }
385
386
387 if (ret) {
388 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
389 if (cb.cb_zhp)
390 zpool_close(cb.cb_zhp);
391 nvlist_free(config);
392 return (-1);
393 }
394 *state = (pool_state_t)stateval;
395 }
396
397 if (cb.cb_zhp)
398 zpool_close(cb.cb_zhp);
399
400 nvlist_free(config);
401 *inuse = ret;
402 return (0);
403 }