All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: use shifts for sectorsize and nodesize
@ 2026-05-27 11:16 David Sterba
  2026-05-27 16:04 ` Boris Burkov
  2026-05-27 21:45 ` Qu Wenruo
  0 siblings, 2 replies; 4+ messages in thread
From: David Sterba @ 2026-05-27 11:16 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Convert more multiplications of sectorsize or nodesize to use the
shifts. The remaining cases are multiplications by constants that
compiler can optimize by itself, and in tests.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/delalloc-space.c | 4 ++--
 fs/btrfs/disk-io.c        | 2 +-
 fs/btrfs/file-item.c      | 2 +-
 fs/btrfs/relocation.c     | 4 ++--
 fs/btrfs/transaction.c    | 2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
index 0970799d0aa443..5d5ff5a0280844 100644
--- a/fs/btrfs/delalloc-space.c
+++ b/fs/btrfs/delalloc-space.c
@@ -279,7 +279,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
 	 *
 	 * This is overestimating in most cases.
 	 */
-	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
+	qgroup_rsv_size = ((u64)outstanding_extents << fs_info->nodesize_bits);
 
 	spin_lock(&block_rsv->lock);
 	block_rsv->size = reserve_size;
@@ -309,7 +309,7 @@ static void calc_inode_reservations(struct btrfs_inode *inode,
 	 * for an inode update.
 	 */
 	*meta_reserve += inode_update;
-	*qgroup_reserve = nr_extents * fs_info->nodesize;
+	*qgroup_reserve = (nr_extents << fs_info->nodesize_bits);
 }
 
 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index e6ed52f5cd6d99..590da1ba5de485 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -563,7 +563,7 @@ static bool btree_dirty_folio(struct address_space *mapping,
 			continue;
 		}
 		spin_unlock_irqrestore(&subpage->lock, flags);
