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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* 26 * ZFS syseventd module. 27 * 28 * The purpose of this module is to identify when devices are added to the 29 * system, and appropriately online or replace the affected vdevs. 30 * 31 * When a device is added to the system: 32 * 33 * 1. Search for any vdevs whose devid matches that of the newly added 34 * device. 35 * 36 * 2. If no vdevs are found, then search for any vdevs whose devfs path 37 * matches that of the new device. 38 * 39 * 3. If no vdevs match by either method, then ignore the event. 40 * 41 * 4. Attempt to online the device with a flag to indicate that it should 42 * be unspared when resilvering completes. If this succeeds, then the 43 * same device was inserted and we should continue normally. 44 * 45 * 5. If the pool does not have the 'autoreplace' property set, attempt to 46 * online the device again without the unspare flag, which will 47 * generate a FMA fault. 48 * 49 * 6. If the pool has the 'autoreplace' property set, and the matching vdev 50 * is a whole disk, then label the new disk and attempt a 'zpool 51 * replace'. 52 * 53 * The module responds to EC_DEV_ADD events for both disks and lofi devices, 54 * with the latter used for testing. The special ESC_ZFS_VDEV_CHECK event 55 * indicates that a device failed to open during pool load, but the autoreplace 56 * property was set. In this case, we deferred the associated FMA fault until 57 * our module had a chance to process the autoreplace logic. If the device 58 * could not be replaced, then the second online attempt will trigger the FMA 59 * fault that we skipped earlier. 60 */ 61 62 #include <alloca.h> 63 #include <devid.h> 64 #include <fcntl.h> 65 #include <libnvpair.h> 66 #include <libsysevent.h> 67 #include <libzfs.h> 68 #include <limits.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <syslog.h> 72 #include <sys/sunddi.h> 73 #include <sys/sysevent/eventdefs.h> 74 #include <sys/sysevent/dev.h> 75 #include <unistd.h> 76 #include "syseventd.h" 77 78 #if defined(__i386) || defined(__amd64) 79 #define PHYS_PATH ":q" 80 #define RAW_SLICE "p0" 81 #elif defined(__sparc) 82 #define PHYS_PATH ":c" 83 #define RAW_SLICE "s2" 84 #else 85 #error Unknown architecture 86 #endif 87 88 typedef void (*zfs_process_func_t)(zpool_handle_t *, nvlist_t *, boolean_t); 89 90 libzfs_handle_t *g_zfshdl; 91 92 /* 93 * The device associated with the given vdev (either by devid or physical path) 94 * has been added to the system. If 'isdisk' is set, then we only attempt a 95 * replacement if it's a whole disk. This also implies that we should label the 96 * disk first. 97 * 98 * First, we attempt to online the device (making sure to undo any spare 99 * operation when finished). If this succeeds, then we're done. If it fails, 100 * and the new state is VDEV_CANT_OPEN, it indicates that the device was opened, 101 * but that the label was not what we expected. If the 'autoreplace' property 102 * is not set, then we relabel the disk (if specified), and attempt a 'zpool 103 * replace'. If the online is successful, but the new state is something else 104 * (REMOVED or FAULTED), it indicates that we're out of sync or in some sort of 105 * race, and we should avoid attempting to relabel the disk. 106 */ 107 static void 108 zfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t isdisk) 109 { 110 char *path; 111 vdev_state_t newstate; 112 nvlist_t *nvroot, *newvd; 113 uint64_t wholedisk = 0ULL; 114 char *physpath = NULL; 115 char rawpath[PATH_MAX], fullpath[PATH_MAX]; 116 size_t len; 117 118 if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path) != 0) 119 return; 120 121 (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_PHYS_PATH, &physpath); 122 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk); 123 124 /* 125 * We should have a way to online a device by guid. With the current 126 * interface, we are forced to chop off the 's0' for whole disks. 127 */ 128 (void) strlcpy(fullpath, path, sizeof (fullpath)); 129 if (wholedisk) 130 fullpath[strlen(fullpath) - 2] = '\0'; 131 132 /* 133 * Attempt to online the device. It would be nice to online this by 134 * GUID, but the current interface only supports lookup by path. 135 */ 136 if (zpool_vdev_online(zhp, fullpath, 137 ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE, &newstate) == 0 && 138 (newstate == VDEV_STATE_HEALTHY || newstate == VDEV_STATE_DEGRADED)) 139 return; 140 141 /* 142 * If the pool doesn't have the autoreplace property set, then attempt a 143 * true online (without the unspare flag), which will trigger a FMA 144 * fault. 145 */ 146 if (!zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOREPLACE, NULL) || 147 (isdisk && !wholedisk)) { 148 (void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT, 149 &newstate); 150 return; 151 } 152 153 if (isdisk) { 154 /* 155 * If this is a request to label a whole disk, then attempt to 156 * write out the label. Before we can label the disk, we need 157 * access to a raw node. Ideally, we'd like to walk the devinfo 158 * tree and find a raw node from the corresponding parent node. 159 * This is overly complicated, and since we know how we labeled 160 * this device in the first place, we know it's save to switch 161 * from /dev/dsk to /dev/rdsk and append the backup slice. 162 * 163 * If any part of this process fails, then do a force online to 164 * trigger a ZFS fault for the device (and any hot spare 165 * replacement). 166 */ 167 if (strncmp(path, "/dev/dsk/", 9) != 0) { 168 (void) zpool_vdev_online(zhp, fullpath, 169 ZFS_ONLINE_FORCEFAULT, &newstate); 170 return; 171 } 172 173 (void) strlcpy(rawpath, path + 9, sizeof (rawpath)); 174 len = strlen(rawpath); 175 rawpath[len - 2] = '\0'; 176 177 if (zpool_label_disk(g_zfshdl, zhp, rawpath) != 0) { 178 (void) zpool_vdev_online(zhp, fullpath, 179 ZFS_ONLINE_FORCEFAULT, &newstate); 180 return; 181 } 182 } 183 184 /* 185 * Cosntruct the root vdev to pass to zpool_vdev_attach(). While adding 186 * the entire vdev structure is harmless, we construct a reduced set of 187 * path/physpath/wholedisk to keep it simple. 188 */ 189 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) 190 return; 191 192 if (nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) { 193 nvlist_free(nvroot); 194 return; 195 } 196 197 if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK) != 0 || 198 nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0 || 199 (physpath != NULL && nvlist_add_string(newvd, 200 ZPOOL_CONFIG_PHYS_PATH, physpath) != 0) || 201 nvlist_add_uint64(newvd, ZPOOL_CONFIG_WHOLE_DISK, wholedisk) != 0 || 202 nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0 || 203 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &newvd, 204 1) != 0) { 205 nvlist_free(newvd); 206 nvlist_free(nvroot); 207 return; 208 } 209 210 nvlist_free(newvd); 211 212 (void) zpool_vdev_attach(zhp, fullpath, path, nvroot, B_TRUE); 213 214 nvlist_free(nvroot); 215 216 } 217 218 /* 219 * Utility functions to find a vdev matching given criteria. 220 */ 221 typedef struct dev_data { 222 const char *dd_compare; 223 const char *dd_prop; 224 zfs_process_func_t dd_func; 225 boolean_t dd_found; 226 boolean_t dd_isdisk; 227 uint64_t dd_pool_guid; 228 uint64_t dd_vdev_guid; 229 } dev_data_t; 230 231 static void 232 zfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data) 233 { 234 dev_data_t *dp = data; 235 char *path; 236 uint_t c, children; 237 nvlist_t **child; 238 size_t len; 239 uint64_t guid; 240 241 /* 242 * First iterate over any children. 243 */ 244 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, 245 &child, &children) == 0) { 246 for (c = 0; c < children; c++) 247 zfs_iter_vdev(zhp, child[c], data); 248 return; 249 } 250 251 if (dp->dd_vdev_guid != 0) { 252 if (nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_GUID, 253 &guid) != 0 || guid != dp->dd_vdev_guid) 254 return; 255 } else { 256 len = strlen(dp->dd_compare); 257 258 if (nvlist_lookup_string(nvl, dp->dd_prop, &path) != 0 || 259 strncmp(dp->dd_compare, path, len) != 0) 260 return; 261 262 /* 263 * Normally, we want to have an exact match for the comparison 264 * string. However, we allow substring matches in the following 265 * cases: 266 * 267 * <path>: This is a devpath, and the target is one 268 * of its children. 269 * 270 * <path/> This is a devid for a whole disk, and 271 * the target is one of its children. 272 */ 273 if (path[len] != '\0' && path[len] != ':' && 274 path[len - 1] != '/') 275 return; 276 } 277 278 (dp->dd_func)(zhp, nvl, dp->dd_isdisk); 279 } 280 281 static int 282 zfs_iter_pool(zpool_handle_t *zhp, void *data) 283 { 284 nvlist_t *config, *nvl; 285 dev_data_t *dp = data; 286 uint64_t pool_guid; 287 288 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 289 if (dp->dd_pool_guid == 0 || 290 (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 291 &pool_guid) == 0 && pool_guid == dp->dd_pool_guid)) { 292 (void) nvlist_lookup_nvlist(config, 293 ZPOOL_CONFIG_VDEV_TREE, &nvl); 294 zfs_iter_vdev(zhp, nvl, data); 295 } 296 } 297 298 zpool_close(zhp); 299 return (0); 300 } 301 302 /* 303 * Given a physical device path, iterate over all (pool, vdev) pairs which 304 * correspond to the given path. 305 */ 306 static boolean_t 307 devpath_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk) 308 { 309 dev_data_t data = { 0 }; 310 311 data.dd_compare = devpath; 312 data.dd_func = func; 313 data.dd_prop = ZPOOL_CONFIG_PHYS_PATH; 314 data.dd_found = B_FALSE; 315 data.dd_isdisk = wholedisk; 316 317 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 318 319 return (data.dd_found); 320 } 321 322 /* 323 * Given a /devices path, lookup the corresponding devid for each minor node, 324 * and find any vdevs with matching devids. Doing this straight up would be 325 * rather inefficient, O(minor nodes * vdevs in system), so we take advantage of 326 * the fact that each devid ends with "/<minornode>". Once we find any valid 327 * minor node, we chop off the portion after the last slash, and then search for 328 * matching vdevs, which is O(vdevs in system). 329 */ 330 static boolean_t 331 devid_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk) 332 { 333 size_t len = strlen(devpath) + sizeof ("/devices") + 334 sizeof (PHYS_PATH) - 1; 335 char *fullpath; 336 int fd; 337 ddi_devid_t devid; 338 char *devidstr, *fulldevid; 339 dev_data_t data = { 0 }; 340 341 /* 342 * Try to open a known minor node. 343 */ 344 fullpath = alloca(len); 345 (void) snprintf(fullpath, len, "/devices%s%s", devpath, PHYS_PATH); 346 if ((fd = open(fullpath, O_RDONLY)) < 0) 347 return (B_FALSE); 348 349 /* 350 * Determine the devid as a string, with no trailing slash for the minor 351 * node. 352 */ 353 if (devid_get(fd, &devid) != 0) { 354 (void) close(fd); 355 return (B_FALSE); 356 } 357 (void) close(fd); 358 359 if ((devidstr = devid_str_encode(devid, NULL)) == NULL) { 360 devid_free(devid); 361 return (B_FALSE); 362 } 363 364 len = strlen(devidstr) + 2; 365 fulldevid = alloca(len); 366 (void) snprintf(fulldevid, len, "%s/", devidstr); 367 368 data.dd_compare = fulldevid; 369 data.dd_func = func; 370 data.dd_prop = ZPOOL_CONFIG_DEVID; 371 data.dd_found = B_FALSE; 372 data.dd_isdisk = wholedisk; 373 374 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 375 376 devid_str_free(devidstr); 377 378 return (data.dd_found); 379 } 380 381 /* 382 * This function is called when we receive a devfs add event. This can be 383 * either a disk event or a lofi event, and the behavior is slightly different 384 * depending on which it is. 385 */ 386 static int 387 zfs_deliver_add(nvlist_t *nvl, boolean_t is_lofi) 388 { 389 char *devpath, *devname; 390 char path[PATH_MAX], realpath[PATH_MAX]; 391 char *colon, *raw; 392 int ret; 393 394 /* 395 * The main unit of operation is the physical device path. For disks, 396 * this is the device node, as all minor nodes are affected. For lofi 397 * devices, this includes the minor path. Unfortunately, this isn't 398 * represented in the DEV_PHYS_PATH for various reasons. 399 */ 400 if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devpath) != 0) 401 return (-1); 402 403 /* 404 * If this is a lofi device, then also get the minor instance name. 405 * Unfortunately, the current payload doesn't include an easy way to get 406 * this information. So we cheat by resolving the 'dev_name' (which 407 * refers to the raw device) and taking the portion between ':(*),raw'. 408 */ 409 (void) strlcpy(realpath, devpath, sizeof (realpath)); 410 if (is_lofi) { 411 if (nvlist_lookup_string(nvl, DEV_NAME, 412 &devname) == 0 && 413 (ret = resolvepath(devname, path, 414 sizeof (path))) > 0) { 415 path[ret] = '\0'; 416 colon = strchr(path, ':'); 417 if (colon != NULL) 418 raw = strstr(colon + 1, ",raw"); 419 if (colon != NULL && raw != NULL) { 420 *raw = '\0'; 421 (void) snprintf(realpath, 422 sizeof (realpath), "%s%s", 423 devpath, colon); 424 *raw = ','; 425 } 426 } 427 } 428 429 /* 430 * Iterate over all vdevs with a matching devid, and then those with a 431 * matching /devices path. For disks, we only want to pay attention to 432 * vdevs marked as whole disks. For lofi, we don't care (because we're 433 * matching an exact minor name). 434 */ 435 if (!devid_iter(realpath, zfs_process_add, !is_lofi)) 436 (void) devpath_iter(realpath, zfs_process_add, !is_lofi); 437 438 return (0); 439 } 440 441 /* 442 * Called when we receive a VDEV_CHECK event, which indicates a device could not 443 * be opened during initial pool open, but the autoreplace property was set on 444 * the pool. In this case, we treat it as if it were an add event. 445 */ 446 static int 447 zfs_deliver_check(nvlist_t *nvl) 448 { 449 dev_data_t data = { 0 }; 450 451 if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID, 452 &data.dd_pool_guid) != 0 || 453 nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, 454 &data.dd_vdev_guid) != 0) 455 return (0); 456 457 data.dd_isdisk = B_TRUE; 458 data.dd_func = zfs_process_add; 459 460 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 461 462 return (0); 463 } 464 465 #define DEVICE_PREFIX "/devices" 466 467 static int 468 zfsdle_vdev_online(zpool_handle_t *zhp, void *data) 469 { 470 char *devname = data; 471 boolean_t avail_spare, l2cache; 472 vdev_state_t newstate; 473 nvlist_t *tgt; 474 475 syseventd_print(9, "zfsdle_vdev_online: searching for %s in pool %s\n", 476 devname, zpool_get_name(zhp)); 477 478 if ((tgt = zpool_find_vdev_by_physpath(zhp, devname, 479 &avail_spare, &l2cache, NULL)) != NULL) { 480 char *path, fullpath[MAXPATHLEN]; 481 uint64_t wholedisk = 0ULL; 482 483 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, 484 &path) == 0); 485 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 486 &wholedisk) == 0); 487 488 (void) strlcpy(fullpath, path, sizeof (fullpath)); 489 if (wholedisk) 490 fullpath[strlen(fullpath) - 2] = '\0'; 491 492 if (zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 493 syseventd_print(9, "zfsdle_vdev_online: setting device" 494 " device %s to ONLINE state in pool %s.\n", 495 fullpath, zpool_get_name(zhp)); 496 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) 497 (void) zpool_vdev_online(zhp, fullpath, 0, 498 &newstate); 499 } 500 return (1); 501 } 502 return (0); 503 } 504 505 int 506 zfs_deliver_dle(nvlist_t *nvl) 507 { 508 char *devname; 509 if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devname) != 0) { 510 syseventd_print(9, "zfs_deliver_event: no physpath\n"); 511 return (-1); 512 } 513 if (strncmp(devname, DEVICE_PREFIX, strlen(DEVICE_PREFIX)) != 0) { 514 syseventd_print(9, "zfs_deliver_event: invalid " 515 "device '%s'", devname); 516 return (-1); 517 } 518 519 /* 520 * We try to find the device using the physical 521 * path that has been supplied. We need to strip off 522 * the /devices prefix before starting our search. 523 */ 524 devname += strlen(DEVICE_PREFIX); 525 if (zpool_iter(g_zfshdl, zfsdle_vdev_online, devname) != 1) { 526 syseventd_print(9, "zfs_deliver_event: device '%s' not" 527 " found\n", devname); 528 return (1); 529 } 530 nvlist_free(nvl); 531 return (0); 532 } 533 534 535 /*ARGSUSED*/ 536 static int 537 zfs_deliver_event(sysevent_t *ev, int unused) 538 { 539 const char *class = sysevent_get_class_name(ev); 540 const char *subclass = sysevent_get_subclass_name(ev); 541 nvlist_t *nvl; 542 int ret; 543 boolean_t is_lofi, is_check, is_dle = B_FALSE; 544 545 if (strcmp(class, EC_DEV_ADD) == 0) { 546 /* 547 * We're mainly interested in disk additions, but we also listen 548 * for new lofi devices, to allow for simplified testing. 549 */ 550 if (strcmp(subclass, ESC_DISK) == 0) 551 is_lofi = B_FALSE; 552 else if (strcmp(subclass, ESC_LOFI) == 0) 553 is_lofi = B_TRUE; 554 else 555 return (0); 556 557 is_check = B_FALSE; 558 } else if (strcmp(class, EC_ZFS) == 0 && 559 strcmp(subclass, ESC_ZFS_VDEV_CHECK) == 0) { 560 /* 561 * This event signifies that a device failed to open during pool 562 * load, but the 'autoreplace' property was set, so we should 563 * pretend it's just been added. 564 */ 565 is_check = B_TRUE; 566 } else if (strcmp(class, EC_DEV_STATUS) == 0 && 567 strcmp(subclass, ESC_DEV_DLE) == 0) { 568 is_dle = B_TRUE; 569 } else { 570 return (0); 571 } 572 573 if (sysevent_get_attr_list(ev, &nvl) != 0) 574 return (-1); 575 576 if (is_dle) 577 ret = zfs_deliver_dle(nvl); 578 else if (is_check) 579 ret = zfs_deliver_check(nvl); 580 else 581 ret = zfs_deliver_add(nvl, is_lofi); 582 583 nvlist_free(nvl); 584 return (ret); 585 } 586 587 static struct slm_mod_ops zfs_mod_ops = { 588 SE_MAJOR_VERSION, SE_MINOR_VERSION, 10, zfs_deliver_event 589 }; 590 591 struct slm_mod_ops * 592 slm_init() 593 { 594 if ((g_zfshdl = libzfs_init()) == NULL) 595 return (NULL); 596 597 return (&zfs_mod_ops); 598 } 599 600 void 601 slm_fini() 602 { 603 } 604