1 | /** @file
|
---|
2 | EFI_FILE_PROTOCOL.Delete() member function for the Virtio Filesystem driver.
|
---|
3 |
|
---|
4 | Copyright (C) 2020, Red Hat, Inc.
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include <Library/BaseLib.h> // RemoveEntryList()
|
---|
10 | #include <Library/MemoryAllocationLib.h> // FreePool()
|
---|
11 |
|
---|
12 | #include "VirtioFsDxe.h"
|
---|
13 |
|
---|
14 | EFI_STATUS
|
---|
15 | EFIAPI
|
---|
16 | VirtioFsSimpleFileDelete (
|
---|
17 | IN EFI_FILE_PROTOCOL *This
|
---|
18 | )
|
---|
19 | {
|
---|
20 | VIRTIO_FS_FILE *VirtioFsFile;
|
---|
21 | VIRTIO_FS *VirtioFs;
|
---|
22 | EFI_STATUS Status;
|
---|
23 |
|
---|
24 | VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
|
---|
25 | VirtioFs = VirtioFsFile->OwnerFs;
|
---|
26 |
|
---|
27 | //
|
---|
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).
|
---|
32 | //
|
---|
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.
|
---|
38 | //
|
---|
39 | // If any action fails below, we still try the others.
|
---|
40 | //
|
---|
41 | VirtioFsFuseReleaseFileOrDir (VirtioFs, VirtioFsFile->NodeId,
|
---|
42 | VirtioFsFile->FuseHandle, VirtioFsFile->IsDirectory);
|
---|
43 |
|
---|
44 | //
|
---|
45 | // VirtioFsFile->FuseHandle is gone at this point, but VirtioFsFile->NodeId
|
---|
46 | // is still valid. Continue with removing the file or directory. The result
|
---|
47 | // of this operation determines the return status of the function.
|
---|
48 | //
|
---|
49 | if (VirtioFsFile->IsOpenForWriting) {
|
---|
50 | UINT64 ParentNodeId;
|
---|
51 | CHAR8 *LastComponent;
|
---|
52 |
|
---|
53 | //
|
---|
54 | // Split our canonical pathname into most specific parent directory
|
---|
55 | // (identified by NodeId), and single-component filename within that
|
---|
56 | // directory. If This stands for the root directory "/", then the following
|
---|
57 | // function call will gracefully fail.
|
---|
58 | //
|
---|
59 | Status = VirtioFsLookupMostSpecificParentDir (
|
---|
60 | VirtioFs,
|
---|
61 | VirtioFsFile->CanonicalPathname,
|
---|
62 | &ParentNodeId,
|
---|
63 | &LastComponent
|
---|
64 | );
|
---|
65 | if (!EFI_ERROR (Status)) {
|
---|
66 | //
|
---|
67 | // Attempt the actual removal. Regardless of the outcome, ParentNodeId
|
---|
68 | // must be forgotten right after (unless it stands for the root
|
---|
69 | // directory).
|
---|
70 | //
|
---|
71 | Status = VirtioFsFuseRemoveFileOrDir (
|
---|
72 | VirtioFs,
|
---|
73 | ParentNodeId,
|
---|
74 | LastComponent,
|
---|
75 | VirtioFsFile->IsDirectory
|
---|
76 | );
|
---|
77 | if (ParentNodeId != VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID) {
|
---|
78 | VirtioFsFuseForget (VirtioFs, ParentNodeId);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if (EFI_ERROR (Status)) {
|
---|
82 | //
|
---|
83 | // Map any failure to the spec-mandated warning code.
|
---|
84 | //
|
---|
85 | Status = EFI_WARN_DELETE_FAILURE;
|
---|
86 | }
|
---|
87 | } else {
|
---|
88 | Status = EFI_WARN_DELETE_FAILURE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | //
|
---|
92 | // Finally, if we've known VirtioFsFile->NodeId from a lookup, then we should
|
---|
93 | // also ask the server to forget it *once*.
|
---|
94 | //
|
---|
95 | if (VirtioFsFile->NodeId != VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID) {
|
---|
96 | VirtioFsFuseForget (VirtioFs, VirtioFsFile->NodeId);
|
---|
97 | }
|
---|
98 |
|
---|
99 | //
|
---|
100 | // One fewer file left open for the owner filesystem.
|
---|
101 | //
|
---|
102 | RemoveEntryList (&VirtioFsFile->OpenFilesEntry);
|
---|
103 |
|
---|
104 | FreePool (VirtioFsFile->CanonicalPathname);
|
---|
105 | if (VirtioFsFile->FileInfoArray != NULL) {
|
---|
106 | FreePool (VirtioFsFile->FileInfoArray);
|
---|
107 | }
|
---|
108 | FreePool (VirtioFsFile);
|
---|
109 | return Status;
|
---|
110 | }
|
---|