DynamicVolume.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 DiskAccessLibrary;
  12. using Utilities;
  13. namespace DiskAccessLibrary.LogicalDiskManager
  14. {
  15. public abstract class DynamicVolume : Volume
  16. {
  17. private Guid m_volumeGuid;
  18. private Guid m_diskGroupGuid;
  19. public ulong VolumeID;
  20. public string Name;
  21. public string DiskGroupName;
  22. private int? m_bytesPerSector;
  23. public DynamicVolume(Guid volumeGuid, Guid diskGroupGuid)
  24. {
  25. m_volumeGuid = volumeGuid;
  26. m_diskGroupGuid = diskGroupGuid;
  27. }
  28. public override bool Equals(object obj)
  29. {
  30. if (obj is DynamicVolume)
  31. {
  32. return ((DynamicVolume)obj).VolumeGuid == this.VolumeGuid;
  33. }
  34. return false;
  35. }
  36. public override int GetHashCode()
  37. {
  38. return this.VolumeGuid.GetHashCode();
  39. }
  40. /// <summary>
  41. /// "All disks holding extents for a given volume must have the same sector size"
  42. /// </summary>
  43. public override int BytesPerSector
  44. {
  45. get
  46. {
  47. if (!m_bytesPerSector.HasValue)
  48. {
  49. m_bytesPerSector = GetBytesPerSector(this.Columns, DynamicColumn.DefaultBytesPerSector);
  50. }
  51. return m_bytesPerSector.Value;
  52. }
  53. }
  54. public Guid VolumeGuid
  55. {
  56. get
  57. {
  58. return m_volumeGuid;
  59. }
  60. }
  61. public Guid DiskGroupGuid
  62. {
  63. get
  64. {
  65. return m_diskGroupGuid;
  66. }
  67. }
  68. public override List<DiskExtent> Extents
  69. {
  70. get
  71. {
  72. List<DiskExtent> result = new List<DiskExtent>();
  73. foreach (DynamicDiskExtent extent in this.DynamicExtents)
  74. {
  75. result.Add(extent);
  76. }
  77. return result;
  78. }
  79. }
  80. public abstract List<DynamicColumn> Columns
  81. {
  82. get;
  83. }
  84. public List<DynamicDiskExtent> DynamicExtents
  85. {
  86. get
  87. {
  88. List<DynamicDiskExtent> result = new List<DynamicDiskExtent>();
  89. foreach (DynamicColumn column in Columns)
  90. {
  91. result.AddRange(column.Extents);
  92. }
  93. return result;
  94. }
  95. }
  96. public virtual bool IsHealthy
  97. {
  98. get
  99. {
  100. foreach (DynamicColumn column in this.Columns)
  101. {
  102. if (!column.IsOperational)
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. }
  110. public virtual bool IsOperational
  111. {
  112. get
  113. {
  114. return IsHealthy;
  115. }
  116. }
  117. public static int GetBytesPerSector(List<DynamicColumn> columns, int defaultValue)
  118. {
  119. int? bytesPerSector = GetBytesPerSector(columns);
  120. return bytesPerSector.HasValue ? bytesPerSector.Value : defaultValue;
  121. }
  122. /// <summary>
  123. /// "All disks holding extents for a given volume must have the same sector size"
  124. /// </summary>
  125. public static int? GetBytesPerSector(List<DynamicColumn> columns)
  126. {
  127. foreach (DynamicColumn column in columns)
  128. {
  129. int? bytesPerSector = DynamicColumn.GetBytesPerSector(column.Extents);
  130. if (bytesPerSector.HasValue)
  131. {
  132. return bytesPerSector.Value;
  133. }
  134. }
  135. return null;
  136. }
  137. }
  138. }