static int _open_and_activate_luks2(struct crypt_device *cd,
	int keyslot,
	const char *name,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags)
{
	crypt_reencrypt_info ri;
	int r, rv;
	struct luks2_hdr *hdr = &cd->u.luks2.hdr;
	struct volume_key *vks = NULL;

	ri = LUKS2_reencrypt_status(hdr);
	if (ri == CRYPT_REENCRYPT_INVALID)
		return -EINVAL;

	if (ri > CRYPT_REENCRYPT_NONE) {
		if (name)
			r = _open_and_activate_reencrypt_device(cd, hdr, keyslot, name, passphrase,
					passphrase_size, flags);
		else {
			r = _open_all_keys(cd, hdr, keyslot, passphrase,
					   passphrase_size, flags, &vks);
			if (r < 0)
				return r;

			rv = LUKS2_reencrypt_digest_verify(cd, hdr, vks);
			crypt_free_volume_key(vks);
			if (rv < 0)
				return rv;
		}
	} else
		r = _open_and_activate(cd, keyslot, name, passphrase,
				passphrase_size, flags);

	return r;
}
static int _open_and_activate_luks2(struct crypt_device *cd,
	int keyslot,
	const char *name,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags)
{
	crypt_reencrypt_info ri;
	int r, rv;
	struct luks2_hdr *hdr = &cd->u.luks2.hdr;
	struct volume_key *vks = NULL;

	ri = LUKS2_reencrypt_status(hdr);
	if (ri == CRYPT_REENCRYPT_INVALID)
		return -EINVAL;

	if (ri > CRYPT_REENCRYPT_NONE) {
		if (name)
			r = _open_and_activate_reencrypt_device(cd, hdr, keyslot, name, passphrase,
					passphrase_size, flags);
		else {
			r = _open_all_keys(cd, hdr, keyslot, passphrase,
					   passphrase_size, flags, &vks);
			if (r < 0)
				return r;

			rv = LUKS2_reencrypt_digest_verify(cd, hdr, vks);
			crypt_free_volume_key(vks);
			if (rv < 0)
				return rv;
		}
	} else
		r = _open_and_activate(cd, keyslot, name, passphrase,
				passphrase_size, flags);

	return r;
}