diff mbox series

[10/25] ext4: add EXT4_LBLK_TO_P and EXT4_P_TO_LBLK for block/page conversion

Message ID 20251025032221.2905818-11-libaokun@huaweicloud.com
State Superseded
Headers show
Series ext4: enable block size larger than page size | expand

Commit Message

Baokun Li Oct. 25, 2025, 3:22 a.m. UTC
From: Baokun Li <libaokun1@huawei.com>

As BS > PS support is coming, all block number to page index (and
vice-versa) conversions must now go via bytes. Added EXT4_LBLK_TO_P()
and EXT4_P_TO_LBLK() macros to simplify these conversions and handle
both BS <= PS and BS > PS scenarios cleanly.

Signed-off-by: Baokun Li <libaokun1@huawei.com>
Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
---
 fs/ext4/ext4.h | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Jan Kara Nov. 3, 2025, 8:26 a.m. UTC | #1
On Sat 25-10-25 11:22:06, libaokun@huaweicloud.com wrote:
> From: Baokun Li <libaokun1@huawei.com>
> 
> As BS > PS support is coming, all block number to page index (and
> vice-versa) conversions must now go via bytes. Added EXT4_LBLK_TO_P()
> and EXT4_P_TO_LBLK() macros to simplify these conversions and handle
> both BS <= PS and BS > PS scenarios cleanly.
> 
> Signed-off-by: Baokun Li <libaokun1@huawei.com>
> Reviewed-by: Zhang Yi <yi.zhang@huawei.com>

'P' in the macro names seems too terse :). I'd probably use PG to give a
better hint this is about pages? So EXT4_LBLK_TO_PG() and
EXT4_PG_TO_LBLK(). BTW, patch 8 could already use these macros...

								Honza

> ---
>  fs/ext4/ext4.h | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index 9b236f620b3a..8223ed29b343 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
> @@ -369,6 +369,12 @@ struct ext4_io_submit {
>  	(round_up((offset), i_blocksize(inode)) >> (inode)->i_blkbits)
>  #define EXT4_LBLK_TO_B(inode, lblk) ((loff_t)(lblk) << (inode)->i_blkbits)
>  
> +/* Translate a block number to a page index */
> +#define EXT4_LBLK_TO_P(inode, lblk)	(EXT4_LBLK_TO_B((inode), (lblk)) >> \
> +					 PAGE_SHIFT)
> +/* Translate a page index to a block number */
> +#define EXT4_P_TO_LBLK(inode, pnum)	(((loff_t)(pnum) << PAGE_SHIFT) >> \
> +					 (inode)->i_blkbits)
>  /* Translate a block number to a cluster number */
>  #define EXT4_B2C(sbi, blk)	((blk) >> (sbi)->s_cluster_bits)
>  /* Translate a cluster number to a block number */
> -- 
> 2.46.1
>
Baokun Li Nov. 3, 2025, 2:45 p.m. UTC | #2
On 2025-11-03 16:26, Jan Kara wrote:
> On Sat 25-10-25 11:22:06, libaokun@huaweicloud.com wrote:
>> From: Baokun Li <libaokun1@huawei.com>
>>
>> As BS > PS support is coming, all block number to page index (and
>> vice-versa) conversions must now go via bytes. Added EXT4_LBLK_TO_P()
>> and EXT4_P_TO_LBLK() macros to simplify these conversions and handle
>> both BS <= PS and BS > PS scenarios cleanly.
>>
>> Signed-off-by: Baokun Li <libaokun1@huawei.com>
>> Reviewed-by: Zhang Yi <yi.zhang@huawei.com>
> 'P' in the macro names seems too terse :). I'd probably use PG to give a
> better hint this is about pages? So EXT4_LBLK_TO_PG() and
> EXT4_PG_TO_LBLK(). 

Indeed, EXT4_LBLK_TO_PG reads much clearer. I will use it in v2.

> BTW, patch 8 could already use these macros...
>
> 								Honza

In Patch 8, the conversion is for a physical block number, which has a
different variable type than lblk. Since this is the only location where
this conversion is used in the code, I made a dedicated modification there.

Thank you for your review!


Cheers,
Baokun

>> ---
>>  fs/ext4/ext4.h | 6 ++++++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
>> index 9b236f620b3a..8223ed29b343 100644
>> --- a/fs/ext4/ext4.h
>> +++ b/fs/ext4/ext4.h
>> @@ -369,6 +369,12 @@ struct ext4_io_submit {
>>  	(round_up((offset), i_blocksize(inode)) >> (inode)->i_blkbits)
>>  #define EXT4_LBLK_TO_B(inode, lblk) ((loff_t)(lblk) << (inode)->i_blkbits)
>>  
>> +/* Translate a block number to a page index */
>> +#define EXT4_LBLK_TO_P(inode, lblk)	(EXT4_LBLK_TO_B((inode), (lblk)) >> \
>> +					 PAGE_SHIFT)
>> +/* Translate a page index to a block number */
>> +#define EXT4_P_TO_LBLK(inode, pnum)	(((loff_t)(pnum) << PAGE_SHIFT) >> \
>> +					 (inode)->i_blkbits)
>>  /* Translate a block number to a cluster number */
>>  #define EXT4_B2C(sbi, blk)	((blk) >> (sbi)->s_cluster_bits)
>>  /* Translate a cluster number to a block number */
>> -- 
>> 2.46.1
>>
Jan Kara Nov. 5, 2025, 8:27 a.m. UTC | #3
On Mon 03-11-25 22:45:45, Baokun Li wrote:
> On 2025-11-03 16:26, Jan Kara wrote:
> > On Sat 25-10-25 11:22:06, libaokun@huaweicloud.com wrote:
> > BTW, patch 8 could already use these macros...
> >
> > 								Honza
> 
> In Patch 8, the conversion is for a physical block number, which has a
> different variable type than lblk. Since this is the only location where
> this conversion is used in the code, I made a dedicated modification there.

Ok, fair.

								Honza
diff mbox series

Patch

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 9b236f620b3a..8223ed29b343 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -369,6 +369,12 @@  struct ext4_io_submit {
 	(round_up((offset), i_blocksize(inode)) >> (inode)->i_blkbits)
 #define EXT4_LBLK_TO_B(inode, lblk) ((loff_t)(lblk) << (inode)->i_blkbits)
 
+/* Translate a block number to a page index */
+#define EXT4_LBLK_TO_P(inode, lblk)	(EXT4_LBLK_TO_B((inode), (lblk)) >> \
+					 PAGE_SHIFT)
+/* Translate a page index to a block number */
+#define EXT4_P_TO_LBLK(inode, pnum)	(((loff_t)(pnum) << PAGE_SHIFT) >> \
+					 (inode)->i_blkbits)
 /* Translate a block number to a cluster number */
 #define EXT4_B2C(sbi, blk)	((blk) >> (sbi)->s_cluster_bits)
 /* Translate a cluster number to a block number */