]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
umount: split out MD detaching code from umount.c
authorLennart Poettering <lennart@poettering.net>
Fri, 2 Jun 2023 08:25:19 +0000 (10:25 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 2 Jun 2023 13:56:06 +0000 (15:56 +0200)
umount.c does so much stuff, and MD detaching is relatively separate,
hence split it out into its own .c/.h file pair.

src/shutdown/detach-md.c [new file with mode: 0644]
src/shutdown/detach-md.h [new file with mode: 0644]
src/shutdown/meson.build
src/shutdown/shutdown.c
src/shutdown/umount.c
src/shutdown/umount.h

diff --git a/src/shutdown/detach-md.c b/src/shutdown/detach-md.c
new file mode 100644 (file)
index 0000000..aff20e5
--- /dev/null
@@ -0,0 +1,151 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/***
+  Copyright © 2010 ProFUSION embedded systems
+***/
+
+#include <linux/major.h>
+#include <linux/raid/md_u.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include "sd-device.h"
+
+#include "alloc-util.h"
+#include "blockdev-util.h"
+#include "detach-md.h"
+#include "device-util.h"
+#include "devnum-util.h"
+#include "errno-util.h"
+#include "fd-util.h"
+#include "string-util.h"
+#include "umount.h"
+
+static int md_list_get(MountPoint **head) {
+        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+        sd_device *d;
+        int r;
+
+        assert(head);
+
+        r = sd_device_enumerator_new(&e);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_allow_uninitialized(e);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_add_match_subsystem(e, "block", true);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_add_match_sysname(e, "md*");
+        if (r < 0)
+                return r;
+
+        /* Filter out partitions. */
+        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "disk");
+        if (r < 0)
+                return r;
+
+        FOREACH_DEVICE(e, d) {
+                _cleanup_free_ char *p = NULL;
+                const char *dn, *md_level;
+                MountPoint *m;
+                dev_t devnum;
+
+                if (sd_device_get_devnum(d, &devnum) < 0 ||
+                    sd_device_get_devname(d, &dn) < 0)
+                        continue;
+
+                r = sd_device_get_property_value(d, "MD_LEVEL", &md_level);
+                if (r < 0) {
+                        log_warning_errno(r, "Failed to get MD_LEVEL property for %s, ignoring: %m", dn);
+                        continue;
+                }
+
+                /* MD "containers" are a special type of MD devices, used for external metadata.  Since it
+                 * doesn't provide RAID functionality in itself we don't need to stop it. */
+                if (streq(md_level, "container"))
+                        continue;
+
+                p = strdup(dn);
+                if (!p)
+                        return -ENOMEM;
+
+                m = new(MountPoint, 1);
+                if (!m)
+                        return -ENOMEM;
+
+                *m = (MountPoint) {
+                        .path = TAKE_PTR(p),
+                        .devnum = devnum,
+                };
+
+                LIST_PREPEND(mount_point, *head, m);
+        }
+
+        return 0;
+}
+
+static int delete_md(MountPoint *m) {
+        _cleanup_close_ int fd = -EBADF;
+
+        assert(m);
+        assert(major(m->devnum) != 0);
+        assert(m->path);
+
+        fd = open(m->path, O_RDONLY|O_CLOEXEC|O_EXCL);
+        if (fd < 0)
+                return -errno;
+
+        if (fsync(fd) < 0)
+                log_debug_errno(errno, "Failed to sync MD block device %s, ignoring: %m", m->path);
+
+        return RET_NERRNO(ioctl(fd, STOP_ARRAY, NULL));
+}
+
+static int md_points_list_detach(MountPoint **head, bool *changed, bool last_try) {
+        int n_failed = 0, r;
+        dev_t rootdev = 0;
+
+        assert(head);
+        assert(changed);
+
+        (void) get_block_device("/", &rootdev);
+
+        LIST_FOREACH(mount_point, m, *head) {
+                if (major(rootdev) != 0 && rootdev == m->devnum) {
+                        n_failed ++;
+                        continue;
+                }
+
+                log_info("Stopping MD %s (" DEVNUM_FORMAT_STR ").", m->path, DEVNUM_FORMAT_VAL(m->devnum));
+                r = delete_md(m);
+                if (r < 0) {
+                        log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not stop MD %s: %m", m->path);
+                        n_failed++;
+                        continue;
+                }
+
+                *changed = true;
+                mount_point_free(head, m);
+        }
+
+        return n_failed;
+}
+
+int md_detach_all(bool *changed, bool last_try) {
+        _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, md_list_head);
+        int r;
+
+        assert(changed);
+
+        LIST_HEAD_INIT(md_list_head);
+
+        r = md_list_get(&md_list_head);
+        if (r < 0)
+                return r;
+
+        return md_points_list_detach(&md_list_head, changed, last_try);
+}
diff --git a/src/shutdown/detach-md.h b/src/shutdown/detach-md.h
new file mode 100644 (file)
index 0000000..3784598
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+/***
+  Copyright © 2010 ProFUSION embedded systems
+***/
+
+#include <stdbool.h>
+
+int md_detach_all(bool *changed, bool last_try);
index 1e28528b9712f8c6a0cd646c7aa36737d119a7a7..a41dc772b5ed1e01c9af0a25603fd5a17fce2d03 100644 (file)
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 
 systemd_shutdown_sources = files(
+        'detach-md.c',
         'shutdown.c',
         'umount.c',
 )
