2 EFI_FILE_PROTOCOL.Delete() member function for the Virtio Filesystem driver.
4 Copyright (C) 2020, Red Hat, Inc.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/BaseLib.h> // RemoveEntryList()
10 #include <Library/MemoryAllocationLib.h> // FreePool()
12 #include "VirtioFsDxe.h"
16 VirtioFsSimpleFileDelete (
17 IN EFI_FILE_PROTOCOL
*This
20 VIRTIO_FS_FILE
*VirtioFsFile
;
24 VirtioFsFile
= VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This
);
25 VirtioFs
= VirtioFsFile
->OwnerFs
;
28 // All actions in this function are "best effort"; the UEFI spec requires
29 // EFI_FILE_PROTOCOL.Delete() to release resources unconditionally. If a step
30 // related to removing the file fails, it's only reflected in the return
31 // status (EFI_WARN_DELETE_FAILURE rather than EFI_SUCCESS).
33 // Release, remove, and (if needed) forget. We don't waste time flushing and
34 // syncing; if the EFI_FILE_PROTOCOL user cares enough, they should keep the
35 // parent directory open until after this function call returns, and then
36 // force a sync on *that* EFI_FILE_PROTOCOL instance, using either the
37 // Flush() member function, or the Close() member function.
39 // If any action fails below, we still try the others.
41 VirtioFsFuseReleaseFileOrDir (
44 VirtioFsFile
->FuseHandle
,
45 VirtioFsFile
->IsDirectory
49 // VirtioFsFile->FuseHandle is gone at this point, but VirtioFsFile->NodeId
50 // is still valid. Continue with removing the file or directory. The result
51 // of this operation determines the return status of the function.
53 if (VirtioFsFile
->IsOpenForWriting
) {
58 // Split our canonical pathname into most specific parent directory
59 // (identified by NodeId), and single-component filename within that
60 // directory. If This stands for the root directory "/", then the following
61 // function call will gracefully fail.
63 Status
= VirtioFsLookupMostSpecificParentDir (
65 VirtioFsFile
->CanonicalPathname
,
69 if (!EFI_ERROR (Status
)) {
71 // Attempt the actual removal. Regardless of the outcome, ParentNodeId
72 // must be forgotten right after (unless it stands for the root
75 Status
= VirtioFsFuseRemoveFileOrDir (
79 VirtioFsFile
->IsDirectory
81 if (ParentNodeId
!= VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID
) {
82 VirtioFsFuseForget (VirtioFs
, ParentNodeId
);
86 if (EFI_ERROR (Status
)) {
88 // Map any failure to the spec-mandated warning code.
90 Status
= EFI_WARN_DELETE_FAILURE
;
93 Status
= EFI_WARN_DELETE_FAILURE
;
97 // Finally, if we've known VirtioFsFile->NodeId from a lookup, then we should
98 // also ask the server to forget it *once*.
100 if (VirtioFsFile
->NodeId
!= VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID
) {
101 VirtioFsFuseForget (VirtioFs
, VirtioFsFile
->NodeId
);
105 // One fewer file left open for the owner filesystem.
107 RemoveEntryList (&VirtioFsFile
->OpenFilesEntry
);
109 FreePool (VirtioFsFile
->CanonicalPathname
);
110 if (VirtioFsFile
->FileInfoArray
!= NULL
) {
111 FreePool (VirtioFsFile
->FileInfoArray
);
114 FreePool (VirtioFsFile
);