diff mbox series

[v3,08/10] ext4: factor out check for whether a cluster is allocated

Message ID 20240508061220.967970-9-yi.zhang@huaweicloud.com
State Superseded
Headers show
Series ext4: support adding multi-delalloc blocks | expand

Commit Message

Zhang Yi May 8, 2024, 6:12 a.m. UTC
From: Zhang Yi <yi.zhang@huawei.com>

Factor out a common helper ext4_da_check_clu_allocated(), check whether
the cluster containing a delalloc block to be added has been delayed or
allocated, no logic changes.

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
---
 fs/ext4/inode.c | 52 +++++++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 17 deletions(-)

Comments

Jan Kara May 12, 2024, 3:40 p.m. UTC | #1
On Wed 08-05-24 14:12:18, Zhang Yi wrote:
> From: Zhang Yi <yi.zhang@huawei.com>
> 
> Factor out a common helper ext4_da_check_clu_allocated(), check whether
> the cluster containing a delalloc block to be added has been delayed or
> allocated, no logic changes.
> 
> Signed-off-by: Zhang Yi <yi.zhang@huawei.com>

I have one suggestion for improvement here.

> +/*
> + * Check whether the cluster containing lblk has been delayed or allocated,
> + * if not, it means we should reserve a cluster when add delalloc, return 1,
> + * otherwise return 0 or error code.
> + */
> +static int ext4_da_check_clu_allocated(struct inode *inode, ext4_lblk_t lblk,
> +				       bool *allocated)

The name of the function does not quite match what it is returning and that
is confusing. Essentially we have three states here:

a) cluster allocated
b) cluster has delalloc reservation
c) cluster doesn't have either

So maybe we could call the function ext4_clu_alloc_state() and return 0 /
1 / 2 based on the state?

								Honza

> +{
> +	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
> +	int ret;
> +
> +	*allocated = false;
> +	if (ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk))
> +		return 0;
> +
> +	if (ext4_es_scan_clu(inode, &ext4_es_is_mapped, lblk))
> +		goto allocated;
> +
> +	ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk));
> +	if (ret < 0)
> +		return ret;
> +	if (ret == 0)
> +		return 1;
> +allocated:
> +	*allocated = true;
> +	return 0;
> +}
> +
>  /*
>   * ext4_insert_delayed_block - adds a delayed block to the extents status
>   *                             tree, incrementing the reserved cluster/block
> @@ -1682,23 +1710,13 @@ static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
>  		if (ret != 0)   /* ENOSPC */
>  			return ret;
>  	} else {   /* bigalloc */
> -		if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
> -			if (!ext4_es_scan_clu(inode,
> -					      &ext4_es_is_mapped, lblk)) {
> -				ret = ext4_clu_mapped(inode,
> -						      EXT4_B2C(sbi, lblk));
> -				if (ret < 0)
> -					return ret;
> -				if (ret == 0) {
> -					ret = ext4_da_reserve_space(inode, 1);
> -					if (ret != 0)   /* ENOSPC */
> -						return ret;
> -				} else {
> -					allocated = true;
> -				}
> -			} else {
> -				allocated = true;
> -			}
> +		ret = ext4_da_check_clu_allocated(inode, lblk, &allocated);
> +		if (ret < 0)
> +			return ret;
> +		if (ret > 0) {
> +			ret = ext4_da_reserve_space(inode, 1);
> +			if (ret != 0)   /* ENOSPC */
> +				return ret;
>  		}
>  	}
>  
> -- 
> 2.39.2
>
Zhang Yi May 14, 2024, 2:37 a.m. UTC | #2
On 2024/5/12 23:40, Jan Kara wrote:
> On Wed 08-05-24 14:12:18, Zhang Yi wrote:
>> From: Zhang Yi <yi.zhang@huawei.com>
>>
>> Factor out a common helper ext4_da_check_clu_allocated(), check whether
>> the cluster containing a delalloc block to be added has been delayed or
>> allocated, no logic changes.
>>
>> Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
> 
> I have one suggestion for improvement here.
> 
>> +/*
>> + * Check whether the cluster containing lblk has been delayed or allocated,
>> + * if not, it means we should reserve a cluster when add delalloc, return 1,
>> + * otherwise return 0 or error code.
>> + */
>> +static int ext4_da_check_clu_allocated(struct inode *inode, ext4_lblk_t lblk,
>> +				       bool *allocated)
> 
> The name of the function does not quite match what it is returning and that
> is confusing. Essentially we have three states here:
> 
> a) cluster allocated
> b) cluster has delalloc reservation
> c) cluster doesn't have either
> 
> So maybe we could call the function ext4_clu_alloc_state() and return 0 /
> 1 / 2 based on the state?
> 
> 								Honza

