Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp938222pxb; Wed, 3 Mar 2021 21:47:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJw2pDU1jPF4cjGod6QlzFbweISYIQzceB6oScKl1O+0/qMVSP9SfHayQUSN0cpaVVPXP6o5 X-Received: by 2002:a05:6402:181a:: with SMTP id g26mr2502346edy.225.1614836858079; Wed, 03 Mar 2021 21:47:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614836858; cv=none; d=google.com; s=arc-20160816; b=LpYSJSeOsFhzH0hln9U3EK+GC6l2GMXAudDKFjDoV4zssqS38Q3pBajIyNtyo++GSZ JQEHCnujbikHXSWODxT4ldpPs7I7fdNWtM4fuahirsvamqJlH1w+VCjI8oe+2jdC+5Wd z0rbQQZ+2c4TTJZ0xwGjLPG6HrpMvp2OU4EM3/AWKpC1E2J9IWPGEWzT9bX+txMme1dx kEHey/H+HLgx9Qnj9NPCA0Lgwl7DmQEp1nNxnNE5IgqYv1REwD8dSRnTXT6+3vmRy1CW Hx4dgLSZFe6YDXv8EEh8y7aBt+LRWBuRsuPkwiQKkJ+K7tNJ+oiYMOzg34H1jnLVBM8w MSFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=zZOl6Cc8YyKQ+7Z3TVdTaXI7F6UWJsbPu2NlP16+NOs=; b=1AEREzBgBwg6WLaT5Xwx6kXf6/UhypqExnwxDUYhQQnQVckavu8M2TJILCdHUi3r+M n5+FUkq/KKmQLxelPxnF09a/CcL85ahxGayMOc85vYJ7288Yczf0gwSvZKLck8ZMy1bm 9wqS0Dz5Zzez0nFldwoHTlhqIE6ogfCfhzNBBASnGi05WkTQ0efsbtgrlTaAc7bEvXfX 4/ZgJrT/aMYx9wDhUuwIvTjUaj0h7fatu/Pyx61HUl+TOAVwSk7aIJ9prneIwaNRdB8o 6hwy0P50f/HjQ/i7FLjG+q2TD54OkJOgtEj+rqjWSBmKGnfkN2qYkIvnPT23umECyrPI lu0Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a14si12819380ejk.698.2021.03.03.21.47.14; Wed, 03 Mar 2021 21:47:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1577520AbhCBGXN (ORCPT + 99 others); Tue, 2 Mar 2021 01:23:13 -0500 Received: from mx2.suse.de ([195.135.220.15]:44732 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1576136AbhCBEYS (ORCPT ); Mon, 1 Mar 2021 23:24:18 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9FF02AED8; Tue, 2 Mar 2021 04:03:27 +0000 (UTC) From: Coly Li To: linux-block@vger.kernel.org, axboe@kernel.dk, dan.j.williams@intel.com, vishal.l.verma@intel.com, neilb@suse.de Cc: antlists@youngman.org.uk, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, linux-nvdimm@lists.01.org, Coly Li Subject: [RFC PATCH v1 6/6] badblocks: switch to the improved badblock handling code Date: Tue, 2 Mar 2021 12:02:52 +0800 Message-Id: <20210302040252.103720-7-colyli@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210302040252.103720-1-colyli@suse.de> References: <20210302040252.103720-1-colyli@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch removes old code of badblocks_set(), badblocks_clear() and badblocks_check(), and make them as wrappers to call _badblocks_set(), _badblocks_clear() and _badblocks_check(). By this change now the badblock handing switch to the improved algorithm in _badblocks_set(), _badblocks_clear() and _badblocks_check(). This patch only contains the changes of old code deletion, new added code for the improved algorithms are in previous patches. Signed-off-by: Coly Li --- block/badblocks.c | 310 +--------------------------------------------- 1 file changed, 3 insertions(+), 307 deletions(-) diff --git a/block/badblocks.c b/block/badblocks.c index 304b91159a42..904c6ed0de6d 100644 --- a/block/badblocks.c +++ b/block/badblocks.c @@ -1386,75 +1386,7 @@ static int _badblocks_check(struct badblocks *bb, sector_t s, int sectors, int badblocks_check(struct badblocks *bb, sector_t s, int sectors, sector_t *first_bad, int *bad_sectors) { - int hi; - int lo; - u64 *p = bb->page; - int rv; - sector_t target = s + sectors; - unsigned seq; - - if (bb->shift > 0) { - /* round the start down, and the end up */ - s >>= bb->shift; - target += (1<shift) - 1; - target >>= bb->shift; - sectors = target - s; - } - /* 'target' is now the first block after the bad range */ - -retry: - seq = read_seqbegin(&bb->lock); - lo = 0; - rv = 0; - hi = bb->count; - - /* Binary search between lo and hi for 'target' - * i.e. for the last range that starts before 'target' - */ - /* INVARIANT: ranges before 'lo' and at-or-after 'hi' - * are known not to be the last range before target. - * VARIANT: hi-lo is the number of possible - * ranges, and decreases until it reaches 1 - */ - while (hi - lo > 1) { - int mid = (lo + hi) / 2; - sector_t a = BB_OFFSET(p[mid]); - - if (a < target) - /* This could still be the one, earlier ranges - * could not. - */ - lo = mid; - else - /* This and later ranges are definitely out. */ - hi = mid; - } - /* 'lo' might be the last that started before target, but 'hi' isn't */ - if (hi > lo) { - /* need to check all range that end after 's' to see if - * any are unacknowledged. - */ - while (lo >= 0 && - BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { - if (BB_OFFSET(p[lo]) < target) { - /* starts before the end, and finishes after - * the start, so they must overlap - */ - if (rv != -1 && BB_ACK(p[lo])) - rv = 1; - else - rv = -1; - *first_bad = BB_OFFSET(p[lo]); - *bad_sectors = BB_LEN(p[lo]); - } - lo--; - } - } - - if (read_seqretry(&bb->lock, seq)) - goto retry; - - return rv; + return _badblocks_check(bb, s, sectors, first_bad, bad_sectors); } EXPORT_SYMBOL_GPL(badblocks_check); @@ -1476,154 +1408,7 @@ EXPORT_SYMBOL_GPL(badblocks_check); int badblocks_set(struct badblocks *bb, sector_t s, int sectors, int acknowledged) { - u64 *p; - int lo, hi; - int rv = 0; - unsigned long flags; - - if (bb->shift < 0) - /* badblocks are disabled */ - return 1; - - if (bb->shift) { - /* round the start down, and the end up */ - sector_t next = s + sectors; - - s >>= bb->shift; - next += (1<shift) - 1; - next >>= bb->shift; - sectors = next - s; - } - - write_seqlock_irqsave(&bb->lock, flags); - - p = bb->page; - lo = 0; - hi = bb->count; - /* Find the last range that starts at-or-before 's' */ - while (hi - lo > 1) { - int mid = (lo + hi) / 2; - sector_t a = BB_OFFSET(p[mid]); - - if (a <= s) - lo = mid; - else - hi = mid; - } - if (hi > lo && BB_OFFSET(p[lo]) > s) - hi = lo; - - if (hi > lo) { - /* we found a range that might merge with the start - * of our new range - */ - sector_t a = BB_OFFSET(p[lo]); - sector_t e = a + BB_LEN(p[lo]); - int ack = BB_ACK(p[lo]); - - if (e >= s) { - /* Yes, we can merge with a previous range */ - if (s == a && s + sectors >= e) - /* new range covers old */ - ack = acknowledged; - else - ack = ack && acknowledged; - - if (e < s + sectors) - e = s + sectors; - if (e - a <= BB_MAX_LEN) { - p[lo] = BB_MAKE(a, e-a, ack); - s = e; - } else { - /* does not all fit in one range, - * make p[lo] maximal - */ - if (BB_LEN(p[lo]) != BB_MAX_LEN) - p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); - s = a + BB_MAX_LEN; - } - sectors = e - s; - } - } - if (sectors && hi < bb->count) { - /* 'hi' points to the first range that starts after 's'. - * Maybe we can merge with the start of that range - */ - sector_t a = BB_OFFSET(p[hi]); - sector_t e = a + BB_LEN(p[hi]); - int ack = BB_ACK(p[hi]); - - if (a <= s + sectors) { - /* merging is possible */ - if (e <= s + sectors) { - /* full overlap */ - e = s + sectors; - ack = acknowledged; - } else - ack = ack && acknowledged; - - a = s; - if (e - a <= BB_MAX_LEN) { - p[hi] = BB_MAKE(a, e-a, ack); - s = e; - } else { - p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); - s = a + BB_MAX_LEN; - } - sectors = e - s; - lo = hi; - hi++; - } - } - 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[hi]); - int lolen = BB_LEN(p[lo]); - int hilen = BB_LEN(p[hi]); - int newlen = lolen + hilen - (s - a); - - if (s >= a && newlen < BB_MAX_LEN) { - /* yes, we can combine them */ - int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); - - p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); - memmove(p + hi, p + hi + 1, - (bb->count - hi - 1) * 8); - bb->count--; - } - } - while (sectors) { - /* didn't merge (it all). - * Need to add a range just before 'hi' - */ - if (bb->count >= MAX_BADBLOCKS) { - /* No room for more */ - rv = 1; - break; - } else { - int this_sectors = sectors; - - memmove(p + hi + 1, p + hi, - (bb->count - hi) * 8); - bb->count++; - - if (this_sectors > BB_MAX_LEN) - this_sectors = BB_MAX_LEN; - p[hi] = BB_MAKE(s, this_sectors, acknowledged); - sectors -= this_sectors; - s += this_sectors; - } - } - - bb->changed = 1; - if (!acknowledged) - bb->unacked_exist = 1; - else - badblocks_update_acked(bb); - write_sequnlock_irqrestore(&bb->lock, flags); - - return rv; + return _badblocks_set(bb, s, sectors, acknowledged); } EXPORT_SYMBOL_GPL(badblocks_set); @@ -1643,96 +1428,7 @@ EXPORT_SYMBOL_GPL(badblocks_set); */ int badblocks_clear(struct badblocks *bb, sector_t s, int sectors) { - u64 *p; - int lo, hi; - sector_t target = s + sectors; - int rv = 0; - - if (bb->shift > 0) { - /* When clearing we round the start up and the end down. - * This should not matter as the shift should align with - * the block size and no rounding should ever be needed. - * However it is better the think a block is bad when it - * isn't than to think a block is not bad when it is. - */ - s += (1<shift) - 1; - s >>= bb->shift; - target >>= bb->shift; - sectors = target - s; - } - - write_seqlock_irq(&bb->lock); - - p = bb->page; - lo = 0; - hi = bb->count; - /* Find the last range that starts before 'target' */ - while (hi - lo > 1) { - int mid = (lo + hi) / 2; - sector_t a = BB_OFFSET(p[mid]); - - if (a < target) - lo = mid; - else - hi = mid; - } - if (hi > lo) { - /* p[lo] is the last range that could overlap the - * current range. Earlier ranges could also overlap, - * but only this one can overlap the end of the range. - */ - if ((BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) && - (BB_OFFSET(p[lo]) < target)) { - /* Partial overlap, leave the tail of this range */ - int ack = BB_ACK(p[lo]); - sector_t a = BB_OFFSET(p[lo]); - sector_t end = a + BB_LEN(p[lo]); - - if (a < s) { - /* we need to split this range */ - if (bb->count >= MAX_BADBLOCKS) { - rv = -ENOSPC; - goto out; - } - memmove(p+lo+1, p+lo, (bb->count - lo) * 8); - bb->count++; - p[lo] = BB_MAKE(a, s-a, ack); - lo++; - } - p[lo] = BB_MAKE(target, end - target, ack); - /* there is no longer an overlap */ - hi = lo; - lo--; - } - while (lo >= 0 && - (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) && - (BB_OFFSET(p[lo]) < target)) { - /* This range does overlap */ - if (BB_OFFSET(p[lo]) < s) { - /* Keep the early parts of this range. */ - int ack = BB_ACK(p[lo]); - sector_t start = BB_OFFSET(p[lo]); - - p[lo] = BB_MAKE(start, s - start, ack); - /* now low doesn't overlap, so.. */ - break; - } - lo--; - } - /* 'lo' is strictly before, 'hi' is strictly after, - * anything between needs to be discarded - */ - if (hi - lo > 1) { - memmove(p+lo+1, p+hi, (bb->count - hi) * 8); - bb->count -= (hi - lo - 1); - } - } - - badblocks_update_acked(bb); - bb->changed = 1; -out: - write_sequnlock_irq(&bb->lock); - return rv; + return _badblocks_clear(bb, s, sectors); } EXPORT_SYMBOL_GPL(badblocks_clear); -- 2.26.2