xref: /openbmc/linux/lib/raid6/recov.c (revision d670b479)
1 /* -*- linux-c -*- ------------------------------------------------------- *
2  *
3  *   Copyright 2002 H. Peter Anvin - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12 
13 /*
14  * raid6/recov.c
15  *
16  * RAID-6 data recovery in dual failure mode.  In single failure mode,
17  * use the RAID-5 algorithm (or, in the case of Q failure, just reconstruct
18  * the syndrome.)
19  */
20 
21 #include <linux/export.h>
22 #include <linux/raid/pq.h>
23 
24 /* Recover two failed data blocks. */
25 void raid6_2data_recov_intx1(int disks, size_t bytes, int faila, int failb,
26 		       void **ptrs)
27 {
28 	u8 *p, *q, *dp, *dq;
29 	u8 px, qx, db;
30 	const u8 *pbmul;	/* P multiplier table for B data */
31 	const u8 *qmul;		/* Q multiplier table (for both) */
32 
33 	p = (u8 *)ptrs[disks-2];
34 	q = (u8 *)ptrs[disks-1];
35 
36 	/* Compute syndrome with zero for the missing data pages
37 	   Use the dead data pages as temporary storage for
38 	   delta p and delta q */
39 	dp = (u8 *)ptrs[faila];
40 	ptrs[faila] = (void *)raid6_empty_zero_page;
41 	ptrs[disks-2] = dp;
42 	dq = (u8 *)ptrs[failb];
43 	ptrs[failb] = (void *)raid6_empty_zero_page;
44 	ptrs[disks-1] = dq;
45 
46 	raid6_call.gen_syndrome(disks, bytes, ptrs);
47 
48 	/* Restore pointer table */
49 	ptrs[faila]   = dp;
50 	ptrs[failb]   = dq;
51 	ptrs[disks-2] = p;
52 	ptrs[disks-1] = q;
53 
54 	/* Now, pick the proper data tables */
55 	pbmul = raid6_gfmul[raid6_gfexi[failb-faila]];
56 	qmul  = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]]];
57 
58 	/* Now do it... */
59 	while ( bytes-- ) {
60 		px    = *p ^ *dp;
61 		qx    = qmul[*q ^ *dq];
62 		*dq++ = db = pbmul[px] ^ qx; /* Reconstructed B */
63 		*dp++ = db ^ px; /* Reconstructed A */
64 		p++; q++;
65 	}
66 }
67 
68 /* Recover failure of one data block plus the P block */
69 void raid6_datap_recov_intx1(int disks, size_t bytes, int faila, void **ptrs)
70 {
71 	u8 *p, *q, *dq;
72 	const u8 *qmul;		/* Q multiplier table */
73 
74 	p = (u8 *)ptrs[disks-2];
75 	q = (u8 *)ptrs[disks-1];
76 
77 	/* Compute syndrome with zero for the missing data page
78 	   Use the dead data page as temporary storage for delta q */
79 	dq = (u8 *)ptrs[faila];
80 	ptrs[faila] = (void *)raid6_empty_zero_page;
81 	ptrs[disks-1] = dq;
82 
83 	raid6_call.gen_syndrome(disks, bytes, ptrs);
84 
85 	/* Restore pointer table */
86 	ptrs[faila]   = dq;
87 	ptrs[disks-1] = q;
88 
89 	/* Now, pick the proper data tables */
90 	qmul  = raid6_gfmul[raid6_gfinv[raid6_gfexp[faila]]];
91 
92 	/* Now do it... */
93 	while ( bytes-- ) {
94 		*p++ ^= *dq = qmul[*q ^ *dq];
95 		q++; dq++;
96 	}
97 }
98 
99 
100 const struct raid6_recov_calls raid6_recov_intx1 = {
101 	.data2 = raid6_2data_recov_intx1,
102 	.datap = raid6_datap_recov_intx1,
103 	.valid = NULL,
104 	.name = "intx1",
105 	.priority = 0,
106 };
107 
108 #ifndef __KERNEL__
109 /* Testing only */
110 
111 /* Recover two failed blocks. */
112 void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, void **ptrs)
113 {
114 	if ( faila > failb ) {
115 		int tmp = faila;
116 		faila = failb;
117 		failb = tmp;
118 	}
119 
120 	if ( failb == disks-1 ) {
121 		if ( faila == disks-2 ) {
122 			/* P+Q failure.  Just rebuild the syndrome. */
123 			raid6_call.gen_syndrome(disks, bytes, ptrs);
124 		} else {
125 			/* data+Q failure.  Reconstruct data from P,
126 			   then rebuild syndrome. */
127 			/* NOT IMPLEMENTED - equivalent to RAID-5 */
128 		}
129 	} else {
130 		if ( failb == disks-2 ) {
131 			/* data+P failure. */
132 			raid6_datap_recov(disks, bytes, faila, ptrs);
133 		} else {
134 			/* data+data failure. */
135 			raid6_2data_recov(disks, bytes, faila, failb, ptrs);
136 		}
137 	}
138 }
139 
140 #endif
141