From 706302897acdcbe442de6e5c66023938675cee8d Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Mon, 10 Jun 2024 17:43:52 +0200 Subject: [PATCH] mount-util: clean up mount_exchange_graceful a bit, don't duplicate move_mount when fallback --- src/shared/mount-util.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index 7e18d12df4..0b3154fbd9 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -759,27 +759,19 @@ int mount_exchange_graceful(int fsmount_fd, const char *dest, bool mount_beneath * this is not supported (minimum kernel v6.5), or if there is no mount on the mountpoint, we get * -EINVAL and then we fallback to normal mounting. */ - r = RET_NERRNO(move_mount( - fsmount_fd, - /* from_path= */ "", - /* to_fd= */ -EBADF, - dest, - MOVE_MOUNT_F_EMPTY_PATH | (mount_beneath ? MOVE_MOUNT_BENEATH : 0))); + r = RET_NERRNO(move_mount(fsmount_fd, /* from_path = */ "", + /* to_fd = */ -EBADF, dest, + MOVE_MOUNT_F_EMPTY_PATH | (mount_beneath ? MOVE_MOUNT_BENEATH : 0))); if (mount_beneath) { + if (r >= 0) /* Mounting beneath worked! Now unmount the upper mount. */ + return umount_verbose(LOG_DEBUG, dest, UMOUNT_NOFOLLOW|MNT_DETACH); + if (r == -EINVAL) { /* Fallback if mount_beneath is not supported */ log_debug_errno(r, - "Failed to mount beneath '%s', falling back to overmount", + "Cannot mount beneath '%s', falling back to overmount: %m", dest); - return RET_NERRNO(move_mount( - fsmount_fd, - /* from_path= */ "", - /* to_fd= */ -EBADF, - dest, - MOVE_MOUNT_F_EMPTY_PATH)); + return mount_exchange_graceful(fsmount_fd, dest, /* mount_beneath = */ false); } - - if (r >= 0) /* If it is, now remove the old mount */ - return umount_verbose(LOG_DEBUG, dest, UMOUNT_NOFOLLOW|MNT_DETACH); } return r;