]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseOpenOrCreate.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / FuseOpenOrCreate.c
1 /** @file
2 FUSE_CREATE 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 <Library/BaseLib.h> // AsciiStrSize()
10
11 #include "VirtioFsDxe.h"
12
13 /**
14 Send a FUSE_CREATE request to the Virtio Filesystem device, for opening a
15 regular file with (O_RDWR | O_CREAT) semantics.
16
17 The function may only be called after VirtioFsFuseInitSession() returns
18 successfully and before VirtioFsUninit() is called.
19
20 @param[in,out] VirtioFs The Virtio Filesystem device to send the FUSE_CREATE
21 request to. On output, the FUSE request counter
22 "VirtioFs->RequestId" will have been incremented.
23
24 @param[in] ParentNodeId The inode number of the direct parent directory of
25 the regular file to open or create.
26
27 @param[in] Name The single-component filename of the regular file to
28 open or create, under the parent directory
29 identified by ParentNodeId.
30
31 @param[out] NodeId The inode number of the regular file, returned by
32 the Virtio Filesystem device.
33
34 @param[out] FuseHandle The open file handle returned by the Virtio
35 Filesystem device.
36
37 @retval EFI_SUCCESS The regular file has been opened, and (if necessary)
38 created.
39
40 @return The "errno" value mapped to an EFI_STATUS code, if the
41 Virtio Filesystem device explicitly reported an error.
42
43 @return Error codes propagated from VirtioFsSgListsValidate(),
44 VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),
45 VirtioFsFuseCheckResponse().
46 **/
47 EFI_STATUS
48 VirtioFsFuseOpenOrCreate (
49 IN OUT VIRTIO_FS *VirtioFs,
50 IN UINT64 ParentNodeId,
51 IN CHAR8 *Name,
52 OUT UINT64 *NodeId,
53 OUT UINT64 *FuseHandle
54 )
55 {
56 VIRTIO_FS_FUSE_REQUEST CommonReq;
57 VIRTIO_FS_FUSE_CREATE_REQUEST CreateReq;
58 VIRTIO_FS_IO_VECTOR ReqIoVec[3];
59 VIRTIO_FS_SCATTER_GATHER_LIST ReqSgList;
60 VIRTIO_FS_FUSE_RESPONSE CommonResp;
61 VIRTIO_FS_FUSE_NODE_RESPONSE NodeResp;
62 VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE AttrResp;
63 VIRTIO_FS_FUSE_OPEN_RESPONSE OpenResp;
64 VIRTIO_FS_IO_VECTOR RespIoVec[4];
65 VIRTIO_FS_SCATTER_GATHER_LIST RespSgList;
66 EFI_STATUS Status;
67
68 //
69 // Set up the scatter-gather lists.
70 //
71 ReqIoVec[0].Buffer = &CommonReq;
72 ReqIoVec[0].Size = sizeof CommonReq;
73 ReqIoVec[1].Buffer = &CreateReq;
74 ReqIoVec[1].Size = sizeof CreateReq;
75 ReqIoVec[2].Buffer = Name;
76 ReqIoVec[2].Size = AsciiStrSize (Name);
77 ReqSgList.IoVec = ReqIoVec;
78 ReqSgList.NumVec = ARRAY_SIZE (ReqIoVec);
79
80 RespIoVec[0].Buffer = &CommonResp;
81 RespIoVec[0].Size = sizeof CommonResp;
82 RespIoVec[1].Buffer = &NodeResp;
83 RespIoVec[1].Size = sizeof NodeResp;
84 RespIoVec[2].Buffer = &AttrResp;
85 RespIoVec[2].Size = sizeof AttrResp;
86 RespIoVec[3].Buffer = &OpenResp;
87 RespIoVec[3].Size = sizeof OpenResp;
88 RespSgList.IoVec = RespIoVec;
89 RespSgList.NumVec = ARRAY_SIZE (RespIoVec);
90
91 //
92 // Validate the scatter-gather lists; calculate the total transfer sizes.
93 //
94 Status = VirtioFsSgListsValidate (VirtioFs, &ReqSgList, &RespSgList);
95 if (EFI_ERROR (Status)) {
96 return Status;
97 }
98
99 //
100 // Populate the common request header.
101 //
102 Status = VirtioFsFuseNewRequest (
103 VirtioFs,
104 &CommonReq,
105 ReqSgList.TotalSize,
106 VirtioFsFuseOpCreate,
107 ParentNodeId
108 );
109 if (EFI_ERROR (Status)) {
110 return Status;
111 }
112
113 //
114 // Populate the FUSE_CREATE-specific fields.
115 //
116 // VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR is why this request can never open a
117 // directory (EISDIR). And VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR is consistent with
118 // the only OpenMode of EFI_FILE_PROTOCOL.Open() that enables filesystem
119 // object creation -- that is, Create/Read/Write.
120 //
121 CreateReq.Flags = VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR;
122 CreateReq.Mode = (VIRTIO_FS_FUSE_MODE_PERM_RUSR |
123 VIRTIO_FS_FUSE_MODE_PERM_WUSR |
124 VIRTIO_FS_FUSE_MODE_PERM_RGRP |
125 VIRTIO_FS_FUSE_MODE_PERM_WGRP |
126 VIRTIO_FS_FUSE_MODE_PERM_ROTH |
127 VIRTIO_FS_FUSE_MODE_PERM_WOTH);
128 CreateReq.Umask = 0;
129 CreateReq.Padding = 0;
130
131 //
132 // Submit the request.
133 //
134 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
135 if (EFI_ERROR (Status)) {
136 return Status;
137 }
138
139 //
140 // Verify the response (all response buffers are fixed size).
141 //
142 Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);
143 if (EFI_ERROR (Status)) {
144 if (Status == EFI_DEVICE_ERROR) {
145 DEBUG ((
146 DEBUG_ERROR,
147 "%a: Label=\"%s\" ParentNodeId=%Lu Name=\"%a\" "
148 "Errno=%d\n",
149 __FUNCTION__,
150 VirtioFs->Label,
151 ParentNodeId,
152 Name,
153 CommonResp.Error
154 ));
155 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
156 }
157
158 return Status;
159 }
160
161 //
162 // Output the NodeId of the (possibly new) regular file. Also output the open
163 // file handle.
164 //
165 *NodeId = NodeResp.NodeId;
166 *FuseHandle = OpenResp.FileHandle;
167 return EFI_SUCCESS;
168 }