]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/VirtioFsDxe: add a shared wrapper for FUSE_READ / FUSE_READDIRPLUS
authorLaszlo Ersek <lersek@redhat.com>
Wed, 16 Dec 2020 21:11:09 +0000 (22:11 +0100)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 21 Dec 2020 17:16:23 +0000 (17:16 +0000)
Add the VirtioFsFuseReadFileOrDir() function, for sending the FUSE_READ or
FUSE_READDIRPLUS command to the Virtio Filesystem device.

Parsing the structured FUSE_READDIRPLUS output is complex, and cannot be
integrated into the wrapper function. Given that fact, FUSE_READ and
FUSE_READDIRPLUS turn out to need identical low-level handling, except for
the opcode. Hence the shared wrapper function.

(It's prudent to verify whether the FUSE server supports FUSE_READDIRPLUS,
so update the session init code accordingly.)

This is the first FUSE request wrapper function that deals with a variable
size tail buffer.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3097
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20201216211125.19496-33-lersek@redhat.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
OvmfPkg/Include/IndustryStandard/VirtioFs.h
OvmfPkg/VirtioFsDxe/FuseInit.c
OvmfPkg/VirtioFsDxe/FuseRead.c [new file with mode: 0644]
OvmfPkg/VirtioFsDxe/VirtioFsDxe.h
OvmfPkg/VirtioFsDxe/VirtioFsDxe.inf

