1 // SPDX-License-Identifier: GPL-2.0
5 * - Heavily based on MD badblocks code from Neil Brown
7 * Copyright (c) 2015, Intel Corporation.
10 #include <linux/badblocks.h>
11 #include <linux/seqlock.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/stddef.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
20 * badblocks_check() - check a given range for bad sectors
21 * @bb: the badblocks structure that holds all badblock information
22 * @s: sector (start) at which to check for badblocks
23 * @sectors: number of sectors to check for badblocks
24 * @first_bad: pointer to store location of the first badblock
25 * @bad_sectors: pointer to store number of badblocks after @first_bad
27 * We can record which blocks on each device are 'bad' and so just
28 * fail those blocks, or that stripe, rather than the whole device.
29 * Entries in the bad-block table are 64bits wide. This comprises:
30 * Length of bad-range, in sectors: 0-511 for lengths 1-512
31 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
32 * A 'shift' can be set so that larger blocks are tracked and
33 * consequently larger devices can be covered.
34 * 'Acknowledged' flag - 1 bit. - the most significant bit.
36 * Locking of the bad-block table uses a seqlock so badblocks_check
37 * might need to retry if it is very unlucky.
38 * We will sometimes want to check for bad blocks in a bi_end_io function,
39 * so we use the write_seqlock_irq variant.
41 * When looking for a bad block we specify a range and want to
42 * know if any block in the range is bad. So we binary-search
43 * to the last range that starts at-or-before the given endpoint,
44 * (or "before the sector after the target range")
45 * then see if it ends after the given start.
48 * 0: there are no known bad blocks in the range
49 * 1: there are known bad block which are all acknowledged
50 * -1: there are bad blocks which have not yet been acknowledged in metadata.
51 * plus the start/length of the first bad section we overlap.
53 int badblocks_check(struct badblocks *bb, sector_t s, int sectors,
54 sector_t *first_bad, int *bad_sectors)
60 sector_t target = s + sectors;
64 /* round the start down, and the end up */
66 target += (1<<bb->shift) - 1;
69 /* 'target' is now the first block after the bad range */
72 seq = read_seqbegin(&bb->lock);
77 /* Binary search between lo and hi for 'target'
78 * i.e. for the last range that starts before 'target'
80 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
81 * are known not to be the last range before target.
82 * VARIANT: hi-lo is the number of possible
83 * ranges, and decreases until it reaches 1
86 int mid = (lo + hi) / 2;
87 sector_t a = BB_OFFSET(p[mid]);
90 /* This could still be the one, earlier ranges
95 /* This and later ranges are definitely out. */
98 /* 'lo' might be the last that started before target, but 'hi' isn't */
100 /* need to check all range that end after 's' to see if
101 * any are unacknowledged.
104 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
105 if (BB_OFFSET(p[lo]) < target) {
106 /* starts before the end, and finishes after
107 * the start, so they must overlap
109 if (rv != -1 && BB_ACK(p[lo]))
113 *first_bad = BB_OFFSET(p[lo]);
114 *bad_sectors = BB_LEN(p[lo]);
120 if (read_seqretry(&bb->lock, seq))
125 EXPORT_SYMBOL_GPL(badblocks_check);
127 static void badblocks_update_acked(struct badblocks *bb)
131 bool unacked = false;
133 if (!bb->unacked_exist)
136 for (i = 0; i < bb->count ; i++) {
144 bb->unacked_exist = 0;
148 * badblocks_set() - Add a range of bad blocks to the table.
149 * @bb: the badblocks structure that holds all badblock information
150 * @s: first sector to mark as bad
151 * @sectors: number of sectors to mark as bad
152 * @acknowledged: weather to mark the bad sectors as acknowledged
154 * This might extend the table, or might contract it if two adjacent ranges
155 * can be merged. We binary-search to find the 'insertion' point, then
156 * decide how best to handle it.
160 * 1: failed to set badblocks (out of space)
162 int badblocks_set(struct badblocks *bb, sector_t s, int sectors,
171 /* badblocks are disabled */
175 /* round the start down, and the end up */
176 sector_t next = s + sectors;
179 next += (1<<bb->shift) - 1;
184 write_seqlock_irqsave(&bb->lock, flags);
189 /* Find the last range that starts at-or-before 's' */
190 while (hi - lo > 1) {
191 int mid = (lo + hi) / 2;
192 sector_t a = BB_OFFSET(p[mid]);
199 if (hi > lo && BB_OFFSET(p[lo]) > s)
203 /* we found a range that might merge with the start
206 sector_t a = BB_OFFSET(p[lo]);
207 sector_t e = a + BB_LEN(p[lo]);
208 int ack = BB_ACK(p[lo]);
211 /* Yes, we can merge with a previous range */
212 if (s == a && s + sectors >= e)
213 /* new range covers old */
216 ack = ack && acknowledged;
220 if (e - a <= BB_MAX_LEN) {
221 p[lo] = BB_MAKE(a, e-a, ack);
224 /* does not all fit in one range,
227 if (BB_LEN(p[lo]) != BB_MAX_LEN)
228 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
234 if (sectors && hi < bb->count) {
235 /* 'hi' points to the first range that starts after 's'.
236 * Maybe we can merge with the start of that range
238 sector_t a = BB_OFFSET(p[hi]);
239 sector_t e = a + BB_LEN(p[hi]);
240 int ack = BB_ACK(p[hi]);
242 if (a <= s + sectors) {
243 /* merging is possible */
244 if (e <= s + sectors) {
249 ack = ack && acknowledged;
252 if (e - a <= BB_MAX_LEN) {
253 p[hi] = BB_MAKE(a, e-a, ack);
256 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
264 if (sectors == 0 && hi < bb->count) {
265 /* we might be able to combine lo and hi */
266 /* Note: 's' is at the end of 'lo' */
267 sector_t a = BB_OFFSET(p[hi]);
268 int lolen = BB_LEN(p[lo]);
269 int hilen = BB_LEN(p[hi]);
270 int newlen = lolen + hilen - (s - a);
272 if (s >= a && newlen < BB_MAX_LEN) {
273 /* yes, we can combine them */
274 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
276 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
277 memmove(p + hi, p + hi + 1,
278 (bb->count - hi - 1) * 8);
283 /* didn't merge (it all).
284 * Need to add a range just before 'hi'
286 if (bb->count >= MAX_BADBLOCKS) {
287 /* No room for more */
291 int this_sectors = sectors;
293 memmove(p + hi + 1, p + hi,
294 (bb->count - hi) * 8);
297 if (this_sectors > BB_MAX_LEN)
298 this_sectors = BB_MAX_LEN;
299 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
300 sectors -= this_sectors;
307 bb->unacked_exist = 1;
309 badblocks_update_acked(bb);
310 write_sequnlock_irqrestore(&bb->lock, flags);
314 EXPORT_SYMBOL_GPL(badblocks_set);
317 * badblocks_clear() - Remove a range of bad blocks to the table.
318 * @bb: the badblocks structure that holds all badblock information
319 * @s: first sector to mark as bad
320 * @sectors: number of sectors to mark as bad
322 * This may involve extending the table if we spilt a region,
323 * but it must not fail. So if the table becomes full, we just
324 * drop the remove request.
328 * 1: failed to clear badblocks
330 int badblocks_clear(struct badblocks *bb, sector_t s, int sectors)
334 sector_t target = s + sectors;
338 /* When clearing we round the start up and the end down.
339 * This should not matter as the shift should align with
340 * the block size and no rounding should ever be needed.
341 * However it is better the think a block is bad when it
342 * isn't than to think a block is not bad when it is.
344 s += (1<<bb->shift) - 1;
346 target >>= bb->shift;
349 write_seqlock_irq(&bb->lock);
354 /* Find the last range that starts before 'target' */
355 while (hi - lo > 1) {
356 int mid = (lo + hi) / 2;
357 sector_t a = BB_OFFSET(p[mid]);
365 /* p[lo] is the last range that could overlap the
366 * current range. Earlier ranges could also overlap,
367 * but only this one can overlap the end of the range.
369 if ((BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) &&
370 (BB_OFFSET(p[lo]) < target)) {
371 /* Partial overlap, leave the tail of this range */
372 int ack = BB_ACK(p[lo]);
373 sector_t a = BB_OFFSET(p[lo]);
374 sector_t end = a + BB_LEN(p[lo]);
377 /* we need to split this range */
378 if (bb->count >= MAX_BADBLOCKS) {
382 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
384 p[lo] = BB_MAKE(a, s-a, ack);
387 p[lo] = BB_MAKE(target, end - target, ack);
388 /* there is no longer an overlap */
393 (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) &&
394 (BB_OFFSET(p[lo]) < target)) {
395 /* This range does overlap */
396 if (BB_OFFSET(p[lo]) < s) {
397 /* Keep the early parts of this range. */
398 int ack = BB_ACK(p[lo]);
399 sector_t start = BB_OFFSET(p[lo]);
401 p[lo] = BB_MAKE(start, s - start, ack);
402 /* now low doesn't overlap, so.. */
407 /* 'lo' is strictly before, 'hi' is strictly after,
408 * anything between needs to be discarded
411 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
412 bb->count -= (hi - lo - 1);
416 badblocks_update_acked(bb);
419 write_sequnlock_irq(&bb->lock);
422 EXPORT_SYMBOL_GPL(badblocks_clear);
425 * ack_all_badblocks() - Acknowledge all bad blocks in a list.
426 * @bb: the badblocks structure that holds all badblock information
428 * This only succeeds if ->changed is clear. It is used by
429 * in-kernel metadata updates
431 void ack_all_badblocks(struct badblocks *bb)
433 if (bb->page == NULL || bb->changed)
434 /* no point even trying */
436 write_seqlock_irq(&bb->lock);
438 if (bb->changed == 0 && bb->unacked_exist) {
442 for (i = 0; i < bb->count ; i++) {
444 sector_t start = BB_OFFSET(p[i]);
445 int len = BB_LEN(p[i]);
447 p[i] = BB_MAKE(start, len, 1);
450 bb->unacked_exist = 0;
452 write_sequnlock_irq(&bb->lock);
454 EXPORT_SYMBOL_GPL(ack_all_badblocks);
457 * badblocks_show() - sysfs access to bad-blocks list
458 * @bb: the badblocks structure that holds all badblock information
459 * @page: buffer received from sysfs
460 * @unack: weather to show unacknowledged badblocks
463 * Length of returned data
465 ssize_t badblocks_show(struct badblocks *bb, char *page, int unack)
476 seq = read_seqbegin(&bb->lock);
481 while (len < PAGE_SIZE && i < bb->count) {
482 sector_t s = BB_OFFSET(p[i]);
483 unsigned int length = BB_LEN(p[i]);
484 int ack = BB_ACK(p[i]);
491 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
492 (unsigned long long)s << bb->shift,
493 length << bb->shift);
495 if (unack && len == 0)
496 bb->unacked_exist = 0;
498 if (read_seqretry(&bb->lock, seq))
503 EXPORT_SYMBOL_GPL(badblocks_show);
506 * badblocks_store() - sysfs access to bad-blocks list
507 * @bb: the badblocks structure that holds all badblock information
508 * @page: buffer received from sysfs
509 * @len: length of data received from sysfs
510 * @unack: weather to show unacknowledged badblocks
513 * Length of the buffer processed or -ve error.
515 ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len,
518 unsigned long long sector;
522 switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) {
535 if (badblocks_set(bb, sector, length, !unack))
540 EXPORT_SYMBOL_GPL(badblocks_store);
542 static int __badblocks_init(struct device *dev, struct badblocks *bb,
552 bb->page = devm_kzalloc(dev, PAGE_SIZE, GFP_KERNEL);
554 bb->page = kzalloc(PAGE_SIZE, GFP_KERNEL);
559 seqlock_init(&bb->lock);
565 * badblocks_init() - initialize the badblocks structure
566 * @bb: the badblocks structure that holds all badblock information
567 * @enable: weather to enable badblocks accounting
571 * -ve errno: on error
573 int badblocks_init(struct badblocks *bb, int enable)
575 return __badblocks_init(NULL, bb, enable);
577 EXPORT_SYMBOL_GPL(badblocks_init);
579 int devm_init_badblocks(struct device *dev, struct badblocks *bb)
583 return __badblocks_init(dev, bb, 1);
585 EXPORT_SYMBOL_GPL(devm_init_badblocks);
588 * badblocks_exit() - free the badblocks structure
589 * @bb: the badblocks structure that holds all badblock information
591 void badblocks_exit(struct badblocks *bb)
596 devm_kfree(bb->dev, bb->page);
601 EXPORT_SYMBOL_GPL(badblocks_exit);