X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Modules%2FFilesystems%2FFAT%2Ffat.c;h=97044ebbb4a28657c90ca86ce145a94d43aac1f9;hb=349e14ded0e8bf502cc9f672f3c6e2c6ec5f6fa1;hp=fbf4f71b9305844b11b2f59a56046ac20168f81c;hpb=6a945643557084578509e149c84cf5dde3c59c3c;p=tpg%2Facess2.git diff --git a/Modules/Filesystems/FAT/fat.c b/Modules/Filesystems/FAT/fat.c index fbf4f71b..97044ebb 100644 --- a/Modules/Filesystems/FAT/fat.c +++ b/Modules/Filesystems/FAT/fat.c @@ -40,6 +40,7 @@ typedef struct sFAT_LFNCacheEnt { int ID; + // TODO: Handle UTF16 names correctly char Data[256]; } tFAT_LFNCacheEnt; /** @@ -55,7 +56,7 @@ typedef struct sFAT_LFNCache // === PROTOTYPES === // --- Driver Core int FAT_Install(char **Arguments); -tVFS_Node *FAT_InitDevice(char *device, char **options); +tVFS_Node *FAT_InitDevice(const char *device, const char **options); void FAT_Unmount(tVFS_Node *Node); // --- Helpers int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Cluster); @@ -74,6 +75,7 @@ Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer); // --- Directory IO char *FAT_ReadDir(tVFS_Node *Node, int ID); tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name); +tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode); #if SUPPORT_WRITE int FAT_Mknod(tVFS_Node *Node, const char *Name, Uint Flags); int FAT_Relink(tVFS_Node *node, const char *OldName, const char *NewName); @@ -87,7 +89,7 @@ void FAT_CloseFile(tVFS_Node *node); MODULE_DEFINE(0, (0<<8)|50 /*v0.50*/, VFAT, FAT_Install, NULL, NULL); tFAT_VolInfo gFAT_Disks[8]; int giFAT_PartCount = 0; -tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, NULL}; +tVFS_Driver gFAT_FSInfo = {"fat", 0, FAT_InitDevice, FAT_Unmount, FAT_GetNodeFromINode, NULL}; // === CODE === /** @@ -101,10 +103,9 @@ int FAT_Install(char **Arguments) } /** - * \fn tVFS_Node *FAT_InitDevice(char *Device, char **Options) * \brief Reads the boot sector of a disk and prepares the structures for it */ -tVFS_Node *FAT_InitDevice(char *Device, char **Options) +tVFS_Node *FAT_InitDevice(const char *Device, const char **Options) { fat_bootsect *bs; int i; @@ -133,11 +134,15 @@ tVFS_Node *FAT_InitDevice(char *Device, char **Options) // - From Microsoft FAT Specifcation RootDirSectors = ((bs->files_in_root*32) + (bs->bps - 1)) / bs->bps; - if(bs->fatSz16 != 0) FATSz = bs->fatSz16; - else FATSz = bs->spec.fat32.fatSz32; + if(bs->fatSz16 != 0) + FATSz = bs->fatSz16; + else + FATSz = bs->spec.fat32.fatSz32; - if(bs->totalSect16 != 0) TotSec = bs->totalSect16; - else TotSec = bs->totalSect32; + if(bs->totalSect16 != 0) + TotSec = bs->totalSect16; + else + TotSec = bs->totalSect32; diskInfo->ClusterCount = (TotSec - (bs->resvSectCount + (bs->fatCount * FATSz) + RootDirSectors)) / bs->spc; @@ -325,8 +330,8 @@ int FAT_int_GetAddress(tVFS_Node *Node, Uint64 Offset, Uint64 *Addr, Uint32 *Clu ENTER("pNode XOffset", Node, Offset); - cluster = Node->Inode & 0xFFFFFFFF; // Cluster ID - LOG("cluster = %08x", cluster); + cluster = Node->Inode & 0xFFFFFFF; // Cluster ID + LOG("cluster = 0x%07x", cluster); // Do Cluster Skip // - Pre FAT32 had a reserved area for the root. @@ -393,7 +398,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) Uint32 val = 0; Uint32 ofs; ENTER("pDisk xCluster", Disk, cluster); - LOCK( &Disk->lFAT ); + Mutex_Acquire( &Disk->lFAT ); #if CACHE_FAT if( Disk->ClusterCount <= giFAT_MaxCachedClusters ) { @@ -420,7 +425,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) #if CACHE_FAT } #endif /*CACHE_FAT*/ - RELEASE( &Disk->lFAT ); + Mutex_Release( &Disk->lFAT ); LEAVE('x', val); return val; } @@ -610,9 +615,9 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { int preSkip, count; int i, cluster, pos; - int bpc; - void *tmpBuf; tFAT_VolInfo *disk = Node->ImplPtr; + char tmpBuf[disk->BytesPerCluster]; + int bpc = disk->BytesPerCluster; ENTER("pNode Xoffset Xlength pbuffer", Node, Offset, Length, Buffer); @@ -623,11 +628,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) return 0; } - // Calculate and Allocate Bytes Per Cluster - bpc = disk->BytesPerCluster; - tmpBuf = (void*) malloc(bpc); - if( !tmpBuf ) return 0; - // Cluster is stored in the low 32-bits of the Inode field cluster = Node->Inode & 0xFFFFFFFF; @@ -644,7 +644,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) LOG("First cluster only"); FAT_int_ReadCluster(disk, cluster, bpc, tmpBuf); memcpy( Buffer, (void*)( tmpBuf + Offset%bpc ), Length ); - free(tmpBuf); #if DEBUG //Debug_HexDump("FAT_Read", Buffer, Length); #endif @@ -658,7 +657,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { Log_Warning("FAT", "Offset is past end of cluster chain mark"); - free(tmpBuf); LEAVE('i', 0); return 0; } @@ -688,7 +686,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) #if DEBUG //Debug_HexDump("FAT_Read", Buffer, Length); #endif - free(tmpBuf); LEAVE('i', 1); return Length; } @@ -704,8 +701,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { - Warning("FAT_Read - Read past End of Cluster Chain"); - free(tmpBuf); + Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain"); LEAVE('i', 0); return 0; } @@ -717,8 +713,7 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) // Get next cluster in the chain cluster = FAT_int_GetFatValue(disk, cluster); if(cluster == -1) { - Warning("FAT_Read - Read past End of Cluster Chain"); - free(tmpBuf); + Log_Warning("FAT", "FAT_Read: Read past End of Cluster Chain"); LEAVE('i', 0); return 0; } @@ -738,7 +733,6 @@ Uint64 FAT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) //Debug_HexDump("FAT_Read", Buffer, Length); #endif - free(tmpBuf); LEAVE('X', Length); return Length; } @@ -770,7 +764,7 @@ void FAT_int_WriteCluster(tFAT_VolInfo *Disk, Uint32 Cluster, void *Buffer) Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { tFAT_VolInfo *disk = Node->ImplPtr; - void *tmpBuf; + char tmpBuf[disk->BytesPerCluster]; int remLength = Length; Uint32 cluster, tmpCluster; int bNewCluster = 0; @@ -798,29 +792,24 @@ Uint64 FAT_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) if( Offset + Length < disk->BytesPerCluster ) { - tmpBuf = malloc( disk->BytesPerCluster ); + char tmpBuf[disk->BytesPerCluster]; // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, Length ); FAT_int_WriteCluster( disk, cluster, tmpBuf ); - free(tmpBuf); return Length; } // Clean up changes within a cluster if( Offset ) - { - tmpBuf = malloc( disk->BytesPerCluster ); - + { // Read-Modify-Write FAT_int_ReadCluster( disk, cluster, disk->BytesPerCluster, tmpBuf ); memcpy( tmpBuf + Offset, Buffer, disk->BytesPerCluster - Offset ); FAT_int_WriteCluster( disk, cluster, tmpBuf ); - free(tmpBuf); - remLength -= disk->BytesPerCluster - Offset; Buffer += disk->BytesPerCluster - Offset; @@ -911,6 +900,7 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName) { char *ret; ENTER("pft sLongFileName", ft, LongFileName); + //Log_Debug("FAT", "FAT_int_CreateName(ft=%p, LongFileName=%p'%s')", ft, LongFileName); #if USE_LFN if(LongFileName && LongFileName[0] != '\0') { @@ -920,6 +910,10 @@ char *FAT_int_CreateName(fat_filetable *ft, char *LongFileName) { #endif ret = (char*) malloc(13); + if( !ret ) { + Log_Warning("FAT", "FAT_int_CreateName: malloc(13) failed"); + return NULL; + } FAT_int_ProperFilename(ret, ft->name); #if USE_LFN } @@ -941,11 +935,12 @@ tVFS_Node *FAT_int_CreateNode(tVFS_Node *Parent, fat_filetable *Entry, int Pos) tFAT_VolInfo *disk = Parent->ImplPtr; ENTER("pParent pFT", Parent, Entry); + LOG("disk = %p", disk); memset(&node, 0, sizeof(tVFS_Node)); // Set Other Data - // 0-31: Cluster, 32-63: Parent Cluster + // 0-27: Cluster, 32-59: Parent Cluster node.Inode = Entry->cluster | (Entry->clusterHi<<16) | (Parent->Inode << 32); LOG("node.Inode = %llx", node.Inode); // Position in parent directory @@ -1098,12 +1093,7 @@ int FAT_int_WriteDirEntry(tVFS_Node *Node, int ID, fat_filetable *Entry) } #endif -#if USE_LFN -// I should probably more tightly associate the LFN cache with the node -// somehow, maybe by adding a field to tVFS_Node before locking it -// Maybe .Cache or something like that (something that is free'd by the -// Inode_UncacheNode function) - +#if USE_LFN /** * \fn char *FAT_int_GetLFN(tVFS_Node *node) * \brief Return pointer to LFN cache entry @@ -1116,6 +1106,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) tFAT_LFNCache *cache; int i, firstFree; + Mutex_Acquire( &Node->Lock ); + // TODO: Thread Safety (Lock things) cache = Node->Data; @@ -1125,15 +1117,20 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->NumEntries = 1; cache->Entries[0].ID = ID; cache->Entries[0].Data[0] = '\0'; + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (new)", cache->Entries[0].Data); return cache->Entries[0].Data; } - // Scan for a current entry + // Scan for this entry firstFree = -1; for( i = 0; i < cache->NumEntries; i++ ) { - if( cache->Entries[i].ID == ID ) + if( cache->Entries[i].ID == ID ) { + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (match)", cache->Entries[i].Data); return cache->Entries[i].Data; + } if( cache->Entries[i].ID == -1 && firstFree == -1 ) firstFree = i; } @@ -1143,9 +1140,11 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) i = sizeof(tFAT_LFNCache) + (cache->NumEntries+1)*sizeof(tFAT_LFNCacheEnt); Node->Data = realloc( Node->Data, i ); if( !Node->Data ) { - Log_Error("FAT", "malloc() fail, unable to allocate %i for LFN cache", i); + Log_Error("FAT", "realloc() fail, unable to allocate %i for LFN cache", i); + Mutex_Release( &Node->Lock ); return NULL; } + //Log_Debug("FAT", "Realloc (%i)\n", i); cache = Node->Data; i = cache->NumEntries; cache->NumEntries ++; @@ -1158,7 +1157,8 @@ char *FAT_int_GetLFN(tVFS_Node *Node, int ID) cache->Entries[ i ].ID = ID; cache->Entries[ i ].Data[0] = '\0'; - //TODO: Unlock + Mutex_Release( &Node->Lock ); + //Log_Debug("FAT", "Return = %p (firstFree, i = %i)", cache->Entries[i].Data, i); return cache->Entries[ i ].Data; } @@ -1240,33 +1240,32 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) if(fileinfo[a].attrib == ATTR_LFN) { fat_longfilename *lfnInfo; - int len; lfnInfo = (fat_longfilename *) &fileinfo[a]; // Get cache for corresponding file + // > ID + Index gets the corresponding short node lfn = FAT_int_GetLFN( Node, ID + (lfnInfo->id & 0x3F) ); // Bit 6 indicates the start of an entry if(lfnInfo->id & 0x40) memset(lfn, 0, 256); - // Get the current length - len = strlen(lfn); + a = ((lfnInfo->id & 0x3F) - 1) * 13; + //Log_Debug("FAT", "ID = 0x%02x, a = %i", lfnInfo->id, a); // Sanity Check (FAT implementations should not allow >255 character names) - if(len + 13 > 255) return VFS_SKIP; - // Rebase all bytes - for(a=len+1;a--;) lfn[a+13] = lfn[a]; + if(a > 255) return VFS_SKIP; // Append new bytes - lfn[ 0] = lfnInfo->name1[0]; lfn[ 1] = lfnInfo->name1[1]; - lfn[ 2] = lfnInfo->name1[2]; lfn[ 3] = lfnInfo->name1[3]; - lfn[ 4] = lfnInfo->name1[4]; - lfn[ 5] = lfnInfo->name2[0]; lfn[ 6] = lfnInfo->name2[1]; - lfn[ 7] = lfnInfo->name2[2]; lfn[ 8] = lfnInfo->name2[3]; - lfn[ 9] = lfnInfo->name2[4]; lfn[10] = lfnInfo->name2[5]; - lfn[11] = lfnInfo->name3[0]; lfn[12] = lfnInfo->name3[1]; + lfn[a+ 0] = lfnInfo->name1[0]; lfn[a+ 1] = lfnInfo->name1[1]; + lfn[a+ 2] = lfnInfo->name1[2]; lfn[a+ 3] = lfnInfo->name1[3]; + lfn[a+ 4] = lfnInfo->name1[4]; + lfn[a+ 5] = lfnInfo->name2[0]; lfn[a+ 6] = lfnInfo->name2[1]; + lfn[a+ 7] = lfnInfo->name2[2]; lfn[a+ 8] = lfnInfo->name2[3]; + lfn[a+ 9] = lfnInfo->name2[4]; lfn[a+10] = lfnInfo->name2[5]; + lfn[a+11] = lfnInfo->name3[0]; lfn[a+12] = lfnInfo->name3[1]; LOG("lfn = '%s'", lfn); + //Log_Debug("FAT", "lfn = '%s'", lfn); LEAVE('p', VFS_SKIP); return VFS_SKIP; } @@ -1277,11 +1276,16 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) LEAVE('p', VFS_SKIP); return VFS_SKIP; } - // Ignore . and .. - if(fileinfo[a].name[0] == '.') { + // Ignore . + if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == ' ') { + LEAVE('p', VFS_SKIP); + return VFS_SKIP; + } + // and .. + if(fileinfo[a].name[0] == '.' && fileinfo[a].name[1] == '.' && fileinfo[a].name[2] == ' ') { LEAVE('p', VFS_SKIP); return VFS_SKIP; - } + } LOG("name='%c%c%c%c%c%c%c%c.%c%c%c'", fileinfo[a].name[0], fileinfo[a].name[1], fileinfo[a].name[2], fileinfo[a].name[3], @@ -1290,6 +1294,7 @@ char *FAT_ReadDir(tVFS_Node *Node, int ID) #if USE_LFN lfn = FAT_int_GetLFN(Node, ID); + //Log_Debug("FAT", "lfn = %p'%s'", lfn, lfn); ret = FAT_int_CreateName(&fileinfo[a], lfn); #else ret = FAT_int_CreateName(&fileinfo[a], NULL); @@ -1398,6 +1403,60 @@ tVFS_Node *FAT_FindDir(tVFS_Node *Node, const char *Name) return NULL; } +tVFS_Node *FAT_GetNodeFromINode(tVFS_Node *Root, Uint64 Inode) +{ + tFAT_VolInfo *disk = Root->ImplPtr; + int ents_per_sector = 512 / sizeof(fat_filetable); + fat_filetable fileinfo[ents_per_sector]; + int sector = 0, i; + tVFS_Node stub_node; + + ENTER("pRoot XInode", Root, Inode); + + stub_node.ImplPtr = disk; + stub_node.Size = -1; + stub_node.Inode = Inode >> 32; + + for( i = 0; ; i ++ ) + { + if( i == 0 || i == ents_per_sector ) + { + if(FAT_int_ReadDirSector(&stub_node, sector, fileinfo)) + { + LOG("ReadDirSector failed"); + LEAVE('n'); + return NULL; + } + i = 0; + sector ++; + } + + // Check for free/end of list + if(fileinfo[i].name[0] == '\0') break; // End of List marker + if(fileinfo[i].name[0] == '\xE5') continue; // Free entry + + if(fileinfo[i].attrib == ATTR_LFN) continue; + + LOG("fileinfo[i].cluster = %x %04x", fileinfo[i].clusterHi, fileinfo[i].cluster); + #if DEBUG + { + char tmpName[13]; + FAT_int_ProperFilename(tmpName, fileinfo[i].name); + LOG("tmpName = '%s'", tmpName); + } + #endif + + + if(fileinfo[i].cluster != (Inode & 0xFFFF)) continue; + if(fileinfo[i].clusterHi != ((Inode >> 16) & 0xFFFF)) continue; + + LEAVE_RET('p', FAT_int_CreateNode(&stub_node, &fileinfo[i], sector*ents_per_sector+i)); + } + LOG("sector = %i, i = %i", sector, i); + LEAVE('n'); + return NULL; +} + #if SUPPORT_WRITE /** * \fn int FAT_Mknod(tVFS_Node *Node, char *Name, Uint Flags)