ComponentRecord.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Text;
  10. using Utilities;
  11. namespace DiskAccessLibrary.LogicalDiskManager
  12. {
  13. public enum ComponentFlags : uint
  14. {
  15. Recover = 0x02,
  16. IOFail = 0x04,
  17. Pending = 0x08,
  18. }
  19. // a.k.a. Plex
  20. public class ComponentRecord : DatabaseRecord
  21. {
  22. public string StateString = "ACTIVE";
  23. public ExtentLayoutName ExtentLayout;
  24. public ComponentFlags ComponentFlags;
  25. public uint NumberOfExtents; // number of extents
  26. public ulong CommitTransactionID;
  27. // 8 zeros
  28. public ulong VolumeId;
  29. public ulong LogSD; // DMDiag reports this as 'logging: logsd=x'
  30. public ulong StripeSizeLBA; // PaddedVarULong
  31. public uint NumberOfColumns; // DMDiag will read this value as (signed) Int32
  32. public ComponentRecord()
  33. {
  34. this.RecordRevision = 3;
  35. this.RecordType = RecordType.Component;
  36. }
  37. public ComponentRecord(List<DatabaseRecordFragment> fragments) : base(fragments)
  38. {
  39. // Data begins at 0x10 (VBLK header is at 0x00)
  40. int offset = 0x00; // relative to Data
  41. ReadCommonFields(this.Data, ref offset);
  42. if (RecordRevision != 3)
  43. {
  44. throw new NotImplementedException("Unsupported record revision");
  45. }
  46. StateString = ReadVarString(this.Data, ref offset);
  47. ExtentLayout = (ExtentLayoutName)ByteReader.ReadByte(this.Data, ref offset);
  48. ComponentFlags = (ComponentFlags)BigEndianReader.ReadUInt32(this.Data, ref offset);
  49. NumberOfExtents = ReadVarUInt(this.Data, ref offset);
  50. CommitTransactionID = BigEndianReader.ReadUInt64(this.Data, ref offset);
  51. offset += 8;
  52. VolumeId = ReadVarULong(this.Data, ref offset);
  53. LogSD = ReadVarULong(this.Data, ref offset);
  54. if (HasStripedExtentsFlag)
  55. {
  56. StripeSizeLBA = ReadVarULong(this.Data, ref offset);
  57. NumberOfColumns = ReadVarUInt(this.Data, ref offset);
  58. }
  59. }
  60. public override byte[] GetDataBytes()
  61. {
  62. int dataLength = 29; // fixed length components
  63. dataLength += VarULongSize(ComponentId);
  64. dataLength += Name.Length + 1;
  65. dataLength += StateString.Length + 1;
  66. dataLength += VarUIntSize(NumberOfExtents);
  67. dataLength += VarULongSize(VolumeId);
  68. dataLength += VarULongSize(LogSD);
  69. if (HasStripedExtentsFlag)
  70. {
  71. dataLength += PaddedVarULongSize(StripeSizeLBA);
  72. dataLength += VarUIntSize(NumberOfColumns);
  73. }
  74. byte[] data = new byte[dataLength];
  75. int offset = 0x00;
  76. WriteCommonFields(data, ref offset);
  77. WriteVarString(data, ref offset, StateString);
  78. ByteWriter.WriteByte(data, ref offset, (byte)ExtentLayout);
  79. BigEndianWriter.WriteUInt32(data, ref offset, (uint)ComponentFlags);
  80. WriteVarUInt(data, ref offset, NumberOfExtents);
  81. BigEndianWriter.WriteUInt64(data, ref offset, CommitTransactionID);
  82. offset += 8;
  83. WriteVarULong(data, ref offset, VolumeId);
  84. WriteVarULong(data, ref offset, LogSD);
  85. if (HasStripedExtentsFlag)
  86. {
  87. WritePaddedVarULong(data, ref offset, StripeSizeLBA);
  88. WriteVarUInt(data, ref offset, NumberOfColumns);
  89. }
  90. return data;
  91. }
  92. public ulong ComponentId
  93. {
  94. get
  95. {
  96. return this.Id;
  97. }
  98. }
  99. public bool HasStripedExtentsFlag
  100. {
  101. get
  102. {
  103. return ((Flags & 0x10) != 0);
  104. }
  105. set
  106. {
  107. if (value)
  108. {
  109. this.Flags = 0x10;
  110. }
  111. else
  112. {
  113. this.Flags = 0;
  114. }
  115. }
  116. }
  117. }
  118. }