1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
2003-10-11 17:51:56 +00:00
|
|
|
* FILE: drivers/fs/vfat/fat.c
|
1999-12-11 21:14:49 +00:00
|
|
|
* PURPOSE: VFAT Filesystem
|
|
|
|
* PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2001-04-29 21:08:14 +00:00
|
|
|
#define NDEBUG
|
1999-12-11 21:14:49 +00:00
|
|
|
#include "vfat.h"
|
|
|
|
|
2001-07-13 10:31:14 +00:00
|
|
|
/* GLOBALS ******************************************************************/
|
|
|
|
|
2002-10-01 19:27:25 +00:00
|
|
|
#define CACHEPAGESIZE(pDeviceExt) ((pDeviceExt)->FatInfo.BytesPerCluster > PAGE_SIZE ? \
|
|
|
|
(pDeviceExt)->FatInfo.BytesPerCluster : PAGE_SIZE)
|
2001-07-13 10:31:14 +00:00
|
|
|
|
1999-12-11 21:14:49 +00:00
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
2001-01-12 21:00:08 +00:00
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT32GetNextCluster(PDEVICE_EXTENSION DeviceExt,
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG CurrentCluster,
|
|
|
|
PULONG NextCluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Retrieve the next FAT32 cluster from the FAT table via a physical
|
|
|
|
* disk read
|
|
|
|
*/
|
|
|
|
{
|
2001-02-06 00:41:19 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
ULONG FATOffset;
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG ChunkSize;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2001-10-10 22:16:46 +00:00
|
|
|
FATOffset = CurrentCluster * sizeof(ULONG);
|
|
|
|
Offset.QuadPart = ROUND_DOWN(FATOffset, ChunkSize);
|
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2003-07-24 19:00:42 +00:00
|
|
|
CurrentCluster = (*(PULONG)((char*)BaseAddress + (FATOffset % ChunkSize))) & 0x0fffffff;
|
2000-12-29 23:17:12 +00:00
|
|
|
if (CurrentCluster >= 0xffffff8 && CurrentCluster <= 0xfffffff)
|
1999-12-11 21:14:49 +00:00
|
|
|
CurrentCluster = 0xffffffff;
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2001-01-12 21:00:08 +00:00
|
|
|
*NextCluster = CurrentCluster;
|
|
|
|
return (STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 21:00:08 +00:00
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT16GetNextCluster(PDEVICE_EXTENSION DeviceExt,
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG CurrentCluster,
|
|
|
|
PULONG NextCluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
2001-03-01 07:48:17 +00:00
|
|
|
* FUNCTION: Retrieve the next FAT16 cluster from the FAT table
|
1999-12-11 21:14:49 +00:00
|
|
|
*/
|
|
|
|
{
|
2001-01-08 02:14:06 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
ULONG FATOffset;
|
2001-07-13 10:31:14 +00:00
|
|
|
ULONG ChunkSize;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
2001-10-03 18:23:02 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2001-10-10 22:16:46 +00:00
|
|
|
FATOffset = CurrentCluster * 2;
|
|
|
|
Offset.QuadPart = ROUND_DOWN(FATOffset, ChunkSize);
|
2011-05-26 20:59:48 +00:00
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, ChunkSize, MAP_WAIT, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2003-07-24 19:00:42 +00:00
|
|
|
CurrentCluster = *((PUSHORT)((char*)BaseAddress + (FATOffset % ChunkSize)));
|
2000-12-29 23:17:12 +00:00
|
|
|
if (CurrentCluster >= 0xfff8 && CurrentCluster <= 0xffff)
|
2001-01-12 21:00:08 +00:00
|
|
|
CurrentCluster = 0xffffffff;
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2001-01-12 21:00:08 +00:00
|
|
|
*NextCluster = CurrentCluster;
|
|
|
|
return (STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-12 21:00:08 +00:00
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT12GetNextCluster(PDEVICE_EXTENSION DeviceExt,
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG CurrentCluster,
|
|
|
|
PULONG NextCluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
2001-01-12 21:00:08 +00:00
|
|
|
* FUNCTION: Retrieve the next FAT12 cluster from the FAT table
|
1999-12-11 21:14:49 +00:00
|
|
|
*/
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
PUSHORT CBlock;
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG Entry;
|
|
|
|
PVOID BaseAddress;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
2001-07-20 08:00:21 +00:00
|
|
|
|
|
|
|
*NextCluster = 0;
|
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
Offset.QuadPart = 0;
|
2002-06-26 18:36:41 +00:00
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2003-07-24 19:00:42 +00:00
|
|
|
CBlock = (PUSHORT)((char*)BaseAddress + (CurrentCluster * 12) / 8);
|
2000-12-29 23:17:12 +00:00
|
|
|
if ((CurrentCluster % 2) == 0)
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock & 0x0fff;
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock >> 4;
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
2001-03-01 07:48:17 +00:00
|
|
|
// DPRINT("Entry %x\n",Entry);
|
2000-12-29 23:17:12 +00:00
|
|
|
if (Entry >= 0xff8 && Entry <= 0xfff)
|
1999-12-11 21:14:49 +00:00
|
|
|
Entry = 0xffffffff;
|
2001-03-01 07:48:17 +00:00
|
|
|
// DPRINT("Returning %x\n",Entry);
|
2001-01-12 21:00:08 +00:00
|
|
|
*NextCluster = Entry;
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2001-08-14 08:06:26 +00:00
|
|
|
// return Entry == 0xffffffff ? STATUS_END_OF_FILE : STATUS_SUCCESS;
|
|
|
|
return STATUS_SUCCESS;
|
2001-01-12 21:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT16FindAndMarkAvailableCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
PULONG Cluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Finds the first available cluster in a FAT16 table
|
|
|
|
*/
|
|
|
|
{
|
2001-01-14 15:05:53 +00:00
|
|
|
ULONG FatLength;
|
2001-11-02 22:35:31 +00:00
|
|
|
ULONG StartCluster;
|
|
|
|
ULONG i, j;
|
2001-01-12 21:00:08 +00:00
|
|
|
PVOID BaseAddress;
|
2001-07-13 10:31:14 +00:00
|
|
|
ULONG ChunkSize;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context = 0;
|
|
|
|
LARGE_INTEGER Offset;
|
2004-08-01 21:57:18 +00:00
|
|
|
PUSHORT Block;
|
|
|
|
PUSHORT BlockEnd;
|
2001-10-10 22:16:46 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = (DeviceExt->FatInfo.NumberOfClusters + 2);
|
2001-01-14 15:05:53 +00:00
|
|
|
*Cluster = 0;
|
2001-11-02 22:35:31 +00:00
|
|
|
StartCluster = DeviceExt->LastAvailableCluster;
|
2001-01-14 15:05:53 +00:00
|
|
|
|
2001-11-02 22:35:31 +00:00
|
|
|
for (j = 0; j < 2; j++)
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
for (i = StartCluster; i < FatLength; )
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
Offset.QuadPart = ROUND_DOWN(i * 2, ChunkSize);
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
2003-10-11 17:51:56 +00:00
|
|
|
{
|
2013-05-11 11:03:12 +00:00
|
|
|
DPRINT1("CcMapData(Offset %x, Length %u) failed\n", (ULONG)Offset.QuadPart, ChunkSize);
|
2004-08-01 21:57:18 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
Block = (PUSHORT)((ULONG_PTR)BaseAddress + (i * 2) % ChunkSize);
|
|
|
|
BlockEnd = (PUSHORT)((ULONG_PTR)BaseAddress + ChunkSize);
|
|
|
|
|
|
|
|
/* Now process the whole block */
|
|
|
|
while (Block < BlockEnd && i < FatLength)
|
|
|
|
{
|
|
|
|
if (*Block == 0)
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
DPRINT("Found available cluster 0x%x\n", i);
|
|
|
|
DeviceExt->LastAvailableCluster = *Cluster = i;
|
|
|
|
*Block = 0xffff;
|
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
2001-11-02 22:35:31 +00:00
|
|
|
CcUnpinData(Context);
|
2008-12-18 03:09:45 +00:00
|
|
|
if (DeviceExt->AvailableClustersValid)
|
|
|
|
InterlockedDecrement((PLONG)&DeviceExt->AvailableClusters);
|
2004-08-01 21:57:18 +00:00
|
|
|
return(STATUS_SUCCESS);
|
2001-11-02 22:35:31 +00:00
|
|
|
}
|
2004-08-01 21:57:18 +00:00
|
|
|
|
|
|
|
Block++;
|
|
|
|
i++;
|
2003-10-11 17:51:56 +00:00
|
|
|
}
|
2001-11-02 22:35:31 +00:00
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
CcUnpinData(Context);
|
|
|
|
}
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = StartCluster;
|
|
|
|
StartCluster = 2;
|
2001-10-10 22:16:46 +00:00
|
|
|
}
|
2001-01-14 15:05:53 +00:00
|
|
|
return(STATUS_DISK_FULL);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT12FindAndMarkAvailableCluster(PDEVICE_EXTENSION DeviceExt, PULONG Cluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Finds the first available cluster in a FAT12 table
|
|
|
|
*/
|
|
|
|
{
|
2001-11-02 22:35:31 +00:00
|
|
|
ULONG FatLength;
|
|
|
|
ULONG StartCluster;
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG Entry;
|
2002-03-18 22:37:13 +00:00
|
|
|
PUSHORT CBlock;
|
2001-11-02 22:35:31 +00:00
|
|
|
ULONG i, j;
|
2001-01-16 09:55:02 +00:00
|
|
|
PVOID BaseAddress;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
FatLength = DeviceExt->FatInfo.NumberOfClusters + 2;
|
2001-01-16 09:55:02 +00:00
|
|
|
*Cluster = 0;
|
2001-11-02 22:35:31 +00:00
|
|
|
StartCluster = DeviceExt->LastAvailableCluster;
|
2001-10-10 22:16:46 +00:00
|
|
|
Offset.QuadPart = 0;
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
2013-05-11 11:03:12 +00:00
|
|
|
DPRINT1("CcMapData(Offset %x, Length %u) failed\n", (ULONG)Offset.QuadPart, DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector);
|
2001-10-10 22:16:46 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2001-01-16 09:55:02 +00:00
|
|
|
|
2001-11-02 22:35:31 +00:00
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
{
|
2001-11-20 23:40:26 +00:00
|
|
|
for (i = StartCluster; i < FatLength; i++)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2003-07-24 19:00:42 +00:00
|
|
|
CBlock = (PUSHORT)((char*)BaseAddress + (i * 12) / 8);
|
2000-12-29 23:17:12 +00:00
|
|
|
if ((i % 2) == 0)
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock & 0xfff;
|
2001-11-02 22:35:31 +00:00
|
|
|
}
|
2000-12-29 23:17:12 +00:00
|
|
|
else
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock >> 4;
|
2001-11-02 22:35:31 +00:00
|
|
|
}
|
2000-12-29 23:17:12 +00:00
|
|
|
if (Entry == 0)
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
|
|
|
DPRINT("Found available cluster 0x%x\n", i);
|
|
|
|
DeviceExt->LastAvailableCluster = *Cluster = i;
|
2004-08-01 21:57:18 +00:00
|
|
|
if ((i % 2) == 0)
|
|
|
|
*CBlock = (*CBlock & 0xf000) | 0xfff;
|
|
|
|
else
|
|
|
|
*CBlock = (*CBlock & 0xf) | 0xfff0;
|
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
|
|
|
CcUnpinData(Context);
|
2008-12-18 03:09:45 +00:00
|
|
|
if (DeviceExt->AvailableClustersValid)
|
|
|
|
InterlockedDecrement((PLONG)&DeviceExt->AvailableClusters);
|
2001-11-02 22:35:31 +00:00
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
2001-11-20 23:40:26 +00:00
|
|
|
FatLength = StartCluster;
|
2001-11-02 22:35:31 +00:00
|
|
|
StartCluster = 2;
|
|
|
|
}
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2001-01-16 09:55:02 +00:00
|
|
|
return (STATUS_DISK_FULL);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS
|
2004-08-01 21:57:18 +00:00
|
|
|
FAT32FindAndMarkAvailableCluster (PDEVICE_EXTENSION DeviceExt, PULONG Cluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Finds the first available cluster in a FAT32 table
|
|
|
|
*/
|
|
|
|
{
|
2001-07-20 08:00:21 +00:00
|
|
|
ULONG FatLength;
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG StartCluster;
|
|
|
|
ULONG i, j;
|
2001-07-20 08:00:21 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
ULONG ChunkSize;
|
2004-08-01 21:57:18 +00:00
|
|
|
PVOID Context;
|
2001-10-10 22:16:46 +00:00
|
|
|
LARGE_INTEGER Offset;
|
2004-08-01 21:57:18 +00:00
|
|
|
PULONG Block;
|
|
|
|
PULONG BlockEnd;
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = (DeviceExt->FatInfo.NumberOfClusters + 2);
|
2001-07-20 08:00:21 +00:00
|
|
|
*Cluster = 0;
|
2002-03-18 22:37:13 +00:00
|
|
|
StartCluster = DeviceExt->LastAvailableCluster;
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
for (j = 0; j < 2; j++)
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
for (i = StartCluster; i < FatLength;)
|
2002-03-18 22:37:13 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
Offset.QuadPart = ROUND_DOWN(i * 4, ChunkSize);
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
2001-11-02 22:35:31 +00:00
|
|
|
{
|
2013-05-11 11:03:12 +00:00
|
|
|
DPRINT1("CcMapData(Offset %x, Length %u) failed\n", (ULONG)Offset.QuadPart, ChunkSize);
|
2004-08-01 21:57:18 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
2001-10-10 22:16:46 +00:00
|
|
|
}
|
2004-08-01 21:57:18 +00:00
|
|
|
Block = (PULONG)((ULONG_PTR)BaseAddress + (i * 4) % ChunkSize);
|
|
|
|
BlockEnd = (PULONG)((ULONG_PTR)BaseAddress + ChunkSize);
|
2002-03-18 22:37:13 +00:00
|
|
|
|
2004-08-01 21:57:18 +00:00
|
|
|
/* Now process the whole block */
|
|
|
|
while (Block < BlockEnd && i < FatLength)
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
if ((*Block & 0x0fffffff) == 0)
|
|
|
|
{
|
|
|
|
DPRINT("Found available cluster 0x%x\n", i);
|
|
|
|
DeviceExt->LastAvailableCluster = *Cluster = i;
|
|
|
|
*Block = 0x0fffffff;
|
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
|
|
|
CcUnpinData(Context);
|
2008-12-18 03:09:45 +00:00
|
|
|
if (DeviceExt->AvailableClustersValid)
|
|
|
|
InterlockedDecrement((PLONG)&DeviceExt->AvailableClusters);
|
2004-08-01 21:57:18 +00:00
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block++;
|
|
|
|
i++;
|
2001-10-10 22:16:46 +00:00
|
|
|
}
|
2004-08-01 21:57:18 +00:00
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2002-03-18 22:37:13 +00:00
|
|
|
}
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = StartCluster;
|
|
|
|
StartCluster = 2;
|
2001-10-10 22:16:46 +00:00
|
|
|
}
|
2001-01-14 15:05:53 +00:00
|
|
|
return (STATUS_DISK_FULL);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2005-10-06 21:39:18 +00:00
|
|
|
static NTSTATUS
|
2002-03-18 22:37:13 +00:00
|
|
|
FAT12CountAvailableClusters(PDEVICE_EXTENSION DeviceExt)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Counts free cluster in a FAT12 table
|
|
|
|
*/
|
|
|
|
{
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG Entry;
|
2002-03-18 22:37:13 +00:00
|
|
|
PVOID BaseAddress;
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG ulCount = 0;
|
|
|
|
ULONG i;
|
2001-03-01 07:48:17 +00:00
|
|
|
ULONG numberofclusters;
|
2001-10-10 22:16:46 +00:00
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
PVOID Context;
|
2002-03-18 22:37:13 +00:00
|
|
|
PUSHORT CBlock;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
Offset.QuadPart = 0;
|
2002-06-26 18:36:41 +00:00
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2002-03-18 22:37:13 +00:00
|
|
|
|
|
|
|
numberofclusters = DeviceExt->FatInfo.NumberOfClusters + 2;
|
2001-10-10 22:16:46 +00:00
|
|
|
|
2001-03-01 07:48:17 +00:00
|
|
|
for (i = 2; i < numberofclusters; i++)
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2003-07-24 19:00:42 +00:00
|
|
|
CBlock = (PUSHORT)((char*)BaseAddress + (i * 12) / 8);
|
2000-12-29 23:17:12 +00:00
|
|
|
if ((i % 2) == 0)
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock & 0x0fff;
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
Entry = *CBlock >> 4;
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
|
|
|
if (Entry == 0)
|
|
|
|
ulCount++;
|
|
|
|
}
|
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
CcUnpinData(Context);
|
2002-03-18 22:37:13 +00:00
|
|
|
DeviceExt->AvailableClusters = ulCount;
|
|
|
|
DeviceExt->AvailableClustersValid = TRUE;
|
2001-06-14 10:02:59 +00:00
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2005-10-06 21:39:18 +00:00
|
|
|
static NTSTATUS
|
2002-03-18 22:37:13 +00:00
|
|
|
FAT16CountAvailableClusters(PDEVICE_EXTENSION DeviceExt)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Counts free clusters in a FAT16 table
|
|
|
|
*/
|
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
PUSHORT Block;
|
|
|
|
PUSHORT BlockEnd;
|
2002-03-18 22:37:13 +00:00
|
|
|
PVOID BaseAddress = NULL;
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG ulCount = 0;
|
|
|
|
ULONG i;
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG ChunkSize;
|
|
|
|
PVOID Context = NULL;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
ULONG FatLength;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = (DeviceExt->FatInfo.NumberOfClusters + 2);
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2004-08-01 21:57:18 +00:00
|
|
|
for (i = 2; i < FatLength; )
|
2002-03-18 22:37:13 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
Offset.QuadPart = ROUND_DOWN(i * 2, ChunkSize);
|
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
2000-12-29 23:17:12 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
Block = (PUSHORT)((ULONG_PTR)BaseAddress + (i * 2) % ChunkSize);
|
|
|
|
BlockEnd = (PUSHORT)((ULONG_PTR)BaseAddress + ChunkSize);
|
|
|
|
|
|
|
|
/* Now process the whole block */
|
|
|
|
while (Block < BlockEnd && i < FatLength)
|
|
|
|
{
|
|
|
|
if (*Block == 0)
|
|
|
|
ulCount++;
|
|
|
|
Block++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
CcUnpinData(Context);
|
|
|
|
}
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
DeviceExt->AvailableClusters = ulCount;
|
|
|
|
DeviceExt->AvailableClustersValid = TRUE;
|
2001-06-14 10:02:59 +00:00
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
2001-07-20 08:00:21 +00:00
|
|
|
|
1999-12-11 21:14:49 +00:00
|
|
|
|
2005-10-06 21:39:18 +00:00
|
|
|
static NTSTATUS
|
2002-03-18 22:37:13 +00:00
|
|
|
FAT32CountAvailableClusters(PDEVICE_EXTENSION DeviceExt)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Counts free clusters in a FAT32 table
|
|
|
|
*/
|
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
PULONG Block;
|
|
|
|
PULONG BlockEnd;
|
2002-03-18 22:37:13 +00:00
|
|
|
PVOID BaseAddress = NULL;
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG ulCount = 0;
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG i;
|
|
|
|
ULONG ChunkSize;
|
|
|
|
PVOID Context = NULL;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
ULONG FatLength;
|
2000-12-29 23:17:12 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2004-08-01 21:57:18 +00:00
|
|
|
FatLength = (DeviceExt->FatInfo.NumberOfClusters + 2);
|
2001-03-01 07:48:17 +00:00
|
|
|
|
2004-08-01 21:57:18 +00:00
|
|
|
for (i = 2; i < FatLength; )
|
2002-03-18 22:37:13 +00:00
|
|
|
{
|
2004-08-01 21:57:18 +00:00
|
|
|
Offset.QuadPart = ROUND_DOWN(i * 4, ChunkSize);
|
|
|
|
if(!CcMapData(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
|
|
|
{
|
2013-05-11 11:03:12 +00:00
|
|
|
DPRINT1("CcMapData(Offset %x, Length %u) failed\n", (ULONG)Offset.QuadPart, ChunkSize);
|
2004-08-01 21:57:18 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
Block = (PULONG)((ULONG_PTR)BaseAddress + (i * 4) % ChunkSize);
|
|
|
|
BlockEnd = (PULONG)((ULONG_PTR)BaseAddress + ChunkSize);
|
|
|
|
|
|
|
|
/* Now process the whole block */
|
|
|
|
while (Block < BlockEnd && i < FatLength)
|
|
|
|
{
|
|
|
|
if ((*Block & 0x0fffffff) == 0)
|
|
|
|
ulCount++;
|
|
|
|
Block++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
CcUnpinData(Context);
|
2002-03-18 22:37:13 +00:00
|
|
|
}
|
2001-06-14 10:02:59 +00:00
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
DeviceExt->AvailableClusters = ulCount;
|
|
|
|
DeviceExt->AvailableClustersValid = TRUE;
|
2001-06-14 10:02:59 +00:00
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 22:37:13 +00:00
|
|
|
NTSTATUS
|
|
|
|
CountAvailableClusters(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
PLARGE_INTEGER Clusters)
|
|
|
|
{
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
ExAcquireResourceExclusiveLite (&DeviceExt->FatResource, TRUE);
|
|
|
|
if (!DeviceExt->AvailableClustersValid)
|
|
|
|
{
|
|
|
|
if (DeviceExt->FatInfo.FatType == FAT12)
|
|
|
|
Status = FAT12CountAvailableClusters(DeviceExt);
|
2004-12-05 16:31:51 +00:00
|
|
|
else if (DeviceExt->FatInfo.FatType == FAT16 || DeviceExt->FatInfo.FatType == FATX16)
|
2002-03-18 22:37:13 +00:00
|
|
|
Status = FAT16CountAvailableClusters(DeviceExt);
|
|
|
|
else
|
|
|
|
Status = FAT32CountAvailableClusters(DeviceExt);
|
|
|
|
}
|
|
|
|
Clusters->QuadPart = DeviceExt->AvailableClusters;
|
|
|
|
ExReleaseResourceLite (&DeviceExt->FatResource);
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS
|
2001-06-14 10:02:59 +00:00
|
|
|
FAT12WriteCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG ClusterToWrite,
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG NewValue,
|
|
|
|
PULONG OldValue)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Writes a cluster to the FAT12 physical and in-memory tables
|
|
|
|
*/
|
|
|
|
{
|
2000-12-29 23:17:12 +00:00
|
|
|
ULONG FATOffset;
|
2001-01-16 09:55:02 +00:00
|
|
|
PUCHAR CBlock;
|
|
|
|
PVOID BaseAddress;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
|
|
|
|
|
|
|
Offset.QuadPart = 0;
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, DeviceExt->FatInfo.FATSectors * DeviceExt->FatInfo.BytesPerSector, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
2001-01-16 09:55:02 +00:00
|
|
|
CBlock = (PUCHAR)BaseAddress;
|
|
|
|
|
2000-12-29 23:17:12 +00:00
|
|
|
FATOffset = (ClusterToWrite * 12) / 8;
|
2001-01-16 15:43:42 +00:00
|
|
|
DPRINT("Writing 0x%x for 0x%x at 0x%x\n",
|
2001-01-16 09:55:02 +00:00
|
|
|
NewValue, ClusterToWrite, FATOffset);
|
2000-12-29 23:17:12 +00:00
|
|
|
if ((ClusterToWrite % 2) == 0)
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
*OldValue = CBlock[FATOffset] + ((CBlock[FATOffset + 1] & 0x0f) << 8);
|
2003-07-24 19:00:42 +00:00
|
|
|
CBlock[FATOffset] = (UCHAR)NewValue;
|
2000-12-29 23:17:12 +00:00
|
|
|
CBlock[FATOffset + 1] &= 0xf0;
|
|
|
|
CBlock[FATOffset + 1] |= (NewValue & 0xf00) >> 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
*OldValue = (CBlock[FATOffset] >> 4) + (CBlock[FATOffset + 1] << 4);
|
2000-12-29 23:17:12 +00:00
|
|
|
CBlock[FATOffset] &= 0x0f;
|
|
|
|
CBlock[FATOffset] |= (NewValue & 0xf) << 4;
|
2003-07-24 19:00:42 +00:00
|
|
|
CBlock[FATOffset + 1] = (UCHAR)(NewValue >> 4);
|
2000-12-29 23:17:12 +00:00
|
|
|
}
|
|
|
|
/* Write the changed FAT sector(s) to disk */
|
2001-10-10 22:16:46 +00:00
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
|
|
|
CcUnpinData(Context);
|
2001-01-16 09:55:02 +00:00
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS
|
2001-06-14 10:02:59 +00:00
|
|
|
FAT16WriteCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG ClusterToWrite,
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG NewValue,
|
|
|
|
PULONG OldValue)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Writes a cluster to the FAT16 physical and in-memory tables
|
|
|
|
*/
|
|
|
|
{
|
2001-01-14 15:05:53 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
ULONG FATOffset;
|
2001-07-13 10:31:14 +00:00
|
|
|
ULONG ChunkSize;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
2002-03-18 22:37:13 +00:00
|
|
|
PUSHORT Cluster;
|
2001-10-10 22:16:46 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2001-10-10 22:16:46 +00:00
|
|
|
FATOffset = ClusterToWrite * 2;
|
|
|
|
Offset.QuadPart = ROUND_DOWN(FATOffset, ChunkSize);
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
DPRINT("Writing 0x%x for offset 0x%x 0x%x\n", NewValue, FATOffset,
|
2001-01-16 09:55:02 +00:00
|
|
|
ClusterToWrite);
|
2003-07-24 19:00:42 +00:00
|
|
|
Cluster = ((PUSHORT)((char*)BaseAddress + (FATOffset % ChunkSize)));
|
2002-03-18 22:37:13 +00:00
|
|
|
*OldValue = *Cluster;
|
2003-07-24 19:00:42 +00:00
|
|
|
*Cluster = (USHORT)NewValue;
|
2001-10-10 22:16:46 +00:00
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
|
|
|
CcUnpinData(Context);
|
2001-07-20 08:00:21 +00:00
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-06-14 10:02:59 +00:00
|
|
|
NTSTATUS
|
|
|
|
FAT32WriteCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG ClusterToWrite,
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG NewValue,
|
|
|
|
PULONG OldValue)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Writes a cluster to the FAT32 physical tables
|
|
|
|
*/
|
|
|
|
{
|
2001-07-20 08:00:21 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
ULONG FATOffset;
|
|
|
|
ULONG ChunkSize;
|
2001-10-10 22:16:46 +00:00
|
|
|
PVOID Context;
|
|
|
|
LARGE_INTEGER Offset;
|
2002-03-18 22:37:13 +00:00
|
|
|
PULONG Cluster;
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2002-09-08 10:23:54 +00:00
|
|
|
ChunkSize = CACHEPAGESIZE(DeviceExt);
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
FATOffset = (ClusterToWrite * 4);
|
|
|
|
Offset.QuadPart = ROUND_DOWN(FATOffset, ChunkSize);
|
2005-01-12 11:07:31 +00:00
|
|
|
if(!CcPinRead(DeviceExt->FATFileObject, &Offset, ChunkSize, 1, &Context, &BaseAddress))
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
DPRINT("Writing 0x%x for offset 0x%x 0x%x\n", NewValue, FATOffset,
|
|
|
|
ClusterToWrite);
|
2003-07-24 19:00:42 +00:00
|
|
|
Cluster = ((PULONG)((char*)BaseAddress + (FATOffset % ChunkSize)));
|
2002-03-18 22:37:13 +00:00
|
|
|
*OldValue = *Cluster & 0x0fffffff;
|
|
|
|
*Cluster = (*Cluster & 0xf0000000) | (NewValue & 0x0fffffff);
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
CcSetDirtyPinnedData(Context, NULL);
|
|
|
|
CcUnpinData(Context);
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2001-06-14 10:02:59 +00:00
|
|
|
return(STATUS_SUCCESS);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-07-20 08:00:21 +00:00
|
|
|
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS
|
2001-06-14 10:02:59 +00:00
|
|
|
WriteCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG ClusterToWrite,
|
|
|
|
ULONG NewValue)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Write a changed FAT entry
|
|
|
|
*/
|
|
|
|
{
|
2001-01-14 15:05:53 +00:00
|
|
|
NTSTATUS Status;
|
2002-03-18 22:37:13 +00:00
|
|
|
ULONG OldValue;
|
|
|
|
ExAcquireResourceExclusiveLite (&DeviceExt->FatResource, TRUE);
|
2004-08-01 21:57:18 +00:00
|
|
|
Status = DeviceExt->WriteCluster(DeviceExt, ClusterToWrite, NewValue, &OldValue);
|
2002-03-18 22:37:13 +00:00
|
|
|
if (DeviceExt->AvailableClustersValid)
|
2004-08-01 21:57:18 +00:00
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
if (OldValue && NewValue == 0)
|
2004-12-25 11:22:37 +00:00
|
|
|
InterlockedIncrement((PLONG)&DeviceExt->AvailableClusters);
|
2002-03-18 22:37:13 +00:00
|
|
|
else if (OldValue == 0 && NewValue)
|
2004-12-25 11:22:37 +00:00
|
|
|
InterlockedDecrement((PLONG)&DeviceExt->AvailableClusters);
|
2004-08-01 21:57:18 +00:00
|
|
|
}
|
2002-03-18 22:37:13 +00:00
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
2001-01-14 15:05:53 +00:00
|
|
|
return(Status);
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 02:07:18 +00:00
|
|
|
ULONGLONG
|
2001-07-20 08:00:21 +00:00
|
|
|
ClusterToSector(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG Cluster)
|
1999-12-11 21:14:49 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Converts the cluster number to a sector number for this physical
|
|
|
|
* device
|
|
|
|
*/
|
|
|
|
{
|
2002-03-18 22:37:13 +00:00
|
|
|
return DeviceExt->FatInfo.dataStart +
|
2003-01-04 02:07:18 +00:00
|
|
|
((ULONGLONG)(Cluster - 2) * DeviceExt->FatInfo.SectorsPerCluster);
|
|
|
|
|
1999-12-11 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2001-01-16 09:55:02 +00:00
|
|
|
NTSTATUS
|
2001-07-20 08:00:21 +00:00
|
|
|
GetNextCluster(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG CurrentCluster,
|
2004-08-05 02:48:18 +00:00
|
|
|
PULONG NextCluster)
|
2001-01-16 09:55:02 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Retrieve the next cluster depending on the FAT type
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
2001-10-03 18:23:02 +00:00
|
|
|
|
2007-11-21 16:50:07 +00:00
|
|
|
DPRINT ("GetNextCluster(DeviceExt %p, CurrentCluster %x)\n",
|
2002-03-18 22:37:13 +00:00
|
|
|
DeviceExt, CurrentCluster);
|
2001-10-03 18:23:02 +00:00
|
|
|
|
2004-08-05 02:48:18 +00:00
|
|
|
if (CurrentCluster == 0)
|
2004-12-05 16:31:51 +00:00
|
|
|
return(STATUS_INVALID_PARAMETER);
|
2004-08-05 02:48:18 +00:00
|
|
|
|
|
|
|
ExAcquireResourceSharedLite(&DeviceExt->FatResource, TRUE);
|
|
|
|
Status = DeviceExt->GetNextCluster(DeviceExt, CurrentCluster, NextCluster);
|
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
|
|
|
|
|
|
|
return(Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
GetNextClusterExtend(PDEVICE_EXTENSION DeviceExt,
|
|
|
|
ULONG CurrentCluster,
|
|
|
|
PULONG NextCluster)
|
|
|
|
/*
|
|
|
|
* FUNCTION: Retrieve the next cluster depending on the FAT type
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2007-11-21 16:50:07 +00:00
|
|
|
DPRINT ("GetNextClusterExtend(DeviceExt %p, CurrentCluster %x)\n",
|
2004-08-05 02:48:18 +00:00
|
|
|
DeviceExt, CurrentCluster);
|
|
|
|
|
|
|
|
ExAcquireResourceExclusiveLite(&DeviceExt->FatResource, TRUE);
|
2001-01-16 09:55:02 +00:00
|
|
|
/*
|
|
|
|
* If the file hasn't any clusters allocated then we need special
|
|
|
|
* handling
|
|
|
|
*/
|
2004-08-05 02:48:18 +00:00
|
|
|
if (CurrentCluster == 0)
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
ULONG NewCluster;
|
|
|
|
|
2004-08-01 21:57:18 +00:00
|
|
|
Status = DeviceExt->FindAndMarkAvailableCluster(DeviceExt, &NewCluster);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
*NextCluster = NewCluster;
|
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
2001-01-16 09:55:02 +00:00
|
|
|
|
2004-08-01 21:57:18 +00:00
|
|
|
Status = DeviceExt->GetNextCluster(DeviceExt, CurrentCluster, NextCluster);
|
|
|
|
|
2004-08-05 02:48:18 +00:00
|
|
|
if ((*NextCluster) == 0xFFFFFFFF)
|
2001-10-10 22:16:46 +00:00
|
|
|
{
|
|
|
|
ULONG NewCluster;
|
|
|
|
|
|
|
|
/* We are after last existing cluster, we must add one to file */
|
2004-08-01 21:57:18 +00:00
|
|
|
/* Firstly, find the next available open allocation unit and
|
|
|
|
mark it as end of file */
|
|
|
|
Status = DeviceExt->FindAndMarkAvailableCluster(DeviceExt, &NewCluster);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2001-10-10 22:16:46 +00:00
|
|
|
/* Now, write the AU of the LastCluster with the value of the newly
|
|
|
|
found AU */
|
|
|
|
WriteCluster(DeviceExt, CurrentCluster, NewCluster);
|
|
|
|
*NextCluster = NewCluster;
|
|
|
|
}
|
2001-01-16 09:55:02 +00:00
|
|
|
|
2001-07-20 08:00:21 +00:00
|
|
|
ExReleaseResourceLite(&DeviceExt->FatResource);
|
2001-01-16 09:55:02 +00:00
|
|
|
|
2001-07-20 08:00:21 +00:00
|
|
|
return(Status);
|
2001-01-16 09:55:02 +00:00
|
|
|
}
|
|
|
|
|
2001-07-13 10:31:14 +00:00
|
|
|
/* EOF */
|