Permalink
Browse files

* Implemented some basics of FileVDev.

Signed-off-by: cathode <cathode@live.com>
  • Loading branch information...
1 parent 27a5351 commit cedc45e75a99ef750386b052adfaa29cf1597ee0 @cathode committed Nov 6, 2011
Showing with 84 additions and 11 deletions.
  1. +43 −4 ManagedZFS/FileVDev.cs
  2. +18 −0 ManagedZFS/IBlockDevice.cs
  3. +1 −0 ManagedZFS/ManagedZFS.csproj
  4. +6 −1 ManagedZFS/RaidZVDev.cs
  5. +13 −6 ManagedZFS/VDev.cs
  6. +3 −0 zfs/ZFSProgram.cs
View
@@ -21,6 +21,8 @@ public class FileVDev : VDev
#region Fields
private FileStream fs;
private string path;
+ private int ashift;
+ private long sectorCount;
#endregion
#region Constructors
internal FileVDev(FileStream fs, string path)
@@ -34,7 +36,7 @@ public static FileVDev Open(string path)
{
Contract.Requires(!string.IsNullOrWhiteSpace(path));
- var fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
+ var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
var fvd = new FileVDev(fs, path);
// Check if the file is non-empty.
@@ -46,11 +48,48 @@ public static FileVDev Open(string path)
return fvd;
}
- #endregion
+ /// <summary>
+ /// Creates and initializes a new file-backed vdev.
+ /// </summary>
+ /// <param name="path"></param>
+ /// <param name="ashift"></param>
+ /// <param name="sectorCount"></param>
+ /// <returns></returns>
+ public static FileVDev Create(string path, int ashift, int sectorCount)
+ {
+ var fs = File.Open(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read);
+
+ var fvd = new FileVDev(fs, path);
+ fvd.ashift = ashift;
+ fvd.sectorCount = sectorCount;
- protected override void ReadSectors(byte[] buffer, long bufferIndex, long startSector, long sectorCount)
+ var sec = new byte[1 << ashift];
+
+ // Zero the file.
+ for (int i = 0; i < sectorCount; ++i)
+ fvd.WriteSector(sec, 0, i);
+ fs.Flush();
+
+ // Forces the labels to be built.
+ fvd.VerifyLabels();
+
+ return fvd;
+ }
+
+ protected override void ReadBlock(byte[] buffer, int bufferIndex, long sector)
{
- throw new NotImplementedException();
+ var addr = sector << this.ashift; // physical block address
+
+ this.fs.Seek(addr, SeekOrigin.Begin);
+ this.fs.Read(buffer, bufferIndex, (1 << ashift));
}
+ protected override void WriteSector(byte[] buffer, int bufferIndex, long sector)
+ {
+ var pba = sector << this.ashift;
+
+ this.fs.Seek(pba, SeekOrigin.Begin);
+ this.fs.Write(buffer, bufferIndex, (1 << ashift));
+ }
+ #endregion
}
}
View
@@ -0,0 +1,18 @@
+/******************************************************************************
+ * ManagedZFS - A fully-managed ZFS filesystem implementation. *
+ * Copyright © 2011 William 'cathode' Shelley. All Rights Reserved. *
+ * This software is released under the terms and conditions of the MIT/X11 *
+ * license; see the included 'license.txt' file for the full text. *
+ *****************************************************************************/
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace ManagedZFS
+{
+ public interface IBlockDevice
+ {
+ void ReadSector(byte[] buffer, int bufferIndex, long sector);
+ }
+}
@@ -42,6 +42,7 @@
<Compile Include="DiskVDev.cs" />
<Compile Include="DmuObjectType.cs" />
<Compile Include="FileVDev.cs" />
+ <Compile Include="IBlockDevice.cs" />
<Compile Include="IO\LzjbStream.cs" />
<Compile Include="IO\ZfsFileStream.cs" />
<Compile Include="PoolState.cs" />
View
@@ -13,7 +13,12 @@ namespace ManagedZFS
{
public class RaidZVDev : VDev
{
- protected override void ReadSectors(byte[] buffer, long bufferIndex, long startSector, long sectorCount)
+ protected override void ReadBlock(byte[] buffer, int bufferIndex, long sector)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override void WriteSector(byte[] buffer, int bufferIndex, long sector)
{
throw new NotImplementedException();
}
View
@@ -54,22 +54,29 @@ public VDevLabel[] ReadLabels()
return labels;
}
- protected virtual void ReadBytes(byte[] buffer, long bufferIndex, long offset, long count)
+ public bool VerifyLabels()
{
+ var labels = this.ReadLabels();
+
+
+ return false;
}
- protected virtual byte[] ReadSector(long sector)
+ protected virtual byte[] ReadBlock(long block)
{
var buffer = new byte[512];
- this.ReadSector(buffer, sector);
+ this.ReadBlock(buffer, 0, block);
return buffer;
}
- protected virtual void ReadSector(byte[] buffer, long sector)
- {
+ protected abstract void ReadBlock(byte[] buffer, int bufferIndex, long sector);
+ protected virtual void ReadBlocks(byte[] buffer, int bufferIndex, long startSector, long sectorCount)
+ {
+ throw new NotImplementedException();
}
- protected abstract void ReadSectors(byte[] buffer, long bufferIndex, long startSector, long sectorCount);
+
+ protected abstract void WriteSector(byte[] buffer, int bufferIndex, long sector);
#endregion
}
}
View
@@ -15,6 +15,9 @@ public class ZFSProgram : ZProgram
static void Main(string[] args)
{
Console.WriteLine("The ZFS Provider Service is not running.");
+
+ System.IO.File.Delete("test.vdev");
+ var fvd = FileVDev.Create("test.vdev", 9, 64 * 1024); // 32 MiB (64K sectors, 512B / sector)
}
}
}

0 comments on commit cedc45e

Please sign in to comment.