]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect-image: take a reference of LoopDevice into DissectedImage 24720/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Sep 2022 13:46:39 +0000 (22:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 17 Sep 2022 22:56:32 +0000 (07:56 +0900)
To make LoopDevice object freed after DissectedImage is freed.
At least currently, this should not change anything. Preparation for
later commits.

src/shared/dissect-image.c
src/shared/dissect-image.h

index e97c22d79644b68489e60f4b3037b837597aa8b5..ab8813447a0b3c46d05beca9347f88fe64eb0b71 100644 (file)
@@ -1120,6 +1120,10 @@ DissectedImage* dissected_image_unref(DissectedImage *m) {
          * DecryptedImage may try to deactivate partitions. */
         decrypted_image_unref(m->decrypted_image);
 
+        /* Third, unref LoopDevice. This must be called after the above two, as freeing LoopDevice may try to
+         * remove existing partitions on the loopback block device. */
+        loop_device_unref(m->loop);
+
         free(m->image_name);
         free(m->hostname);
         strv_free(m->machine_info);
@@ -2794,8 +2798,31 @@ finish:
         return r;
 }
 
+int dissect_loop_device(
+                LoopDevice *loop,
+                const VeritySettings *verity,
+                const MountOptions *mount_options,
+                DissectImageFlags flags,
+                DissectedImage **ret) {
+
+        _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
+        int r;
+
+        assert(loop);
+        assert(ret);
+
+        r = dissect_image(loop->fd, loop->node, loop->backing_file ?: loop->node, verity, mount_options, flags, &m);
+        if (r < 0)
+                return r;
+
+        m->loop = loop_device_ref(loop);
+
+        *ret = TAKE_PTR(m);
+        return 0;
+}
+
 int dissect_loop_device_and_warn(
-                const LoopDevice *loop,
+                LoopDevice *loop,
                 const VeritySettings *verity,
                 const MountOptions *mount_options,
                 DissectImageFlags flags,
index 7308798493b9a408add44cb616aab4c23fa9eaf1..efddea80c7b5bca48d24c1e3bd04adb9d429c26a 100644 (file)
@@ -210,6 +210,7 @@ struct DissectedImage {
         bool verity_sig_ready:1;   /* verity signature logic, fully specified and usable */
         bool single_file_system:1; /* MBR/GPT or single file system */
 
+        LoopDevice *loop;
         DissectedPartition partitions[_PARTITION_DESIGNATOR_MAX];
         DecryptedImage *decrypted_image;
 
@@ -262,11 +263,8 @@ int dissect_image(
                 const MountOptions *mount_options,
                 DissectImageFlags flags,
                 DissectedImage **ret);
-static inline int dissect_loop_device(const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret) {
-        assert(loop);
-        return dissect_image(loop->fd, loop->node, loop->backing_file ?: loop->node, verity, mount_options, flags, ret);
-}
-int dissect_loop_device_and_warn(const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
+int dissect_loop_device(LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
+int dissect_loop_device_and_warn(LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
 
 DissectedImage* dissected_image_unref(DissectedImage *m);
 DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);