2 * Acess2 Kernel - RAM Disk Support
3 * - By John Hodge (thePowersGang)
12 #define VERSION VER2(0,1)
14 #define MIN_RAMDISK_SIZE (1*1024*1024)
15 #define MAX_RAMDISK_SIZE (64*1024*1024)
18 int RAMFS_Install(char **Arguments);
19 void RAMFS_Cleanup(void);
20 // --- Mount/Unmount ---
21 tVFS_Node *RAMFS_InitDevice(const char *Device, const char **Options);
22 void RAMFS_Unmount(tVFS_Node *Node);
23 // --- Directories ---
24 char *RAMFS_ReadDir(tVFS_Node *Node, int Index);
25 tVFS_Node *RAMFS_FindDir(tVFS_Node *Node, const char *Name);
26 int RAMFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
27 int RAMFS_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *Node);
28 int RAMFS_Unlink(tVFS_Node *Node, const char *Name);
30 size_t RAMFS_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer);
31 size_t RAMFS_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer);
33 void RAMFS_int_RefFile(tRAMFS_Inode *Inode);
34 void RAMFS_int_DerefFile(tRAMFS_Inode *Inode);
35 void *RAMFS_int_GetPage(tRAMFS_File *File, int Page, int bCanAlloc);
36 void RAMFS_int_DropPage(void *Page);
37 Uint32 RAMFS_int_AllocatePage(tRAMDisk *Disk);
38 void RAMFS_int_RefFile(tRAMFS_Inode *Inode);
39 void RAMFS_int_DerefFile(tRAMFS_Inode *Inode);
42 MODULE_DEFINE(0, VERSION, FS_RAMDisk, RAMFS_Install, RAMFS_Cleanup, NULL);
43 tVFS_Driver gRAMFS_Driver = {
45 .InitDevice = RAMFS_InitDevice,
46 .Unmount = RAMFS_Unmount
47 // TODO: GetNodeFromInode
49 tVFS_NodeType gRAMFS_DirNodeType = {
50 .ReadDir = RAMFS_ReadDir,
51 .FindDir = RAMFS_FindDir,
54 .Unlink = RAMFS_Unlink
56 tVFS_NodeType gRAMFS_FileNodeType = {
62 int RAMFS_Install(char **Arguments)
64 VFS_AddDriver( &gRAMFS_Driver );
68 void RAMFS_Cleanup(void)
75 const char *_GetOption(const char *Data, const char *Option)
77 int len = strlen(Option);
78 if( strncmp(Data, Option, len) != 0 )
80 if( Data[len] != '=' )
82 return Data + len + 1;
85 tVFS_Node *RAMFS_InitDevice(const char *Device, const char **Options)
93 for( int i = 0; Options[i]; i ++ )
95 LOG("Options[%i] = '%s'", i, Options[i]);
96 if( (v = _GetOption(Options[i], "megs")) )
98 size = atoi(v) * 1024 * 1024;
99 LOG("Size set to %iMiB", size>>20);
104 LOG("Disk Size %iKiB", size>>10);
106 if( size > MAX_RAMDISK_SIZE || size < MIN_RAMDISK_SIZE )
109 int n_pages = size / PAGE_SIZE;
111 rd = calloc(1, sizeof(tRAMDisk) + n_pages * sizeof(tPAddr) + n_pages / 8);
112 rd->MaxPages = n_pages;
113 rd->RootDir.Inode.Disk = rd;
114 rd->RootDir.Inode.Node.ImplPtr = &rd->RootDir;
115 rd->RootDir.Inode.Node.Flags = VFS_FFLAG_DIRECTORY;
116 rd->RootDir.Inode.Node.Size = -1;
117 rd->RootDir.Inode.Node.Type = &gRAMFS_DirNodeType;
118 rd->Bitmap = (void*)&rd->PhysPages[ n_pages ];
120 for( int i = 0; i < n_pages; i ++ )
122 rd->PhysPages[i] = MM_AllocPhys();
123 if( !rd->PhysPages[i] ) {
130 return &rd->RootDir.Inode.Node;
133 void RAMFS_Unmount(tVFS_Node *Node)
135 Log_Warning("RAMDisk", "TODO: Impliment unmounting");
138 // --- Directories ---
139 char *RAMFS_ReadDir(tVFS_Node *Node, int Index)
141 tRAMFS_Dir *dir = Node->ImplPtr;
142 for( tRAMFS_DirEnt *d = dir->FirstEnt; d; d = d->Next )
144 if( Index -- == 0 ) {
145 LOG("Return %s", d->Name);
146 return strdup(d->Name);
153 tVFS_Node *RAMFS_FindDir(tVFS_Node *Node, const char *Name)
155 tRAMFS_Dir *dir = Node->ImplPtr;
157 for( tRAMFS_DirEnt *d = dir->FirstEnt; d; d = d->Next )
159 if( strcmp(d->Name, Name) == 0 ) {
160 LOG("Return %p", &d->Inode->Node);
161 return &d->Inode->Node;
169 int RAMFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
171 tRAMFS_Dir *dir = Node->ImplPtr;
172 if( RAMFS_FindDir(Node, Name) != NULL )
175 tRAMFS_DirEnt *de = malloc( sizeof(tRAMFS_DirEnt) + strlen(Name) + 1 );
177 de->NameLen = strlen(Name);
178 strcpy(de->Name, Name);
180 if( Flags & VFS_FFLAG_DIRECTORY ) {
181 tRAMFS_Dir *newdir = calloc(1, sizeof(tRAMFS_Dir));
182 newdir->Inode.Node.Type = &gRAMFS_DirNodeType;
183 de->Inode = &newdir->Inode;
186 tRAMFS_File *newfile = calloc(1, sizeof(tRAMFS_File));
187 newfile->Inode.Node.Type = &gRAMFS_FileNodeType;
188 de->Inode = &newfile->Inode;
190 de->Inode->Disk = dir->Inode.Disk;
191 de->Inode->Node.Flags = Flags;
192 de->Inode->Node.ImplPtr = de->Inode;
194 RAMFS_int_RefFile(de->Inode);
198 dir->LastEnt->Next = de;
206 int RAMFS_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *FileNode)
208 tRAMFS_Dir *dir = DirNode->ImplPtr;
211 for( dp = dir->FirstEnt; dp; dp = dp->Next )
213 if( strcmp(dp->Name, Name) == 0 )
217 dp = malloc( sizeof(tRAMFS_DirEnt) + strlen(Name) + 1 );
219 dp->Inode = FileNode->ImplPtr;
220 RAMFS_int_RefFile(dp->Inode);
221 strcpy(dp->Name, Name);
225 dir->LastEnt->Next = dp;
233 int RAMFS_Unlink(tVFS_Node *Node, const char *Name)
235 tRAMFS_Dir *dir = Node->ImplPtr;
236 tRAMFS_DirEnt *dp, *p = NULL;
238 // TODO: Is locking needed?
240 // Find the directory entry
241 for( dp = dir->FirstEnt; dp; p = dp, dp = dp->Next )
243 if( strcmp(dp->Name, Name) == 0 )
248 // Dereference the file
249 RAMFS_int_DerefFile( dp->Inode );
251 // Remove and free directory entry
253 dir->FirstEnt = dp->Next;
256 if( dir->LastEnt == dp )
264 size_t RAMFS_int_DoIO(tRAMFS_File *File, off_t Offset, size_t Size, void *Buffer, int bRead)
270 if( Offset >= File->Size ) return 0;
272 if( Offset + Size > File->Size ) Size = File->Size - Size;
274 if( Size == 0 ) return 0;
276 page = Offset / PAGE_SIZE;
280 page_virt = RAMFS_int_GetPage(File, page++, !bRead);
281 if(!page_virt) return 0;
283 if( Offset + Size <= PAGE_SIZE )
286 memcpy(Buffer, page_virt + Offset, Size);
288 memcpy(page_virt + Offset, Buffer, Size);
293 memcpy(Buffer, page_virt + Offset, PAGE_SIZE - Offset);
295 memcpy(page_virt + Offset, Buffer, PAGE_SIZE - Offset);
296 Buffer += PAGE_SIZE - Offset;
297 rem -= PAGE_SIZE - Offset;
299 while( rem >= PAGE_SIZE )
301 RAMFS_int_DropPage(page_virt);
302 page_virt = RAMFS_int_GetPage(File, page++, !bRead);
303 if(!page_virt) return Size - rem;
306 memcpy(Buffer, page_virt, PAGE_SIZE);
308 memcpy(page_virt, Buffer, PAGE_SIZE);
316 page_virt = RAMFS_int_GetPage(File, page, !bRead);
317 if(!page_virt) return Size - rem;
319 memcpy(Buffer, page_virt, rem);
321 memcpy(page_virt, Buffer, rem);
324 RAMFS_int_DropPage(page_virt);
329 size_t RAMFS_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer)
331 tRAMFS_File *file = Node->ImplPtr;
333 return RAMFS_int_DoIO(file, Offset, Size, Buffer, 1);
336 size_t RAMFS_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer)
338 tRAMFS_File *file = Node->ImplPtr;
340 // TODO: Limit checks?
341 if( Offset == file->Size )
344 return RAMFS_int_DoIO(file, Offset, Size, (void*)Buffer, 0);
348 void *RAMFS_int_GetPage(tRAMFS_File *File, int Page, int bCanAlloc)
351 Uint32 *page_in_1 = NULL;
352 Uint32 *page_in_2 = NULL;
356 if( Page < 0 ) return NULL;
358 if( Page < RAMFS_NDIRECT )
360 if( File->PagesDirect[Page] )
361 page_id = File->PagesDirect[Page];
363 else if( Page - RAMFS_NDIRECT < PAGE_SIZE/4 )
365 ofs = Page - RAMFS_NDIRECT;
366 if( File->Indirect1Page == 0 ) {
370 File->Indirect1Page = RAMFS_int_AllocatePage(File->Inode.Disk) + 1;
372 page_in_1 = MM_MapTemp( File->Inode.Disk->PhysPages[File->Indirect1Page-1] );
373 page_id = page_in_1[ofs];
375 else if( Page - RAMFS_NDIRECT - PAGE_SIZE/4 < (PAGE_SIZE/4)*(PAGE_SIZE/4) )
377 block = (Page - RAMFS_NDIRECT - PAGE_SIZE/4) / (PAGE_SIZE/4);
378 ofs = (Page - RAMFS_NDIRECT - PAGE_SIZE/4) % (PAGE_SIZE/4);
379 if( File->Indirect2Page == 0 ){
383 File->Indirect2Page = RAMFS_int_AllocatePage(File->Inode.Disk) + 1;
386 page_in_2 = MM_MapTemp( File->Inode.Disk->PhysPages[File->Indirect2Page-1] );
387 if( page_in_2[block] == 0 ) {
391 page_in_2[block] = RAMFS_int_AllocatePage(File->Inode.Disk) + 1;
394 page_in_1 = MM_MapTemp( File->Inode.Disk->PhysPages[page_in_2[block] - 1] );
395 page_id = page_in_1[ofs];
403 page_id = RAMFS_int_AllocatePage(File->Inode.Disk) + 1;
405 page_in_1[ofs] = page_id;
407 File->PagesDirect[Page] = page_id;
410 MM_FreeTemp( page_in_1 );
411 MM_FreeTemp( page_in_2 );
413 return MM_MapTemp( File->Inode.Disk->PhysPages[page_id - 1] );
416 void RAMFS_int_DropPage(void *Page)
421 Uint32 RAMFS_int_AllocatePage(tRAMDisk *Disk)
426 if( Disk->nUsedPages == Disk->MaxPages )
429 // Find a chunk with at least one free page
430 for( i = 0; i < Disk->MaxPages / 32; i ++ )
432 if( Disk->Bitmap[i] != -1 )
435 if( i == Disk->MaxPages / 32 ) {
436 Log_Error("RAMFS", "Bookkeeping error, count and bitmap disagree");
440 // Find the exact page
441 for( j = 0; j < 32; j ++ )
443 if( !(Disk->Bitmap[i] & (1U << j)) )
451 void RAMFS_int_RefFile(tRAMFS_Inode *Inode)
456 void RAMFS_int_DerefFile(tRAMFS_Inode *Inode)
459 if( Inode->nLink >= 0 )
462 Log_Error("RAMFS", "TODO: Clean up files when deleted");
464 // Need to delete file
465 switch( Inode->Type )