Mdmonitor: Improve udev event handling
PATCH v4 1/2 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.
--- Makefile | 18 +++---- Manage.c | 3 +- Monitor.c | 137 ++++++++++++++++++------------------------------- lib.c | 13 ----- mdadm.h | 1 - mdopen.c | 7 +-- udev.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ udev.h | 37 ++++++++++++++ 8 files changed, 252 insertions(+), 114 deletions(-) create mode 100644 udev.c create mode 100644 udev.h
Makefile +9 -9
@@ -145,14 +145,14 @@ else ECHO=: endif -OBJS = mdadm.o config.o policy.o mdstat.o ReadMe.o uuid.o util.o maps.o lib.o \ - Manage.o Assemble.o Build.o \ - Create.o Detail.o Examine.o Grow.o Monitor.o dlink.o Kill.o Query.o \ - Incremental.o Dump.o \ - mdopen.o super0.o super1.o super-ddf.o super-intel.o bitmap.o \ - super-mbr.o super-gpt.o \ - restripe.o sysfs.o sha1.o mapfile.o crc32.o sg_io.o msg.o xmalloc.o \ - platform-intel.o probe_roms.o crc32c.o +OBJS = mdadm.o config.o policy.o mdstat.o ReadMe.o uuid.o util.o maps.o lib.o udev.o \ + Manage.o Assemble.o Build.o \ + Create.o Detail.o Examine.o Grow.o Monitor.o dlink.o Kill.o Query.o \ + Incremental.o Dump.o \ + mdopen.o super0.o super1.o super-ddf.o super-intel.o bitmap.o \ + super-mbr.o super-gpt.o \ + restripe.o sysfs.o sha1.o mapfile.o crc32.o sg_io.o msg.o xmalloc.o \ + platform-intel.o probe_roms.o crc32c.o CHECK_OBJS = restripe.o uuid.o sysfs.o maps.o lib.o xmalloc.o dlink.o
@@ -161,7 +161,7 @@ SRCS = $(patsubst %.o,%.c,$(OBJS)) INCL = mdadm.h part.h bitmap.h MON_OBJS = mdmon.o monitor.o managemon.o uuid.o util.o maps.o mdstat.o sysfs.o config.o mapfile.o mdopen.o\ - policy.o lib.o \ + policy.o lib.o udev.o \ Kill.o sg_io.o dlink.o ReadMe.o super-intel.o \ super-mbr.o super-gpt.o \ super-ddf.o sha1.o crc32.o msg.o bitmap.o xmalloc.o \
Manage.c +2 -1
@@ -25,6 +25,7 @@#include "mdadm.h" #include "md_u.h" #include "md_p.h" +#include "udev.h" #include <ctype.h> int Manage_ro (char *devname,int fd,int readonly)
@@ -461,7 +462,7 @@ done: goto out; } -if (devnm[0 ] &&use_udev ()) { +if (devnm[0 ] &&udev_is_available ()) {struct map_ent *mp =map_by_devnm (&map, devnm);remove_devices (devnm, mp ? mp->path :NULL ); }
Monitor.c +50 -87
182 lines not on this page · raw message
lib.c +0 -13
@@ -495,19 +495,6 @@ int check_env(char *name)return 0 ; } -int use_udev (void ) -{ -static int use =-1 ; -struct stat stb; - -if (use <0 ) { - use = ((stat ("/dev/.udev" , &stb) ==0 || -stat ("/run/udev" , &stb) ==0 ) && -check_env ("MDADM_NO_UDEV" ) ==0 ); - } -return use; -} -unsigned long GCD (unsigned long a,unsigned long b) {while (a != b) {
mdadm.h +0 -1
@@ -1647,7 +1647,6 @@ extern char *conf_line(FILE *file);extern char *conf_word (FILE *file,int allow_key);extern void print_quoted (char *str);extern void print_escape (char *str); -extern int use_udev (void );extern unsigned long GCD (unsigned long a,unsigned long b);extern int conf_name_is_free (char *name);extern bool is_devname_ignore (char *devname);
mdopen.c +4 -3
@@ -23,6 +23,7 @@*/ #include "mdadm.h" +#include "udev.h" #include "md_p.h" #include <ctype.h>
@@ -176,7 +177,7 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,char devnm[32 ];char cbuf[400 ]; -if (!use_udev ()) +if (!udev_is_available ()) block_udev =0 ;if (chosen ==NULL )
@@ -384,7 +385,7 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,* If we cannot detect udev, we need to make * devices and links ourselves. */ -if (!use_udev ()) { +if (!udev_is_available ()) {/* Make sure 'devname' exists and 'chosen' is a symlink to it */ if (lstat (devname, &stb) ==0 ) {/* Must be the correct device, else error */
@@ -508,7 +509,7 @@ char *find_free_devnm(int use_partitions)continue ;if (!conf_name_is_free (devnm))continue ; -if (!use_udev ()) { +if (!udev_is_available ()) {/* make sure it is new to /dev too, at least as a * non-standard */ dev_t devid =devnm2devid (devnm);
udev.c new file +150 -0
150 lines not on this page · raw message
udev.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* mdadm - manage Linux "md" devices aka RAID arrays. +* +* Copyright (C) 2022 Mateusz Grzonka <[email protected]> +* +* 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 +*/ + +#ifndef MONITOR_UDEV_H +#define MONITOR_UDEV_H + +enum udev_status { +UDEV_STATUS_ERROR_NO_UDEV =-2 , +UDEV_STATUS_ERROR , +UDEV_STATUS_SUCCESS =0 , +UDEV_STATUS_TIMEOUT +}; + +bool udev_is_available (void ); + +#ifndef NO_LIBUDEV +enum udev_status udev_wait_for_events (int seconds); +#endif + +#endif
Signature · 1 line
2.26.2
Quoted 11 lines · Mateusz Grzonka
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.
Quoted 8 lines
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.cfile. Also moveuse_udev()logic fromlib.cinto newly created file.Signed-off-by Mateusz Grzonka <[email protected]>
Generally looks good to me.
One question, is there a minimum version of udev required for this to work or has it been around long enough that we don't need to worry?
Thanks,
Jes
Quoted 21 lines · On Fri, 1 Sep 2023 11:35:57 -0400 Jes Sorensen <[email protected]> wrote:
Quoted 11 lines · Mateusz Grzonka
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.
Quoted 8 lines
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.cfile. Also moveuse_udev()logic fromlib.cinto newly created file.Signed-off-by Mateusz Grzonka <[email protected]>
Quoted 9 lines
Generally looks good to me.
One question, is there a minimum version of udev required for this to work or has it been around long enough that we don't need to worry?
Thanks,
Jes
Hi Jes,
We don't need to worry. I checked one random commit (25e773e) from 2014 and udev_monitor stuff is there.
Thanks,
Mariusz
Quoted 29 lines · Mariusz Tkaczyk
Quoted 26 lines · On Fri, 1 Sep 2023 11:35:57 -0400 Jes Sorensen <[email protected]> wrote:
Quoted 11 lines · Mateusz Grzonka
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.
Quoted 8 lines
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.cfile. Also moveuse_udev()logic fromlib.cinto newly created file.Signed-off-by Mateusz Grzonka <[email protected]>Generally looks good to me.
One question, is there a minimum version of udev required for this to work or has it been around long enough that we don't need to worry?
Quoted 8 lines
Quoted 4 lines
Thanks,
JesHi Jes,
We don't need to worry. I checked one random commit (25e773e) from 2014 andudev_monitorstuff is there.
Unfortunately this set no longer applies. If you want to send an updated version I'll do my best to look at it quickly.
Thanks,
Jes