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 <errno.h> 30 #include <libintl.h> 31 #include <libgen.h> 32 #include <stddef.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <sys/stat.h> 36 #include <unistd.h> 37 #include <sys/vdev_impl.h> 38 #include <libzfs.h> 39 #include <libzfs_impl.h> 40 #include <libzutil.h> 41 #include <sys/arc_impl.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 = zfs_ioctl(hdl, 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 l2arc_dev_hdr_phys_t *l2dhdr; 151 uint64_t size; 152 int labels_cleared = 0, header_cleared = 0; 153 boolean_t clear_l2arc_header = B_FALSE; 154 155 if (fstat64_blk(fd, &statbuf) == -1) 156 return (0); 157 158 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); 159 160 if ((label = calloc(1, sizeof (vdev_label_t))) == NULL) 161 return (-1); 162 163 if ((l2dhdr = calloc(1, sizeof (l2arc_dev_hdr_phys_t))) == NULL) { 164 free(label); 165 return (-1); 166 } 167 168 for (l = 0; l < VDEV_LABELS; l++) { 169 uint64_t state, guid, l2cache; 170 nvlist_t *config; 171 172 if (pread64(fd, label, sizeof (vdev_label_t), 173 label_offset(size, l)) != sizeof (vdev_label_t)) { 174 continue; 175 } 176 177 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 178 sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) { 179 continue; 180 } 181 182 /* Skip labels which do not have a valid guid. */ 183 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 184 &guid) != 0 || guid == 0) { 185 nvlist_free(config); 186 continue; 187 } 188 189 /* Skip labels which are not in a known valid state. */ 190 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 191 &state) != 0 || state > POOL_STATE_L2CACHE) { 192 nvlist_free(config); 193 continue; 194 } 195 196 /* If the device is a cache device clear the header. */ 197 if (!clear_l2arc_header) { 198 if (nvlist_lookup_uint64(config, 199 ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 && 200 l2cache == POOL_STATE_L2CACHE) { 201 clear_l2arc_header = B_TRUE; 202 } 203 } 204 205 nvlist_free(config); 206 207 /* 208 * A valid label was found, overwrite this label's nvlist 209 * and uberblocks with zeros on disk. This is done to prevent 210 * system utilities, like blkid, from incorrectly detecting a 211 * partial label. The leading pad space is left untouched. 212 */ 213 memset(label, 0, sizeof (vdev_label_t)); 214 size_t label_size = sizeof (vdev_label_t) - (2 * VDEV_PAD_SIZE); 215 216 if (pwrite64(fd, label, label_size, label_offset(size, l) + 217 (2 * VDEV_PAD_SIZE)) == label_size) { 218 labels_cleared++; 219 } 220 } 221 222 /* Clear the L2ARC header. */ 223 if (clear_l2arc_header) { 224 memset(l2dhdr, 0, sizeof (l2arc_dev_hdr_phys_t)); 225 if (pwrite64(fd, l2dhdr, sizeof (l2arc_dev_hdr_phys_t), 226 VDEV_LABEL_START_SIZE) == sizeof (l2arc_dev_hdr_phys_t)) { 227 header_cleared++; 228 } 229 } 230 231 free(label); 232 free(l2dhdr); 233 234 if (labels_cleared == 0) 235 return (-1); 236 237 return (0); 238 } 239 240 static boolean_t 241 find_guid(nvlist_t *nv, uint64_t guid) 242 { 243 uint64_t tmp; 244 nvlist_t **child; 245 uint_t c, children; 246 247 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); 248 if (tmp == guid) 249 return (B_TRUE); 250 251 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 252 &child, &children) == 0) { 253 for (c = 0; c < children; c++) 254 if (find_guid(child[c], guid)) 255 return (B_TRUE); 256 } 257 258 return (B_FALSE); 259 } 260 261 typedef struct aux_cbdata { 262 const char *cb_type; 263 uint64_t cb_guid; 264 zpool_handle_t *cb_zhp; 265 } aux_cbdata_t; 266 267 static int 268 find_aux(zpool_handle_t *zhp, void *data) 269 { 270 aux_cbdata_t *cbp = data; 271 nvlist_t **list; 272 uint_t i, count; 273 uint64_t guid; 274 nvlist_t *nvroot; 275 276 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, 277 &nvroot) == 0); 278 279 if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, 280 &list, &count) == 0) { 281 for (i = 0; i < count; i++) { 282 verify(nvlist_lookup_uint64(list[i], 283 ZPOOL_CONFIG_GUID, &guid) == 0); 284 if (guid == cbp->cb_guid) { 285 cbp->cb_zhp = zhp; 286 return (1); 287 } 288 } 289 } 290 291 zpool_close(zhp); 292 return (0); 293 } 294 295 /* 296 * Determines if the pool is in use. If so, it returns true and the state of 297 * the pool as well as the name of the pool. Name string is allocated and 298 * must be freed by the caller. 299 */ 300 int 301 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, 302 boolean_t *inuse) 303 { 304 nvlist_t *config; 305 char *name; 306 boolean_t ret; 307 uint64_t guid, vdev_guid; 308 zpool_handle_t *zhp; 309 nvlist_t *pool_config; 310 uint64_t stateval, isspare; 311 aux_cbdata_t cb = { 0 }; 312 boolean_t isactive; 313 314 *inuse = B_FALSE; 315 316 if (zpool_read_label(fd, &config, NULL) != 0) { 317 (void) no_memory(hdl); 318 return (-1); 319 } 320 321 if (config == NULL) 322 return (0); 323 324 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 325 &stateval) == 0); 326 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 327 &vdev_guid) == 0); 328 329 if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { 330 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, 331 &name) == 0); 332 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 333 &guid) == 0); 334 } 335 336 switch (stateval) { 337 case POOL_STATE_EXPORTED: 338 /* 339 * A pool with an exported state may in fact be imported 340 * read-only, so check the in-core state to see if it's 341 * active and imported read-only. If it is, set 342 * its state to active. 343 */ 344 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive && 345 (zhp = zpool_open_canfail(hdl, name)) != NULL) { 346 if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL)) 347 stateval = POOL_STATE_ACTIVE; 348 349 /* 350 * All we needed the zpool handle for is the 351 * readonly prop check. 352 */ 353 zpool_close(zhp); 354 } 355 356 ret = B_TRUE; 357 break; 358 359 case POOL_STATE_ACTIVE: 360 /* 361 * For an active pool, we have to determine if it's really part 362 * of a currently active pool (in which case the pool will exist 363 * and the guid will be the same), or whether it's part of an 364 * active pool that was disconnected without being explicitly 365 * exported. 366 */ 367 if (pool_active(hdl, name, guid, &isactive) != 0) { 368 nvlist_free(config); 369 return (-1); 370 } 371 372 if (isactive) { 373 /* 374 * Because the device may have been removed while 375 * offlined, we only report it as active if the vdev is 376 * still present in the config. Otherwise, pretend like 377 * it's not in use. 378 */ 379 if ((zhp = zpool_open_canfail(hdl, name)) != NULL && 380 (pool_config = zpool_get_config(zhp, NULL)) 381 != NULL) { 382 nvlist_t *nvroot; 383 384 verify(nvlist_lookup_nvlist(pool_config, 385 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 386 ret = find_guid(nvroot, vdev_guid); 387 } else { 388 ret = B_FALSE; 389 } 390 391 /* 392 * If this is an active spare within another pool, we 393 * treat it like an unused hot spare. This allows the 394 * user to create a pool with a hot spare that currently 395 * in use within another pool. Since we return B_TRUE, 396 * libdiskmgt will continue to prevent generic consumers 397 * from using the device. 398 */ 399 if (ret && nvlist_lookup_uint64(config, 400 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 401 stateval = POOL_STATE_SPARE; 402 403 if (zhp != NULL) 404 zpool_close(zhp); 405 } else { 406 stateval = POOL_STATE_POTENTIALLY_ACTIVE; 407 ret = B_TRUE; 408 } 409 break; 410 411 case POOL_STATE_SPARE: 412 /* 413 * For a hot spare, it can be either definitively in use, or 414 * potentially active. To determine if it's in use, we iterate 415 * over all pools in the system and search for one with a spare 416 * with a matching guid. 417 * 418 * Due to the shared nature of spares, we don't actually report 419 * the potentially active case as in use. This means the user 420 * can freely create pools on the hot spares of exported pools, 421 * but to do otherwise makes the resulting code complicated, and 422 * we end up having to deal with this case anyway. 423 */ 424 cb.cb_zhp = NULL; 425 cb.cb_guid = vdev_guid; 426 cb.cb_type = ZPOOL_CONFIG_SPARES; 427 if (zpool_iter(hdl, find_aux, &cb) == 1) { 428 name = (char *)zpool_get_name(cb.cb_zhp); 429 ret = B_TRUE; 430 } else { 431 ret = B_FALSE; 432 } 433 break; 434 435 case POOL_STATE_L2CACHE: 436 437 /* 438 * Check if any pool is currently using this l2cache device. 439 */ 440 cb.cb_zhp = NULL; 441 cb.cb_guid = vdev_guid; 442 cb.cb_type = ZPOOL_CONFIG_L2CACHE; 443 if (zpool_iter(hdl, find_aux, &cb) == 1) { 444 name = (char *)zpool_get_name(cb.cb_zhp); 445 ret = B_TRUE; 446 } else { 447 ret = B_FALSE; 448 } 449 break; 450 451 default: 452 ret = B_FALSE; 453 } 454 455 456 if (ret) { 457 if ((*namestr = zfs_strdup(hdl, name)) == NULL) { 458 if (cb.cb_zhp) 459 zpool_close(cb.cb_zhp); 460 nvlist_free(config); 461 return (-1); 462 } 463 *state = (pool_state_t)stateval; 464 } 465 466 if (cb.cb_zhp) 467 zpool_close(cb.cb_zhp); 468 469 nvlist_free(config); 470 *inuse = ret; 471 return (0); 472 } 473