Modules/LVM - Log basics of supervolumes
[tpg/acess2.git] / KernelLand / Modules / Storage / LVM / volumes.c
index fdac4fd..1768267 100644 (file)
@@ -5,10 +5,12 @@
  * volumes.c
  * - Volume management
  */
-#define DEBUG  1
+#define DEBUG  0
 #include "lvm_int.h"
+#define USE_IOCACHE    1
 
 // === PROTOTYPES ===
+ int   LVM_int_CacheWriteback(void *ID, Uint64 Sector, const void *Buffer);
  int   LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest);
  int   LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source);
 
@@ -23,21 +25,34 @@ int LVM_AddVolumeVFS(const char *Name, int FD)
        return 0;
 }
 
-int LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount)
+void *LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount)
 {
        tLVM_Vol        dummy_vol;
-       tLVM_Vol        *real_vol;
        tLVM_Format     *fmt;
-       void    *first_block;
+
+       if( BlockCount == 0 || BlockSize == 0 ) {
+               Log_Error("LVM", "BlockSize(0x%x)/BlockCount(0x%x) invalid in LVM_AddVolume",
+                       BlockSize, BlockCount);
+               return NULL;
+       }
 
        dummy_vol.Type = Type;
        dummy_vol.Ptr = Ptr;
        dummy_vol.BlockCount = BlockCount;
        dummy_vol.BlockSize = BlockSize;
+       dummy_vol.CacheHandle = NULL;
 
        // Read the first block of the volume   
-       first_block = malloc(BlockSize);
-       Type->Read(Ptr, 0, 1, first_block);
+       void *first_block = malloc(BlockSize);
+       if( !first_block ) {
+               Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", BlockSize);
+               return NULL;
+       }
+       if( Type->Read(Ptr, 0, 1, first_block) != 1 ) {
+               Log_Error("LVM", "LVM_AddVolume - Failed to read first sector");
+               free(first_block);
+               return NULL;
+       }
        
        // Determine Format
        // TODO: Determine format
@@ -45,15 +60,26 @@ int LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t
 
        // Type->CountSubvolumes
        dummy_vol.nSubVolumes = fmt->CountSubvolumes(&dummy_vol, first_block);
+       Log_Debug("LVM", "Volume %s as format %s gives %i sub-volumes",
+               Name, fmt->Name, dummy_vol.nSubVolumes);
        
        // Create real volume descriptor
        // TODO: If this needs to be rescanned later, having the subvolume list separate might be an idea
-       real_vol = malloc( sizeof(tLVM_Vol) + strlen(Name) + 1 + sizeof(tLVM_SubVolume*) * dummy_vol.nSubVolumes );
+       size_t  allocsize = sizeof(tLVM_Vol) + strlen(Name) + 1 + sizeof(tLVM_SubVolume*) * dummy_vol.nSubVolumes;
+       tLVM_Vol        *real_vol = malloc( allocsize );
+       if( !real_vol ) {
+               Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", allocsize);
+               free(first_block);
+               return NULL;
+       }
+       real_vol->Next = NULL;
        real_vol->Type = Type;
        real_vol->Ptr = Ptr;
+       real_vol->BlockSize = BlockSize;
        real_vol->BlockCount = BlockCount;
        real_vol->nSubVolumes = dummy_vol.nSubVolumes;
        real_vol->SubVolumes = (void*)( real_vol->Name + strlen(Name) + 1 );
+       real_vol->BlockSize = BlockSize;
        strcpy(real_vol->Name, Name);
        memset(real_vol->SubVolumes, 0, sizeof(tLVM_SubVolume*) * real_vol->nSubVolumes);
        // - VFS Nodes
@@ -67,6 +93,14 @@ int LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t
        real_vol->VolNode.ImplPtr = real_vol;
        real_vol->VolNode.Size = BlockCount * BlockSize;
 
+       // TODO: Better selection of cache size
+       // TODO: Allow a volume type to disallow caching
+       #if USE_IOCACHE
+       real_vol->CacheHandle = IOCache_Create(LVM_int_CacheWriteback, real_vol, BlockSize, 1024);
+       #else
+       real_vol->CacheHandle = NULL;
+       #endif
+
        // Type->PopulateSubvolumes
        fmt->PopulateSubvolumes(real_vol, first_block);
        free(first_block);
@@ -75,7 +109,7 @@ int LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t
        gpLVM_LastVolume->Next = real_vol;
        gpLVM_LastVolume = real_vol;
 
-       return 0;
+       return real_vol;
 }
 
 void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, Uint64 BlockCount)
@@ -120,14 +154,65 @@ void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, U
 // --------------------------------------------------------------------
 // IO
 // --------------------------------------------------------------------
+int LVM_int_CacheWriteback(void *ID, Uint64 Sector, const void *Buffer)
+{
+       tLVM_Vol *Volume = ID;
+       return Volume->Type->Write(Volume->Ptr, Sector, 1, Buffer);
+}
+
 size_t LVM_int_ReadVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, void *Dest)
 {
-       return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
+       #if USE_IOCACHE
+       if( Volume->CacheHandle )
+       {
+                int    done = 0;
+               while( done < BlockCount )
+               {
+                       while( done < BlockCount && IOCache_Read(Volume->CacheHandle, BlockNum+done, Dest) == 1 )
+                               done ++, Dest = (char*)Dest + Volume->BlockSize;
+                       size_t first_uncached = done;
+                       void *uncache_buf = Dest;
+                       LOG("%i/%i: cached", done, BlockCount);
+                       while( done < BlockCount && IOCache_Read(Volume->CacheHandle, BlockNum+done, Dest) == 0 )
+                               done ++, Dest = (char*)Dest + Volume->BlockSize;
+                       LOG("%i/%i: uncached", done, BlockCount);
+                       size_t  count = done-first_uncached;
+                       if( count ) {
+                               Volume->Type->Read(Volume->Ptr, BlockNum+first_uncached, count, uncache_buf);
+                               while(count--)
+                               {
+                                       IOCache_Add(Volume->CacheHandle, BlockNum+first_uncached, uncache_buf);
+                                       first_uncached ++;
+                                       uncache_buf = (char*)uncache_buf + Volume->BlockSize;
+                               }
+                       }
+               }
+               return done;
+       }
+       else
+       #endif
+               return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
 }
 
 size_t LVM_int_WriteVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, const void *Src)
 {
-       return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);     
+       #if USE_IOCACHE
+       if( Volume->CacheHandle )
+       {
+               int done = 0;
+               while( BlockCount )
+               {
+                       IOCache_Write(Volume->CacheHandle, BlockNum, Src);
+                       Src = (const char*)Src + Volume->BlockSize;
+                       BlockNum ++;
+                       BlockCount --;
+                       done ++;
+               }
+               return done;
+       }
+       else
+       #endif
+               return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);
 }
 
 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest)

UCC git Repository :: git.ucc.asn.au