diff mbox series

[4/4] fsx: move range generation logic into a common helper

Message ID 20200420170931.10047-1-fdmanana@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/4] fsx: allow zero range operations to cross eof | expand

Commit Message

Filipe Manana April 20, 2020, 5:09 p.m. UTC
From: Filipe Manana <fdmanana@suse.com>

We have very similar code that generates the destination range for clone,
dedupe and copy_file_range operations, so avoid duplicating the code three
times and move it into a helper function.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 ltp/fsx.c | 94 ++++++++++++++++++++++++++-------------------------------------
 1 file changed, 39 insertions(+), 55 deletions(-)

Comments

Brian Foster April 21, 2020, 2:22 p.m. UTC | #1
On Mon, Apr 20, 2020 at 06:09:31PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> We have very similar code that generates the destination range for clone,
> dedupe and copy_file_range operations, so avoid duplicating the code three
> times and move it into a helper function.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> ---

Reviewed-by: Brian Foster <bfoster@redhat.com>

>  ltp/fsx.c | 94 ++++++++++++++++++++++++++-------------------------------------
>  1 file changed, 39 insertions(+), 55 deletions(-)
> 
> diff --git a/ltp/fsx.c b/ltp/fsx.c
> index 40cbd401..7c76655a 100644
> --- a/ltp/fsx.c
> +++ b/ltp/fsx.c
> @@ -1939,6 +1939,39 @@ range_overlaps(
>  	return llabs((unsigned long long)off1 - off0) < size;
>  }
>  
> +static void generate_dest_range(bool bdy_align,
> +				unsigned long max_range_end,
> +				unsigned long *src_offset,
> +				unsigned long *size,
> +				unsigned long *dst_offset)
> +{
> +	int tries = 0;
> +
> +	TRIM_OFF_LEN(*src_offset, *size, file_size);
> +	if (bdy_align) {
> +		*src_offset -= *src_offset % readbdy;
> +		if (o_direct)
> +			*size -= *size % readbdy;
> +	} else {
> +		*src_offset = *src_offset & ~(block_size - 1);
> +		*size = *size & ~(block_size - 1);
> +	}
> +
> +	do {
> +		if (tries++ >= 30) {
> +			*size = 0;
> +			break;
> +		}
> +		*dst_offset = random();
> +		TRIM_OFF(*dst_offset, max_range_end);
> +		if (bdy_align)
> +			*dst_offset -= *dst_offset % writebdy;
> +		else
> +			*dst_offset = *dst_offset & ~(block_size - 1);
> +	} while (range_overlaps(*src_offset, *dst_offset, *size) ||
> +		 *dst_offset + *size > max_range_end);
> +}
> +
>  int
>  test(void)
>  {
> @@ -2013,63 +2046,14 @@ test(void)
>  			keep_size = random() % 2;
>  		break;
>  	case OP_CLONE_RANGE:
> -		{
> -			int tries = 0;
> -
> -			TRIM_OFF_LEN(offset, size, file_size);
> -			offset = offset & ~(block_size - 1);
> -			size = size & ~(block_size - 1);
> -			do {
> -				if (tries++ >= 30) {
> -					size = 0;
> -					break;
> -				}
> -				offset2 = random();
> -				TRIM_OFF(offset2, maxfilelen);
> -				offset2 = offset2 & ~(block_size - 1);
> -			} while (range_overlaps(offset, offset2, size) ||
> -				 offset2 + size > maxfilelen);
> -			break;
> -		}
> +		generate_dest_range(false, maxfilelen, &offset, &size, &offset2);
> +		break;
>  	case OP_DEDUPE_RANGE:
> -		{
> -			int tries = 0;
> -
> -			TRIM_OFF_LEN(offset, size, file_size);
> -			offset = offset & ~(block_size - 1);
> -			size = size & ~(block_size - 1);
> -			do {
> -				if (tries++ >= 30) {
> -					size = 0;
> -					break;
> -				}
> -				offset2 = random();
> -				TRIM_OFF(offset2, file_size);
> -				offset2 = offset2 & ~(block_size - 1);
> -			} while (range_overlaps(offset, offset2, size) ||
> -				 offset2 + size > file_size);
> -			break;
> -		}
> +		generate_dest_range(false, file_size, &offset, &size, &offset2);
> +		break;
>  	case OP_COPY_RANGE:
> -		{
> -			int tries = 0;
> -
> -			TRIM_OFF_LEN(offset, size, file_size);
> -			offset -= offset % readbdy;
> -			if (o_direct)
> -				size -= size % readbdy;
> -			do {
> -				if (tries++ >= 30) {
> -					size = 0;
> -					break;
> -				}
> -				offset2 = random();
> -				TRIM_OFF(offset2, maxfilelen);
> -				offset2 -= offset2 % writebdy;
> -			} while (range_overlaps(offset, offset2, size) ||
> -				 offset2 + size > maxfilelen);
> -			break;
> -		}
> +		generate_dest_range(true, maxfilelen, &offset, &size, &offset2);
> +		break;
>  	}
>  
>  have_op:
> -- 
> 2.11.0
>
diff mbox series