index 800d3651e12bea7b3057995ed77a131b27b0619c..fa8c40019f4e2d5bc027d6f56489bce6512cfa6f 100644 (file)
@@ -108,6 +108,39 @@ typedef struct {
 #define VIRTIO_FS_FUSE_OPEN_REQ_F_RDONLY 0\r
 #define VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR   2\r
 \r
+//\r
+// Flags for VirtioFsFuseOpInit.\r
+//\r
+#define VIRTIO_FS_FUSE_INIT_REQ_F_DO_READDIRPLUS BIT13\r
+\r
+/**\r
+  Macro for calculating the size of a directory stream entry.\r
+\r
+  The macro may evaluate Namelen multiple times.\r
+\r
+  The macro evaluates to a UINTN value that is safe to cast to UINT32.\r
+\r
+  @param[in] Namelen  The size of the filename byte array that follows\r
+                      VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE in the directory\r
+                      stream, as reported by\r
+                      VIRTIO_FS_FUSE_STATFS_RESPONSE.Namelen or\r
+                      VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE.Namelen. The filename\r
+                      byte array is not NUL-terminated.\r
+\r
+  @retval 0  Namelen was zero or greater than SIZE_4KB.\r
+\r
+  @return    The number of bytes in the directory entry, including the\r
+             VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE header.\r
+**/\r
+#define VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE(Namelen)             \\r
+  ((Namelen) == 0 || (Namelen) > SIZE_4KB ?                          \\r
+   (UINTN)0 :                                                        \\r
+   ALIGN_VALUE (                                                     \\r
+     sizeof (VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE) + (UINTN)(Namelen), \\r
+     sizeof (UINT64)                                                 \\r
+     )                                                               \\r
+   )\r
+\r
 //\r
 // FUSE operation codes.\r
 //\r
@@ -119,6 +152,7 @@ typedef enum {
   VirtioFsFuseOpUnlink      = 10,\r
   VirtioFsFuseOpRmDir       = 11,\r
   VirtioFsFuseOpOpen        = 14,\r
+  VirtioFsFuseOpRead        = 15,\r
   VirtioFsFuseOpStatFs      = 17,\r
   VirtioFsFuseOpRelease     = 18,\r
   VirtioFsFuseOpFsync       = 20,\r
@@ -128,6 +162,7 @@ typedef enum {
   VirtioFsFuseOpReleaseDir  = 29,\r
   VirtioFsFuseOpFsyncDir    = 30,\r
   VirtioFsFuseOpCreate      = 35,\r
+  VirtioFsFuseOpReadDirPlus = 44,\r
 } VIRTIO_FS_FUSE_OPCODE;\r
 \r
 #pragma pack (1)\r
@@ -234,6 +269,19 @@ typedef struct {
   UINT32 Padding;\r
 } VIRTIO_FS_FUSE_OPEN_RESPONSE;\r
 \r
+//\r
+// Header for VirtioFsFuseOpRead and VirtioFsFuseOpReadDirPlus.\r
+//\r
+typedef struct {\r
+  UINT64 FileHandle;\r
+  UINT64 Offset;\r
+  UINT32 Size;\r
+  UINT32 ReadFlags;\r
+  UINT64 LockOwner;\r
+  UINT32 Flags;\r
+  UINT32 Padding;\r
+} VIRTIO_FS_FUSE_READ_REQUEST;\r
+\r
 //\r
 // Header for VirtioFsFuseOpStatFs.\r
 //\r
@@ -312,6 +360,25 @@ typedef struct {
   UINT32 Umask;\r
   UINT32 Padding;\r
 } VIRTIO_FS_FUSE_CREATE_REQUEST;\r
+\r
+//\r
+// Header for VirtioFsFuseOpReadDirPlus.\r
+//\r
+// Diverging from the rest of the headers, this structure embeds other\r
+// structures. The reason is that a scatter list cannot be used to receive\r
+// NodeResp and AttrResp separately; the record below is followed by a variable\r
+// size filename byte array, and then such pairs are repeated a number of\r
+// times. Thus, later header start offsets depend on earlier filename array\r
+// sizes.\r
+//\r
+typedef struct {\r
+  VIRTIO_FS_FUSE_NODE_RESPONSE       NodeResp;\r
+  VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE AttrResp;\r
+  UINT64                             NodeId;\r
+  UINT64                             CookieForNextEntry;\r
+  UINT32                             Namelen;\r
+  UINT32                             Type;\r
+} VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE;\r
 #pragma pack ()\r
 \r
 #endif // VIRTIO_FS_H_\r
index aa19dbdc05cbd760e671d7e953a37a5c279fb9e7..7aa6ee75caf67b5e523988fa0e4fa4f7ea8bd0eb 100644 (file)
@@ -26,7 +26,8 @@
 \r
   @retval EFI_SUCCESS      The FUSE session has been started.\r
 \r
-  @retval EFI_UNSUPPORTED  FUSE interface version negotiation failed.\r
+  @retval EFI_UNSUPPORTED  FUSE interface version or feature negotiation\r
+                           failed.\r
 \r
   @return                  The "errno" value mapped to an EFI_STATUS code, if\r
                            the Virtio Filesystem device explicitly reported an\r
@@ -97,7 +98,7 @@ VirtioFsFuseInitSession (
   InitReq.Major        = VIRTIO_FS_FUSE_MAJOR;\r
   InitReq.Minor        = VIRTIO_FS_FUSE_MINOR;\r
   InitReq.MaxReadahead = 0;\r
-  InitReq.Flags        = 0;\r
+  InitReq.Flags        = VIRTIO_FS_FUSE_INIT_REQ_F_DO_READDIRPLUS;\r
 \r
   //\r
   // Submit the request.\r
@@ -121,10 +122,11 @@ VirtioFsFuseInitSession (
   }\r
 \r
   //\r
-  // Check FUSE interface version compatibility.\r
+  // Check FUSE interface version / feature compatibility.\r
   //\r
   if (InitResp.Major < InitReq.Major ||\r
-      (InitResp.Major == InitReq.Major && InitResp.Minor < InitReq.Minor)) {\r
+      (InitResp.Major == InitReq.Major && InitResp.Minor < InitReq.Minor) ||\r
+      (InitResp.Flags & VIRTIO_FS_FUSE_INIT_REQ_F_DO_READDIRPLUS) == 0) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
diff --git a/OvmfPkg/VirtioFsDxe/FuseRead.c b/OvmfPkg/VirtioFsDxe/FuseRead.c
new file mode 100644 (file)
index 0000000..1611e29
--- /dev/null
@@ -0,0 +1,191 @@
+/** @file\r
+  FUSE_READ / FUSE_READDIRPLUS 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
+  Read a chunk from a regular file or a directory stream, by sending the\r
+  FUSE_READ / FUSE_READDIRPLUS 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_READ\r
+                           or FUSE_READDIRPLUS request to. On output, the FUSE\r
+                           request counter "VirtioFs->RequestId" will have been\r
+                           incremented.\r
+\r
+  @param[in] NodeId        The inode number of the regular file or directory\r
+                           stream to read from.\r
+\r
+  @param[in] FuseHandle    The open handle to the regular file or directory\r
+                           stream to read from.\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
+  @param[in] Offset        If IsDir is FALSE: the absolute file position at\r
+                           which to start reading.\r
+\r
+                           If IsDir is TRUE: the directory stream cookie at\r
+                           which to start or continue reading. The zero-valued\r
+                           cookie identifies the start of the directory stream.\r
+                           Further positions in the directory stream can be\r
+                           passed in from the CookieForNextEntry field of\r
+                           VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE.\r
+\r
+  @param[in,out] Size      On input, the number of bytes to read. On successful\r
+                           return, the number of bytes actually read, which may\r
+                           be smaller than the value on input.\r
+\r
+                           When reading a regular file (i.e., when IsDir is\r
+                           FALSE), EOF can be detected by passing in a nonzero\r
+                           Size, and finding a zero Size on output.\r
+\r
+                           When reading a directory stream (i.e., when IsDir is\r
+                           TRUE), Data consists of a sequence of variably-sized\r
+                           records (directory entries). A read operation\r
+                           returns the maximal sequence of records that fits in\r
+                           Size, without having to truncate a record. In order\r
+                           to guarantee progress, call\r
+\r
+                             VirtioFsFuseStatFs (VirtioFs, NodeId,\r
+                               &FilesysAttr)\r
+\r
+                           first, to learn the maximum Namelen for the\r
+                           directory stream. Then assign Size at least\r
+\r
+                             VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE (\r
+                               FilesysAttr.Namelen)\r
+\r
+                           on input. (Remember that\r
+                           VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE() may return\r
+                           0 if its Namelen argument is invalid.) EOF can be\r
+                           detected if Size is set on input like described\r
+                           above, and Size is zero on output.\r
+\r
+  @param[out] Data         Buffer to read the bytes from the regular file or\r
+                           the directory stream into. The caller is responsible\r
+                           for providing room for (at least) as many bytes in\r
+                           Data as Size is on input.\r
+\r
+  @retval EFI_SUCCESS  Read successful. The caller is responsible for checking\r
+                       Size to learn the actual byte count transferred.\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
+VirtioFsFuseReadFileOrDir (\r
+  IN OUT VIRTIO_FS *VirtioFs,\r
+  IN     UINT64    NodeId,\r
+  IN     UINT64    FuseHandle,\r
+  IN     BOOLEAN   IsDir,\r
+  IN     UINT64    Offset,\r
+  IN OUT UINT32    *Size,\r
+     OUT VOID      *Data\r
+  )\r
+{\r
+  VIRTIO_FS_FUSE_REQUEST        CommonReq;\r
+  VIRTIO_FS_FUSE_READ_REQUEST   ReadReq;\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[2];\r
+  VIRTIO_FS_SCATTER_GATHER_LIST RespSgList;\r
+  EFI_STATUS                    Status;\r
+  UINTN                         TailBufferFill;\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 = &ReadReq;\r
+  ReqIoVec[1].Size   = sizeof ReadReq;\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 = Data;\r
+  RespIoVec[1].Size   = *Size;\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 ? VirtioFsFuseOpReadDirPlus : VirtioFsFuseOpRead,\r
+             NodeId\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Populate the FUSE_READ- / FUSE_READDIRPLUS-specific fields.\r
+  //\r
+  ReadReq.FileHandle = FuseHandle;\r
+  ReadReq.Offset     = Offset;\r
+  ReadReq.Size       = *Size;\r
+  ReadReq.ReadFlags  = 0;\r
+  ReadReq.LockOwner  = 0;\r
+  ReadReq.Flags      = 0;\r
+  ReadReq.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. Note that TailBufferFill is variable.\r
+  //\r
+  Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique,\r
+             &TailBufferFill);\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_DEVICE_ERROR) {\r
+      DEBUG ((DEBUG_ERROR, "%a: Label=\"%s\" NodeId=%Lu FuseHandle=%Lu "\r
+        "IsDir=%d Offset=0x%Lx Size=0x%x Data@%p Errno=%d\n", __FUNCTION__,\r
+        VirtioFs->Label, NodeId, FuseHandle, IsDir, 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
+  // Integer overflow in the (UINT32) cast below is not possible; the\r
+  // VIRTIO_FS_SCATTER_GATHER_LIST functions would have caught that.\r
+  //\r
+  *Size = (UINT32)TailBufferFill;\r
+  return EFI_SUCCESS;\r
+}\r
index 948fcfb6b6f30751005e87a056950327b057a557..f5501af7d0a4f9f065e3a5539006c048bc40d4c3 100644 (file)
@@ -298,6 +298,17 @@ VirtioFsFuseOpen (
      OUT UINT64    *FuseHandle\r
   );\r
 \r
+EFI_STATUS\r
+VirtioFsFuseReadFileOrDir (\r
+  IN OUT VIRTIO_FS *VirtioFs,\r
+  IN     UINT64    NodeId,\r
+  IN     UINT64    FuseHandle,\r
+  IN     BOOLEAN   IsDir,\r
+  IN     UINT64    Offset,\r
+  IN OUT UINT32    *Size,\r
+     OUT VOID      *Data\r
+  );\r
+\r
 EFI_STATUS\r
 VirtioFsFuseStatFs (\r
   IN OUT VIRTIO_FS                      *VirtioFs,\r
index 3cb5c101c3a6e8a3ee2f92a9e752d4d23d3308a8..39c77c7c4ee98a55da07c81fe8776bcbfbd3c008 100644 (file)
@@ -93,6 +93,7 @@
   FuseOpen.c\r
   FuseOpenDir.c\r
   FuseOpenOrCreate.c\r
+  FuseRead.c\r
   FuseRelease.c\r
   FuseStatFs.c\r
   FuseUnlink.c\r