]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseRead.c
UefiPayloadPkg: Fix build error
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / FuseRead.c
1 /** @file
2 FUSE_READ / FUSE_READDIRPLUS wrapper for the Virtio Filesystem device.
3
4 Copyright (C) 2020, Red Hat, Inc.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include "VirtioFsDxe.h"
10
11 /**
12 Read a chunk from a regular file or a directory stream, by sending the
13 FUSE_READ / FUSE_READDIRPLUS request to the Virtio Filesystem device.
14
15 The function may only be called after VirtioFsFuseInitSession() returns
16 successfully and before VirtioFsUninit() is called.
17
18 @param[in,out] VirtioFs The Virtio Filesystem device to send the FUSE_READ
19 or FUSE_READDIRPLUS request to. On output, the FUSE
20 request counter "VirtioFs->RequestId" will have been
21 incremented.
22
23 @param[in] NodeId The inode number of the regular file or directory
24 stream to read from.
25
26 @param[in] FuseHandle The open handle to the regular file or directory
27 stream to read from.
28
29 @param[in] IsDir TRUE if NodeId and FuseHandle refer to a directory,
30 FALSE if NodeId and FuseHandle refer to a regular
31 file.
32
33 @param[in] Offset If IsDir is FALSE: the absolute file position at
34 which to start reading.
35
36 If IsDir is TRUE: the directory stream cookie at
37 which to start or continue reading. The zero-valued
38 cookie identifies the start of the directory stream.
39 Further positions in the directory stream can be
40 passed in from the CookieForNextEntry field of
41 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE.
42
43 @param[in,out] Size On input, the number of bytes to read. On successful
44 return, the number of bytes actually read, which may
45 be smaller than the value on input.
46
47 When reading a regular file (i.e., when IsDir is
48 FALSE), EOF can be detected by passing in a nonzero
49 Size, and finding a zero Size on output.
50
51 When reading a directory stream (i.e., when IsDir is
52 TRUE), Data consists of a sequence of variably-sized
53 records (directory entries). A read operation
54 returns the maximal sequence of records that fits in
55 Size, without having to truncate a record. In order
56 to guarantee progress, call
57
58 VirtioFsFuseStatFs (VirtioFs, NodeId,
59 &FilesysAttr)
60
61 first, to learn the maximum Namelen for the
62 directory stream. Then assign Size at least
63
64 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE (
65 FilesysAttr.Namelen)
66
67 on input. (Remember that
68 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE() may return
69 0 if its Namelen argument is invalid.) EOF can be
70 detected if Size is set on input like described
71 above, and Size is zero on output.
72
73 @param[out] Data Buffer to read the bytes from the regular file or
74 the directory stream into. The caller is responsible
75 for providing room for (at least) as many bytes in
76 Data as Size is on input.
77
78 @retval EFI_SUCCESS Read successful. The caller is responsible for checking
79 Size to learn the actual byte count transferred.
80
81 @return The "errno" value mapped to an EFI_STATUS code, if the
82 Virtio Filesystem device explicitly reported an error.
83
84 @return Error codes propagated from VirtioFsSgListsValidate(),
85 VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),
86 VirtioFsFuseCheckResponse().
87 **/
88 EFI_STATUS
89 VirtioFsFuseReadFileOrDir (
90 IN OUT VIRTIO_FS *VirtioFs,
91 IN UINT64 NodeId,
92 IN UINT64 FuseHandle,
93 IN BOOLEAN IsDir,
94 IN UINT64 Offset,
95 IN OUT UINT32 *Size,
96 OUT VOID *Data
97 )
98 {
99 VIRTIO_FS_FUSE_REQUEST CommonReq;
100 VIRTIO_FS_FUSE_READ_REQUEST ReadReq;
101 VIRTIO_FS_IO_VECTOR ReqIoVec[2];
102 VIRTIO_FS_SCATTER_GATHER_LIST ReqSgList;
103 VIRTIO_FS_FUSE_RESPONSE CommonResp;
104 VIRTIO_FS_IO_VECTOR RespIoVec[2];
105 VIRTIO_FS_SCATTER_GATHER_LIST RespSgList;
106 EFI_STATUS Status;
107 UINTN TailBufferFill;
108
109 //
110 // Set up the scatter-gather lists.
111 //
112 ReqIoVec[0].Buffer = &CommonReq;
113 ReqIoVec[0].Size = sizeof CommonReq;
114 ReqIoVec[1].Buffer = &ReadReq;
115 ReqIoVec[1].Size = sizeof ReadReq;
116 ReqSgList.IoVec = ReqIoVec;
117 ReqSgList.NumVec = ARRAY_SIZE (ReqIoVec);
118
119 RespIoVec[0].Buffer = &CommonResp;
120 RespIoVec[0].Size = sizeof CommonResp;
121 RespIoVec[1].Buffer = Data;
122 RespIoVec[1].Size = *Size;
123 RespSgList.IoVec = RespIoVec;
124 RespSgList.NumVec = ARRAY_SIZE (RespIoVec);
125
126 //
127 // Validate the scatter-gather lists; calculate the total transfer sizes.
128 //
129 Status = VirtioFsSgListsValidate (VirtioFs, &ReqSgList, &RespSgList);
130 if (EFI_ERROR (Status)) {
131 return Status;
132 }
133
134 //
135 // Populate the common request header.
136 //
137 Status = VirtioFsFuseNewRequest (
138 VirtioFs,
139 &CommonReq,
140 ReqSgList.TotalSize,
141 IsDir ? VirtioFsFuseOpReadDirPlus : VirtioFsFuseOpRead,
142 NodeId
143 );
144 if (EFI_ERROR (Status)) {
145 return Status;
146 }
147
148 //
149 // Populate the FUSE_READ- / FUSE_READDIRPLUS-specific fields.
150 //
151 ReadReq.FileHandle = FuseHandle;
152 ReadReq.Offset = Offset;
153 ReadReq.Size = *Size;
154 ReadReq.ReadFlags = 0;
155 ReadReq.LockOwner = 0;
156 ReadReq.Flags = 0;
157 ReadReq.Padding = 0;
158
159 //
160 // Submit the request.
161 //
162 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
163 if (EFI_ERROR (Status)) {
164 return Status;
165 }
166
167 //
168 // Verify the response. Note that TailBufferFill is variable.
169 //
170 Status = VirtioFsFuseCheckResponse (
171 &RespSgList,
172 CommonReq.Unique,
173 &TailBufferFill
174 );
175 if (EFI_ERROR (Status)) {
176 if (Status == EFI_DEVICE_ERROR) {
177 DEBUG ((
178 DEBUG_ERROR,
179 "%a: Label=\"%s\" NodeId=%Lu FuseHandle=%Lu "
180 "IsDir=%d Offset=0x%Lx Size=0x%x Data@%p Errno=%d\n",
181 __FUNCTION__,
182 VirtioFs->Label,
183 NodeId,
184 FuseHandle,
185 IsDir,
186 Offset,
187 *Size,
188 Data,
189 CommonResp.Error
190 ));
191 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
192 }
193
194 return Status;
195 }
196
197 //
198 // Report the actual transfer size.
199 //
200 // Integer overflow in the (UINT32) cast below is not possible; the
201 // VIRTIO_FS_SCATTER_GATHER_LIST functions would have caught that.
202 //
203 *Size = (UINT32)TailBufferFill;
204 return EFI_SUCCESS;
205 }