static int _open_and_activate(struct crypt_device *cd,
	int keyslot,
	const char *name,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags)
{
	bool use_keyring;
	int r;
	struct volume_key *vk = NULL;

	r = LUKS2_keyslot_open(cd, keyslot,
			       (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ?
			       CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT,
			       passphrase, passphrase_size, &vk);
	if (r < 0)
		return r;
	keyslot = r;

	if (!crypt_use_keyring_for_vk(cd))
		use_keyring = false;
	else
		use_keyring = ((name && !crypt_is_cipher_null(crypt_get_cipher(cd))) ||
			       (flags & CRYPT_ACTIVATE_KEYRING_KEY));

	if (use_keyring) {
		r = LUKS2_volume_key_load_in_keyring_by_keyslot(cd,
				&cd->u.luks2.hdr, vk, keyslot);
		if (r < 0)
			goto out;
		flags |= CRYPT_ACTIVATE_KEYRING_KEY;
	}

	if (name)
		r = LUKS2_activate(cd, name, vk, flags);
out:
	if (r < 0)
		crypt_drop_keyring_key(cd, vk);
	crypt_free_volume_key(vk);

	return r < 0 ? r : keyslot;
}
static int _open_and_activate(struct crypt_device *cd,
	int keyslot,
	const char *name,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags)
{
	bool use_keyring;
	int r;
	struct volume_key *vk = NULL;

	r = LUKS2_keyslot_open(cd, keyslot,
			       (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ?
			       CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT,
			       passphrase, passphrase_size, &vk);
	if (r < 0)
		return r;
	keyslot = r;

	if (!crypt_use_keyring_for_vk(cd))
		use_keyring = false;
	else
		use_keyring = ((name && !crypt_is_cipher_null(crypt_get_cipher(cd))) ||
			       (flags & CRYPT_ACTIVATE_KEYRING_KEY));

	if (use_keyring) {
		r = LUKS2_volume_key_load_in_keyring_by_keyslot(cd,
				&cd->u.luks2.hdr, vk, keyslot);
		if (r < 0)
			goto out;
		flags |= CRYPT_ACTIVATE_KEYRING_KEY;
	}

	if (name)
		r = LUKS2_activate(cd, name, vk, flags);
out:
	if (r < 0)
		crypt_drop_keyring_key(cd, vk);
	crypt_free_volume_key(vk);

	return r < 0 ? r : keyslot;
}