index cf0351cf78298161cc4bdc3568482abc07b1143e..44dd765ad610bc85bf6b2d5cd5e723c8c9157439 100644 (file)
@@ -21,8 +21,9 @@
 #include "binfmt-util.h"
 #include "cgroup-setup.h"
 #include "cgroup-util.h"
-#include "coredump-util.h"
 #include "constants.h"
+#include "coredump-util.h"
+#include "detach-md.h"
 #include "errno-util.h"
 #include "exec-util.h"
 #include "fd-util.h"
index 221d29ba6c03ef5e43114ffa9b07eca9b75f246f..17722d710542a11b1c41a420c45424b5689a8827 100644 (file)
@@ -6,8 +6,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/dm-ioctl.h>
-#include <linux/major.h>
-#include <linux/raid/md_u.h>
 #include <linux/loop.h>
 #include <sys/mount.h>
 #include <sys/swap.h>
@@ -50,7 +48,7 @@
 #include "umount.h"
 #include "virt.h"
 
-static void mount_point_free(MountPoint **head, MountPoint *m) {
+void mount_point_free(MountPoint **head, MountPoint *m) {
         assert(head);
         assert(m);
 
@@ -343,74 +341,6 @@ static int dm_list_get(MountPoint **head) {
         return 0;
 }
 
-static int md_list_get(MountPoint **head) {
-        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
-        sd_device *d;
-        int r;
-
-        assert(head);
-
-        r = sd_device_enumerator_new(&e);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_allow_uninitialized(e);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_add_match_subsystem(e, "block", true);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_add_match_sysname(e, "md*");
-        if (r < 0)
-                return r;
-
-        /* Filter out partitions. */
-        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "disk");
-        if (r < 0)
-                return r;
-
-        FOREACH_DEVICE(e, d) {
-                _cleanup_free_ char *p = NULL;
-                const char *dn, *md_level;
-                MountPoint *m;
-                dev_t devnum;
-
-                if (sd_device_get_devnum(d, &devnum) < 0 ||
-                    sd_device_get_devname(d, &dn) < 0)
-                        continue;
-
-                r = sd_device_get_property_value(d, "MD_LEVEL", &md_level);
-                if (r < 0) {
-                        log_warning_errno(r, "Failed to get MD_LEVEL property for %s, ignoring: %m", dn);
-                        continue;
-                }
-
-                /* MD "containers" are a special type of MD devices, used for external metadata.  Since it
-                 * doesn't provide RAID functionality in itself we don't need to stop it. */
-                if (streq(md_level, "container"))
-                        continue;
-
-                p = strdup(dn);
-                if (!p)
-                        return -ENOMEM;
-
-                m = new(MountPoint, 1);
-                if (!m)
-                        return -ENOMEM;
-
-                *m = (MountPoint) {
-                        .path = TAKE_PTR(p),
-                        .devnum = devnum,
-                };
-
-                LIST_PREPEND(mount_point, *head, m);
-        }
-
-        return 0;
-}
-
 static int delete_loopback(const char *device) {
         _cleanup_close_ int fd = -EBADF;
         struct loop_info64 info;
@@ -513,23 +443,6 @@ static int delete_dm(MountPoint *m) {
         }));
 }
 
