From 9dccbc6f16485ea62caa8c4153f6f878da8cbb0d Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 22 May 2012 15:40:50 +0800 Subject: [PATCH] Cleanups - MM_MapTemp to return void*, VFS use Unlink instead of Relink --- KernelLand/Kernel/arch/x86/mm_virt.c | 20 +- KernelLand/Kernel/drv/fifo.c | 27 +- KernelLand/Kernel/include/acess.h | 4 +- KernelLand/Kernel/include/vfs.h | 7 +- KernelLand/Kernel/vfs/mount.c | 23 +- KernelLand/Modules/Filesystems/Ext2/dir.c | 14 +- .../Modules/Filesystems/Ext2/ext2_common.h | 2 +- .../Modules/Filesystems/RAMDisk/ramdisk.c | 311 +++++++++++++----- .../Modules/Filesystems/RAMDisk/ramdisk.h | 34 +- KernelLand/Modules/IPStack/routing.c | 36 +- KernelLand/Modules/USB/UHCI/uhci.c | 20 +- 11 files changed, 323 insertions(+), 175 deletions(-) diff --git a/KernelLand/Kernel/arch/x86/mm_virt.c b/KernelLand/Kernel/arch/x86/mm_virt.c index 51a3f621..d15c3d72 100644 --- a/KernelLand/Kernel/arch/x86/mm_virt.c +++ b/KernelLand/Kernel/arch/x86/mm_virt.c @@ -694,9 +694,9 @@ tPAddr MM_Clone(int bNoUserCopy) MM_RefPhys( gaTmpTable[i*1024+j] & ~0xFFF ); - tmp = (void *) MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF ); + tmp = MM_MapTemp( gaTmpTable[i*1024+j] & ~0xFFF ); memcpy( tmp, (void *)( (i*1024+j)*0x1000 ), 0x1000 ); - MM_FreeTemp( (Uint)tmp ); + MM_FreeTemp( tmp ); } } @@ -804,9 +804,9 @@ tVAddr MM_NewWorkerStack(Uint *StackContents, size_t ContentsSize) // NOTE: Max of 1 page // `page` is the last allocated page from the previious for loop - tmpPage = MM_MapTemp( page ); + tmpPage = (tVAddr)MM_MapTemp( page ); memcpy( (void*)( tmpPage + (0x1000 - ContentsSize) ), StackContents, ContentsSize); - MM_FreeTemp(tmpPage); + MM_FreeTemp( (void*)tmpPage ); //Log("MM_NewWorkerStack: RETURN 0x%x", base); return base + WORKER_STACK_SIZE; @@ -935,7 +935,7 @@ int MM_IsValidBuffer(tVAddr Addr, size_t Size) tPAddr MM_DuplicatePage(tVAddr VAddr) { tPAddr ret; - Uint temp; + void *temp; int wasRO = 0; //ENTER("xVAddr", VAddr); @@ -960,7 +960,7 @@ tPAddr MM_DuplicatePage(tVAddr VAddr) // Copy Data temp = MM_MapTemp(ret); - memcpy( (void*)temp, (void*)VAddr, 0x1000 ); + memcpy( temp, (void*)VAddr, 0x1000 ); MM_FreeTemp(temp); // Restore Writeable status @@ -976,7 +976,7 @@ tPAddr MM_DuplicatePage(tVAddr VAddr) * \brief Create a temporary memory mapping * \todo Show Luigi Barone (C Lecturer) and see what he thinks */ -tVAddr MM_MapTemp(tPAddr PAddr) +void * MM_MapTemp(tPAddr PAddr) { int i; @@ -999,7 +999,7 @@ tVAddr MM_MapTemp(tPAddr PAddr) INVLPG( TEMP_MAP_ADDR + (i << 12) ); //LEAVE('p', TEMP_MAP_ADDR + (i << 12)); Mutex_Release( &glTempMappings ); - return TEMP_MAP_ADDR + (i << 12); + return (void*)( TEMP_MAP_ADDR + (i << 12) ); } Mutex_Release( &glTempMappings ); Threads_Yield(); // TODO: Use a sleep queue here instead @@ -1010,9 +1010,9 @@ tVAddr MM_MapTemp(tPAddr PAddr) * \fn void MM_FreeTemp(tVAddr PAddr) * \brief Free's a temp mapping */ -void MM_FreeTemp(tVAddr VAddr) +void MM_FreeTemp(void *VAddr) { - int i = VAddr >> 12; + int i = (tVAddr)VAddr >> 12; //ENTER("xVAddr", VAddr); if(i >= (TEMP_MAP_ADDR >> 12)) diff --git a/KernelLand/Kernel/drv/fifo.c b/KernelLand/Kernel/drv/fifo.c index 94e8aaeb..cb62193a 100644 --- a/KernelLand/Kernel/drv/fifo.c +++ b/KernelLand/Kernel/drv/fifo.c @@ -31,7 +31,7 @@ tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename); int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); void FIFO_Reference(tVFS_Node *Node); void FIFO_Close(tVFS_Node *Node); - int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName); + int FIFO_Unlink(tVFS_Node *Node, const char *OldName); size_t FIFO_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer); size_t FIFO_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer); tPipe *FIFO_Int_NewPipe(int Size, const char *Name); @@ -43,7 +43,7 @@ tVFS_NodeType gFIFO_DirNodeType = { .ReadDir = FIFO_ReadDir, .FindDir = FIFO_FindDir, .MkNod = FIFO_MkNod, - .Relink = FIFO_Relink, + .Unlink = FIFO_Unlink, .IOCtl = FIFO_IOCtl }; tVFS_NodeType gFIFO_PipeNodeType = { @@ -178,12 +178,11 @@ void FIFO_Close(tVFS_Node *Node) } /** - * \fn int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) - * \brief Relink a file (Deletes named pipes) + * \brief Delete a pipe */ -int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) +int FIFO_Unlink(tVFS_Node *Node, const char *OldName) { - tPipe *pipe, *tmp; + tPipe *pipe; if(Node != &gFIFO_DriverInfo.RootNode) return 0; @@ -200,22 +199,6 @@ int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) } if(!pipe) return 0; - // Relink a named pipe - if(NewName) { - // Check new name - for(tmp = gFIFO_NamedPipes; - tmp; - tmp = tmp->Next) - { - if(strcmp(tmp->Name, NewName) == 0) return 0; - } - // Create new name - free(pipe->Name); - pipe->Name = malloc(strlen(NewName)+1); - strcpy(pipe->Name, NewName); - return 1; - } - // Unlink the pipe if(Node->ImplPtr) { free(Node->ImplPtr); diff --git a/KernelLand/Kernel/include/acess.h b/KernelLand/Kernel/include/acess.h index 767230fe..e673290a 100644 --- a/KernelLand/Kernel/include/acess.h +++ b/KernelLand/Kernel/include/acess.h @@ -278,12 +278,12 @@ extern Uint MM_GetFlags(tVAddr VAddr); * \return Virtual address of page in memory * \note There is only a limited ammount of slots avaliable */ -extern tVAddr MM_MapTemp(tPAddr PAddr); +extern void *MM_MapTemp(tPAddr PAddr); /** * \brief Free a temporarily mapped page * \param VAddr Allocate virtual addres of page */ -extern void MM_FreeTemp(tVAddr VAddr); +extern void MM_FreeTemp(void *Ptr); /** * \brief Map a physcal address range into the virtual address space * \param PAddr Physical address to map in diff --git a/KernelLand/Kernel/include/vfs.h b/KernelLand/Kernel/include/vfs.h index 7d0a26db..b746d0e7 100644 --- a/KernelLand/Kernel/include/vfs.h +++ b/KernelLand/Kernel/include/vfs.h @@ -307,19 +307,18 @@ struct sVFS_NodeType * \brief Relink (Rename/Remove) a file/directory * \param Node Pointer to this node * \param OldName Name of the item to move/delete - * \param NewName New name (or NULL if unlinking is wanted) * \return Zero on Success, non-zero on error (see errno.h) */ - int (*Relink)(struct sVFS_Node *Node, const char *OldName, const char *NewName); + int (*Unlink)(struct sVFS_Node *Node, const char *OldName); /** * \brief Link a node to a name * \param Node Pointer to this node (directory) - * \param Child Node to create a new link to * \param NewName Name for the new link + * \param Child Node to create a new link to * \retur Zeron on success, non-zero on error (see errno.h) */ - int (*Link)(struct sVFS_Node *Node, struct sVFS_Node *Child, const char *NewName); + int (*Link)(struct sVFS_Node *Node, const char *NewName, struct sVFS_Node *Child); /** * \} diff --git a/KernelLand/Kernel/vfs/mount.c b/KernelLand/Kernel/vfs/mount.c index 1773218a..f281247b 100644 --- a/KernelLand/Kernel/vfs/mount.c +++ b/KernelLand/Kernel/vfs/mount.c @@ -74,13 +74,34 @@ int VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem mnt->Options = &mnt->StrData[deviceLen+1+mountLen+1]; memcpy( mnt->Options, Options, argLen+1 ); + // Parse options string + char *str = mnt->Options; + int nArg = 0; + do { + nArg ++; + } while( (str = strchr(str, ',')) ); + + char *args[nArg + 1]; + str = mnt->Options; + nArg = 0; + do { + args[nArg++] = str; + str = strchr(str, ','); + if(str) *str = '\0'; + } while( str ); + args[nArg] = 0; // NULL terminal + // Initialise Volume - mnt->RootNode = fs->InitDevice(Device, NULL); //&ArgString); + mnt->RootNode = fs->InitDevice(Device, (const char **)args); if(!mnt->RootNode) { free(mnt); return -2; } + // Repair the options string + while( nArg -- > 1 ) + args[nArg][-1] = ','; + mnt->Identifier = giVFS_NextMountIdent++; #if 0 // Ensure identifiers don't repeat diff --git a/KernelLand/Modules/Filesystems/Ext2/dir.c b/KernelLand/Modules/Filesystems/Ext2/dir.c index 2a7bc2fe..0887ed28 100644 --- a/KernelLand/Modules/Filesystems/Ext2/dir.c +++ b/KernelLand/Modules/Filesystems/Ext2/dir.c @@ -16,8 +16,8 @@ char *Ext2_ReadDir(tVFS_Node *Node, int Pos); tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *FileName); int Ext2_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); - int Ext2_Relink(tVFS_Node *Node, const char *OldName, const char *NewName); - int Ext2_Link(tVFS_Node *Parent, tVFS_Node *Node, const char *Name); + int Ext2_Unlink(tVFS_Node *Node, const char *OldName); + int Ext2_Link(tVFS_Node *Parent, const char *Name, tVFS_Node *Node); // --- Helpers --- tVFS_Node *Ext2_int_CreateNode(tExt2_Disk *Disk, Uint InodeId); @@ -27,7 +27,7 @@ tVFS_NodeType gExt2_DirType = { .ReadDir = Ext2_ReadDir, .FindDir = Ext2_FindDir, .MkNod = Ext2_MkNod, - .Relink = Ext2_Relink, + .Unlink = Ext2_Unlink, .Link = Ext2_Link, .Close = Ext2_CloseFile }; @@ -207,9 +207,9 @@ int Ext2_MkNod(tVFS_Node *Parent, const char *Name, Uint Flags) * \param Node This (directory) node * \param OldName Old name of file * \param NewName New name for file - * \return Boolean Failure - See ::tVFS_Node.Relink for info + * \return Boolean Failure - See ::tVFS_Node.Unlink for info */ -int Ext2_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) +int Ext2_Unlink(tVFS_Node *Node, const char *OldName) { return 1; } @@ -217,11 +217,11 @@ int Ext2_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) /** * \brief Links an existing node to a new name * \param Parent Parent (directory) node - * \param Node Node to link * \param Name New name for the node + * \param Node Node to link * \return Boolean Failure - See ::tVFS_Node.Link for info */ -int Ext2_Link(tVFS_Node *Node, tVFS_Node *Child, const char *Name) +int Ext2_Link(tVFS_Node *Node, const char *Name, tVFS_Node *Child) { #if 0 tExt2_Disk *disk = Node->ImplPtr; diff --git a/KernelLand/Modules/Filesystems/Ext2/ext2_common.h b/KernelLand/Modules/Filesystems/Ext2/ext2_common.h index f151f2c4..036cd59f 100644 --- a/KernelLand/Modules/Filesystems/Ext2/ext2_common.h +++ b/KernelLand/Modules/Filesystems/Ext2/ext2_common.h @@ -38,7 +38,7 @@ extern int Ext2_int_WriteInode(tExt2_Disk *Disk, Uint32 InodeId, tExt2_Inode *In extern char *Ext2_ReadDir(tVFS_Node *Node, int Pos); extern tVFS_Node *Ext2_FindDir(tVFS_Node *Node, const char *FileName); extern int Ext2_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); -extern int Ext2_Link(tVFS_Node *Parent, tVFS_Node *Node, const char *Name); +extern int Ext2_Link(tVFS_Node *Parent, const char *Name, tVFS_Node *Node); // --- Read --- extern size_t Ext2_Read(tVFS_Node *node, off_t offset, size_t length, void *buffer); // --- Write --- diff --git a/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.c b/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.c index 309c8425..adfc38a0 100644 --- a/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.c +++ b/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.c @@ -5,7 +5,7 @@ * ramdisk.c * - Core File */ -#define DEBUG 0 +#define DEBUG 1 #include #include #include "ramdisk.h" @@ -15,76 +15,94 @@ #define MAX_RAMDISK_SIZE (64*1024*1024) // === PROTOTYPES === - int RAMDisk_Install(char **Arguments); -void RAMDisk_Cleanup(void); + int RAMFS_Install(char **Arguments); +void RAMFS_Cleanup(void); // --- Mount/Unmount --- -tVFS_Node *RAMDisk_InitDevice(const char *Device, const char **Options); -void RAMDisk_Unmount(tVFS_Node *Node); +tVFS_Node *RAMFS_InitDevice(const char *Device, const char **Options); +void RAMFS_Unmount(tVFS_Node *Node); // --- Directories --- -char *RAMDisk_ReadDir(tVFS_Node *Node, int Index); -tVFS_Node *RAMDisk_FindDir(tVFS_Node *Node, const char *Name); - int RAMDisk_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); - int RAMDisk_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *Node); - int RAMDisk_Unlink(tVFS_Node *Node, const char *Name); +char *RAMFS_ReadDir(tVFS_Node *Node, int Index); +tVFS_Node *RAMFS_FindDir(tVFS_Node *Node, const char *Name); + int RAMFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); + int RAMFS_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *Node); + int RAMFS_Unlink(tVFS_Node *Node, const char *Name); // --- Files --- -size_t RAMDisk_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer); -size_t RAMDisk_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer); +size_t RAMFS_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer); +size_t RAMFS_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer); // --- Internals -- -void RAMDisk_int_RefFile(tRAMDisk_Inode *Inode); -void RAMDisk_int_DerefFile(tRAMDisk_Inode *Inode); -void *_GetPage(tRAMDisk_File *File, int Page, int bCanAlloc); -void _DropPage(void *Page); +void RAMFS_int_RefFile(tRAMFS_Inode *Inode); +void RAMFS_int_DerefFile(tRAMFS_Inode *Inode); +void *RAMFS_int_GetPage(tRAMFS_File *File, int Page, int bCanAlloc); +void RAMFS_int_DropPage(void *Page); +Uint32 RAMFS_int_AllocatePage(tRAMDisk *Disk); +void RAMFS_int_RefFile(tRAMFS_Inode *Inode); +void RAMFS_int_DerefFile(tRAMFS_Inode *Inode); // === GLOBALS === -MODULE_DEFINE(0, VERSION, FS_RAMDisk, RAMDisk_Install, RAMDisk_Cleanup, NULL); -tVFS_Driver gRAMDisk_Driver = { - .Name = "RAMDisk", - .InitDevice = RAMDisk_InitDevice, - .Unmount = RAMDisk_Unmount +MODULE_DEFINE(0, VERSION, FS_RAMDisk, RAMFS_Install, RAMFS_Cleanup, NULL); +tVFS_Driver gRAMFS_Driver = { + .Name = "ramfs", + .InitDevice = RAMFS_InitDevice, + .Unmount = RAMFS_Unmount // TODO: GetNodeFromInode }; -tVFS_NodeType gRAMDisk_DirNodeType = { - .ReadDir = RAMDisk_ReadDir, - .FindDir = RAMDisk_FindDir, - .MkNod = RAMDisk_MkNod, -// .Link = RAMDisk_Link, -// .Unlink = RAMDisk_Unink +tVFS_NodeType gRAMFS_DirNodeType = { + .ReadDir = RAMFS_ReadDir, + .FindDir = RAMFS_FindDir, + .MkNod = RAMFS_MkNod, + .Link = RAMFS_Link, + .Unlink = RAMFS_Unlink }; -tVFS_NodeType gRAMDisk_FileNodeType = { - .Read = RAMDisk_Read, - .Write = RAMDisk_Write +tVFS_NodeType gRAMFS_FileNodeType = { + .Read = RAMFS_Read, + .Write = RAMFS_Write }; // === CODE === -int RAMDisk_Install(char **Arguments) +int RAMFS_Install(char **Arguments) { - VFS_AddDriver( &gRAMDisk_Driver ); + VFS_AddDriver( &gRAMFS_Driver ); return 0; } -void RAMDisk_Cleanup(void) +void RAMFS_Cleanup(void) { } // --- Mount/Unmount -tVFS_Node *RAMDisk_InitDevice(const char *Device, const char **Options) +const char *_GetOption(const char *Data, const char *Option) +{ + int len = strlen(Option); + if( strncmp(Data, Option, len) != 0 ) + return NULL; + if( Data[len] != '=' ) + return NULL; + return Data + len + 1; +} + +tVFS_Node *RAMFS_InitDevice(const char *Device, const char **Options) { size_t size = 0; tRAMDisk *rd; if( Options ) { + const char *v; for( int i = 0; Options[i]; i ++ ) { - if( strcmp(Options[i], "megs") == '=' ) + LOG("Options[%i] = '%s'", i, Options[i]); + if( (v = _GetOption(Options[i], "megs")) ) { - size = atoi(Options[i] + 5) * 1024 * 1024; + size = atoi(v) * 1024 * 1024; + LOG("Size set to %iMiB", size>>20); } } } + LOG("Disk Size %iKiB", size>>10); + if( size > MAX_RAMDISK_SIZE || size < MIN_RAMDISK_SIZE ) return NULL; @@ -96,7 +114,7 @@ tVFS_Node *RAMDisk_InitDevice(const char *Device, const char **Options) rd->RootDir.Inode.Node.ImplPtr = &rd->RootDir; rd->RootDir.Inode.Node.Flags = VFS_FFLAG_DIRECTORY; rd->RootDir.Inode.Node.Size = -1; - rd->RootDir.Inode.Node.Type = &gRAMDisk_DirNodeType; + rd->RootDir.Inode.Node.Type = &gRAMFS_DirNodeType; rd->Bitmap = (void*)&rd->PhysPages[ n_pages ]; for( int i = 0; i < n_pages; i ++ ) @@ -108,64 +126,72 @@ tVFS_Node *RAMDisk_InitDevice(const char *Device, const char **Options) } } + LOG("Mounted"); return &rd->RootDir.Inode.Node; } -void RAMDisk_Unmount(tVFS_Node *Node) +void RAMFS_Unmount(tVFS_Node *Node) { Log_Warning("RAMDisk", "TODO: Impliment unmounting"); } // --- Directories --- -char *RAMDisk_ReadDir(tVFS_Node *Node, int Index) +char *RAMFS_ReadDir(tVFS_Node *Node, int Index) { - tRAMDisk_Dir *dir = Node->ImplPtr; - for( tRAMDisk_DirEnt *d = dir->FirstEnt; d; d = d->Next ) + tRAMFS_Dir *dir = Node->ImplPtr; + for( tRAMFS_DirEnt *d = dir->FirstEnt; d; d = d->Next ) { - if( Index -- == 0 ) + if( Index -- == 0 ) { + LOG("Return %s", d->Name); return strdup(d->Name); + } } + LOG("Return NULL"); return NULL; } -tVFS_Node *RAMDisk_FindDir(tVFS_Node *Node, const char *Name) +tVFS_Node *RAMFS_FindDir(tVFS_Node *Node, const char *Name) { - tRAMDisk_Dir *dir = Node->ImplPtr; - for( tRAMDisk_DirEnt *d = dir->FirstEnt; d; d = d->Next ) + tRAMFS_Dir *dir = Node->ImplPtr; + + for( tRAMFS_DirEnt *d = dir->FirstEnt; d; d = d->Next ) { - if( strcmp(d->Name, Name) == 0 ) + if( strcmp(d->Name, Name) == 0 ) { + LOG("Return %p", &d->Inode->Node); return &d->Inode->Node; + } } + LOG("Return NULL"); return NULL; } -int RAMDisk_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) +int RAMFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) { - tRAMDisk_Dir *dir = Node->ImplPtr; - if( RAMDisk_FindDir(Node, Name) != NULL ) + tRAMFS_Dir *dir = Node->ImplPtr; + if( RAMFS_FindDir(Node, Name) != NULL ) return -1; - tRAMDisk_DirEnt *de = malloc( sizeof(tRAMDisk_DirEnt) + strlen(Name) + 1 ); + tRAMFS_DirEnt *de = malloc( sizeof(tRAMFS_DirEnt) + strlen(Name) + 1 ); de->Next = NULL; de->NameLen = strlen(Name); strcpy(de->Name, Name); if( Flags & VFS_FFLAG_DIRECTORY ) { - tRAMDisk_Dir *newdir = calloc(1, sizeof(tRAMDisk_Dir)); - newdir->Inode.Node.Type = &gRAMDisk_DirNodeType; + tRAMFS_Dir *newdir = calloc(1, sizeof(tRAMFS_Dir)); + newdir->Inode.Node.Type = &gRAMFS_DirNodeType; de->Inode = &newdir->Inode; } else { - tRAMDisk_File *newfile = calloc(1, sizeof(tRAMDisk_File)); - newfile->Inode.Node.Type = &gRAMDisk_FileNodeType; + tRAMFS_File *newfile = calloc(1, sizeof(tRAMFS_File)); + newfile->Inode.Node.Type = &gRAMFS_FileNodeType; de->Inode = &newfile->Inode; } de->Inode->Disk = dir->Inode.Disk; de->Inode->Node.Flags = Flags; de->Inode->Node.ImplPtr = de->Inode; - RAMDisk_int_RefFile(de->Inode); + RAMFS_int_RefFile(de->Inode); // TODO: Lock? if(dir->FirstEnt) @@ -177,10 +203,10 @@ int RAMDisk_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) return 0; } -int RAMDisk_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *FileNode) +int RAMFS_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *FileNode) { - tRAMDisk_Dir *dir = DirNode->ImplPtr; - tRAMDisk_DirEnt *dp; + tRAMFS_Dir *dir = DirNode->ImplPtr; + tRAMFS_DirEnt *dp; for( dp = dir->FirstEnt; dp; dp = dp->Next ) { @@ -188,10 +214,10 @@ int RAMDisk_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *FileNode) return -1; } - dp = malloc( sizeof(tRAMDisk_DirEnt) + strlen(Name) + 1 ); + dp = malloc( sizeof(tRAMFS_DirEnt) + strlen(Name) + 1 ); dp->Next = NULL; dp->Inode = FileNode->ImplPtr; - RAMDisk_int_RefFile(dp->Inode); + RAMFS_int_RefFile(dp->Inode); strcpy(dp->Name, Name); // TODO: Lock? @@ -204,13 +230,14 @@ int RAMDisk_Link(tVFS_Node *DirNode, const char *Name, tVFS_Node *FileNode) return 0; } -int RAMDisk_Unlink(tVFS_Node *Node, const char *Name) +int RAMFS_Unlink(tVFS_Node *Node, const char *Name) { - tRAMDisk_Dir *dir = Node->ImplPtr; - tRAMDisk_DirEnt *dp, *p = NULL; + tRAMFS_Dir *dir = Node->ImplPtr; + tRAMFS_DirEnt *dp, *p = NULL; // TODO: Is locking needed? + // Find the directory entry for( dp = dir->FirstEnt; dp; p = dp, dp = dp->Next ) { if( strcmp(dp->Name, Name) == 0 ) @@ -218,9 +245,11 @@ int RAMDisk_Unlink(tVFS_Node *Node, const char *Name) } if( !dp ) return -1; - RAMDisk_int_DerefFile( dp->Inode ); + // Dereference the file + RAMFS_int_DerefFile( dp->Inode ); - if( !p ) + // Remove and free directory entry + if(!p) dir->FirstEnt = dp->Next; else p->Next = dp->Next; @@ -232,7 +261,7 @@ int RAMDisk_Unlink(tVFS_Node *Node, const char *Name) } // --- Files --- -size_t _DoIO(tRAMDisk_File *File, off_t Offset, size_t Size, void *Buffer, int bRead) +size_t RAMFS_int_DoIO(tRAMFS_File *File, off_t Offset, size_t Size, void *Buffer, int bRead) { size_t rem; Uint8 *page_virt; @@ -248,7 +277,7 @@ size_t _DoIO(tRAMDisk_File *File, off_t Offset, size_t Size, void *Buffer, int b Offset %= PAGE_SIZE; rem = Size; - page_virt = _GetPage(File, page++, !bRead); + page_virt = RAMFS_int_GetPage(File, page++, !bRead); if(!page_virt) return 0; if( Offset + Size <= PAGE_SIZE ) @@ -269,8 +298,8 @@ size_t _DoIO(tRAMDisk_File *File, off_t Offset, size_t Size, void *Buffer, int b while( rem >= PAGE_SIZE ) { - _DropPage(page_virt); - page_virt = _GetPage(File, page++, !bRead); + RAMFS_int_DropPage(page_virt); + page_virt = RAMFS_int_GetPage(File, page++, !bRead); if(!page_virt) return Size - rem; if( bRead ) @@ -284,7 +313,7 @@ size_t _DoIO(tRAMDisk_File *File, off_t Offset, size_t Size, void *Buffer, int b if( rem > 0 ) { - page_virt = _GetPage(File, page, !bRead); + page_virt = RAMFS_int_GetPage(File, page, !bRead); if(!page_virt) return Size - rem; if( bRead ) memcpy(Buffer, page_virt, rem); @@ -292,27 +321,155 @@ size_t _DoIO(tRAMDisk_File *File, off_t Offset, size_t Size, void *Buffer, int b memcpy(page_virt, Buffer, rem); } - _DropPage(page_virt); + RAMFS_int_DropPage(page_virt); return Size; } -size_t RAMDisk_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer) +size_t RAMFS_Read(tVFS_Node *Node, off_t Offset, size_t Size, void *Buffer) { - tRAMDisk_File *file = Node->ImplPtr; + tRAMFS_File *file = Node->ImplPtr; - return _DoIO(file, Offset, Size, Buffer, 1); + return RAMFS_int_DoIO(file, Offset, Size, Buffer, 1); } -size_t RAMDisk_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer) +size_t RAMFS_Write(tVFS_Node *Node, off_t Offset, size_t Size, const void *Buffer) { - tRAMDisk_File *file = Node->ImplPtr; + tRAMFS_File *file = Node->ImplPtr; // TODO: Limit checks? if( Offset == file->Size ) file->Size += Size; - return _DoIO(file, Offset, Size, (void*)Buffer, 0); + return RAMFS_int_DoIO(file, Offset, Size, (void*)Buffer, 0); +} + +// --- Internals --- +void *RAMFS_int_GetPage(tRAMFS_File *File, int Page, int bCanAlloc) +{ + Uint32 page_id = 0; + Uint32 *page_in_1 = NULL; + Uint32 *page_in_2 = NULL; + + int ofs = 0, block; + + if( Page < 0 ) return NULL; + + if( Page < RAMFS_NDIRECT ) + { + if( File->PagesDirect[Page] ) + page_id = File->PagesDirect[Page]; + } + else if( Page - RAMFS_NDIRECT < PAGE_SIZE/4 ) + { + ofs = Page - RAMFS_NDIRECT; + if( File->Indirect1Page == 0 ) { + if( !bCanAlloc ) + return NULL; + else + File->Indirect1Page = RAMFS_int_AllocatePage(File->Inode.Disk) + 1; + } + page_in_1 = MM_MapTemp( File->Inode.Disk->PhysPages[File->Indirect1Page-1] ); + page_id = page_in_1[ofs]; + } + else if( Page - RAMFS_NDIRECT - PAGE_SIZE/4 < (PAGE_SIZE/4)*(PAGE_SIZE/4) ) + { + block = (Page - RAMFS_NDIRECT - PAGE_SIZE/4) / (PAGE_SIZE/4); + ofs = (Page - RAMFS_NDIRECT - PAGE_SIZE/4) % (PAGE_SIZE/4); + if( File->Indirect2Page == 0 ){ + if( !bCanAlloc ) + return NULL; + else + File->Indirect2Page = RAMFS_int_AllocatePage(File->Inode.Disk) + 1; + } + + page_in_2 = MM_MapTemp( File->Inode.Disk->PhysPages[File->Indirect2Page-1] ); + if( page_in_2[block] == 0 ) { + if( !bCanAlloc ) + return NULL; + else + page_in_2[block] = RAMFS_int_AllocatePage(File->Inode.Disk) + 1; + } + + page_in_1 = MM_MapTemp( File->Inode.Disk->PhysPages[page_in_2[block] - 1] ); + page_id = page_in_1[ofs]; + } + + if( page_id == 0 ) + { + if( !bCanAlloc ) + return NULL; + + page_id = RAMFS_int_AllocatePage(File->Inode.Disk) + 1; + if(page_in_1) + page_in_1[ofs] = page_id; + else + File->PagesDirect[Page] = page_id; + } + + MM_FreeTemp( page_in_1 ); + MM_FreeTemp( page_in_2 ); + + return MM_MapTemp( File->Inode.Disk->PhysPages[page_id - 1] ); +} + +void RAMFS_int_DropPage(void *Page) +{ + MM_FreeTemp( Page ); +} + +Uint32 RAMFS_int_AllocatePage(tRAMDisk *Disk) +{ + int i, j; + + // Quick check + if( Disk->nUsedPages == Disk->MaxPages ) + return 0; + + // Find a chunk with at least one free page + for( i = 0; i < Disk->MaxPages / 32; i ++ ) + { + if( Disk->Bitmap[i] != -1 ) + break; + } + if( i == Disk->MaxPages / 32 ) { + Log_Error("RAMFS", "Bookkeeping error, count and bitmap disagree"); + return 0; + } + + // Find the exact page + for( j = 0; j < 32; j ++ ) + { + if( !(Disk->Bitmap[i] & (1U << j)) ) + break ; + } + ASSERT(j < 32); + + return i * 32 + j; } +void RAMFS_int_RefFile(tRAMFS_Inode *Inode) +{ + Inode->nLink ++; +} + +void RAMFS_int_DerefFile(tRAMFS_Inode *Inode) +{ + Inode->nLink --; + if( Inode->nLink >= 0 ) + return ; + + Log_Error("RAMFS", "TODO: Clean up files when deleted"); + // Need to delete file + switch( Inode->Type ) + { + case 0: // File + break ; + case 1: // Directory + break ; + case 2: // Symlink + break ; + } + free(Inode); +} diff --git a/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.h b/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.h index 0765d08f..4b0ce6c8 100644 --- a/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.h +++ b/KernelLand/Modules/Filesystems/RAMDisk/ramdisk.h @@ -10,13 +10,15 @@ #include -typedef struct sRAMDisk_Inode tRAMDisk_Inode; -typedef struct sRAMDisk_File tRAMDisk_File; -typedef struct sRAMDisk_DirEnt tRAMDisk_DirEnt; -typedef struct sRAMDisk_Dir tRAMDisk_Dir; +#define RAMFS_NDIRECT 12 + +typedef struct sRAMFS_Inode tRAMFS_Inode; +typedef struct sRAMFS_File tRAMFS_File; +typedef struct sRAMFS_DirEnt tRAMFS_DirEnt; +typedef struct sRAMFS_Dir tRAMFS_Dir; typedef struct sRAMDisk tRAMDisk; -struct sRAMDisk_Inode +struct sRAMFS_Inode { tRAMDisk *Disk; tVFS_Node Node; @@ -24,35 +26,35 @@ struct sRAMDisk_Inode int nLink; }; -struct sRAMDisk_File +struct sRAMFS_File { - tRAMDisk_Inode Inode; + tRAMFS_Inode Inode; size_t Size; int nAllocPageSlots; - Uint32 PagesDirect[12]; + Uint32 PagesDirect[RAMFS_NDIRECT]; Uint32 Indirect1Page; // PAGE_SIZE/sizeof(Uint32) = 1024 on x86 Uint32 Indirect2Page; // ~1 Million on x86 }; -struct sRAMDisk_DirEnt +struct sRAMFS_DirEnt { - tRAMDisk_DirEnt *Next; - tRAMDisk_Inode *Inode; + tRAMFS_DirEnt *Next; + tRAMFS_Inode *Inode; Uint8 NameLen; char Name[]; }; -struct sRAMDisk_Dir +struct sRAMFS_Dir { - tRAMDisk_Inode Inode; + tRAMFS_Inode Inode; - tRAMDisk_DirEnt *FirstEnt; - tRAMDisk_DirEnt *LastEnt; + tRAMFS_DirEnt *FirstEnt; + tRAMFS_DirEnt *LastEnt; }; struct sRAMDisk { - tRAMDisk_Dir RootDir; + tRAMFS_Dir RootDir; int MaxPages; Uint32 *Bitmap; diff --git a/KernelLand/Modules/IPStack/routing.c b/KernelLand/Modules/IPStack/routing.c index c4b53926..3153ef26 100644 --- a/KernelLand/Modules/IPStack/routing.c +++ b/KernelLand/Modules/IPStack/routing.c @@ -20,7 +20,7 @@ extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Filename); char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos); tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name); int IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags); - int IPStack_RouteDir_Relink(tVFS_Node *Node, const char *OldName, const char *NewName); + int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName); tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric); int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric); int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data); @@ -43,7 +43,7 @@ tVFS_NodeType gIP_RouteDirNodeType = { .ReadDir = IPStack_RouteDir_ReadDir, .FindDir = IPStack_RouteDir_FindDir, .MkNod = IPStack_RouteDir_MkNod, - .Relink = IPStack_RouteDir_Relink, + .Unlink = IPStack_RouteDir_Unlink, .IOCtl = IPStack_RouteDir_IOCtl }; tVFS_Node gIP_RouteNode = { @@ -195,7 +195,7 @@ int IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags) /** * \brief Rename / Delete a route */ -int IPStack_RouteDir_Relink(tVFS_Node *Node, const char *OldName, const char *NewName) +int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName) { tRoute *rt; @@ -212,29 +212,15 @@ int IPStack_RouteDir_Relink(tVFS_Node *Node, const char *OldName, const char *Ne rt = _Route_FindExactRoute(type, addr, subnet, metric); } - if( NewName == NULL ) - { - // Delete the route - tRoute *prev = NULL; - for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next); - - if(prev) - prev->Next = rt->Next; - else - gIP_Routes = rt->Next; - free(rt); - } - else - { - // Change the route - int type = _Route_ParseRouteName(NewName, NULL, NULL, NULL); - if(type <= 0) return -EINVAL; - Uint8 addr[IPStack_GetAddressSize(type)]; - int subnet, metric; - _Route_ParseRouteName(NewName, addr, &subnet, &metric); + // Delete the route + tRoute *prev = NULL; + for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next); - return -ENOTIMPL; - } + if(prev) + prev->Next = rt->Next; + else + gIP_Routes = rt->Next; + free(rt); return 0; } diff --git a/KernelLand/Modules/USB/UHCI/uhci.c b/KernelLand/Modules/USB/UHCI/uhci.c index be27c39b..55f33ddd 100644 --- a/KernelLand/Modules/USB/UHCI/uhci.c +++ b/KernelLand/Modules/USB/UHCI/uhci.c @@ -379,8 +379,8 @@ tUHCI_TD *UHCI_int_CreateTD( else { LOG("Relocated OUT/SETUP"); - tVAddr ptr = MM_MapTemp(td->BufferPointer); - memcpy( (void*)ptr, Data, Length ); + void *ptr = MM_MapTemp(td->BufferPointer); + memcpy( ptr, Data, Length ); MM_FreeTemp(ptr); td->Control |= TD_CTL_IOC; } @@ -728,8 +728,8 @@ void UHCI_int_HandleTDComplete(tUHCI_Controller *Cont, tUHCI_TD *TD) { char *src, *dest; int src_ofs = TD->BufferPointer & (PAGE_SIZE-1); - src = (void *) MM_MapTemp(TD->BufferPointer); - dest = (void *) MM_MapTemp(info->FirstPage); + src = MM_MapTemp(TD->BufferPointer); + dest = MM_MapTemp(info->FirstPage); // Check for a single page transfer if( byte_count + info->Offset <= PAGE_SIZE ) { @@ -744,21 +744,21 @@ void UHCI_int_HandleTDComplete(tUHCI_Controller *Cont, tUHCI_TD *TD) TD->BufferPointer, info->FirstPage, info->SecondPage, TD); int part_len = PAGE_SIZE - info->Offset; memcpy(dest + info->Offset, src + src_ofs, part_len); - MM_FreeTemp( (tVAddr)dest ); - dest = (void *) MM_MapTemp(info->SecondPage); + MM_FreeTemp( dest ); + dest = MM_MapTemp(info->SecondPage); memcpy(dest, src + src_ofs + part_len, byte_count - part_len); } - MM_FreeTemp( (tVAddr)src ); - MM_FreeTemp( (tVAddr)dest ); + MM_FreeTemp( src ); + MM_FreeTemp( dest ); } // Callback if( info->Callback != NULL ) { LOG("Calling cb %p (%i bytes)", info->Callback, byte_count); - void *ptr = (void *) MM_MapTemp( TD->BufferPointer ); + void *ptr = MM_MapTemp( TD->BufferPointer ); info->Callback( info->CallbackPtr, ptr, byte_count ); - MM_FreeTemp( (tVAddr)ptr ); + MM_FreeTemp( ptr ); } // Clean up info -- 2.20.1