]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
badblocks: return boolean from badblocks_set() and badblocks_clear()
authorZheng Qixing <zhengqixing@huawei.com>
Thu, 27 Feb 2025 07:55:05 +0000 (15:55 +0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 6 Mar 2025 15:03:28 +0000 (08:03 -0700)
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 are 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>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Acked-by: Coly Li <colyli@kernel.org>
Acked-by: Ira Weiny <ira.weiny@intel.com>
Link: https://lore.kernel.org/r/20250227075507.151331-11-zhengqixing@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/badblocks.c
drivers/block/null_blk/main.c
drivers/md/md.c
drivers/nvdimm/badrange.c
include/linux/badblocks.h

index b66d5f12a7669f6dcffe1caad269355d6d03e683..e326a16fd05691e83a8e9ae824ea1a352fd3cf96 100644 (file)
@@ -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 @@ out:
 
        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 @@ update_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,12 +1337,12 @@ EXPORT_SYMBOL_GPL(badblocks_check);
  * decide how best to handle it.
  *
  * Return:
- *  0: success
- *  other: failed to set badblocks (out of space). Parital setting will be
+ *  true: success
+ *  false: failed to set badblocks (out of space). Parital setting will be
  *  treated as failure.
  */
-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);
 }
@@ -1360,10 +1359,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);
 }
@@ -1485,10 +1484,10 @@ 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
-               return len;
+
+       return len;
 }
 EXPORT_SYMBOL_GPL(badblocks_store);
 
index 31d44cef68414585a3f38eb0c2bf47e00975042d..8f6025efc54305737dd7bbfc4187635f9cec0918 100644 (file)
@@ -561,14 +561,14 @@ 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)
+       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)) {
                ret = count;
+       }
 out:
        kfree(orig);
        return ret;
index 79cabe4be77dd3139f620b774c4c8b6ecb8d79b0..95ceb60523605100f2741255b91dab64969be58f 100644 (file)
@@ -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)
@@ -9833,7 +9833,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
@@ -9849,33 +9848,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);
 
index a002ea6fdd8424384d84c4627294b3339e104829..ee478ccde7c6c78af9e73666a41f5807386ac285 100644 (file)
@@ -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);
 }
index 670f2dae692fb20cab66a2781557b6b713a8c418..8764bed9ff167d84c2e2b49f18f0e8bc2f766628 100644 (file)
@@ -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,