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 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright 2015 RackTop Systems.
26 * Copyright 2017 Nexenta Systems, Inc.
27 */
28
29 /*
30 * Pool import support functions.
31 *
32 * To import a pool, we rely on reading the configuration information from the
33 * ZFS label of each device. If we successfully read the label, then we
34 * organize the configuration information in the following hierarchy:
35 *
36 * pool guid -> toplevel vdev guid -> label txg
37 *
38 * Duplicate entries matching this same tuple will be discarded. Once we have
39 * examined every device, we pick the best label txg config for each toplevel
40 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
41 * update any paths that have changed. Finally, we attempt to import the pool
42 * using our derived config, and record the results.
43 */
44
45 #include <ctype.h>
46 #include <devid.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <libintl.h>
50 #include <stddef.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <fcntl.h>
56 #include <sys/vtoc.h>
57 #include <sys/dktp/fdisk.h>
58 #include <sys/efi_partition.h>
59 #include <thread_pool.h>
60
61 #include <sys/vdev_impl.h>
62 #include <libzutil.h>
63 #include <sys/arc_impl.h>
64
65 #include "libzfs.h"
66 #include "libzfs_impl.h"
67
68 /*
69 * Returns true if the named pool matches the given GUID.
70 */
71 static int
pool_active(libzfs_handle_t * hdl,const char * name,uint64_t guid,boolean_t * isactive)72 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
73 boolean_t *isactive)
74 {
75 zpool_handle_t *zhp;
76 uint64_t theguid;
77
78 if (zpool_open_silent(hdl, name, &zhp) != 0)
79 return (-1);
80
81 if (zhp == NULL) {
82 *isactive = B_FALSE;
83 return (0);
84 }
85
86 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
87 &theguid) == 0);
88
89 zpool_close(zhp);
90
91 *isactive = (theguid == guid);
92 return (0);
93 }
94
95 static nvlist_t *
refresh_config(libzfs_handle_t * hdl,nvlist_t * config)96 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
97 {
98 nvlist_t *nvl;
99 zfs_cmd_t zc = {"\0"};
100 int err, dstbuf_size;
101
102 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
103 return (NULL);
104
105 dstbuf_size = MAX(CONFIG_BUF_MINSIZE, zc.zc_nvlist_conf_size * 4);
106
107 if (zcmd_alloc_dst_nvlist(hdl, &zc, dstbuf_size) != 0) {
108 zcmd_free_nvlists(&zc);
109 return (NULL);
110 }
111
112 while ((err = zfs_ioctl(hdl, ZFS_IOC_POOL_TRYIMPORT,
113 &zc)) != 0 && errno == ENOMEM) {
114 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
115 zcmd_free_nvlists(&zc);
116 return (NULL);
117 }
118 }
119
120 if (err) {
121 zcmd_free_nvlists(&zc);
122 return (NULL);
123 }
124
125 if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
126 zcmd_free_nvlists(&zc);
127 return (NULL);
128 }
129
130 zcmd_free_nvlists(&zc);
131 return (nvl);
132 }
133
134 static nvlist_t *
refresh_config_libzfs(void * handle,nvlist_t * tryconfig)135 refresh_config_libzfs(void *handle, nvlist_t *tryconfig)
136 {
137 return (refresh_config((libzfs_handle_t *)handle, tryconfig));
138 }
139
140 static int
pool_active_libzfs(void * handle,const char * name,uint64_t guid,boolean_t * isactive)141 pool_active_libzfs(void *handle, const char *name, uint64_t guid,
142 boolean_t *isactive)
143 {
144 return (pool_active((libzfs_handle_t *)handle, name, guid, isactive));
145 }
146
147 const pool_config_ops_t libzfs_config_ops = {
148 .pco_refresh_config = refresh_config_libzfs,
149 .pco_pool_active = pool_active_libzfs,
150 };
151
152 /*
153 * Return the offset of the given label.
154 */
155 static uint64_t
label_offset(uint64_t size,int l)156 label_offset(uint64_t size, int l)
157 {
158 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
159 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
160 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
161 }
162
163 /*
164 * Given a file descriptor, clear (zero) the label information.
165 */
166 int
zpool_clear_label(int fd)167 zpool_clear_label(int fd)
168 {
169 struct stat64 statbuf;
170 int l;
171 vdev_label_t *label;
172 uint64_t size;
173 boolean_t labels_cleared = B_FALSE, clear_l2arc_header = B_FALSE,
174 header_cleared = B_FALSE;
175
176 if (fstat64(fd, &statbuf) == -1)
177 return (0);
178
179 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
180
181 if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
182 return (-1);
183
184 for (l = 0; l < VDEV_LABELS; l++) {
185 uint64_t state, guid, l2cache;
186 nvlist_t *config;
187
188 if (pread64(fd, label, sizeof (vdev_label_t),
189 label_offset(size, l)) != sizeof (vdev_label_t)) {
190 continue;
191 }
192
193 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
194 sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) {
195 continue;
196 }
197
198 /* Skip labels which do not have a valid guid. */
199 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
200 &guid) != 0 || guid == 0) {
201 nvlist_free(config);
202 continue;
203 }
204
205 /* Skip labels which are not in a known valid state. */
206 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
207 &state) != 0 || state > POOL_STATE_L2CACHE) {
208 nvlist_free(config);
209 continue;
210 }
211
212 /* If the device is a cache device clear the header. */
213 if (!clear_l2arc_header) {
214 if (nvlist_lookup_uint64(config,
215 ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
216 l2cache == POOL_STATE_L2CACHE) {
217 clear_l2arc_header = B_TRUE;
218 }
219 }
220
221 nvlist_free(config);
222
223 /*
224 * A valid label was found, overwrite this label's nvlist
225 * and uberblocks with zeros on disk. This is done to prevent
226 * system utilities, like blkid, from incorrectly detecting a
227 * partial label. The leading pad space is left untouched.
228 */
229 memset(label, 0, sizeof (vdev_label_t));
230 size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE);
231
232 if (pwrite64(fd, label, label_size, label_offset(size, l) +
233 (2 * VDEV_PAD_SIZE)) == label_size)
234 labels_cleared = B_TRUE;
235 }
236
237 if (clear_l2arc_header) {
238 _Static_assert(sizeof (*label) >= sizeof (l2arc_dev_hdr_phys_t),
239 "label < l2arc_dev_hdr_phys_t");
240 memset(label, 0, sizeof (l2arc_dev_hdr_phys_t));
241 if (pwrite64(fd, label, sizeof (l2arc_dev_hdr_phys_t),
242 VDEV_LABEL_START_SIZE) == sizeof (l2arc_dev_hdr_phys_t))
243 header_cleared = B_TRUE;
244 }
245
246 free(label);
247
248 if (!labels_cleared || (clear_l2arc_header && !header_cleared))
249 return (-1);
250
251 return (0);
252 }
253
254 boolean_t
find_guid(nvlist_t * nv,uint64_t guid)255 find_guid(nvlist_t *nv, uint64_t guid)
256 {
257 uint64_t tmp;
258 nvlist_t **child;
259 uint_t c, children;
260
261 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
262 if (tmp == guid)
263 return (B_TRUE);
264
265 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
266 &child, &children) == 0) {
267 for (c = 0; c < children; c++)
268 if (find_guid(child[c], guid))
269 return (B_TRUE);
270 }
271
272 return (B_FALSE);
273 }
274
275 typedef struct aux_cbdata {
276 const char *cb_type;
277 uint64_t cb_guid;
278 zpool_handle_t *cb_zhp;
279 } aux_cbdata_t;
280
281 static int
find_aux(zpool_handle_t * zhp,void * data)282 find_aux(zpool_handle_t *zhp, void *data)
283 {
284 aux_cbdata_t *cbp = data;
285 nvlist_t **list;
286 uint_t i, count;
287 uint64_t guid;
288 nvlist_t *nvroot;
289
290 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
291 &nvroot) == 0);
292
293 if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
294 &list, &count) == 0) {
295 for (i = 0; i < count; i++) {
296 verify(nvlist_lookup_uint64(list[i],
297 ZPOOL_CONFIG_GUID, &guid) == 0);
298 if (guid == cbp->cb_guid) {
299 cbp->cb_zhp = zhp;
300 return (1);
301 }
302 }
303 }
304
305 zpool_close(zhp);
306 return (0);
307 }
308
309 /*
310 * Determines if the pool is in use. If so, it returns true and the state of
311 * the pool as well as the name of the pool. Both strings are allocated and
312 * must be freed by the caller.
313 */
314 int
zpool_in_use(libzfs_handle_t * hdl,int fd,pool_state_t * state,char ** namestr,boolean_t * inuse)315 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
316 boolean_t *inuse)
317 {
318 nvlist_t *config;
319 char *name;
320 boolean_t ret;
321 uint64_t guid, vdev_guid;
322 zpool_handle_t *zhp;
323 nvlist_t *pool_config;
324 uint64_t stateval, isspare;
325 aux_cbdata_t cb = { 0 };
326 boolean_t isactive;
327
328 *inuse = B_FALSE;
329
330 if (zpool_read_label(fd, &config, NULL) != 0 && errno == ENOMEM) {
331 (void) no_memory(hdl);
332 return (-1);
333 }
334
335 if (config == NULL)
336 return (0);
337
338 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
339 &stateval) == 0);
340 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
341 &vdev_guid) == 0);
342
343 if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
344 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
345 &name) == 0);
346 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
347 &guid) == 0);
348 }
349
350 switch (stateval) {
351 case POOL_STATE_EXPORTED:
352 /*
353 * A pool with an exported state may in fact be imported
354 * read-only, so check the in-core state to see if it's
355 * active and imported read-only. If it is, set
356 * its state to active.
357 */
358 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
359 (zhp = zpool_open_canfail(hdl, name)) != NULL) {
360 if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
361 stateval = POOL_STATE_ACTIVE;
362
363 /*
364 * All we needed the zpool handle for is the
365 * readonly prop check.
366 */
367 zpool_close(zhp);
368 }
369
370 ret = B_TRUE;
371 break;
372
373 case POOL_STATE_ACTIVE:
374 /*
375 * For an active pool, we have to determine if it's really part
376 * of a currently active pool (in which case the pool will exist
377 * and the guid will be the same), or whether it's part of an
378 * active pool that was disconnected without being explicitly
379 * exported.
380 */
381 if (pool_active(hdl, name, guid, &isactive) != 0) {
382 nvlist_free(config);
383 return (-1);
384 }
385
386 if (isactive) {
387 /*
388 * Because the device may have been removed while
389 * offlined, we only report it as active if the vdev is
390 * still present in the config. Otherwise, pretend like
391 * it's not in use.
392 */
393 if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
394 (pool_config = zpool_get_config(zhp, NULL))
395 != NULL) {
396 nvlist_t *nvroot;
397
398 verify(nvlist_lookup_nvlist(pool_config,
399 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
400 ret = find_guid(nvroot, vdev_guid);
401 } else {
402 ret = B_FALSE;
403 }
404
405 /*
406 * If this is an active spare within another pool, we
407 * treat it like an unused hot spare. This allows the
408 * user to create a pool with a hot spare that currently
409 * in use within another pool. Since we return B_TRUE,
410 * libdiskmgt will continue to prevent generic consumers
411 * from using the device.
412 */
413 if (ret && nvlist_lookup_uint64(config,
414 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
415 stateval = POOL_STATE_SPARE;
416
417 if (zhp != NULL)
418 zpool_close(zhp);
419 } else {
420 stateval = POOL_STATE_POTENTIALLY_ACTIVE;
421 ret = B_TRUE;
422 }
423 break;
424
425 case POOL_STATE_SPARE:
426 /*
427 * For a hot spare, it can be either definitively in use, or
428 * potentially active. To determine if it's in use, we iterate
429 * over all pools in the system and search for one with a spare
430 * with a matching guid.
431 *
432 * Due to the shared nature of spares, we don't actually report
433 * the potentially active case as in use. This means the user
434 * can freely create pools on the hot spares of exported pools,
435 * but to do otherwise makes the resulting code complicated, and
436 * we end up having to deal with this case anyway.
437 */
438 cb.cb_zhp = NULL;
439 cb.cb_guid = vdev_guid;
440 cb.cb_type = ZPOOL_CONFIG_SPARES;
441 if (zpool_iter(hdl, find_aux, &cb) == 1) {
442 name = (char *)zpool_get_name(cb.cb_zhp);
443 ret = B_TRUE;
444 } else {
445 ret = B_FALSE;
446 }
447 break;
448
449 case POOL_STATE_L2CACHE:
450
451 /*
452 * Check if any pool is currently using this l2cache device.
453 */
454 cb.cb_zhp = NULL;
455 cb.cb_guid = vdev_guid;
456 cb.cb_type = ZPOOL_CONFIG_L2CACHE;
457 if (zpool_iter(hdl, find_aux, &cb) == 1) {
458 name = (char *)zpool_get_name(cb.cb_zhp);
459 ret = B_TRUE;
460 } else {
461 ret = B_FALSE;
462 }
463 break;
464
465 default:
466 ret = B_FALSE;
467 }
468
469
470 if (ret) {
471 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
472 if (cb.cb_zhp)
473 zpool_close(cb.cb_zhp);
474 nvlist_free(config);
475 return (-1);
476 }
477 *state = (pool_state_t)stateval;
478 }
479
480 if (cb.cb_zhp)
481 zpool_close(cb.cb_zhp);
482
483 nvlist_free(config);
484 *inuse = ret;
485 return (0);
486 }
487