1/*	$NetBSD: rf_paritylog.h,v 1.13 2019/10/10 03:43:59 christos Exp $	*/
2/*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: William V. Courtright II
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21 *  School of Computer Science
22 *  Carnegie Mellon University
23 *  Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29/* header file for parity log
30 *
31 */
32
33#ifndef _RF__RF_PARITYLOG_H_
34#define _RF__RF_PARITYLOG_H_
35
36#include <dev/raidframe/raidframevar.h>
37
38#define RF_DEFAULT_NUM_SECTORS_PER_LOG 64
39
40typedef int RF_RegionId_t;
41
42typedef enum RF_ParityRecordType_e {
43	RF_STOP,
44	RF_UPDATE,
45	RF_OVERWRITE
46}       RF_ParityRecordType_t;
47
48struct RF_CommonLogData_s {
49	rf_declare_mutex2(mutex);	/* protects cnt */
50	int     cnt;			/* when 0, time to call wakeFunc */
51	RF_Raid_t *raidPtr;
52	void  (*wakeFunc) (void *, int);
53	void   *wakeArg;
54	RF_AccTraceEntry_t *tracerec;
55	RF_Etimer_t startTime;
56	void *bufPtr;
57	RF_ParityRecordType_t operation;
58	RF_CommonLogData_t *next;
59};
60
61struct RF_ParityLogData_s {
62	RF_RegionId_t regionID;	/* this struct guaranteed to span a single
63				 * region */
64	int     bufOffset;	/* offset from common->bufPtr */
65	RF_PhysDiskAddr_t diskAddress;
66	RF_CommonLogData_t *common;	/* info shared by one or more
67					 * parityLogData structs */
68	RF_ParityLogData_t *next;
69	RF_ParityLogData_t *prev;
70};
71
72struct RF_ParityLogAppendQueue_s {
73	rf_declare_mutex2(mutex);
74};
75
76struct RF_ParityLogRecord_s {
77	RF_PhysDiskAddr_t parityAddr;
78	RF_ParityRecordType_t operation;
79};
80
81struct RF_ParityLog_s {
82	RF_RegionId_t regionID;
83	int     numRecords;
84	int     diskOffset;
85	RF_ParityLogRecord_t *records;
86	void *bufPtr;
87	RF_ParityLog_t *next;
88};
89
90struct RF_ParityLogQueue_s {
91	rf_declare_mutex2(mutex);
92	RF_ParityLog_t *parityLogs;
93};
94
95struct RF_RegionBufferQueue_s {
96	rf_declare_mutex2(mutex);
97	rf_declare_cond2(cond);
98	int     bufferSize;
99	int     totalBuffers;	/* size of array 'buffers' */
100	int     availableBuffers;	/* num available 'buffers' */
101	int     emptyBuffersIndex;	/* stick next freed buffer here */
102	int     availBuffersIndex;	/* grab next buffer from here */
103	void **buffers;	/* array buffers used to hold parity */
104};
105#define RF_PLOG_CREATED   (1<<0)/* thread is created */
106#define RF_PLOG_RUNNING   (1<<1)/* thread is running */
107#define RF_PLOG_TERMINATE (1<<2)/* thread is terminated (should exit) */
108#define RF_PLOG_SHUTDOWN  (1<<3)/* thread is aware and exiting/exited */
109
110struct RF_ParityLogDiskQueue_s {
111	rf_declare_mutex2(mutex);/* protects all vars in this struct */
112	rf_declare_cond2(cond);
113	int     threadState;	/* is thread running, should it shutdown  (see
114				 * above) */
115	RF_ParityLog_t *flushQueue;	/* list of parity logs to be flushed
116					 * to log disk */
117	RF_ParityLog_t *reintQueue;	/* list of parity logs waiting to be
118					 * reintegrated */
119	RF_ParityLogData_t *bufHead;	/* head of FIFO list of log data,
120					 * waiting on a buffer */
121	RF_ParityLogData_t *bufTail;	/* tail of FIFO list of log data,
122					 * waiting on a buffer */
123	RF_ParityLogData_t *reintHead;	/* head of FIFO list of log data,
124					 * waiting on reintegration */
125	RF_ParityLogData_t *reintTail;	/* tail of FIFO list of log data,
126					 * waiting on reintegration */
127	RF_ParityLogData_t *logBlockHead;	/* queue of work, blocked
128						 * until a log is available */
129	RF_ParityLogData_t *logBlockTail;
130	RF_ParityLogData_t *reintBlockHead;	/* queue of work, blocked
131						 * until reintegration is
132						 * complete */
133	RF_ParityLogData_t *reintBlockTail;
134	RF_CommonLogData_t *freeCommonList;	/* list of unused common data
135						 * structs */
136	RF_ParityLogData_t *freeDataList;	/* list of unused log data
137						 * structs */
138};
139
140struct RF_DiskMap_s {
141	RF_PhysDiskAddr_t parityAddr;
142	RF_ParityRecordType_t operation;
143};
144
145struct RF_RegionInfo_s {
146	rf_declare_mutex2(mutex);	/* protects: diskCount, diskMap,
147					 * loggingEnabled, coreLog */
148	rf_declare_mutex2(reintMutex);	/* protects: reintInProgress */
149	int     reintInProgress;/* flag used to suspend flushing operations */
150	RF_SectorCount_t capacity;	/* capacity of this region in sectors */
151	RF_SectorNum_t regionStartAddr;	/* starting disk address for this
152					 * region */
153	RF_SectorNum_t parityStartAddr;	/* starting disk address for this
154					 * region */
155	RF_SectorCount_t numSectorsParity;	/* number of parity sectors
156						 * protected by this region */
157	RF_SectorCount_t diskCount;	/* num of sectors written to this
158					 * region's disk log */
159	RF_DiskMap_t *diskMap;	/* in-core map of what's in this region's disk
160				 * log */
161	int     loggingEnabled;	/* logging enable for this region */
162	RF_ParityLog_t *coreLog;/* in-core log for this region */
163};
164
165RF_ParityLogData_t *
166rf_CreateParityLogData(RF_ParityRecordType_t operation,
167    RF_PhysDiskAddr_t * pda, void *bufPtr, RF_Raid_t * raidPtr,
168    void (*wakeFunc) (void *, int),
169    void *wakeArg, RF_AccTraceEntry_t * tracerec,
170    RF_Etimer_t startTime);
171	RF_ParityLogData_t *rf_SearchAndDequeueParityLogData(RF_Raid_t * raidPtr,
172            RF_RegionId_t regionID, RF_ParityLogData_t ** head,
173            RF_ParityLogData_t ** tail, int ignoreLocks);
174	void    rf_ReleaseParityLogs(RF_Raid_t * raidPtr, RF_ParityLog_t * firstLog);
175	int     rf_ParityLogAppend(RF_ParityLogData_t * logData, int finish,
176            RF_ParityLog_t ** incomingLog, int clearReintFlag);
177	void    rf_EnableParityLogging(RF_Raid_t * raidPtr);
178
179#endif				/* !_RF__RF_PARITYLOG_H_ */
180