static uint64_t reencrypt_length(struct crypt_device *cd,
		struct luks2_hdr *hdr,
		struct luks2_reencrypt *rh,
		uint64_t keyslot_area_length,
		uint64_t length_max)
{
	unsigned long dummy, optimal_alignment;
	uint64_t length, soft_mem_limit;

	if (rh->rp.type == REENC_PROTECTION_NONE)
		length = length_max ?: LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH;
	else if (rh->rp.type == REENC_PROTECTION_CHECKSUM)
		length = (keyslot_area_length / rh->rp.p.csum.hash_size) * rh->alignment;
	else if (rh->rp.type == REENC_PROTECTION_DATASHIFT)
		return reencrypt_data_shift(hdr);
	else
		length = keyslot_area_length;

	/* hard limit */
	if (length > LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH)
		length = LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH;

	/* soft limit is 1/4 of system memory */
	soft_mem_limit = crypt_getphysmemory_kb() << 8; /* multiply by (1024/4) */

	if (soft_mem_limit && length > soft_mem_limit)
		length = soft_mem_limit;

	if (length_max && length > length_max)
		length = length_max;

	length -= (length % rh->alignment);

	/* Emits error later */
	if (!length)
		return length;

	device_topology_alignment(cd, crypt_data_device(cd), &optimal_alignment, &dummy, length);

	/* we have to stick with encryption sector size alignment */
	if (optimal_alignment % rh->alignment)
		return length;

	/* align to opt-io size only if remaining size allows it */
	if (length > optimal_alignment)
		length -= (length % optimal_alignment);

	return length;
}
static uint64_t reencrypt_length(struct crypt_device *cd,
		struct luks2_hdr *hdr,
		struct luks2_reencrypt *rh,
		uint64_t keyslot_area_length,
		uint64_t length_max)
{
	unsigned long dummy, optimal_alignment;
	uint64_t length, soft_mem_limit;

	if (rh->rp.type == REENC_PROTECTION_NONE)
		length = length_max ?: LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH;
	else if (rh->rp.type == REENC_PROTECTION_CHECKSUM)
		length = (keyslot_area_length / rh->rp.p.csum.hash_size) * rh->alignment;
	else if (rh->rp.type == REENC_PROTECTION_DATASHIFT)
		return reencrypt_data_shift(hdr);
	else
		length = keyslot_area_length;

	 
	if (length > LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH)
		length = LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH;

	 
	soft_mem_limit = crypt_getphysmemory_kb() << 8;  

	if (soft_mem_limit && length > soft_mem_limit)
		length = soft_mem_limit;

	if (length_max && length > length_max)
		length = length_max;

	length -= (length % rh->alignment);

	 
	if (!length)
		return length;

	device_topology_alignment(cd, crypt_data_device(cd), &optimal_alignment, &dummy, length);

	 
	if (optimal_alignment % rh->alignment)
		return length;

	 
	if (length > optimal_alignment)
		length -= (length % optimal_alignment);

	return length;
}