From nobody Mon Feb 9 07:05:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+69050+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+69050+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1608157646; cv=none; d=zohomail.com; s=zohoarc; b=KPcLSVpffMPgvSFb9306uD50XUqbbLpwOCeGYWv8zGJAhcy96LxU8ErAgwTYXwxqoLbWeG0I0U4HkN5km0rrzvD6h25OYSMb+B/bm1Kx6fnfTsEcBEP64BnSB4VjXSVKey27XR4UkDpifWo0XEMo000dXYYGACPL3RpDCA++CuY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608157646; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=G3YnKevy7aWVDSx1a6rVftvkVB4K75p1vmuntXmW8qE=; b=j9WNyJuAyQwIsovYZiMKPwFHPyrXFbvn9++4m6lS4iqpC8ficNNpGaLN/1WwEBL9QPbrhsz/a9sSc7Gz2+iNRljfgx8u+nPG7sLRlU+8T6IyE6/od3OVeLO+2ukU/kSF2JtwYxLzRPBYkxok9ymlkmO2OYr3Z+66/d9Fk7LI7U4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+69050+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1608157646579964.0066777104219; Wed, 16 Dec 2020 14:27:26 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id cXRdYY1788612xH1nSfTrLX3; Wed, 16 Dec 2020 14:27:26 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.groups.io with SMTP id smtpd.web12.4796.1608157645451111499 for ; Wed, 16 Dec 2020 14:27:25 -0800 X-Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-391-3cwK6gj8Pn6GMxIjzNij9Q-1; Wed, 16 Dec 2020 17:27:22 -0500 X-MC-Unique: 3cwK6gj8Pn6GMxIjzNij9Q-1 X-Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A7343801817; Wed, 16 Dec 2020 22:27:21 +0000 (UTC) X-Received: from lacos-laptop-7.usersys.redhat.com (ovpn-114-152.ams2.redhat.com [10.36.114.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id D23AE60CC4; Wed, 16 Dec 2020 22:27:16 +0000 (UTC) From: "Laszlo Ersek" To: devel@edk2.groups.io, virtio-fs@redhat.com, lersek@redhat.com Cc: Ard Biesheuvel , Jordan Justen , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [edk2-devel] [edk2 PATCH 36/48] OvmfPkg/VirtioFsDxe: implement EFI_FILE_PROTOCOL.Read() for directories Date: Wed, 16 Dec 2020 22:11:13 +0100 Message-Id: <20201216211125.19496-37-lersek@redhat.com> In-Reply-To: <20201216211125.19496-1-lersek@redhat.com> References: <20201216211125.19496-1-lersek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lersek@redhat.com X-Gm-Message-State: T8Jm6ffM8QvZazL3h3oxdFZ0x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1608157646; bh=G3YnKevy7aWVDSx1a6rVftvkVB4K75p1vmuntXmW8qE=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=bTmWJ0h+WcC6uLBPUfjW0dqwbCSOqmoysjzk20j/CmHO/HmGK0SDKuoWfhRsYdu9KlY xLr8gEE5XDesid0OpJV+soOONvGbZ2Vel+SGgpLNI5JBywa6vwNADzpsOepCExtRYTUZs ehXyKWCMvxDUhptT9DVW3cODWwnwnYPkOFk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" Using the functions introduced previously, we can now implement VirtioFsSimpleFileRead() for directories as well. This patch completes the read-only support for virtio-fs. Commands like "TYPE" and "DIR" work in the UEFI shell. Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Philippe Mathieu-Daud=C3=A9 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3097 Signed-off-by: Laszlo Ersek --- OvmfPkg/VirtioFsDxe/SimpleFsRead.c | 343 +++++++++++++++++++- 1 file changed, 342 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/VirtioFsDxe/SimpleFsRead.c b/OvmfPkg/VirtioFsDxe/Simpl= eFsRead.c index c4ad07c9aec4..d6368342a139 100644 --- a/OvmfPkg/VirtioFsDxe/SimpleFsRead.c +++ b/OvmfPkg/VirtioFsDxe/SimpleFsRead.c @@ -1,18 +1,359 @@ /** @file EFI_FILE_PROTOCOL.Read() member function for the Virtio Filesystem drive= r. =20 Copyright (C) 2020, Red Hat, Inc. =20 SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 +#include // CopyMem() +#include // AllocatePool() + #include "VirtioFsDxe.h" =20 +/** + Populate a caller-allocated EFI_FILE_INFO object from + VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE. + + @param[in] Dirent The entry read from the directory stream.= The + caller is responsible for ensuring that + Dirent->Namelen describe valid storage. + + @param[in] SingleFileInfoSize The allocated size of FileInfo. + + @param[out] FileInfo The EFI_FILE_INFO object to populate. On + success, all fields in FileInfo will be + updated, setting FileInfo->Size to the + actually used size (which will not exceed + SingleFileInfoSize). + + @retval EFI_SUCCESS FileInfo has been filled in. + + @return Error codes propagated from + VirtioFsFuseDirentPlusToEfiFileInfo() and + VirtioFsFuseAttrToEfiFileInfo(). The contents of + FileInfo are indeterminate. +**/ +STATIC +EFI_STATUS +PopulateFileInfo ( + IN VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE *Dirent, + IN UINTN SingleFileInfoSize, + OUT EFI_FILE_INFO *FileInfo + ) +{ + EFI_STATUS Status; + + // + // Convert the name, set the actual size. + // + FileInfo->Size =3D SingleFileInfoSize; + Status =3D VirtioFsFuseDirentPlusToEfiFileInfo (Dirent, FileInfo); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Populate the scalar fields. + // + Status =3D VirtioFsFuseAttrToEfiFileInfo (&Dirent->AttrResp, FileInfo); + return Status; +} + +/** + Refill the EFI_FILE_INFO cache from the directory stream. +**/ +STATIC +EFI_STATUS +RefillFileInfoCache ( + IN OUT VIRTIO_FS_FILE *VirtioFsFile + ) +{ + VIRTIO_FS *VirtioFs; + EFI_STATUS Status; + VIRTIO_FS_FUSE_STATFS_RESPONSE FilesysAttr; + UINT32 DirentBufSize; + UINT8 *DirentBuf; + UINTN SingleFileInfoSize; + UINT8 *FileInfoArray; + UINT64 DirStreamCookie; + UINT64 CacheEndsAtCookie; + UINTN NumFileInfo; + + // + // Allocate a DirentBuf that can receive at least + // VIRTIO_FS_FILE_MAX_FILE_INFO directory entries, based on the maximum + // filename length supported by the filesystem. Note that the multiplica= tion + // is safe from overflow due to the VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_S= IZE() + // check. + // + VirtioFs =3D VirtioFsFile->OwnerFs; + Status =3D VirtioFsFuseStatFs (VirtioFs, VirtioFsFile->NodeId, &FilesysA= ttr); + if (EFI_ERROR (Status)) { + return Status; + } + DirentBufSize =3D (UINT32)VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE ( + FilesysAttr.Namelen); + if (DirentBufSize =3D=3D 0) { + return EFI_UNSUPPORTED; + } + DirentBufSize *=3D VIRTIO_FS_FILE_MAX_FILE_INFO; + DirentBuf =3D AllocatePool (DirentBufSize); + if (DirentBuf =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + // + // Allocate the EFI_FILE_INFO cache. A single EFI_FILE_INFO element is s= ized + // accordingly to the maximum filename length supported by the filesyste= m. + // + // Note that the calculation below cannot overflow, due to the filename = limit + // imposed by the VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE() check above.= The + // calculation takes the L'\0' character that we'll need to append into + // account. + // + SingleFileInfoSize =3D (OFFSET_OF (EFI_FILE_INFO, FileName) + + ((UINTN)FilesysAttr.Namelen + 1) * sizeof (CHAR16)= ); + FileInfoArray =3D AllocatePool ( + VIRTIO_FS_FILE_MAX_FILE_INFO * SingleFileInfoSize + ); + if (FileInfoArray =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto FreeDirentBuf; + } + + // + // Pick up reading the directory stream where the previous cache ended. + // + DirStreamCookie =3D VirtioFsFile->FilePosition; + CacheEndsAtCookie =3D VirtioFsFile->FilePosition; + NumFileInfo =3D 0; + do { + UINT32 Remaining; + UINT32 Consumed; + + // + // Fetch a chunk of the directory stream. The chunk may hold more entr= ies + // than what we can fit in the cache. The chunk may also not entirely = fill + // the cache, especially after filtering out entries that cannot be + // supported under UEFI (sockets, FIFOs, filenames with backslashes, e= tc). + // + Remaining =3D DirentBufSize; + Status =3D VirtioFsFuseReadFileOrDir ( + VirtioFs, + VirtioFsFile->NodeId, + VirtioFsFile->FuseHandle, + TRUE, // IsDir + DirStreamCookie, // Offset + &Remaining, // Size + DirentBuf // Data + ); + if (EFI_ERROR (Status)) { + goto FreeFileInfoArray; + } + + if (Remaining =3D=3D 0) { + // + // The directory stream ends. + // + break; + } + + // + // Iterate over all records in DirentBuf. Primarily, forget them all. + // Secondarily, if a record proves transformable to EFI_FILE_INFO, add= it + // to the EFI_FILE_INFO cache (unless the cache is full). + // + Consumed =3D 0; + while (Remaining >=3D sizeof (VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE)) { + VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE *Dirent; + UINT32 DirentSize; + + Dirent =3D (VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE *)(DirentBuf + Consum= ed); + DirentSize =3D (UINT32)VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE ( + Dirent->Namelen); + if (DirentSize =3D=3D 0) { + // + // This means one of two things: (a) Dirent->Namelen is zero, or (= b) + // (b) Dirent->Namelen is unsupportably large. (a) is just invalid= for + // the Virtio Filesystem device to send, while (b) shouldn't happen + // because "FilesysAttr.Namelen" -- the maximum filename length + // supported by the filesystem -- proved acceptable above. + // + Status =3D EFI_PROTOCOL_ERROR; + goto FreeFileInfoArray; + } + if (DirentSize > Remaining) { + // + // Dirent->Namelen suggests that the filename byte array (plus any + // padding) are truncated. This should never happen; the Virtio + // Filesystem device is supposed to send complete entries only. + // + Status =3D EFI_PROTOCOL_ERROR; + goto FreeFileInfoArray; + } + if (Dirent->Namelen > FilesysAttr.Namelen) { + // + // This is possible without tripping the truncation check above, d= ue to + // how entries are padded. The condition means that Dirent->Namele= n is + // reportedly larger than the filesystem limit, without spilling i= nto + // the next alignment bucket. Should never happen. + // + Status =3D EFI_PROTOCOL_ERROR; + goto FreeFileInfoArray; + } + + // + // If we haven't filled the EFI_FILE_INFO cache yet, attempt transfo= rming + // Dirent to EFI_FILE_INFO. + // + if (NumFileInfo < VIRTIO_FS_FILE_MAX_FILE_INFO) { + EFI_FILE_INFO *FileInfo; + + FileInfo =3D (EFI_FILE_INFO *)(FileInfoArray + + (NumFileInfo * SingleFileInfoSize)); + Status =3D PopulateFileInfo (Dirent, SingleFileInfoSize, FileInfo); + if (!EFI_ERROR (Status)) { + // + // Dirent has been transformed and cached successfully. + // + NumFileInfo++; + // + // The next time we refill the cache, restart reading the direct= ory + // stream right after the entry that we've just transformed and + // cached. + // + CacheEndsAtCookie =3D Dirent->CookieForNextEntry; + } + // + // If Dirent wasn't transformable to an EFI_FILE_INFO, we'll just = skip + // it. + // + } + + // + // Make the Virtio Filesystem device forget the NodeId in this direc= tory + // entry, as we'll need it no more. (The "." and ".." entries need no + // FUSE_FORGET requests, when returned by FUSE_READDIRPLUS -- and so= the + // Virtio Filesystem device reports their NodeId fields as zero.) + // + if (Dirent->NodeResp.NodeId !=3D 0) { + VirtioFsFuseForget (VirtioFs, Dirent->NodeResp.NodeId); + } + + // + // Advance to the next entry in DirentBuf. + // + DirStreamCookie =3D Dirent->CookieForNextEntry; + Consumed +=3D DirentSize; + Remaining -=3D DirentSize; + } + + if (Remaining > 0) { + // + // This suggests that a VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE header was + // truncated. This should never happen; the Virtio Filesystem device= is + // supposed to send complete entries only. + // + Status =3D EFI_PROTOCOL_ERROR; + goto FreeFileInfoArray; + } + // + // Fetch another DirentBuf from the directory stream, unless we've fil= led + // the EFI_FILE_INFO cache. + // + } while (NumFileInfo < VIRTIO_FS_FILE_MAX_FILE_INFO); + + // + // Commit the results. (Note that the result may be an empty cache.) + // + if (VirtioFsFile->FileInfoArray !=3D NULL) { + FreePool (VirtioFsFile->FileInfoArray); + } + VirtioFsFile->FileInfoArray =3D FileInfoArray; + VirtioFsFile->SingleFileInfoSize =3D SingleFileInfoSize; + VirtioFsFile->NumFileInfo =3D NumFileInfo; + VirtioFsFile->NextFileInfo =3D 0; + VirtioFsFile->FilePosition =3D CacheEndsAtCookie; + + FreePool (DirentBuf); + return EFI_SUCCESS; + +FreeFileInfoArray: + FreePool (FileInfoArray); + +FreeDirentBuf: + FreePool (DirentBuf); + + return Status; +} + +/** + Read an entry from the EFI_FILE_INFO cache. +**/ +STATIC +EFI_STATUS +ReadFileInfoCache ( + IN OUT VIRTIO_FS_FILE *VirtioFsFile, + IN OUT UINTN *BufferSize, + OUT VOID *Buffer + ) +{ + EFI_FILE_INFO *FileInfo; + UINTN CallerAllocated; + + // + // Refill the cache if needed. If the refill doesn't produce any new + // EFI_FILE_INFO, report End of Directory, by setting (*BufferSize) to 0. + // + if (VirtioFsFile->NextFileInfo =3D=3D VirtioFsFile->NumFileInfo) { + EFI_STATUS Status; + + Status =3D RefillFileInfoCache (VirtioFsFile); + if (EFI_ERROR (Status)) { + return (Status =3D=3D EFI_BUFFER_TOO_SMALL) ? EFI_DEVICE_ERROR : Sta= tus; + } + if (VirtioFsFile->NumFileInfo =3D=3D 0) { + *BufferSize =3D 0; + return EFI_SUCCESS; + } + } + FileInfo =3D (EFI_FILE_INFO *)(VirtioFsFile->FileInfoArray + + (VirtioFsFile->NextFileInfo * + VirtioFsFile->SingleFileInfoSize)); + + // + // Check if the caller is ready to accept FileInfo. If not, we'll just + // present the required size for now. + // + // (The (UINTN) cast below is safe because FileInfo->Size has been reduc= ed + // from VirtioFsFile->SingleFileInfoSize, in + // + // RefillFileInfoCache() + // PopulateFileInfo() + // VirtioFsFuseDirentPlusToEfiFileInfo() + // + // and VirtioFsFile->SingleFileInfoSize was computed from + // FilesysAttr.Namelen, which had been accepted by + // VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE().) + // + CallerAllocated =3D *BufferSize; + *BufferSize =3D (UINTN)FileInfo->Size; + if (CallerAllocated < *BufferSize) { + return EFI_BUFFER_TOO_SMALL; + } + // + // Output FileInfo, and remove it from the cache. + // + CopyMem (Buffer, FileInfo, *BufferSize); + VirtioFsFile->NextFileInfo++; + return EFI_SUCCESS; +} + /** Read from a regular file. **/ STATIC EFI_STATUS ReadRegularFile ( IN OUT VIRTIO_FS_FILE *VirtioFsFile, IN OUT UINTN *BufferSize, @@ -80,14 +421,14 @@ VirtioFsSimpleFileRead ( ) { VIRTIO_FS_FILE *VirtioFsFile; EFI_STATUS Status; =20 VirtioFsFile =3D VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This); =20 if (VirtioFsFile->IsDirectory) { - Status =3D EFI_NO_MEDIA; + Status =3D ReadFileInfoCache (VirtioFsFile, BufferSize, Buffer); } else { Status =3D ReadRegularFile (VirtioFsFile, BufferSize, Buffer); } return Status; } --=20 2.19.1.3.g30247aa5d201 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#69050): https://edk2.groups.io/g/devel/message/69050 Mute This Topic: https://groups.io/mt/79024159/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-