reactos/drivers/filesystems/fastfat/cleanup.c

197 lines
6.1 KiB
C
Raw Normal View History

/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: drivers/fs/vfat/cleanup.c
* PURPOSE: VFAT Filesystem
* PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
* Pierre Schweitzer (pierre@reactos.org)
*/
/* INCLUDES *****************************************************************/
#include "vfat.h"
#define NDEBUG
#include <debug.h>
/* FUNCTIONS ****************************************************************/
/*
* FUNCTION: Cleans up after a file has been closed.
*/
static
NTSTATUS
VfatCleanupFile(
PVFAT_IRP_CONTEXT IrpContext)
{
PVFATFCB pFcb;
PVFATCCB pCcb;
BOOLEAN IsVolume;
PDEVICE_EXTENSION DeviceExt = IrpContext->DeviceExt;
PFILE_OBJECT FileObject = IrpContext->FileObject;
DPRINT("VfatCleanupFile(DeviceExt %p, FileObject %p)\n",
IrpContext->DeviceExt, FileObject);
/* FIXME: handle file/directory deletion here */
pFcb = (PVFATFCB)FileObject->FsContext;
if (!pFcb)
return STATUS_SUCCESS;
IsVolume = BooleanFlagOn(pFcb->Flags, FCB_IS_VOLUME);
if (IsVolume)
{
pFcb->OpenHandleCount--;
DeviceExt->OpenHandleCount--;
if (pFcb->OpenHandleCount != 0)
{
IoRemoveShareAccess(FileObject, &pFcb->FCBShareAccess);
}
}
else
{
ExAcquireResourceExclusiveLite(&pFcb->MainResource, TRUE);
ExAcquireResourceExclusiveLite(&pFcb->PagingIoResource, TRUE);
pCcb = FileObject->FsContext2;
if (BooleanFlagOn(pCcb->Flags, CCB_DELETE_ON_CLOSE))
{
pFcb->Flags |= FCB_DELETE_PENDING;
}
/* Notify about the cleanup */
FsRtlNotifyCleanup(IrpContext->DeviceExt->NotifySync,
&(IrpContext->DeviceExt->NotifyList),
FileObject->FsContext2);
pFcb->OpenHandleCount--;
DeviceExt->OpenHandleCount--;
if (!vfatFCBIsDirectory(pFcb) &&
FsRtlAreThereCurrentFileLocks(&pFcb->FileLock))
{
/* remove all locks this process have on this file */
FsRtlFastUnlockAll(&pFcb->FileLock,
FileObject,
IoGetRequestorProcess(IrpContext->Irp),
NULL);
}
if (BooleanFlagOn(pFcb->Flags, FCB_IS_DIRTY))
{
VfatUpdateEntry (DeviceExt, pFcb);
}
if (BooleanFlagOn(pFcb->Flags, FCB_DELETE_PENDING) &&
pFcb->OpenHandleCount == 0)
{
if (vfatFCBIsDirectory(pFcb) &&
!VfatIsDirectoryEmpty(DeviceExt, pFcb))
{
pFcb->Flags &= ~FCB_DELETE_PENDING;
}
else
{
PFILE_OBJECT tmpFileObject;
tmpFileObject = pFcb->FileObject;
if (tmpFileObject != NULL)
{
pFcb->FileObject = NULL;
CcUninitializeCacheMap(tmpFileObject, NULL, NULL);
ClearFlag(pFcb->Flags, FCB_CACHE_INITIALIZED);
ObDereferenceObject(tmpFileObject);
}
pFcb->RFCB.ValidDataLength.QuadPart = 0;
pFcb->RFCB.FileSize.QuadPart = 0;
pFcb->RFCB.AllocationSize.QuadPart = 0;
}
}
/* Uninitialize the cache (should be done even if caching was never initialized) */
CcUninitializeCacheMap(FileObject, &pFcb->RFCB.FileSize, NULL);
if (BooleanFlagOn(pFcb->Flags, FCB_DELETE_PENDING) &&
pFcb->OpenHandleCount == 0)
{
VfatDelEntry(DeviceExt, pFcb, NULL);
vfatReportChange(DeviceExt,
pFcb,
(vfatFCBIsDirectory(pFcb) ?
FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME),
FILE_ACTION_REMOVED);
}
if (pFcb->OpenHandleCount != 0)
{
IoRemoveShareAccess(FileObject, &pFcb->FCBShareAccess);
}
/* FIXME: causes FS corruption and breaks selfhosting/testbots and so on */
#if 0
/* If that's the last open handle we just closed, try to see whether
* we can delay close operation
*/
else if (!BooleanFlagOn(pFcb->Flags, FCB_DELETE_PENDING) && !BooleanFlagOn(pFcb->Flags, FCB_IS_PAGE_FILE) &&
!BooleanFlagOn(pFcb->Flags, FCB_IS_FAT) && !BooleanFlagOn(pFcb->Flags, FCB_IS_VOLUME))
{
/* This is only allowed if that's a directory with no open files
* OR if it's a file with no section opened
*/
if ((vfatFCBIsDirectory(pFcb) && IsListEmpty(&pFcb->ParentListHead)) ||
(!vfatFCBIsDirectory(pFcb) && FileObject->SectionObjectPointer->DataSectionObject == NULL &&
FileObject->SectionObjectPointer->ImageSectionObject == NULL))
{
DPRINT("Delaying close of: %wZ\n", &pFcb->PathNameU);
SetFlag(pFcb->Flags, FCB_DELAYED_CLOSE);
}
}
#endif
FileObject->Flags |= FO_CLEANUP_COMPLETE;
#ifdef KDBG
pFcb->Flags |= FCB_CLEANED_UP;
#endif
ExReleaseResourceLite(&pFcb->PagingIoResource);
ExReleaseResourceLite(&pFcb->MainResource);
}
#ifdef ENABLE_SWAPOUT
if (IsVolume && BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
{
[FASTFAT] Improvements for volume dismount + minor bugfixing. - Cache the RootFcb so that its cleanup can be handled separately during dismounting. - Force volume dismount at cleanup if the VCB_DISMOUNT_PENDING flag is set. - Actually dismount a volume if its VCB has been flagged as not good, or if we force dismounting. NOTE: In their *CheckForDismount() function, our 3rd-party FS drivers as well as MS' fastfat, perform a comparison check of the current VCB's VPB ReferenceCount with some sort of "dangling"/"residual" open count. It seems to be related to the fact that the volume root directory as well as auxiliary data stream(s) are still opened, and only these are allowed to be opened at that moment. After analysis it appears that for the ReactOS' fastfat, this number is equal to "3". - On dismounting, cleanup and destroy the RootFcb, VolumeFcb and the FATFileObject. Then cleanup the SpareVPB or the IoVPB members, and finish by removing the dismounted volume from the VolumeListEntry and cleaning up the notify synchronization object and the resources. - During dismounting, and on shutdown, flush the volume before resetting its dirty bit. - On shutdown, after volume flushing, try to unmount it without forcing. - Release the VCB resources only when we actually dismount the volume in VfatCheckForDismount(). - Initialize first the notify list and the synchronization object, before sending the FSRTL_VOLUME_MOUNT notification. - If we failed at mounting a volume but its VCB's FATFileObject was already initialized, first call CcUninitializeCacheMap() on it before dereferencing it. - Send FSRTL_VOLUME_LOCK, FSRTL_VOLUME_LOCK_FAILED and FSRTL_VOLUME_UNLOCK notifications during volume locking (and failure) and volume unlocking. - Flush the volume before locking it, and clean its dirty bit if needed. NOTE: In addition to checking for VCB_CLEAR_DIRTY, we also check for the presence of the VCB_IS_DIRTY flag before cleaning up the dirty bit: this allows us to not re-clean the bit if it has been previously cleaned. This is needed for instance in this scenario: - The volume is locked (it gets flushed and the dirty bit is possibly cleared); - The volume then gets formatted with a completely different FS, that possibly clears up the first sector (e.g. BTRFS ignores 1st sector); - The volume is then dismounted: if we didn't check whether VCB_IS_DIRTY was set prior to resetting it, we could attempt clearing it again! But now that the volume's filesystem has been completely changed, we would then try to modify the dirty bit on an erroneous position on disk! That's why it should not be touched in this case during dismounting. - The volume is unlocked (same comment as above), and later can be detected as being BTRFS.
2018-11-11 16:17:48 +00:00
VfatCheckForDismount(DeviceExt, TRUE);
}
#endif
return STATUS_SUCCESS;
}
/*
* FUNCTION: Cleans up after a file has been closed.
*/
NTSTATUS
VfatCleanup(
PVFAT_IRP_CONTEXT IrpContext)
{
NTSTATUS Status;
DPRINT("VfatCleanup(DeviceObject %p, Irp %p)\n", IrpContext->DeviceObject, IrpContext->Irp);
if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
{
IrpContext->Irp->IoStatus.Information = 0;
return STATUS_SUCCESS;
}
ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
Status = VfatCleanupFile(IrpContext);
ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
IrpContext->Irp->IoStatus.Information = 0;
return Status;
}
/* EOF */