Raid5Volume.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using DiskAccessLibrary;
  11. using Utilities;
  12. namespace DiskAccessLibrary.LogicalDiskManager
  13. {
  14. /// <summary>
  15. /// Windows Software RAID-5 array
  16. /// </summary>
  17. public class Raid5Volume : DynamicVolume
  18. {
  19. private List<DynamicColumn> m_columns = new List<DynamicColumn>(); // must be sorted
  20. private int m_sectorsPerStripe;
  21. private long m_size;
  22. /// <param name="diskArray">One of the disks in the array can be null</param>
  23. public Raid5Volume(List<DynamicColumn> columns, int sectorsPerStripe, Guid volumeGuid, Guid diskGroupGuid) : base(volumeGuid, diskGroupGuid)
  24. {
  25. m_columns = columns;
  26. m_sectorsPerStripe = sectorsPerStripe;
  27. m_size = m_columns[0].Size * (m_columns.Count - 1);
  28. }
  29. // Each ArrayPosition is within a single stripe
  30. public List<ArrayPosition> TranslateSectors(long startSectorIndex, int sectorCount)
  31. {
  32. List<ArrayPosition> result = new List<ArrayPosition>();
  33. int numberOfColumns = m_columns.Count;
  34. int sectorsLeft = sectorCount;
  35. long currentSectorIndex = startSectorIndex;
  36. while (sectorsLeft > 0)
  37. {
  38. long dataStripeIndexInVolume = currentSectorIndex / m_sectorsPerStripe; // stripe index if we don't count parity stripes
  39. long stripeIndexInColumn = dataStripeIndexInVolume / (numberOfColumns - 1);
  40. int parityColumnIndex = (numberOfColumns - 1) - (int)(stripeIndexInColumn % numberOfColumns);
  41. int columnIndex = (int)(dataStripeIndexInVolume % numberOfColumns);
  42. // Another way to calculate columnIndex:
  43. //int stripeVerticalIndex = (int)(dataStripeIndexInVolume % (numberOfColumns - 1));
  44. //int columnIndex2 = (parityColumnIndex + 1 + stripeVerticalIndex) % numberOfColumns;
  45. long columnSectorIndex = stripeIndexInColumn * m_sectorsPerStripe + (currentSectorIndex % m_sectorsPerStripe);
  46. int sectorsToReadFromCurrentColumnStripe = Math.Min(m_sectorsPerStripe - (int)(columnSectorIndex % m_sectorsPerStripe), sectorsLeft);
  47. // e.g. :
  48. // Column 0: 0 3 P ...
  49. // Column 1: 1 P 4 ...
  50. // Column 2: P 2 5 ...
  51. // Column 0: 0 4 8 P ...
  52. // Column 1: 1 5 P 09 ...
  53. // Column 2: 2 P 6 10 ...
  54. // Column 3: P 3 7 11 ...
  55. ArrayPosition position = new ArrayPosition(columnIndex, columnSectorIndex, sectorsToReadFromCurrentColumnStripe);
  56. result.Add(position);
  57. currentSectorIndex += sectorsToReadFromCurrentColumnStripe;
  58. sectorsLeft -= sectorsToReadFromCurrentColumnStripe;
  59. }
  60. return result;
  61. }
  62. public override byte[] ReadSectors(long sectorIndex, int sectorCount)
  63. {
  64. CheckBoundaries(sectorIndex, sectorCount);
  65. List<ArrayPosition> readPositions = TranslateSectors(sectorIndex, sectorCount);
  66. byte[] result = new byte[sectorCount * BytesPerSector];
  67. int bytesRead = 0;
  68. foreach (ArrayPosition readPosition in readPositions)
  69. {
  70. DynamicColumn column = m_columns[readPosition.DiskIndex];
  71. byte[] stripeBytes;
  72. if (column.IsOperational)
  73. {
  74. stripeBytes = column.ReadSectors(readPosition.SectorIndex, readPosition.SectorCount);
  75. }
  76. else
  77. {
  78. stripeBytes = new byte[readPosition.SectorCount * BytesPerDynamicDiskSector];
  79. for (int index = 0; index < m_columns.Count; index++)
  80. {
  81. if (index != readPosition.DiskIndex)
  82. {
  83. byte[] currentBytes = m_columns[index].ReadSectors(readPosition.SectorIndex, readPosition.SectorCount);
  84. stripeBytes = ByteUtils.XOR(stripeBytes, currentBytes);
  85. }
  86. }
  87. }
  88. Array.Copy(stripeBytes, 0, result, bytesRead, stripeBytes.Length);
  89. bytesRead += stripeBytes.Length;
  90. }
  91. return result;
  92. }
  93. // We support degraded arrays
  94. public override void WriteSectors(long sectorIndex, byte[] data)
  95. {
  96. CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
  97. int numberOfColumns = m_columns.Count;
  98. int sectorCount = data.Length / this.BytesPerSector;
  99. List<ArrayPosition> writePositions = TranslateSectors(sectorIndex, sectorCount);
  100. int bytesWritten = 0;
  101. foreach (ArrayPosition writePosition in writePositions)
  102. {
  103. DynamicColumn column = m_columns[writePosition.DiskIndex];
  104. byte[] stripeBytes = new byte[writePosition.SectorCount * this.BytesPerSector];
  105. Array.Copy(data, bytesWritten, stripeBytes, 0, stripeBytes.Length);
  106. // first we obtain the necessary data from the other columns
  107. long stripeIndexInColumn = writePosition.SectorIndex / m_sectorsPerStripe;
  108. int parityColumnIndex = (numberOfColumns - 1) - (int)(stripeIndexInColumn % numberOfColumns);
  109. List<byte[]> segment = new List<byte[]>();
  110. for (int index = 0; index < numberOfColumns; index++)
  111. {
  112. if (m_columns[index].IsOperational)
  113. {
  114. byte[] bytes = m_columns[index].ReadSectors(writePosition.SectorIndex, writePosition.SectorCount);
  115. segment.Add(bytes);
  116. }
  117. else
  118. {
  119. segment.Add(null);
  120. }
  121. }
  122. int missingColumnIndex = segment.IndexOf(null);
  123. if (missingColumnIndex >= 0)
  124. {
  125. if (missingColumnIndex != parityColumnIndex && missingColumnIndex != writePosition.DiskIndex)
  126. {
  127. // let's calculate the missing data stripe
  128. byte[] missingBytes = segment[parityColumnIndex];
  129. for (int index = 0; index < numberOfColumns; index++)
  130. {
  131. if (index != missingColumnIndex && index != parityColumnIndex)
  132. {
  133. missingBytes = ByteUtils.XOR(missingBytes, segment[index]);
  134. }
  135. }
  136. segment[missingColumnIndex] = missingBytes;
  137. }
  138. }
  139. if (column.IsOperational)
  140. {
  141. column.WriteSectors(writePosition.SectorIndex, stripeBytes);
  142. }
  143. if (missingColumnIndex != parityColumnIndex)
  144. {
  145. // lets calculate the new parity disk
  146. segment[writePosition.DiskIndex] = stripeBytes;
  147. byte[] parityBytes = new byte[writePosition.SectorCount * this.BytesPerSector];
  148. for (int index = 0; index < numberOfColumns; index++)
  149. {
  150. if (index != parityColumnIndex)
  151. {
  152. parityBytes = ByteUtils.XOR(parityBytes, segment[index]);
  153. }
  154. }
  155. m_columns[parityColumnIndex].WriteSectors(writePosition.SectorIndex, parityBytes);
  156. }
  157. bytesWritten += stripeBytes.Length;
  158. }
  159. }
  160. public byte[] ReadStripes(long stripeIndex, int stripeCount)
  161. {
  162. return ReadSectors(stripeIndex * m_sectorsPerStripe, m_sectorsPerStripe * stripeCount);
  163. }
  164. public void WriteStripes(long stripeIndex, byte[] data)
  165. {
  166. WriteSectors(stripeIndex * m_sectorsPerStripe, data);
  167. }
  168. public override int BytesPerSector
  169. {
  170. get
  171. {
  172. return BytesPerDynamicDiskSector;
  173. }
  174. }
  175. public override long Size
  176. {
  177. get
  178. {
  179. return m_size;
  180. }
  181. }
  182. /// <summary>
  183. /// The number of sectors is always a multiple of SectorsPerStripe
  184. /// (if we modify the number of sectors manually to any other number, Windows will mark the array as "Failed" ["Too many bad RAID-5 column"])
  185. /// </summary>
  186. public long TotalStripes
  187. {
  188. get
  189. {
  190. return this.TotalSectors / m_sectorsPerStripe;
  191. }
  192. }
  193. public override List<DynamicColumn> Columns
  194. {
  195. get
  196. {
  197. return m_columns;
  198. }
  199. }
  200. public int SectorsPerStripe
  201. {
  202. get
  203. {
  204. return m_sectorsPerStripe;
  205. }
  206. }
  207. public int BytesPerStripe
  208. {
  209. get
  210. {
  211. return m_sectorsPerStripe * this.BytesPerSector;
  212. }
  213. }
  214. public int NumberOfColumns
  215. {
  216. get
  217. {
  218. return m_columns.Count;
  219. }
  220. }
  221. public long ColumnSize
  222. {
  223. get
  224. {
  225. return m_columns[0].Size;
  226. }
  227. }
  228. /// <summary>
  229. /// RAID-5 array can operate with a single missing disk (Failed redundancy)
  230. /// </summary>
  231. public override bool IsOperational
  232. {
  233. get
  234. {
  235. bool isDegraded = false;
  236. foreach (DynamicColumn column in m_columns)
  237. {
  238. if (!column.IsOperational)
  239. {
  240. if (isDegraded)
  241. {
  242. return false;
  243. }
  244. else
  245. {
  246. isDegraded = true;
  247. }
  248. }
  249. }
  250. return true;
  251. }
  252. }
  253. }
  254. public class ArrayPosition
  255. {
  256. public ArrayPosition(int diskIndex, long sectorIndex, int sectorCount)
  257. {
  258. DiskIndex = diskIndex;
  259. SectorIndex = sectorIndex;
  260. SectorCount = sectorCount;
  261. }
  262. public int DiskIndex; // Extent index or column index
  263. public long SectorIndex;
  264. public int SectorCount; // We are not going to read > 2^32 sectors at once
  265. }
  266. }