Kernel - Adding network debugging, disabled
[tpg/acess2.git] / KernelLand / Modules / Storage / LVM / main.c
1 /*
2  * Acess2 Logical Volume Manager
3  * - By John Hodge (thePowersGang)
4  *
5  * lvm.h
6  * - LVM Core definitions
7  */
8 #define DEBUG   0
9 #define VERSION VER2(0,1)
10 #include "lvm_int.h"
11 #include <fs_devfs.h>
12 #include <modules.h>
13 #include <api_drv_disk.h>
14
15 // === PROTOTYPES ===
16 // ---
17  int    LVM_Initialise(char **Arguments);
18 void    LVM_Cleanup(void);
19 // ---
20 char    *LVM_Root_ReadDir(tVFS_Node *Node, int ID);
21 tVFS_Node       *LVM_Root_FindDir(tVFS_Node *Node, const char *Name);
22 char    *LVM_Vol_ReadDir(tVFS_Node *Node, int ID);
23 tVFS_Node       *LVM_Vol_FindDir(tVFS_Node *Node, const char *Name);
24 size_t  LVM_Vol_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
25 size_t  LVM_Vol_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
26 size_t  LVM_SubVol_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
27 size_t  LVM_SubVol_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
28
29 Uint    LVM_int_DrvUtil_ReadBlock(Uint64 Address, Uint Count, void *Buffer, void *Argument);
30 Uint    LVM_int_DrvUtil_WriteBlock(Uint64 Address, Uint Count, const void *Buffer, void *Argument);
31
32 // === GLOBALS ===
33 MODULE_DEFINE(0, VERSION, LVM, LVM_Initialise, LVM_Cleanup, NULL);
34 tVFS_NodeType   gLVM_RootNodeType = {
35         .ReadDir = LVM_Root_ReadDir,
36         .FindDir = LVM_Root_FindDir
37 };
38 tVFS_NodeType   gLVM_VolNodeType = {
39         .ReadDir = LVM_Vol_ReadDir,
40         .FindDir = LVM_Vol_FindDir,
41         .Read = LVM_Vol_Read,
42         .Write = LVM_Vol_Write
43 };
44 tVFS_NodeType   gLVM_SubVolNodeType = {
45         .Read = LVM_SubVol_Read,
46         .Write = LVM_SubVol_Write
47 };
48 tDevFS_Driver   gLVM_DevFS = {
49         NULL, "LVM",
50         {.Flags = VFS_FFLAG_DIRECTORY, .Type = &gLVM_RootNodeType, .Size = -1}
51 };
52
53 tLVM_Vol        *gpLVM_FirstVolume;
54 tLVM_Vol        *gpLVM_LastVolume = (void*)&gpLVM_FirstVolume;
55
56 // === CODE ===
57 int LVM_Initialise(char **Arguments)
58 {
59         DevFS_AddDevice( &gLVM_DevFS );
60         return 0;
61 }
62
63 void LVM_Cleanup(void)
64 {
65         
66 }
67
68 // --------------------------------------------------------------------
69 // VFS Inteface
70 // --------------------------------------------------------------------
71 char *LVM_Root_ReadDir(tVFS_Node *Node, int ID)
72 {
73         tLVM_Vol        *vol;
74         
75         if( ID < 0 )    return NULL;    
76
77         for( vol = gpLVM_FirstVolume; vol && ID --; vol = vol->Next );
78         
79         if(vol)
80                 return strdup(vol->Name);
81         else
82                 return NULL;
83 }
84 tVFS_Node *LVM_Root_FindDir(tVFS_Node *Node, const char *Name)
85 {
86         tLVM_Vol        *vol;
87         for( vol = gpLVM_FirstVolume; vol; vol = vol->Next )
88         {
89                 if( strcmp(vol->Name, Name) == 0 )
90                 {
91                         return &vol->DirNode;
92                 }
93         }
94         return NULL;
95 }
96
97 char *LVM_Vol_ReadDir(tVFS_Node *Node, int ID)
98 {
99         tLVM_Vol        *vol = Node->ImplPtr;
100         
101         if( ID < 0 || ID >= vol->nSubVolumes+1 )
102                 return NULL;
103
104         if( ID == 0 )
105                 return strdup("ROOT");
106         else
107                 return strdup( vol->SubVolumes[ID-1]->Name );
108 }
109 tVFS_Node *LVM_Vol_FindDir(tVFS_Node *Node, const char *Name)
110 {
111         tLVM_Vol        *vol = Node->ImplPtr;
112
113         if( strcmp("ROOT", Name) == 0 )
114                 return &vol->VolNode;
115         
116         for( int i = 0; i < vol->nSubVolumes; i ++ )
117         {
118                 if( strcmp(vol->SubVolumes[i]->Name, Name) == 0 )
119                 {
120                         return &vol->SubVolumes[i]->Node;
121                 }
122         }
123
124         return NULL;
125 }
126
127 size_t LVM_Vol_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
128 {
129         tLVM_Vol        *vol = Node->ImplPtr;
130         Uint64  byte_size = vol->BlockCount * vol->BlockSize;   
131
132         if( Offset > byte_size )
133                 return 0;
134         if( Length > byte_size )
135                 Length = byte_size;
136         if( Offset + Length > byte_size )
137                 Length = byte_size - Offset;
138
139         return DrvUtil_ReadBlock(
140                 Offset, Length, Buffer, 
141                 LVM_int_DrvUtil_ReadBlock, vol->BlockSize, vol
142                 );
143 }
144
145 size_t LVM_Vol_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
146 {
147         return 0;
148 }
149
150 size_t LVM_SubVol_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
151 {
152         tLVM_SubVolume  *sv = Node->ImplPtr;
153         Uint64  byte_size = sv->BlockCount * sv->Vol->BlockSize;
154
155         if( Offset > byte_size )
156                 return 0;
157         if( Length > byte_size )
158                 Length = byte_size;
159         if( Offset + Length > byte_size )
160                 Length = byte_size - Offset;
161
162         LOG("Reading (0x%llx+0x%llx)+0x%x to %p",
163                 (Uint64)(sv->FirstBlock * sv->Vol->BlockSize), Offset,
164                 Length, Buffer
165                 );
166         
167         Offset += sv->FirstBlock * sv->Vol->BlockSize;  
168
169         return DrvUtil_ReadBlock(
170                 Offset, Length, Buffer, 
171                 LVM_int_DrvUtil_ReadBlock, sv->Vol->BlockSize, sv->Vol
172                 );
173 }
174 size_t LVM_SubVol_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
175 {
176         tLVM_SubVolume  *sv = Node->ImplPtr;
177         Uint64  byte_size = sv->BlockCount * sv->Vol->BlockSize;
178
179         if( Offset > byte_size )
180                 return 0;
181         if( Length > byte_size )
182                 Length = byte_size;
183         if( Offset + Length > byte_size )
184                 Length = byte_size - Offset;
185
186         Offset += sv->FirstBlock * sv->Vol->BlockSize;  
187         
188         return DrvUtil_WriteBlock(
189                 Offset, Length, Buffer,
190                 LVM_int_DrvUtil_ReadBlock, LVM_int_DrvUtil_WriteBlock,
191                 sv->Vol->BlockSize, sv->Vol
192                 );
193 }
194
195 Uint LVM_int_DrvUtil_ReadBlock(Uint64 Address, Uint Count, void *Buffer, void *Argument)
196 {
197         return LVM_int_ReadVolume( Argument, Address, Count, Buffer );
198 }
199
200 Uint LVM_int_DrvUtil_WriteBlock(Uint64 Address, Uint Count, const void *Buffer, void *Argument)
201 {
202         return LVM_int_WriteVolume( Argument, Address, Count, Buffer );
203 }
204

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