X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FModules%2FFilesystems%2FFAT%2Ffat.c;h=be0a51c7f20e9a0b77e37b76a43eb7b6788ed89b;hb=f0b5018caef356cda6afa147ddb6917068c62dd7;hp=07066975a2dd2bd9d943e3cae9e005b692e277cc;hpb=17be74ac4510cfe4962b1423e70c3b3a0333102f;p=tpg%2Facess2.git diff --git a/KernelLand/Modules/Filesystems/FAT/fat.c b/KernelLand/Modules/Filesystems/FAT/fat.c index 07066975..be0a51c7 100644 --- a/KernelLand/Modules/Filesystems/FAT/fat.c +++ b/KernelLand/Modules/Filesystems/FAT/fat.c @@ -17,7 +17,7 @@ * \todo Implement changing of the parent directory when a file is written to * \todo Implement file creation / deletion */ -#define DEBUG 1 +#define DEBUG 0 #define VERBOSE 1 #include @@ -27,6 +27,7 @@ // === PROTOTYPES === // --- Driver Core int FAT_Install(char **Arguments); + int FAT_Detect(int FD); tVFS_Node *FAT_InitDevice(const char *device, const char **options); void FAT_Unmount(tVFS_Node *Node); // --- Helpers @@ -43,10 +44,16 @@ void FAT_CloseFile(tVFS_Node *node); int giFAT_MaxCachedClusters = 1024*512/4; // === SEMI-GLOBALS === -MODULE_DEFINE(0, VER2(1,1) /*v1.01*/, VFAT, FAT_Install, NULL, NULL); +MODULE_DEFINE(0, VER2(0,80) /*v0.80*/, VFAT, FAT_Install, NULL, NULL); tFAT_VolInfo gFAT_Disks[8]; int giFAT_PartCount = 0; -tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL}; +tVFS_Driver gFAT_FSInfo = { + .Name = "fat", + .Detect = FAT_Detect, + .InitDevice = FAT_InitDevice, + .Unmount = FAT_Unmount, + .GetNodeFromINode = FAT_GetNodeFromINode +}; tVFS_NodeType gFAT_DirType = { .TypeName = "FAT-Dir", .ReadDir = FAT_ReadDir, @@ -78,6 +85,26 @@ int FAT_Install(char **Arguments) return MODULE_ERR_OK; } +/** + * \brief Detect if a file is a FAT device + */ +int FAT_Detect(int FD) +{ + fat_bootsect bs; + + if( VFS_ReadAt(FD, 0, 512, &bs) != 512) { + return 0; + } + + if(bs.bps == 0 || bs.spc == 0) + return 0; + + Log_Debug("FAT", "_Detect: Media type = %02x", bs.mediaDesc); + if( bs.mediaDesc < 0xF0 ) + return 0; + + return 1; +} /** * \brief Reads the boot sector of a disk and prepares the structures for it */ @@ -105,6 +132,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) if(bs->bps == 0 || bs->spc == 0) { Log_Notice("FAT", "Error in FAT Boot Sector (zero BPS/SPC)"); + VFS_Close(diskInfo->fileHandle); return NULL; } @@ -185,6 +213,7 @@ tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) diskInfo->FATCache = (Uint32*)malloc(sizeof(Uint32)*diskInfo->ClusterCount); if(diskInfo->FATCache == NULL) { Log_Warning("FAT", "Heap Exhausted"); + VFS_Cose(diskInfo->fileHandle); return NULL; } Ofs = bs->resvSectCount*512; @@ -295,7 +324,7 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu ENTER("pNode XOffset", Node, Offset); cluster = base_cluster = Node->Inode & 0xFFFFFFF; // Cluster ID - LOG("base cluster = 0x%07x", cluster); +// LOG("base cluster = 0x%07x", cluster); // Do Cluster Skip // - Pre FAT32 had a reserved area for the root. @@ -309,18 +338,18 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu if(Cluster) *Cluster = cluster; cluster = FAT_int_GetFatValue(disk, cluster); // Check for end of cluster chain - if(cluster == 0xFFFFFFFF) { LEAVE('i', 1); return 1;} + if(cluster == GETFATVALUE_EOC) { LEAVE('i', 1); return 1; } } if(Cluster) *Cluster = cluster; } else { // TODO: Bounds checking on root - LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster); +// LOG("Root cluster count %i", disk->bootsect.files_in_root*32/disk->BytesPerCluster); // Increment by clusters in offset cluster += Offset / disk->BytesPerCluster; } - LOG("cluster = 0x%07x", cluster); +// LOG("cluster = 0x%07x", cluster); // Bounds Checking (Used to spot corruption) if(cluster > disk->ClusterCount + 2) @@ -392,7 +421,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) for(i = preSkip; i--; ) { cluster = FAT_int_GetFatValue(disk, cluster); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Offset is past end of cluster chain mark"); LEAVE('i', 0); return 0; @@ -418,7 +447,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) LOG("pos = %i, Reading the rest of the clusters"); // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Read past End of Cluster Chain (Align)"); LEAVE('X', pos); return pos; @@ -436,7 +465,7 @@ size_t FAT_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer) // Read the rest of the cluster data for( ; count; count -- ) { - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "Read past End of Cluster Chain (Bulk)"); LEAVE('X', pos); return pos; @@ -483,16 +512,20 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe int remLength = Length; Uint32 cluster, tmpCluster; int bNewCluster = 0; + off_t original_offset = Offset; if(Offset > Node->Size) return 0; + ENTER("pNode Xoffset xlength pbuffer", Node, Offset, Length, Buffer); + // Seek Clusters cluster = Node->Inode & 0xFFFFFFFF; while( Offset > disk->BytesPerCluster ) { cluster = FAT_int_GetFatValue( disk, cluster ); - if(cluster == -1) { + if(cluster == GETFATVALUE_EOC) { Log_Warning("FAT", "EOC Unexpectedly Reached"); + LEAVE('i', 0); return 0; } Offset -= disk->BytesPerCluster; @@ -500,7 +533,10 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe if( Offset == disk->BytesPerCluster ) { Uint32 tmp = FAT_int_AllocateCluster(disk, cluster); - if(!tmp) return 0; + if(!tmp) { + LEAVE('i', 0); + return 0; + } cluster = tmp; Offset -= disk->BytesPerCluster; } @@ -509,17 +545,20 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe { char tmpBuf[disk->BytesPerCluster]; + LOG("Read-Modify-Write single"); + // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, Length ); FAT_int_WriteCluster( disk, cluster, tmpBuf ); - - return Length; + goto return_full; } // Clean up changes within a cluster if( Offset ) { + LOG("Read-Modify-Write first"); + // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset ); @@ -530,11 +569,10 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe // Get next cluster (allocating if needed) tmpCluster = FAT_int_GetFatValue(disk, cluster); - if(tmpCluster == -1) { + if(tmpCluster == GETFATVALUE_EOC) { tmpCluster = FAT_int_AllocateCluster(disk, cluster); - if( tmpCluster == 0 ) { - return Length - remLength; - } + if( tmpCluster == 0 ) + goto ret_incomplete; } cluster = tmpCluster; } @@ -543,28 +581,47 @@ size_t FAT_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffe { FAT_int_WriteCluster( disk, cluster, Buffer ); Buffer += disk->BytesPerCluster; + remLength -= disk->BytesPerCluster; // Get next cluster (allocating if needed) tmpCluster = FAT_int_GetFatValue(disk, cluster); - if(tmpCluster == -1) { + if(tmpCluster == GETFATVALUE_EOC) { bNewCluster = 1; tmpCluster = FAT_int_AllocateCluster(disk, cluster); - if( tmpCluster == 0 ) { - return Length - remLength; - } + if( tmpCluster == 0 ) + goto ret_incomplete; } cluster = tmpCluster; } // Finish off - if( bNewCluster ) - memset(tmpBuf, 0, disk->BytesPerCluster); - else - FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); - memcpy( tmpBuf, Buffer, remLength ); - FAT_int_WriteCluster( disk, cluster, tmpBuf ); - free( tmpBuf ); - + if( remLength ) + { + if( bNewCluster ) + memset(tmpBuf, 0, disk->BytesPerCluster); + else + FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); + memcpy( tmpBuf, Buffer, remLength ); + FAT_int_WriteCluster( disk, cluster, tmpBuf ); + } + +return_full: + if( original_offset + Length > Node->Size ) { + Node->Size = original_offset + Length; + LOG("Updated size to %x", Node->Size); + Node->ImplInt |= FAT_FLAG_DIRTY; + } + + LEAVE('i', Length); + return Length; +ret_incomplete: + LOG("Write incomplete"); + Length -= remLength; + if( original_offset + Length > Node->Size ) { + Node->Size = original_offset + Length; + Node->ImplInt |= FAT_FLAG_DIRTY; + } + LEAVE('i', Length); return Length; } #endif @@ -577,7 +634,9 @@ void FAT_CloseFile(tVFS_Node *Node) { tFAT_VolInfo *disk = Node->ImplPtr; if(Node == NULL) return ; - + + ENTER("pNode", Node); + #if SUPPORT_WRITE // Update the node if it's dirty (don't bother if it's marked for // deletion) @@ -589,6 +648,7 @@ void FAT_CloseFile(tVFS_Node *Node) dirnode = FAT_int_CreateIncompleteDirNode(disk, Node->Inode >> 32); if( !dirnode ) { Log_Error("FAT", "Can't get node for directory cluster #0x%x", Node->Inode>>32); + LEAVE('-'); return ; } @@ -602,22 +662,22 @@ void FAT_CloseFile(tVFS_Node *Node) Node->ImplInt &= ~FAT_FLAG_DIRTY; } #endif + + Uint32 cluster = Node->Inode; + Uint32 implint = Node->ImplInt; - // TODO: Make this more thread safe somehow, probably by moving the - // Inode_UncacheNode higher up and saving the cluster value somewhere - if( Node->ReferenceCount == 1 ) - { - #if SUPPORT_WRITE + #if SUPPORT_WRITE + if( FAT_int_DerefNode(Node) == 1 ) + { + LOG("implint = %x", implint); // Delete File - if( Node->ImplInt & FAT_FLAG_DELETE ) { + if( implint & FAT_FLAG_DELETE ) { + Log_Debug("FAT", "Deallocating chain stating at 0x%07x", cluster); // Since the node is marked, we only need to remove it's data - Uint32 cluster = Node->Inode & 0xFFFFFFFF; while( cluster != -1 ) - cluster = FAT_int_FreeCluster(Node->ImplPtr, cluster); + cluster = FAT_int_FreeCluster(disk, cluster); } - #endif } - - FAT_int_DerefNode(Node); - return ; + #endif + LEAVE('-'); }