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.
14 * Copyright (c) 2016, Intel Corporation.
21 #include <libnvpair.h>
28 #include <sys/sysevent/eventdefs.h>
29 #include <sys/sysevent/dev.h>
32 #include "zed_disk_event.h"
33 #include "agents/zfs_agents.h"
36 * Portions of ZED need to see disk events for disks belonging to ZFS pools.
37 * A libudev monitor is established to monitor block device actions and pass
38 * them on to internal ZED logic modules. Initially, zfs_mod.c is the only
39 * consumer and is the Linux equivalent for the illumos syseventd ZFS SLM
40 * module responsible for handeling disk events for ZFS.
45 struct udev_monitor *g_mon;
48 #define DEV_BYID_PATH "/dev/disk/by-id/"
50 /* 64MB is minimum usable disk for ZFS */
51 #define MINIMUM_SECTORS 131072
55 * Post disk event to SLM module
57 * occurs in the context of monitor thread
60 zed_udev_event(const char *class, const char *subclass, nvlist_t *nvl)
65 zed_log_msg(LOG_INFO, "zed_disk_event:");
66 zed_log_msg(LOG_INFO, "\tclass: %s", class);
67 zed_log_msg(LOG_INFO, "\tsubclass: %s", subclass);
68 if (nvlist_lookup_string(nvl, DEV_NAME, &strval) == 0)
69 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_NAME, strval);
70 if (nvlist_lookup_string(nvl, DEV_PATH, &strval) == 0)
71 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_PATH, strval);
72 if (nvlist_lookup_string(nvl, DEV_IDENTIFIER, &strval) == 0)
73 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_IDENTIFIER, strval);
74 if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &strval) == 0)
75 zed_log_msg(LOG_INFO, "\t%s: %s", DEV_PHYS_PATH, strval);
76 if (nvlist_lookup_uint64(nvl, DEV_SIZE, &numval) == 0)
77 zed_log_msg(LOG_INFO, "\t%s: %llu", DEV_SIZE, numval);
78 if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID, &numval) == 0)
79 zed_log_msg(LOG_INFO, "\t%s: %llu", ZFS_EV_POOL_GUID, numval);
80 if (nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &numval) == 0)
81 zed_log_msg(LOG_INFO, "\t%s: %llu", ZFS_EV_VDEV_GUID, numval);
83 (void) zfs_agent_post_event(class, subclass, nvl);
87 * dev_event_nvlist: place event schema into an nv pair list
89 * NAME VALUE (example)
90 * -------------- --------------------------------------------------------
92 * DEV_PATH /devices/pci0000:00/0000:00:03.0/0000:04:00.0/host0/...
93 * DEV_IDENTIFIER ata-Hitachi_HTS725050A9A362_100601PCG420VLJ37DMC
94 * DEV_PHYS_PATH pci-0000:04:00.0-sas-0x4433221101000000-lun-0
96 * DEV_SIZE 500107862016
97 * ZFS_EV_POOL_GUID 17523635698032189180
98 * ZFS_EV_VDEV_GUID 14663607734290803088
101 dev_event_nvlist(struct udev_device *dev)
105 const char *value, *path;
108 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
111 if (zfs_device_get_devid(dev, strval, sizeof (strval)) == 0)
112 (void) nvlist_add_string(nvl, DEV_IDENTIFIER, strval);
113 if (zfs_device_get_physical(dev, strval, sizeof (strval)) == 0)
114 (void) nvlist_add_string(nvl, DEV_PHYS_PATH, strval);
115 if ((path = udev_device_get_devnode(dev)) != NULL)
116 (void) nvlist_add_string(nvl, DEV_NAME, path);
117 if ((value = udev_device_get_devpath(dev)) != NULL)
118 (void) nvlist_add_string(nvl, DEV_PATH, value);
119 value = udev_device_get_devtype(dev);
120 if ((value != NULL && strcmp("partition", value) == 0) ||
121 (udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER")
123 (void) nvlist_add_boolean(nvl, DEV_IS_PART);
125 if ((value = udev_device_get_sysattr_value(dev, "size")) != NULL) {
126 uint64_t numval = DEV_BSIZE;
128 numval *= strtoull(value, NULL, 10);
129 (void) nvlist_add_uint64(nvl, DEV_SIZE, numval);
133 * Grab the pool and vdev guids from blkid cache
135 value = udev_device_get_property_value(dev, "ID_FS_UUID");
136 if (value != NULL && (guid = strtoull(value, NULL, 10)) != 0)
137 (void) nvlist_add_uint64(nvl, ZFS_EV_POOL_GUID, guid);
139 value = udev_device_get_property_value(dev, "ID_FS_UUID_SUB");
140 if (value != NULL && (guid = strtoull(value, NULL, 10)) != 0)
141 (void) nvlist_add_uint64(nvl, ZFS_EV_VDEV_GUID, guid);
144 * Either a vdev guid or a devid must be present for matching
146 if (!nvlist_exists(nvl, DEV_IDENTIFIER) &&
147 !nvlist_exists(nvl, ZFS_EV_VDEV_GUID)) {
156 * Listen for block device uevents
159 zed_udev_monitor(void *arg)
161 struct udev_monitor *mon = arg;
164 zed_log_msg(LOG_INFO, "Waiting for new uduev disk events...");
167 struct udev_device *dev;
168 const char *action, *type, *part, *sectors;
169 const char *bus, *uuid;
170 const char *class, *subclass;
172 boolean_t is_zfs = B_FALSE;
174 /* allow a cancellation while blocked (recvmsg) */
175 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
177 /* blocks at recvmsg until an event occurs */
178 if ((dev = udev_monitor_receive_device(mon)) == NULL) {
179 zed_log_msg(LOG_WARNING, "zed_udev_monitor: receive "
180 "device error %d", errno);
184 /* allow all steps to complete before a cancellation */
185 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
188 * Strongly typed device is the prefered filter
190 type = udev_device_get_property_value(dev, "ID_FS_TYPE");
191 if (type != NULL && type[0] != '\0') {
192 if (strcmp(type, "zfs_member") == 0) {
195 /* not ours, so skip */
196 zed_log_msg(LOG_INFO, "zed_udev_monitor: skip "
198 udev_device_get_devnode(dev), type);
199 udev_device_unref(dev);
205 * if this is a disk and it is partitioned, then the
206 * zfs label will reside in a DEVTYPE=partition and
207 * we can skip passing this event
209 type = udev_device_get_property_value(dev, "DEVTYPE");
210 part = udev_device_get_property_value(dev,
211 "ID_PART_TABLE_TYPE");
212 if (type != NULL && type[0] != '\0' &&
213 strcmp(type, "disk") == 0 &&
214 part != NULL && part[0] != '\0') {
215 /* skip and wait for partition event */
216 udev_device_unref(dev);
221 * ignore small partitions
223 sectors = udev_device_get_property_value(dev,
224 "ID_PART_ENTRY_SIZE");
226 sectors = udev_device_get_sysattr_value(dev, "size");
227 if (sectors != NULL &&
228 strtoull(sectors, NULL, 10) < MINIMUM_SECTORS) {
229 udev_device_unref(dev);
234 * If the blkid probe didn't find ZFS, then a persistent
235 * device id string is required in the message schema
236 * for matching with vdevs. Preflight here for expected
239 bus = udev_device_get_property_value(dev, "ID_BUS");
240 uuid = udev_device_get_property_value(dev, "DM_UUID");
241 if (!is_zfs && (bus == NULL && uuid == NULL)) {
242 zed_log_msg(LOG_INFO, "zed_udev_monitor: %s no devid "
243 "source", udev_device_get_devnode(dev));
244 udev_device_unref(dev);
248 action = udev_device_get_action(dev);
249 if (strcmp(action, "add") == 0) {
252 } else if (strcmp(action, "remove") == 0) {
253 class = EC_DEV_REMOVE;
255 } else if (strcmp(action, "change") == 0) {
256 class = EC_DEV_STATUS;
257 subclass = ESC_DEV_DLE;
259 zed_log_msg(LOG_WARNING, "zed_udev_monitor: %s unknown",
261 udev_device_unref(dev);
266 * Special case an EC_DEV_ADD for multipath devices
268 * When a multipath device is created, udev reports the
271 * 1. "add" event of the dm device for the multipath device
273 * 2. "change" event to create the actual multipath device
274 * symlink (like /dev/mapper/mpatha). The event also
275 * passes back the relevant DM vars we care about, like
277 * 3. Another "change" event identical to #2 (that we ignore).
279 * To get the behavior we want, we treat the "change" event
280 * in #2 as a "add" event; as if "/dev/mapper/mpatha" was
281 * a new disk being added.
283 if (strcmp(class, EC_DEV_STATUS) == 0 &&
284 udev_device_get_property_value(dev, "DM_UUID") &&
285 udev_device_get_property_value(dev, "MPATH_SBIN_PATH")) {
286 tmp = (char *) udev_device_get_devnode(dev);
287 tmp2 = zfs_get_underlying_path(tmp);
288 if (tmp && tmp2 && (strcmp(tmp, tmp2) != 0)) {
290 * We have a real underlying device, which
291 * means that this multipath "change" event is
294 * If the multipath device and the underlying
295 * dev are the same name (i.e. /dev/dm-5), then
296 * there is no real underlying disk for this
297 * multipath device, and so this "change" event
298 * really is a multipath removal.
304 udev_device_get_property_value(dev,
305 "DM_NR_VALID_PATHS");
306 /* treat as a multipath remove */
307 if (tmp != NULL && strcmp(tmp, "0") == 0) {
308 class = EC_DEV_REMOVE;
315 if ((nvl = dev_event_nvlist(dev)) != NULL) {
316 zed_udev_event(class, subclass, nvl);
320 udev_device_unref(dev);
327 zed_disk_event_init()
331 if ((g_udev = udev_new()) == NULL) {
332 zed_log_msg(LOG_WARNING, "udev_new failed (%d)", errno);
336 /* Set up a udev monitor for block devices */
337 g_mon = udev_monitor_new_from_netlink(g_udev, "udev");
338 udev_monitor_filter_add_match_subsystem_devtype(g_mon, "block", "disk");
339 udev_monitor_filter_add_match_subsystem_devtype(g_mon, "block",
341 udev_monitor_enable_receiving(g_mon);
343 /* Make sure monitoring socket is blocking */
344 fd = udev_monitor_get_fd(g_mon);
345 if ((fflags = fcntl(fd, F_GETFL)) & O_NONBLOCK)
346 (void) fcntl(fd, F_SETFL, fflags & ~O_NONBLOCK);
348 /* spawn a thread to monitor events */
349 if (pthread_create(&g_mon_tid, NULL, zed_udev_monitor, g_mon) != 0) {
350 udev_monitor_unref(g_mon);
352 zed_log_msg(LOG_WARNING, "pthread_create failed");
356 zed_log_msg(LOG_INFO, "zed_disk_event_init");
362 zed_disk_event_fini()
364 /* cancel monitor thread at recvmsg() */
365 (void) pthread_cancel(g_mon_tid);
366 (void) pthread_join(g_mon_tid, NULL);
368 /* cleanup udev resources */
369 udev_monitor_unref(g_mon);
372 zed_log_msg(LOG_INFO, "zed_disk_event_fini");
377 #include "zed_disk_event.h"
380 zed_disk_event_init()
386 zed_disk_event_fini()
390 #endif /* HAVE_LIBUDEV */