rf_dagfuncs.c revision 1.24 1 1.24 christos /* $NetBSD: rf_dagfuncs.c,v 1.24 2005/05/29 22:03:09 christos Exp $ */
2 1.1 oster /*
3 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
4 1.1 oster * All rights reserved.
5 1.1 oster *
6 1.1 oster * Author: Mark Holland, William V. Courtright II
7 1.1 oster *
8 1.1 oster * Permission to use, copy, modify and distribute this software and
9 1.1 oster * its documentation is hereby granted, provided that both the copyright
10 1.1 oster * notice and this permission notice appear in all copies of the
11 1.1 oster * software, derivative works or modified versions, and any portions
12 1.1 oster * thereof, and that both notices appear in supporting documentation.
13 1.1 oster *
14 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 oster *
18 1.1 oster * Carnegie Mellon requests users of this software to return to
19 1.1 oster *
20 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 oster * School of Computer Science
22 1.1 oster * Carnegie Mellon University
23 1.1 oster * Pittsburgh PA 15213-3890
24 1.1 oster *
25 1.1 oster * any improvements or extensions that they make and grant Carnegie the
26 1.1 oster * rights to redistribute these changes.
27 1.1 oster */
28 1.1 oster
29 1.1 oster /*
30 1.1 oster * dagfuncs.c -- DAG node execution routines
31 1.1 oster *
32 1.1 oster * Rules:
33 1.1 oster * 1. Every DAG execution function must eventually cause node->status to
34 1.1 oster * get set to "good" or "bad", and "FinishNode" to be called. In the
35 1.1 oster * case of nodes that complete immediately (xor, NullNodeFunc, etc),
36 1.1 oster * the node execution function can do these two things directly. In
37 1.1 oster * the case of nodes that have to wait for some event (a disk read to
38 1.1 oster * complete, a lock to be released, etc) to occur before they can
39 1.1 oster * complete, this is typically achieved by having whatever module
40 1.1 oster * is doing the operation call GenericWakeupFunc upon completion.
41 1.1 oster * 2. DAG execution functions should check the status in the DAG header
42 1.1 oster * and NOP out their operations if the status is not "enable". However,
43 1.1 oster * execution functions that release resources must be sure to release
44 1.1 oster * them even when they NOP out the function that would use them.
45 1.1 oster * Functions that acquire resources should go ahead and acquire them
46 1.1 oster * even when they NOP, so that a downstream release node will not have
47 1.1 oster * to check to find out whether or not the acquire was suppressed.
48 1.1 oster */
49 1.8 lukem
50 1.8 lukem #include <sys/cdefs.h>
51 1.24 christos __KERNEL_RCSID(0, "$NetBSD: rf_dagfuncs.c,v 1.24 2005/05/29 22:03:09 christos Exp $");
52 1.1 oster
53 1.7 mrg #include <sys/param.h>
54 1.1 oster #include <sys/ioctl.h>
55 1.1 oster
56 1.1 oster #include "rf_archs.h"
57 1.1 oster #include "rf_raid.h"
58 1.1 oster #include "rf_dag.h"
59 1.1 oster #include "rf_layout.h"
60 1.1 oster #include "rf_etimer.h"
61 1.1 oster #include "rf_acctrace.h"
62 1.1 oster #include "rf_diskqueue.h"
63 1.1 oster #include "rf_dagfuncs.h"
64 1.1 oster #include "rf_general.h"
65 1.1 oster #include "rf_engine.h"
66 1.1 oster #include "rf_dagutils.h"
67 1.1 oster
68 1.1 oster #include "rf_kintf.h"
69 1.1 oster
70 1.1 oster #if RF_INCLUDE_PARITYLOGGING > 0
71 1.1 oster #include "rf_paritylog.h"
72 1.3 oster #endif /* RF_INCLUDE_PARITYLOGGING > 0 */
73 1.1 oster
74 1.3 oster int (*rf_DiskReadFunc) (RF_DagNode_t *);
75 1.3 oster int (*rf_DiskWriteFunc) (RF_DagNode_t *);
76 1.3 oster int (*rf_DiskReadUndoFunc) (RF_DagNode_t *);
77 1.3 oster int (*rf_DiskWriteUndoFunc) (RF_DagNode_t *);
78 1.3 oster int (*rf_DiskUnlockFunc) (RF_DagNode_t *);
79 1.3 oster int (*rf_DiskUnlockUndoFunc) (RF_DagNode_t *);
80 1.3 oster int (*rf_RegularXorUndoFunc) (RF_DagNode_t *);
81 1.3 oster int (*rf_SimpleXorUndoFunc) (RF_DagNode_t *);
82 1.3 oster int (*rf_RecoveryXorUndoFunc) (RF_DagNode_t *);
83 1.1 oster
84 1.14 oster /*****************************************************************************
85 1.1 oster * main (only) configuration routine for this module
86 1.14 oster ****************************************************************************/
87 1.23 perry int
88 1.15 oster rf_ConfigureDAGFuncs(RF_ShutdownList_t **listp)
89 1.3 oster {
90 1.23 perry RF_ASSERT(((sizeof(long) == 8) && RF_LONGSHIFT == 3) ||
91 1.14 oster ((sizeof(long) == 4) && RF_LONGSHIFT == 2));
92 1.3 oster rf_DiskReadFunc = rf_DiskReadFuncForThreads;
93 1.3 oster rf_DiskReadUndoFunc = rf_DiskUndoFunc;
94 1.3 oster rf_DiskWriteFunc = rf_DiskWriteFuncForThreads;
95 1.3 oster rf_DiskWriteUndoFunc = rf_DiskUndoFunc;
96 1.3 oster rf_DiskUnlockFunc = rf_DiskUnlockFuncForThreads;
97 1.3 oster rf_DiskUnlockUndoFunc = rf_NullNodeUndoFunc;
98 1.3 oster rf_RegularXorUndoFunc = rf_NullNodeUndoFunc;
99 1.3 oster rf_SimpleXorUndoFunc = rf_NullNodeUndoFunc;
100 1.3 oster rf_RecoveryXorUndoFunc = rf_NullNodeUndoFunc;
101 1.3 oster return (0);
102 1.1 oster }
103 1.1 oster
104 1.1 oster
105 1.1 oster
106 1.14 oster /*****************************************************************************
107 1.1 oster * the execution function associated with a terminate node
108 1.14 oster ****************************************************************************/
109 1.23 perry int
110 1.15 oster rf_TerminateFunc(RF_DagNode_t *node)
111 1.1 oster {
112 1.3 oster RF_ASSERT(node->dagHdr->numCommits == node->dagHdr->numCommitNodes);
113 1.3 oster node->status = rf_good;
114 1.3 oster return (rf_FinishNode(node, RF_THREAD_CONTEXT));
115 1.1 oster }
116 1.1 oster
117 1.23 perry int
118 1.15 oster rf_TerminateUndoFunc(RF_DagNode_t *node)
119 1.1 oster {
120 1.3 oster return (0);
121 1.1 oster }
122 1.1 oster
123 1.1 oster
124 1.15 oster /*****************************************************************************
125 1.1 oster * execution functions associated with a mirror node
126 1.1 oster *
127 1.1 oster * parameters:
128 1.1 oster *
129 1.1 oster * 0 - physical disk addres of data
130 1.1 oster * 1 - buffer for holding read data
131 1.1 oster * 2 - parity stripe ID
132 1.1 oster * 3 - flags
133 1.1 oster * 4 - physical disk address of mirror (parity)
134 1.1 oster *
135 1.15 oster ****************************************************************************/
136 1.1 oster
137 1.23 perry int
138 1.15 oster rf_DiskReadMirrorIdleFunc(RF_DagNode_t *node)
139 1.1 oster {
140 1.3 oster /* select the mirror copy with the shortest queue and fill in node
141 1.3 oster * parameters with physical disk address */
142 1.1 oster
143 1.3 oster rf_SelectMirrorDiskIdle(node);
144 1.3 oster return (rf_DiskReadFunc(node));
145 1.1 oster }
146 1.1 oster
147 1.11 oster #if (RF_INCLUDE_CHAINDECLUSTER > 0) || (RF_INCLUDE_INTERDECLUSTER > 0) || (RF_DEBUG_VALIDATE_DAG > 0)
148 1.23 perry int
149 1.15 oster rf_DiskReadMirrorPartitionFunc(RF_DagNode_t *node)
150 1.1 oster {
151 1.3 oster /* select the mirror copy with the shortest queue and fill in node
152 1.3 oster * parameters with physical disk address */
153 1.1 oster
154 1.3 oster rf_SelectMirrorDiskPartition(node);
155 1.3 oster return (rf_DiskReadFunc(node));
156 1.1 oster }
157 1.11 oster #endif
158 1.1 oster
159 1.23 perry int
160 1.15 oster rf_DiskReadMirrorUndoFunc(RF_DagNode_t *node)
161 1.1 oster {
162 1.3 oster return (0);
163 1.1 oster }
164 1.1 oster
165 1.1 oster
166 1.1 oster
167 1.1 oster #if RF_INCLUDE_PARITYLOGGING > 0
168 1.14 oster /*****************************************************************************
169 1.1 oster * the execution function associated with a parity log update node
170 1.14 oster ****************************************************************************/
171 1.23 perry int
172 1.15 oster rf_ParityLogUpdateFunc(RF_DagNode_t *node)
173 1.3 oster {
174 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
175 1.24 christos caddr_t bf = (caddr_t) node->params[1].p;
176 1.3 oster RF_ParityLogData_t *logData;
177 1.19 oster #if RF_ACC_TRACE > 0
178 1.3 oster RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
179 1.3 oster RF_Etimer_t timer;
180 1.19 oster #endif
181 1.3 oster
182 1.3 oster if (node->dagHdr->status == rf_enable) {
183 1.19 oster #if RF_ACC_TRACE > 0
184 1.3 oster RF_ETIMER_START(timer);
185 1.19 oster #endif
186 1.24 christos logData = rf_CreateParityLogData(RF_UPDATE, pda, bf,
187 1.3 oster (RF_Raid_t *) (node->dagHdr->raidPtr),
188 1.3 oster node->wakeFunc, (void *) node,
189 1.3 oster node->dagHdr->tracerec, timer);
190 1.3 oster if (logData)
191 1.3 oster rf_ParityLogAppend(logData, RF_FALSE, NULL, RF_FALSE);
192 1.3 oster else {
193 1.19 oster #if RF_ACC_TRACE > 0
194 1.3 oster RF_ETIMER_STOP(timer);
195 1.3 oster RF_ETIMER_EVAL(timer);
196 1.3 oster tracerec->plog_us += RF_ETIMER_VAL_US(timer);
197 1.19 oster #endif
198 1.3 oster (node->wakeFunc) (node, ENOMEM);
199 1.3 oster }
200 1.1 oster }
201 1.3 oster return (0);
202 1.1 oster }
203 1.1 oster
204 1.1 oster
205 1.15 oster /*****************************************************************************
206 1.1 oster * the execution function associated with a parity log overwrite node
207 1.15 oster ****************************************************************************/
208 1.23 perry int
209 1.15 oster rf_ParityLogOverwriteFunc(RF_DagNode_t *node)
210 1.3 oster {
211 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
212 1.24 christos caddr_t bf = (caddr_t) node->params[1].p;
213 1.3 oster RF_ParityLogData_t *logData;
214 1.19 oster #if RF_ACC_TRACE > 0
215 1.3 oster RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
216 1.3 oster RF_Etimer_t timer;
217 1.19 oster #endif
218 1.3 oster
219 1.3 oster if (node->dagHdr->status == rf_enable) {
220 1.19 oster #if RF_ACC_TRACE > 0
221 1.3 oster RF_ETIMER_START(timer);
222 1.19 oster #endif
223 1.24 christos logData = rf_CreateParityLogData(RF_OVERWRITE, pda, bf,
224 1.14 oster (RF_Raid_t *) (node->dagHdr->raidPtr),
225 1.3 oster node->wakeFunc, (void *) node, node->dagHdr->tracerec, timer);
226 1.3 oster if (logData)
227 1.3 oster rf_ParityLogAppend(logData, RF_FALSE, NULL, RF_FALSE);
228 1.3 oster else {
229 1.19 oster #if RF_ACC_TRACE > 0
230 1.3 oster RF_ETIMER_STOP(timer);
231 1.3 oster RF_ETIMER_EVAL(timer);
232 1.3 oster tracerec->plog_us += RF_ETIMER_VAL_US(timer);
233 1.19 oster #endif
234 1.3 oster (node->wakeFunc) (node, ENOMEM);
235 1.3 oster }
236 1.1 oster }
237 1.3 oster return (0);
238 1.1 oster }
239 1.1 oster
240 1.23 perry int
241 1.15 oster rf_ParityLogUpdateUndoFunc(RF_DagNode_t *node)
242 1.1 oster {
243 1.3 oster return (0);
244 1.1 oster }
245 1.1 oster
246 1.23 perry int
247 1.15 oster rf_ParityLogOverwriteUndoFunc(RF_DagNode_t *node)
248 1.1 oster {
249 1.3 oster return (0);
250 1.1 oster }
251 1.10 oster #endif /* RF_INCLUDE_PARITYLOGGING > 0 */
252 1.10 oster
253 1.14 oster /*****************************************************************************
254 1.1 oster * the execution function associated with a NOP node
255 1.14 oster ****************************************************************************/
256 1.23 perry int
257 1.15 oster rf_NullNodeFunc(RF_DagNode_t *node)
258 1.1 oster {
259 1.3 oster node->status = rf_good;
260 1.3 oster return (rf_FinishNode(node, RF_THREAD_CONTEXT));
261 1.1 oster }
262 1.1 oster
263 1.23 perry int
264 1.15 oster rf_NullNodeUndoFunc(RF_DagNode_t *node)
265 1.1 oster {
266 1.3 oster node->status = rf_undone;
267 1.3 oster return (rf_FinishNode(node, RF_THREAD_CONTEXT));
268 1.1 oster }
269 1.1 oster
270 1.1 oster
271 1.14 oster /*****************************************************************************
272 1.1 oster * the execution function associated with a disk-read node
273 1.14 oster ****************************************************************************/
274 1.23 perry int
275 1.15 oster rf_DiskReadFuncForThreads(RF_DagNode_t *node)
276 1.3 oster {
277 1.3 oster RF_DiskQueueData_t *req;
278 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
279 1.24 christos caddr_t bf = (caddr_t) node->params[1].p;
280 1.3 oster RF_StripeNum_t parityStripeID = (RF_StripeNum_t) node->params[2].v;
281 1.3 oster unsigned priority = RF_EXTRACT_PRIORITY(node->params[3].v);
282 1.3 oster unsigned which_ru = RF_EXTRACT_RU(node->params[3].v);
283 1.3 oster RF_IoType_t iotype = (node->dagHdr->status == rf_enable) ? RF_IO_TYPE_READ : RF_IO_TYPE_NOP;
284 1.13 oster RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
285 1.3 oster void *b_proc = NULL;
286 1.1 oster
287 1.3 oster if (node->dagHdr->bp)
288 1.3 oster b_proc = (void *) ((struct buf *) node->dagHdr->bp)->b_proc;
289 1.1 oster
290 1.3 oster req = rf_CreateDiskQueueData(iotype, pda->startSector, pda->numSector,
291 1.24 christos bf, parityStripeID, which_ru,
292 1.3 oster (int (*) (void *, int)) node->wakeFunc,
293 1.22 oster node,
294 1.19 oster #if RF_ACC_TRACE > 0
295 1.19 oster node->dagHdr->tracerec,
296 1.19 oster #else
297 1.19 oster NULL,
298 1.19 oster #endif
299 1.21 oster (void *) (node->dagHdr->raidPtr), 0, b_proc, PR_NOWAIT);
300 1.3 oster if (!req) {
301 1.3 oster (node->wakeFunc) (node, ENOMEM);
302 1.3 oster } else {
303 1.3 oster node->dagFuncData = (void *) req;
304 1.13 oster rf_DiskIOEnqueue(&(dqs[pda->col]), req, priority);
305 1.3 oster }
306 1.3 oster return (0);
307 1.1 oster }
308 1.1 oster
309 1.1 oster
310 1.14 oster /*****************************************************************************
311 1.1 oster * the execution function associated with a disk-write node
312 1.14 oster ****************************************************************************/
313 1.23 perry int
314 1.15 oster rf_DiskWriteFuncForThreads(RF_DagNode_t *node)
315 1.3 oster {
316 1.3 oster RF_DiskQueueData_t *req;
317 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
318 1.24 christos caddr_t bf = (caddr_t) node->params[1].p;
319 1.3 oster RF_StripeNum_t parityStripeID = (RF_StripeNum_t) node->params[2].v;
320 1.3 oster unsigned priority = RF_EXTRACT_PRIORITY(node->params[3].v);
321 1.3 oster unsigned which_ru = RF_EXTRACT_RU(node->params[3].v);
322 1.3 oster RF_IoType_t iotype = (node->dagHdr->status == rf_enable) ? RF_IO_TYPE_WRITE : RF_IO_TYPE_NOP;
323 1.13 oster RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
324 1.3 oster void *b_proc = NULL;
325 1.1 oster
326 1.3 oster if (node->dagHdr->bp)
327 1.3 oster b_proc = (void *) ((struct buf *) node->dagHdr->bp)->b_proc;
328 1.1 oster
329 1.3 oster /* normal processing (rollaway or forward recovery) begins here */
330 1.3 oster req = rf_CreateDiskQueueData(iotype, pda->startSector, pda->numSector,
331 1.24 christos bf, parityStripeID, which_ru,
332 1.3 oster (int (*) (void *, int)) node->wakeFunc,
333 1.22 oster (void *) node,
334 1.19 oster #if RF_ACC_TRACE > 0
335 1.3 oster node->dagHdr->tracerec,
336 1.19 oster #else
337 1.19 oster NULL,
338 1.19 oster #endif
339 1.3 oster (void *) (node->dagHdr->raidPtr),
340 1.21 oster 0, b_proc, PR_NOWAIT);
341 1.3 oster
342 1.3 oster if (!req) {
343 1.3 oster (node->wakeFunc) (node, ENOMEM);
344 1.3 oster } else {
345 1.3 oster node->dagFuncData = (void *) req;
346 1.13 oster rf_DiskIOEnqueue(&(dqs[pda->col]), req, priority);
347 1.3 oster }
348 1.3 oster
349 1.3 oster return (0);
350 1.1 oster }
351 1.14 oster /*****************************************************************************
352 1.1 oster * the undo function for disk nodes
353 1.1 oster * Note: this is not a proper undo of a write node, only locks are released.
354 1.1 oster * old data is not restored to disk!
355 1.14 oster ****************************************************************************/
356 1.23 perry int
357 1.15 oster rf_DiskUndoFunc(RF_DagNode_t *node)
358 1.3 oster {
359 1.3 oster RF_DiskQueueData_t *req;
360 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
361 1.13 oster RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
362 1.3 oster
363 1.3 oster req = rf_CreateDiskQueueData(RF_IO_TYPE_NOP,
364 1.3 oster 0L, 0, NULL, 0L, 0,
365 1.3 oster (int (*) (void *, int)) node->wakeFunc,
366 1.3 oster (void *) node,
367 1.19 oster #if RF_ACC_TRACE > 0
368 1.19 oster node->dagHdr->tracerec,
369 1.19 oster #else
370 1.19 oster NULL,
371 1.19 oster #endif
372 1.3 oster (void *) (node->dagHdr->raidPtr),
373 1.21 oster RF_UNLOCK_DISK_QUEUE, NULL, PR_NOWAIT);
374 1.3 oster if (!req)
375 1.3 oster (node->wakeFunc) (node, ENOMEM);
376 1.3 oster else {
377 1.3 oster node->dagFuncData = (void *) req;
378 1.13 oster rf_DiskIOEnqueue(&(dqs[pda->col]), req, RF_IO_NORMAL_PRIORITY);
379 1.3 oster }
380 1.1 oster
381 1.3 oster return (0);
382 1.1 oster }
383 1.14 oster /*****************************************************************************
384 1.1 oster * the execution function associated with an "unlock disk queue" node
385 1.14 oster ****************************************************************************/
386 1.23 perry int
387 1.15 oster rf_DiskUnlockFuncForThreads(RF_DagNode_t *node)
388 1.3 oster {
389 1.3 oster RF_DiskQueueData_t *req;
390 1.3 oster RF_PhysDiskAddr_t *pda = (RF_PhysDiskAddr_t *) node->params[0].p;
391 1.13 oster RF_DiskQueue_t *dqs = ((RF_Raid_t *) (node->dagHdr->raidPtr))->Queues;
392 1.3 oster
393 1.3 oster req = rf_CreateDiskQueueData(RF_IO_TYPE_NOP,
394 1.3 oster 0L, 0, NULL, 0L, 0,
395 1.3 oster (int (*) (void *, int)) node->wakeFunc,
396 1.3 oster (void *) node,
397 1.19 oster #if RF_ACC_TRACE > 0
398 1.19 oster node->dagHdr->tracerec,
399 1.19 oster #else
400 1.19 oster NULL,
401 1.19 oster #endif
402 1.3 oster (void *) (node->dagHdr->raidPtr),
403 1.21 oster RF_UNLOCK_DISK_QUEUE, NULL, PR_NOWAIT);
404 1.3 oster if (!req)
405 1.3 oster (node->wakeFunc) (node, ENOMEM);
406 1.3 oster else {
407 1.3 oster node->dagFuncData = (void *) req;
408 1.13 oster rf_DiskIOEnqueue(&(dqs[pda->col]), req, RF_IO_NORMAL_PRIORITY);
409 1.3 oster }
410 1.1 oster
411 1.3 oster return (0);
412 1.1 oster }
413 1.14 oster /*****************************************************************************
414 1.14 oster * Callback routine for DiskRead and DiskWrite nodes. When the disk
415 1.14 oster * op completes, the routine is called to set the node status and
416 1.14 oster * inform the execution engine that the node has fired.
417 1.14 oster ****************************************************************************/
418 1.23 perry int
419 1.15 oster rf_GenericWakeupFunc(RF_DagNode_t *node, int status)
420 1.3 oster {
421 1.15 oster
422 1.3 oster switch (node->status) {
423 1.3 oster case rf_fired:
424 1.3 oster if (status)
425 1.3 oster node->status = rf_bad;
426 1.3 oster else
427 1.3 oster node->status = rf_good;
428 1.3 oster break;
429 1.3 oster case rf_recover:
430 1.3 oster /* probably should never reach this case */
431 1.3 oster if (status)
432 1.3 oster node->status = rf_panic;
433 1.3 oster else
434 1.3 oster node->status = rf_undone;
435 1.3 oster break;
436 1.3 oster default:
437 1.4 oster printf("rf_GenericWakeupFunc:");
438 1.4 oster printf("node->status is %d,", node->status);
439 1.4 oster printf("status is %d \n", status);
440 1.3 oster RF_PANIC();
441 1.3 oster break;
442 1.3 oster }
443 1.3 oster if (node->dagFuncData)
444 1.3 oster rf_FreeDiskQueueData((RF_DiskQueueData_t *) node->dagFuncData);
445 1.3 oster return (rf_FinishNode(node, RF_INTR_CONTEXT));
446 1.1 oster }
447 1.1 oster
448 1.1 oster
449 1.14 oster /*****************************************************************************
450 1.14 oster * there are three distinct types of xor nodes:
451 1.14 oster
452 1.14 oster * A "regular xor" is used in the fault-free case where the access
453 1.14 oster * spans a complete stripe unit. It assumes that the result buffer is
454 1.14 oster * one full stripe unit in size, and uses the stripe-unit-offset
455 1.14 oster * values that it computes from the PDAs to determine where within the
456 1.14 oster * stripe unit to XOR each argument buffer.
457 1.14 oster *
458 1.14 oster * A "simple xor" is used in the fault-free case where the access
459 1.14 oster * touches only a portion of one (or two, in some cases) stripe
460 1.14 oster * unit(s). It assumes that all the argument buffers are of the same
461 1.14 oster * size and have the same stripe unit offset.
462 1.14 oster *
463 1.14 oster * A "recovery xor" is used in the degraded-mode case. It's similar
464 1.14 oster * to the regular xor function except that it takes the failed PDA as
465 1.14 oster * an additional parameter, and uses it to determine what portions of
466 1.14 oster * the argument buffers need to be xor'd into the result buffer, and
467 1.14 oster * where in the result buffer they should go.
468 1.14 oster ****************************************************************************/
469 1.1 oster
470 1.1 oster /* xor the params together and store the result in the result field.
471 1.14 oster * assume the result field points to a buffer that is the size of one
472 1.14 oster * SU, and use the pda params to determine where within the buffer to
473 1.14 oster * XOR the input buffers. */
474 1.23 perry int
475 1.15 oster rf_RegularXorFunc(RF_DagNode_t *node)
476 1.3 oster {
477 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
478 1.19 oster #if RF_ACC_TRACE > 0
479 1.3 oster RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
480 1.3 oster RF_Etimer_t timer;
481 1.19 oster #endif
482 1.3 oster int i, retcode;
483 1.1 oster
484 1.3 oster retcode = 0;
485 1.3 oster if (node->dagHdr->status == rf_enable) {
486 1.3 oster /* don't do the XOR if the input is the same as the output */
487 1.19 oster #if RF_ACC_TRACE > 0
488 1.3 oster RF_ETIMER_START(timer);
489 1.19 oster #endif
490 1.3 oster for (i = 0; i < node->numParams - 1; i += 2)
491 1.3 oster if (node->params[i + 1].p != node->results[0]) {
492 1.3 oster retcode = rf_XorIntoBuffer(raidPtr, (RF_PhysDiskAddr_t *) node->params[i].p,
493 1.17 oster (char *) node->params[i + 1].p, (char *) node->results[0]);
494 1.3 oster }
495 1.19 oster #if RF_ACC_TRACE > 0
496 1.3 oster RF_ETIMER_STOP(timer);
497 1.3 oster RF_ETIMER_EVAL(timer);
498 1.3 oster tracerec->xor_us += RF_ETIMER_VAL_US(timer);
499 1.19 oster #endif
500 1.3 oster }
501 1.3 oster return (rf_GenericWakeupFunc(node, retcode)); /* call wake func
502 1.3 oster * explicitly since no
503 1.3 oster * I/O in this node */
504 1.1 oster }
505 1.1 oster /* xor the inputs into the result buffer, ignoring placement issues */
506 1.23 perry int
507 1.15 oster rf_SimpleXorFunc(RF_DagNode_t *node)
508 1.3 oster {
509 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
510 1.3 oster int i, retcode = 0;
511 1.19 oster #if RF_ACC_TRACE > 0
512 1.3 oster RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
513 1.3 oster RF_Etimer_t timer;
514 1.19 oster #endif
515 1.1 oster
516 1.3 oster if (node->dagHdr->status == rf_enable) {
517 1.19 oster #if RF_ACC_TRACE > 0
518 1.3 oster RF_ETIMER_START(timer);
519 1.19 oster #endif
520 1.3 oster /* don't do the XOR if the input is the same as the output */
521 1.3 oster for (i = 0; i < node->numParams - 1; i += 2)
522 1.3 oster if (node->params[i + 1].p != node->results[0]) {
523 1.3 oster retcode = rf_bxor((char *) node->params[i + 1].p, (char *) node->results[0],
524 1.17 oster rf_RaidAddressToByte(raidPtr, ((RF_PhysDiskAddr_t *) node->params[i].p)->numSector));
525 1.3 oster }
526 1.19 oster #if RF_ACC_TRACE > 0
527 1.3 oster RF_ETIMER_STOP(timer);
528 1.3 oster RF_ETIMER_EVAL(timer);
529 1.3 oster tracerec->xor_us += RF_ETIMER_VAL_US(timer);
530 1.19 oster #endif
531 1.3 oster }
532 1.3 oster return (rf_GenericWakeupFunc(node, retcode)); /* call wake func
533 1.3 oster * explicitly since no
534 1.3 oster * I/O in this node */
535 1.1 oster }
536 1.14 oster /* this xor is used by the degraded-mode dag functions to recover lost
537 1.14 oster * data. the second-to-last parameter is the PDA for the failed
538 1.14 oster * portion of the access. the code here looks at this PDA and assumes
539 1.14 oster * that the xor target buffer is equal in size to the number of
540 1.14 oster * sectors in the failed PDA. It then uses the other PDAs in the
541 1.14 oster * parameter list to determine where within the target buffer the
542 1.14 oster * corresponding data should be xored. */
543 1.23 perry int
544 1.15 oster rf_RecoveryXorFunc(RF_DagNode_t *node)
545 1.3 oster {
546 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) node->params[node->numParams - 1].p;
547 1.3 oster RF_RaidLayout_t *layoutPtr = (RF_RaidLayout_t *) & raidPtr->Layout;
548 1.3 oster RF_PhysDiskAddr_t *failedPDA = (RF_PhysDiskAddr_t *) node->params[node->numParams - 2].p;
549 1.3 oster int i, retcode = 0;
550 1.3 oster RF_PhysDiskAddr_t *pda;
551 1.3 oster int suoffset, failedSUOffset = rf_StripeUnitOffset(layoutPtr, failedPDA->startSector);
552 1.3 oster char *srcbuf, *destbuf;
553 1.19 oster #if RF_ACC_TRACE > 0
554 1.3 oster RF_AccTraceEntry_t *tracerec = node->dagHdr->tracerec;
555 1.3 oster RF_Etimer_t timer;
556 1.19 oster #endif
557 1.1 oster
558 1.3 oster if (node->dagHdr->status == rf_enable) {
559 1.19 oster #if RF_ACC_TRACE > 0
560 1.3 oster RF_ETIMER_START(timer);
561 1.19 oster #endif
562 1.3 oster for (i = 0; i < node->numParams - 2; i += 2)
563 1.3 oster if (node->params[i + 1].p != node->results[0]) {
564 1.3 oster pda = (RF_PhysDiskAddr_t *) node->params[i].p;
565 1.3 oster srcbuf = (char *) node->params[i + 1].p;
566 1.3 oster suoffset = rf_StripeUnitOffset(layoutPtr, pda->startSector);
567 1.3 oster destbuf = ((char *) node->results[0]) + rf_RaidAddressToByte(raidPtr, suoffset - failedSUOffset);
568 1.17 oster retcode = rf_bxor(srcbuf, destbuf, rf_RaidAddressToByte(raidPtr, pda->numSector));
569 1.3 oster }
570 1.19 oster #if RF_ACC_TRACE > 0
571 1.3 oster RF_ETIMER_STOP(timer);
572 1.3 oster RF_ETIMER_EVAL(timer);
573 1.3 oster tracerec->xor_us += RF_ETIMER_VAL_US(timer);
574 1.19 oster #endif
575 1.3 oster }
576 1.3 oster return (rf_GenericWakeupFunc(node, retcode));
577 1.1 oster }
578 1.14 oster /*****************************************************************************
579 1.14 oster * The next three functions are utilities used by the above
580 1.14 oster * xor-execution functions.
581 1.14 oster ****************************************************************************/
582 1.1 oster
583 1.1 oster
584 1.1 oster /*
585 1.14 oster * this is just a glorified buffer xor. targbuf points to a buffer
586 1.14 oster * that is one full stripe unit in size. srcbuf points to a buffer
587 1.14 oster * that may be less than 1 SU, but never more. When the access
588 1.14 oster * described by pda is one SU in size (which by implication means it's
589 1.14 oster * SU-aligned), all that happens is (targbuf) <- (srcbuf ^ targbuf).
590 1.14 oster * When the access is less than one SU in size the XOR occurs on only
591 1.14 oster * the portion of targbuf identified in the pda. */
592 1.1 oster
593 1.23 perry int
594 1.15 oster rf_XorIntoBuffer(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda,
595 1.17 oster char *srcbuf, char *targbuf)
596 1.3 oster {
597 1.3 oster char *targptr;
598 1.3 oster int sectPerSU = raidPtr->Layout.sectorsPerStripeUnit;
599 1.3 oster int SUOffset = pda->startSector % sectPerSU;
600 1.3 oster int length, retcode = 0;
601 1.3 oster
602 1.3 oster RF_ASSERT(pda->numSector <= sectPerSU);
603 1.3 oster
604 1.3 oster targptr = targbuf + rf_RaidAddressToByte(raidPtr, SUOffset);
605 1.3 oster length = rf_RaidAddressToByte(raidPtr, pda->numSector);
606 1.17 oster retcode = rf_bxor(srcbuf, targptr, length);
607 1.3 oster return (retcode);
608 1.1 oster }
609 1.14 oster /* it really should be the case that the buffer pointers (returned by
610 1.14 oster * malloc) are aligned to the natural word size of the machine, so
611 1.14 oster * this is the only case we optimize for. The length should always be
612 1.14 oster * a multiple of the sector size, so there should be no problem with
613 1.14 oster * leftover bytes at the end. */
614 1.23 perry int
615 1.17 oster rf_bxor(char *src, char *dest, int len)
616 1.3 oster {
617 1.3 oster unsigned mask = sizeof(long) - 1, retcode = 0;
618 1.3 oster
619 1.23 perry if (!(((unsigned long) src) & mask) &&
620 1.14 oster !(((unsigned long) dest) & mask) && !(len & mask)) {
621 1.23 perry retcode = rf_longword_bxor((unsigned long *) src,
622 1.23 perry (unsigned long *) dest,
623 1.17 oster len >> RF_LONGSHIFT);
624 1.3 oster } else {
625 1.3 oster RF_ASSERT(0);
626 1.3 oster }
627 1.3 oster return (retcode);
628 1.1 oster }
629 1.1 oster
630 1.14 oster /* When XORing in kernel mode, we need to map each user page to kernel
631 1.14 oster * space before we can access it. We don't want to assume anything
632 1.14 oster * about which input buffers are in kernel/user space, nor about their
633 1.14 oster * alignment, so in each loop we compute the maximum number of bytes
634 1.14 oster * that we can xor without crossing any page boundaries, and do only
635 1.23 perry * this many bytes before the next remap.
636 1.23 perry *
637 1.23 perry * len - is in longwords
638 1.15 oster */
639 1.23 perry int
640 1.17 oster rf_longword_bxor(unsigned long *src, unsigned long *dest, int len)
641 1.3 oster {
642 1.6 augustss unsigned long *end = src + len;
643 1.6 augustss unsigned long d0, d1, d2, d3, s0, s1, s2, s3; /* temps */
644 1.14 oster unsigned long *pg_src, *pg_dest; /* per-page source/dest pointers */
645 1.3 oster int longs_this_time;/* # longwords to xor in the current iteration */
646 1.3 oster
647 1.16 oster pg_src = src;
648 1.16 oster pg_dest = dest;
649 1.3 oster if (!pg_src || !pg_dest)
650 1.3 oster return (EFAULT);
651 1.3 oster
652 1.3 oster while (len >= 4) {
653 1.3 oster longs_this_time = RF_MIN(len, RF_MIN(RF_BLIP(pg_src), RF_BLIP(pg_dest)) >> RF_LONGSHIFT); /* note len in longwords */
654 1.3 oster src += longs_this_time;
655 1.3 oster dest += longs_this_time;
656 1.3 oster len -= longs_this_time;
657 1.3 oster while (longs_this_time >= 4) {
658 1.3 oster d0 = pg_dest[0];
659 1.3 oster d1 = pg_dest[1];
660 1.3 oster d2 = pg_dest[2];
661 1.3 oster d3 = pg_dest[3];
662 1.3 oster s0 = pg_src[0];
663 1.3 oster s1 = pg_src[1];
664 1.3 oster s2 = pg_src[2];
665 1.3 oster s3 = pg_src[3];
666 1.3 oster pg_dest[0] = d0 ^ s0;
667 1.3 oster pg_dest[1] = d1 ^ s1;
668 1.3 oster pg_dest[2] = d2 ^ s2;
669 1.3 oster pg_dest[3] = d3 ^ s3;
670 1.3 oster pg_src += 4;
671 1.3 oster pg_dest += 4;
672 1.3 oster longs_this_time -= 4;
673 1.3 oster }
674 1.3 oster while (longs_this_time > 0) { /* cannot cross any page
675 1.3 oster * boundaries here */
676 1.3 oster *pg_dest++ ^= *pg_src++;
677 1.3 oster longs_this_time--;
678 1.3 oster }
679 1.3 oster
680 1.3 oster /* either we're done, or we've reached a page boundary on one
681 1.3 oster * (or possibly both) of the pointers */
682 1.3 oster if (len) {
683 1.3 oster if (RF_PAGE_ALIGNED(src))
684 1.16 oster pg_src = src;
685 1.3 oster if (RF_PAGE_ALIGNED(dest))
686 1.16 oster pg_dest = dest;
687 1.3 oster if (!pg_src || !pg_dest)
688 1.3 oster return (EFAULT);
689 1.3 oster }
690 1.3 oster }
691 1.3 oster while (src < end) {
692 1.3 oster *pg_dest++ ^= *pg_src++;
693 1.3 oster src++;
694 1.3 oster dest++;
695 1.3 oster len--;
696 1.3 oster if (RF_PAGE_ALIGNED(src))
697 1.16 oster pg_src = src;
698 1.3 oster if (RF_PAGE_ALIGNED(dest))
699 1.16 oster pg_dest = dest;
700 1.3 oster }
701 1.3 oster RF_ASSERT(len == 0);
702 1.3 oster return (0);
703 1.1 oster }
704 1.1 oster
705 1.9 oster #if 0
706 1.1 oster /*
707 1.1 oster dst = a ^ b ^ c;
708 1.1 oster a may equal dst
709 1.1 oster see comment above longword_bxor
710 1.15 oster len is length in longwords
711 1.1 oster */
712 1.23 perry int
713 1.15 oster rf_longword_bxor3(unsigned long *dst, unsigned long *a, unsigned long *b,
714 1.15 oster unsigned long *c, int len, void *bp)
715 1.3 oster {
716 1.3 oster unsigned long a0, a1, a2, a3, b0, b1, b2, b3;
717 1.6 augustss unsigned long *pg_a, *pg_b, *pg_c, *pg_dst; /* per-page source/dest
718 1.3 oster * pointers */
719 1.3 oster int longs_this_time;/* # longs to xor in the current iteration */
720 1.3 oster char dst_is_a = 0;
721 1.3 oster
722 1.16 oster pg_a = a;
723 1.16 oster pg_b = b;
724 1.16 oster pg_c = c;
725 1.3 oster if (a == dst) {
726 1.3 oster pg_dst = pg_a;
727 1.3 oster dst_is_a = 1;
728 1.3 oster } else {
729 1.16 oster pg_dst = dst;
730 1.3 oster }
731 1.3 oster
732 1.3 oster /* align dest to cache line. Can't cross a pg boundary on dst here. */
733 1.3 oster while ((((unsigned long) pg_dst) & 0x1f)) {
734 1.3 oster *pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
735 1.3 oster dst++;
736 1.3 oster a++;
737 1.3 oster b++;
738 1.3 oster c++;
739 1.3 oster if (RF_PAGE_ALIGNED(a)) {
740 1.16 oster pg_a = a;
741 1.3 oster if (!pg_a)
742 1.3 oster return (EFAULT);
743 1.3 oster }
744 1.3 oster if (RF_PAGE_ALIGNED(b)) {
745 1.16 oster pg_b = a;
746 1.3 oster if (!pg_b)
747 1.3 oster return (EFAULT);
748 1.3 oster }
749 1.3 oster if (RF_PAGE_ALIGNED(c)) {
750 1.16 oster pg_c = a;
751 1.3 oster if (!pg_c)
752 1.3 oster return (EFAULT);
753 1.3 oster }
754 1.3 oster len--;
755 1.3 oster }
756 1.3 oster
757 1.3 oster while (len > 4) {
758 1.3 oster longs_this_time = RF_MIN(len, RF_MIN(RF_BLIP(a), RF_MIN(RF_BLIP(b), RF_MIN(RF_BLIP(c), RF_BLIP(dst)))) >> RF_LONGSHIFT);
759 1.3 oster a += longs_this_time;
760 1.3 oster b += longs_this_time;
761 1.3 oster c += longs_this_time;
762 1.3 oster dst += longs_this_time;
763 1.3 oster len -= longs_this_time;
764 1.3 oster while (longs_this_time >= 4) {
765 1.3 oster a0 = pg_a[0];
766 1.3 oster longs_this_time -= 4;
767 1.3 oster
768 1.3 oster a1 = pg_a[1];
769 1.3 oster a2 = pg_a[2];
770 1.3 oster
771 1.3 oster a3 = pg_a[3];
772 1.3 oster pg_a += 4;
773 1.3 oster
774 1.3 oster b0 = pg_b[0];
775 1.3 oster b1 = pg_b[1];
776 1.3 oster
777 1.3 oster b2 = pg_b[2];
778 1.3 oster b3 = pg_b[3];
779 1.3 oster /* start dual issue */
780 1.3 oster a0 ^= b0;
781 1.3 oster b0 = pg_c[0];
782 1.3 oster
783 1.3 oster pg_b += 4;
784 1.3 oster a1 ^= b1;
785 1.3 oster
786 1.3 oster a2 ^= b2;
787 1.3 oster a3 ^= b3;
788 1.3 oster
789 1.3 oster b1 = pg_c[1];
790 1.3 oster a0 ^= b0;
791 1.3 oster
792 1.3 oster b2 = pg_c[2];
793 1.3 oster a1 ^= b1;
794 1.3 oster
795 1.3 oster b3 = pg_c[3];
796 1.3 oster a2 ^= b2;
797 1.3 oster
798 1.3 oster pg_dst[0] = a0;
799 1.3 oster a3 ^= b3;
800 1.3 oster pg_dst[1] = a1;
801 1.3 oster pg_c += 4;
802 1.3 oster pg_dst[2] = a2;
803 1.3 oster pg_dst[3] = a3;
804 1.3 oster pg_dst += 4;
805 1.3 oster }
806 1.3 oster while (longs_this_time > 0) { /* cannot cross any page
807 1.3 oster * boundaries here */
808 1.3 oster *pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
809 1.3 oster longs_this_time--;
810 1.3 oster }
811 1.3 oster
812 1.3 oster if (len) {
813 1.3 oster if (RF_PAGE_ALIGNED(a)) {
814 1.16 oster pg_a = a;
815 1.3 oster if (!pg_a)
816 1.3 oster return (EFAULT);
817 1.3 oster if (dst_is_a)
818 1.3 oster pg_dst = pg_a;
819 1.3 oster }
820 1.3 oster if (RF_PAGE_ALIGNED(b)) {
821 1.16 oster pg_b = b;
822 1.3 oster if (!pg_b)
823 1.3 oster return (EFAULT);
824 1.3 oster }
825 1.3 oster if (RF_PAGE_ALIGNED(c)) {
826 1.16 oster pg_c = c;
827 1.3 oster if (!pg_c)
828 1.3 oster return (EFAULT);
829 1.3 oster }
830 1.3 oster if (!dst_is_a)
831 1.3 oster if (RF_PAGE_ALIGNED(dst)) {
832 1.16 oster pg_dst = dst;
833 1.3 oster if (!pg_dst)
834 1.3 oster return (EFAULT);
835 1.3 oster }
836 1.3 oster }
837 1.3 oster }
838 1.3 oster while (len) {
839 1.3 oster *pg_dst++ = *pg_a++ ^ *pg_b++ ^ *pg_c++;
840 1.3 oster dst++;
841 1.3 oster a++;
842 1.3 oster b++;
843 1.3 oster c++;
844 1.3 oster if (RF_PAGE_ALIGNED(a)) {
845 1.16 oster pg_a = a;
846 1.3 oster if (!pg_a)
847 1.3 oster return (EFAULT);
848 1.3 oster if (dst_is_a)
849 1.3 oster pg_dst = pg_a;
850 1.3 oster }
851 1.3 oster if (RF_PAGE_ALIGNED(b)) {
852 1.16 oster pg_b = b;
853 1.3 oster if (!pg_b)
854 1.3 oster return (EFAULT);
855 1.3 oster }
856 1.3 oster if (RF_PAGE_ALIGNED(c)) {
857 1.16 oster pg_c = c;
858 1.3 oster if (!pg_c)
859 1.3 oster return (EFAULT);
860 1.3 oster }
861 1.3 oster if (!dst_is_a)
862 1.3 oster if (RF_PAGE_ALIGNED(dst)) {
863 1.16 oster pg_dst = dst;
864 1.3 oster if (!pg_dst)
865 1.3 oster return (EFAULT);
866 1.3 oster }
867 1.3 oster len--;
868 1.3 oster }
869 1.3 oster return (0);
870 1.3 oster }
871 1.3 oster
872 1.23 perry int
873 1.23 perry rf_bxor3(unsigned char *dst, unsigned char *a, unsigned char *b,
874 1.15 oster unsigned char *c, unsigned long len, void *bp)
875 1.1 oster {
876 1.3 oster RF_ASSERT(((RF_UL(dst) | RF_UL(a) | RF_UL(b) | RF_UL(c) | len) & 0x7) == 0);
877 1.1 oster
878 1.3 oster return (rf_longword_bxor3((unsigned long *) dst, (unsigned long *) a,
879 1.3 oster (unsigned long *) b, (unsigned long *) c, len >> RF_LONGSHIFT, bp));
880 1.1 oster }
881 1.9 oster #endif
882