MoveExtentHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.IO;
  10. using System.Text;
  11. using Utilities;
  12. using DiskAccessLibrary;
  13. namespace DiskAccessLibrary.LogicalDiskManager
  14. {
  15. public class MoveExtentHelper
  16. {
  17. public const int BackupBufferSizeLBA = 128; // there are about 180 contiguous free sectors in a private region
  18. /// <summary>
  19. /// Move extent to another disk
  20. /// </summary>
  21. public static void MoveExtentToAnotherDisk(List<DynamicDisk> disks, DynamicVolume volume, DynamicDiskExtent sourceExtent, DiskExtent relocatedExtent, ref long bytesCopied)
  22. {
  23. DiskGroupDatabase database = DiskGroupDatabase.ReadFromDisks(disks, volume.DiskGroupGuid);
  24. // copy the data
  25. long transferSizeLBA = Settings.MaximumTransferSizeLBA;
  26. for (long sectorIndex = 0; sectorIndex < relocatedExtent.TotalSectors; sectorIndex += transferSizeLBA)
  27. {
  28. long sectorsLeft = relocatedExtent.TotalSectors - sectorIndex;
  29. int sectorsToRead = (int)Math.Min(transferSizeLBA, sectorsLeft);
  30. byte[] data = sourceExtent.ReadSectors(sectorIndex, sectorsToRead);
  31. relocatedExtent.WriteSectors(sectorIndex, data);
  32. bytesCopied += sectorsToRead * sourceExtent.BytesPerSector;
  33. }
  34. // Update the database to point to the relocated extent
  35. DynamicDisk targetDisk = DynamicDisk.ReadFromDisk(relocatedExtent.Disk);
  36. DynamicDiskExtent dynamicRelocatedExtent = new DynamicDiskExtent(relocatedExtent, sourceExtent.ExtentID);
  37. dynamicRelocatedExtent.Name = sourceExtent.Name;
  38. dynamicRelocatedExtent.DiskGuid = targetDisk.DiskGuid;
  39. VolumeManagerDatabaseHelper.UpdateExtentLocation(database, volume, dynamicRelocatedExtent);
  40. }
  41. /// <summary>
  42. /// Move extent to a new location on the same disk
  43. /// </summary>
  44. public static void MoveExtentWithinSameDisk(List<DynamicDisk> disks, DynamicVolume volume, DynamicDiskExtent sourceExtent, DiskExtent relocatedExtent, ref long bytesCopied)
  45. {
  46. DiskGroupDatabase database = DiskGroupDatabase.ReadFromDisks(disks, volume.DiskGroupGuid);
  47. MoveExtentOperationBootRecord resumeRecord = new MoveExtentOperationBootRecord();
  48. // If there will be a power failure during the move, a RAID volume will resync during boot,
  49. // To prevent destruction of the data, we temporarily convert the array to striped volume
  50. if (volume is Raid5Volume)
  51. {
  52. VolumeManagerDatabaseHelper.ConvertRaidToStripedVolume(database, volume.VolumeGuid);
  53. resumeRecord.RestoreRAID5 = true;
  54. }
  55. // We want to write our own volume boot sector for recovery purposes, so we must find where to backup the old boot sector.
  56. // We don't want to store the backup in the range of the existing or relocated extent, because then we would have to move
  57. // the backup around during the move operation, other options include:
  58. // 1. Store it between sectors 1-62 (cons: Could be in use, Windows occasionally start a volume from sector 1)
  59. // 2. Find an easily compressible sector (e.g. zero-filled) within the existing extent, overwrite it with the backup, and restore it when the operation is done.
  60. // 3. use the LDM private region to store the sector.
  61. DynamicDisk dynamicDisk = DynamicDisk.ReadFromDisk(relocatedExtent.Disk);
  62. // Note: backupSectorIndex will be from the beginning of the private region while backupBufferStartSector will be from the end
  63. // so there is no need to allocate them
  64. long backupSectorIndex = DynamicDiskHelper.FindUnusedSectorInPrivateRegion(dynamicDisk);
  65. resumeRecord.VolumeGuid = volume.VolumeGuid;
  66. resumeRecord.NumberOfCommittedSectors = 0;
  67. resumeRecord.ExtentID = sourceExtent.ExtentID;
  68. resumeRecord.OldStartSector = (ulong)sourceExtent.FirstSector;
  69. resumeRecord.NewStartSector = (ulong)relocatedExtent.FirstSector;
  70. resumeRecord.BootRecordBackupSector = (ulong)backupSectorIndex;
  71. long distanceLBA = (long)Math.Abs((double)resumeRecord.NewStartSector - resumeRecord.OldStartSector);
  72. if (distanceLBA < MoveHelper.BufferedModeThresholdLBA)
  73. {
  74. long backupBufferStartSector = DynamicDiskHelper.FindUnusedRegionInPrivateRegion(dynamicDisk, BackupBufferSizeLBA);
  75. if (backupBufferStartSector == -1)
  76. {
  77. throw new Exception("Private region is full");
  78. }
  79. if (backupBufferStartSector <= backupSectorIndex)
  80. {
  81. throw new Exception("Private region structure is unknown");
  82. }
  83. resumeRecord.BackupBufferStartSector = (ulong)backupBufferStartSector;
  84. resumeRecord.BackupBufferSizeLBA = BackupBufferSizeLBA;
  85. }
  86. // Backup the first sector of the first extent
  87. // (We replace the filesystem boot record with our own sector for recovery purposes)
  88. byte[] filesystemBootRecord = volume.ReadSector(0);
  89. relocatedExtent.Disk.WriteSectors(backupSectorIndex, filesystemBootRecord);
  90. // we write the resume record instead of the boot record
  91. volume.WriteSectors(0, resumeRecord.GetBytes());
  92. if (sourceExtent.FirstSector < relocatedExtent.FirstSector)
  93. {
  94. // move right
  95. MoveExtentRight(disks, volume, resumeRecord, ref bytesCopied);
  96. }
  97. else
  98. {
  99. // move left
  100. // we write the resume record at the new location as well (to be able to resume if a power failure will occur immediately after updating the database)
  101. relocatedExtent.WriteSectors(0, resumeRecord.GetBytes());
  102. DynamicDiskExtent dynamicRelocatedExtent = new DynamicDiskExtent(relocatedExtent, sourceExtent.ExtentID);
  103. dynamicRelocatedExtent.Name = sourceExtent.Name;
  104. dynamicRelocatedExtent.DiskGuid = sourceExtent.DiskGuid;
  105. VolumeManagerDatabaseHelper.UpdateExtentLocation(database, volume, dynamicRelocatedExtent);
  106. int extentIndex = DynamicDiskExtentHelper.GetIndexOfExtentID(volume.DynamicExtents, sourceExtent.ExtentID);
  107. // get the updated volume (we just moved an extent)
  108. volume = DynamicVolumeHelper.GetVolumeByGuid(disks, volume.VolumeGuid);
  109. MoveExtentLeft(disks, volume, resumeRecord, ref bytesCopied);
  110. }
  111. }
  112. public static void ResumeMoveExtent(List<DynamicDisk> disks, DynamicVolume volume, MoveExtentOperationBootRecord resumeRecord, ref long bytesCopied)
  113. {
  114. if (resumeRecord.OldStartSector == resumeRecord.NewStartSector)
  115. {
  116. throw new InvalidDataException("Invalid move record");
  117. }
  118. if (resumeRecord.RestoreFromBuffer)
  119. {
  120. // we need to use the backup buffer to restore the data that may have been overwritten
  121. int extentIndex = DynamicDiskExtentHelper.GetIndexOfExtentID(volume.DynamicExtents, resumeRecord.ExtentID);
  122. DynamicDiskExtent sourceExtent = volume.DynamicExtents[extentIndex];
  123. byte[] backupBuffer = sourceExtent.Disk.ReadSectors((long)resumeRecord.BackupBufferStartSector, BackupBufferSizeLBA);
  124. if (resumeRecord.OldStartSector < resumeRecord.NewStartSector)
  125. {
  126. // move right
  127. long readCount = (long)resumeRecord.NumberOfCommittedSectors;
  128. int sectorsToRead = BackupBufferSizeLBA;
  129. long sectorIndex = sourceExtent.TotalSectors - readCount - sectorsToRead;
  130. sourceExtent.WriteSectors(sectorIndex, backupBuffer);
  131. System.Diagnostics.Debug.WriteLine("Restored to " + sectorIndex);
  132. }
  133. else
  134. {
  135. // move left
  136. long sectorIndex = (long)resumeRecord.NumberOfCommittedSectors;
  137. sourceExtent.WriteSectors(sectorIndex, backupBuffer);
  138. System.Diagnostics.Debug.WriteLine("Restored to " + sectorIndex);
  139. }
  140. }
  141. if (resumeRecord.OldStartSector < resumeRecord.NewStartSector)
  142. {
  143. MoveExtentRight(disks, volume, resumeRecord, ref bytesCopied);
  144. }
  145. else
  146. {
  147. MoveExtentLeft(disks, volume, resumeRecord, ref bytesCopied);
  148. }
  149. }
  150. private static void MoveExtentRight(List<DynamicDisk> disks, DynamicVolume volume, MoveExtentOperationBootRecord resumeRecord, ref long bytesCopied)
  151. {
  152. DiskGroupDatabase database = DiskGroupDatabase.ReadFromDisks(disks, volume.DiskGroupGuid);
  153. int extentIndex = DynamicDiskExtentHelper.GetIndexOfExtentID(volume.DynamicExtents, resumeRecord.ExtentID);
  154. DynamicDiskExtent sourceExtent = volume.DynamicExtents[extentIndex];
  155. DiskExtent relocatedExtent = new DiskExtent(sourceExtent.Disk, (long)resumeRecord.NewStartSector, sourceExtent.Size);
  156. MoveHelper.MoveExtentDataRight(volume, sourceExtent, relocatedExtent, resumeRecord, ref bytesCopied);
  157. // even if the database update won't complete, the resume record was copied
  158. // update the database
  159. DynamicDiskExtent dynamicRelocatedExtent = new DynamicDiskExtent(relocatedExtent, sourceExtent.ExtentID);
  160. dynamicRelocatedExtent.Name = sourceExtent.Name;
  161. dynamicRelocatedExtent.DiskGuid = sourceExtent.DiskGuid;
  162. VolumeManagerDatabaseHelper.UpdateExtentLocation(database, volume, dynamicRelocatedExtent);
  163. // if this is a resume, then volume is StripedVolume, otherwise it is a Raid5Volume
  164. if (resumeRecord.RestoreRAID5)
  165. {
  166. VolumeManagerDatabaseHelper.ConvertStripedVolumeToRaid(database, volume.VolumeGuid);
  167. }
  168. // get the updated volume (we moved an extent and possibly reconverted to RAID-5)
  169. volume = DynamicVolumeHelper.GetVolumeByGuid(disks, volume.VolumeGuid);
  170. // restore the filesystem boot sector
  171. byte[] filesystemBootRecord = relocatedExtent.Disk.ReadSector((long)resumeRecord.BootRecordBackupSector);
  172. volume.WriteSectors(0, filesystemBootRecord);
  173. ClearBackupData(relocatedExtent.Disk, resumeRecord);
  174. }
  175. private static void MoveExtentLeft(List<DynamicDisk> disks, DynamicVolume volume, MoveExtentOperationBootRecord resumeRecord, ref long bytesCopied)
  176. {
  177. DiskGroupDatabase database = DiskGroupDatabase.ReadFromDisks(disks, volume.DiskGroupGuid);
  178. DynamicDiskExtent relocatedExtent = DynamicDiskExtentHelper.GetByExtentID(volume.DynamicExtents, resumeRecord.ExtentID);
  179. if (resumeRecord.OldStartSector == (ulong)relocatedExtent.FirstSector)
  180. {
  181. // the database update was not completed (this must be a resume operation)
  182. relocatedExtent = new DynamicDiskExtent(relocatedExtent.Disk, (long)resumeRecord.NewStartSector, relocatedExtent.Size, resumeRecord.ExtentID);
  183. VolumeManagerDatabaseHelper.UpdateExtentLocation(database, volume, relocatedExtent);
  184. }
  185. DiskExtent sourceExtent = new DiskExtent(relocatedExtent.Disk, (long)resumeRecord.OldStartSector, relocatedExtent.Size);
  186. MoveHelper.MoveExtentDataLeft(volume, sourceExtent, relocatedExtent, resumeRecord, ref bytesCopied);
  187. // if this is a resume, then volume is StripedVolume, otherwise it is a Raid5Volume
  188. if (resumeRecord.RestoreRAID5)
  189. {
  190. VolumeManagerDatabaseHelper.ConvertStripedVolumeToRaid(database, volume.VolumeGuid);
  191. // get the updated volume (we just reconverted to RAID-5)
  192. volume = DynamicVolumeHelper.GetVolumeByGuid(disks, volume.VolumeGuid);
  193. }
  194. // restore the filesystem boot sector
  195. byte[] filesystemBootRecord = relocatedExtent.Disk.ReadSector((long)resumeRecord.BootRecordBackupSector);
  196. volume.WriteSectors(0, filesystemBootRecord);
  197. ClearBackupData(relocatedExtent.Disk, resumeRecord);
  198. }
  199. private static void ClearBackupData(Disk relocatedExtentDisk, MoveExtentOperationBootRecord resumeRecord)
  200. {
  201. byte[] emptySector = new byte[relocatedExtentDisk.BytesPerSector];
  202. relocatedExtentDisk.WriteSectors((long)resumeRecord.BootRecordBackupSector, emptySector);
  203. if (resumeRecord.BackupBufferStartSector > 0)
  204. {
  205. byte[] emptyRegion = new byte[resumeRecord.BackupBufferSizeLBA * relocatedExtentDisk.BytesPerSector];
  206. relocatedExtentDisk.WriteSectors((long)resumeRecord.BackupBufferStartSector, emptyRegion);
  207. }
  208. }
  209. }
  210. }