xref: /linux/block/badblocks.c (revision 7e76336e14de9a2b67af96012ddd46c5676cf340)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Bad block management
4  *
5  * - Heavily based on MD badblocks code from Neil Brown
6  *
7  * Copyright (c) 2015, Intel Corporation.
8  */
9 
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>
18 
19 /*
20  * The purpose of badblocks set/clear is to manage bad blocks ranges which are
21  * identified by LBA addresses.
22  *
23  * When the caller of badblocks_set() wants to set a range of bad blocks, the
24  * setting range can be acked or unacked. And the setting range may merge,
25  * overwrite, skip the overlapped already set range, depends on who they are
26  * overlapped or adjacent, and the acknowledgment type of the ranges. It can be
27  * more complicated when the setting range covers multiple already set bad block
28  * ranges, with restrictions of maximum length of each bad range and the bad
29  * table space limitation.
30  *
31  * It is difficult and unnecessary to take care of all the possible situations,
32  * for setting a large range of bad blocks, we can handle it by dividing the
33  * large range into smaller ones when encounter overlap, max range length or
34  * bad table full conditions. Every time only a smaller piece of the bad range
35  * is handled with a limited number of conditions how it is interacted with
36  * possible overlapped or adjacent already set bad block ranges. Then the hard
37  * complicated problem can be much simpler to handle in proper way.
38  *
39  * When setting a range of bad blocks to the bad table, the simplified situations
40  * to be considered are, (The already set bad blocks ranges are naming with
41  *  prefix E, and the setting bad blocks range is naming with prefix S)
42  *
43  * 1) A setting range is not overlapped or adjacent to any other already set bad
44  *    block range.
45  *                         +--------+
46  *                         |    S   |
47  *                         +--------+
48  *        +-------------+               +-------------+
49  *        |      E1     |               |      E2     |
50  *        +-------------+               +-------------+
51  *    For this situation if the bad blocks table is not full, just allocate a
52  *    free slot from the bad blocks table to mark the setting range S. The
53  *    result is,
54  *        +-------------+  +--------+   +-------------+
55  *        |      E1     |  |    S   |   |      E2     |
56  *        +-------------+  +--------+   +-------------+
57  * 2) A setting range starts exactly at a start LBA of an already set bad blocks
58  *    range.
59  * 2.1) The setting range size < already set range size
60  *        +--------+
61  *        |    S   |
62  *        +--------+
63  *        +-------------+
64  *        |      E      |
65  *        +-------------+
66  * 2.1.1) If S and E are both acked or unacked range, the setting range S can
67  *    be merged into existing bad range E. The result is,
68  *        +-------------+
69  *        |      S      |
70  *        +-------------+
71  * 2.1.2) If S is unacked setting and E is acked, the setting will be denied, and
72  *    the result is,
73  *        +-------------+
74  *        |      E      |
75  *        +-------------+
76  * 2.1.3) If S is acked setting and E is unacked, range S can overwrite on E.
77  *    An extra slot from the bad blocks table will be allocated for S, and head
78  *    of E will move to end of the inserted range S. The result is,
79  *        +--------+----+
80  *        |    S   | E  |
81  *        +--------+----+
82  * 2.2) The setting range size == already set range size
83  * 2.2.1) If S and E are both acked or unacked range, the setting range S can
84  *    be merged into existing bad range E. The result is,
85  *        +-------------+
86  *        |      S      |
87  *        +-------------+
88  * 2.2.2) If S is unacked setting and E is acked, the setting will be denied, and
89  *    the result is,
90  *        +-------------+
91  *        |      E      |
92  *        +-------------+
93  * 2.2.3) If S is acked setting and E is unacked, range S can overwrite all of
94       bad blocks range E. The result is,
95  *        +-------------+
96  *        |      S      |
97  *        +-------------+
98  * 2.3) The setting range size > already set range size
99  *        +-------------------+
100  *        |          S        |
101  *        +-------------------+
102  *        +-------------+
103  *        |      E      |
104  *        +-------------+
105  *    For such situation, the setting range S can be treated as two parts, the
106  *    first part (S1) is as same size as the already set range E, the second
107  *    part (S2) is the rest of setting range.
108  *        +-------------+-----+        +-------------+       +-----+
109  *        |    S1       | S2  |        |     S1      |       | S2  |
110  *        +-------------+-----+  ===>  +-------------+       +-----+
111  *        +-------------+              +-------------+
112  *        |      E      |              |      E      |
113  *        +-------------+              +-------------+
114  *    Now we only focus on how to handle the setting range S1 and already set
115  *    range E, which are already explained in 2.2), for the rest S2 it will be
116  *    handled later in next loop.
117  * 3) A setting range starts before the start LBA of an already set bad blocks
118  *    range.
119  *        +-------------+
120  *        |      S      |
121  *        +-------------+
122  *             +-------------+
123  *             |      E      |
124  *             +-------------+
125  *    For this situation, the setting range S can be divided into two parts, the
126  *    first (S1) ends at the start LBA of already set range E, the second part
127  *    (S2) starts exactly at a start LBA of the already set range E.
128  *        +----+---------+             +----+      +---------+
129  *        | S1 |    S2   |             | S1 |      |    S2   |
130  *        +----+---------+      ===>   +----+      +---------+
131  *             +-------------+                     +-------------+
132  *             |      E      |                     |      E      |
133  *             +-------------+                     +-------------+
134  *    Now only the first part S1 should be handled in this loop, which is in
135  *    similar condition as 1). The rest part S2 has exact same start LBA address
136  *    of the already set range E, they will be handled in next loop in one of
137  *    situations in 2).
138  * 4) A setting range starts after the start LBA of an already set bad blocks
139  *    range.
140  * 4.1) If the setting range S exactly matches the tail part of already set bad
141  *    blocks range E, like the following chart shows,
142  *            +---------+
143  *            |   S     |
144  *            +---------+
145  *        +-------------+
146  *        |      E      |
147  *        +-------------+
148  * 4.1.1) If range S and E have same acknowledge value (both acked or unacked),
149  *    they will be merged into one, the result is,
150  *        +-------------+
151  *        |      S      |
152  *        +-------------+
153  * 4.1.2) If range E is acked and the setting range S is unacked, the setting
154  *    request of S will be rejected, the result is,
155  *        +-------------+
156  *        |      E      |
157  *        +-------------+
158  * 4.1.3) If range E is unacked, and the setting range S is acked, then S may
159  *    overwrite the overlapped range of E, the result is,
160  *        +---+---------+
161  *        | E |    S    |
162  *        +---+---------+
163  * 4.2) If the setting range S stays in middle of an already set range E, like
164  *    the following chart shows,
165  *             +----+
166  *             | S  |
167  *             +----+
168  *        +--------------+
169  *        |       E      |
170  *        +--------------+
171  * 4.2.1) If range S and E have same acknowledge value (both acked or unacked),
172  *    they will be merged into one, the result is,
173  *        +--------------+
174  *        |       S      |
175  *        +--------------+
176  * 4.2.2) If range E is acked and the setting range S is unacked, the setting
177  *    request of S will be rejected, the result is also,
178  *        +--------------+
179  *        |       E      |
180  *        +--------------+
181  * 4.2.3) If range E is unacked, and the setting range S is acked, then S will
182  *    inserted into middle of E and split previous range E into two parts (E1
183  *    and E2), the result is,
184  *        +----+----+----+
185  *        | E1 |  S | E2 |
186  *        +----+----+----+
187  * 4.3) If the setting bad blocks range S is overlapped with an already set bad
188  *    blocks range E. The range S starts after the start LBA of range E, and
189  *    ends after the end LBA of range E, as the following chart shows,
190  *            +-------------------+
191  *            |          S        |
192  *            +-------------------+
193  *        +-------------+
194  *        |      E      |
195  *        +-------------+
196  *    For this situation the range S can be divided into two parts, the first
197  *    part (S1) ends at end range E, and the second part (S2) has rest range of
198  *    origin S.
199  *            +---------+---------+            +---------+      +---------+
200  *            |    S1   |    S2   |            |    S1   |      |    S2   |
201  *            +---------+---------+  ===>      +---------+      +---------+
202  *        +-------------+                  +-------------+
203  *        |      E      |                  |      E      |
204  *        +-------------+                  +-------------+
205  *     Now in this loop the setting range S1 and already set range E can be
206  *     handled as the situations 4.1), the rest range S2 will be handled in next
207  *     loop and ignored in this loop.
208  * 5) A setting bad blocks range S is adjacent to one or more already set bad
209  *    blocks range(s), and they are all acked or unacked range.
210  * 5.1) Front merge: If the already set bad blocks range E is before setting
211  *    range S and they are adjacent,
212  *                +------+
213  *                |  S   |
214  *                +------+
215  *        +-------+
216  *        |   E   |
217  *        +-------+
218  * 5.1.1) When total size of range S and E <= BB_MAX_LEN, and their acknowledge
219  *    values are same, the setting range S can front merges into range E. The
220  *    result is,
221  *        +--------------+
222  *        |       S      |
223  *        +--------------+
224  * 5.1.2) Otherwise these two ranges cannot merge, just insert the setting
225  *    range S right after already set range E into the bad blocks table. The
226  *    result is,
227  *        +--------+------+
228  *        |   E    |   S  |
229  *        +--------+------+
230  * 6) Special cases which above conditions cannot handle
231  * 6.1) Multiple already set ranges may merge into less ones in a full bad table
232  *        +-------------------------------------------------------+
233  *        |                           S                           |
234  *        +-------------------------------------------------------+
235  *        |<----- BB_MAX_LEN ----->|
236  *                                 +-----+     +-----+   +-----+
237  *                                 | E1  |     | E2  |   | E3  |
238  *                                 +-----+     +-----+   +-----+
239  *     In the above example, when the bad blocks table is full, inserting the
240  *     first part of setting range S will fail because no more available slot
241  *     can be allocated from bad blocks table. In this situation a proper
242  *     setting method should be go though all the setting bad blocks range and
243  *     look for chance to merge already set ranges into less ones. When there
244  *     is available slot from bad blocks table, re-try again to handle more
245  *     setting bad blocks ranges as many as possible.
246  *        +------------------------+
247  *        |          S3            |
248  *        +------------------------+
249  *        |<----- BB_MAX_LEN ----->|
250  *                                 +-----+-----+-----+---+-----+--+
251  *                                 |       S1        |     S2     |
252  *                                 +-----+-----+-----+---+-----+--+
253  *     The above chart shows although the first part (S3) cannot be inserted due
254  *     to no-space in bad blocks table, but the following E1, E2 and E3 ranges
255  *     can be merged with rest part of S into less range S1 and S2. Now there is
256  *     1 free slot in bad blocks table.
257  *        +------------------------+-----+-----+-----+---+-----+--+
258  *        |           S3           |       S1        |     S2     |
259  *        +------------------------+-----+-----+-----+---+-----+--+
260  *     Since the bad blocks table is not full anymore, re-try again for the
261  *     origin setting range S. Now the setting range S3 can be inserted into the
262  *     bad blocks table with previous freed slot from multiple ranges merge.
263  * 6.2) Front merge after overwrite
264  *    In the following example, in bad blocks table, E1 is an acked bad blocks
265  *    range and E2 is an unacked bad blocks range, therefore they are not able
266  *    to merge into a larger range. The setting bad blocks range S is acked,
267  *    therefore part of E2 can be overwritten by S.
268  *                      +--------+
269  *                      |    S   |                             acknowledged
270  *                      +--------+                         S:       1
271  *              +-------+-------------+                   E1:       1
272  *              |   E1  |    E2       |                   E2:       0
273  *              +-------+-------------+
274  *     With previous simplified routines, after overwriting part of E2 with S,
275  *     the bad blocks table should be (E3 is remaining part of E2 which is not
276  *     overwritten by S),
277  *                                                             acknowledged
278  *              +-------+--------+----+                    S:       1
279  *              |   E1  |    S   | E3 |                   E1:       1
280  *              +-------+--------+----+                   E3:       0
281  *     The above result is correct but not perfect. Range E1 and S in the bad
282  *     blocks table are all acked, merging them into a larger one range may
283  *     occupy less bad blocks table space and make badblocks_check() faster.
284  *     Therefore in such situation, after overwriting range S, the previous range
285  *     E1 should be checked for possible front combination. Then the ideal
286  *     result can be,
287  *              +----------------+----+                        acknowledged
288  *              |       E1       | E3 |                   E1:       1
289  *              +----------------+----+                   E3:       0
290  * 6.3) Behind merge: If the already set bad blocks range E is behind the setting
291  *    range S and they are adjacent. Normally we don't need to care about this
292  *    because front merge handles this while going though range S from head to
293  *    tail, except for the tail part of range S. When the setting range S are
294  *    fully handled, all the above simplified routine doesn't check whether the
295  *    tail LBA of range S is adjacent to the next already set range and not
296  *    merge them even it is possible.
297  *        +------+
298  *        |  S   |
299  *        +------+
300  *               +-------+
301  *               |   E   |
302  *               +-------+
303  *    For the above special situation, when the setting range S are all handled
304  *    and the loop ends, an extra check is necessary for whether next already
305  *    set range E is right after S and mergeable.
306  * 6.3.1) When total size of range E and S <= BB_MAX_LEN, and their acknowledge
307  *    values are same, the setting range S can behind merges into range E. The
308  *    result is,
309  *        +--------------+
310  *        |       S      |
311  *        +--------------+
312  * 6.3.2) Otherwise these two ranges cannot merge, just insert the setting range
313  *     S in front of the already set range E in the bad blocks table. The result
314  *     is,
315  *        +------+-------+
316  *        |  S   |   E   |
317  *        +------+-------+
318  *
319  * All the above 5 simplified situations and 3 special cases may cover 99%+ of
320  * the bad block range setting conditions. Maybe there is some rare corner case
321  * is not considered and optimized, it won't hurt if badblocks_set() fails due
322  * to no space, or some ranges are not merged to save bad blocks table space.
323  *
324  * Inside badblocks_set() each loop starts by jumping to re_insert label, every
325  * time for the new loop prev_badblocks() is called to find an already set range
326  * which starts before or at current setting range. Since the setting bad blocks
327  * range is handled from head to tail, most of the cases it is unnecessary to do
328  * the binary search inside prev_badblocks(), it is possible to provide a hint
329  * to prev_badblocks() for a fast path, then the expensive binary search can be
330  * avoided. In my test with the hint to prev_badblocks(), except for the first
331  * loop, all rested calls to prev_badblocks() can go into the fast path and
332  * return correct bad blocks table index immediately.
333  *
334  *
335  * Clearing a bad blocks range from the bad block table has similar idea as
336  * setting does, but much more simpler. The only thing needs to be noticed is
337  * when the clearing range hits middle of a bad block range, the existing bad
338  * block range will split into two, and one more item should be added into the
339  * bad block table. The simplified situations to be considered are, (The already
340  * set bad blocks ranges in bad block table are naming with prefix E, and the
341  * clearing bad blocks range is naming with prefix C)
342  *
343  * 1) A clearing range is not overlapped to any already set ranges in bad block
344  *    table.
345  *    +-----+         |          +-----+         |          +-----+
346  *    |  C  |         |          |  C  |         |          |  C  |
347  *    +-----+         or         +-----+         or         +-----+
348  *            +---+   |   +----+         +----+  |  +---+
349  *            | E |   |   | E1 |         | E2 |  |  | E |
350  *            +---+   |   +----+         +----+  |  +---+
351  *    For the above situations, no bad block to be cleared and no failure
352  *    happens, simply returns 0.
353  * 2) The clearing range hits middle of an already setting bad blocks range in
354  *    the bad block table.
355  *            +---+
356  *            | C |
357  *            +---+
358  *     +-----------------+
359  *     |         E       |
360  *     +-----------------+
361  *    In this situation if the bad block table is not full, the range E will be
362  *    split into two ranges E1 and E2. The result is,
363  *     +------+   +------+
364  *     |  E1  |   |  E2  |
365  *     +------+   +------+
366  * 3) The clearing range starts exactly at same LBA as an already set bad block range
367  *    from the bad block table.
368  * 3.1) Partially covered at head part
369  *         +------------+
370  *         |     C      |
371  *         +------------+
372  *         +-----------------+
373  *         |         E       |
374  *         +-----------------+
375  *    For this situation, the overlapped already set range will update the
376  *    start LBA to end of C and shrink the range to BB_LEN(E) - BB_LEN(C). No
377  *    item deleted from bad block table. The result is,
378  *                      +----+
379  *                      | E1 |
380  *                      +----+
381  * 3.2) Exact fully covered
382  *         +-----------------+
383  *         |         C       |
384  *         +-----------------+
385  *         +-----------------+
386  *         |         E       |
387  *         +-----------------+
388  *    For this situation the whole bad blocks range E will be cleared and its
389  *    corresponded item is deleted from the bad block table.
390  * 4) The clearing range exactly ends at same LBA as an already set bad block
391  *    range.
392  *                   +-------+
393  *                   |   C   |
394  *                   +-------+
395  *         +-----------------+
396  *         |         E       |
397  *         +-----------------+
398  *    For the above situation, the already set range E is updated to shrink its
399  *    end to the start of C, and reduce its length to BB_LEN(E) - BB_LEN(C).
400  *    The result is,
401  *         +---------+
402  *         |    E    |
403  *         +---------+
404  * 5) The clearing range is partially overlapped with an already set bad block
405  *    range from the bad block table.
406  * 5.1) The already set bad block range is front overlapped with the clearing
407  *    range.
408  *         +----------+
409  *         |     C    |
410  *         +----------+
411  *              +------------+
412  *              |      E     |
413  *              +------------+
414  *   For such situation, the clearing range C can be treated as two parts. The
415  *   first part ends at the start LBA of range E, and the second part starts at
416  *   same LBA of range E.
417  *         +----+-----+               +----+   +-----+
418  *         | C1 | C2  |               | C1 |   | C2  |
419  *         +----+-----+         ===>  +----+   +-----+
420  *              +------------+                 +------------+
421  *              |      E     |                 |      E     |
422  *              +------------+                 +------------+
423  *   Now the first part C1 can be handled as condition 1), and the second part C2 can be
424  *   handled as condition 3.1) in next loop.
425  * 5.2) The already set bad block range is behind overlaopped with the clearing
426  *   range.
427  *                 +----------+
428  *                 |     C    |
429  *                 +----------+
430  *         +------------+
431  *         |      E     |
432  *         +------------+
433  *   For such situation, the clearing range C can be treated as two parts. The
434  *   first part C1 ends at same end LBA of range E, and the second part starts
435  *   at end LBA of range E.
436  *                 +----+-----+                 +----+    +-----+
437  *                 | C1 | C2  |                 | C1 |    | C2  |
438  *                 +----+-----+  ===>           +----+    +-----+
439  *         +------------+               +------------+
440  *         |      E     |               |      E     |
441  *         +------------+               +------------+
442  *   Now the first part clearing range C1 can be handled as condition 4), and
443  *   the second part clearing range C2 can be handled as condition 1) in next
444  *   loop.
445  *
446  *   All bad blocks range clearing can be simplified into the above 5 situations
447  *   by only handling the head part of the clearing range in each run of the
448  *   while-loop. The idea is similar to bad blocks range setting but much
449  *   simpler.
450  */
451 
452 /*
453  * Find the range starts at-or-before 's' from bad table. The search
454  * starts from index 'hint' and stops at index 'hint_end' from the bad
455  * table.
456  */
457 static int prev_by_hint(struct badblocks *bb, sector_t s, int hint)
458 {
459 	int hint_end = hint + 2;
460 	u64 *p = bb->page;
461 	int ret = -1;
462 
463 	while ((hint < hint_end) && ((hint + 1) <= bb->count) &&
464 	       (BB_OFFSET(p[hint]) <= s)) {
465 		if ((hint + 1) == bb->count || BB_OFFSET(p[hint + 1]) > s) {
466 			ret = hint;
467 			break;
468 		}
469 		hint++;
470 	}
471 
472 	return ret;
473 }
474 
475 /*
476  * Find the range starts at-or-before bad->start. If 'hint' is provided
477  * (hint >= 0) then search in the bad table from hint firstly. It is
478  * very probably the wanted bad range can be found from the hint index,
479  * then the unnecessary while-loop iteration can be avoided.
480  */
481 static int prev_badblocks(struct badblocks *bb, struct badblocks_context *bad,
482 			  int hint)
483 {
484 	sector_t s = bad->start;
485 	int ret = -1;
486 	int lo, hi;
487 	u64 *p;
488 
489 	if (!bb->count)
490 		goto out;
491 
492 	if (hint >= 0) {
493 		ret = prev_by_hint(bb, s, hint);
494 		if (ret >= 0)
495 			goto out;
496 	}
497 
498 	lo = 0;
499 	hi = bb->count;
500 	p = bb->page;
501 
502 	/* The following bisect search might be unnecessary */
503 	if (BB_OFFSET(p[lo]) > s)
504 		return -1;
505 	if (BB_OFFSET(p[hi - 1]) <= s)
506 		return hi - 1;
507 
508 	/* Do bisect search in bad table */
509 	while (hi - lo > 1) {
510 		int mid = (lo + hi)/2;
511 		sector_t a = BB_OFFSET(p[mid]);
512 
513 		if (a == s) {
514 			ret = mid;
515 			goto out;
516 		}
517 
518 		if (a < s)
519 			lo = mid;
520 		else
521 			hi = mid;
522 	}
523 
524 	if (BB_OFFSET(p[lo]) <= s)
525 		ret = lo;
526 out:
527 	return ret;
528 }
529 
530 /*
531  * Return 'true' if the range indicated by 'bad' can be forward
532  * merged with the bad range (from the bad table) indexed by 'prev'.
533  */
534 static bool can_merge_front(struct badblocks *bb, int prev,
535 			    struct badblocks_context *bad)
536 {
537 	sector_t s = bad->start;
538 	u64 *p = bb->page;
539 
540 	if (BB_ACK(p[prev]) == bad->ack &&
541 	    (s < BB_END(p[prev]) ||
542 	     (s == BB_END(p[prev]) && (BB_LEN(p[prev]) < BB_MAX_LEN))))
543 		return true;
544 	return false;
545 }
546 
547 /*
548  * Do forward merge for range indicated by 'bad' and the bad range
549  * (from bad table) indexed by 'prev'. The return value is sectors
550  * merged from bad->len.
551  */
552 static int front_merge(struct badblocks *bb, int prev, struct badblocks_context *bad)
553 {
554 	sector_t sectors = bad->len;
555 	sector_t s = bad->start;
556 	u64 *p = bb->page;
557 	int merged = 0;
558 
559 	WARN_ON(s > BB_END(p[prev]));
560 
561 	if (s < BB_END(p[prev])) {
562 		merged = min_t(sector_t, sectors, BB_END(p[prev]) - s);
563 	} else {
564 		merged = min_t(sector_t, sectors, BB_MAX_LEN - BB_LEN(p[prev]));
565 		if ((prev + 1) < bb->count &&
566 		    merged > (BB_OFFSET(p[prev + 1]) - BB_END(p[prev]))) {
567 			merged = BB_OFFSET(p[prev + 1]) - BB_END(p[prev]);
568 		}
569 
570 		p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
571 				  BB_LEN(p[prev]) + merged, bad->ack);
572 	}
573 
574 	return merged;
575 }
576 
577 /*
578  * 'Combine' is a special case which can_merge_front() is not able to
579  * handle: If a bad range (indexed by 'prev' from bad table) exactly
580  * starts as bad->start, and the bad range ahead of 'prev' (indexed by
581  * 'prev - 1' from bad table) exactly ends at where 'prev' starts, and
582  * the sum of their lengths does not exceed BB_MAX_LEN limitation, then
583  * these two bad range (from bad table) can be combined.
584  *
585  * Return 'true' if bad ranges indexed by 'prev' and 'prev - 1' from bad
586  * table can be combined.
587  */
588 static bool can_combine_front(struct badblocks *bb, int prev,
589 			      struct badblocks_context *bad)
590 {
591 	u64 *p = bb->page;
592 
593 	if ((prev > 0) &&
594 	    (BB_OFFSET(p[prev]) == bad->start) &&
595 	    (BB_END(p[prev - 1]) == BB_OFFSET(p[prev])) &&
596 	    (BB_LEN(p[prev - 1]) + BB_LEN(p[prev]) <= BB_MAX_LEN) &&
597 	    (BB_ACK(p[prev - 1]) == BB_ACK(p[prev])))
598 		return true;
599 	return false;
600 }
601 
602 /*
603  * Combine the bad ranges indexed by 'prev' and 'prev - 1' (from bad
604  * table) into one larger bad range, and the new range is indexed by
605  * 'prev - 1'.
606  * The caller of front_combine() will decrease bb->count, therefore
607  * it is unnecessary to clear p[perv] after front merge.
608  */
609 static void front_combine(struct badblocks *bb, int prev)
610 {
611 	u64 *p = bb->page;
612 
613 	p[prev - 1] = BB_MAKE(BB_OFFSET(p[prev - 1]),
614 			      BB_LEN(p[prev - 1]) + BB_LEN(p[prev]),
615 			      BB_ACK(p[prev]));
616 	if ((prev + 1) < bb->count)
617 		memmove(p + prev, p + prev + 1, (bb->count - prev - 1) * 8);
618 }
619 
620 /*
621  * Return 'true' if the range indicated by 'bad' is exactly forward
622  * overlapped with the bad range (from bad table) indexed by 'front'.
623  * Exactly forward overlap means the bad range (from bad table) indexed
624  * by 'prev' does not cover the whole range indicated by 'bad'.
625  */
626 static bool overlap_front(struct badblocks *bb, int front,
627 			  struct badblocks_context *bad)
628 {
629 	u64 *p = bb->page;
630 
631 	if (bad->start >= BB_OFFSET(p[front]) &&
632 	    bad->start < BB_END(p[front]))
633 		return true;
634 	return false;
635 }
636 
637 /*
638  * Return 'true' if the range indicated by 'bad' is exactly backward
639  * overlapped with the bad range (from bad table) indexed by 'behind'.
640  */
641 static bool overlap_behind(struct badblocks *bb, struct badblocks_context *bad,
642 			   int behind)
643 {
644 	u64 *p = bb->page;
645 
646 	if (bad->start < BB_OFFSET(p[behind]) &&
647 	    (bad->start + bad->len) > BB_OFFSET(p[behind]))
648 		return true;
649 	return false;
650 }
651 
652 /*
653  * Return 'true' if the range indicated by 'bad' can overwrite the bad
654  * range (from bad table) indexed by 'prev'.
655  *
656  * The range indicated by 'bad' can overwrite the bad range indexed by
657  * 'prev' when,
658  * 1) The whole range indicated by 'bad' can cover partial or whole bad
659  *    range (from bad table) indexed by 'prev'.
660  * 2) The ack value of 'bad' is larger or equal to the ack value of bad
661  *    range 'prev'.
662  *
663  * If the overwriting doesn't cover the whole bad range (from bad table)
664  * indexed by 'prev', new range might be split from existing bad range,
665  * 1) The overwrite covers head or tail part of existing bad range, 1
666  *    extra bad range will be split and added into the bad table.
667  * 2) The overwrite covers middle of existing bad range, 2 extra bad
668  *    ranges will be split (ahead and after the overwritten range) and
669  *    added into the bad table.
670  * The number of extra split ranges of the overwriting is stored in
671  * 'extra' and returned for the caller.
672  */
673 static bool can_front_overwrite(struct badblocks *bb, int prev,
674 				struct badblocks_context *bad, int *extra)
675 {
676 	u64 *p = bb->page;
677 	int len;
678 
679 	WARN_ON(!overlap_front(bb, prev, bad));
680 
681 	if (BB_ACK(p[prev]) >= bad->ack)
682 		return false;
683 
684 	if (BB_END(p[prev]) <= (bad->start + bad->len)) {
685 		len = BB_END(p[prev]) - bad->start;
686 		if (BB_OFFSET(p[prev]) == bad->start)
687 			*extra = 0;
688 		else
689 			*extra = 1;
690 
691 		bad->len = len;
692 	} else {
693 		if (BB_OFFSET(p[prev]) == bad->start)
694 			*extra = 1;
695 		else
696 		/*
697 		 * prev range will be split into two, beside the overwritten
698 		 * one, an extra slot needed from bad table.
699 		 */
700 			*extra = 2;
701 	}
702 
703 	if ((bb->count + (*extra)) > MAX_BADBLOCKS)
704 		return false;
705 
706 	return true;
707 }
708 
709 /*
710  * Do the overwrite from the range indicated by 'bad' to the bad range
711  * (from bad table) indexed by 'prev'.
712  * The previously called can_front_overwrite() will provide how many
713  * extra bad range(s) might be split and added into the bad table. All
714  * the splitting cases in the bad table will be handled here.
715  */
716 static int front_overwrite(struct badblocks *bb, int prev,
717 			   struct badblocks_context *bad, int extra)
718 {
719 	u64 *p = bb->page;
720 	sector_t orig_end = BB_END(p[prev]);
721 	int orig_ack = BB_ACK(p[prev]);
722 
723 	switch (extra) {
724 	case 0:
725 		p[prev] = BB_MAKE(BB_OFFSET(p[prev]), BB_LEN(p[prev]),
726 				  bad->ack);
727 		break;
728 	case 1:
729 		if (BB_OFFSET(p[prev]) == bad->start) {
730 			p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
731 					  bad->len, bad->ack);
732 			memmove(p + prev + 2, p + prev + 1,
733 				(bb->count - prev - 1) * 8);
734 			p[prev + 1] = BB_MAKE(bad->start + bad->len,
735 					      orig_end - BB_END(p[prev]),
736 					      orig_ack);
737 		} else {
738 			p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
739 					  bad->start - BB_OFFSET(p[prev]),
740 					  orig_ack);
741 			/*
742 			 * prev +2 -> prev + 1 + 1, which is for,
743 			 * 1) prev + 1: the slot index of the previous one
744 			 * 2) + 1: one more slot for extra being 1.
745 			 */
746 			memmove(p + prev + 2, p + prev + 1,
747 				(bb->count - prev - 1) * 8);
748 			p[prev + 1] = BB_MAKE(bad->start, bad->len, bad->ack);
749 		}
750 		break;
751 	case 2:
752 		p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
753 				  bad->start - BB_OFFSET(p[prev]),
754 				  orig_ack);
755 		/*
756 		 * prev + 3 -> prev + 1 + 2, which is for,
757 		 * 1) prev + 1: the slot index of the previous one
758 		 * 2) + 2: two more slots for extra being 2.
759 		 */
760 		memmove(p + prev + 3, p + prev + 1,
761 			(bb->count - prev - 1) * 8);
762 		p[prev + 1] = BB_MAKE(bad->start, bad->len, bad->ack);
763 		p[prev + 2] = BB_MAKE(BB_END(p[prev + 1]),
764 				      orig_end - BB_END(p[prev + 1]),
765 				      orig_ack);
766 		break;
767 	default:
768 		break;
769 	}
770 
771 	return bad->len;
772 }
773 
774 /*
775  * Explicitly insert a range indicated by 'bad' to the bad table, where
776  * the location is indexed by 'at'.
777  */
778 static int insert_at(struct badblocks *bb, int at, struct badblocks_context *bad)
779 {
780 	u64 *p = bb->page;
781 	int len;
782 
783 	WARN_ON(badblocks_full(bb));
784 
785 	len = min_t(sector_t, bad->len, BB_MAX_LEN);
786 	if (at < bb->count)
787 		memmove(p + at + 1, p + at, (bb->count - at) * 8);
788 	p[at] = BB_MAKE(bad->start, len, bad->ack);
789 
790 	return len;
791 }
792 
793 static void badblocks_update_acked(struct badblocks *bb)
794 {
795 	bool unacked = false;
796 	u64 *p = bb->page;
797 	int i;
798 
799 	if (!bb->unacked_exist)
800 		return;
801 
802 	for (i = 0; i < bb->count ; i++) {
803 		if (!BB_ACK(p[i])) {
804 			unacked = true;
805 			break;
806 		}
807 	}
808 
809 	if (!unacked)
810 		bb->unacked_exist = 0;
811 }
812 
813 /*
814  * Return 'true' if the range indicated by 'bad' is exactly backward
815  * overlapped with the bad range (from bad table) indexed by 'behind'.
816  */
817 static bool try_adjacent_combine(struct badblocks *bb, int prev)
818 {
819 	u64 *p = bb->page;
820 
821 	if (prev >= 0 && (prev + 1) < bb->count &&
822 	    BB_END(p[prev]) == BB_OFFSET(p[prev + 1]) &&
823 	    (BB_LEN(p[prev]) + BB_LEN(p[prev + 1])) <= BB_MAX_LEN &&
824 	    BB_ACK(p[prev]) == BB_ACK(p[prev + 1])) {
825 		p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
826 				  BB_LEN(p[prev]) + BB_LEN(p[prev + 1]),
827 				  BB_ACK(p[prev]));
828 
829 		if ((prev + 2) < bb->count)
830 			memmove(p + prev + 1, p + prev + 2,
831 				(bb->count -  (prev + 2)) * 8);
832 		bb->count--;
833 		return true;
834 	}
835 	return false;
836 }
837 
838 /* Do exact work to set bad block range into the bad block table */
839 static bool _badblocks_set(struct badblocks *bb, sector_t s, sector_t sectors,
840 			   int acknowledged)
841 {
842 	int len = 0, added = 0;
843 	struct badblocks_context bad;
844 	int prev = -1, hint = -1;
845 	unsigned long flags;
846 	u64 *p;
847 
848 	if (bb->shift < 0)
849 		/* badblocks are disabled */
850 		return false;
851 
852 	if (sectors == 0)
853 		/* Invalid sectors number */
854 		return false;
855 
856 	if (bb->shift) {
857 		/* round the start down, and the end up */
858 		sector_t next = s + sectors;
859 
860 		rounddown(s, 1 << bb->shift);
861 		roundup(next, 1 << bb->shift);
862 		sectors = next - s;
863 	}
864 
865 	write_seqlock_irqsave(&bb->lock, flags);
866 
867 	bad.ack = acknowledged;
868 	p = bb->page;
869 
870 re_insert:
871 	bad.start = s;
872 	bad.len = sectors;
873 	len = 0;
874 
875 	if (badblocks_full(bb))
876 		goto out;
877 
878 	if (badblocks_empty(bb)) {
879 		len = insert_at(bb, 0, &bad);
880 		bb->count++;
881 		added++;
882 		goto update_sectors;
883 	}
884 
885 	prev = prev_badblocks(bb, &bad, hint);
886 
887 	/* start before all badblocks */
888 	if (prev < 0) {
889 		/* insert on the first */
890 		if (bad.len > (BB_OFFSET(p[0]) - bad.start))
891 			bad.len = BB_OFFSET(p[0]) - bad.start;
892 		len = insert_at(bb, 0, &bad);
893 		bb->count++;
894 		added++;
895 		hint = ++prev;
896 		goto update_sectors;
897 	}
898 
899 	/* in case p[prev-1] can be merged with p[prev] */
900 	if (can_combine_front(bb, prev, &bad)) {
901 		front_combine(bb, prev);
902 		bb->count--;
903 		added++;
904 		hint = prev;
905 		goto update_sectors;
906 	}
907 
908 	if (can_merge_front(bb, prev, &bad)) {
909 		len = front_merge(bb, prev, &bad);
910 		added++;
911 		hint = prev;
912 		goto update_sectors;
913 	}
914 
915 	if (overlap_front(bb, prev, &bad)) {
916 		int extra = 0;
917 
918 		if (!can_front_overwrite(bb, prev, &bad, &extra)) {
919 			if (extra > 0)
920 				goto out;
921 
922 			len = min_t(sector_t,
923 				    BB_END(p[prev]) - s, sectors);
924 			hint = prev;
925 			goto update_sectors;
926 		}
927 
928 		len = front_overwrite(bb, prev, &bad, extra);
929 		added++;
930 		bb->count += extra;
931 
932 		if (can_combine_front(bb, prev, &bad)) {
933 			front_combine(bb, prev);
934 			bb->count--;
935 		}
936 
937 		hint = prev;
938 		goto update_sectors;
939 	}
940 
941 	/* cannot merge and there is space in bad table */
942 	if ((prev + 1) < bb->count &&
943 	    overlap_behind(bb, &bad, prev + 1))
944 		bad.len = min_t(sector_t,
945 				bad.len, BB_OFFSET(p[prev + 1]) - bad.start);
946 
947 	len = insert_at(bb, prev + 1, &bad);
948 	bb->count++;
949 	added++;
950 	hint = ++prev;
951 
952 update_sectors:
953 	s += len;
954 	sectors -= len;
955 
956 	if (sectors > 0)
957 		goto re_insert;
958 
959 	/*
960 	 * Check whether the following already set range can be
961 	 * merged. (prev < 0) condition is not handled here,
962 	 * because it's already complicated enough.
963 	 */
964 	try_adjacent_combine(bb, prev);
965 
966 out:
967 	if (added) {
968 		set_changed(bb);
969 
970 		if (!acknowledged)
971 			bb->unacked_exist = 1;
972 		else
973 			badblocks_update_acked(bb);
974 	}
975 
976 	write_sequnlock_irqrestore(&bb->lock, flags);
977 
978 	return sectors == 0;
979 }
980 
981 /*
982  * Clear the bad block range from bad block table which is front overlapped
983  * with the clearing range. The return value is how many sectors from an
984  * already set bad block range are cleared. If the whole bad block range is
985  * covered by the clearing range and fully cleared, 'delete' is set as 1 for
986  * the caller to reduce bb->count.
987  */
988 static int front_clear(struct badblocks *bb, int prev,
989 		       struct badblocks_context *bad, int *deleted)
990 {
991 	sector_t sectors = bad->len;
992 	sector_t s = bad->start;
993 	u64 *p = bb->page;
994 	int cleared = 0;
995 
996 	*deleted = 0;
997 	if (s == BB_OFFSET(p[prev])) {
998 		if (BB_LEN(p[prev]) > sectors) {
999 			p[prev] = BB_MAKE(BB_OFFSET(p[prev]) + sectors,
1000 					  BB_LEN(p[prev]) - sectors,
1001 					  BB_ACK(p[prev]));
1002 			cleared = sectors;
1003 		} else {
1004 			/* BB_LEN(p[prev]) <= sectors */
1005 			cleared = BB_LEN(p[prev]);
1006 			if ((prev + 1) < bb->count)
1007 				memmove(p + prev, p + prev + 1,
1008 				       (bb->count - prev - 1) * 8);
1009 			*deleted = 1;
1010 		}
1011 	} else if (s > BB_OFFSET(p[prev])) {
1012 		if (BB_END(p[prev]) <= (s + sectors)) {
1013 			cleared = BB_END(p[prev]) - s;
1014 			p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
1015 					  s - BB_OFFSET(p[prev]),
1016 					  BB_ACK(p[prev]));
1017 		} else {
1018 			/* Splitting is handled in front_splitting_clear() */
1019 			BUG();
1020 		}
1021 	}
1022 
1023 	return cleared;
1024 }
1025 
1026 /*
1027  * Handle the condition that the clearing range hits middle of an already set
1028  * bad block range from bad block table. In this condition the existing bad
1029  * block range is split into two after the middle part is cleared.
1030  */
1031 static int front_splitting_clear(struct badblocks *bb, int prev,
1032 				  struct badblocks_context *bad)
1033 {
1034 	u64 *p = bb->page;
1035 	u64 end = BB_END(p[prev]);
1036 	int ack = BB_ACK(p[prev]);
1037 	sector_t sectors = bad->len;
1038 	sector_t s = bad->start;
1039 
1040 	p[prev] = BB_MAKE(BB_OFFSET(p[prev]),
1041 			  s - BB_OFFSET(p[prev]),
1042 			  ack);
1043 	memmove(p + prev + 2, p + prev + 1, (bb->count - prev - 1) * 8);
1044 	p[prev + 1] = BB_MAKE(s + sectors, end - s - sectors, ack);
1045 	return sectors;
1046 }
1047 
1048 /* Do the exact work to clear bad block range from the bad block table */
1049 static bool _badblocks_clear(struct badblocks *bb, sector_t s, sector_t sectors)
1050 {
1051 	struct badblocks_context bad;
1052 	int prev = -1, hint = -1;
1053 	int len = 0, cleared = 0;
1054 	u64 *p;
1055 
1056 	if (bb->shift < 0)
1057 		/* badblocks are disabled */
1058 		return false;
1059 
1060 	if (sectors == 0)
1061 		/* Invalid sectors number */
1062 		return false;
1063 
1064 	if (bb->shift) {
1065 		sector_t target;
1066 
1067 		/* When clearing we round the start up and the end down.
1068 		 * This should not matter as the shift should align with
1069 		 * the block size and no rounding should ever be needed.
1070 		 * However it is better the think a block is bad when it
1071 		 * isn't than to think a block is not bad when it is.
1072 		 */
1073 		target = s + sectors;
1074 		roundup(s, 1 << bb->shift);
1075 		rounddown(target, 1 << bb->shift);
1076 		sectors = target - s;
1077 	}
1078 
1079 	write_seqlock_irq(&bb->lock);
1080 
1081 	bad.ack = true;
1082 	p = bb->page;
1083 
1084 re_clear:
1085 	bad.start = s;
1086 	bad.len = sectors;
1087 
1088 	if (badblocks_empty(bb)) {
1089 		len = sectors;
1090 		cleared++;
1091 		goto update_sectors;
1092 	}
1093 
1094 
1095 	prev = prev_badblocks(bb, &bad, hint);
1096 
1097 	/* Start before all badblocks */
1098 	if (prev < 0) {
1099 		if (overlap_behind(bb, &bad, 0)) {
1100 			len = BB_OFFSET(p[0]) - s;
1101 			hint = 0;
1102 		} else {
1103 			len = sectors;
1104 		}
1105 		/*
1106 		 * Both situations are to clear non-bad range,
1107 		 * should be treated as successful
1108 		 */
1109 		cleared++;
1110 		goto update_sectors;
1111 	}
1112 
1113 	/* Start after all badblocks */
1114 	if ((prev + 1) >= bb->count && !overlap_front(bb, prev, &bad)) {
1115 		len = sectors;
1116 		cleared++;
1117 		goto update_sectors;
1118 	}
1119 
1120 	/* Clear will split a bad record but the table is full */
1121 	if (badblocks_full(bb) && (BB_OFFSET(p[prev]) < bad.start) &&
1122 	    (BB_END(p[prev]) > (bad.start + sectors))) {
1123 		len = sectors;
1124 		goto update_sectors;
1125 	}
1126 
1127 	if (overlap_front(bb, prev, &bad)) {
1128 		if ((BB_OFFSET(p[prev]) < bad.start) &&
1129 		    (BB_END(p[prev]) > (bad.start + bad.len))) {
1130 			/* Splitting */
1131 			if ((bb->count + 1) <= MAX_BADBLOCKS) {
1132 				len = front_splitting_clear(bb, prev, &bad);
1133 				bb->count += 1;
1134 				cleared++;
1135 			} else {
1136 				/* No space to split, give up */
1137 				len = sectors;
1138 			}
1139 		} else {
1140 			int deleted = 0;
1141 
1142 			len = front_clear(bb, prev, &bad, &deleted);
1143 			bb->count -= deleted;
1144 			cleared++;
1145 			hint = prev;
1146 		}
1147 
1148 		goto update_sectors;
1149 	}
1150 
1151 	/* Not front overlap, but behind overlap */
1152 	if ((prev + 1) < bb->count && overlap_behind(bb, &bad, prev + 1)) {
1153 		len = BB_OFFSET(p[prev + 1]) - bad.start;
1154 		hint = prev + 1;
1155 		/* Clear non-bad range should be treated as successful */
1156 		cleared++;
1157 		goto update_sectors;
1158 	}
1159 
1160 	/* Not cover any badblocks range in the table */
1161 	len = sectors;
1162 	/* Clear non-bad range should be treated as successful */
1163 	cleared++;
1164 
1165 update_sectors:
1166 	s += len;
1167 	sectors -= len;
1168 
1169 	if (sectors > 0)
1170 		goto re_clear;
1171 
1172 	if (cleared) {
1173 		badblocks_update_acked(bb);
1174 		set_changed(bb);
1175 	}
1176 
1177 	write_sequnlock_irq(&bb->lock);
1178 
1179 	if (!cleared)
1180 		return false;
1181 
1182 	return true;
1183 }
1184 
1185 /* Do the exact work to check bad blocks range from the bad block table */
1186 static int _badblocks_check(struct badblocks *bb, sector_t s, sector_t sectors,
1187 			    sector_t *first_bad, sector_t *bad_sectors)
1188 {
1189 	int prev = -1, hint = -1, set = 0;
1190 	struct badblocks_context bad;
1191 	int unacked_badblocks = 0;
1192 	int acked_badblocks = 0;
1193 	u64 *p = bb->page;
1194 	int len, rv;
1195 
1196 re_check:
1197 	bad.start = s;
1198 	bad.len = sectors;
1199 
1200 	if (badblocks_empty(bb)) {
1201 		len = sectors;
1202 		goto update_sectors;
1203 	}
1204 
1205 	prev = prev_badblocks(bb, &bad, hint);
1206 
1207 	/* start after all badblocks */
1208 	if ((prev >= 0) &&
1209 	    ((prev + 1) >= bb->count) && !overlap_front(bb, prev, &bad)) {
1210 		len = sectors;
1211 		goto update_sectors;
1212 	}
1213 
1214 	/* Overlapped with front badblocks record */
1215 	if ((prev >= 0) && overlap_front(bb, prev, &bad)) {
1216 		if (BB_ACK(p[prev]))
1217 			acked_badblocks++;
1218 		else
1219 			unacked_badblocks++;
1220 
1221 		if (BB_END(p[prev]) >= (s + sectors))
1222 			len = sectors;
1223 		else
1224 			len = BB_END(p[prev]) - s;
1225 
1226 		if (set == 0) {
1227 			*first_bad = BB_OFFSET(p[prev]);
1228 			*bad_sectors = BB_LEN(p[prev]);
1229 			set = 1;
1230 		}
1231 		goto update_sectors;
1232 	}
1233 
1234 	/* Not front overlap, but behind overlap */
1235 	if ((prev + 1) < bb->count && overlap_behind(bb, &bad, prev + 1)) {
1236 		len = BB_OFFSET(p[prev + 1]) - bad.start;
1237 		hint = prev + 1;
1238 		goto update_sectors;
1239 	}
1240 
1241 	/* not cover any badblocks range in the table */
1242 	len = sectors;
1243 
1244 update_sectors:
1245 	/* This situation should never happen */
1246 	WARN_ON(sectors < len);
1247 
1248 	s += len;
1249 	sectors -= len;
1250 
1251 	if (sectors > 0)
1252 		goto re_check;
1253 
1254 	if (unacked_badblocks > 0)
1255 		rv = -1;
1256 	else if (acked_badblocks > 0)
1257 		rv = 1;
1258 	else
1259 		rv = 0;
1260 
1261 	return rv;
1262 }
1263 
1264 /**
1265  * badblocks_check() - check a given range for bad sectors
1266  * @bb:		the badblocks structure that holds all badblock information
1267  * @s:		sector (start) at which to check for badblocks
1268  * @sectors:	number of sectors to check for badblocks
1269  * @first_bad:	pointer to store location of the first badblock
1270  * @bad_sectors: pointer to store number of badblocks after @first_bad
1271  *
1272  * We can record which blocks on each device are 'bad' and so just
1273  * fail those blocks, or that stripe, rather than the whole device.
1274  * Entries in the bad-block table are 64bits wide.  This comprises:
1275  * Length of bad-range, in sectors: 0-511 for lengths 1-512
1276  * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
1277  *  A 'shift' can be set so that larger blocks are tracked and
1278  *  consequently larger devices can be covered.
1279  * 'Acknowledged' flag - 1 bit. - the most significant bit.
1280  *
1281  * Locking of the bad-block table uses a seqlock so badblocks_check
1282  * might need to retry if it is very unlucky.
1283  * We will sometimes want to check for bad blocks in a bi_end_io function,
1284  * so we use the write_seqlock_irq variant.
1285  *
1286  * When looking for a bad block we specify a range and want to
1287  * know if any block in the range is bad.  So we binary-search
1288  * to the last range that starts at-or-before the given endpoint,
1289  * (or "before the sector after the target range")
1290  * then see if it ends after the given start.
1291  *
1292  * Return:
1293  *  0: there are no known bad blocks in the range
1294  *  1: there are known bad block which are all acknowledged
1295  * -1: there are bad blocks which have not yet been acknowledged in metadata.
1296  * plus the start/length of the first bad section we overlap.
1297  */
1298 int badblocks_check(struct badblocks *bb, sector_t s, sector_t sectors,
1299 			sector_t *first_bad, sector_t *bad_sectors)
1300 {
1301 	unsigned int seq;
1302 	int rv;
1303 
1304 	WARN_ON(bb->shift < 0 || sectors == 0);
1305 
1306 	if (bb->shift > 0) {
1307 		/* round the start down, and the end up */
1308 		sector_t target = s + sectors;
1309 
1310 		rounddown(s, 1 << bb->shift);
1311 		roundup(target, 1 << bb->shift);
1312 		sectors = target - s;
1313 	}
1314 
1315 retry:
1316 	seq = read_seqbegin(&bb->lock);
1317 	rv = _badblocks_check(bb, s, sectors, first_bad, bad_sectors);
1318 	if (read_seqretry(&bb->lock, seq))
1319 		goto retry;
1320 
1321 	return rv;
1322 }
1323 EXPORT_SYMBOL_GPL(badblocks_check);
1324 
1325 /**
1326  * badblocks_set() - Add a range of bad blocks to the table.
1327  * @bb:		the badblocks structure that holds all badblock information
1328  * @s:		first sector to mark as bad
1329  * @sectors:	number of sectors to mark as bad
1330  * @acknowledged: weather to mark the bad sectors as acknowledged
1331  *
1332  * This might extend the table, or might contract it if two adjacent ranges
1333  * can be merged. We binary-search to find the 'insertion' point, then
1334  * decide how best to handle it.
1335  *
1336  * Return:
1337  *  true: success
1338  *  false: failed to set badblocks (out of space). Parital setting will be
1339  *  treated as failure.
1340  */
1341 bool badblocks_set(struct badblocks *bb, sector_t s, sector_t sectors,
1342 		   int acknowledged)
1343 {
1344 	return _badblocks_set(bb, s, sectors, acknowledged);
1345 }
1346 EXPORT_SYMBOL_GPL(badblocks_set);
1347 
1348 /**
1349  * badblocks_clear() - Remove a range of bad blocks to the table.
1350  * @bb:		the badblocks structure that holds all badblock information
1351  * @s:		first sector to mark as bad
1352  * @sectors:	number of sectors to mark as bad
1353  *
1354  * This may involve extending the table if we spilt a region,
1355  * but it must not fail.  So if the table becomes full, we just
1356  * drop the remove request.
1357  *
1358  * Return:
1359  *  true: success
1360  *  false: failed to clear badblocks
1361  */
1362 bool badblocks_clear(struct badblocks *bb, sector_t s, sector_t sectors)
1363 {
1364 	return _badblocks_clear(bb, s, sectors);
1365 }
1366 EXPORT_SYMBOL_GPL(badblocks_clear);
1367 
1368 /**
1369  * ack_all_badblocks() - Acknowledge all bad blocks in a list.
1370  * @bb:		the badblocks structure that holds all badblock information
1371  *
1372  * This only succeeds if ->changed is clear.  It is used by
1373  * in-kernel metadata updates
1374  */
1375 void ack_all_badblocks(struct badblocks *bb)
1376 {
1377 	if (bb->page == NULL || bb->changed)
1378 		/* no point even trying */
1379 		return;
1380 	write_seqlock_irq(&bb->lock);
1381 
1382 	if (bb->changed == 0 && bb->unacked_exist) {
1383 		u64 *p = bb->page;
1384 		int i;
1385 
1386 		for (i = 0; i < bb->count ; i++) {
1387 			if (!BB_ACK(p[i])) {
1388 				sector_t start = BB_OFFSET(p[i]);
1389 				int len = BB_LEN(p[i]);
1390 
1391 				p[i] = BB_MAKE(start, len, 1);
1392 			}
1393 		}
1394 
1395 		for (i = 0; i < bb->count ; i++)
1396 			while (try_adjacent_combine(bb, i))
1397 				;
1398 
1399 		bb->unacked_exist = 0;
1400 	}
1401 	write_sequnlock_irq(&bb->lock);
1402 }
1403 EXPORT_SYMBOL_GPL(ack_all_badblocks);
1404 
1405 /**
1406  * badblocks_show() - sysfs access to bad-blocks list
1407  * @bb:		the badblocks structure that holds all badblock information
1408  * @page:	buffer received from sysfs
1409  * @unack:	weather to show unacknowledged badblocks
1410  *
1411  * Return:
1412  *  Length of returned data
1413  */
1414 ssize_t badblocks_show(struct badblocks *bb, char *page, int unack)
1415 {
1416 	size_t len;
1417 	int i;
1418 	u64 *p = bb->page;
1419 	unsigned seq;
1420 
1421 	if (bb->shift < 0)
1422 		return 0;
1423 
1424 retry:
1425 	seq = read_seqbegin(&bb->lock);
1426 
1427 	len = 0;
1428 	i = 0;
1429 
1430 	while (len < PAGE_SIZE && i < bb->count) {
1431 		sector_t s = BB_OFFSET(p[i]);
1432 		unsigned int length = BB_LEN(p[i]);
1433 		int ack = BB_ACK(p[i]);
1434 
1435 		i++;
1436 
1437 		if (unack && ack)
1438 			continue;
1439 
1440 		len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
1441 				(unsigned long long)s << bb->shift,
1442 				length << bb->shift);
1443 	}
1444 	if (unack && len == 0)
1445 		bb->unacked_exist = 0;
1446 
1447 	if (read_seqretry(&bb->lock, seq))
1448 		goto retry;
1449 
1450 	return len;
1451 }
1452 EXPORT_SYMBOL_GPL(badblocks_show);
1453 
1454 /**
1455  * badblocks_store() - sysfs access to bad-blocks list
1456  * @bb:		the badblocks structure that holds all badblock information
1457  * @page:	buffer received from sysfs
1458  * @len:	length of data received from sysfs
1459  * @unack:	weather to show unacknowledged badblocks
1460  *
1461  * Return:
1462  *  Length of the buffer processed or -ve error.
1463  */
1464 ssize_t badblocks_store(struct badblocks *bb, const char *page, size_t len,
1465 			int unack)
1466 {
1467 	unsigned long long sector;
1468 	int length;
1469 	char newline;
1470 
1471 	switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
1472 	case 3:
1473 		if (newline != '\n')
1474 			return -EINVAL;
1475 		fallthrough;
1476 	case 2:
1477 		if (length <= 0)
1478 			return -EINVAL;
1479 		break;
1480 	default:
1481 		return -EINVAL;
1482 	}
1483 
1484 	if (!badblocks_set(bb, sector, length, !unack))
1485 		return -ENOSPC;
1486 
1487 	return len;
1488 }
1489 EXPORT_SYMBOL_GPL(badblocks_store);
1490 
1491 static int __badblocks_init(struct device *dev, struct badblocks *bb,
1492 		int enable)
1493 {
1494 	bb->dev = dev;
1495 	bb->count = 0;
1496 	if (enable)
1497 		bb->shift = 0;
1498 	else
1499 		bb->shift = -1;
1500 	if (dev)
1501 		bb->page = devm_kzalloc(dev, PAGE_SIZE, GFP_KERNEL);
1502 	else
1503 		bb->page = kzalloc(PAGE_SIZE, GFP_KERNEL);
1504 	if (!bb->page) {
1505 		bb->shift = -1;
1506 		return -ENOMEM;
1507 	}
1508 	seqlock_init(&bb->lock);
1509 
1510 	return 0;
1511 }
1512 
1513 /**
1514  * badblocks_init() - initialize the badblocks structure
1515  * @bb:		the badblocks structure that holds all badblock information
1516  * @enable:	weather to enable badblocks accounting
1517  *
1518  * Return:
1519  *  0: success
1520  *  -ve errno: on error
1521  */
1522 int badblocks_init(struct badblocks *bb, int enable)
1523 {
1524 	return __badblocks_init(NULL, bb, enable);
1525 }
1526 EXPORT_SYMBOL_GPL(badblocks_init);
1527 
1528 int devm_init_badblocks(struct device *dev, struct badblocks *bb)
1529 {
1530 	if (!bb)
1531 		return -EINVAL;
1532 	return __badblocks_init(dev, bb, 1);
1533 }
1534 EXPORT_SYMBOL_GPL(devm_init_badblocks);
1535 
1536 /**
1537  * badblocks_exit() - free the badblocks structure
1538  * @bb:		the badblocks structure that holds all badblock information
1539  */
1540 void badblocks_exit(struct badblocks *bb)
1541 {
1542 	if (!bb)
1543 		return;
1544 	if (bb->dev)
1545 		devm_kfree(bb->dev, bb->page);
1546 	else
1547 		kfree(bb->page);
1548 	bb->page = NULL;
1549 }
1550 EXPORT_SYMBOL_GPL(badblocks_exit);
1551