123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using DiskAccessLibrary;
- using Utilities;
- namespace DiskAccessLibrary.LogicalDiskManager
- {
- /// <summary>
- /// Windows Software RAID-5 array
- /// </summary>
- public class Raid5Volume : DynamicVolume
- {
- private List<DynamicColumn> m_columns = new List<DynamicColumn>(); // must be sorted
- private int m_sectorsPerStripe;
- private long m_size;
-
- /// <param name="diskArray">One of the disks in the array can be null</param>
- public Raid5Volume(List<DynamicColumn> columns, int sectorsPerStripe, Guid volumeGuid, Guid diskGroupGuid) : base(volumeGuid, diskGroupGuid)
- {
- m_columns = columns;
- m_sectorsPerStripe = sectorsPerStripe;
- m_size = m_columns[0].Size * (m_columns.Count - 1);
- }
- // Each ArrayPosition is within a single stripe
- public List<ArrayPosition> TranslateSectors(long startSectorIndex, int sectorCount)
- {
- List<ArrayPosition> result = new List<ArrayPosition>();
- int numberOfColumns = m_columns.Count;
- int sectorsLeft = sectorCount;
- long currentSectorIndex = startSectorIndex;
- while (sectorsLeft > 0)
- {
- long dataStripeIndexInVolume = currentSectorIndex / m_sectorsPerStripe; // stripe index if we don't count parity stripes
- long stripeIndexInColumn = dataStripeIndexInVolume / (numberOfColumns - 1);
- int parityColumnIndex = (numberOfColumns - 1) - (int)(stripeIndexInColumn % numberOfColumns);
- int columnIndex = (int)(dataStripeIndexInVolume % numberOfColumns);
- // Another way to calculate columnIndex:
- //int stripeVerticalIndex = (int)(dataStripeIndexInVolume % (numberOfColumns - 1));
- //int columnIndex2 = (parityColumnIndex + 1 + stripeVerticalIndex) % numberOfColumns;
- long columnSectorIndex = stripeIndexInColumn * m_sectorsPerStripe + (currentSectorIndex % m_sectorsPerStripe);
-
- int sectorsToReadFromCurrentColumnStripe = Math.Min(m_sectorsPerStripe - (int)(columnSectorIndex % m_sectorsPerStripe), sectorsLeft);
- // e.g. :
- // Column 0: 0 3 P ...
- // Column 1: 1 P 4 ...
- // Column 2: P 2 5 ...
- // Column 0: 0 4 8 P ...
- // Column 1: 1 5 P 09 ...
- // Column 2: 2 P 6 10 ...
- // Column 3: P 3 7 11 ...
- ArrayPosition position = new ArrayPosition(columnIndex, columnSectorIndex, sectorsToReadFromCurrentColumnStripe);
- result.Add(position);
- currentSectorIndex += sectorsToReadFromCurrentColumnStripe;
- sectorsLeft -= sectorsToReadFromCurrentColumnStripe;
- }
- return result;
- }
- public override byte[] ReadSectors(long sectorIndex, int sectorCount)
- {
- CheckBoundaries(sectorIndex, sectorCount);
- List<ArrayPosition> readPositions = TranslateSectors(sectorIndex, sectorCount);
- byte[] result = new byte[sectorCount * BytesPerSector];
- int bytesRead = 0;
- foreach (ArrayPosition readPosition in readPositions)
- {
- DynamicColumn column = m_columns[readPosition.DiskIndex];
- byte[] stripeBytes;
- if (column.IsOperational)
- {
- stripeBytes = column.ReadSectors(readPosition.SectorIndex, readPosition.SectorCount);
- }
- else
- {
- stripeBytes = new byte[readPosition.SectorCount * BytesPerDynamicDiskSector];
- for (int index = 0; index < m_columns.Count; index++)
- {
- if (index != readPosition.DiskIndex)
- {
- byte[] currentBytes = m_columns[index].ReadSectors(readPosition.SectorIndex, readPosition.SectorCount);
- stripeBytes = XOR(stripeBytes, currentBytes);
- }
- }
- }
- Array.Copy(stripeBytes, 0, result, bytesRead, stripeBytes.Length);
- bytesRead += stripeBytes.Length;
- }
- return result;
- }
-
- // We support degraded arrays
- public override void WriteSectors(long sectorIndex, byte[] data)
- {
- CheckBoundaries(sectorIndex, data.Length / this.BytesPerSector);
- int numberOfColumns = m_columns.Count;
- int sectorCount = data.Length / this.BytesPerSector;
- List<ArrayPosition> writePositions = TranslateSectors(sectorIndex, sectorCount);
- int bytesWritten = 0;
- foreach (ArrayPosition writePosition in writePositions)
- {
- DynamicColumn column = m_columns[writePosition.DiskIndex];
- byte[] stripeBytes = new byte[writePosition.SectorCount * this.BytesPerSector];
- Array.Copy(data, bytesWritten, stripeBytes, 0, stripeBytes.Length);
-
- // first we obtain the necessary data from the other columns
- long stripeIndexInColumn = writePosition.SectorIndex / m_sectorsPerStripe;
- int parityColumnIndex = (numberOfColumns - 1) - (int)(stripeIndexInColumn % numberOfColumns);
- List<byte[]> segment = new List<byte[]>();
- for (int index = 0; index < numberOfColumns; index++)
- {
- if (m_columns[index].IsOperational)
- {
- byte[] bytes = m_columns[index].ReadSectors(writePosition.SectorIndex, writePosition.SectorCount);
- segment.Add(bytes);
- }
- else
- {
- segment.Add(null);
- }
- }
- int missingColumnIndex = segment.IndexOf(null);
- if (missingColumnIndex >= 0)
- {
- if (missingColumnIndex != parityColumnIndex && missingColumnIndex != writePosition.DiskIndex)
- {
- // let's calculate the missing data stripe
- byte[] missingBytes = segment[parityColumnIndex];
- for (int index = 0; index < numberOfColumns; index++)
- {
- if (index != missingColumnIndex && index != parityColumnIndex)
- {
- missingBytes = XOR(missingBytes, segment[index]);
- }
- }
- segment[missingColumnIndex] = missingBytes;
- }
- }
- if (column.IsOperational)
- {
- column.WriteSectors(writePosition.SectorIndex, stripeBytes);
- }
- if (missingColumnIndex != parityColumnIndex)
- {
- // lets calculate the new parity disk
- segment[writePosition.DiskIndex] = stripeBytes;
- byte[] parityBytes = new byte[writePosition.SectorCount * this.BytesPerSector];
- for (int index = 0; index < numberOfColumns; index++)
- {
- if (index != parityColumnIndex)
- {
- parityBytes = XOR(parityBytes, segment[index]);
- }
- }
- m_columns[parityColumnIndex].WriteSectors(writePosition.SectorIndex, parityBytes);
- }
- bytesWritten += stripeBytes.Length;
- }
- }
- public byte[] ReadStripes(long stripeIndex, int stripeCount)
- {
- return ReadSectors(stripeIndex * m_sectorsPerStripe, m_sectorsPerStripe * stripeCount);
- }
- public void WriteStripes(long stripeIndex, byte[] data)
- {
- WriteSectors(stripeIndex * m_sectorsPerStripe, data);
- }
- public override int BytesPerSector
- {
- get
- {
- return BytesPerDynamicDiskSector;
- }
- }
- public override long Size
- {
- get
- {
- return m_size;
- }
- }
- /// <summary>
- /// The number of sectors is always a multiple of SectorsPerStripe
- /// (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"])
- /// </summary>
- public long TotalStripes
- {
- get
- {
- return this.TotalSectors / m_sectorsPerStripe;
- }
- }
- public override List<DynamicColumn> Columns
- {
- get
- {
- return m_columns;
- }
- }
- public int SectorsPerStripe
- {
- get
- {
- return m_sectorsPerStripe;
- }
- }
- public int BytesPerStripe
- {
- get
- {
- return m_sectorsPerStripe * this.BytesPerSector;
- }
- }
- public int NumberOfColumns
- {
- get
- {
- return m_columns.Count;
- }
- }
- public long ColumnSize
- {
- get
- {
- return m_columns[0].Size;
- }
- }
- /// <summary>
- /// RAID-5 array can operate with a single missing disk (Failed redundancy)
- /// </summary>
- public override bool IsOperational
- {
- get
- {
- bool isDegraded = false;
- foreach (DynamicColumn column in m_columns)
- {
- if (!column.IsOperational)
- {
- if (isDegraded)
- {
- return false;
- }
- else
- {
- isDegraded = true;
- }
- }
- }
- return true;
- }
- }
- public static byte[] XOR(byte[] array1, byte[] array2)
- {
- return XOR(array1, array2, 0, array2.Length);
- }
- /// <param name="offset">In array 2</param>
- /// <param name="length">Of bytes in Array 2 to XOR</param>
- public static byte[] XOR(byte[] array1, byte[] array2, int offset, int length)
- {
- if (array1.Length == length)
- {
- byte[] result = new byte[length];
- for (int index = 0; index < length; index++)
- {
- result[index] = (byte)(array1[index] ^ array2[index + offset]);
- }
- return result;
- }
- else
- {
- throw new ArgumentException("Arrays must be of equal length");
- }
- }
- }
- public class ArrayPosition
- {
- public ArrayPosition(int diskIndex, long sectorIndex, int sectorCount)
- {
- DiskIndex = diskIndex;
- SectorIndex = sectorIndex;
- SectorCount = sectorCount;
- }
- public int DiskIndex; // Extent index or column index
- public long SectorIndex;
- public int SectorCount; // We are not going to read > 2^32 sectors at once
- }
- }
|