static int _open_all_keys(struct crypt_device *cd,
	struct luks2_hdr *hdr,
	int keyslot,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags,
	struct volume_key **vks)
{
	int r, segment;
	struct volume_key *_vks = NULL;
	crypt_reencrypt_info ri = LUKS2_reencrypt_status(hdr);

	segment = (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ? CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT;

	switch (ri) {
	case CRYPT_REENCRYPT_NONE:
		r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase, passphrase_size, &_vks);
		break;
	case CRYPT_REENCRYPT_CLEAN:
	case CRYPT_REENCRYPT_CRASH:
		if (segment == CRYPT_ANY_SEGMENT)
			r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase,
					       passphrase_size, &_vks);
		else
			r = LUKS2_keyslot_open_all_segments(cd, keyslot,
					keyslot, passphrase, passphrase_size,
					&_vks);
		break;
	default:
		r = -EINVAL;
	}

	if (keyslot == CRYPT_ANY_SLOT)
		keyslot = r;

	if (r >= 0 && (flags & CRYPT_ACTIVATE_KEYRING_KEY))
		r = load_all_keys(cd, hdr, _vks);

	if (r >= 0 && vks)
		MOVE_REF(*vks, _vks);

	if (r < 0)
		crypt_drop_keyring_key(cd, _vks);
	crypt_free_volume_key(_vks);

	return r < 0 ? r : keyslot;
}
static int _open_all_keys(struct crypt_device *cd,
	struct luks2_hdr *hdr,
	int keyslot,
	const char *passphrase,
	size_t passphrase_size,
	uint32_t flags,
	struct volume_key **vks)
{
	int r, segment;
	struct volume_key *_vks = NULL;
	crypt_reencrypt_info ri = LUKS2_reencrypt_status(hdr);

	segment = (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) ? CRYPT_ANY_SEGMENT : CRYPT_DEFAULT_SEGMENT;

	switch (ri) {
	case CRYPT_REENCRYPT_NONE:
		r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase, passphrase_size, &_vks);
		break;
	case CRYPT_REENCRYPT_CLEAN:
	case CRYPT_REENCRYPT_CRASH:
		if (segment == CRYPT_ANY_SEGMENT)
			r = LUKS2_keyslot_open(cd, keyslot, segment, passphrase,
					       passphrase_size, &_vks);
		else
			r = LUKS2_keyslot_open_all_segments(cd, keyslot,
					keyslot, passphrase, passphrase_size,
					&_vks);
		break;
	default:
		r = -EINVAL;
	}

	if (keyslot == CRYPT_ANY_SLOT)
		keyslot = r;

	if (r >= 0 && (flags & CRYPT_ACTIVATE_KEYRING_KEY))
		r = load_all_keys(cd, hdr, _vks);

	if (r >= 0 && vks)
		MOVE_REF(*vks, _vks);

	if (r < 0)
		crypt_drop_keyring_key(cd, _vks);
	crypt_free_volume_key(_vks);

	return r < 0 ? r : keyslot;
}