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 Version 1.0 (CDDL-1.0). 6 * You can obtain a copy of the license from the top-level file 7 * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>. 8 * You may not use this file except in compliance with the license. 9 * 10 * CDDL HEADER END 11 */ 12 13 /* 14 * Copyright (c) 2016, 2017, Intel Corporation. 15 */ 16 17 #ifdef HAVE_LIBUDEV 18 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <libnvpair.h> 22 #include <libudev.h> 23 #include <libzfs.h> 24 #include <libzutil.h> 25 #include <pthread.h> 26 #include <stdlib.h> 27 #include <string.h> 28 29 #include <sys/sysevent/eventdefs.h> 30 #include <sys/sysevent/dev.h> 31 32 #include "zed_log.h" 33 #include "zed_disk_event.h" 34 #include "agents/zfs_agents.h" 35 36 /* 37 * Portions of ZED need to see disk events for disks belonging to ZFS pools. 38 * A libudev monitor is established to monitor block device actions and pass 39 * them on to internal ZED logic modules. Initially, zfs_mod.c is the only 40 * consumer and is the Linux equivalent for the illumos syseventd ZFS SLM 41 * module responsible for handling disk events for ZFS. 42 */ 43 44 pthread_t g_mon_tid; 45 struct udev *g_udev; 46 struct udev_monitor *g_mon; 47 48 49 #define DEV_BYID_PATH "/dev/disk/by-id/" 50 51 /* 64MB is minimum usable disk for ZFS */ 52 #define MINIMUM_SECTORS 131072 53 54 55 /* 56 * Post disk event to SLM module 57 * 58 * occurs in the context of monitor thread 59 */ 60 static void 61 zed_udev_event(const char *class, const char *subclass, nvlist_t *nvl) 62 { 63 char *strval; 64 uint64_t numval; 65 66 zed_log_msg(LOG_INFO, "zed_disk_event:"); 67 zed_log_msg(LOG_INFO, "\tclass: %s", class); 68 zed_log_msg(LOG_INFO, "\tsubclass: %s", subclass); 69 if (nvlist_lookup_string(nvl, DEV_NAME, &strval) == 0) 70 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_NAME, strval); 71 if (nvlist_lookup_string(nvl, DEV_PATH, &strval) == 0) 72 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_PATH, strval); 73 if (nvlist_lookup_string(nvl, DEV_IDENTIFIER, &strval) == 0) 74 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_IDENTIFIER, strval); 75 if (nvlist_lookup_boolean(nvl, DEV_IS_PART) == B_TRUE) 76 zed_log_msg(LOG_INFO, "\t%s: B_TRUE", DEV_IS_PART); 77 if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &strval) == 0) 78 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_PHYS_PATH, strval); 79 if (nvlist_lookup_uint64(nvl, DEV_SIZE, &numval) == 0) 80 zed_log_msg(LOG_INFO, "\t%s: %llu", DEV_SIZE, numval); 81 if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID, &numval) == 0) 82 zed_log_msg(LOG_INFO, "\t%s: %llu", ZFS_EV_POOL_GUID, numval); 83 if (nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &numval) == 0) 84 zed_log_msg(LOG_INFO, "\t%s: %llu", ZFS_EV_VDEV_GUID, numval); 85 86 (void) zfs_agent_post_event(class, subclass, nvl); 87 } 88 89 /* 90 * dev_event_nvlist: place event schema into an nv pair list 91 * 92 * NAME VALUE (example) 93 * -------------- -------------------------------------------------------- 94 * DEV_NAME /dev/sdl 95 * DEV_PATH /devices/pci0000:00/0000:00:03.0/0000:04:00.0/host0/... 96 * DEV_IDENTIFIER ata-Hitachi_HTS725050A9A362_100601PCG420VLJ37DMC 97 * DEV_PHYS_PATH pci-0000:04:00.0-sas-0x4433221101000000-lun-0 98 * DEV_IS_PART --- 99 * DEV_SIZE 500107862016 100 * ZFS_EV_POOL_GUID 17523635698032189180 101 * ZFS_EV_VDEV_GUID 14663607734290803088 102 */ 103 static nvlist_t * 104 dev_event_nvlist(struct udev_device *dev) 105 { 106 nvlist_t *nvl; 107 char strval[128]; 108 const char *value, *path; 109 uint64_t guid; 110 111 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 112 return (NULL); 113 114 if (zfs_device_get_devid(dev, strval, sizeof (strval)) == 0) 115 (void) nvlist_add_string(nvl, DEV_IDENTIFIER, strval); 116 if (zfs_device_get_physical(dev, strval, sizeof (strval)) == 0) 117 (void) nvlist_add_string(nvl, DEV_PHYS_PATH, strval); 118 if ((path = udev_device_get_devnode(dev)) != NULL) 119 (void) nvlist_add_string(nvl, DEV_NAME, path); 120 if ((value = udev_device_get_devpath(dev)) != NULL) 121 (void) nvlist_add_string(nvl, DEV_PATH, value); 122 value = udev_device_get_devtype(dev); 123 if ((value != NULL && strcmp("partition", value) == 0) || 124 (udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER") 125 != NULL)) { 126 (void) nvlist_add_boolean(nvl, DEV_IS_PART); 127 } 128 if ((value = udev_device_get_sysattr_value(dev, "size")) != NULL) { 129 uint64_t numval = DEV_BSIZE; 130 131 numval *= strtoull(value, NULL, 10); 132 (void) nvlist_add_uint64(nvl, DEV_SIZE, numval); 133 } 134 135 /* 136 * Grab the pool and vdev guids from blkid cache 137 */ 138 value = udev_device_get_property_value(dev, "ID_FS_UUID"); 139 if (value != NULL && (guid = strtoull(value, NULL, 10)) != 0) 140 (void) nvlist_add_uint64(nvl, ZFS_EV_POOL_GUID, guid); 141 142 value = udev_device_get_property_value(dev, "ID_FS_UUID_SUB"); 143 if (value != NULL && (guid = strtoull(value, NULL, 10)) != 0) 144 (void) nvlist_add_uint64(nvl, ZFS_EV_VDEV_GUID, guid); 145 146 /* 147 * Either a vdev guid or a devid must be present for matching 148 */ 149 if (!nvlist_exists(nvl, DEV_IDENTIFIER) && 150 !nvlist_exists(nvl, ZFS_EV_VDEV_GUID)) { 151 nvlist_free(nvl); 152 return (NULL); 153 } 154 155 return (nvl); 156 } 157 158 /* 159 * Listen for block device uevents 160 */ 161 static void * 162 zed_udev_monitor(void *arg) 163 { 164 struct udev_monitor *mon = arg; 165 char *tmp, *tmp2; 166 167 zed_log_msg(LOG_INFO, "Waiting for new udev disk events..."); 168 169 while (1) { 170 struct udev_device *dev; 171 const char *action, *type, *part, *sectors; 172 const char *bus, *uuid, *devpath; 173 const char *class, *subclass; 174 nvlist_t *nvl; 175 boolean_t is_zfs = B_FALSE; 176 177 /* allow a cancellation while blocked (recvmsg) */ 178 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); 179 180 /* blocks at recvmsg until an event occurs */ 181 if ((dev = udev_monitor_receive_device(mon)) == NULL) { 182 zed_log_msg(LOG_WARNING, "zed_udev_monitor: receive " 183 "device error %d", errno); 184 continue; 185 } 186 187 /* allow all steps to complete before a cancellation */ 188 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 189 190 /* 191 * Strongly typed device is the preferred filter 192 */ 193 type = udev_device_get_property_value(dev, "ID_FS_TYPE"); 194 if (type != NULL && type[0] != '\0') { 195 if (strcmp(type, "zfs_member") == 0) { 196 is_zfs = B_TRUE; 197 } else { 198 /* not ours, so skip */ 199 zed_log_msg(LOG_INFO, "zed_udev_monitor: skip " 200 "%s (in use by %s)", 201 udev_device_get_devnode(dev), type); 202 udev_device_unref(dev); 203 continue; 204 } 205 } 206 207 /* 208 * if this is a disk and it is partitioned, then the 209 * zfs label will reside in a DEVTYPE=partition and 210 * we can skip passing this event 211 * 212 * Special case: Blank disks are sometimes reported with 213 * an erroneous 'atari' partition, and should not be 214 * excluded from being used as an autoreplace disk: 215 * 216 * https://github.com/openzfs/zfs/issues/13497 217 */ 218 type = udev_device_get_property_value(dev, "DEVTYPE"); 219 part = udev_device_get_property_value(dev, 220 "ID_PART_TABLE_TYPE"); 221 if (type != NULL && type[0] != '\0' && 222 strcmp(type, "disk") == 0 && 223 part != NULL && part[0] != '\0') { 224 const char *devname = 225 udev_device_get_property_value(dev, "DEVNAME"); 226 227 if (strcmp(part, "atari") == 0) { 228 zed_log_msg(LOG_INFO, 229 "%s: %s is reporting an atari partition, " 230 "but we're going to assume it's a false " 231 "positive and still use it (issue #13497)", 232 __func__, devname); 233 } else { 234 zed_log_msg(LOG_INFO, 235 "%s: skip %s since it has a %s partition " 236 "already", __func__, devname, part); 237 /* skip and wait for partition event */ 238 udev_device_unref(dev); 239 continue; 240 } 241 } 242 243 /* 244 * ignore small partitions 245 */ 246 sectors = udev_device_get_property_value(dev, 247 "ID_PART_ENTRY_SIZE"); 248 if (sectors == NULL) 249 sectors = udev_device_get_sysattr_value(dev, "size"); 250 if (sectors != NULL && 251 strtoull(sectors, NULL, 10) < MINIMUM_SECTORS) { 252 zed_log_msg(LOG_INFO, 253 "%s: %s sectors %s < %llu (minimum)", 254 __func__, 255 udev_device_get_property_value(dev, "DEVNAME"), 256 sectors, MINIMUM_SECTORS); 257 udev_device_unref(dev); 258 continue; 259 } 260 261 /* 262 * If the blkid probe didn't find ZFS, then a persistent 263 * device id string is required in the message schema 264 * for matching with vdevs. Preflight here for expected 265 * udev information. 266 * 267 * Special case: 268 * NVMe devices don't have ID_BUS set (at least on RHEL 7-8), 269 * but they are valid for autoreplace. Add a special case for 270 * them by searching for "/nvme/" in the udev DEVPATH: 271 * 272 * DEVPATH=/devices/pci0000:00/0000:00:1e.0/nvme/nvme2/nvme2n1 273 */ 274 bus = udev_device_get_property_value(dev, "ID_BUS"); 275 uuid = udev_device_get_property_value(dev, "DM_UUID"); 276 devpath = udev_device_get_devpath(dev); 277 if (!is_zfs && (bus == NULL && uuid == NULL && 278 strstr(devpath, "/nvme/") == NULL)) { 279 zed_log_msg(LOG_INFO, "zed_udev_monitor: %s no devid " 280 "source", udev_device_get_devnode(dev)); 281 udev_device_unref(dev); 282 continue; 283 } 284 285 action = udev_device_get_action(dev); 286 if (strcmp(action, "add") == 0) { 287 class = EC_DEV_ADD; 288 subclass = ESC_DISK; 289 } else if (strcmp(action, "remove") == 0) { 290 class = EC_DEV_REMOVE; 291 subclass = ESC_DISK; 292 } else if (strcmp(action, "change") == 0) { 293 class = EC_DEV_STATUS; 294 subclass = ESC_DEV_DLE; 295 } else { 296 zed_log_msg(LOG_WARNING, "zed_udev_monitor: %s unknown", 297 action); 298 udev_device_unref(dev); 299 continue; 300 } 301 302 /* 303 * Special case an EC_DEV_ADD for multipath devices 304 * 305 * When a multipath device is created, udev reports the 306 * following: 307 * 308 * 1. "add" event of the dm device for the multipath device 309 * (like /dev/dm-3). 310 * 2. "change" event to create the actual multipath device 311 * symlink (like /dev/mapper/mpatha). The event also 312 * passes back the relevant DM vars we care about, like 313 * DM_UUID. 314 * 3. Another "change" event identical to #2 (that we ignore). 315 * 316 * To get the behavior we want, we treat the "change" event 317 * in #2 as a "add" event; as if "/dev/mapper/mpatha" was 318 * a new disk being added. 319 */ 320 if (strcmp(class, EC_DEV_STATUS) == 0 && 321 udev_device_get_property_value(dev, "DM_UUID") && 322 udev_device_get_property_value(dev, "MPATH_SBIN_PATH")) { 323 tmp = (char *)udev_device_get_devnode(dev); 324 tmp2 = zfs_get_underlying_path(tmp); 325 if (tmp && tmp2 && (strcmp(tmp, tmp2) != 0)) { 326 /* 327 * We have a real underlying device, which 328 * means that this multipath "change" event is 329 * an "add" event. 330 * 331 * If the multipath device and the underlying 332 * dev are the same name (i.e. /dev/dm-5), then 333 * there is no real underlying disk for this 334 * multipath device, and so this "change" event 335 * really is a multipath removal. 336 */ 337 class = EC_DEV_ADD; 338 subclass = ESC_DISK; 339 } else { 340 tmp = (char *) 341 udev_device_get_property_value(dev, 342 "DM_NR_VALID_PATHS"); 343 /* treat as a multipath remove */ 344 if (tmp != NULL && strcmp(tmp, "0") == 0) { 345 class = EC_DEV_REMOVE; 346 subclass = ESC_DISK; 347 } 348 } 349 free(tmp2); 350 } 351 352 /* 353 * Special case an EC_DEV_ADD for scsi_debug devices 354 * 355 * These devices require a udevadm trigger command after 356 * creation in order to register the vdev_id scsidebug alias 357 * rule (adds a persistent path (phys_path) used for fault 358 * management automated tests in the ZFS test suite. 359 * 360 * After udevadm trigger command, event registers as a "change" 361 * event but needs to instead be handled as another "add" event 362 * to allow for disk labeling and partitioning to occur. 363 */ 364 if (strcmp(class, EC_DEV_STATUS) == 0 && 365 udev_device_get_property_value(dev, "ID_VDEV") && 366 udev_device_get_property_value(dev, "ID_MODEL")) { 367 const char *id_model, *id_model_sd = "scsi_debug"; 368 369 id_model = udev_device_get_property_value(dev, 370 "ID_MODEL"); 371 if (strcmp(id_model, id_model_sd) == 0) { 372 class = EC_DEV_ADD; 373 subclass = ESC_DISK; 374 } 375 } 376 377 if ((nvl = dev_event_nvlist(dev)) != NULL) { 378 zed_udev_event(class, subclass, nvl); 379 nvlist_free(nvl); 380 } 381 382 udev_device_unref(dev); 383 } 384 385 return (NULL); 386 } 387 388 int 389 zed_disk_event_init(void) 390 { 391 int fd, fflags; 392 393 if ((g_udev = udev_new()) == NULL) { 394 zed_log_msg(LOG_WARNING, "udev_new failed (%d)", errno); 395 return (-1); 396 } 397 398 /* Set up a udev monitor for block devices */ 399 g_mon = udev_monitor_new_from_netlink(g_udev, "udev"); 400 udev_monitor_filter_add_match_subsystem_devtype(g_mon, "block", "disk"); 401 udev_monitor_filter_add_match_subsystem_devtype(g_mon, "block", 402 "partition"); 403 udev_monitor_enable_receiving(g_mon); 404 405 /* Make sure monitoring socket is blocking */ 406 fd = udev_monitor_get_fd(g_mon); 407 if ((fflags = fcntl(fd, F_GETFL)) & O_NONBLOCK) 408 (void) fcntl(fd, F_SETFL, fflags & ~O_NONBLOCK); 409 410 /* spawn a thread to monitor events */ 411 if (pthread_create(&g_mon_tid, NULL, zed_udev_monitor, g_mon) != 0) { 412 udev_monitor_unref(g_mon); 413 udev_unref(g_udev); 414 zed_log_msg(LOG_WARNING, "pthread_create failed"); 415 return (-1); 416 } 417 418 pthread_setname_np(g_mon_tid, "udev monitor"); 419 zed_log_msg(LOG_INFO, "zed_disk_event_init"); 420 421 return (0); 422 } 423 424 void 425 zed_disk_event_fini(void) 426 { 427 /* cancel monitor thread at recvmsg() */ 428 (void) pthread_cancel(g_mon_tid); 429 (void) pthread_join(g_mon_tid, NULL); 430 431 /* cleanup udev resources */ 432 udev_monitor_unref(g_mon); 433 udev_unref(g_udev); 434 435 zed_log_msg(LOG_INFO, "zed_disk_event_fini"); 436 } 437 438 #else 439 440 #include "zed_disk_event.h" 441 442 int 443 zed_disk_event_init(void) 444 { 445 return (0); 446 } 447 448 void 449 zed_disk_event_fini(void) 450 { 451 } 452 453 #endif /* HAVE_LIBUDEV */ 454