SimpleFsClose.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /** @file
  2. EFI_FILE_PROTOCOL.Close() member function for the Virtio Filesystem driver.
  3. Copyright (C) 2020, Red Hat, Inc.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseLib.h> // RemoveEntryList()
  7. #include <Library/MemoryAllocationLib.h> // FreePool()
  8. #include "VirtioFsDxe.h"
  9. EFI_STATUS
  10. EFIAPI
  11. VirtioFsSimpleFileClose (
  12. IN EFI_FILE_PROTOCOL *This
  13. )
  14. {
  15. VIRTIO_FS_FILE *VirtioFsFile;
  16. VIRTIO_FS *VirtioFs;
  17. VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
  18. VirtioFs = VirtioFsFile->OwnerFs;
  19. //
  20. // All actions in this function are "best effort"; the UEFI spec requires
  21. // EFI_FILE_PROTOCOL.Close() to sync all data to the device, but it also
  22. // requires EFI_FILE_PROTOCOL.Close() to release resources unconditionally,
  23. // and to return EFI_SUCCESS unconditionally.
  24. //
  25. // Flush, sync, release, and (if needed) forget. If any action fails, we
  26. // still try the others.
  27. //
  28. if (VirtioFsFile->IsOpenForWriting) {
  29. if (!VirtioFsFile->IsDirectory) {
  30. VirtioFsFuseFlush (
  31. VirtioFs,
  32. VirtioFsFile->NodeId,
  33. VirtioFsFile->FuseHandle
  34. );
  35. }
  36. VirtioFsFuseFsyncFileOrDir (
  37. VirtioFs,
  38. VirtioFsFile->NodeId,
  39. VirtioFsFile->FuseHandle,
  40. VirtioFsFile->IsDirectory
  41. );
  42. }
  43. VirtioFsFuseReleaseFileOrDir (
  44. VirtioFs,
  45. VirtioFsFile->NodeId,
  46. VirtioFsFile->FuseHandle,
  47. VirtioFsFile->IsDirectory
  48. );
  49. //
  50. // VirtioFsFile->FuseHandle is gone at this point, but VirtioFsFile->NodeId
  51. // is still valid. If we've known VirtioFsFile->NodeId from a lookup, then
  52. // now we should ask the server to forget it *once*.
  53. //
  54. if (VirtioFsFile->NodeId != VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID) {
  55. VirtioFsFuseForget (VirtioFs, VirtioFsFile->NodeId);
  56. }
  57. //
  58. // One fewer file left open for the owner filesystem.
  59. //
  60. RemoveEntryList (&VirtioFsFile->OpenFilesEntry);
  61. FreePool (VirtioFsFile->CanonicalPathname);
  62. if (VirtioFsFile->FileInfoArray != NULL) {
  63. FreePool (VirtioFsFile->FileInfoArray);
  64. }
  65. FreePool (VirtioFsFile);
  66. return EFI_SUCCESS;
  67. }