--- /dev/null
+/** @file\r
+ FUSE_UNLINK / FUSE_RMDIR 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 <Library/BaseLib.h> // AsciiStrSize()\r
+\r
+#include "VirtioFsDxe.h"\r
+\r
+/**\r
+ Remove a regular file or a directory, by sending the FUSE_UNLINK or\r
+ FUSE_RMDIR 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 FUSE_UNLINK\r
+ / FUSE_RMDIR request to. On output, the FUSE request\r
+ counter "VirtioFs->RequestId" will have been\r
+ incremented.\r
+\r
+ @param[in] ParentNodeId The inode number of the directory in which Name\r
+ should be removed.\r
+\r
+ @param[in] Name The single-component filename to remove in the\r
+ directory identified by ParentNodeId.\r
+\r
+ @param[in] IsDir TRUE if Name refers to a directory, FALSE otherwise.\r
+\r
+ @retval EFI_SUCCESS The file or directory has been removed.\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
+VirtioFsFuseRemoveFileOrDir (\r
+ IN OUT VIRTIO_FS *VirtioFs,\r
+ IN UINT64 ParentNodeId,\r
+ IN CHAR8 *Name,\r
+ IN BOOLEAN IsDir\r
+ )\r
+{\r
+ VIRTIO_FS_FUSE_REQUEST CommonReq;\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 = Name;\r
+ ReqIoVec[1].Size = AsciiStrSize (Name);\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 ? VirtioFsFuseOpRmDir : VirtioFsFuseOpUnlink,\r
+ ParentNodeId\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\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\" ParentNodeId=%Lu Name=\"%a\" "\r
+ "IsDir=%d Errno=%d\n", __FUNCTION__, VirtioFs->Label, ParentNodeId, Name,\r
+ IsDir, CommonResp.Error));\r
+ Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);\r
+ }\r
+ return Status;\r
+}\r