]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/Flush.c
BaseTools: Library hashing fix and optimization for --hash feature
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Flush.c
CommitLineData
cae7420b
DB
1/** @file\r
2 Routines that check references and flush OFiles\r
b9ec9330 3\r
149d6335 4Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r
eb6cb4ce 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
b9ec9330
QH
6\r
7\r
cae7420b 8**/\r
b9ec9330 9\r
cae7420b 10#include "Fat.h"\r
b9ec9330 11\r
cae7420b 12/**\r
b9ec9330 13\r
cae7420b 14 Flushes all data associated with the file handle.\r
b9ec9330 15\r
cae7420b
DB
16 @param FHand - Handle to file to flush.\r
17 @param Token - A pointer to the token associated with the transaction.\r
b9ec9330 18\r
cae7420b
DB
19 @retval EFI_SUCCESS - Flushed the file successfully.\r
20 @retval EFI_WRITE_PROTECTED - The volume is read only.\r
21 @retval EFI_ACCESS_DENIED - The file is read only.\r
22 @return Others - Flushing of the file failed.\r
b9ec9330 23\r
cae7420b 24**/\r
b9ec9330
QH
25EFI_STATUS\r
26EFIAPI\r
149d6335
RN
27FatFlushEx (\r
28 IN EFI_FILE_PROTOCOL *FHand,\r
29 IN EFI_FILE_IO_TOKEN *Token\r
b9ec9330 30 )\r
b9ec9330
QH
31{\r
32 FAT_IFILE *IFile;\r
33 FAT_OFILE *OFile;\r
34 FAT_VOLUME *Volume;\r
35 EFI_STATUS Status;\r
149d6335 36 FAT_TASK *Task;\r
b9ec9330
QH
37\r
38 IFile = IFILE_FROM_FHAND (FHand);\r
39 OFile = IFile->OFile;\r
40 Volume = OFile->Volume;\r
149d6335 41 Task = NULL;\r
b9ec9330
QH
42\r
43 //\r
44 // If the file has a permanent error, return it\r
45 //\r
46 if (EFI_ERROR (OFile->Error)) {\r
47 return OFile->Error;\r
48 }\r
49\r
50 if (Volume->ReadOnly) {\r
51 return EFI_WRITE_PROTECTED;\r
52 }\r
53 //\r
54 // If read only, return error\r
55 //\r
56 if (IFile->ReadOnly) {\r
57 return EFI_ACCESS_DENIED;\r
58 }\r
149d6335
RN
59\r
60 if (Token == NULL) {\r
61 FatWaitNonblockingTask (IFile);\r
62 } else {\r
63 //\r
64 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.\r
65 // But if it calls, the below check can avoid crash.\r
66 //\r
67 if (FHand->Revision < EFI_FILE_PROTOCOL_REVISION2) {\r
68 return EFI_UNSUPPORTED;\r
69 }\r
70 Task = FatCreateTask (IFile, Token);\r
71 if (Task == NULL) {\r
72 return EFI_OUT_OF_RESOURCES;\r
73 }\r
74 }\r
75\r
b9ec9330
QH
76 //\r
77 // Flush the OFile\r
78 //\r
79 FatAcquireLock ();\r
80 Status = FatOFileFlush (OFile);\r
149d6335 81 Status = FatCleanupVolume (OFile->Volume, OFile, Status, Task);\r
b9ec9330 82 FatReleaseLock ();\r
149d6335
RN
83\r
84 if (Token != NULL) {\r
85 if (!EFI_ERROR (Status)) {\r
86 Status = FatQueueTask (IFile, Task);\r
87 } else {\r
88 FatDestroyTask (Task);\r
89 }\r
90 }\r
91\r
b9ec9330
QH
92 return Status;\r
93}\r
94\r
cae7420b 95/**\r
149d6335
RN
96\r
97 Flushes all data associated with the file handle.\r
98\r
cae7420b 99 @param FHand - Handle to file to flush.\r
149d6335 100\r
cae7420b
DB
101 @retval EFI_SUCCESS - Flushed the file successfully.\r
102 @retval EFI_WRITE_PROTECTED - The volume is read only.\r
103 @retval EFI_ACCESS_DENIED - The file is read only.\r
104 @return Others - Flushing of the file failed.\r
149d6335 105\r
cae7420b 106**/\r
b9ec9330
QH
107EFI_STATUS\r
108EFIAPI\r
cae7420b 109FatFlush (\r
dba03ba1 110 IN EFI_FILE_PROTOCOL *FHand\r
b9ec9330 111 )\r
cae7420b
DB
112{\r
113 return FatFlushEx (FHand, NULL);\r
114}\r
b9ec9330 115\r
cae7420b 116/**\r
b9ec9330
QH
117\r
118 Flushes & Closes the file handle.\r
119\r
cae7420b 120 @param FHand - Handle to the file to delete.\r
b9ec9330 121\r
cae7420b 122 @retval EFI_SUCCESS - Closed the file successfully.\r
b9ec9330 123\r
cae7420b
DB
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127FatClose (\r
128 IN EFI_FILE_PROTOCOL *FHand\r
129 )\r
b9ec9330
QH
130{\r
131 FAT_IFILE *IFile;\r
132 FAT_OFILE *OFile;\r
133 FAT_VOLUME *Volume;\r
134\r
135 IFile = IFILE_FROM_FHAND (FHand);\r
136 OFile = IFile->OFile;\r
137 Volume = OFile->Volume;\r
138\r
139 //\r
140 // Lock the volume\r
141 //\r
142 FatAcquireLock ();\r
143\r
144 //\r
145 // Close the file instance handle\r
146 //\r
147 FatIFileClose (IFile);\r
148\r
149 //\r
150 // Done. Unlock the volume\r
151 //\r
149d6335 152 FatCleanupVolume (Volume, OFile, EFI_SUCCESS, NULL);\r
b9ec9330
QH
153 FatReleaseLock ();\r
154\r
155 //\r
156 // Close always succeed\r
157 //\r
158 return EFI_SUCCESS;\r
159}\r
160\r
cae7420b 161/**\r
b9ec9330
QH
162\r
163 Close the open file instance.\r
164\r
cae7420b 165 @param IFile - Open file instance.\r
b9ec9330 166\r
cae7420b 167 @retval EFI_SUCCESS - Closed the file successfully.\r
b9ec9330 168\r
cae7420b
DB
169**/\r
170EFI_STATUS\r
171FatIFileClose (\r
172 FAT_IFILE *IFile\r
173 )\r
b9ec9330
QH
174{\r
175 FAT_OFILE *OFile;\r
176 FAT_VOLUME *Volume;\r
177\r
178 OFile = IFile->OFile;\r
179 Volume = OFile->Volume;\r
180\r
181 ASSERT_VOLUME_LOCKED (Volume);\r
182\r
149d6335
RN
183 FatWaitNonblockingTask (IFile);\r
184\r
b9ec9330
QH
185 //\r
186 // Remove the IFile struct\r
187 //\r
188 RemoveEntryList (&IFile->Link);\r
189\r
190 //\r
191 // Add the OFile to the check reference list\r
192 //\r
193 if (OFile->CheckLink.ForwardLink == NULL) {\r
194 InsertHeadList (&Volume->CheckRef, &OFile->CheckLink);\r
195 }\r
196 //\r
197 // Done. Free the open instance structure\r
198 //\r
199 FreePool (IFile);\r
200 return EFI_SUCCESS;\r
201}\r
202\r
cae7420b 203/**\r
b9ec9330
QH
204\r
205 Flush the data associated with an open file.\r
206 In this implementation, only last Mod/Access time is updated.\r
207\r
cae7420b 208 @param OFile - The open file.\r
b9ec9330 209\r
cae7420b
DB
210 @retval EFI_SUCCESS - The OFile is flushed successfully.\r
211 @return Others - An error occurred when flushing this OFile.\r
b9ec9330 212\r
cae7420b
DB
213**/\r
214EFI_STATUS\r
215FatOFileFlush (\r
216 IN FAT_OFILE *OFile\r
217 )\r
b9ec9330
QH
218{\r
219 EFI_STATUS Status;\r
220 FAT_OFILE *Parent;\r
221 FAT_DIRENT *DirEnt;\r
222 FAT_DATE_TIME FatNow;\r
223\r
224 //\r
225 // Flush each entry up the tree while dirty\r
226 //\r
227 do {\r
228 //\r
229 // If the file has a permanant error, then don't write any\r
230 // of its data to the device (may be from different media)\r
231 //\r
232 if (EFI_ERROR (OFile->Error)) {\r
233 return OFile->Error;\r
234 }\r
235\r
236 Parent = OFile->Parent;\r
237 DirEnt = OFile->DirEnt;\r
238 if (OFile->Dirty) {\r
239 //\r
240 // Update the last modification time\r
241 //\r
242 FatGetCurrentFatTime (&FatNow);\r
243 CopyMem (&DirEnt->Entry.FileLastAccess, &FatNow.Date, sizeof (FAT_DATE));\r
244 if (!OFile->PreserveLastModification) {\r
245 FatGetCurrentFatTime (&DirEnt->Entry.FileModificationTime);\r
246 }\r
247\r
248 OFile->PreserveLastModification = FALSE;\r
249 if (OFile->Archive) {\r
250 DirEnt->Entry.Attributes |= FAT_ATTRIBUTE_ARCHIVE;\r
251 OFile->Archive = FALSE;\r
252 }\r
253 //\r
254 // Write the directory entry\r
255 //\r
256 if (Parent != NULL && !DirEnt->Invalid) {\r
257 //\r
258 // Write the OFile's directory entry\r
259 //\r
260 Status = FatStoreDirEnt (Parent, DirEnt);\r
261 if (EFI_ERROR (Status)) {\r
262 return Status;\r
263 }\r
264 }\r
265\r
266 OFile->Dirty = FALSE;\r
267 }\r
268 //\r
269 // Check the parent\r
270 //\r
271 OFile = Parent;\r
272 } while (OFile != NULL);\r
273 return EFI_SUCCESS;\r
274}\r
275\r
cae7420b 276/**\r
b9ec9330
QH
277\r
278 Check the references of the OFile.\r
279 If the OFile (that is checked) is no longer\r
280 referenced, then it is freed.\r
281\r
cae7420b 282 @param OFile - The OFile to be checked.\r
b9ec9330 283\r
cae7420b
DB
284 @retval TRUE - The OFile is not referenced and freed.\r
285 @retval FALSE - The OFile is kept.\r
b9ec9330 286\r
cae7420b
DB
287**/\r
288BOOLEAN\r
289FatCheckOFileRef (\r
290 IN FAT_OFILE *OFile\r
291 )\r
b9ec9330
QH
292{\r
293 //\r
294 // If the OFile is on the check ref list, remove it\r
295 //\r
296 if (OFile->CheckLink.ForwardLink != NULL) {\r
297 RemoveEntryList (&OFile->CheckLink);\r
298 OFile->CheckLink.ForwardLink = NULL;\r
299 }\r
300\r
301 FatOFileFlush (OFile);\r
302 //\r
303 // Are there any references to this OFile?\r
304 //\r
305 if (!IsListEmpty (&OFile->Opens) || !IsListEmpty (&OFile->ChildHead)) {\r
306 //\r
307 // The OFile cannot be freed\r
308 //\r
309 return FALSE;\r
310 }\r
311 //\r
312 // Free the Ofile\r
313 //\r
314 FatCloseDirEnt (OFile->DirEnt);\r
315 return TRUE;\r
316}\r
317\r
cae7420b 318/**\r
b9ec9330
QH
319\r
320 Check the references of all open files on the volume.\r
321 Any open file (that is checked) that is no longer\r
322 referenced, is freed - and it's parent open file\r
323 is then referenced checked.\r
324\r
cae7420b 325 @param Volume - The volume to check the pending open file list.\r
b9ec9330 326\r
cae7420b
DB
327**/\r
328STATIC\r
329VOID\r
330FatCheckVolumeRef (\r
331 IN FAT_VOLUME *Volume\r
332 )\r
b9ec9330
QH
333{\r
334 FAT_OFILE *OFile;\r
335 FAT_OFILE *Parent;\r
336\r
337 //\r
338 // Check all files on the pending check list\r
339 //\r
340 while (!IsListEmpty (&Volume->CheckRef)) {\r
341 //\r
342 // Start with the first file listed\r
343 //\r
344 Parent = OFILE_FROM_CHECKLINK (Volume->CheckRef.ForwardLink);\r
345 //\r
346 // Go up the tree cleaning up any un-referenced OFiles\r
347 //\r
348 while (Parent != NULL) {\r
349 OFile = Parent;\r
350 Parent = OFile->Parent;\r
351 if (!FatCheckOFileRef (OFile)) {\r
352 break;\r
353 }\r
354 }\r
355 }\r
356}\r
357\r
cae7420b 358/**\r
b9ec9330
QH
359\r
360 Set error status for a specific OFile, reference checking the volume.\r
361 If volume is already marked as invalid, and all resources are freed\r
362 after reference checking, the file system protocol is uninstalled and\r
363 the volume structure is freed.\r
364\r
cae7420b
DB
365 @param Volume - the Volume that is to be reference checked and unlocked.\r
366 @param OFile - the OFile whose permanent error code is to be set.\r
367 @param EfiStatus - error code to be set.\r
368 @param Task point to task instance.\r
b9ec9330 369\r
cae7420b
DB
370 @retval EFI_SUCCESS - Clean up the volume successfully.\r
371 @return Others - Cleaning up of the volume is failed.\r
b9ec9330 372\r
cae7420b
DB
373**/\r
374EFI_STATUS\r
375FatCleanupVolume (\r
376 IN FAT_VOLUME *Volume,\r
377 IN FAT_OFILE *OFile,\r
378 IN EFI_STATUS EfiStatus,\r
379 IN FAT_TASK *Task\r
380 )\r
b9ec9330
QH
381{\r
382 EFI_STATUS Status;\r
383 //\r
384 // Flag the OFile\r
385 //\r
386 if (OFile != NULL) {\r
387 FatSetVolumeError (OFile, EfiStatus);\r
388 }\r
389 //\r
390 // Clean up any dangling OFiles that don't have IFiles\r
391 // we don't check return status here because we want the\r
392 // volume be cleaned up even the volume is invalid.\r
393 //\r
394 FatCheckVolumeRef (Volume);\r
395 if (Volume->Valid) {\r
396 //\r
397 // Update the free hint info. Volume->FreeInfoPos != 0\r
398 // indicates this a FAT32 volume\r
399 //\r
400 if (Volume->FreeInfoValid && Volume->FatDirty && Volume->FreeInfoPos) {\r
c1680e88 401 Status = FatDiskIo (Volume, WriteDisk, Volume->FreeInfoPos, sizeof (FAT_INFO_SECTOR), &Volume->FatInfoSector, Task);\r
b9ec9330
QH
402 if (EFI_ERROR (Status)) {\r
403 return Status;\r
404 }\r
405 }\r
406 //\r
407 // Update that the volume is not dirty\r
408 //\r
c1680e88 409 if (Volume->FatDirty && Volume->FatType != Fat12) {\r
b9ec9330 410 Volume->FatDirty = FALSE;\r
c1680e88 411 Status = FatAccessVolumeDirty (Volume, WriteFat, &Volume->NotDirtyValue);\r
b9ec9330
QH
412 if (EFI_ERROR (Status)) {\r
413 return Status;\r
414 }\r
415 }\r
416 //\r
417 // Flush all dirty cache entries to disk\r
418 //\r
149d6335 419 Status = FatVolumeFlushCache (Volume, Task);\r
b9ec9330
QH
420 if (EFI_ERROR (Status)) {\r
421 return Status;\r
422 }\r
423 }\r
424 //\r
425 // If the volume is cleared , remove it.\r
426 // The only time volume be invalidated is in DriverBindingStop.\r
427 //\r
428 if (Volume->Root == NULL && !Volume->Valid) {\r
429 //\r
430 // Free the volume structure\r
431 //\r
432 FatFreeVolume (Volume);\r
433 }\r
434\r
435 return EfiStatus;\r
436}\r
437\r
cae7420b
DB
438/**\r
439\r
440 Set the OFile and its child OFile with the error Status\r
441\r
442 @param OFile - The OFile whose permanent error code is to be set.\r
443 @param Status - Error code to be set.\r
444\r
445**/\r
b9ec9330
QH
446VOID\r
447FatSetVolumeError (\r
448 IN FAT_OFILE *OFile,\r
449 IN EFI_STATUS Status\r
450 )\r
b9ec9330
QH
451{\r
452 LIST_ENTRY *Link;\r
453 FAT_OFILE *ChildOFile;\r
454\r
455 //\r
456 // If this OFile doesn't already have an error, set one\r
457 //\r
458 if (!EFI_ERROR (OFile->Error)) {\r
459 OFile->Error = Status;\r
460 }\r
461 //\r
462 // Set the error on each child OFile\r
463 //\r
464 for (Link = OFile->ChildHead.ForwardLink; Link != &OFile->ChildHead; Link = Link->ForwardLink) {\r
465 ChildOFile = OFILE_FROM_CHILDLINK (Link);\r
466 FatSetVolumeError (ChildOFile, Status);\r
467 }\r
468}\r