X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FModules%2FStorage%2FLVM%2Fvolumes.c;h=d923d88fc48fae24f0d77c7838b6b34adacda1a6;hb=e7a76b0d8a0cc6aa77966509780973a6f8216ef7;hp=aac987e283684933a4fe4e5d981436f6b378d3c3;hpb=1d65dd9606edfcaeef8dfff3724ef6c49180b131;p=tpg%2Facess2.git diff --git a/KernelLand/Modules/Storage/LVM/volumes.c b/KernelLand/Modules/Storage/LVM/volumes.c index aac987e2..d923d88f 100644 --- a/KernelLand/Modules/Storage/LVM/volumes.c +++ b/KernelLand/Modules/Storage/LVM/volumes.c @@ -5,29 +5,109 @@ * volumes.c * - Volume management */ +#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); // === CODE === // -------------------------------------------------------------------- // Managment / Initialisation // -------------------------------------------------------------------- -int LVM_AddVolume(const char *Name, int FD) +int LVM_AddVolumeVFS(const char *Name, int FD) { - // Make dummy volume descriptor (for the read code) + // Assuming 512-byte blocks, not a good idea +// return LVM_AddVolume(Name, (void*)(Uint)FD, 512, LVM_int_VFSReadEmul, LVM_int_VFSWriteEmul); + return 0; +} + +void *LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount) +{ + tLVM_Vol dummy_vol; + tLVM_Format *fmt; - // Determine Type + 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 + 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 + fmt = &gLVM_MBRType; // Type->CountSubvolumes + dummy_vol.nSubVolumes = fmt->CountSubvolumes(&dummy_vol, first_block); // Create real volume descriptor + // TODO: If this needs to be rescanned later, having the subvolume list separate might be an idea + 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 + memset(&real_vol->DirNode, 0, sizeof(tVFS_Node)); + real_vol->DirNode.Type = &gLVM_VolNodeType; + real_vol->DirNode.ImplPtr = real_vol; + real_vol->DirNode.Flags = VFS_FFLAG_DIRECTORY; + real_vol->DirNode.Size = -1; + memset(&real_vol->VolNode, 0, sizeof(tVFS_Node)); + real_vol->VolNode.Type = &gLVM_VolNodeType; + 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); // Add to volume list + 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) @@ -63,32 +143,97 @@ void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, U sv->Node.ImplPtr = sv; sv->Node.Type = &gLVM_SubVolNodeType; + sv->Node.Size = BlockCount * Volume->BlockSize; + + Log_Log("LVM", "Partition %s/%s - 0x%llx+0x%llx blocks", + Volume->Name, sv->Name, FirstBlock, BlockCount); } // -------------------------------------------------------------------- // 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) { - size_t rv; - rv = VFS_ReadAt( - Volume->BackingDescriptor, - BlockNum * Volume->BlockSize, - BlockCount * Volume->BlockSize, - Dest - ); - return rv / Volume->BlockSize; + #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) { + #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) +{ + size_t blocksize; size_t rv; - rv = VFS_WriteAt( - Volume->BackingDescriptor, - BlockNum * Volume->BlockSize, - BlockCount * Volume->BlockSize, - Src - ); - return rv / Volume->BlockSize; + + blocksize = 512; // TODO: Don't assume + + rv = VFS_ReadAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Dest ); + rv /= blocksize; + return rv; +} + +int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source) +{ + size_t blocksize; + size_t rv; + + blocksize = 512; // TODO: Don't assume + + rv = VFS_WriteAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Source ); + rv /= blocksize; + return rv; }