forked from aosm/AppleRAID
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppleRAIDConcatSet.cpp
More file actions
275 lines (200 loc) · 8.17 KB
/
AppleRAIDConcatSet.cpp
File metadata and controls
275 lines (200 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (c) 2001-2007 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "AppleRAID.h"
#define super AppleRAIDSet
OSDefineMetaClassAndStructors(AppleRAIDConcatSet, AppleRAIDSet);
AppleRAIDSet * AppleRAIDConcatSet::createRAIDSet(AppleRAIDMember * firstMember)
{
AppleRAIDConcatSet *raidSet = new AppleRAIDConcatSet;
IOLog1("AppleRAIDConcatSet::createRAIDSet(%p) called, new set = %p *********\n", firstMember, raidSet);
while (raidSet){
if (!raidSet->init()) break;
if (!raidSet->initWithHeader(firstMember->getHeader(), true)) break;
if (raidSet->resizeSet(raidSet->getMemberCount())) return raidSet;
break;
}
if (raidSet) raidSet->release();
return 0;
}
bool AppleRAIDConcatSet::init()
{
IOLog1("AppleRAIDConcatSet::init() called\n");
if (super::init() == false) return false;
arMemberBlockCounts = 0;
arExpectingLiveAdd = 0;
setProperty(kAppleRAIDLevelNameKey, kAppleRAIDLevelNameConcat);
arAllocateRequestMethod = OSMemberFunctionCast(IOCommandGate::Action, this, &AppleRAIDSet::allocateRAIDRequest);
return true;
}
void AppleRAIDConcatSet::free(void)
{
if (arMemberBlockCounts) IODelete(arMemberBlockCounts, UInt64, arMemberCount);
super::free();
}
//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
bool AppleRAIDConcatSet::addSpare(AppleRAIDMember * member)
{
if (super::addSpare(member) == false) return false;
member->changeMemberState(kAppleRAIDMemberStateBroken);
return true;
}
bool AppleRAIDConcatSet::addMember(AppleRAIDMember * member)
{
if (super::addMember(member) == false) return false;
OSNumber * number = OSDynamicCast(OSNumber, member->getHeaderProperty(kAppleRAIDChunkCountKey));
if (!number) return false;
UInt64 memberBlockCount = number->unsigned64BitValue();
UInt32 memberIndex = member->getMemberIndex();
arMemberBlockCounts[memberIndex] = memberBlockCount;
// total up the block count as we go
arSetBlockCount += memberBlockCount;
arSetMediaSize = arSetBlockCount * arSetBlockSize;
return true;
}
bool AppleRAIDConcatSet::removeMember(AppleRAIDMember * member, IOOptionBits options)
{
UInt32 memberIndex = member->getMemberIndex();
UInt64 memberBlockCount = arMemberBlockCounts[memberIndex];
IOLog1("AppleRAIDConcatSet::removeMember(%p) called for index %d block count %lld\n",
member, (int)memberIndex, memberBlockCount);
// remove this member's blocks from the total block count
arSetBlockCount -= memberBlockCount;
arSetMediaSize = arSetBlockCount * arSetBlockSize;
arMemberBlockCounts[memberIndex] = 0;
return super::removeMember(member, options);
}
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
bool AppleRAIDConcatSet::resizeSet(UInt32 newMemberCount)
{
UInt32 oldMemberCount = arMemberCount;
UInt64 *oldBlockCounts = arMemberBlockCounts;
arMemberBlockCounts = IONew(UInt64, newMemberCount);
bzero(arMemberBlockCounts, sizeof(UInt64) * newMemberCount);
if (oldBlockCounts) {
bcopy(oldBlockCounts, arMemberBlockCounts, sizeof(UInt64) * MIN(newMemberCount, oldMemberCount));
IODelete(oldBlockCounts, sizeof(UInt64), oldMemberCount);
}
if (super::resizeSet(newMemberCount) == false) return false;
if (oldMemberCount && arMemberCount > oldMemberCount) arExpectingLiveAdd += arMemberCount - oldMemberCount;
return true;
}
bool AppleRAIDConcatSet::startSet(void)
{
if (super::startSet() == false) return false;
// the set remains paused when a new member is added
if (arExpectingLiveAdd) {
arExpectingLiveAdd--;
if (arExpectingLiveAdd == 0) unpauseSet();
}
return true;
}
bool AppleRAIDConcatSet::publishSet(void)
{
if (arExpectingLiveAdd) {
IOLog1("AppleRAIDConcat::publishSet() publish ignored.\n");
return false;
}
return super::publishSet();
}
void AppleRAIDConcatSet::unpauseSet()
{
if (arExpectingLiveAdd) {
IOLog1("AppleRAIDConcat::unpauseSet() unpause ignored.\n");
return;
}
super::unpauseSet();
}
UInt64 AppleRAIDConcatSet::getMemberSize(UInt32 memberIndex) const
{
assert(arMemberBlockCounts);
assert(memberIndex < arMemberCount);
return arMemberBlockCounts[memberIndex] * arSetBlockSize;;
}
AppleRAIDMemoryDescriptor * AppleRAIDConcatSet::allocateMemoryDescriptor(AppleRAIDStorageRequest *storageRequest, UInt32 memberIndex)
{
return AppleRAIDConcatMemoryDescriptor::withStorageRequest(storageRequest, memberIndex);
}
// AppleRAIDConcatMemoryDescriptor
// AppleRAIDConcatMemoryDescriptor
// AppleRAIDConcatMemoryDescriptor
#undef super
#define super AppleRAIDMemoryDescriptor
OSDefineMetaClassAndStructors(AppleRAIDConcatMemoryDescriptor, AppleRAIDMemoryDescriptor);
AppleRAIDMemoryDescriptor *
AppleRAIDConcatMemoryDescriptor::withStorageRequest(AppleRAIDStorageRequest *storageRequest, UInt32 memberIndex)
{
AppleRAIDMemoryDescriptor *memoryDescriptor = new AppleRAIDConcatMemoryDescriptor;
if (memoryDescriptor != 0) {
if (!memoryDescriptor->initWithStorageRequest(storageRequest, memberIndex)) {
memoryDescriptor->release();
memoryDescriptor = 0;
}
}
return memoryDescriptor;
}
bool AppleRAIDConcatMemoryDescriptor::initWithStorageRequest(AppleRAIDStorageRequest *storageRequest, UInt32 memberIndex)
{
if (!super::initWithStorageRequest(storageRequest, memberIndex)) return false;
AppleRAIDConcatSet * set = (AppleRAIDConcatSet *)mdStorageRequest->srRAIDSet;
mdMemberStart = 0;
mdMemberEnd = set->getMemberSize(0) - 1;
for (UInt32 index = 1; index <= memberIndex; index++) {
mdMemberStart += set->getMemberSize(index - 1);
mdMemberEnd += set->getMemberSize(index);
}
return true;
}
bool AppleRAIDConcatMemoryDescriptor::configureForMemoryDescriptor(IOMemoryDescriptor *memoryDescriptor, UInt64 byteStart, UInt32 activeIndex)
{
UInt32 byteCount = memoryDescriptor->getLength();
UInt64 byteEnd = byteStart + byteCount - 1;
IOLogRW("concat start=%llu end=%llu bytestart=%llu byteCount=%u\n", mdMemberStart, mdMemberEnd, byteStart, (uint32_t)byteCount);
assert(mdMemberIndex == activeIndex);
if ((byteEnd < mdMemberStart) || (byteStart > mdMemberEnd)) return false;
if (byteStart < mdMemberStart) {
mdMemberByteStart = 0;
mdMemberOffset = mdMemberStart - byteStart;
byteCount -= mdMemberOffset;
} else {
mdMemberByteStart = byteStart - mdMemberStart;
mdMemberOffset = 0;
}
if (byteEnd > mdMemberEnd) {
byteCount -= byteEnd - mdMemberEnd;
}
_length = byteCount;
mdMemoryDescriptor = memoryDescriptor;
_flags = (_flags & ~kIOMemoryDirectionMask) | memoryDescriptor->getDirection();
IOLogRW("concat mdbytestart=%llu _length=0x%x\n", byteStart, (uint32_t)_length);
return true;
}
addr64_t AppleRAIDConcatMemoryDescriptor::getPhysicalSegment(IOByteCount offset, IOByteCount *length, IOOptionBits options)
{
IOByteCount setOffset = offset + mdMemberOffset;
addr64_t physAddress;
physAddress = mdMemoryDescriptor->getPhysicalSegment(setOffset, length, options);
return physAddress;
}