Mdmonitor refactor and udev event handling improvements
v5 cover Mdmonitor refactor and udev event handling improvements
Along the way we observed many problems with current approach to event handling in mdmonitor. It frequently doesn't report Fail and DeviceDisappeared events.
It's due to time races with udev, and too long delay in some cases.
While there was a patch intending to address time races with udev, it didn't remove them completely. This patch series presents alternative approach, where mdmonitor wakes up on udev events, so that there should be no more conflicts with udev we saw before.
Additionally some code quality improvements were done, to make the code more maintainable.
v2:
Fixed mismatched comment style and rebased onto master.
v3:
Resend to cleanup on patchwork.
v4:
Fix building errors. Only two last patches out of 8 didn't apply, so I'm sending just them.
v5:
Patches didn't apply. Send updated version.
Mateusz Grzonka (2):
Mdmonitor: Improve udev event handling
udev: Move udev_block() and udev_unblock() into udev.c
Create.c | 1 + Makefile | 18 ++--- Manage.c | 3 +- Monitor.c | 137 ++++++++++++++------------------------ lib.c | 42 ------------ mdadm.h | 3 +- mdopen.c | 19 +++--- udev.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ udev.h | 40 +++++++++++ 9 files changed, 307 insertions(+), 150 deletions(-) create mode 100644 udev.c create mode 100644 udev.h
Signature · 1 line
2.26.2
PATCH v5 1/2 Mdmonitor: Improve udev event handling
From: Mateusz Grzonka <[email protected]>
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, 253 insertions(+), 113 deletions(-) create mode 100644 udev.c create mode 100644 udev.h
Makefile +9 -9
@@ -163,14 +163,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
@@ -179,7 +179,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)
@@ -462,7 +463,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
@@ -539,19 +539,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 +1 -0
@@ -1656,6 +1656,7 @@ extern char *conf_line(FILE *file);extern char *conf_word (FILE *file,int allow_key);extern void print_quoted (char *str);extern int use_udev (void ); +extern void print_escape (char *str);extern unsigned long GCD (unsigned long a,unsigned long b);extern int conf_name_is_free (char *name);extern bool is_devname_ignore (const 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
PATCH v5 2/2 udev: Move udev_block() and udev_unblock() into udev.c
In reply to Kinga TanskaFrom: Mateusz Grzonka <[email protected]>
Add kernel style comments and better error handling.
--- Create.c | 1 + lib.c | 29 ----------------------------- mdadm.h | 2 -- mdopen.c | 12 ++++++------ udev.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ udev.h | 3 +++ 6 files changed, 54 insertions(+), 37 deletions(-)
Create.c +1 -0
@@ -23,6 +23,7 @@*/ #include "mdadm.h" +#include "udev.h" #include "md_u.h" #include "md_p.h" #include <ctype.h>
lib.c +0 -29
@@ -204,35 +204,6 @@ char *fd2devnm(int fd)return NULL ; } -/* When we create a new array, we don't want the content to -* be immediately examined by udev - it is probably meaningless. -* So create /run/mdadm/creating-mdXXX and expect that a udev -* rule will noticed this and act accordingly. -*/ -static char block_path[] ="/run/mdadm/creating-%s" ; -static char *unblock_path =NULL ; -void udev_block (char *devnm) -{ -int fd; -char *path =NULL ; - -xasprintf (&path, block_path, devnm); - fd =open (path,O_CREAT |O_RDWR ,0600 ); -if (fd >=0 ) { -close (fd); - unblock_path = path; - }else -free (path); -} - -void udev_unblock (void ) -{ -if (unblock_path) -unlink (unblock_path); -free (unblock_path); - unblock_path =NULL ; -} -/* * convert a major/minor pair for a block device into a name in /dev, if possible. * On the first call, walk /dev collecting name.
mdadm.h +0 -2
@@ -1765,8 +1765,6 @@ extern char *fd2kname(int fd);extern char *stat2devnm (struct stat *st);bool stat_is_md_dev (struct stat *st);extern char *fd2devnm (int fd); -extern void udev_block (char *devnm); -extern void udev_unblock (void );extern int in_initrd (void );
mdopen.c +6 -6
@@ -336,8 +336,8 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy, devnm[0 ] =0 ;if (num <0 && cname && ci->names) {sprintf (devnm,"md_%s" , cname); -if (block_udev) -udev_block (devnm); +if (block_udev &&udev_block (devnm) !=UDEV_STATUS_SUCCESS ) +return -1 ;if (!create_named_array (devnm)) { devnm[0 ] =0 ;udev_unblock ();
@@ -345,8 +345,8 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy, }if (num >=0 ) {sprintf (devnm,"md%d" , num); -if (block_udev) -udev_block (devnm); +if (block_udev &&udev_block (devnm) !=UDEV_STATUS_SUCCESS ) +return -1 ;if (!create_named_array (devnm)) { devnm[0 ] =0 ;udev_unblock ();
@@ -369,8 +369,8 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,return -1 ; } } -if (block_udev) -udev_block (devnm); +if (block_udev &&udev_block (devnm) !=UDEV_STATUS_SUCCESS ) +return -1 ;create_named_array (devnm); }
udev.c +44 -0
53 lines not on this page · raw message
udev.h +3 -0
@@ -34,4 +34,7 @@ bool udev_is_available(void);enum udev_status udev_wait_for_events (int seconds);#endif +enum udev_status udev_block (char *devnm); +void udev_unblock (void ); +#endif
Signature · 1 line
2.26.2
Jes Sorensen
AppliedQuoted 18 lines · Kinga Tanska
Along the way we observed many problems with current approach to event handling in mdmonitor. It frequently doesn't report Fail and DeviceDisappeared events.
It's due to time races with udev, and too long delay in some cases.
While there was a patch intending to address time races with udev, it didn't remove them completely. This patch series presents alternative approach, where mdmonitor wakes up on udev events, so that there should be no more conflicts with udev we saw before.Additionally some code quality improvements were done, to make the code more maintainable.
v2:
Fixed mismatched comment style and rebased onto master.v3:
Resend to cleanup on patchwork.v4:
Fix building errors. Only two last patches out of 8 didn't apply, so I'm sending just them.
Quoted 6 lines
v5:
Patches didn't apply. Send updated version.Mateusz Grzonka (2):
Mdmonitor: Improve udev event handling
udev: Moveudev_block()andudev_unblock()intoudev.c
Applied!
Thanks,
Jes
Quoted 13 lines
Create.c | 1 + Makefile | 18 ++--- Manage.c | 3 +- Monitor.c | 137 ++++++++++++++------------------------ lib.c | 42 ------------ mdadm.h | 3 +- mdopen.c | 19 +++--- udev.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ udev.h | 40 +++++++++++ 9 files changed, 307 insertions(+), 150 deletions(-) create mode 100644 udev.c create mode 100644 udev.h