summaryrefslogtreecommitdiffstats
path: root/src/shutdown
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-06-02 10:36:39 +0200
committerLennart Poettering <lennart@poettering.net>2023-06-02 15:56:06 +0200
commit2e2c472b1c424e8ae15d4da5a754e1f3b8182a00 (patch)
tree0a217ae0a1e3319cc9ac6c4a16f98aa2994b1842 /src/shutdown
parentumount: similar as previous commit, split out DM detaching (diff)
downloadsystemd-2e2c472b1c424e8ae15d4da5a754e1f3b8182a00.tar.xz
systemd-2e2c472b1c424e8ae15d4da5a754e1f3b8182a00.zip
umount: split out loopback detach code
Like the similar commits, no actual code changes, just splitting up large C files.
Diffstat (limited to 'src/shutdown')
-rw-r--r--src/shutdown/detach-loopback.c198
-rw-r--r--src/shutdown/detach-loopback.h10
-rw-r--r--src/shutdown/meson.build1
-rw-r--r--src/shutdown/shutdown.c1
-rw-r--r--src/shutdown/umount.c197
-rw-r--r--src/shutdown/umount.h1
6 files changed, 211 insertions, 197 deletions
diff --git a/src/shutdown/detach-loopback.c b/src/shutdown/detach-loopback.c
new file mode 100644
index 0000000000..49150077d6
--- /dev/null
+++ b/src/shutdown/detach-loopback.c
@@ -0,0 +1,198 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/***
+ Copyright © 2010 ProFUSION embedded systems
+***/
+
+#include <linux/loop.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
+#include "sd-device.h"
+
+#include "alloc-util.h"
+#include "blockdev-util.h"
+#include "detach-loopback.h"
+#include "device-util.h"
+#include "fd-util.h"
+#include "umount.h"
+
+static int loopback_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, "loop*");
+ if (r < 0)
+ return r;
+
+ r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true);
+ if (r < 0)
+ return r;
+
+ FOREACH_DEVICE(e, d) {
+ _cleanup_free_ char *p = NULL;
+ const char *dn;
+ MountPoint *lb;
+ dev_t devnum;
+
+ if (sd_device_get_devnum(d, &devnum) < 0 ||
+ sd_device_get_devname(d, &dn) < 0)
+ continue;
+
+ p = strdup(dn);
+ if (!p)
+ return -ENOMEM;
+
+ lb = new(MountPoint, 1);
+ if (!lb)
+ return -ENOMEM;
+
+ *lb = (MountPoint) {
+ .path = TAKE_PTR(p),
+ .devnum = devnum,
+ };
+
+ LIST_PREPEND(mount_point, *head, lb);
+ }
+
+ return 0;
+}
+
+static int delete_loopback(const char *device) {
+ _cleanup_close_ int fd = -EBADF;
+ struct loop_info64 info;
+
+ assert(device);
+
+ fd = open(device, O_RDONLY|O_CLOEXEC);
+ if (fd < 0) {
+ log_debug_errno(errno, "Failed to open loopback device %s: %m", device);
+ return errno == ENOENT ? 0 : -errno;
+ }
+
+ /* Loopback block devices don't sync in-flight blocks when we clear the fd, hence sync explicitly
+ * first */
+ if (fsync(fd) < 0)
+ log_debug_errno(errno, "Failed to sync loop block device %s, ignoring: %m", device);
+
+ if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
+ if (errno == ENXIO) /* Nothing bound, didn't do anything */
+ return 0;
+
+ if (errno != EBUSY)
+ return log_debug_errno(errno, "Failed to clear loopback device %s: %m", device);
+
+ if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
+ if (errno == ENXIO) /* What? Suddenly detached after all? That's fine by us then. */
+ return 1;
+
+ log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device);
+ return -EBUSY; /* propagate original error */
+ }
+
+#if HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
+#endif
+
+ if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR)) /* someone else already set LO_FLAGS_AUTOCLEAR for us? fine by us */
+ return -EBUSY; /* propagate original error */
+
+ info.lo_flags |= LO_FLAGS_AUTOCLEAR;
+ if (ioctl(fd, LOOP_SET_STATUS64, &info) < 0) {
+ if (errno == ENXIO) /* Suddenly detached after all? Fine by us */
+ return 1;
+
+ log_debug_errno(errno, "Failed to set LO_FLAGS_AUTOCLEAR flag for loop device %s, ignoring: %m", device);
+ } else
+ log_debug("Successfully set LO_FLAGS_AUTOCLEAR flag for loop device %s.", device);
+
+ return -EBUSY;
+ }
+
+ if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
+ /* If the LOOP_CLR_FD above succeeded we'll see ENXIO here. */
+ if (errno == ENXIO)
+ log_debug("Successfully detached loopback device %s.", device);
+ else
+ log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device); /* the LOOP_CLR_FD at least worked, let's hope for the best */
+
+ return 1;
+ }
+
+#if HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
+#endif
+
+ /* Linux makes LOOP_CLR_FD succeed whenever LO_FLAGS_AUTOCLEAR is set without actually doing
+ * anything. Very confusing. Let's hence not claim we did anything in this case. */
+ if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR))
+ log_debug("Successfully called LOOP_CLR_FD on a loopback device %s with autoclear set, which is a NOP.", device);
+ else
+ log_debug("Weird, LOOP_CLR_FD succeeded but the device is still attached on %s.", device);
+
+ return -EBUSY; /* Nothing changed, the device is still attached, hence it apparently is still busy */
+}
+
+static int loopback_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("Detaching loopback %s.", m->path);
+ r = delete_loopback(m->path);
+ if (r < 0) {
+ log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not detach loopback %s: %m", m->path);
+ n_failed++;
+ continue;
+ }
+ if (r > 0)
+ *changed = true;
+
+ mount_point_free(head, m);
+ }
+
+ return n_failed;
+}
+
+int loopback_detach_all(bool *changed, bool last_try) {
+ _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, loopback_list_head);
+ int r;
+
+ assert(changed);
+
+ LIST_HEAD_INIT(loopback_list_head);
+
+ r = loopback_list_get(&loopback_list_head);
+ if (r < 0)
+ return r;
+
+ return loopback_points_list_detach(&loopback_list_head, changed, last_try);
+}
diff --git a/src/shutdown/detach-loopback.h b/src/shutdown/detach-loopback.h
new file mode 100644
index 0000000000..d6d73f31af
--- /dev/null
+++ b/src/shutdown/detach-loopback.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+/***
+ Copyright © 2010 ProFUSION embedded systems
+***/
+
+#include <stdbool.h>
+
+int loopback_detach_all(bool *changed, bool last_try);
diff --git a/src/shutdown/meson.build b/src/shutdown/meson.build
index 1e30f57cbb..fbf0f09795 100644
--- a/src/shutdown/meson.build
+++ b/src/shutdown/meson.build
@@ -2,6 +2,7 @@
systemd_shutdown_sources = files(
'detach-dm.c',
+ 'detach-loopback.c',
'detach-md.c',
'shutdown.c',
'umount.c',
diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c
index 6fa0107a5d..30b4bdd65a 100644
--- a/src/shutdown/shutdown.c
+++ b/src/shutdown/shutdown.c
@@ -24,6 +24,7 @@
#include "constants.h"
#include "coredump-util.h"
#include "detach-dm.h"
+#include "detach-loopback.h"
#include "detach-md.h"
#include "errno-util.h"
#include "exec-util.h"
diff --git a/src/shutdown/umount.c b/src/shutdown/umount.c
index 85f0eb9fe6..8a59efd385 100644
--- a/src/shutdown/umount.c
+++ b/src/shutdown/umount.c
@@ -5,27 +5,15 @@
#include <errno.h>
#include <fcntl.h>
-#include <linux/loop.h>
#include <sys/mount.h>
-#include <sys/swap.h>
#include <sys/stat.h>
+#include <sys/swap.h>
#include <sys/types.h>
#include <unistd.h>
-#if HAVE_VALGRIND_MEMCHECK_H
-#include <valgrind/memcheck.h>
-#endif
-
-#include "sd-device.h"
-
#include "alloc-util.h"
-#include "blockdev-util.h"
#include "chase.h"
-#include "constants.h"
-#include "device-util.h"
-#include "devnum-util.h"
#include "dirent-util.h"
-#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
@@ -36,14 +24,9 @@
#include "mount-util.h"
#include "mountpoint-util.h"
#include "parse-util.h"
-#include "path-util.h"
#include "process-util.h"
#include "random-util.h"
#include "signal-util.h"
-#include "stat-util.h"
-#include "string-util.h"
-#include "strv.h"
-#include "sync-util.h"
#include "umount.h"
#include "virt.h"
@@ -232,138 +215,6 @@ int swap_list_get(const char *swaps, MountPoint **head) {
return 0;
}
-static int loopback_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, "loop*");
- if (r < 0)
- return r;
-
- r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true);
- if (r < 0)
- return r;
-
- FOREACH_DEVICE(e, d) {
- _cleanup_free_ char *p = NULL;
- const char *dn;
- MountPoint *lb;
- dev_t devnum;
-
- if (sd_device_get_devnum(d, &devnum) < 0 ||
- sd_device_get_devname(d, &dn) < 0)
- continue;
-
- p = strdup(dn);
- if (!p)
- return -ENOMEM;
-
- lb = new(MountPoint, 1);
- if (!lb)
- return -ENOMEM;
-
- *lb = (MountPoint) {
- .path = TAKE_PTR(p),
- .devnum = devnum,
- };
-
- LIST_PREPEND(mount_point, *head, lb);
- }
-
- return 0;
-}
-
-
-static int delete_loopback(const char *device) {
- _cleanup_close_ int fd = -EBADF;
- struct loop_info64 info;
-
- assert(device);
-
- fd = open(device, O_RDONLY|O_CLOEXEC);
- if (fd < 0) {
- log_debug_errno(errno, "Failed to open loopback device %s: %m", device);
- return errno == ENOENT ? 0 : -errno;
- }
-
- /* Loopback block devices don't sync in-flight blocks when we clear the fd, hence sync explicitly
- * first */
- if (fsync(fd) < 0)
- log_debug_errno(errno, "Failed to sync loop block device %s, ignoring: %m", device);
-
- if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
- if (errno == ENXIO) /* Nothing bound, didn't do anything */
- return 0;
-
- if (errno != EBUSY)
- return log_debug_errno(errno, "Failed to clear loopback device %s: %m", device);
-
- if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
- if (errno == ENXIO) /* What? Suddenly detached after all? That's fine by us then. */
- return 1;
-
- log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device);
- return -EBUSY; /* propagate original error */
- }
-
-#if HAVE_VALGRIND_MEMCHECK_H
- VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
-#endif
-
- if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR)) /* someone else already set LO_FLAGS_AUTOCLEAR for us? fine by us */
- return -EBUSY; /* propagate original error */
-
- info.lo_flags |= LO_FLAGS_AUTOCLEAR;
- if (ioctl(fd, LOOP_SET_STATUS64, &info) < 0) {
- if (errno == ENXIO) /* Suddenly detached after all? Fine by us */
- return 1;
-
- log_debug_errno(errno, "Failed to set LO_FLAGS_AUTOCLEAR flag for loop device %s, ignoring: %m", device);
- } else
- log_debug("Successfully set LO_FLAGS_AUTOCLEAR flag for loop device %s.", device);
-
- return -EBUSY;
- }
-
- if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
- /* If the LOOP_CLR_FD above succeeded we'll see ENXIO here. */
- if (errno == ENXIO)
- log_debug("Successfully detached loopback device %s.", device);
- else
- log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device); /* the LOOP_CLR_FD at least worked, let's hope for the best */
-
- return 1;
- }
-
-#if HAVE_VALGRIND_MEMCHECK_H
- VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
-#endif
-
- /* Linux makes LOOP_CLR_FD succeed whenever LO_FLAGS_AUTOCLEAR is set without actually doing
- * anything. Very confusing. Let's hence not claim we did anything in this case. */
- if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR))
- log_debug("Successfully called LOOP_CLR_FD on a loopback device %s with autoclear set, which is a NOP.", device);
- else
- log_debug("Weird, LOOP_CLR_FD succeeded but the device is still attached on %s.", device);
-
- return -EBUSY; /* Nothing changed, the device is still attached, hence it apparently is still busy */
-}
-
static bool nonunmountable_path(const char *path) {
return path_equal(path, "/")
#if ! HAVE_SPLIT_USR
@@ -679,37 +530,6 @@ static int swap_points_list_off(MountPoint **head, bool *changed) {
return n_failed;
}
-static int loopback_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("Detaching loopback %s.", m->path);
- r = delete_loopback(m->path);
- if (r < 0) {
- log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not detach loopback %s: %m", m->path);
- n_failed++;
- continue;
- }
- if (r > 0)
- *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;
@@ -757,18 +577,3 @@ int swapoff_all(bool *changed) {
return swap_points_list_off(&swap_list_head, changed);
}
-
-int loopback_detach_all(bool *changed, bool last_try) {
- _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, loopback_list_head);
- int r;
-
- assert(changed);
-
- LIST_HEAD_INIT(loopback_list_head);
-
- r = loopback_list_get(&loopback_list_head);
- if (r < 0)
- return r;
-
- return loopback_points_list_detach(&loopback_list_head, changed, last_try);
-}
diff --git a/src/shutdown/umount.h b/src/shutdown/umount.h
index 7a849ca8a6..b3a397bddf 100644
--- a/src/shutdown/umount.h
+++ b/src/shutdown/umount.h
@@ -9,7 +9,6 @@
int umount_all(bool *changed, bool last_try);
int swapoff_all(bool *changed);
-int loopback_detach_all(bool *changed, bool last_try);
/* This is exported just for testing */
typedef struct MountPoint {