123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Utilities;
- namespace DiskAccessLibrary.LogicalDiskManager
- {
- public class VolumeManagerDatabase
- {
- public const uint FirstSequenceNumber = 4;
- private DynamicDisk m_disk;
- private VolumeManagerDatabaseHeader m_databaseHeader;
- private List<DatabaseRecord> m_databaseRecords;
- private KernelUpdateLog m_kernelUpdateLog;
-
- private ulong m_nextRecordID;
- public VolumeManagerDatabase(DynamicDisk disk, VolumeManagerDatabaseHeader databaseHeader, List<DatabaseRecord> databaseRecords, KernelUpdateLog kernelUpdateLog)
- {
- m_disk = disk;
- m_databaseHeader = databaseHeader;
- m_databaseRecords = databaseRecords;
- m_kernelUpdateLog = kernelUpdateLog;
- m_nextRecordID = m_databaseHeader.CommitTransactionID + 1;
- }
- public ulong AllocateNewRecordID()
- {
- m_nextRecordID++;
- return m_nextRecordID - 1;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void UpdateDatabase(List<DatabaseRecord> records)
- {
- foreach (DatabaseRecord newRecord in records)
- {
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (newRecord == record)
- {
-
- throw new ArgumentException("New record must not reference record already in the database");
- }
- }
- }
- VerifyDatabaseConsistency();
-
- MarkOldRecordsAsPendingDeletion(records);
-
-
- ulong pendingTransactionID = m_databaseHeader.CommitTransactionID + (ulong)records.Count + 1;
- PrepareNewRecordsForWriting(records, pendingTransactionID);
-
- m_databaseRecords.AddRange(records);
- WritePendingRecords();
-
- m_databaseHeader.UpdateStatus = DatabaseHeaderUpdateStatus.Change;
- m_databaseHeader.PendingTransactionID = pendingTransactionID;
- m_databaseHeader.PendingTotalNumberOfVolumeRecords = GetPendingTotalNumberOfRecords<VolumeRecord>();
- m_databaseHeader.PendingTotalNumberOfComponentRecords = GetPendingTotalNumberOfRecords<ComponentRecord>();
- m_databaseHeader.PendingTotalNumberOfExtentRecords = GetPendingTotalNumberOfRecords<ExtentRecord>();
- m_databaseHeader.PendingTotalNumberOfDiskRecords = GetPendingTotalNumberOfRecords<DiskRecord>();
- WriteDatabaseHeader();
-
- SetKernelUpdateLogLastEntry(m_databaseHeader.CommitTransactionID, pendingTransactionID);
-
-
- m_databaseHeader.UpdateStatus = DatabaseHeaderUpdateStatus.Commit;
- WriteDatabaseHeader();
-
-
- DeletePendingDeletionRecords();
- ActivatePendingActivationRecords();
-
- m_databaseHeader.UpdateStatus = DatabaseHeaderUpdateStatus.Clean;
- m_databaseHeader.CommitTransactionID = pendingTransactionID;
- m_databaseHeader.CommittedTotalNumberOfVolumeRecords = (uint)this.VolumeRecords.Count;
- m_databaseHeader.CommittedTotalNumberOfComponentRecords = (uint)this.ComponentRecords.Count;
- m_databaseHeader.CommittedTotalNumberOfExtentRecords = (uint)this.ExtentRecords.Count;
- m_databaseHeader.CommittedTotalNumberOfDiskRecords = (uint)this.DiskRecords.Count;
- WriteDatabaseHeader();
- m_nextRecordID = m_databaseHeader.CommitTransactionID + 1;
- }
- virtual public void VerifyDatabaseConsistency()
- {
- if (m_databaseHeader.PendingTransactionID != m_databaseHeader.CommitTransactionID ||
- m_databaseHeader.UpdateStatus != DatabaseHeaderUpdateStatus.Clean)
- {
- throw new Exception("Database is in inconsistent state");
- }
- if (m_databaseHeader.MajorVersion != 4 || m_databaseHeader.MinorVersion != 10)
- {
- throw new NotImplementedException("Database version is not supported");
- }
- }
-
-
-
- private void MarkOldRecordsAsPendingDeletion(List<DatabaseRecord> newRecords)
- {
- foreach (DatabaseRecord newRecord in newRecords)
- {
- int index = m_databaseRecords.IndexOf(newRecord);
- if (index >= 0)
- {
- m_databaseRecords[index].UpdateStatus = DatabaseRecordUpdateStatus.ActivePendingDeletion;
- m_databaseRecords[index].UpdateHeader();
- }
- }
- }
-
-
-
- private void WritePendingRecords()
- {
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (record.UpdateStatus != DatabaseRecordUpdateStatus.Active)
- {
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- WriteDatabaseRecordFragment(fragment);
- }
- }
- }
- }
- private void DeletePendingDeletionRecords()
- {
- List<DatabaseRecord> pendingDeletion = new List<DatabaseRecord>();
-
- for (int index = 0; index < m_databaseRecords.Count; index++)
- {
- if (m_databaseRecords[index].UpdateStatus == DatabaseRecordUpdateStatus.ActivePendingDeletion)
- {
- pendingDeletion.Add(m_databaseRecords[index]);
- m_databaseRecords.RemoveAt(index);
- index--;
- }
- }
-
- foreach (DatabaseRecord record in pendingDeletion)
- {
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- fragment.Clear();
- WriteDatabaseRecordFragment(fragment);
- }
- }
- }
- private void ActivatePendingActivationRecords()
- {
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (record.UpdateStatus == DatabaseRecordUpdateStatus.PendingActivation)
- {
- record.UpdateStatus = DatabaseRecordUpdateStatus.Active;
- record.UpdateHeader();
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- WriteDatabaseRecordFragment(fragment);
- }
- }
- }
- }
-
-
-
- private void PrepareNewRecordsForWriting(List<DatabaseRecord> newRecords, ulong pendingTransactionID)
- {
- uint startFromSequenceNumber = FirstSequenceNumber;
- uint startFromGroupNumber = 1;
- foreach (DatabaseRecord record in newRecords)
- {
-
-
- if (record.Id == 0)
- {
- record.Id = AllocateNewRecordID();
- }
- if (record is VolumeRecord)
- {
- ((VolumeRecord)record).CommitTransactionID = pendingTransactionID;
- }
- else if (record is ComponentRecord)
- {
- ((ComponentRecord)record).CommitTransactionID = pendingTransactionID;
- }
- else if (record is ExtentRecord)
- {
- ((ExtentRecord)record).CommitTransactionID = pendingTransactionID;
- }
- if (record is DiskRecord)
- {
- ((DiskRecord)record).CommitTransactionID = pendingTransactionID;
- }
- else if (record is DiskGroupRecord)
- {
- ((DiskGroupRecord)record).CommitTransactionID = pendingTransactionID;
- }
- record.UpdateStatus = DatabaseRecordUpdateStatus.PendingActivation;
-
- record.UpdateFragments((int)DatabaseHeader.BlockSize);
- uint groupNumber = GetAvailableFragmentGroupNumber(startFromGroupNumber);
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- fragment.SequenceNumber = GetAvailableFragmentSequenceNumber(startFromSequenceNumber);
- fragment.GroupNumber = groupNumber;
- startFromSequenceNumber = fragment.SequenceNumber + 1;
- }
- startFromGroupNumber = groupNumber + 1;
- }
- }
- virtual protected void WriteDatabaseHeader()
- {
- VolumeManagerDatabaseHeader.WriteToDisk(m_disk, m_databaseHeader);
- }
- virtual protected void WriteDatabaseRecordFragment(DatabaseRecordFragment fragment)
- {
- WriteDatabaseRecordFragment(m_disk, fragment, (int)m_databaseHeader.BlockSize);
- }
- virtual protected void SetKernelUpdateLogLastEntry(ulong committedTransactionID, ulong pendingTransactionID)
- {
- m_kernelUpdateLog.SetLastEntry(m_disk, committedTransactionID, pendingTransactionID);
- }
-
-
- public uint GetAvailableFragmentSequenceNumber(uint startFromSequenceNumber)
- {
- List<uint> sequenceNumbers = new List<uint>();
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- sequenceNumbers.Add(fragment.SequenceNumber);
- }
- }
- sequenceNumbers.Sort();
- for (uint sequenceNumber = startFromSequenceNumber; sequenceNumber < m_databaseHeader.NumberOfVBlks; sequenceNumber++)
- {
- if (!sequenceNumbers.Contains(sequenceNumber))
- {
- return sequenceNumber;
- }
- }
- throw new Exception("VMDB is full");
- }
-
- public uint GetAvailableFragmentGroupNumber(uint startFromGroupNumber)
- {
- List<uint> groupNumbers = new List<uint>();
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- foreach (DatabaseRecordFragment fragment in record.Fragments)
- {
- groupNumbers.Add(fragment.GroupNumber);
- }
- }
- groupNumbers.Sort();
-
- for (uint groupNumber = startFromGroupNumber; groupNumber < m_databaseHeader.NumberOfVBlks; groupNumber++)
- {
- if (!groupNumbers.Contains(groupNumber))
- {
- return groupNumber;
- }
- }
- throw new Exception("VMDB is full, can't find available GroupNumber");
- }
- public List<T> GetRecords<T>() where T:DatabaseRecord
- {
- List<T> result = new List<T>();
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (record is T)
- {
- result.Add((T)record);
- }
- }
- return result;
- }
- public List<T> GetActiveRecords<T>() where T : DatabaseRecord
- {
- List<T> result = new List<T>();
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (record.UpdateStatus != DatabaseRecordUpdateStatus.PendingActivation && record is T)
- {
- result.Add((T)record);
- }
- }
- return result;
- }
- public uint GetPendingTotalNumberOfRecords<T>() where T : DatabaseRecord
- {
- uint result = 0;
- foreach (DatabaseRecord record in m_databaseRecords)
- {
- if (record is T)
- {
- if (record.UpdateStatus != DatabaseRecordUpdateStatus.ActivePendingDeletion)
- {
- result++;
- }
- }
- }
- return result;
- }
- public VolumeRecord FindVolumeByVolumeID(ulong volumeID)
- {
- foreach (VolumeRecord record in this.VolumeRecords)
- {
- if (record.VolumeId == volumeID)
- {
- return record;
- }
- }
- return null;
- }
- public VolumeRecord FindVolumeByVolumeGuid(Guid volumeGuid)
- {
- foreach (VolumeRecord record in this.VolumeRecords)
- {
- if (record.VolumeGuid == volumeGuid)
- {
- return record;
- }
- }
- return null;
- }
- public List<ComponentRecord> FindComponentsByVolumeID(ulong volumeID)
- {
- List<ComponentRecord> result = new List<ComponentRecord>();
- foreach (ComponentRecord record in this.ComponentRecords)
- {
- if (record.VolumeId == volumeID)
- {
- result.Add(record);
- }
- }
- return result;
- }
-
-
-
- public List<ExtentRecord> FindExtentsByComponentID(ulong componentID)
- {
- List<ExtentRecord> result = new List<ExtentRecord>();
- foreach (ExtentRecord record in this.ExtentRecords)
- {
- if (record.ComponentId == componentID)
- {
- result.Add(record);
- }
- }
-
- result.Sort(CompareByOffsetInColumn);
-
- return result;
- }
-
- public List<ExtentRecord> FindExtentsByDiskID(ulong diskID)
- {
- List<ExtentRecord> result = new List<ExtentRecord>();
- foreach (ExtentRecord record in this.ExtentRecords)
- {
- if (record.DiskId == diskID)
- {
- result.Add(record);
- }
- }
- return result;
- }
- public ExtentRecord FindExtentByExtentID(ulong extentID)
- {
- foreach (ExtentRecord record in this.ExtentRecords)
- {
- if (record.ExtentId == extentID)
- {
- return record;
- }
- }
- return null;
- }
- public DiskRecord FindDiskByDiskID(ulong diskID)
- {
- foreach (DiskRecord record in this.DiskRecords)
- {
- if (record.DiskId == diskID)
- {
- return record;
- }
- }
- return null;
- }
- public DiskRecord FindDiskByDiskGuid(Guid diskGuid)
- {
- foreach (DiskRecord record in this.DiskRecords)
- {
- if (record.DiskGuid == diskGuid)
- {
- return record;
- }
- }
- return null;
- }
- public VolumeManagerDatabaseHeader DatabaseHeader
- {
- get
- {
- return m_databaseHeader;
- }
- }
- public List<DatabaseRecord> DatabaseRecords
- {
- get
- {
- return m_databaseRecords;
- }
- }
- public KernelUpdateLog KernelUpdateLog
- {
- get
- {
- return m_kernelUpdateLog;
- }
- }
-
- public List<DiskRecord> DiskRecords
- {
- get
- {
- return GetActiveRecords<DiskRecord>();
- }
- }
- public List<VolumeRecord> VolumeRecords
- {
- get
- {
- return GetActiveRecords<VolumeRecord>();
- }
- }
- public List<ComponentRecord> ComponentRecords
- {
- get
- {
- return GetActiveRecords<ComponentRecord>();
- }
- }
- public List<DiskGroupRecord> DiskGroupRecords
- {
- get
- {
- return GetActiveRecords<DiskGroupRecord>();
- }
- }
- public List<ExtentRecord> ExtentRecords
- {
- get
- {
- return GetActiveRecords<ExtentRecord>();
- }
- }
- public Guid DiskGroupGuid
- {
- get
- {
- return m_databaseHeader.DiskGroupGuid;
- }
- }
- public string DiskGroupName
- {
- get
- {
- return m_databaseHeader.DiskGroupName;
- }
- }
- public static VolumeManagerDatabase ReadFromDisk(DynamicDisk disk)
- {
- return ReadFromDisk(disk.Disk, disk.PrivateHeader, disk.TOCBlock);
- }
- public static VolumeManagerDatabase ReadFromDisk(Disk disk)
- {
- if (DynamicDisk.IsDynamicDisk(disk))
- {
- PrivateHeader privateHeader = PrivateHeader.ReadFromDisk(disk);
- if (privateHeader != null)
- {
- return ReadFromDisk(disk, privateHeader);
- }
- }
- return null;
- }
- public static VolumeManagerDatabase ReadFromDisk(Disk disk, PrivateHeader privateHeader)
- {
- TOCBlock tocBlock = TOCBlock.ReadFromDisk(disk, privateHeader);
- if (tocBlock != null)
- {
- return ReadFromDisk(disk, privateHeader, tocBlock);
- }
- return null;
- }
- public static VolumeManagerDatabase ReadFromDisk(Disk disk, PrivateHeader privateHeader, TOCBlock tocBlock)
- {
- VolumeManagerDatabaseHeader databaseHeader = VolumeManagerDatabaseHeader.ReadFromDisk(disk, privateHeader, tocBlock);
- if (databaseHeader == null)
- {
- return null;
- }
- List<DatabaseRecord> databaseRecords = new List<DatabaseRecord>();
-
- ulong firstSector = privateHeader.PrivateRegionStartLBA + tocBlock.ConfigStart + 1;
- int sectorCount = (int)Math.Ceiling(databaseHeader.NumberOfVBlks * databaseHeader.BlockSize / (double)disk.BytesPerSector);
- byte[] databaseBytes = disk.ReadSectors((long)firstSector, sectorCount);
-
-
- Dictionary<uint, List<DatabaseRecordFragment>> fragments = new Dictionary<uint, List<DatabaseRecordFragment>>();
- for (uint index = 0; index < databaseHeader.NumberOfVBlks - 4; index++)
- {
- byte[] fragmentBytes = new byte[databaseHeader.BlockSize];
- Array.Copy(databaseBytes, (long)index * databaseHeader.BlockSize, fragmentBytes, 0, databaseHeader.BlockSize);
- DatabaseRecordFragment fragment = DatabaseRecordFragment.GetDatabaseRecordFragment(fragmentBytes);
- if (fragment != null)
- {
- if (fragments.ContainsKey(fragment.GroupNumber))
- {
- fragments[fragment.GroupNumber].Add(fragment);
- }
- else
- {
- List<DatabaseRecordFragment> recordFragments = new List<DatabaseRecordFragment>();
- recordFragments.Add(fragment);
- fragments.Add(fragment.GroupNumber, recordFragments);
- }
- }
- }
-
-
-
- foreach (List<DatabaseRecordFragment> recorFragments in fragments.Values)
- {
- DatabaseRecord databaseRecord = DatabaseRecord.GetDatabaseRecord(recorFragments);
- databaseRecords.Add(databaseRecord);
- }
-
- KernelUpdateLog kernelUpdateLog = KernelUpdateLog.ReadFromDisk(disk, privateHeader, tocBlock);
- DynamicDisk dynamicDisk = new DynamicDisk(disk, privateHeader, tocBlock);
- return new VolumeManagerDatabase(dynamicDisk, databaseHeader, databaseRecords, kernelUpdateLog);
- }
- public static void WriteDatabaseRecordFragment(DynamicDisk disk, DatabaseRecordFragment fragment, int blockSize)
- {
- if (fragment.SequenceNumber < 4)
- {
- throw new ArgumentException("VBLK SequenceNumber must start from 4");
- }
- PrivateHeader privateHeader = disk.PrivateHeader;
- TOCBlock tocBlock = disk.TOCBlock;
- ulong sectorIndex = privateHeader.PrivateRegionStartLBA + tocBlock.ConfigStart;
- int fragmentsPerSector = (int)(disk.Disk.BytesPerSector / blockSize);
- sectorIndex += (ulong)(fragment.SequenceNumber / fragmentsPerSector);
- byte[] sectorBytes = disk.Disk.ReadSector((long)sectorIndex);
- byte[] fragmentBytes = fragment.GetBytes(blockSize);
- int indexInSector = (int)(fragment.SequenceNumber % fragmentsPerSector);
- Array.Copy(fragmentBytes, 0, sectorBytes, indexInSector * blockSize, blockSize);
- disk.Disk.WriteSectors((long)sectorIndex, sectorBytes);
- }
- private static int CompareByColumnIndex(ExtentRecord x, ExtentRecord y)
- {
- return x.ColumnIndex.CompareTo(y.ColumnIndex);
- }
- private static int CompareByOffsetInColumn(ExtentRecord x, ExtentRecord y)
- {
- return x.OffsetInColumnLBA.CompareTo(y.OffsetInColumnLBA);
- }
- }
- }
|