]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
homework: sync everything to disk before we rename LUKS loopback file into place
authorLennart Poettering <lennart@poettering.net>
Mon, 17 Aug 2020 17:54:38 +0000 (19:54 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 Aug 2020 10:41:18 +0000 (12:41 +0200)
This how this works on Linux: when atomically creating a file we need to
fully populate it under a temporary name and then when we are fully
done, sync it and the directory it is contained in, before renaming it
to the final name.

src/home/homework-luks.c

index 1eb08e14231188c156bef6d92f099457a86d4d73..7ce59c8bf331960cb688661cb1dcbbea7cb5eb2b 100644 (file)
@@ -2174,8 +2174,22 @@ int home_create_luks(
                         goto fail;
         }
 
+        /* Sync everything to disk before we move things into place under the final name. */
+        if (fsync(image_fd) < 0) {
+                r = log_error_errno(r, "Failed to synchronize image to disk: %m");
+                goto fail;
+        }
+
         if (disk_uuid_path)
                 (void) ioctl(image_fd, BLKRRPART, 0);
+        else {
+                /* If we operate on a file, sync the contaning directory too. */
+                r = fsync_directory_of_file(image_fd);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to synchronize directory of image file to disk: %m");
+                        goto fail;
+                }
+        }
 
         /* Let's close the image fd now. If we are operating on a real block device this will release the BSD
          * lock that ensures udev doesn't interfere with what we are doing */