summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/VirtioFsDxe/SimpleFsClose.c
blob: 4c1cf52112cea2afefd23f0a30228e01d5e61dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/** @file
  EFI_FILE_PROTOCOL.Close() member function for the Virtio Filesystem driver.

  Copyright (C) 2020, Red Hat, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/BaseLib.h>             // RemoveEntryList()
#include <Library/MemoryAllocationLib.h> // FreePool()

#include "VirtioFsDxe.h"

EFI_STATUS
EFIAPI
VirtioFsSimpleFileClose (
  IN EFI_FILE_PROTOCOL *This
  )
{
  VIRTIO_FS_FILE *VirtioFsFile;
  VIRTIO_FS      *VirtioFs;

  VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
  VirtioFs     = VirtioFsFile->OwnerFs;

  //
  // All actions in this function are "best effort"; the UEFI spec requires
  // EFI_FILE_PROTOCOL.Close() to sync all data to the device, but it also
  // requires EFI_FILE_PROTOCOL.Close() to release resources unconditionally,
  // and to return EFI_SUCCESS unconditionally.
  //
  // Flush, sync, release, and (if needed) forget. If any action fails, we
  // still try the others.
  //
  if (VirtioFsFile->IsOpenForWriting) {
    if (!VirtioFsFile->IsDirectory) {
      VirtioFsFuseFlush (VirtioFs, VirtioFsFile->NodeId,
        VirtioFsFile->FuseHandle);
    }

    VirtioFsFuseFsyncFileOrDir (VirtioFs, VirtioFsFile->NodeId,
      VirtioFsFile->FuseHandle, VirtioFsFile->IsDirectory);
  }

  VirtioFsFuseReleaseFileOrDir (VirtioFs, VirtioFsFile->NodeId,
    VirtioFsFile->FuseHandle, VirtioFsFile->IsDirectory);

  //
  // VirtioFsFile->FuseHandle is gone at this point, but VirtioFsFile->NodeId
  // is still valid. If we've known VirtioFsFile->NodeId from a lookup, then
  // now we should ask the server to forget it *once*.
  //
  if (VirtioFsFile->NodeId != VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID) {
    VirtioFsFuseForget (VirtioFs, VirtioFsFile->NodeId);
  }

  //
  // One fewer file left open for the owner filesystem.
  //
  RemoveEntryList (&VirtioFsFile->OpenFilesEntry);

  FreePool (VirtioFsFile->CanonicalPathname);
  if (VirtioFsFile->FileInfoArray != NULL) {
    FreePool (VirtioFsFile->FileInfoArray);
  }
  FreePool (VirtioFsFile);
  return EFI_SUCCESS;
}