]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseOpenOrCreate.c
MdePkg/Include: Add STATIC_ASSERT for L'' and L"" strings
[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 (VirtioFs, &CommonReq, ReqSgList.TotalSize,
103 VirtioFsFuseOpCreate, ParentNodeId);
104 if (EFI_ERROR (Status)) {
105 return Status;
106 }
107
108 //
109 // Populate the FUSE_CREATE-specific fields.
110 //
111 // VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR is why this request can never open a
112 // directory (EISDIR). And VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR is consistent with
113 // the only OpenMode of EFI_FILE_PROTOCOL.Open() that enables filesystem
114 // object creation -- that is, Create/Read/Write.
115 //
116 CreateReq.Flags = VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR;
117 CreateReq.Mode = (VIRTIO_FS_FUSE_MODE_PERM_RUSR |
118 VIRTIO_FS_FUSE_MODE_PERM_WUSR |
119 VIRTIO_FS_FUSE_MODE_PERM_RGRP |
120 VIRTIO_FS_FUSE_MODE_PERM_WGRP |
121 VIRTIO_FS_FUSE_MODE_PERM_ROTH |
122 VIRTIO_FS_FUSE_MODE_PERM_WOTH);
123 CreateReq.Umask = 0;
124 CreateReq.Padding = 0;
125
126 //
127 // Submit the request.
128 //
129 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
130 if (EFI_ERROR (Status)) {
131 return Status;
132 }
133
134 //
135 // Verify the response (all response buffers are fixed size).
136 //
137 Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);
138 if (EFI_ERROR (Status)) {
139 if (Status == EFI_DEVICE_ERROR) {
140 DEBUG ((DEBUG_ERROR, "%a: Label=\"%s\" ParentNodeId=%Lu Name=\"%a\" "
141 "Errno=%d\n", __FUNCTION__, VirtioFs->Label, ParentNodeId, Name,
142 CommonResp.Error));
143 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
144 }
145 return Status;
146 }
147
148 //
149 // Output the NodeId of the (possibly new) regular file. Also output the open
150 // file handle.
151 //
152 *NodeId = NodeResp.NodeId;
153 *FuseHandle = OpenResp.FileHandle;
154 return EFI_SUCCESS;
155 }