mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yu Kuai <yukuai1@huaweicloud.com>
To: Zheng Qixing <zhengqixing@huaweicloud.com>,
	axboe@kernel.dk, song@kernel.org, colyli@kernel.org,
	dan.j.williams@intel.com, vishal.l.verma@intel.com,
	dave.jiang@intel.com, ira.weiny@intel.com, dlemoal@kernel.org,
	yanjun.zhu@linux.dev, kch@nvidia.com, hare@suse.de,
	zhengqixing@huawei.com, john.g.garry@oracle.com,
	geliang@kernel.org, xni@redhat.com, colyli@suse.de
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-raid@vger.kernel.org, nvdimm@lists.linux.dev,
	yi.zhang@huawei.com, yangerkun@huawei.com,
	"yukuai (C)" <yukuai3@huawei.com>
Subject: Re: [PATCH 10/12] badblocks: return boolen from badblocks_set() and badblocks_clear()
Date: Sat, 22 Feb 2025 09:26:20 +0800	[thread overview]
Message-ID: <fe2dedca-0b71-3c80-6958-4bca61707fcc@huaweicloud.com> (raw)
In-Reply-To: <20250221081109.734170-11-zhengqixing@huaweicloud.com>

Hi,

Just two simple coding styes below.

在 2025/02/21 16:11, Zheng Qixing 写道:
> From: Zheng Qixing <zhengqixing@huawei.com>
> 
> Change the return type of badblocks_set() and badblocks_clear()
> from int to bool, indicating success or failure. Specifically:
> 
> - _badblocks_set() and _badblocks_clear() functions now return
> true for success and false for failure.
> - All calls to these functions have been updated to handle the
> new boolean return type.
> - This change improves code clarity and ensures a more consistent
> handling of success and failure states.
> 
> Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
> ---
>   block/badblocks.c             | 37 +++++++++++++++++------------------
>   drivers/block/null_blk/main.c | 17 ++++++++--------
>   drivers/md/md.c               | 35 +++++++++++++++++----------------
>   drivers/nvdimm/badrange.c     |  2 +-
>   include/linux/badblocks.h     |  6 +++---
>   5 files changed, 49 insertions(+), 48 deletions(-)
> 
> diff --git a/block/badblocks.c b/block/badblocks.c
> index 79d91be468c4..8f057563488a 100644
> --- a/block/badblocks.c
> +++ b/block/badblocks.c
> @@ -836,8 +836,8 @@ static bool try_adjacent_combine(struct badblocks *bb, int prev)
>   }
>   
>   /* Do exact work to set bad block range into the bad block table */
> -static int _badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> -			  int acknowledged)
> +static bool _badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> +			   int acknowledged)
>   {
>   	int len = 0, added = 0;
>   	struct badblocks_context bad;
> @@ -847,11 +847,11 @@ static int _badblocks_set(struct badblocks *bb, sector_t s, int sectors,
>   
>   	if (bb->shift < 0)
>   		/* badblocks are disabled */
> -		return 1;
> +		return false;
>   
>   	if (sectors == 0)
>   		/* Invalid sectors number */
> -		return 1;
> +		return false;
>   
>   	if (bb->shift) {
>   		/* round the start down, and the end up */
> @@ -977,7 +977,7 @@ static int _badblocks_set(struct badblocks *bb, sector_t s, int sectors,
>   
>   	write_sequnlock_irqrestore(&bb->lock, flags);
>   
> -	return sectors;
> +	return sectors == 0;
>   }
>   
>   /*
> @@ -1048,21 +1048,20 @@ static int front_splitting_clear(struct badblocks *bb, int prev,
>   }
>   
>   /* Do the exact work to clear bad block range from the bad block table */
> -static int _badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
> +static bool _badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
>   {
>   	struct badblocks_context bad;
>   	int prev = -1, hint = -1;
>   	int len = 0, cleared = 0;
> -	int rv = 0;
>   	u64 *p;
>   
>   	if (bb->shift < 0)
>   		/* badblocks are disabled */
> -		return 1;
> +		return false;
>   
>   	if (sectors == 0)
>   		/* Invalid sectors number */
> -		return 1;
> +		return false;
>   
>   	if (bb->shift) {
>   		sector_t target;
> @@ -1182,9 +1181,9 @@ static int _badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
>   	write_sequnlock_irq(&bb->lock);
>   
>   	if (!cleared)
> -		rv = 1;
> +		return false;
>   
> -	return rv;
> +	return true;
>   }
>   
>   /* Do the exact work to check bad blocks range from the bad block table */
> @@ -1338,11 +1337,11 @@ EXPORT_SYMBOL_GPL(badblocks_check);
>    * decide how best to handle it.
>    *
>    * Return:
> - *  0: success
> - *  other: failed to set badblocks (out of space)
> + *  true: success
> + *  false: failed to set badblocks (out of space)
>    */
> -int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> -			int acknowledged)
> +bool badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> +		   int acknowledged)
>   {
>   	return _badblocks_set(bb, s, sectors, acknowledged);
>   }
> @@ -1359,10 +1358,10 @@ EXPORT_SYMBOL_GPL(badblocks_set);
>    * drop the remove request.
>    *
>    * Return:
> - *  0: success
> - *  1: failed to clear badblocks
> + *  true: success
> + *  false: failed to clear badblocks
>    */
> -int badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
> +bool badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
>   {
>   	return _badblocks_clear(bb, s, sectors);
>   }
> @@ -1484,7 +1483,7 @@ ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len,
>   		return -EINVAL;
>   	}
>   
> -	if (badblocks_set(bb, sector, length, !unack))
> +	if (!badblocks_set(bb, sector, length, !unack))
>   		return -ENOSPC;
>   	else

Since we're here, can you also remove the else branch as well?

>   		return len;
> diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
> index d94ef37480bd..623db72ad66b 100644
> --- a/drivers/block/null_blk/main.c
> +++ b/drivers/block/null_blk/main.c
> @@ -559,14 +559,15 @@ static ssize_t nullb_device_badblocks_store(struct config_item *item,
>   		goto out;
>   	/* enable badblocks */
>   	cmpxchg(&t_dev->badblocks.shift, -1, 0);
> -	if (buf[0] == '+')
> -		ret = badblocks_set(&t_dev->badblocks, start,
> -			end - start + 1, 1);
> -	else
> -		ret = badblocks_clear(&t_dev->badblocks, start,
> -			end - start + 1);
> -	if (ret == 0)
> -		ret = count;
> +	if (buf[0] == '+') {
> +		if (badblocks_set(&t_dev->badblocks, start,
> +				  end - start + 1, 1))
> +			ret = count;
> +	} else {
> +		if (badblocks_clear(&t_dev->badblocks, start,
> +				    end - start + 1))

else if ()

Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Thanks
> +			ret = count;
> +	}
>   out:
>   	kfree(orig);
>   	return ret;
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 30b3dbbce2d2..49d826e475cb 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -1748,7 +1748,7 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
>   			count <<= sb->bblog_shift;
>   			if (bb + 1 == 0)
>   				break;
> -			if (badblocks_set(&rdev->badblocks, sector, count, 1))
> +			if (!badblocks_set(&rdev->badblocks, sector, count, 1))
>   				return -EINVAL;
>   		}
>   	} else if (sb->bblog_offset != 0)
> @@ -9846,7 +9846,6 @@ int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
>   		       int is_new)
>   {
>   	struct mddev *mddev = rdev->mddev;
> -	int rv;
>   
>   	/*
>   	 * Recording new badblocks for faulty rdev will force unnecessary
> @@ -9862,33 +9861,35 @@ int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
>   		s += rdev->new_data_offset;
>   	else
>   		s += rdev->data_offset;
> -	rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
> -	if (rv == 0) {
> -		/* Make sure they get written out promptly */
> -		if (test_bit(ExternalBbl, &rdev->flags))
> -			sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
> -		sysfs_notify_dirent_safe(rdev->sysfs_state);
> -		set_mask_bits(&mddev->sb_flags, 0,
> -			      BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
> -		md_wakeup_thread(rdev->mddev->thread);
> -		return 1;
> -	} else
> +
> +	if (!badblocks_set(&rdev->badblocks, s, sectors, 0))
>   		return 0;
> +
> +	/* Make sure they get written out promptly */
> +	if (test_bit(ExternalBbl, &rdev->flags))
> +		sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
> +	sysfs_notify_dirent_safe(rdev->sysfs_state);
> +	set_mask_bits(&mddev->sb_flags, 0,
> +		      BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
> +	md_wakeup_thread(rdev->mddev->thread);
> +	return 1;
>   }
>   EXPORT_SYMBOL_GPL(rdev_set_badblocks);
>   
>   int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
>   			 int is_new)
>   {
> -	int rv;
>   	if (is_new)
>   		s += rdev->new_data_offset;
>   	else
>   		s += rdev->data_offset;
> -	rv = badblocks_clear(&rdev->badblocks, s, sectors);
> -	if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
> +
> +	if (!badblocks_clear(&rdev->badblocks, s, sectors))
> +		return 0;
> +
> +	if (test_bit(ExternalBbl, &rdev->flags))
>   		sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
> -	return rv;
> +	return 1;
>   }
>   EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
>   
> diff --git a/drivers/nvdimm/badrange.c b/drivers/nvdimm/badrange.c
> index a002ea6fdd84..ee478ccde7c6 100644
> --- a/drivers/nvdimm/badrange.c
> +++ b/drivers/nvdimm/badrange.c
> @@ -167,7 +167,7 @@ static void set_badblock(struct badblocks *bb, sector_t s, int num)
>   	dev_dbg(bb->dev, "Found a bad range (0x%llx, 0x%llx)\n",
>   			(u64) s * 512, (u64) num * 512);
>   	/* this isn't an error as the hardware will still throw an exception */
> -	if (badblocks_set(bb, s, num, 1))
> +	if (!badblocks_set(bb, s, num, 1))
>   		dev_info_once(bb->dev, "%s: failed for sector %llx\n",
>   				__func__, (u64) s);
>   }
> diff --git a/include/linux/badblocks.h b/include/linux/badblocks.h
> index 670f2dae692f..8764bed9ff16 100644
> --- a/include/linux/badblocks.h
> +++ b/include/linux/badblocks.h
> @@ -50,9 +50,9 @@ struct badblocks_context {
>   
>   int badblocks_check(struct badblocks *bb, sector_t s, int sectors,
>   		   sector_t *first_bad, int *bad_sectors);
> -int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> -			int acknowledged);
> -int badblocks_clear(struct badblocks *bb, sector_t s, int sectors);
> +bool badblocks_set(struct badblocks *bb, sector_t s, int sectors,
> +		   int acknowledged);
> +bool badblocks_clear(struct badblocks *bb, sector_t s, int sectors);
>   void ack_all_badblocks(struct badblocks *bb);
>   ssize_t badblocks_show(struct badblocks *bb, char *page, int unack);
>   ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len,
> 


  reply	other threads:[~2025-02-22  1:26 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-21  8:10 [PATCH 00/12] badblocks: bugfix and cleanup for badblocks Zheng Qixing
2025-02-21  8:10 ` [PATCH 01/12] badblocks: Fix error shitf ops Zheng Qixing
2025-02-21  9:05   ` Yu Kuai
2025-02-21  9:58   ` Coly Li
2025-02-21  8:10 ` [PATCH 02/12] badblocks: factor out a helper try_adjacent_combine Zheng Qixing
2025-02-21  9:07   ` Yu Kuai
2025-02-21 10:04   ` Coly Li
2025-02-21 10:06     ` Coly Li
2025-02-21  8:11 ` [PATCH 03/12] badblocks: attempt to merge adjacent badblocks during ack_all_badblocks Zheng Qixing
2025-02-21  9:12   ` Yu Kuai
2025-02-21 10:08   ` Coly Li
2025-02-21  8:11 ` [PATCH 04/12] badblocks: return error directly when setting badblocks exceeds 512 Zheng Qixing
2025-02-21  9:55   ` Yu Kuai
2025-02-25  9:13     ` Li Nan
2025-02-21  8:11 ` [PATCH 05/12] badblocks: return error if any badblock set fails Zheng Qixing
2025-02-21  9:52   ` Coly Li
2025-02-21 10:09     ` Yu Kuai
2025-02-21 10:12       ` Coly Li
2025-02-22  1:12         ` Yu Kuai
2025-02-22  6:16           ` Coly Li
2025-02-25  9:12             ` Li Nan
2025-02-22  6:18   ` Coly Li
2025-02-21  8:11 ` [PATCH 06/12] badblocks: fix the using of MAX_BADBLOCKS Zheng Qixing
2025-02-21 10:09   ` Zhu Yanjun
2025-02-25  9:14     ` Li Nan
2025-02-21 10:22   ` Coly Li
2025-02-22  1:15   ` Yu Kuai
2025-02-21  8:11 ` [PATCH 07/12] badblocks: try can_merge_front before overlap_front Zheng Qixing
2025-02-22  1:14   ` Yu Kuai
2025-02-21  8:11 ` [PATCH 08/12] badblocks: fix merge issue when new badblocks align with pre+1 Zheng Qixing
2025-02-22  1:21   ` Yu Kuai
2025-02-21  8:11 ` [PATCH 09/12] badblocks: fix missing bad blocks on retry in _badblocks_check() Zheng Qixing
2025-02-21 10:27   ` Coly Li
2025-02-22  1:28   ` Yu Kuai
2025-02-21  8:11 ` [PATCH 10/12] badblocks: return boolen from badblocks_set() and badblocks_clear() Zheng Qixing
2025-02-22  1:26   ` Yu Kuai [this message]
2025-02-22  4:32     ` Paul Menzel
2025-02-22  4:36       ` Paul Menzel
2025-02-22  6:20   ` Coly Li
2025-02-21  8:11 ` [PATCH 11/12] md: improve return types of badblocks handling functions Zheng Qixing
2025-02-22  1:27   ` Yu Kuai
2025-02-21  8:11 ` [PATCH 12/12] badblocks: use sector_t instead of int to avoid truncation of badblocks length Zheng Qixing
2025-02-21 10:37   ` Coly Li
2025-02-22  1:43   ` Yu Kuai
2025-02-21  9:00 ` [PATCH 00/12] badblocks: bugfix and cleanup for badblocks Yu Kuai
2025-02-21 11:52 ` Coly Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fe2dedca-0b71-3c80-6958-4bca61707fcc@huaweicloud.com \
    --to=yukuai1@huaweicloud.com \
    --cc=axboe@kernel.dk \
    --cc=colyli@kernel.org \
    --cc=colyli@suse.de \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dlemoal@kernel.org \
    --cc=geliang@kernel.org \
    --cc=hare@suse.de \
    --cc=ira.weiny@intel.com \
    --cc=john.g.garry@oracle.com \
    --cc=kch@nvidia.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=xni@redhat.com \
    --cc=yangerkun@huawei.com \
    --cc=yanjun.zhu@linux.dev \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai3@huawei.com \
    --cc=zhengqixing@huawei.com \
    --cc=zhengqixing@huaweicloud.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®