Kernel - Added return value to module cleanup, fixed LVM bugs
[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->BlockSize = BlockSize;
56         real_vol->BlockCount = BlockCount;
57         real_vol->nSubVolumes = dummy_vol.nSubVolumes;
58         real_vol->SubVolumes = (void*)( real_vol->Name + strlen(Name) + 1 );
59         strcpy(real_vol->Name, Name);
60         memset(real_vol->SubVolumes, 0, sizeof(tLVM_SubVolume*) * real_vol->nSubVolumes);
61         // - VFS Nodes
62         memset(&real_vol->DirNode, 0, sizeof(tVFS_Node));
63         real_vol->DirNode.Type = &gLVM_VolNodeType;
64         real_vol->DirNode.ImplPtr = real_vol;
65         real_vol->DirNode.Flags = VFS_FFLAG_DIRECTORY;
66         real_vol->DirNode.Size = -1;
67         memset(&real_vol->VolNode, 0, sizeof(tVFS_Node));
68         real_vol->VolNode.Type = &gLVM_VolNodeType;
69         real_vol->VolNode.ImplPtr = real_vol;
70         real_vol->VolNode.Size = BlockCount * BlockSize;
71
72         // Type->PopulateSubvolumes
73         fmt->PopulateSubvolumes(real_vol, first_block);
74         free(first_block);
75
76         // Add to volume list
77         gpLVM_LastVolume->Next = real_vol;
78         gpLVM_LastVolume = real_vol;
79
80         return 0;
81 }
82
83 void LVM_int_SetSubvolume_Anon(tLVM_Vol *Volume, int Index, Uint64 FirstBlock, Uint64 BlockCount)
84 {
85         tLVM_SubVolume  *sv;
86          int    namelen;
87
88         if( Index < 0 || Index >= Volume->nSubVolumes ) {
89                 Log_Warning("LVM", "SV ID is out of range (0 < %i < %i)",
90                         Index, Volume->nSubVolumes);
91                 return ;
92         }
93
94         if( Volume->SubVolumes[Index] ) {
95                 Log_Warning("LVM", "Attempt to set SV %i of %p twice", Index, Volume);
96                 return ;
97         }
98         
99         namelen = snprintf(NULL, 0, "%i", Index);
100
101         sv = malloc( sizeof(tLVM_SubVolume) + namelen + 1 );
102         if(!sv) {
103                 // Oh, f*ck
104                 return ;
105         }
106         Volume->SubVolumes[Index] = sv; 
107
108         sv->Vol = Volume;
109         sprintf(sv->Name, "%i", Index);
110         sv->FirstBlock = FirstBlock;
111         sv->BlockCount = BlockCount;
112         memset(&sv->Node, 0, sizeof(tVFS_Node));
113         
114         sv->Node.ImplPtr = sv;
115         sv->Node.Type = &gLVM_SubVolNodeType;
116         sv->Node.Size = BlockCount * Volume->BlockSize;
117         
118         Log_Log("LVM", "Partition %s/%s - 0x%llx+0x%llx blocks",
119                 Volume->Name, sv->Name, FirstBlock, BlockCount);
120 }
121
122 // --------------------------------------------------------------------
123 // IO
124 // --------------------------------------------------------------------
125 size_t LVM_int_ReadVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, void *Dest)
126 {
127         return Volume->Type->Read(Volume->Ptr, BlockNum, BlockCount, Dest);
128 }
129
130 size_t LVM_int_WriteVolume(tLVM_Vol *Volume, Uint64 BlockNum, size_t BlockCount, const void *Src)
131 {
132         return Volume->Type->Write(Volume->Ptr, BlockNum, BlockCount, Src);     
133 }
134
135 int LVM_int_VFSReadEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, void *Dest)
136 {
137         size_t  blocksize;
138         size_t  rv;
139
140         blocksize = 512;        // TODO: Don't assume   
141
142         rv = VFS_ReadAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Dest );
143         rv /= blocksize;
144         return rv;
145 }
146
147 int LVM_int_VFSWriteEmul(void *Arg, Uint64 BlockStart, size_t BlockCount, const void *Source)
148 {
149         size_t  blocksize;
150         size_t  rv;
151
152         blocksize = 512;        // TODO: Don't assume   
153
154         rv = VFS_WriteAt( (int)(Uint)Arg, BlockStart * blocksize, BlockCount * blocksize, Source );
155         rv /= blocksize;
156         return rv;
157 }
158

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