-static int delete_md(MountPoint *m) {
-        _cleanup_close_ int fd = -EBADF;
-
-        assert(m);
-        assert(major(m->devnum) != 0);
-        assert(m->path);
-
-        fd = open(m->path, O_RDONLY|O_CLOEXEC|O_EXCL);
-        if (fd < 0)
-                return -errno;
-
-        if (fsync(fd) < 0)
-                log_debug_errno(errno, "Failed to sync MD block device %s, ignoring: %m", m->path);
-
-        return RET_NERRNO(ioctl(fd, STOP_ARRAY, NULL));
-}
-
 static bool nonunmountable_path(const char *path) {
         return path_equal(path, "/")
 #if ! HAVE_SPLIT_USR
@@ -906,36 +819,6 @@ static int dm_points_list_detach(MountPoint **head, bool *changed, bool last_try
         return n_failed;
 }
 
-static int md_points_list_detach(MountPoint **head, bool *changed, bool last_try) {
-        int n_failed = 0, r;
-        dev_t rootdev = 0;
-
-        assert(head);
-        assert(changed);
-
-        (void) get_block_device("/", &rootdev);
-
-        LIST_FOREACH(mount_point, m, *head) {
-                if (major(rootdev) != 0 && rootdev == m->devnum) {
-                        n_failed ++;
-                        continue;
-                }
-
-                log_info("Stopping MD %s (" DEVNUM_FORMAT_STR ").", m->path, DEVNUM_FORMAT_VAL(m->devnum));
-                r = delete_md(m);
-                if (r < 0) {
-                        log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not stop MD %s: %m", m->path);
-                        n_failed++;
-                        continue;
-                }
-
-                *changed = true;
-                mount_point_free(head, m);
-        }
-
-        return n_failed;
-}
-
 static int umount_all_once(bool *changed, bool last_try) {
         _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
         int r;
@@ -1013,18 +896,3 @@ int dm_detach_all(bool *changed, bool last_try) {
 
         return dm_points_list_detach(&dm_list_head, changed, last_try);
 }
-
-int md_detach_all(bool *changed, bool last_try) {
-        _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, md_list_head);
-        int r;
-
-        assert(changed);
-
-        LIST_HEAD_INIT(md_list_head);
-
-        r = md_list_get(&md_list_head);
-        if (r < 0)
-                return r;
-
-        return md_points_list_detach(&md_list_head, changed, last_try);
-}
index a742ac0af55548009b4a8d6eaff044111b18d6ff..0b25a45f5b86ad7ef710560cb009ffb28c2357d5 100644 (file)
@@ -11,7 +11,6 @@ int umount_all(bool *changed, bool last_try);
 int swapoff_all(bool *changed);
 int loopback_detach_all(bool *changed, bool last_try);
 int dm_detach_all(bool *changed, bool last_try);
-int md_detach_all(bool *changed, bool last_try);
 
 /* This is exported just for testing */
 typedef struct MountPoint {
@@ -26,5 +25,6 @@ typedef struct MountPoint {
 } MountPoint;
 
 int mount_points_list_get(const char *mountinfo, MountPoint **head);
+void mount_point_free(MountPoint **head, MountPoint *m);
 void mount_points_list_free(MountPoint **head);
 int swap_list_get(const char *swaps, MountPoint **head);