Skip to contentSkip to conversation

btrfs: fix trivial -Wshadow warnings

PATCH In review David Sterba 3 participants 3 messages Sep 26, 2025 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

PATCH 1/1 btrfs: fix trivial -Wshadow warnings

When compiling with -Wshadow (also in 'make W=2' build) there are several reports of shadowed variables that seem to be harmless:

- btrfs_do_encoded_write() - we can reuse 'ordered', there's no previous value that would need to be preserved

- scrub_write_endio() - we need a standalone 'i' for bio iteration

- scrub_stripe() - duplicate ret2 for errors that must not overwrite 'ret'

- btrfs_subpage_set_writeback() - 'flags' is used for another irqsave lock but is not overwritten when reused for xarray due to scoping, but for clarity let's rename it

- process_dir_items_leaf() - duplicate 'ret', used only for immediate checks

---
 fs/btrfs/inode.c    | 2 --
 fs/btrfs/scrub.c    | 4 +---
 fs/btrfs/subpage.c  | 6 +++---
 fs/btrfs/tree-log.c | 3 ---
 4 files changed, 4 insertions(+), 11 deletions(-)
fs/btrfs/inode.c +0 -2
index 02cb081697fea4..ac2fd589697da4 100644
@@ -9822,8 +9822,6 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
 	}
 
 	for (;;) {
-		struct btrfs_ordered_extent *ordered;
-
 		ret = btrfs_wait_ordered_range(inode, start, num_bytes);
 		if (ret)
 			goto out_folios;
fs/btrfs/scrub.c +1 -3
index 4691d0bdb2e86c..d450ccd3fc7c85 100644
@@ -1284,7 +1284,7 @@ static void scrub_write_endio(struct btrfs_bio *bbio)
 		bitmap_set(&stripe->write_error_bitmap, sector_nr,
 			   bio_size >> fs_info->sectorsize_bits);
 		spin_unlock_irqrestore(&stripe->write_error_lock, flags);
-		for (int i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
+		for (i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
 			btrfs_dev_stat_inc_and_print(stripe->dev,
 						     BTRFS_DEV_STAT_WRITE_ERRS);
 	}
@@ -2527,8 +2527,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
 	}
 
 	if (sctx->is_dev_replace && ret >= 0) {
-		int ret2;
-
 		ret2 = sync_write_pointer_for_zoned(sctx,
 				chunk_logical + offset,
 				map->stripes[stripe_index].physical,
fs/btrfs/subpage.c +3 -3
index 5ca8d4db67220c..01bf58fa92aa2e 100644
@@ -460,12 +460,12 @@ void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,
 	if (!folio_test_dirty(folio)) {
 		struct address_space *mapping = folio_mapping(folio);
 		XA_STATE(xas, &mapping->i_pages, folio->index);
-		unsigned long flags;
+		unsigned long xa_flags;
 
-		xas_lock_irqsave(&xas, flags);
+		xas_lock_irqsave(&xas, xa_flags);
 		xas_load(&xas);
 		xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE);
-		xas_unlock_irqrestore(&xas, flags);
+		xas_unlock_irqrestore(&xas, xa_flags);
 	}
 	spin_unlock_irqrestore(&bfs->lock, flags);
 }
fs/btrfs/tree-log.c +0 -3
index 6aad6b65522b21..08d72506a3d2fb 100644
@@ -4154,7 +4154,6 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
 	for (int i = path->slots[0]; i < nritems; i++) {
 		struct btrfs_dir_item *di;
 		struct btrfs_key key;
-		int ret;
 
 		btrfs_item_key_to_cpu(src, &key, i);
 
@@ -4224,8 +4223,6 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
 	}
 
 	if (batch_size > 0) {
-		int ret;
-
 		ret = flush_dir_items_batch(trans, inode, src, dst_path,
 					    batch_start, batch_size);
 		if (ret < 0)
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · David Sterba

When compiling with -Wshadow (also in 'make W=2' build) there are several reports of shadowed variables that seem to be harmless:

- btrfs_do_encoded_write() - we can reuse 'ordered', there's no previous value that would need to be preserved

- scrub_write_endio() - we need a standalone 'i' for bio iteration

- scrub_stripe() - duplicate ret2 for errors that must not overwrite 'ret'

Quoted 7 lines

- btrfs_subpage_set_writeback() - 'flags' is used for another irqsave lock but is not overwritten when reused for xarray due to scoping, but for clarity let's rename it

- process_dir_items_leaf() - duplicate 'ret', used only for immediate checks

Although I'm still not 100% sure if using the variable in the function scope is really any safer.

The last call site is a good example, it's only safe to use @ret from the function scope as we immediately return after an error.

If we use @ret to record some error but not immediately return, then we can override the previous error code and cause problems.

I'd prefer to always use the local variable, and remove the one in the larger scope, but unfortunately all call sites have directly usage inside the function...

Thanks,
Qu

Quoted 83 lines
---
  fs/btrfs/inode.c    | 2 --
  fs/btrfs/scrub.c    | 4 +---
  fs/btrfs/subpage.c  | 6 +++---
  fs/btrfs/tree-log.c | 3 ---
  4 files changed, 4 insertions(+), 11 deletions(-)
fs/btrfs/inode.c +0 -2
index 02cb081697fea4..ac2fd589697da4 100644
@@ -9822,8 +9822,6 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
  	}
 
  	for (;;) {
-		struct btrfs_ordered_extent *ordered;
-
  		ret = btrfs_wait_ordered_range(inode, start, num_bytes);
  		if (ret)
  			goto out_folios;
fs/btrfs/scrub.c +1 -3
index 4691d0bdb2e86c..d450ccd3fc7c85 100644
@@ -1284,7 +1284,7 @@ static void scrub_write_endio(struct btrfs_bio *bbio)
  		bitmap_set(&stripe->write_error_bitmap, sector_nr,
  			   bio_size >> fs_info->sectorsize_bits);
  		spin_unlock_irqrestore(&stripe->write_error_lock, flags);
-		for (int i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
+		for (i = 0; i < (bio_size >> fs_info->sectorsize_bits); i++)
  			btrfs_dev_stat_inc_and_print(stripe->dev,
  						     BTRFS_DEV_STAT_WRITE_ERRS);
  	}
@@ -2527,8 +2527,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
  	}
 
  	if (sctx->is_dev_replace && ret >= 0) {
-		int ret2;
-
  		ret2 = sync_write_pointer_for_zoned(sctx,
  				chunk_logical + offset,
  				map->stripes[stripe_index].physical,
fs/btrfs/subpage.c +3 -3
index 5ca8d4db67220c..01bf58fa92aa2e 100644
@@ -460,12 +460,12 @@ void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,
  	if (!folio_test_dirty(folio)) {
  		struct address_space *mapping = folio_mapping(folio);
  		XA_STATE(xas, &mapping->i_pages, folio->index);
-		unsigned long flags;
+		unsigned long xa_flags;
 
-		xas_lock_irqsave(&xas, flags);
+		xas_lock_irqsave(&xas, xa_flags);
  		xas_load(&xas);
  		xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE);
-		xas_unlock_irqrestore(&xas, flags);
+		xas_unlock_irqrestore(&xas, xa_flags);
  	}
  	spin_unlock_irqrestore(&bfs->lock, flags);
  }
fs/btrfs/tree-log.c +0 -3
index 6aad6b65522b21..08d72506a3d2fb 100644
@@ -4154,7 +4154,6 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
  	for (int i = path->slots[0]; i < nritems; i++) {
  		struct btrfs_dir_item *di;
  		struct btrfs_key key;
-		int ret;
 
  		btrfs_item_key_to_cpu(src, &key, i);
 
@@ -4224,8 +4223,6 @@ static int process_dir_items_leaf(struct btrfs_trans_handle *trans,
  	}
 
  	if (batch_size > 0) {
-		int ret;
-
  		ret = flush_dir_items_batch(trans, inode, src, dst_path,
  					    batch_start, batch_size);
  		if (ret < 0)
#2 lore raw
Quoted 21 lines · Qu Wenruo
Quoted 10 lines · David Sterba

When compiling with -Wshadow (also in 'make W=2' build) there are several reports of shadowed variables that seem to be harmless:

- btrfs_do_encoded_write() - we can reuse 'ordered', there's no previous value that would need to be preserved

- scrub_write_endio() - we need a standalone 'i' for bio iteration

- scrub_stripe() - duplicate ret2 for errors that must not overwrite 'ret'

Quoted 7 lines

- btrfs_subpage_set_writeback() - 'flags' is used for another irqsave lock but is not overwritten when reused for xarray due to scoping, but for clarity let's rename it

- process_dir_items_leaf() - duplicate 'ret', used only for immediate checks

Quoted 7 lines

Although I'm still not 100% sure if using the variable in the function scope is really any safer.

The last call site is a good example, it's only safe to use @ret from the function scope as we immediately return after an error.

Yes, either that or we 'goto out' and do not use 'ret' anymore other than 'return ret'.

Quoted 6 lines

If we use @ret to record some error but not immediately return, then we can override the previous error code and cause problems.

I'd prefer to always use the local variable, and remove the one in the larger scope, but unfortunately all call sites have directly usage inside the function...

If we can't use the function scope ret then we've been using ret2, otherwise one 'ret' for any use is way more common. I don't think we should declare a shadowing ret in the nested scopes.