Modules/RAMDisk - a bit more work
[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   1
9 #include "lvm_int.h"
10
11 // === PROTOTYPES ===
12  int    LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest);
13  int    LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source);
14
15 // === CODE ===
16 // --------------------------------------------------------------------
17 // Managment / Initialisation
18 // --------------------------------------------------------------------
19 int LVM_AddVolumeVFS(const char *Name, int FD)
20 {
21         // Assuming 512-byte blocks, not a good idea
22 //      return LVM_AddVolume(Name, (void*)(Uint)FD, 512, LVM_int_VFSReadEmul, LVM_int_VFSWriteEmul);
23         return 0;
24 }
25
26 int LVM_AddVolume(const tLVM_VolType *Type, const char *Name, void *Ptr, size_t BlockSize, size_t BlockCount)
27 {
28         tLVM_Vol        dummy_vol;
29         tLVM_Vol        *real_vol;
30         tLVM_Format     *fmt;
31         void    *first_block;
32
33         dummy_vol.Type = Type;
34         dummy_vol.Ptr = Ptr;
35         dummy_vol.BlockCount = BlockCount;
36         dummy_vol.BlockSize = BlockSize;
37
38         // Read the first block of the volume   
39         first_block = malloc(BlockSize);
40         Type->Read(Ptr, 0, 1, first_block);
41         
42         // Determine Format
43         // TODO: Determine format
44         fmt = &gLVM_MBRType;
45
46         // Type->CountSubvolumes
47         dummy_vol.nSubVolumes = fmt->CountSubvolumes(&dummy_vol, first_block);
48         
49         // Create real volume descriptor
50         // TODO: If this needs to be rescanned later, having the subvolume list separate might be an idea
51         real_vol = malloc( sizeof(tLVM_Vol) + strlen(Name) + 1 + sizeof(tLVM_SubVolume*) * dummy_vol.nSubVolumes );
52         real_vol->Next = NULL;
53         real_vol->Type = Type;
54         real_vol->Ptr = Ptr;
55         real_vol->BlockCount = BlockCount;
56         real_vol->nSubVolumes = dummy_vol.nSubVolumes;
57         real_vol->SubVolumes = (void*)( real_vol->Name + strlen(Name) + 1 );
58         strcpy(real_vol->Name, Name);
59         memset(real_vol->SubVolumes, 0, sizeof(tLVM_SubVolume*) * real_vol->nSubVolumes);
60         // - VFS Nodes
61         memset(&real_vol->DirNode, 0, sizeof(tVFS_Node));
62         real_vol->DirNode.Type = &gLVM_VolNodeType;
63         real_vol->DirNode.ImplPtr = real_vol;
64         real_vol->DirNode.Flags = VFS_FFLAG_DIRECTORY;
65         real_vol->DirNode.Size = -1;
66         memset(&real_vol->VolNode, 0, sizeof(tVFS_Node));
67         real_vol->VolNode.Type = &gLVM_VolNodeType;
68         real_vol->VolNode.ImplPtr = real_vol;
69         real_vol->VolNode.Size = BlockCount * BlockSize;
70
71         // Type->PopulateSubvolumes
72         fmt->PopulateSubvolumes(real_vol, first_block);
73         free(first_block);
74
75         // Add to volume list
76         gpLVM_LastVolume->Next = real_vol;
77         gpLVM_LastVolume = real_vol;
78
79         return 0;
80 }
81
82 void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, Uint64 BlockCount)
83 {
84         tLVM_SubVolume  *sv;
85          int    namelen;
86
87         if( Index < 0 || Index >= Volume->nSubVolumes ) {
88                 Log_Warning("LVM", "SV ID is out of range (0 < %i < %i)",
89                         Index, Volume->nSubVolumes);
90                 return ;
91         }
92
93         if( Volume->SubVolumes[Index] ) {
94                 Log_Warning("LVM", "Attempt to set SV %i of %p twice", Index, Volume);
95                 return ;
96         }
97         
98         namelen = snprintf(NULL, 0, "%i", Index);
99
100         sv = malloc( sizeof(tLVM_SubVolume) + namelen + 1 );
101         if(!sv) {
102                 // Oh, f*ck
103                 return ;
104         }
105         Volume->SubVolumes[Index] = sv; 
106
107         sv->Vol = Volume;
108         sprintf(sv->Name, "%i", Index);
109         sv->FirstBlock = FirstBlock;
110         sv->BlockCount = BlockCount;
111         memset(&sv->Node, 0, sizeof(tVFS_Node));
112         
113         sv->Node.ImplPtr = sv;
114         sv->Node.Type = &gLVM_SubVolNodeType;
115         sv->Node.Size = BlockCount * Volume->BlockSize;
116         
117         Log_Log("LVM", "Partition %s/%s - 0x%llx+0x%llx blocks",
118                 Volume->Name, sv->Name, FirstBlock, BlockCount);
119 }
120
121 // --------------------------------------------------------------------
122 // IO
123 // --------------------------------------------------------------------
124 size_t LVM_int_ReadVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, void *Dest)
125 {
126         return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
127 }
128
129 size_t LVM_int_WriteVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, const void *Src)
130 {
131         return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);     
132 }
133
134 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest)
135 {
136         size_t  blocksize;
137         size_t  rv;
138
139         blocksize = 512;        // TODO: Don't assume   
140
141         rv = VFS_ReadAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Dest );
142         rv /= blocksize;
143         return rv;
144 }
145
146 int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source)
147 {
148         size_t  blocksize;
149         size_t  rv;
150
151         blocksize = 512;        // TODO: Don't assume   
152
153         rv = VFS_WriteAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Source );
154         rv /= blocksize;
155         return rv;
156 }
157

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