]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseOpen.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / FuseOpen.c
1 /** @file
2 FUSE_OPEN 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 Send a FUSE_OPEN request to the Virtio Filesystem device, for opening a
13 regular file.
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_OPEN
19 request to. On output, the FUSE request counter
20 "VirtioFs->RequestId" will have been incremented.
21
22 @param[in] NodeId The inode number of the regular file to open.
23
24 @param[in] ReadWrite If TRUE, open the regular file in read-write mode.
25 If FALSE, open the regular file in read-only mode.
26
27 @param[out] FuseHandle The open handle to the regular file, returned by the
28 Virtio Filesystem device.
29
30 @retval EFI_SUCCESS The regular file has been opened.
31
32 @return The "errno" value mapped to an EFI_STATUS code, if the
33 Virtio Filesystem device explicitly reported an error.
34
35 @return Error codes propagated from VirtioFsSgListsValidate(),
36 VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),
37 VirtioFsFuseCheckResponse().
38 **/
39 EFI_STATUS
40 VirtioFsFuseOpen (
41 IN OUT VIRTIO_FS *VirtioFs,
42 IN UINT64 NodeId,
43 IN BOOLEAN ReadWrite,
44 OUT UINT64 *FuseHandle
45 )
46 {
47 VIRTIO_FS_FUSE_REQUEST CommonReq;
48 VIRTIO_FS_FUSE_OPEN_REQUEST OpenReq;
49 VIRTIO_FS_IO_VECTOR ReqIoVec[2];
50 VIRTIO_FS_SCATTER_GATHER_LIST ReqSgList;
51 VIRTIO_FS_FUSE_RESPONSE CommonResp;
52 VIRTIO_FS_FUSE_OPEN_RESPONSE OpenResp;
53 VIRTIO_FS_IO_VECTOR RespIoVec[2];
54 VIRTIO_FS_SCATTER_GATHER_LIST RespSgList;
55 EFI_STATUS Status;
56
57 //
58 // Set up the scatter-gather lists.
59 //
60 ReqIoVec[0].Buffer = &CommonReq;
61 ReqIoVec[0].Size = sizeof CommonReq;
62 ReqIoVec[1].Buffer = &OpenReq;
63 ReqIoVec[1].Size = sizeof OpenReq;
64 ReqSgList.IoVec = ReqIoVec;
65 ReqSgList.NumVec = ARRAY_SIZE (ReqIoVec);
66
67 RespIoVec[0].Buffer = &CommonResp;
68 RespIoVec[0].Size = sizeof CommonResp;
69 RespIoVec[1].Buffer = &OpenResp;
70 RespIoVec[1].Size = sizeof OpenResp;
71 RespSgList.IoVec = RespIoVec;
72 RespSgList.NumVec = ARRAY_SIZE (RespIoVec);
73
74 //
75 // Validate the scatter-gather lists; calculate the total transfer sizes.
76 //
77 Status = VirtioFsSgListsValidate (VirtioFs, &ReqSgList, &RespSgList);
78 if (EFI_ERROR (Status)) {
79 return Status;
80 }
81
82 //
83 // Populate the common request header.
84 //
85 Status = VirtioFsFuseNewRequest (
86 VirtioFs,
87 &CommonReq,
88 ReqSgList.TotalSize,
89 VirtioFsFuseOpOpen,
90 NodeId
91 );
92 if (EFI_ERROR (Status)) {
93 return Status;
94 }
95
96 //
97 // Populate the FUSE_OPEN-specific fields.
98 //
99 OpenReq.Flags = (ReadWrite ?
100 VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR :
101 VIRTIO_FS_FUSE_OPEN_REQ_F_RDONLY);
102 OpenReq.Unused = 0;
103
104 //
105 // Submit the request.
106 //
107 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
108 if (EFI_ERROR (Status)) {
109 return Status;
110 }
111
112 //
113 // Verify the response (all response buffers are fixed size).
114 //
115 Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);
116 if (EFI_ERROR (Status)) {
117 if (Status == EFI_DEVICE_ERROR) {
118 DEBUG ((
119 DEBUG_ERROR,
120 "%a: Label=\"%s\" NodeId=%Lu ReadWrite=%d "
121 "Errno=%d\n",
122 __FUNCTION__,
123 VirtioFs->Label,
124 NodeId,
125 ReadWrite,
126 CommonResp.Error
127 ));
128 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
129 }
130
131 return Status;
132 }
133
134 //
135 // Output the open handle.
136 //
137 *FuseHandle = OpenResp.FileHandle;
138 return EFI_SUCCESS;
139 }