]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/Flush.c
BaseTools/GenFw AARCH64: fix up GOT based relative relocations
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Flush.c
... / ...
CommitLineData
1/** @file\r
2 Routines that check references and flush OFiles\r
3\r
4Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7\r
8**/\r
9\r
10#include "Fat.h"\r
11\r
12/**\r
13\r
14 Flushes all data associated with the file handle.\r
15\r
16 @param FHand - Handle to file to flush.\r
17 @param Token - A pointer to the token associated with the transaction.\r
18\r
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
23\r
24**/\r
25EFI_STATUS\r
26EFIAPI\r
27FatFlushEx (\r
28 IN EFI_FILE_PROTOCOL *FHand,\r
29 IN EFI_FILE_IO_TOKEN *Token\r
30 )\r
31{\r
32 FAT_IFILE *IFile;\r
33 FAT_OFILE *OFile;\r
34 FAT_VOLUME *Volume;\r
35 EFI_STATUS Status;\r
36 FAT_TASK *Task;\r
37\r
38 IFile = IFILE_FROM_FHAND (FHand);\r
39 OFile = IFile->OFile;\r
40 Volume = OFile->Volume;\r
41 Task = NULL;\r
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
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
76 //\r
77 // Flush the OFile\r
78 //\r
79 FatAcquireLock ();\r
80 Status = FatOFileFlush (OFile);\r
81 Status = FatCleanupVolume (OFile->Volume, OFile, Status, Task);\r
82 FatReleaseLock ();\r
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
92 return Status;\r
93}\r
94\r
95/**\r
96\r
97 Flushes all data associated with the file handle.\r
98\r
99 @param FHand - Handle to file to flush.\r
100\r
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
105\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109FatFlush (\r
110 IN EFI_FILE_PROTOCOL *FHand\r
111 )\r
112{\r
113 return FatFlushEx (FHand, NULL);\r
114}\r
115\r
116/**\r
117\r
118 Flushes & Closes the file handle.\r
119\r
120 @param FHand - Handle to the file to delete.\r
121\r
122 @retval EFI_SUCCESS - Closed the file successfully.\r
123\r
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127FatClose (\r
128 IN EFI_FILE_PROTOCOL *FHand\r
129 )\r
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
152 FatCleanupVolume (Volume, OFile, EFI_SUCCESS, NULL);\r
153 FatReleaseLock ();\r
154\r
155 //\r
156 // Close always succeed\r
157 //\r
158 return EFI_SUCCESS;\r
159}\r
160\r
161/**\r
162\r
163 Close the open file instance.\r
164\r
165 @param IFile - Open file instance.\r
166\r
167 @retval EFI_SUCCESS - Closed the file successfully.\r
168\r
169**/\r
170EFI_STATUS\r
171FatIFileClose (\r
172 FAT_IFILE *IFile\r
173 )\r
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
183 FatWaitNonblockingTask (IFile);\r
184\r
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
203/**\r
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
208 @param OFile - The open file.\r
209\r
210 @retval EFI_SUCCESS - The OFile is flushed successfully.\r
211 @return Others - An error occurred when flushing this OFile.\r
212\r
213**/\r
214EFI_STATUS\r
215FatOFileFlush (\r
216 IN FAT_OFILE *OFile\r
217 )\r
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
276/**\r
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
282 @param OFile - The OFile to be checked.\r
283\r
284 @retval TRUE - The OFile is not referenced and freed.\r
285 @retval FALSE - The OFile is kept.\r
286\r
287**/\r
288BOOLEAN\r
289FatCheckOFileRef (\r
290 IN FAT_OFILE *OFile\r
291 )\r
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
318/**\r
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
325 @param Volume - The volume to check the pending open file list.\r
326\r
327**/\r
328STATIC\r
329VOID\r
330FatCheckVolumeRef (\r
331 IN FAT_VOLUME *Volume\r
332 )\r
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
358/**\r
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
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
369\r
370 @retval EFI_SUCCESS - Clean up the volume successfully.\r
371 @return Others - Cleaning up of the volume is failed.\r
372\r
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
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
401 Status = FatDiskIo (Volume, WriteDisk, Volume->FreeInfoPos, sizeof (FAT_INFO_SECTOR), &Volume->FatInfoSector, Task);\r
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
409 if (Volume->FatDirty && Volume->FatType != Fat12) {\r
410 Volume->FatDirty = FALSE;\r
411 Status = FatAccessVolumeDirty (Volume, WriteFat, &Volume->NotDirtyValue);\r
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
419 Status = FatVolumeFlushCache (Volume, Task);\r
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
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
446VOID\r
447FatSetVolumeError (\r
448 IN FAT_OFILE *OFile,\r
449 IN EFI_STATUS Status\r
450 )\r
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