--- /dev/null
+/** @file\r
+ FUSE_LOOKUP 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
+ Send a FUSE_LOOKUP request to the Virtio Filesystem device, for resolving a\r
+ filename to an inode.\r
+\r
+ The function returns EFI_NOT_FOUND exclusively if the Virtio Filesystem\r
+ device explicitly responds with ENOENT -- "No such file or directory".\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_LOOKUP\r
+ request to. On output, the FUSE request counter\r
+ "VirtioFs->RequestId" will have been incremented.\r
+\r
+ @param[in] DirNodeId The inode number of the directory in which Name\r
+ should be resolved to an inode.\r
+\r
+ @param[in] Name The single-component filename to resolve in the\r
+ directory identified by DirNodeId.\r
+\r
+ @param[out] NodeId The inode number which Name has been resolved to.\r
+\r
+ @param[out] FuseAttr The VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE object\r
+ describing the properties of the resolved inode.\r
+\r
+ @retval EFI_SUCCESS Filename to inode resolution successful.\r
+\r
+ @retval EFI_NOT_FOUND The Virtio Filesystem device explicitly reported\r
+ ENOENT -- "No such file or directory".\r
+\r
+ @return The "errno" value mapped to an EFI_STATUS code, if the\r
+ Virtio Filesystem device explicitly reported an error\r
+ different from ENOENT. If said mapping resulted in\r
+ EFI_NOT_FOUND, it is remapped to EFI_DEVICE_ERROR.\r
+\r
+ @return Error codes propagated from VirtioFsSgListsValidate(),\r
+ VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),\r
+ VirtioFsFuseCheckResponse(). EFI_NOT_FOUND is remapped\r
+ to EFI_DEVICE_ERROR.\r
+**/\r
+EFI_STATUS\r
+VirtioFsFuseLookup (\r
+ IN OUT VIRTIO_FS *VirtioFs,\r
+ IN UINT64 DirNodeId,\r
+ IN CHAR8 *Name,\r
+ OUT UINT64 *NodeId,\r
+ OUT VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr\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_FUSE_NODE_RESPONSE NodeResp;\r
+ VIRTIO_FS_IO_VECTOR RespIoVec[3];\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
+ RespIoVec[1].Buffer = &NodeResp;\r
+ RespIoVec[1].Size = sizeof NodeResp;\r
+ RespIoVec[2].Buffer = FuseAttr;\r
+ RespIoVec[2].Size = sizeof *FuseAttr;\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
+ goto Fail;\r
+ }\r
+\r
+ //\r
+ // Populate the common request header.\r
+ //\r
+ Status = VirtioFsFuseNewRequest (VirtioFs, &CommonReq, ReqSgList.TotalSize,\r
+ VirtioFsFuseOpLookup, DirNodeId);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Fail;\r
+ }\r
+\r
+ //\r
+ // Submit the request.\r
+ //\r
+ Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Fail;\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 ((\r
+ ((CommonResp.Error == VIRTIO_FS_FUSE_ERRNO_ENOENT) ?\r
+ DEBUG_VERBOSE :\r
+ DEBUG_ERROR),\r
+ "%a: Label=\"%s\" DirNodeId=%Lu Name=\"%a\" Errno=%d\n",\r
+ __FUNCTION__,\r
+ VirtioFs->Label,\r
+ DirNodeId,\r
+ Name,\r
+ CommonResp.Error\r
+ ));\r
+ if (CommonResp.Error == VIRTIO_FS_FUSE_ERRNO_ENOENT) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);\r
+ }\r
+ goto Fail;\r
+ }\r
+\r
+ //\r
+ // Output the NodeId to which Name has been resolved to.\r
+ //\r
+ *NodeId = NodeResp.NodeId;\r
+ return EFI_SUCCESS;\r
+\r
+Fail:\r
+ return (Status == EFI_NOT_FOUND) ? EFI_DEVICE_ERROR : Status;\r
+}\r