]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-util: clean up mount_exchange_graceful a bit, don't duplicate move_mount when...
authorMike Yuan <me@yhndnzj.com>
Mon, 10 Jun 2024 15:43:52 +0000 (17:43 +0200)
committerMike Yuan <me@yhndnzj.com>
Tue, 23 Jul 2024 17:30:55 +0000 (19:30 +0200)
src/shared/mount-util.c

index 7e18d12df455107aca87a395566738afd94afdee..0b3154fbd901d11c4f020e7f5a31f3f5309747fe 100644 (file)
@@ -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;