2 * Acess2 Logical Volume Manager
3 * - By John Hodge (thePowersGang)
13 int LVM_int_CacheWriteback(void *ID, Uint64 Sector, const void *Buffer);
14 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest);
15 int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source);
18 // --------------------------------------------------------------------
19 // Managment / Initialisation
20 // --------------------------------------------------------------------
21 int LVM_AddVolumeVFS(const char *Name, int FD)
23 // Assuming 512-byte blocks, not a good idea
24 // return LVM_AddVolume(Name, (void*)(Uint)FD, 512, LVM_int_VFSReadEmul, LVM_int_VFSWriteEmul);
28 void *LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount)
33 if( BlockCount == 0 || BlockSize == 0 ) {
34 Log_Error("LVM", "BlockSize(0x%x)/BlockCount(0x%x) invalid in LVM_AddVolume",
35 BlockSize, BlockCount);
39 dummy_vol.Type = Type;
41 dummy_vol.BlockCount = BlockCount;
42 dummy_vol.BlockSize = BlockSize;
43 dummy_vol.CacheHandle = NULL;
45 // Read the first block of the volume
46 void *first_block = malloc(BlockSize);
48 Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", BlockSize);
51 if( Type->Read(Ptr, 0, 1, first_block) != 1 ) {
52 Log_Error("LVM", "LVM_AddVolume - Failed to read first sector");
58 // TODO: Determine format
61 // Type->CountSubvolumes
62 dummy_vol.nSubVolumes = fmt->CountSubvolumes(&dummy_vol, first_block);
63 Log_Debug("LVM", "Volume %s as format %s gives %i sub-volumes",
64 Name, fmt->Name, dummy_vol.nSubVolumes);
66 // Create real volume descriptor
67 // TODO: If this needs to be rescanned later, having the subvolume list separate might be an idea
68 size_t allocsize = sizeof(tLVM_Vol) + strlen(Name) + 1 + sizeof(tLVM_SubVolume*) * dummy_vol.nSubVolumes;
69 tLVM_Vol *real_vol = malloc( allocsize );
71 Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", allocsize);
75 real_vol->Next = NULL;
76 real_vol->Type = Type;
78 real_vol->BlockSize = BlockSize;
79 real_vol->BlockCount = BlockCount;
80 real_vol->nSubVolumes = dummy_vol.nSubVolumes;
81 real_vol->SubVolumes = (void*)( real_vol->Name + strlen(Name) + 1 );
82 real_vol->BlockSize = BlockSize;
83 strcpy(real_vol->Name, Name);
84 memset(real_vol->SubVolumes, 0, sizeof(tLVM_SubVolume*) * real_vol->nSubVolumes);
86 memset(&real_vol->DirNode, 0, sizeof(tVFS_Node));
87 real_vol->DirNode.Type = &gLVM_VolNodeType;
88 real_vol->DirNode.ImplPtr = real_vol;
89 real_vol->DirNode.Flags = VFS_FFLAG_DIRECTORY;
90 real_vol->DirNode.Size = -1;
91 memset(&real_vol->VolNode, 0, sizeof(tVFS_Node));
92 real_vol->VolNode.Type = &gLVM_VolNodeType;
93 real_vol->VolNode.ImplPtr = real_vol;
94 real_vol->VolNode.Size = BlockCount * BlockSize;
96 // TODO: Better selection of cache size
97 // TODO: Allow a volume type to disallow caching
99 real_vol->CacheHandle = IOCache_Create(LVM_int_CacheWriteback, real_vol, BlockSize, 1024);
101 real_vol->CacheHandle = NULL;
104 // Type->PopulateSubvolumes
105 fmt->PopulateSubvolumes(real_vol, first_block);
108 // Add to volume list
109 gpLVM_LastVolume->Next = real_vol;
110 gpLVM_LastVolume = real_vol;
115 void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, Uint64 BlockCount)
120 if( Index < 0 || Index >= Volume->nSubVolumes ) {
121 Log_Warning("LVM", "SV ID is out of range (0 < %i < %i)",
122 Index, Volume->nSubVolumes);
126 if( Volume->SubVolumes[Index] ) {
127 Log_Warning("LVM", "Attempt to set SV %i of %p twice", Index, Volume);
131 namelen = snprintf(NULL, 0, "%i", Index);
133 sv = malloc( sizeof(tLVM_SubVolume) + namelen + 1 );
138 Volume->SubVolumes[Index] = sv;
141 sprintf(sv->Name, "%i", Index);
142 sv->FirstBlock = FirstBlock;
143 sv->BlockCount = BlockCount;
144 memset(&sv->Node, 0, sizeof(tVFS_Node));
146 sv->Node.ImplPtr = sv;
147 sv->Node.Type = &gLVM_SubVolNodeType;
148 sv->Node.Size = BlockCount * Volume->BlockSize;
150 Log_Log("LVM", "Partition %s/%s - 0x%llx+0x%llx blocks",
151 Volume->Name, sv->Name, FirstBlock, BlockCount);
154 // --------------------------------------------------------------------
156 // --------------------------------------------------------------------
157 int LVM_int_CacheWriteback(void *ID, Uint64 Sector, const void *Buffer)
159 tLVM_Vol *Volume = ID;
160 return Volume->Type->Write(Volume->Ptr, Sector, 1, Buffer);
163 size_t LVM_int_ReadVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, void *Dest)
166 if( Volume->CacheHandle )
169 while( done < BlockCount )
171 while( done < BlockCount && IOCache_Read(Volume->CacheHandle, BlockNum+done, Dest) == 1 )
172 done ++, Dest = (char*)Dest + Volume->BlockSize;
173 size_t first_uncached = done;
174 void *uncache_buf = Dest;
175 LOG("%i/%i: cached", done, BlockCount);
176 while( done < BlockCount && IOCache_Read(Volume->CacheHandle, BlockNum+done, Dest) == 0 )
177 done ++, Dest = (char*)Dest + Volume->BlockSize;
178 LOG("%i/%i: uncached", done, BlockCount);
179 size_t count = done-first_uncached;
181 Volume->Type->Read(Volume->Ptr, BlockNum+first_uncached, count, uncache_buf);
184 IOCache_Add(Volume->CacheHandle, BlockNum+first_uncached, uncache_buf);
186 uncache_buf = (char*)uncache_buf + Volume->BlockSize;
194 return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
197 size_t LVM_int_WriteVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, const void *Src)
200 if( Volume->CacheHandle )
205 IOCache_Write(Volume->CacheHandle, BlockNum, Src);
206 Src = (const char*)Src + Volume->BlockSize;
215 return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);
218 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest)
223 blocksize = 512; // TODO: Don't assume
225 rv = VFS_ReadAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Dest );
230 int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source)
235 blocksize = 512; // TODO: Don't assume
237 rv = VFS_WriteAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Source );