]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
udev: Move udev_block() and udev_unblock() into udev.c
authorMateusz Grzonka <mateusz.grzonka@intel.com>
Tue, 21 Nov 2023 00:58:24 +0000 (01:58 +0100)
committerJes Sorensen <jes@trained-monkey.org>
Tue, 21 Nov 2023 16:12:06 +0000 (11:12 -0500)
Add kernel style comments and better error handling.

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>
Create.c
lib.c
mdadm.h
mdopen.c
udev.c
udev.h

index a280c7bc76551550a82009f7216b5e37cd66b5b2..ddd1a79bf3934c37703419367fabc93389c61788 100644 (file)
--- a/Create.c
+++ b/Create.c
@@ -23,6 +23,7 @@
  */
 
 #include       "mdadm.h"
+#include       "udev.h"
 #include       "md_u.h"
 #include       "md_p.h"
 #include       <ctype.h>
diff --git a/lib.c b/lib.c
index cf2701cd6a11f190b1ff69cb42abe2ff9ed888da..2b09293cfaded961d73fd318c737163f7138f2b4 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -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.
diff --git a/mdadm.h b/mdadm.h
index 9514cbe5d0fe581c5ea0ae6e9189007bc93f7192..8dcd8b86f35a2dce98626d8a4f6dc3d5db267c89 100644 (file)
--- a/mdadm.h
+++ b/mdadm.h
@@ -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);
 
index f9b04e1cfb0ec6786e3a553ebe9d4900706e3af3..eaa59b5925af40f0d4b73c02bc9380eee5473785 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -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);
        }
 
diff --git a/udev.c b/udev.c
index 2bac6921f204b2c07cf7b0bbe84fe6fddbb406ef..bc4722b0ef94b6f9ef2a8fe47205f2337a49e9bd 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -28,6 +28,8 @@
 #include       <syslog.h>
 #include       <libudev.h>
 
+static char *unblock_path;
+
 /*
  * udev_is_available() - Checks for udev in the system.
  *
@@ -148,3 +150,45 @@ enum udev_status udev_wait_for_events(int seconds)
        return UDEV_STATUS_TIMEOUT;
 }
 #endif
+
+/*
+ * udev_block() - Block udev from examining newly created arrays.
+ *
+ * When array is created, we don't want udev to examine it immediately.
+ * Function creates /run/mdadm/creating-mdXXX and expects that udev rule
+ * will notice it and act accordingly.
+ *
+ * Return:
+ * UDEV_STATUS_SUCCESS when successfully blocked udev
+ * UDEV_STATUS_ERROR on error
+ */
+enum udev_status udev_block(char *devnm)
+{
+       int fd;
+       char *path = xcalloc(1, BUFSIZ);
+
+       snprintf(path, BUFSIZ, "/run/mdadm/creating-%s", devnm);
+
+       fd = open(path, O_CREAT | O_RDWR, 0600);
+       if (!is_fd_valid(fd)) {
+               pr_err("Cannot block udev, error creating blocking file.\n");
+               pr_err("%s: %s\n", strerror(errno), path);
+               free(path);
+               return UDEV_STATUS_ERROR;
+       }
+
+       close(fd);
+       unblock_path = path;
+       return UDEV_STATUS_SUCCESS;
+}
+
+/*
+ * udev_unblock() - Unblock udev.
+ */
+void udev_unblock(void)
+{
+       if (unblock_path)
+               unlink(unblock_path);
+       free(unblock_path);
+       unblock_path = NULL;
+}
diff --git a/udev.h b/udev.h
index 338848611ec32e0a9a301751728e5d5667d49592..ae0a36170f4695c4b4da3565a228c73f00d045f6 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -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