diff options
author | Mateusz Grzonka <mateusz.grzonka@intel.com> | 2023-11-21 01:58:23 +0100 |
---|---|---|
committer | Jes Sorensen <jes@trained-monkey.org> | 2023-11-21 17:12:06 +0100 |
commit | 9935cf0f64f3f1e70e7840385e9838c30487dc64 (patch) | |
tree | 6c61983631ad6efd404cbe1cfbf1c317c1bf66a3 /udev.c | |
parent | Fix assembling RAID volume by using incremental (diff) | |
download | mdadm-9935cf0f64f3f1e70e7840385e9838c30487dc64.tar.xz mdadm-9935cf0f64f3f1e70e7840385e9838c30487dc64.zip |
Mdmonitor: Improve udev event handling
Mdmonitor is waiting for udev queue to become empty.
Even if the queue becomes empty, udev might still be processing last event.
However we want to wait and wake up mdmonitor when udev finished
processing events..
Also, the udev queue interface is considered legacy and should not be
used outside of udev.
Use udev monitor instead, and wake up mdmonitor on every event triggered
by udev for md block device.
We need to generate more change events from kernel, because they are
missing in some situations, for example, when rebuild started.
This will be addressed in a separate patch.
Move udev specific code into separate functions, and place them in udev.c file.
Also move use_udev() logic from lib.c into newly created file.
Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com>
Signed-off-by: Kinga Tanska <kinga.tanska@intel.com>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Diffstat (limited to 'udev.c')
-rw-r--r-- | udev.c | 150 |
1 files changed, 150 insertions, 0 deletions
@@ -0,0 +1,150 @@ +/* + * mdadm - manage Linux "md" devices aka RAID arrays. + * + * Copyright (C) 2022 Mateusz Grzonka <mateusz.grzonka@intel.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "mdadm.h" +#include "udev.h" +#include "md_p.h" +#include "md_u.h" +#include <sys/wait.h> +#include <signal.h> +#include <limits.h> +#include <syslog.h> +#include <libudev.h> + +/* + * udev_is_available() - Checks for udev in the system. + * + * Function looks whether udev directories are available and MDADM_NO_UDEV env defined. + * + * Return: + * true if udev is available, + * false if not + */ +bool udev_is_available(void) +{ + struct stat stb; + + if (stat("/dev/.udev", &stb) != 0 && + stat("/run/udev", &stb) != 0) + return false; + if (check_env("MDADM_NO_UDEV") == 1) + return false; + return true; +} + +#ifndef NO_LIBUDEV + +static struct udev *udev; +static struct udev_monitor *udev_monitor; + +/* + * udev_release() - Drops references of udev and udev_monitor. + */ +static void udev_release(void) +{ + udev_monitor_unref(udev_monitor); + udev_unref(udev); +} + +/* + * udev_initialize() - Initializes udev and udev_monitor structures. + * + * Function initializes udev, udev_monitor, and sets udev_monitor filter for block devices. + * + * Return: + * UDEV_STATUS_SUCCESS on success + * UDEV_STATUS_ERROR on error + * UDEV_STATUS_ERROR_NO_UDEV when udev not available + */ +static enum udev_status udev_initialize(void) +{ + if (!udev_is_available()) { + pr_err("No udev.\n"); + return UDEV_STATUS_ERROR_NO_UDEV; + } + + udev = udev_new(); + if (!udev) { + pr_err("Cannot initialize udev.\n"); + return UDEV_STATUS_ERROR; + } + + udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); + if (!udev_monitor) { + pr_err("Cannot initialize udev monitor.\n"); + udev = udev_unref(udev); + return UDEV_STATUS_ERROR; + } + + if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "block", 0) < 0) { + pr_err("Cannot add udev monitor event filter for md devices.\n"); + udev_release(); + return UDEV_STATUS_ERROR; + } + if (udev_monitor_enable_receiving(udev_monitor) < 0) { + pr_err("Cannot enable receiving udev events through udev monitor.\n"); + udev_release(); + return UDEV_STATUS_ERROR; + } + atexit(udev_release); + return UDEV_STATUS_SUCCESS; +} + +/* + * udev_wait_for_events() - Waits for events from udev. + * @seconds: Timeout in seconds. + * + * Function waits udev events, wakes up on event or timeout. + * + * Return: + * UDEV_STATUS_SUCCESS on detected event + * UDEV_STATUS_TIMEOUT on timeout + * UDEV_STATUS_ERROR on error + */ +enum udev_status udev_wait_for_events(int seconds) +{ + int fd; + fd_set readfds; + struct timeval tv; + int ret; + + if (!udev || !udev_monitor) { + ret = udev_initialize(); + if (ret != UDEV_STATUS_SUCCESS) + return ret; + } + + fd = udev_monitor_get_fd(udev_monitor); + if (fd < 0) { + pr_err("Cannot access file descriptor associated with udev monitor.\n"); + return UDEV_STATUS_ERROR; + } + + FD_ZERO(&readfds); + FD_SET(fd, &readfds); + tv.tv_sec = seconds; + tv.tv_usec = 0; + + if (select(fd + 1, &readfds, NULL, NULL, &tv) > 0 && FD_ISSET(fd, &readfds)) + if (udev_monitor_receive_device(udev_monitor)) + return UDEV_STATUS_SUCCESS; /* event detected */ + return UDEV_STATUS_TIMEOUT; +} +#endif |