diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index 3fb5b1b745..58a87431a9 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -2703,7 +2703,6 @@ static int verity_partition( DecryptedImage *d) { _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL; - _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; _cleanup_free_ char *node = NULL, *name = NULL; _cleanup_close_ int mount_node_fd = -EBADF; int r; @@ -2766,6 +2765,7 @@ static int verity_partition( * In case of ENODEV/ENOENT, which can happen if another process is activating at the exact same time, * retry a few times before giving up. */ for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { + _cleanup_(dm_deferred_remove_cleanp) char *restore_deferred_remove = NULL; _cleanup_(sym_crypt_freep) struct crypt_device *existing_cd = NULL; _cleanup_close_ int fd = -EBADF; @@ -2795,21 +2795,19 @@ static int verity_partition( return log_debug_errno(r, "Failed to activate verity device %s: %m", node); check: - if (!restore_deferred_remove) { - /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ - r = dm_deferred_remove_cancel(name); - /* -EBUSY and -ENXIO: the device has already been removed or being removed. We cannot - * use the device, try to open again. See target_message() in drivers/md/dm-ioctl.c - * and dm_cancel_deferred_remove() in drivers/md/dm.c */ - if (IN_SET(r, -EBUSY, -ENXIO)) - goto try_again; - if (r < 0) - return log_debug_errno(r, "Failed to disable automated deferred removal for verity device %s: %m", node); + /* To avoid races, disable automatic removal on umount while setting up the new device. Restore it on failure. */ + r = dm_deferred_remove_cancel(name); + /* -EBUSY and -ENXIO: the device has already been removed or being removed. We cannot + * use the device, try to open again. See target_message() in drivers/md/dm-ioctl.c + * and dm_cancel_deferred_remove() in drivers/md/dm.c */ + if (IN_SET(r, -EBUSY, -ENXIO)) + goto try_again; + if (r < 0) + return log_debug_errno(r, "Failed to disable automated deferred removal for verity device %s: %m", node); - restore_deferred_remove = strdup(name); - if (!restore_deferred_remove) - return log_oom_debug(); - } + restore_deferred_remove = strdup(name); + if (!restore_deferred_remove) + return log_oom_debug(); r = verity_can_reuse(verity, name, &existing_cd); /* Same as above, -EINVAL can randomly happen when it actually means -EEXIST */ @@ -2847,6 +2845,9 @@ static int verity_partition( } } + /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ + restore_deferred_remove = mfree(restore_deferred_remove); + mount_node_fd = TAKE_FD(fd); if (existing_cd) crypt_free_and_replace(cd, existing_cd); @@ -2874,9 +2875,6 @@ static int verity_partition( return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), "All attempts to activate verity device %s failed.", name); success: - /* Everything looks good and we'll be able to mount the device, so deferred remove will be re-enabled at that point. */ - restore_deferred_remove = mfree(restore_deferred_remove); - d->decrypted[d->n_decrypted++] = (DecryptedPartition) { .name = TAKE_PTR(name), .device = TAKE_PTR(cd),