Patch

diff --git a/ltp/fsx.c b/ltp/fsx.c
index 40cbd401..7c76655a 100644
--- a/ltp/fsx.c
+++ b/ltp/fsx.c
@@ -1939,6 +1939,39 @@  range_overlaps(
 	return llabs((unsigned long long)off1 - off0) < size;
 }
 
+static void generate_dest_range(bool bdy_align,
+				unsigned long max_range_end,
+				unsigned long *src_offset,
+				unsigned long *size,
+				unsigned long *dst_offset)
+{
+	int tries = 0;
+
+	TRIM_OFF_LEN(*src_offset, *size, file_size);
+	if (bdy_align) {
+		*src_offset -= *src_offset % readbdy;
+		if (o_direct)
+			*size -= *size % readbdy;
+	} else {
+		*src_offset = *src_offset & ~(block_size - 1);
+		*size = *size & ~(block_size - 1);
+	}
+
+	do {
+		if (tries++ >= 30) {
+			*size = 0;
+			break;
+		}
+		*dst_offset = random();
+		TRIM_OFF(*dst_offset, max_range_end);
+		if (bdy_align)
+			*dst_offset -= *dst_offset % writebdy;
+		else
+			*dst_offset = *dst_offset & ~(block_size - 1);
+	} while (range_overlaps(*src_offset, *dst_offset, *size) ||
+		 *dst_offset + *size > max_range_end);
+}
+
 int
 test(void)
 {
@@ -2013,63 +2046,14 @@  test(void)
 			keep_size = random() % 2;
 		break;
 	case OP_CLONE_RANGE:
-		{
-			int tries = 0;
-
-			TRIM_OFF_LEN(offset, size, file_size);
-			offset = offset & ~(block_size - 1);
-			size = size & ~(block_size - 1);
-			do {
-				if (tries++ >= 30) {
-					size = 0;
-					break;
-				}
-				offset2 = random();
-				TRIM_OFF(offset2, maxfilelen);
-				offset2 = offset2 & ~(block_size - 1);
-			} while (range_overlaps(offset, offset2, size) ||
-				 offset2 + size > maxfilelen);
-			break;
-		}
+		generate_dest_range(false, maxfilelen, &offset, &size, &offset2);
+		break;
 	case OP_DEDUPE_RANGE:
-		{
-			int tries = 0;
-
-			TRIM_OFF_LEN(offset, size, file_size);
-			offset = offset & ~(block_size - 1);
-			size = size & ~(block_size - 1);
-			do {
-				if (tries++ >= 30) {
-					size = 0;
-					break;
-				}
-				offset2 = random();
-				TRIM_OFF(offset2, file_size);
-				offset2 = offset2 & ~(block_size - 1);
-			} while (range_overlaps(offset, offset2, size) ||
-				 offset2 + size > file_size);
-			break;
-		}
+		generate_dest_range(false, file_size, &offset, &size, &offset2);
+		break;
 	case OP_COPY_RANGE:
-		{
-			int tries = 0;
-
-			TRIM_OFF_LEN(offset, size, file_size);
-			offset -= offset % readbdy;
-			if (o_direct)
-				size -= size % readbdy;
-			do {
-				if (tries++ >= 30) {
-					size = 0;
-					break;
-				}
-				offset2 = random();
-				TRIM_OFF(offset2, maxfilelen);
-				offset2 -= offset2 % writebdy;
-			} while (range_overlaps(offset, offset2, size) ||
-				 offset2 + size > maxfilelen);
-			break;
-		}
+		generate_dest_range(true, maxfilelen, &offset, &size, &offset2);
+		break;
 	}
 
 have_op: