2016-12-09 03:07:49 +01:00
|
|
|
/** @file
|
|
|
|
Routines that check references and flush OFiles
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2013-10-30 04:13:16 +01:00
|
|
|
Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:03:46 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
#include "Fat.h"
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
Flushes all data associated with the file handle.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param FHand - Handle to file to flush.
|
|
|
|
@param Token - A pointer to the token associated with the transaction.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - Flushed the file successfully.
|
|
|
|
@retval EFI_WRITE_PROTECTED - The volume is read only.
|
|
|
|
@retval EFI_ACCESS_DENIED - The file is read only.
|
|
|
|
@return Others - Flushing of the file failed.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
2007-12-20 10:16:45 +01:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2013-10-30 04:13:16 +01:00
|
|
|
FatFlushEx (
|
|
|
|
IN EFI_FILE_PROTOCOL *FHand,
|
|
|
|
IN EFI_FILE_IO_TOKEN *Token
|
2007-12-20 10:16:45 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
FAT_IFILE *IFile;
|
|
|
|
FAT_OFILE *OFile;
|
|
|
|
FAT_VOLUME *Volume;
|
|
|
|
EFI_STATUS Status;
|
2013-10-30 04:13:16 +01:00
|
|
|
FAT_TASK *Task;
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
IFile = IFILE_FROM_FHAND (FHand);
|
|
|
|
OFile = IFile->OFile;
|
|
|
|
Volume = OFile->Volume;
|
2013-10-30 04:13:16 +01:00
|
|
|
Task = NULL;
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// If the file has a permanent error, return it
|
|
|
|
//
|
|
|
|
if (EFI_ERROR (OFile->Error)) {
|
|
|
|
return OFile->Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Volume->ReadOnly) {
|
|
|
|
return EFI_WRITE_PROTECTED;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If read only, return error
|
|
|
|
//
|
|
|
|
if (IFile->ReadOnly) {
|
|
|
|
return EFI_ACCESS_DENIED;
|
|
|
|
}
|
2013-10-30 04:13:16 +01:00
|
|
|
|
|
|
|
if (Token == NULL) {
|
|
|
|
FatWaitNonblockingTask (IFile);
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
|
|
|
|
// But if it calls, the below check can avoid crash.
|
|
|
|
//
|
|
|
|
if (FHand->Revision < EFI_FILE_PROTOCOL_REVISION2) {
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
Task = FatCreateTask (IFile, Token);
|
|
|
|
if (Task == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-20 10:16:45 +01:00
|
|
|
//
|
|
|
|
// Flush the OFile
|
|
|
|
//
|
|
|
|
FatAcquireLock ();
|
|
|
|
Status = FatOFileFlush (OFile);
|
2013-10-30 04:13:16 +01:00
|
|
|
Status = FatCleanupVolume (OFile->Volume, OFile, Status, Task);
|
2007-12-20 10:16:45 +01:00
|
|
|
FatReleaseLock ();
|
2013-10-30 04:13:16 +01:00
|
|
|
|
|
|
|
if (Token != NULL) {
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Status = FatQueueTask (IFile, Task);
|
|
|
|
} else {
|
|
|
|
FatDestroyTask (Task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-20 10:16:45 +01:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2013-10-30 04:13:16 +01:00
|
|
|
|
|
|
|
Flushes all data associated with the file handle.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param FHand - Handle to file to flush.
|
2013-10-30 04:13:16 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - Flushed the file successfully.
|
|
|
|
@retval EFI_WRITE_PROTECTED - The volume is read only.
|
|
|
|
@retval EFI_ACCESS_DENIED - The file is read only.
|
|
|
|
@return Others - Flushing of the file failed.
|
2013-10-30 04:13:16 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
2007-12-20 10:16:45 +01:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
2016-12-09 03:07:49 +01:00
|
|
|
FatFlush (
|
2009-09-22 04:48:17 +02:00
|
|
|
IN EFI_FILE_PROTOCOL *FHand
|
2007-12-20 10:16:45 +01:00
|
|
|
)
|
2016-12-09 03:07:49 +01:00
|
|
|
{
|
|
|
|
return FatFlushEx (FHand, NULL);
|
|
|
|
}
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Flushes & Closes the file handle.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param FHand - Handle to the file to delete.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - Closed the file successfully.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
FatClose (
|
|
|
|
IN EFI_FILE_PROTOCOL *FHand
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
FAT_IFILE *IFile;
|
|
|
|
FAT_OFILE *OFile;
|
|
|
|
FAT_VOLUME *Volume;
|
|
|
|
|
|
|
|
IFile = IFILE_FROM_FHAND (FHand);
|
|
|
|
OFile = IFile->OFile;
|
|
|
|
Volume = OFile->Volume;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lock the volume
|
|
|
|
//
|
|
|
|
FatAcquireLock ();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Close the file instance handle
|
|
|
|
//
|
|
|
|
FatIFileClose (IFile);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Done. Unlock the volume
|
|
|
|
//
|
2013-10-30 04:13:16 +01:00
|
|
|
FatCleanupVolume (Volume, OFile, EFI_SUCCESS, NULL);
|
2007-12-20 10:16:45 +01:00
|
|
|
FatReleaseLock ();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Close always succeed
|
|
|
|
//
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Close the open file instance.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param IFile - Open file instance.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - Closed the file successfully.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
FatIFileClose (
|
|
|
|
FAT_IFILE *IFile
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
FAT_OFILE *OFile;
|
|
|
|
FAT_VOLUME *Volume;
|
|
|
|
|
|
|
|
OFile = IFile->OFile;
|
|
|
|
Volume = OFile->Volume;
|
|
|
|
|
|
|
|
ASSERT_VOLUME_LOCKED (Volume);
|
|
|
|
|
2013-10-30 04:13:16 +01:00
|
|
|
FatWaitNonblockingTask (IFile);
|
|
|
|
|
2007-12-20 10:16:45 +01:00
|
|
|
//
|
|
|
|
// Remove the IFile struct
|
|
|
|
//
|
|
|
|
RemoveEntryList (&IFile->Link);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add the OFile to the check reference list
|
|
|
|
//
|
|
|
|
if (OFile->CheckLink.ForwardLink == NULL) {
|
|
|
|
InsertHeadList (&Volume->CheckRef, &OFile->CheckLink);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Done. Free the open instance structure
|
|
|
|
//
|
|
|
|
FreePool (IFile);
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Flush the data associated with an open file.
|
|
|
|
In this implementation, only last Mod/Access time is updated.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param OFile - The open file.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - The OFile is flushed successfully.
|
|
|
|
@return Others - An error occurred when flushing this OFile.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
FatOFileFlush (
|
|
|
|
IN FAT_OFILE *OFile
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
FAT_OFILE *Parent;
|
|
|
|
FAT_DIRENT *DirEnt;
|
|
|
|
FAT_DATE_TIME FatNow;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Flush each entry up the tree while dirty
|
|
|
|
//
|
|
|
|
do {
|
|
|
|
//
|
2020-02-07 02:07:20 +01:00
|
|
|
// If the file has a permanent error, then don't write any
|
2007-12-20 10:16:45 +01:00
|
|
|
// of its data to the device (may be from different media)
|
|
|
|
//
|
|
|
|
if (EFI_ERROR (OFile->Error)) {
|
|
|
|
return OFile->Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Parent = OFile->Parent;
|
|
|
|
DirEnt = OFile->DirEnt;
|
|
|
|
if (OFile->Dirty) {
|
|
|
|
//
|
|
|
|
// Update the last modification time
|
|
|
|
//
|
|
|
|
FatGetCurrentFatTime (&FatNow);
|
|
|
|
CopyMem (&DirEnt->Entry.FileLastAccess, &FatNow.Date, sizeof (FAT_DATE));
|
|
|
|
if (!OFile->PreserveLastModification) {
|
|
|
|
FatGetCurrentFatTime (&DirEnt->Entry.FileModificationTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
OFile->PreserveLastModification = FALSE;
|
|
|
|
if (OFile->Archive) {
|
|
|
|
DirEnt->Entry.Attributes |= FAT_ATTRIBUTE_ARCHIVE;
|
|
|
|
OFile->Archive = FALSE;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Write the directory entry
|
|
|
|
//
|
|
|
|
if (Parent != NULL && !DirEnt->Invalid) {
|
|
|
|
//
|
|
|
|
// Write the OFile's directory entry
|
|
|
|
//
|
|
|
|
Status = FatStoreDirEnt (Parent, DirEnt);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OFile->Dirty = FALSE;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Check the parent
|
|
|
|
//
|
|
|
|
OFile = Parent;
|
|
|
|
} while (OFile != NULL);
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Check the references of the OFile.
|
|
|
|
If the OFile (that is checked) is no longer
|
|
|
|
referenced, then it is freed.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param OFile - The OFile to be checked.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval TRUE - The OFile is not referenced and freed.
|
|
|
|
@retval FALSE - The OFile is kept.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
FatCheckOFileRef (
|
|
|
|
IN FAT_OFILE *OFile
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// If the OFile is on the check ref list, remove it
|
|
|
|
//
|
|
|
|
if (OFile->CheckLink.ForwardLink != NULL) {
|
|
|
|
RemoveEntryList (&OFile->CheckLink);
|
|
|
|
OFile->CheckLink.ForwardLink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FatOFileFlush (OFile);
|
|
|
|
//
|
|
|
|
// Are there any references to this OFile?
|
|
|
|
//
|
|
|
|
if (!IsListEmpty (&OFile->Opens) || !IsListEmpty (&OFile->ChildHead)) {
|
|
|
|
//
|
|
|
|
// The OFile cannot be freed
|
|
|
|
//
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Free the Ofile
|
|
|
|
//
|
|
|
|
FatCloseDirEnt (OFile->DirEnt);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Check the references of all open files on the volume.
|
|
|
|
Any open file (that is checked) that is no longer
|
2020-02-07 02:07:20 +01:00
|
|
|
referenced, is freed - and its parent open file
|
2007-12-20 10:16:45 +01:00
|
|
|
is then referenced checked.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param Volume - The volume to check the pending open file list.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
STATIC
|
|
|
|
VOID
|
|
|
|
FatCheckVolumeRef (
|
|
|
|
IN FAT_VOLUME *Volume
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
FAT_OFILE *OFile;
|
|
|
|
FAT_OFILE *Parent;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check all files on the pending check list
|
|
|
|
//
|
|
|
|
while (!IsListEmpty (&Volume->CheckRef)) {
|
|
|
|
//
|
|
|
|
// Start with the first file listed
|
|
|
|
//
|
|
|
|
Parent = OFILE_FROM_CHECKLINK (Volume->CheckRef.ForwardLink);
|
|
|
|
//
|
|
|
|
// Go up the tree cleaning up any un-referenced OFiles
|
|
|
|
//
|
|
|
|
while (Parent != NULL) {
|
|
|
|
OFile = Parent;
|
|
|
|
Parent = OFile->Parent;
|
|
|
|
if (!FatCheckOFileRef (OFile)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
2007-12-20 10:16:45 +01:00
|
|
|
|
|
|
|
Set error status for a specific OFile, reference checking the volume.
|
|
|
|
If volume is already marked as invalid, and all resources are freed
|
|
|
|
after reference checking, the file system protocol is uninstalled and
|
|
|
|
the volume structure is freed.
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@param Volume - the Volume that is to be reference checked and unlocked.
|
|
|
|
@param OFile - the OFile whose permanent error code is to be set.
|
|
|
|
@param EfiStatus - error code to be set.
|
|
|
|
@param Task point to task instance.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
@retval EFI_SUCCESS - Clean up the volume successfully.
|
|
|
|
@return Others - Cleaning up of the volume is failed.
|
2007-12-20 10:16:45 +01:00
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
FatCleanupVolume (
|
|
|
|
IN FAT_VOLUME *Volume,
|
|
|
|
IN FAT_OFILE *OFile,
|
|
|
|
IN EFI_STATUS EfiStatus,
|
|
|
|
IN FAT_TASK *Task
|
|
|
|
)
|
2007-12-20 10:16:45 +01:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
//
|
|
|
|
// Flag the OFile
|
|
|
|
//
|
|
|
|
if (OFile != NULL) {
|
|
|
|
FatSetVolumeError (OFile, EfiStatus);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Clean up any dangling OFiles that don't have IFiles
|
|
|
|
// we don't check return status here because we want the
|
|
|
|
// volume be cleaned up even the volume is invalid.
|
|
|
|
//
|
|
|
|
FatCheckVolumeRef (Volume);
|
|
|
|
if (Volume->Valid) {
|
|
|
|
//
|
|
|
|
// Update the free hint info. Volume->FreeInfoPos != 0
|
|
|
|
// indicates this a FAT32 volume
|
|
|
|
//
|
|
|
|
if (Volume->FreeInfoValid && Volume->FatDirty && Volume->FreeInfoPos) {
|
2016-12-08 06:18:08 +01:00
|
|
|
Status = FatDiskIo (Volume, WriteDisk, Volume->FreeInfoPos, sizeof (FAT_INFO_SECTOR), &Volume->FatInfoSector, Task);
|
2007-12-20 10:16:45 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Update that the volume is not dirty
|
|
|
|
//
|
2016-12-08 06:18:08 +01:00
|
|
|
if (Volume->FatDirty && Volume->FatType != Fat12) {
|
2007-12-20 10:16:45 +01:00
|
|
|
Volume->FatDirty = FALSE;
|
2016-12-08 06:18:08 +01:00
|
|
|
Status = FatAccessVolumeDirty (Volume, WriteFat, &Volume->NotDirtyValue);
|
2007-12-20 10:16:45 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Flush all dirty cache entries to disk
|
|
|
|
//
|
2013-10-30 04:13:16 +01:00
|
|
|
Status = FatVolumeFlushCache (Volume, Task);
|
2007-12-20 10:16:45 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If the volume is cleared , remove it.
|
|
|
|
// The only time volume be invalidated is in DriverBindingStop.
|
|
|
|
//
|
|
|
|
if (Volume->Root == NULL && !Volume->Valid) {
|
|
|
|
//
|
|
|
|
// Free the volume structure
|
|
|
|
//
|
|
|
|
FatFreeVolume (Volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EfiStatus;
|
|
|
|
}
|
|
|
|
|
2016-12-09 03:07:49 +01:00
|
|
|
/**
|
|
|
|
|
|
|
|
Set the OFile and its child OFile with the error Status
|
|
|
|
|
|
|
|
@param OFile - The OFile whose permanent error code is to be set.
|
|
|
|
@param Status - Error code to be set.
|
|
|
|
|
|
|
|
**/
|
2007-12-20 10:16:45 +01:00
|
|
|
VOID
|
|
|
|
FatSetVolumeError (
|
|
|
|
IN FAT_OFILE *OFile,
|
|
|
|
IN EFI_STATUS Status
|
|
|
|
)
|
|
|
|
{
|
|
|
|
LIST_ENTRY *Link;
|
|
|
|
FAT_OFILE *ChildOFile;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If this OFile doesn't already have an error, set one
|
|
|
|
//
|
|
|
|
if (!EFI_ERROR (OFile->Error)) {
|
|
|
|
OFile->Error = Status;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Set the error on each child OFile
|
|
|
|
//
|
|
|
|
for (Link = OFile->ChildHead.ForwardLink; Link != &OFile->ChildHead; Link = Link->ForwardLink) {
|
|
|
|
ChildOFile = OFILE_FROM_CHILDLINK (Link);
|
|
|
|
FatSetVolumeError (ChildOFile, Status);
|
|
|
|
}
|
|
|
|
}
|