@@ -245,6 +245,7 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
/* merging is possible */
if (e <= s + sectors) {
/* full overlap */
+
e = s + sectors;
ack = acknowledged;
} else
@@ -267,17 +268,35 @@ int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
if (sectors == 0 && hi < bb->count) {
/* we might be able to combine lo and hi */
/* Note: 's' is at the end of 'lo' */
- sector_t a = BB_OFFSET(p[lo]);
- int newlen = max(s, BB_OFFSET(p[hi]) + BB_LEN(p[hi])) - a;
-
- if (s >= BB_OFFSET(p[hi]) && newlen < BB_MAX_LEN) {
- /* yes, we can combine them */
- int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
-
- p[lo] = BB_MAKE(a, newlen, ack);
- memmove(p + hi, p + hi + 1,
- (bb->count - hi - 1) * 8);
- bb->count--;
+ sector_t loa = BB_OFFSET(p[lo]), hia = BB_OFFSET(p[hi]);
+ sector_t hie = hia + BB_LEN(p[hi]);
+ int newlen = max(s, hie) - loa;
+ int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
+
+ if (s >= hia) {
+ while (s >= hie) {
+ /* lo contains hi, just remove hi */
+ memmove(p + hi, p + hi + 1,
+ (bb->count - hi - 1) * 8);
+ bb->count--;
+ if (hi >= bb->count)
+ break;
+ hia = BB_OFFSET(p[hi]);
+ hie = hia + BB_LEN(p[hi]);
+ }
+ if (s >= hia && hi < bb->count) {
+ if (newlen > BB_MAX_LEN) {
+ p[lo] = BB_MAKE(loa, BB_MAX_LEN, ack);
+ p[hi] = BB_MAKE(loa + BB_MAX_LEN,
+ newlen - BB_MAX_LEN,
+ BB_ACK(p[hi]));
+ } else {
+ p[lo] = BB_MAKE(loa, newlen, ack);
+ memmove(p + hi, p + hi + 1,
+ (bb->count - hi - 1) * 8);
+ bb->count--;
+ }
+ }
changed = true;
}
}