Sure, thanks for the suggestion, it looks better.

Thanks,
Yi.

> 
>> +{
>> +	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
>> +	int ret;
>> +
>> +	*allocated = false;
>> +	if (ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk))
>> +		return 0;
>> +
>> +	if (ext4_es_scan_clu(inode, &ext4_es_is_mapped, lblk))
>> +		goto allocated;
>> +
>> +	ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk));
>> +	if (ret < 0)
>> +		return ret;
>> +	if (ret == 0)
>> +		return 1;
>> +allocated:
>> +	*allocated = true;
>> +	return 0;
>> +}
>> +
>>  /*
>>   * ext4_insert_delayed_block - adds a delayed block to the extents status
>>   *                             tree, incrementing the reserved cluster/block
>> @@ -1682,23 +1710,13 @@ static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
>>  		if (ret != 0)   /* ENOSPC */
>>  			return ret;
>>  	} else {   /* bigalloc */
>> -		if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
>> -			if (!ext4_es_scan_clu(inode,
>> -					      &ext4_es_is_mapped, lblk)) {
>> -				ret = ext4_clu_mapped(inode,
>> -						      EXT4_B2C(sbi, lblk));
>> -				if (ret < 0)
>> -					return ret;
>> -				if (ret == 0) {
>> -					ret = ext4_da_reserve_space(inode, 1);
>> -					if (ret != 0)   /* ENOSPC */
>> -						return ret;
>> -				} else {
>> -					allocated = true;
>> -				}
>> -			} else {
>> -				allocated = true;
>> -			}
>> +		ret = ext4_da_check_clu_allocated(inode, lblk, &allocated);
>> +		if (ret < 0)
>> +			return ret;
>> +		if (ret > 0) {
>> +			ret = ext4_da_reserve_space(inode, 1);
>> +			if (ret != 0)   /* ENOSPC */
>> +				return ret;
>>  		}
>>  	}
>>  
>> -- 
>> 2.39.2
>>
diff mbox series

Patch

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 0c52969654ac..6e418d3f8e87 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -1649,6 +1649,34 @@  static void ext4_print_free_blocks(struct inode *inode)
 	return;
 }
 
+/*
+ * Check whether the cluster containing lblk has been delayed or allocated,
+ * if not, it means we should reserve a cluster when add delalloc, return 1,
+ * otherwise return 0 or error code.
+ */
+static int ext4_da_check_clu_allocated(struct inode *inode, ext4_lblk_t lblk,
+				       bool *allocated)
+{
+	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
+	int ret;
+
+	*allocated = false;
+	if (ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk))
+		return 0;
+
+	if (ext4_es_scan_clu(inode, &ext4_es_is_mapped, lblk))
+		goto allocated;
+
+	ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk));
+	if (ret < 0)
+		return ret;
+	if (ret == 0)
+		return 1;
+allocated:
+	*allocated = true;
+	return 0;
+}
+
 /*
  * ext4_insert_delayed_block - adds a delayed block to the extents status
  *                             tree, incrementing the reserved cluster/block
@@ -1682,23 +1710,13 @@  static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
 		if (ret != 0)   /* ENOSPC */
 			return ret;
 	} else {   /* bigalloc */
-		if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
-			if (!ext4_es_scan_clu(inode,
-					      &ext4_es_is_mapped, lblk)) {
-				ret = ext4_clu_mapped(inode,
-						      EXT4_B2C(sbi, lblk));
-				if (ret < 0)
-					return ret;
-				if (ret == 0) {
-					ret = ext4_da_reserve_space(inode, 1);
-					if (ret != 0)   /* ENOSPC */
-						return ret;
-				} else {
-					allocated = true;
-				}
-			} else {
-				allocated = true;
-			}
+		ret = ext4_da_check_clu_allocated(inode, lblk, &allocated);
+		if (ret < 0)
+			return ret;
+		if (ret > 0) {
+			ret = ext4_da_reserve_space(inode, 1);
+			if (ret != 0)   /* ENOSPC */
+				return ret;
 		}
 	}