Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / KernelLand / Modules / Storage / LVM / volumes.c
1 /*
2  * Acess2 Logical Volume Manager
3  * - By John Hodge (thePowersGang)
4  *
5  * volumes.c
6  * - Volume management
7  */
8 #define DEBUG   0
9 #include "lvm_int.h"
10 #define USE_IOCACHE     1
11
12 // === PROTOTYPES ===
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);
16
17 // === CODE ===
18 // --------------------------------------------------------------------
19 // Managment / Initialisation
20 // --------------------------------------------------------------------
21 int LVM_AddVolumeVFS(const char *Name, int FD)
22 {
23         // Assuming 512-byte blocks, not a good idea
24 //      return LVM_AddVolume(Name, (void*)(Uint)FD, 512, LVM_int_VFSReadEmul, LVM_int_VFSWriteEmul);
25         return 0;
26 }
27
28 void *LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount)
29 {
30         tLVM_Vol        dummy_vol;
31         tLVM_Format     *fmt;
32
33         if( BlockCount == 0 || BlockSize == 0 ) {
34                 Log_Error("LVM", "BlockSize(0x%x)/BlockCount(0x%x) invalid in LVM_AddVolume",
35                         BlockSize, BlockCount);
36                 return NULL;
37         }
38
39         dummy_vol.Type = Type;
40         dummy_vol.Ptr = Ptr;
41         dummy_vol.BlockCount = BlockCount;
42         dummy_vol.BlockSize = BlockSize;
43         dummy_vol.CacheHandle = NULL;
44
45         // Read the first block of the volume   
46         void *first_block = malloc(BlockSize);
47         if( !first_block ) {
48                 Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", BlockSize);
49                 return NULL;
50         }
51         if( Type->Read(Ptr, 0, 1, first_block) != 1 ) {
52                 Log_Error("LVM", "LVM_AddVolume - Failed to read first sector");
53                 free(first_block);
54                 return NULL;
55         }
56         
57         // Determine Format
58         // TODO: Determine format
59         fmt = &gLVM_MBRType;
60
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);
65         
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 );
70         if( !real_vol ) {
71                 Log_Error("LVM", "LVM_AddVolume - malloc error on %i bytes", allocsize);
72                 free(first_block);
73                 return NULL;
74         }
75         real_vol->Next = NULL;
76         real_vol->Type = Type;
77         real_vol->Ptr = Ptr;
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);
85         // - VFS Nodes
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;
95
96         // TODO: Better selection of cache size
97         // TODO: Allow a volume type to disallow caching
98         #if USE_IOCACHE
99         real_vol->CacheHandle = IOCache_Create(LVM_int_CacheWriteback, real_vol, BlockSize, 1024);
100         #else
101         real_vol->CacheHandle = NULL;
102         #endif
103
104         // Type->PopulateSubvolumes
105         fmt->PopulateSubvolumes(real_vol, first_block);
106         free(first_block);
107
108         // Add to volume list
109         gpLVM_LastVolume->Next = real_vol;
110         gpLVM_LastVolume = real_vol;
111
112         return real_vol;
113 }
114
115 void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, Uint64 BlockCount)
116 {
117         tLVM_SubVolume  *sv;
118          int    namelen;
119
120         if( Index < 0 || Index >= Volume->nSubVolumes ) {
121                 Log_Warning("LVM", "SV ID is out of range (0 < %i < %i)",
122                         Index, Volume->nSubVolumes);
123                 return ;
124         }
125
126         if( Volume->SubVolumes[Index] ) {
127                 Log_Warning("LVM", "Attempt to set SV %i of %p twice", Index, Volume);
128                 return ;
129         }
130         
131         namelen = snprintf(NULL, 0, "%i", Index);
132
133         sv = malloc( sizeof(tLVM_SubVolume) + namelen + 1 );
134         if(!sv) {
135                 // Oh, f*ck
136                 return ;
137         }
138         Volume->SubVolumes[Index] = sv; 
139
140         sv->Vol = Volume;
141         sprintf(sv->Name, "%i", Index);
142         sv->FirstBlock = FirstBlock;
143         sv->BlockCount = BlockCount;
144         memset(&sv->Node, 0, sizeof(tVFS_Node));
145         
146         sv->Node.ImplPtr = sv;
147         sv->Node.Type = &gLVM_SubVolNodeType;
148         sv->Node.Size = BlockCount * Volume->BlockSize;
149         
150         Log_Log("LVM", "Partition %s/%s - 0x%llx+0x%llx blocks",
151                 Volume->Name, sv->Name, FirstBlock, BlockCount);
152 }
153
154 // --------------------------------------------------------------------
155 // IO
156 // --------------------------------------------------------------------
157 int LVM_int_CacheWriteback(void *ID, Uint64 Sector, const void *Buffer)
158 {
159         tLVM_Vol *Volume = ID;
160         return Volume->Type->Write(Volume->Ptr, Sector, 1, Buffer);
161 }
162
163 size_t LVM_int_ReadVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, void *Dest)
164 {
165         #if USE_IOCACHE
166         if( Volume->CacheHandle )
167         {
168                  int    done = 0;
169                 while( done < BlockCount )
170                 {
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;
180                         if( count ) {
181                                 Volume->Type->Read(Volume->Ptr, BlockNum+first_uncached, count, uncache_buf);
182                                 while(count--)
183                                 {
184                                         IOCache_Add(Volume->CacheHandle, BlockNum+first_uncached, uncache_buf);
185                                         first_uncached ++;
186                                         uncache_buf = (char*)uncache_buf + Volume->BlockSize;
187                                 }
188                         }
189                 }
190                 return done;
191         }
192         else
193         #endif
194                 return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
195 }
196
197 size_t LVM_int_WriteVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, const void *Src)
198 {
199         #if USE_IOCACHE
200         if( Volume->CacheHandle )
201         {
202                 int done = 0;
203                 while( BlockCount )
204                 {
205                         IOCache_Write(Volume->CacheHandle, BlockNum, Src);
206                         Src = (const char*)Src + Volume->BlockSize;
207                         BlockNum ++;
208                         BlockCount --;
209                         done ++;
210                 }
211                 return done;
212         }
213         else
214         #endif
215                 return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);
216 }
217
218 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest)
219 {
220         size_t  blocksize;
221         size_t  rv;
222
223         blocksize = 512;        // TODO: Don't assume   
224
225         rv = VFS_ReadAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Dest );
226         rv /= blocksize;
227         return rv;
228 }
229
230 int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source)
231 {
232         size_t  blocksize;
233         size_t  rv;
234
235         blocksize = 512;        // TODO: Don't assume   
236
237         rv = VFS_WriteAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Source );
238         rv /= blocksize;
239         return rv;
240 }
241

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