DynamicDiskHelper.Extents.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 2014-2016 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. namespace DiskAccessLibrary.LogicalDiskManager
  13. {
  14. public partial class DynamicDiskHelper
  15. {
  16. public static List<DiskExtent> GetUnallocatedExtents(DynamicDisk disk)
  17. {
  18. List<DynamicDiskExtent> extents = GetDiskExtents(disk);
  19. // extents are sorted by first sector
  20. if (extents == null)
  21. {
  22. return null;
  23. }
  24. List<DiskExtent> result = new List<DiskExtent>();
  25. PrivateHeader privateHeader = disk.PrivateHeader;
  26. long publicRegionStartSector = (long)privateHeader.PublicRegionStartLBA;
  27. long startSector = publicRegionStartSector;
  28. long publicRegionSize = (long)privateHeader.PublicRegionSizeLBA * disk.Disk.BytesPerSector;
  29. // see if there is room before each extent
  30. foreach (DynamicDiskExtent extent in extents)
  31. {
  32. long extentStartSector = extent.FirstSector;
  33. long nextStartSector = extent.FirstSector + extent.Size / disk.BytesPerSector;
  34. long freeSpaceInBytes = (extentStartSector - startSector) * disk.BytesPerSector;
  35. if (freeSpaceInBytes > 0)
  36. {
  37. result.Add(new DiskExtent(disk.Disk, startSector, freeSpaceInBytes));
  38. }
  39. startSector = nextStartSector;
  40. }
  41. // see if there is room after the last extent
  42. long spaceInBytes = publicRegionSize - (startSector - publicRegionStartSector) * disk.Disk.BytesPerSector;
  43. if (spaceInBytes > 0)
  44. {
  45. result.Add(new DiskExtent(disk.Disk, startSector, spaceInBytes));
  46. }
  47. return result;
  48. }
  49. /// <summary>
  50. /// Sorted by first sector
  51. /// </summary>
  52. /// <returns>null if there was a problem reading extent information from disk</returns>
  53. public static List<DynamicDiskExtent> GetDiskExtents(DynamicDisk disk)
  54. {
  55. List<DynamicDiskExtent> result = new List<DynamicDiskExtent>();
  56. PrivateHeader privateHeader = disk.PrivateHeader;
  57. if (privateHeader != null)
  58. {
  59. VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(disk);
  60. if (database != null)
  61. {
  62. DiskRecord diskRecord = database.FindDiskByDiskGuid(privateHeader.DiskGuid);
  63. List<ExtentRecord> extentRecords = database.FindExtentsByDiskID(diskRecord.DiskId);
  64. foreach (ExtentRecord extentRecord in extentRecords)
  65. {
  66. DynamicDiskExtent extent = DynamicDiskExtentHelper.GetDiskExtent(disk, extentRecord);
  67. result.Add(extent);
  68. }
  69. DynamicDiskExtentsHelper.SortExtentsByFirstSector(result);
  70. return result;
  71. }
  72. }
  73. return null;
  74. }
  75. public static long GetMaxNewExtentLength(DynamicDisk disk)
  76. {
  77. return GetMaxNewExtentLength(disk, 0);
  78. }
  79. /// <returns>In bytes</returns>
  80. public static long GetMaxNewExtentLength(DynamicDisk disk, long alignInSectors)
  81. {
  82. List<DiskExtent> unallocatedExtents = GetUnallocatedExtents(disk);
  83. if (unallocatedExtents == null)
  84. {
  85. return -1;
  86. }
  87. long result = 0;
  88. for (int index = 0; index < unallocatedExtents.Count; index++)
  89. {
  90. DiskExtent extent = unallocatedExtents[index];
  91. if (alignInSectors > 1)
  92. {
  93. extent = DiskExtentHelper.GetAlignedDiskExtent(extent, alignInSectors);
  94. }
  95. if (extent.Size > result)
  96. {
  97. result = extent.Size;
  98. }
  99. }
  100. return result;
  101. }
  102. public static DiskExtent FindExtentAllocation(DynamicDisk disk, long allocationLength)
  103. {
  104. return FindExtentAllocation(disk, allocationLength, 0);
  105. }
  106. /// <param name="allocationLength">In bytes</param>
  107. /// <param name="alignInSectors">0 or 1 for no alignment</param>
  108. /// <returns>Allocated DiskExtent or null if there is not enough free disk space</returns>
  109. public static DiskExtent FindExtentAllocation(DynamicDisk disk, long allocationLength, long alignInSectors)
  110. {
  111. List<DiskExtent> unallocatedExtents = DynamicDiskHelper.GetUnallocatedExtents(disk);
  112. if (unallocatedExtents == null)
  113. {
  114. return null;
  115. }
  116. for (int index = 0; index < unallocatedExtents.Count; index++)
  117. {
  118. DiskExtent extent = unallocatedExtents[index];
  119. if (alignInSectors > 1)
  120. {
  121. extent = DiskExtentHelper.GetAlignedDiskExtent(extent, alignInSectors);
  122. }
  123. if (extent.Size >= allocationLength)
  124. {
  125. return new DiskExtent(extent.Disk, extent.FirstSector, allocationLength);
  126. }
  127. }
  128. return null;
  129. }
  130. /// <param name="targetOffset">in bytes</param>
  131. public static bool IsMoveLocationValid(DynamicDisk disk, DynamicDiskExtent sourceExtent, long targetOffset)
  132. {
  133. List<DynamicDiskExtent> extents = GetDiskExtents(disk);
  134. // extents are sorted by first sector
  135. if (extents == null)
  136. {
  137. return false;
  138. }
  139. PrivateHeader privateHeader = disk.PrivateHeader;
  140. if (targetOffset % privateHeader.BytesPerSector > 0)
  141. {
  142. return false;
  143. }
  144. long targetSector = targetOffset / disk.BytesPerSector;
  145. DiskExtent targetExtent = new DiskExtent(disk.Disk, targetSector, sourceExtent.Size);
  146. List<DiskExtent> usedExtents = new List<DiskExtent>();
  147. foreach (DynamicDiskExtent usedExtent in usedExtents)
  148. {
  149. if (usedExtent.FirstSector != sourceExtent.FirstSector)
  150. {
  151. usedExtents.Add(usedExtent);
  152. }
  153. }
  154. long publicRegionStartSector = (long)privateHeader.PublicRegionStartLBA;
  155. long publicRegionSize = (long)privateHeader.PublicRegionSizeLBA * disk.BytesPerSector;
  156. List<DiskExtent> unallocatedExtents = DiskExtentsHelper.GetUnallocatedExtents(disk.Disk, publicRegionStartSector, publicRegionSize, usedExtents);
  157. foreach (DiskExtent extent in unallocatedExtents)
  158. {
  159. if (extent.FirstSector <= targetExtent.FirstSector && targetExtent.LastSector <= extent.LastSector)
  160. {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. }
  167. }