1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Merge pull request #34330 from poettering/shutdown-async-sync

shutdown: make all fsync()s asynchronous and apply timeout
This commit is contained in:
Lennart Poettering 2024-09-10 06:41:46 +02:00 committed by GitHub
commit b4199a97e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 59 additions and 18 deletions

View File

@ -34,6 +34,28 @@ int asynchronous_sync(pid_t *ret_pid) {
return 0;
}
int asynchronous_fsync(int fd, pid_t *ret_pid) {
int r;
assert(fd >= 0);
/* Same as asynchronous_sync() above, but calls fsync() on a specific fd */
r = safe_fork_full("(sd-fsync)",
/* stdio_fds= */ NULL,
/* except_fds= */ &fd,
/* n_except_fds= */ 1,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|(ret_pid ? 0 : FORK_DETACH), ret_pid);
if (r < 0)
return r;
if (r == 0) {
/* Child process */
fsync(fd);
_exit(EXIT_SUCCESS);
}
return 0;
}
/* We encode the fd to close in the userdata pointer as an unsigned value. The highest bit indicates whether
* we need to fork again */
#define NEED_DOUBLE_FORK (1U << (sizeof(unsigned) * 8 - 1))

View File

@ -20,6 +20,7 @@
* for avoiding threads. */
int asynchronous_sync(pid_t *ret_pid);
int asynchronous_fsync(int fd, pid_t *ret_pid);
int asynchronous_close(int fd);
int asynchronous_rm_rf(const char *p, RemoveFlags flags);

View File

@ -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 = {

View File

@ -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 */

View File

@ -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));
}

View File

@ -40,6 +40,7 @@
#include "process-util.h"
#include "reboot-util.h"
#include "rlimit-util.h"
#include "shutdown.h"
#include "signal-util.h"
#include "string-util.h"
#include "switch-root.h"
@ -223,8 +224,10 @@ static int sync_making_progress(unsigned long long *prev_dirty) {
return r;
}
static int sync_with_progress(void) {
int sync_with_progress(int fd) {
unsigned long long dirty = ULLONG_MAX;
_cleanup_free_ char *path = NULL;
const char *what;
pid_t pid;
int r;
@ -233,11 +236,20 @@ static int sync_with_progress(void) {
/* Due to the possibility of the sync operation hanging, we fork a child process and monitor
* the progress. If the timeout lapses, the assumption is that the particular sync stalled. */
r = asynchronous_sync(&pid);
if (r < 0)
return log_error_errno(r, "Failed to fork sync(): %m");
if (fd >= 0) {
r = asynchronous_fsync(fd, &pid);
if (r < 0)
return log_error_errno(r, "Failed to fork fsync(): %m");
log_info("Syncing filesystems and block devices.");
(void) fd_get_path(fd, &path);
} else {
r = asynchronous_sync(&pid);
if (r < 0)
return log_error_errno(r, "Failed to fork sync(): %m");
}
what = path ?: "filesystems and block devices";
log_info("Syncing %s.", what);
/* Start monitoring the sync operation. If more than
* SYNC_PROGRESS_ATTEMPTS lapse without progress being made,
@ -248,7 +260,7 @@ static int sync_with_progress(void) {
/* Sync finished without error (sync() call itself does not return an error code) */
return 0;
if (r != -ETIMEDOUT)
return log_error_errno(r, "Failed to sync filesystems and block devices: %m");
return log_error_errno(r, "Failed to sync %s: %m", what);
/* Reset the check counter if we made some progress */
if (sync_making_progress(&dirty) > 0)
@ -258,7 +270,8 @@ static int sync_with_progress(void) {
/* Only reached in the event of a timeout. We should issue a kill to the stray process. */
(void) kill(pid, SIGKILL);
return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT),
"Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT".",
"Syncing %s - timed out, issuing SIGKILL to PID "PID_FMT".",
what,
pid);
}
@ -432,7 +445,7 @@ int main(int argc, char *argv[]) {
* desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will
* result. */
if (!in_container)
(void) sync_with_progress();
(void) sync_with_progress(-EBADF);
disable_coredumps();
disable_binfmt();
@ -600,7 +613,7 @@ int main(int argc, char *argv[]) {
* which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption
* will result. */
if (!in_container)
(void) sync_with_progress();
(void) sync_with_progress(-EBADF);
notify_supervisor();

4
src/shutdown/shutdown.h Normal file
View File

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
int sync_with_progress(int fd);