]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/VirtioFsDxe/FuseRelease.c
OvmfPkg/VirtioFsDxe: add shared wrapper for FUSE_RELEASE / FUSE_RELEASEDIR
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / FuseRelease.c
diff --git a/OvmfPkg/VirtioFsDxe/FuseRelease.c b/OvmfPkg/VirtioFsDxe/FuseRelease.c
new file mode 100644 (file)
index 0000000..dd45c78
--- /dev/null
@@ -0,0 +1,121 @@
+/** @file\r
+  FUSE_RELEASE / FUSE_RELEASEDIR wrapper for the Virtio Filesystem device.\r
+\r
+  Copyright (C) 2020, Red Hat, Inc.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include "VirtioFsDxe.h"\r
+\r
+/**\r
+  Close a regular file or a directory that is open, by sending the FUSE_RELEASE\r
+  or FUSE_RELEASEDIR request to the Virtio Filesystem device.\r
+\r
+  The function may only be called after VirtioFsFuseInitSession() returns\r
+  successfully and before VirtioFsUninit() is called.\r
+\r
+  @param[in,out] VirtioFs  The Virtio Filesystem device to send the\r
+                           FUSE_RELEASE / FUSE_RELEASEDIR request to. On\r
+                           output, the FUSE request counter\r
+                           "VirtioFs->RequestId" will have been incremented.\r
+\r
+  @param[in] NodeId        The inode number of the file or directory to close.\r
+\r
+  @param[in] FuseHandle    The open handle to the file or directory to close.\r
+\r
+  @param[in] IsDir         TRUE if NodeId and FuseHandle refer to a directory,\r
+                           FALSE if NodeId and FuseHandle refer to a regular\r
+                           file.\r
+\r
+  @retval EFI_SUCCESS  The file or directory has been closed.\r
+\r
+  @return              The "errno" value mapped to an EFI_STATUS code, if the\r
+                       Virtio Filesystem device explicitly reported an error.\r
+\r
+  @return              Error codes propagated from VirtioFsSgListsValidate(),\r
+                       VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),\r
+                       VirtioFsFuseCheckResponse().\r
+**/\r
+EFI_STATUS\r
+VirtioFsFuseReleaseFileOrDir (\r
+  IN OUT VIRTIO_FS *VirtioFs,\r
+  IN     UINT64    NodeId,\r
+  IN     UINT64    FuseHandle,\r
+  IN     BOOLEAN   IsDir\r
+  )\r
+{\r
+  VIRTIO_FS_FUSE_REQUEST         CommonReq;\r
+  VIRTIO_FS_FUSE_RELEASE_REQUEST ReleaseReq;\r
+  VIRTIO_FS_IO_VECTOR            ReqIoVec[2];\r
+  VIRTIO_FS_SCATTER_GATHER_LIST  ReqSgList;\r
+  VIRTIO_FS_FUSE_RESPONSE        CommonResp;\r
+  VIRTIO_FS_IO_VECTOR            RespIoVec[1];\r
+  VIRTIO_FS_SCATTER_GATHER_LIST  RespSgList;\r
+  EFI_STATUS                     Status;\r
+\r
+  //\r
+  // Set up the scatter-gather lists.\r
+  //\r
+  ReqIoVec[0].Buffer = &CommonReq;\r
+  ReqIoVec[0].Size   = sizeof CommonReq;\r
+  ReqIoVec[1].Buffer = &ReleaseReq;\r
+  ReqIoVec[1].Size   = sizeof ReleaseReq;\r
+  ReqSgList.IoVec    = ReqIoVec;\r
+  ReqSgList.NumVec   = ARRAY_SIZE (ReqIoVec);\r
+\r
+  RespIoVec[0].Buffer = &CommonResp;\r
+  RespIoVec[0].Size   = sizeof CommonResp;\r
+  RespSgList.IoVec    = RespIoVec;\r
+  RespSgList.NumVec   = ARRAY_SIZE (RespIoVec);\r
+\r
+  //\r
+  // Validate the scatter-gather lists; calculate the total transfer sizes.\r
+  //\r
+  Status = VirtioFsSgListsValidate (VirtioFs, &ReqSgList, &RespSgList);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Populate the common request header.\r
+  //\r
+  Status = VirtioFsFuseNewRequest (\r
+             VirtioFs,\r
+             &CommonReq,\r
+             ReqSgList.TotalSize,\r
+             IsDir ? VirtioFsFuseOpReleaseDir : VirtioFsFuseOpRelease,\r
+             NodeId\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Populate the FUSE_RELEASE- / FUSE_RELEASEDIR-specific fields.\r
+  //\r
+  ReleaseReq.FileHandle   = FuseHandle;\r
+  ReleaseReq.Flags        = 0;\r
+  ReleaseReq.ReleaseFlags = 0;\r
+  ReleaseReq.LockOwner    = 0;\r
+\r
+  //\r
+  // Submit the request.\r
+  //\r
+  Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Verify the response (all response buffers are fixed size).\r
+  //\r
+  Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);\r
+  if (Status == EFI_DEVICE_ERROR) {\r
+    DEBUG ((DEBUG_ERROR, "%a: Label=\"%s\" NodeId=%Lu FuseHandle=%Lu "\r
+      "IsDir=%d Errno=%d\n", __FUNCTION__, VirtioFs->Label, NodeId, FuseHandle,\r
+      IsDir, CommonResp.Error));\r
+    Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);\r
+  }\r
+  return Status;\r
+}\r