2005-08-24 18:29:45 +00:00
|
|
|
/*
|
2000-12-29 13:45:01 +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/shutdown.c
|
2000-12-29 13:45:01 +00:00
|
|
|
* PURPOSE: VFAT Filesystem
|
2010-01-09 22:43:16 +00:00
|
|
|
* PROGRAMMER: Eric Kohl
|
2000-12-29 13:45:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include "vfat.h"
|
|
|
|
|
2013-12-19 16:20:28 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2000-12-29 13:45:01 +00:00
|
|
|
/* FUNCTIONS ****************************************************************/
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
VfatDiskShutDown(
|
|
|
|
PVCB Vcb)
|
2005-02-16 15:27:30 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
PIRP Irp;
|
|
|
|
KEVENT Event;
|
|
|
|
NTSTATUS Status;
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(IRP_MJ_SHUTDOWN, Vcb->StorageDevice,
|
|
|
|
NULL, 0, NULL, &Event, &IoStatus);
|
|
|
|
if (Irp)
|
|
|
|
{
|
|
|
|
Status = IoCallDriver(Vcb->StorageDevice, Irp);
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
|
|
|
|
Status = IoStatus.Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-06 11:48:42 +00:00
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
2013-12-09 10:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
2005-02-16 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
VfatShutdown(
|
|
|
|
PDEVICE_OBJECT DeviceObject,
|
|
|
|
PIRP Irp)
|
2000-12-29 13:45:01 +00:00
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PLIST_ENTRY ListEntry;
|
|
|
|
PDEVICE_EXTENSION DeviceExt;
|
|
|
|
|
|
|
|
DPRINT("VfatShutdown(DeviceObject %p, Irp %p)\n",DeviceObject, Irp);
|
|
|
|
|
|
|
|
FsRtlEnterFileSystem();
|
|
|
|
|
|
|
|
/* FIXME: block new mount requests */
|
2018-08-19 07:55:38 +00:00
|
|
|
VfatGlobalData->ShutdownStarted = TRUE;
|
2013-12-09 10:35:15 +00:00
|
|
|
|
|
|
|
if (DeviceObject == VfatGlobalData->DeviceObject)
|
|
|
|
{
|
|
|
|
Irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
ExAcquireResourceExclusiveLite(&VfatGlobalData->VolumeListLock, TRUE);
|
|
|
|
ListEntry = VfatGlobalData->VolumeListHead.Flink;
|
|
|
|
while (ListEntry != &VfatGlobalData->VolumeListHead)
|
|
|
|
{
|
|
|
|
DeviceExt = CONTAINING_RECORD(ListEntry, VCB, VolumeListEntry);
|
|
|
|
ListEntry = ListEntry->Flink;
|
|
|
|
|
|
|
|
ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
|
[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
|
|
|
|
|
|
|
/* Flush volume & files */
|
|
|
|
Status = VfatFlushVolume(DeviceExt, DeviceExt->VolumeFcb);
|
|
|
|
|
|
|
|
/* We're performing a clean shutdown */
|
|
|
|
if (BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_CLEAR_DIRTY) &&
|
|
|
|
BooleanFlagOn(DeviceExt->VolumeFcb->Flags, VCB_IS_DIRTY))
|
2013-12-09 10:35:15 +00:00
|
|
|
{
|
[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
|
|
|
/* Drop the dirty bit */
|
[FASTFAT] Completely rewrite support for dirty volumes.
Until now, our support for dirty volumes was totally broken
to a point where, on FAT32 volume, the dirty couldn't even
be written nor read from the disk.
This commit totally rewrites its handling, for both FAT16 and FAT32
so that it's now fully functionnal. Furthermore, it also gets
totally compatible with our vfatlib, and thus, autochk.
Now, on mount, FastFAT will check if the volume is dirty or not, and
autochk will be able to ask for a repair if dirty. vfatlib will
repair the volume and remove the dirty bit. So that, on next
reboot, the volume will be mounted clean.
As a reminder, the dirty bit is set immediately after mounting
the volume, so that, if you crash or have a powercut, autochk
will always attempt to repair your volume (with more or less,
that's FAT!).
If you want to experience without breaking your FAT volume,
just boot, open a cmd prompt and type: fsutil dirty set c:
and reboot!
CORE-13758
CORE-13760
CORE-13759
2018-05-18 21:00:13 +00:00
|
|
|
if (NT_SUCCESS(SetDirtyStatus(DeviceExt, FALSE)))
|
|
|
|
DeviceExt->VolumeFcb->Flags &= ~VCB_IS_DIRTY;
|
2013-12-09 10:35:15 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 16:04:38 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
2013-12-09 10:35:15 +00:00
|
|
|
Status = VfatDiskShutDown(DeviceExt);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT1("VfatDiskShutDown failed, status = %x\n", Status);
|
|
|
|
}
|
2005-04-16 16:04:38 +00:00
|
|
|
}
|
2013-12-09 10:35:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("VfatFlushVolume failed, status = %x\n", Status);
|
|
|
|
}
|
|
|
|
ExReleaseResourceLite(&DeviceExt->DirResource);
|
2005-02-16 15:27:30 +00:00
|
|
|
|
[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
|
|
|
/* Unmount the logical volume */
|
|
|
|
#ifdef ENABLE_SWAPOUT
|
|
|
|
VfatCheckForDismount(DeviceExt, FALSE);
|
|
|
|
#endif
|
2005-02-16 15:27:30 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
}
|
|
|
|
ExReleaseResourceLite(&VfatGlobalData->VolumeListLock);
|
2005-05-08 02:16:32 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
/* FIXME: Free all global acquired resources */
|
2003-02-09 18:02:56 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Status = Irp->IoStatus.Status;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
|
|
|
|
Status = STATUS_INVALID_DEVICE_REQUEST;
|
|
|
|
}
|
2000-12-29 13:45:01 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
Irp->IoStatus.Information = 0;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2005-02-16 15:27:30 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
FsRtlExitFileSystem();
|
2008-01-12 15:20:22 +00:00
|
|
|
|
2013-12-09 10:35:15 +00:00
|
|
|
return Status;
|
2000-12-29 13:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|