2 Routines that check references and flush OFiles
4 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Flushes all data associated with the file handle.
16 @param FHand - Handle to file to flush.
17 @param Token - A pointer to the token associated with the transaction.
19 @retval EFI_SUCCESS - Flushed the file successfully.
20 @retval EFI_WRITE_PROTECTED - The volume is read only.
21 @retval EFI_ACCESS_DENIED - The file is read only.
22 @return Others - Flushing of the file failed.
28 IN EFI_FILE_PROTOCOL
*FHand
,
29 IN EFI_FILE_IO_TOKEN
*Token
38 IFile
= IFILE_FROM_FHAND (FHand
);
40 Volume
= OFile
->Volume
;
44 // If the file has a permanent error, return it
46 if (EFI_ERROR (OFile
->Error
)) {
50 if (Volume
->ReadOnly
) {
51 return EFI_WRITE_PROTECTED
;
54 // If read only, return error
56 if (IFile
->ReadOnly
) {
57 return EFI_ACCESS_DENIED
;
61 FatWaitNonblockingTask (IFile
);
64 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
65 // But if it calls, the below check can avoid crash.
67 if (FHand
->Revision
< EFI_FILE_PROTOCOL_REVISION2
) {
68 return EFI_UNSUPPORTED
;
70 Task
= FatCreateTask (IFile
, Token
);
72 return EFI_OUT_OF_RESOURCES
;
80 Status
= FatOFileFlush (OFile
);
81 Status
= FatCleanupVolume (OFile
->Volume
, OFile
, Status
, Task
);
85 if (!EFI_ERROR (Status
)) {
86 Status
= FatQueueTask (IFile
, Task
);
88 FatDestroyTask (Task
);
97 Flushes all data associated with the file handle.
99 @param FHand - Handle to file to flush.
101 @retval EFI_SUCCESS - Flushed the file successfully.
102 @retval EFI_WRITE_PROTECTED - The volume is read only.
103 @retval EFI_ACCESS_DENIED - The file is read only.
104 @return Others - Flushing of the file failed.
110 IN EFI_FILE_PROTOCOL
*FHand
113 return FatFlushEx (FHand
, NULL
);
118 Flushes & Closes the file handle.
120 @param FHand - Handle to the file to delete.
122 @retval EFI_SUCCESS - Closed the file successfully.
128 IN EFI_FILE_PROTOCOL
*FHand
135 IFile
= IFILE_FROM_FHAND (FHand
);
136 OFile
= IFile
->OFile
;
137 Volume
= OFile
->Volume
;
145 // Close the file instance handle
147 FatIFileClose (IFile
);
150 // Done. Unlock the volume
152 FatCleanupVolume (Volume
, OFile
, EFI_SUCCESS
, NULL
);
156 // Close always succeed
163 Close the open file instance.
165 @param IFile - Open file instance.
167 @retval EFI_SUCCESS - Closed the file successfully.
178 OFile
= IFile
->OFile
;
179 Volume
= OFile
->Volume
;
181 ASSERT_VOLUME_LOCKED (Volume
);
183 FatWaitNonblockingTask (IFile
);
186 // Remove the IFile struct
188 RemoveEntryList (&IFile
->Link
);
191 // Add the OFile to the check reference list
193 if (OFile
->CheckLink
.ForwardLink
== NULL
) {
194 InsertHeadList (&Volume
->CheckRef
, &OFile
->CheckLink
);
197 // Done. Free the open instance structure
205 Flush the data associated with an open file.
206 In this implementation, only last Mod/Access time is updated.
208 @param OFile - The open file.
210 @retval EFI_SUCCESS - The OFile is flushed successfully.
211 @return Others - An error occurred when flushing this OFile.
222 FAT_DATE_TIME FatNow
;
225 // Flush each entry up the tree while dirty
229 // If the file has a permanant error, then don't write any
230 // of its data to the device (may be from different media)
232 if (EFI_ERROR (OFile
->Error
)) {
236 Parent
= OFile
->Parent
;
237 DirEnt
= OFile
->DirEnt
;
240 // Update the last modification time
242 FatGetCurrentFatTime (&FatNow
);
243 CopyMem (&DirEnt
->Entry
.FileLastAccess
, &FatNow
.Date
, sizeof (FAT_DATE
));
244 if (!OFile
->PreserveLastModification
) {
245 FatGetCurrentFatTime (&DirEnt
->Entry
.FileModificationTime
);
248 OFile
->PreserveLastModification
= FALSE
;
249 if (OFile
->Archive
) {
250 DirEnt
->Entry
.Attributes
|= FAT_ATTRIBUTE_ARCHIVE
;
251 OFile
->Archive
= FALSE
;
254 // Write the directory entry
256 if (Parent
!= NULL
&& !DirEnt
->Invalid
) {
258 // Write the OFile's directory entry
260 Status
= FatStoreDirEnt (Parent
, DirEnt
);
261 if (EFI_ERROR (Status
)) {
266 OFile
->Dirty
= FALSE
;
272 } while (OFile
!= NULL
);
278 Check the references of the OFile.
279 If the OFile (that is checked) is no longer
280 referenced, then it is freed.
282 @param OFile - The OFile to be checked.
284 @retval TRUE - The OFile is not referenced and freed.
285 @retval FALSE - The OFile is kept.
294 // If the OFile is on the check ref list, remove it
296 if (OFile
->CheckLink
.ForwardLink
!= NULL
) {
297 RemoveEntryList (&OFile
->CheckLink
);
298 OFile
->CheckLink
.ForwardLink
= NULL
;
301 FatOFileFlush (OFile
);
303 // Are there any references to this OFile?
305 if (!IsListEmpty (&OFile
->Opens
) || !IsListEmpty (&OFile
->ChildHead
)) {
307 // The OFile cannot be freed
314 FatCloseDirEnt (OFile
->DirEnt
);
320 Check the references of all open files on the volume.
321 Any open file (that is checked) that is no longer
322 referenced, is freed - and it's parent open file
323 is then referenced checked.
325 @param Volume - The volume to check the pending open file list.
331 IN FAT_VOLUME
*Volume
338 // Check all files on the pending check list
340 while (!IsListEmpty (&Volume
->CheckRef
)) {
342 // Start with the first file listed
344 Parent
= OFILE_FROM_CHECKLINK (Volume
->CheckRef
.ForwardLink
);
346 // Go up the tree cleaning up any un-referenced OFiles
348 while (Parent
!= NULL
) {
350 Parent
= OFile
->Parent
;
351 if (!FatCheckOFileRef (OFile
)) {
360 Set error status for a specific OFile, reference checking the volume.
361 If volume is already marked as invalid, and all resources are freed
362 after reference checking, the file system protocol is uninstalled and
363 the volume structure is freed.
365 @param Volume - the Volume that is to be reference checked and unlocked.
366 @param OFile - the OFile whose permanent error code is to be set.
367 @param EfiStatus - error code to be set.
368 @param Task point to task instance.
370 @retval EFI_SUCCESS - Clean up the volume successfully.
371 @return Others - Cleaning up of the volume is failed.
376 IN FAT_VOLUME
*Volume
,
378 IN EFI_STATUS EfiStatus
,
387 FatSetVolumeError (OFile
, EfiStatus
);
390 // Clean up any dangling OFiles that don't have IFiles
391 // we don't check return status here because we want the
392 // volume be cleaned up even the volume is invalid.
394 FatCheckVolumeRef (Volume
);
397 // Update the free hint info. Volume->FreeInfoPos != 0
398 // indicates this a FAT32 volume
400 if (Volume
->FreeInfoValid
&& Volume
->FatDirty
&& Volume
->FreeInfoPos
) {
401 Status
= FatDiskIo (Volume
, WriteDisk
, Volume
->FreeInfoPos
, sizeof (FAT_INFO_SECTOR
), &Volume
->FatInfoSector
, Task
);
402 if (EFI_ERROR (Status
)) {
407 // Update that the volume is not dirty
409 if (Volume
->FatDirty
&& Volume
->FatType
!= Fat12
) {
410 Volume
->FatDirty
= FALSE
;
411 Status
= FatAccessVolumeDirty (Volume
, WriteFat
, &Volume
->NotDirtyValue
);
412 if (EFI_ERROR (Status
)) {
417 // Flush all dirty cache entries to disk
419 Status
= FatVolumeFlushCache (Volume
, Task
);
420 if (EFI_ERROR (Status
)) {
425 // If the volume is cleared , remove it.
426 // The only time volume be invalidated is in DriverBindingStop.
428 if (Volume
->Root
== NULL
&& !Volume
->Valid
) {
430 // Free the volume structure
432 FatFreeVolume (Volume
);
440 Set the OFile and its child OFile with the error Status
442 @param OFile - The OFile whose permanent error code is to be set.
443 @param Status - Error code to be set.
453 FAT_OFILE
*ChildOFile
;
456 // If this OFile doesn't already have an error, set one
458 if (!EFI_ERROR (OFile
->Error
)) {
459 OFile
->Error
= Status
;
462 // Set the error on each child OFile
464 for (Link
= OFile
->ChildHead
.ForwardLink
; Link
!= &OFile
->ChildHead
; Link
= Link
->ForwardLink
) {
465 ChildOFile
= OFILE_FROM_CHILDLINK (Link
);
466 FatSetVolumeError (ChildOFile
, Status
);