-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmart_array_raid_0_reader.cpp
More file actions
157 lines (129 loc) · 4.57 KB
/
smart_array_raid_0_reader.cpp
File metadata and controls
157 lines (129 loc) · 4.57 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
#include "smart_array_raid_0_reader.hpp"
#include <math.h>
#include <memory.h>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <algorithm>
#include <memory>
namespace sg
{
SmartArrayRaid0Reader::SmartArrayRaid0Reader(const SmartArrayRaid0ReaderOptions &options)
{
this->driveName = options.readerName;
this->stripeSizeInBytes = options.stripeSize * 1024;
std::vector<u64> drivesSizes;
for (auto& drive : options.driveReaders)
{
if (!drive)
{
throw std::invalid_argument("For RAID 0 no missings drive are allowed.");
}
drivesSizes.push_back(drive->driveSize());
this->drives.push_back(drive);
}
this->singleDriveSize = *std::min_element(drivesSizes.begin(), drivesSizes.end());
if (!options.nometadata)
{
// 32MiB from the end of drive are stored controller metadata.
this->singleDriveSize -= 1024 * 1024 * 32;
this->setPhysicalDriveOffset(options.offset);
}
// I am skipping last stripe on drive if it's not whole
// I don't know how Smart Array hanle that but you have drive size in metadata
// So use `packard-tell` and copy command from there ^^
u64 wholeStripesOnDrive = (this->singleDriveSize - this->getPhysicalDriveOffset()) / this->stripeSizeInBytes;
u64 defaultSize = wholeStripesOnDrive * this->stripeSizeInBytes * drives.size();
u64 maximumSize = (this->singleDriveSize - this->getPhysicalDriveOffset()) * drives.size();
this->setSize(defaultSize, 0);
if (options.size > 0)
{
this->setSize(options.size, maximumSize);
}
}
int SmartArrayRaid0Reader::read(void* buf, u32 len, u64 offset)
{
if (offset >= this->driveSize())
{
std::cerr << "Tried to read from offset exceeding array size. Skipping." << std::endl;
return -1;
}
u64 stripenum = this->stripeNumber(offset);
u32 stripeRelativeOffset = this->stripeRelativeOffset(stripenum, offset);
while (len != 0)
{
u32 read = this->readFromStripe(buf, stripenum, stripeRelativeOffset, len);
len -= read;
buf = static_cast<char*>(buf) + read;
if (len > 0)
{
stripenum++;
stripeRelativeOffset = 0;
}
}
return 0;
}
u64 SmartArrayRaid0Reader::stripeNumber(u64 offset)
{
u64 stripenum = offset / this->stripeSizeInBytes;
if (this->isLastRow(stripenum / drives.size()))
{
u64 o = offset - (stripenum * this->stripeSizeInBytes);
u64 lastStripeNum = o / this->lastRowStripeSize();
stripenum += lastStripeNum;
}
return stripenum;
}
u32 SmartArrayRaid0Reader::stripeRelativeOffset(u64 stripenum, u64 offset)
{
u32 o = offset - stripenum * this->stripeSizeInBytes;
if (this->isLastRow(stripenum / drives.size()))
{
o %= this->lastRowStripeSize();
}
return o;
}
u16 SmartArrayRaid0Reader::stripeDriveNumber(u64 stripenum)
{
return stripenum % drives.size();
}
u64 SmartArrayRaid0Reader::stripeDriveOffset(u64 stripenum, u32 stripeRelativeOffset)
{
u64 currentStripeRow = stripenum / drives.size();
return (currentStripeRow * this->stripeSizeInBytes) + stripeRelativeOffset + this->getPhysicalDriveOffset();
}
u32 SmartArrayRaid0Reader::lastRowStripeSize()
{
u32 fullStripeSize = this->stripeSizeInBytes * this->drives.size();
u64 wholeStripesOnDrive = this->driveSize() / fullStripeSize;
u32 lastFullStripeSize = this->driveSize() - wholeStripesOnDrive * fullStripeSize;
return lastFullStripeSize / this->drives.size();
}
bool SmartArrayRaid0Reader::isLastRow(u64 rownum)
{
u32 fullStripeSize = this->stripeSizeInBytes * this->drives.size();
u64 wholeStripesOnDrive = this->driveSize() / fullStripeSize;
return rownum == wholeStripesOnDrive;
}
u32 SmartArrayRaid0Reader::readFromStripe(void *buf, u64 stripenum, u32 stripeRelativeOffset, u32 len)
{
auto drivenum = stripeDriveNumber(stripenum);
auto driveOffset = stripeDriveOffset(stripenum, stripeRelativeOffset);
auto stripeSize = this->stripeSizeInBytes;
if (this->isLastRow(stripenum / drives.size()))
{
stripeSize = this->lastRowStripeSize();
}
if ((len + stripeRelativeOffset) > stripeSize)
{
// We will to the end of the stripe if
// stripe area is exceed. We will return len
// from this method so called will know that
// some data must be read from another stripe
len = stripeSize - stripeRelativeOffset;
}
auto& drivePtr = this->drives[drivenum];
drivePtr->read(buf, len, driveOffset);
return len;
}
} // end namespace sg