From b4b66b26620bfaf5818c95d5cffafd85207694e7 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Mon, 9 Sep 2024 17:53:03 +0200
Subject: [PATCH] shutdown: replace unbounded fsync() with bounded
 sync_with_progress()

Let's put a time-out on this syncing.

Inspired-by: #34289 #34283

Conflict:NA
Reference:https://github.com/systemd/systemd/pull/34330/commits/b4b66b26620bfaf5818c95d5cffafd85207694e7

---
 src/shutdown/detach-dm.c       | 11 ++++++-----
 src/shutdown/detach-loopback.c |  4 ++--
 src/shutdown/detach-md.c       |  4 ++--
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/shutdown/detach-dm.c b/src/shutdown/detach-dm.c
index f6f672c7..bddd748d 100644
--- a/src/shutdown/detach-dm.c
+++ b/src/shutdown/detach-dm.c
@@ -15,7 +15,7 @@
 #include "devnum-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
-#include "sync-util.h"
+#include "shutdown.h"
 
 typedef struct DeviceMapper {
         char *path;
@@ -93,7 +93,6 @@ static int dm_list_get(DeviceMapper **head) {
 
 static int delete_dm(DeviceMapper *m) {
         _cleanup_close_ int fd = -EBADF;
-        int r;
 
         assert(m);
         assert(major(m->devnum) != 0);
@@ -103,9 +102,11 @@ static int delete_dm(DeviceMapper *m) {
         if (fd < 0)
                 return -errno;
 
-        r = fsync_path_at(AT_FDCWD, m->path);
-        if (r < 0)
-                log_debug_errno(r, "Failed to sync DM block device %s, ignoring: %m", m->path);
+        _cleanup_close_ int block_fd = open(m->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
+        if (block_fd < 0)
+                log_debug_errno(errno, "Failed to open DM block device %s for syncing, ignoring: %m", m->path);
+        else
+                (void) sync_with_progress(block_fd);
 
         return RET_NERRNO(ioctl(fd, DM_DEV_REMOVE, &(struct dm_ioctl) {
                 .version = {
diff --git a/src/shutdown/detach-loopback.c b/src/shutdown/detach-loopback.c
index 267509f7..8778a9e0 100644
--- a/src/shutdown/detach-loopback.c
+++ b/src/shutdown/detach-loopback.c
@@ -18,6 +18,7 @@
 #include "detach-loopback.h"
 #include "device-util.h"
 #include "fd-util.h"
+#include "shutdown.h"
 
 typedef struct LoopbackDevice {
         char *path;
@@ -111,8 +112,7 @@ static int delete_loopback(const char *device) {
 
         /* 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);
+        (void) sync_with_progress(fd);
 
         if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
                 if (errno == ENXIO) /* Nothing bound, didn't do anything */
diff --git a/src/shutdown/detach-md.c b/src/shutdown/detach-md.c
index ac46670f..b1aad976 100644
--- a/src/shutdown/detach-md.c
+++ b/src/shutdown/detach-md.c
@@ -17,6 +17,7 @@
 #include "devnum-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
+#include "shutdown.h"
 #include "string-util.h"
 
 typedef struct RaidDevice {
@@ -133,8 +134,7 @@ static int delete_md(RaidDevice *m) {
         if (fd < 0)
                 return -errno;
 
-        if (fsync(fd) < 0)
-                log_debug_errno(errno, "Failed to sync MD block device %s, ignoring: %m", m->path);
+        (void) sync_with_progress(fd);
 
         return RET_NERRNO(ioctl(fd, STOP_ARRAY, NULL));
 }
-- 
2.43.0

