2 FUSE_CREATE wrapper for the Virtio Filesystem device.
4 Copyright (C) 2020, Red Hat, Inc.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/BaseLib.h> // AsciiStrSize()
11 #include "VirtioFsDxe.h"
14 Send a FUSE_CREATE request to the Virtio Filesystem device, for opening a
15 regular file with (O_RDWR | O_CREAT) semantics.
17 The function may only be called after VirtioFsFuseInitSession() returns
18 successfully and before VirtioFsUninit() is called.
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.
24 @param[in] ParentNodeId The inode number of the direct parent directory of
25 the regular file to open or create.
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.
31 @param[out] NodeId The inode number of the regular file, returned by
32 the Virtio Filesystem device.
34 @param[out] FuseHandle The open file handle returned by the Virtio
37 @retval EFI_SUCCESS The regular file has been opened, and (if necessary)
40 @return The "errno" value mapped to an EFI_STATUS code, if the
41 Virtio Filesystem device explicitly reported an error.
43 @return Error codes propagated from VirtioFsSgListsValidate(),
44 VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),
45 VirtioFsFuseCheckResponse().
48 VirtioFsFuseOpenOrCreate (
49 IN OUT VIRTIO_FS
*VirtioFs
,
50 IN UINT64 ParentNodeId
,
53 OUT UINT64
*FuseHandle
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
;
69 // Set up the scatter-gather lists.
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
);
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
);
92 // Validate the scatter-gather lists; calculate the total transfer sizes.
94 Status
= VirtioFsSgListsValidate (VirtioFs
, &ReqSgList
, &RespSgList
);
95 if (EFI_ERROR (Status
)) {
100 // Populate the common request header.
102 Status
= VirtioFsFuseNewRequest (
106 VirtioFsFuseOpCreate
,
109 if (EFI_ERROR (Status
)) {
114 // Populate the FUSE_CREATE-specific fields.
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.
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
);
129 CreateReq
.Padding
= 0;
132 // Submit the request.
134 Status
= VirtioFsSgListsSubmit (VirtioFs
, &ReqSgList
, &RespSgList
);
135 if (EFI_ERROR (Status
)) {
140 // Verify the response (all response buffers are fixed size).
142 Status
= VirtioFsFuseCheckResponse (&RespSgList
, CommonReq
.Unique
, NULL
);
143 if (EFI_ERROR (Status
)) {
144 if (Status
== EFI_DEVICE_ERROR
) {
147 "%a: Label=\"%s\" ParentNodeId=%Lu Name=\"%a\" "
155 Status
= VirtioFsErrnoToEfiStatus (CommonResp
.Error
);
162 // Output the NodeId of the (possibly new) regular file. Also output the open
165 *NodeId
= NodeResp
.NodeId
;
166 *FuseHandle
= OpenResp
.FileHandle
;