-		cur = page_start + cur_bit * fs_info->sectorsize;
+		cur = page_start + (cur_bit << fs_info->sectorsize_bits);
 
 		eb = find_extent_buffer(fs_info, cur);
 		ASSERT(eb);
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 82ae4a2afd3471..9f6454e9db8186 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -1309,7 +1309,7 @@ int btrfs_insert_data_csums(struct btrfs_trans_handle *trans,
 
 	index += ins_size;
 	ins_size /= csum_size;
-	total_bytes += ins_size * fs_info->sectorsize;
+	total_bytes += (ins_size << fs_info->sectorsize_bits);
 
 	if (total_bytes < sums->len) {
 		btrfs_release_path(path);
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 67af02e732d0ce..8212abc2d13652 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1560,7 +1560,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
 	 * and * 2 since we have two trees to COW.
 	 */
 	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
-	min_reserved = fs_info->nodesize * reserve_level * 2;
+	min_reserved = (reserve_level << fs_info->nodesize_bits) * 2;
 	memset(&next_key, 0, sizeof(next_key));
 
 	while (1) {
@@ -2558,7 +2558,7 @@ static int relocate_cowonly_block(struct btrfs_trans_handle *trans,
 
 	nr_levels = max(btrfs_header_level(root->node) - block->level, 0) + 1;
 
-	num_bytes = fs_info->nodesize * nr_levels;
+	num_bytes = (nr_levels << fs_info->nodesize_bits);
 	ret = refill_metadata_space(trans, rc, num_bytes);
 	if (ret) {
 		btrfs_put_root(root);
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index a289a8fa237c8a..2263cb6bd8ca47 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -631,7 +631,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
 	 * the appropriate flushing if need be.
 	 */
 	if (num_items && root != fs_info->chunk_root) {
-		qgroup_reserved = num_items * fs_info->nodesize;
+		qgroup_reserved = (num_items << fs_info->nodesize_bits);
 		/*
 		 * Use prealloc for now, as there might be a currently running
 		 * transaction that could free this reserved space prematurely
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: use shifts for sectorsize and nodesize
  2026-05-27 11:16 [PATCH] btrfs: use shifts for sectorsize and nodesize David Sterba
@ 2026-05-27 16:04 ` Boris Burkov
  2026-05-27 18:50   ` David Sterba
  2026-05-27 21:45 ` Qu Wenruo
  1 sibling, 1 reply; 4+ messages in thread
From: Boris Burkov @ 2026-05-27 16:04 UTC (permalink / raw)
  To: David Sterba; +Cc: linux-btrfs

On Wed, May 27, 2026 at 01:16:52PM +0200, David Sterba wrote:
> Convert more multiplications of sectorsize or nodesize to use the
> shifts. The remaining cases are multiplications by constants that
> compiler can optimize by itself, and in tests.
> 
Reviewed-by: Boris Burkov <boris@bur.io>
> Signed-off-by: David Sterba <dsterba@suse.com>
> ---
>  fs/btrfs/delalloc-space.c | 4 ++--
>  fs/btrfs/disk-io.c        | 2 +-
>  fs/btrfs/file-item.c      | 2 +-
>  fs/btrfs/relocation.c     | 4 ++--
>  fs/btrfs/transaction.c    | 2 +-
>  5 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
> index 0970799d0aa443..5d5ff5a0280844 100644
> --- a/fs/btrfs/delalloc-space.c
> +++ b/fs/btrfs/delalloc-space.c
> @@ -279,7 +279,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
>  	 *
>  	 * This is overestimating in most cases.
>  	 */
> -	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
> +	qgroup_rsv_size = ((u64)outstanding_extents << fs_info->nodesize_bits);
>  
>  	spin_lock(&block_rsv->lock);
>  	block_rsv->size = reserve_size;
> @@ -309,7 +309,7 @@ static void calc_inode_reservations(struct btrfs_inode *inode,
>  	 * for an inode update.
>  	 */
>  	*meta_reserve += inode_update;
> -	*qgroup_reserve = nr_extents * fs_info->nodesize;
> +	*qgroup_reserve = (nr_extents << fs_info->nodesize_bits);
>  }
>  
>  int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index e6ed52f5cd6d99..590da1ba5de485 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -563,7 +563,7 @@ static bool btree_dirty_folio(struct address_space *mapping,
>  			continue;
>  		}
>  		spin_unlock_irqrestore(&subpage->lock, flags);
> -		cur = page_start + cur_bit * fs_info->sectorsize;
> +		cur = page_start + (cur_bit << fs_info->sectorsize_bits);
>  
>  		eb = find_extent_buffer(fs_info, cur);
>  		ASSERT(eb);
> diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
> index 82ae4a2afd3471..9f6454e9db8186 100644
> --- a/fs/btrfs/file-item.c
> +++ b/fs/btrfs/file-item.c
> @@ -1309,7 +1309,7 @@ int btrfs_insert_data_csums(struct btrfs_trans_handle *trans,
>  
>  	index += ins_size;
>  	ins_size /= csum_size;
> -	total_bytes += ins_size * fs_info->sectorsize;
> +	total_bytes += (ins_size << fs_info->sectorsize_bits);
>  
>  	if (total_bytes < sums->len) {
>  		btrfs_release_path(path);
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index 67af02e732d0ce..8212abc2d13652 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -1560,7 +1560,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
>  	 * and * 2 since we have two trees to COW.
>  	 */
>  	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
> -	min_reserved = fs_info->nodesize * reserve_level * 2;
> +	min_reserved = (reserve_level << fs_info->nodesize_bits) * 2;

Is it nicer to just add 1 to the shift operand?

>  	memset(&next_key, 0, sizeof(next_key));
>  
>  	while (1) {
> @@ -2558,7 +2558,7 @@ static int relocate_cowonly_block(struct btrfs_trans_handle *trans,
>  
>  	nr_levels = max(btrfs_header_level(root->node) - block->level, 0) + 1;
>  
> -	num_bytes = fs_info->nodesize * nr_levels;
> +	num_bytes = (nr_levels << fs_info->nodesize_bits);
>  	ret = refill_metadata_space(trans, rc, num_bytes);
>  	if (ret) {
>  		btrfs_put_root(root);
> diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
> index a289a8fa237c8a..2263cb6bd8ca47 100644
> --- a/fs/btrfs/transaction.c
> +++ b/fs/btrfs/transaction.c
> @@ -631,7 +631,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
>  	 * the appropriate flushing if need be.
>  	 */
>  	if (num_items && root != fs_info->chunk_root) {
> -		qgroup_reserved = num_items * fs_info->nodesize;
> +		qgroup_reserved = (num_items << fs_info->nodesize_bits);
>  		/*
>  		 * Use prealloc for now, as there might be a currently running
>  		 * transaction that could free this reserved space prematurely
> -- 
> 2.54.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: use shifts for sectorsize and nodesize
  2026-05-27 16:04 ` Boris Burkov
@ 2026-05-27 18:50   ` David Sterba
  0 siblings, 0 replies; 4+ messages in thread
From: David Sterba @ 2026-05-27 18:50 UTC (permalink / raw)
  To: Boris Burkov; +Cc: David Sterba, linux-btrfs

On Wed, May 27, 2026 at 09:04:09AM -0700, Boris Burkov wrote:
> > --- a/fs/btrfs/relocation.c
> > +++ b/fs/btrfs/relocation.c
> > @@ -1560,7 +1560,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
> >  	 * and * 2 since we have two trees to COW.
> >  	 */
> >  	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
> > -	min_reserved = fs_info->nodesize * reserve_level * 2;
> > +	min_reserved = (reserve_level << fs_info->nodesize_bits) * 2;
> 
> Is it nicer to just add 1 to the shift operand?

There's fragment of the comment in the diff context, in full

        /*
         * In merge_reloc_root(), we modify the upper level pointer to swap the
         * tree blocks between reloc tree and subvolume tree.  Thus for tree
         * block COW, we COW at most from level 1 to root level for each tree.
         *
         * Thus the needed metadata size is at most root_level * nodesize,
         * and * 2 since we have two trees to COW.
         */

so the "* 2" matches it.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: use shifts for sectorsize and nodesize
  2026-05-27 11:16 [PATCH] btrfs: use shifts for sectorsize and nodesize David Sterba
  2026-05-27 16:04 ` Boris Burkov
@ 2026-05-27 21:45 ` Qu Wenruo
  1 sibling, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-05-27 21:45 UTC (permalink / raw)
  To: David Sterba, linux-btrfs



在 2026/5/27 20:46, David Sterba 写道:
> Convert more multiplications of sectorsize or nodesize to use the
> shifts. The remaining cases are multiplications by constants that
> compiler can optimize by itself, and in tests.
> 
> Signed-off-by: David Sterba <dsterba@suse.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>   fs/btrfs/delalloc-space.c | 4 ++--
>   fs/btrfs/disk-io.c        | 2 +-
>   fs/btrfs/file-item.c      | 2 +-
>   fs/btrfs/relocation.c     | 4 ++--
>   fs/btrfs/transaction.c    | 2 +-
>   5 files changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/delalloc-space.c b/fs/btrfs/delalloc-space.c
> index 0970799d0aa443..5d5ff5a0280844 100644
> --- a/fs/btrfs/delalloc-space.c
> +++ b/fs/btrfs/delalloc-space.c
> @@ -279,7 +279,7 @@ static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
>   	 *
>   	 * This is overestimating in most cases.
>   	 */
> -	qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
> +	qgroup_rsv_size = ((u64)outstanding_extents << fs_info->nodesize_bits);
>   
>   	spin_lock(&block_rsv->lock);
>   	block_rsv->size = reserve_size;
> @@ -309,7 +309,7 @@ static void calc_inode_reservations(struct btrfs_inode *inode,
>   	 * for an inode update.
>   	 */
>   	*meta_reserve += inode_update;
> -	*qgroup_reserve = nr_extents * fs_info->nodesize;
> +	*qgroup_reserve = (nr_extents << fs_info->nodesize_bits);
>   }
>   
>   int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes,
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index e6ed52f5cd6d99..590da1ba5de485 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -563,7 +563,7 @@ static bool btree_dirty_folio(struct address_space *mapping,
>   			continue;
>   		}
>   		spin_unlock_irqrestore(&subpage->lock, flags);
> -		cur = page_start + cur_bit * fs_info->sectorsize;
> +		cur = page_start + (cur_bit << fs_info->sectorsize_bits);
>   
>   		eb = find_extent_buffer(fs_info, cur);
>   		ASSERT(eb);
> diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
> index 82ae4a2afd3471..9f6454e9db8186 100644
> --- a/fs/btrfs/file-item.c
> +++ b/fs/btrfs/file-item.c
> @@ -1309,7 +1309,7 @@ int btrfs_insert_data_csums(struct btrfs_trans_handle *trans,
>   
>   	index += ins_size;
>   	ins_size /= csum_size;
> -	total_bytes += ins_size * fs_info->sectorsize;
> +	total_bytes += (ins_size << fs_info->sectorsize_bits);
>   
>   	if (total_bytes < sums->len) {
>   		btrfs_release_path(path);
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index 67af02e732d0ce..8212abc2d13652 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -1560,7 +1560,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
>   	 * and * 2 since we have two trees to COW.
>   	 */
>   	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
> -	min_reserved = fs_info->nodesize * reserve_level * 2;
> +	min_reserved = (reserve_level << fs_info->nodesize_bits) * 2;
>   	memset(&next_key, 0, sizeof(next_key));
>   
>   	while (1) {
> @@ -2558,7 +2558,7 @@ static int relocate_cowonly_block(struct btrfs_trans_handle *trans,
>   
>   	nr_levels = max(btrfs_header_level(root->node) - block->level, 0) + 1;
>   
> -	num_bytes = fs_info->nodesize * nr_levels;
> +	num_bytes = (nr_levels << fs_info->nodesize_bits);
>   	ret = refill_metadata_space(trans, rc, num_bytes);
>   	if (ret) {
>   		btrfs_put_root(root);
> diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
> index a289a8fa237c8a..2263cb6bd8ca47 100644
> --- a/fs/btrfs/transaction.c
> +++ b/fs/btrfs/transaction.c
> @@ -631,7 +631,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
>   	 * the appropriate flushing if need be.
>   	 */
>   	if (num_items && root != fs_info->chunk_root) {
> -		qgroup_reserved = num_items * fs_info->nodesize;
> +		qgroup_reserved = (num_items << fs_info->nodesize_bits);
>   		/*
>   		 * Use prealloc for now, as there might be a currently running
>   		 * transaction that could free this reserved space prematurely


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-05-27 21:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-27 11:16 [PATCH] btrfs: use shifts for sectorsize and nodesize David Sterba
2026-05-27 16:04 ` Boris Burkov
2026-05-27 18:50   ` David Sterba
2026-05-27 21:45 ` Qu Wenruo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.