--- /dev/null
+/** @file\r
+ FUSE_WRITE 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
+ Write a chunk to a regular file, by sending the FUSE_WRITE request to the\r
+ 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_WRITE\r
+ request to. On output, the FUSE request counter\r
+ "VirtioFs->RequestId" will have been incremented.\r
+\r
+ @param[in] NodeId The inode number of the regular file to write to.\r
+\r
+ @param[in] FuseHandle The open handle to the regular file to write to.\r
+\r
+ @param[in] Offset The absolute file position at which to start\r
+ writing.\r
+\r
+ @param[in,out] Size On input, the number of bytes to write. On\r
+ successful return, the number of bytes actually\r
+ written, which may be smaller than the value on\r
+ input.\r
+\r
+ @param[in] Data The buffer to write to the regular file.\r
+\r
+ @retval EFI_SUCCESS Write successful. The caller is responsible for\r
+ checking Size to learn the actual byte count\r
+ transferred.\r
+\r
+ @retval EFI_BAD_BUFFER_SIZE On input, Size is larger than\r
+ "VirtioFs->MaxWrite".\r
+\r
+ @return The "errno" value mapped to an EFI_STATUS code,\r
+ if the Virtio Filesystem device explicitly\r
+ reported an error.\r
+\r
+ @return Error codes propagated from\r
+ VirtioFsSgListsValidate(),\r
+ VirtioFsFuseNewRequest(),\r
+ VirtioFsSgListsSubmit(),\r
+ VirtioFsFuseCheckResponse().\r
+**/\r
+EFI_STATUS\r
+VirtioFsFuseWrite (\r
+ IN OUT VIRTIO_FS *VirtioFs,\r
+ IN UINT64 NodeId,\r
+ IN UINT64 FuseHandle,\r
+ IN UINT64 Offset,\r
+ IN OUT UINT32 *Size,\r
+ IN VOID *Data\r
+ )\r
+{\r
+ VIRTIO_FS_FUSE_REQUEST CommonReq;\r
+ VIRTIO_FS_FUSE_WRITE_REQUEST WriteReq;\r
+ VIRTIO_FS_IO_VECTOR ReqIoVec[3];\r
+ VIRTIO_FS_SCATTER_GATHER_LIST ReqSgList;\r
+ VIRTIO_FS_FUSE_RESPONSE CommonResp;\r
+ VIRTIO_FS_FUSE_WRITE_RESPONSE WriteResp;\r
+ VIRTIO_FS_IO_VECTOR RespIoVec[2];\r
+ VIRTIO_FS_SCATTER_GATHER_LIST RespSgList;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Honor the write buffer size limit of the Virtio Filesystem device.\r
+ //\r
+ if (*Size > VirtioFs->MaxWrite) {\r
+ return EFI_BAD_BUFFER_SIZE;\r
+ }\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 = &WriteReq;\r
+ ReqIoVec[1].Size = sizeof WriteReq;\r
+ ReqIoVec[2].Buffer = Data;\r
+ ReqIoVec[2].Size = *Size;\r
+ ReqSgList.IoVec = ReqIoVec;\r
+ ReqSgList.NumVec = ARRAY_SIZE (ReqIoVec);\r
+\r
+ RespIoVec[0].Buffer = &CommonResp;\r
+ RespIoVec[0].Size = sizeof CommonResp;\r
+ RespIoVec[1].Buffer = &WriteResp;\r
+ RespIoVec[1].Size = sizeof WriteResp;\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 (VirtioFs, &CommonReq, ReqSgList.TotalSize,\r
+ VirtioFsFuseOpWrite, NodeId);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Populate the FUSE_WRITE-specific fields.\r
+ //\r
+ WriteReq.FileHandle = FuseHandle;\r
+ WriteReq.Offset = Offset;\r
+ WriteReq.Size = *Size;\r
+ WriteReq.WriteFlags = 0;\r
+ WriteReq.LockOwner = 0;\r
+ WriteReq.Flags = 0;\r
+ WriteReq.Padding = 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 (EFI_ERROR (Status)) {\r
+ if (Status == EFI_DEVICE_ERROR) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Label=\"%s\" NodeId=%Lu FuseHandle=%Lu "\r
+ "Offset=0x%Lx Size=0x%x Data@%p Errno=%d\n", __FUNCTION__,\r
+ VirtioFs->Label, NodeId, FuseHandle, Offset, *Size, Data,\r
+ CommonResp.Error));\r
+ Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);\r
+ }\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Report the actual transfer size.\r
+ //\r
+ *Size = WriteResp.Size;\r
+ return EFI_SUCCESS;\r
+}\r