From: Li Nan <linan122@huawei.com>
If we set a new badblocks, we first merge it with existing region, then
try to combine lo and hi. If there are still badblocks need to be set,
create it. It is a bad way when setting a laget number of badblocks. for
example, it will become chaotic if we set as below:
# echo 1 1 > bad_blocks
# echo 512 1 > bad_blocks
# echo 0 513 > bad_blocks
# cat bad_blocks
0 512
512 1
512 1
Fix it by trying to merge as much as possible. If we have merged any
badblocks, retry to merge next sectors. Do not check sectors while
combining, we should combine lo and hi each sycle.
Fixes: 9e0e252a048b ("badblocks: Add core badblock management code")
Signed-off-by: Li Nan <linan122@huawei.com>
---
block/badblocks.c | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
@@ -347,8 +347,6 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
lo = 0;
hi = bb->count;
if (bb->count) {
- int merged_sectors;
-
/* Find the last range that starts at-or-before 's' */
while (hi - lo > 1) {
int mid = (lo + hi) / 2;
@@ -360,12 +358,19 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
hi = mid;
}
- merged_sectors = badblocks_merge(bb, s, sectors, acknowledged,
- &lo, &hi, &changed);
- s += merged_sectors;
- sectors -= merged_sectors;
- if (sectors == 0)
+ while (sectors) {
+ int merged_sectors;
+
+ merged_sectors = badblocks_merge(bb, s, sectors, acknowledged,
+ &lo, &hi, &changed);
+ /* can't merge, break to create */
+ if (!merged_sectors)
+ break;
+
+ s += merged_sectors;
+ sectors -= merged_sectors;
badblocks_combine(bb, lo);
+ }
}
rv = badblocks_create(bb, s, sectors, hi, acknowledged, &changed);