]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseRename.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / FuseRename.c
1 /** @file
2 FUSE_RENAME2 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 Rename a regular file or a directory, by sending the FUSE_RENAME2 request to
15 the Virtio Filesystem device. If the new filename exists, the request will
16 fail.
17
18 The function may only be called after VirtioFsFuseInitSession() returns
19 successfully and before VirtioFsUninit() is called.
20
21 @param[in,out] VirtioFs The Virtio Filesystem device to send the
22 FUSE_RENAME2 request to. On output, the FUSE
23 request counter "VirtioFs->RequestId" will have
24 been incremented.
25
26 @param[in] OldParentNodeId The inode number of the directory in which
27 OldName should be removed.
28
29 @param[in] OldName The single-component filename to remove in the
30 directory identified by OldParentNodeId.
31
32 @param[in] NewParentNodeId The inode number of the directory in which
33 NewName should be created, such that on
34 successful return, (NewParentNodeId, NewName)
35 refer to the same inode as (OldParentNodeId,
36 OldName) did on entry.
37
38 @param[in] NewName The single-component filename to create in the
39 directory identified by NewParentNodeId.
40
41 @retval EFI_SUCCESS The file or directory has been renamed.
42
43 @return The "errno" value mapped to an EFI_STATUS code, if the
44 Virtio Filesystem device explicitly reported an error.
45
46 @return Error codes propagated from VirtioFsSgListsValidate(),
47 VirtioFsFuseNewRequest(), VirtioFsSgListsSubmit(),
48 VirtioFsFuseCheckResponse().
49 **/
50 EFI_STATUS
51 VirtioFsFuseRename (
52 IN OUT VIRTIO_FS *VirtioFs,
53 IN UINT64 OldParentNodeId,
54 IN CHAR8 *OldName,
55 IN UINT64 NewParentNodeId,
56 IN CHAR8 *NewName
57 )
58 {
59 VIRTIO_FS_FUSE_REQUEST CommonReq;
60 VIRTIO_FS_FUSE_RENAME2_REQUEST Rename2Req;
61 VIRTIO_FS_IO_VECTOR ReqIoVec[4];
62 VIRTIO_FS_SCATTER_GATHER_LIST ReqSgList;
63 VIRTIO_FS_FUSE_RESPONSE CommonResp;
64 VIRTIO_FS_IO_VECTOR RespIoVec[1];
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 = &Rename2Req;
74 ReqIoVec[1].Size = sizeof Rename2Req;
75 ReqIoVec[2].Buffer = OldName;
76 ReqIoVec[2].Size = AsciiStrSize (OldName);
77 ReqIoVec[3].Buffer = NewName;
78 ReqIoVec[3].Size = AsciiStrSize (NewName);
79 ReqSgList.IoVec = ReqIoVec;
80 ReqSgList.NumVec = ARRAY_SIZE (ReqIoVec);
81
82 RespIoVec[0].Buffer = &CommonResp;
83 RespIoVec[0].Size = sizeof CommonResp;
84 RespSgList.IoVec = RespIoVec;
85 RespSgList.NumVec = ARRAY_SIZE (RespIoVec);
86
87 //
88 // Validate the scatter-gather lists; calculate the total transfer sizes.
89 //
90 Status = VirtioFsSgListsValidate (VirtioFs, &ReqSgList, &RespSgList);
91 if (EFI_ERROR (Status)) {
92 return Status;
93 }
94
95 //
96 // Populate the common request header.
97 //
98 Status = VirtioFsFuseNewRequest (
99 VirtioFs,
100 &CommonReq,
101 ReqSgList.TotalSize,
102 VirtioFsFuseOpRename2,
103 OldParentNodeId
104 );
105 if (EFI_ERROR (Status)) {
106 return Status;
107 }
108
109 //
110 // Populate the FUSE_RENAME2-specific fields.
111 //
112 Rename2Req.NewDir = NewParentNodeId;
113 Rename2Req.Flags = VIRTIO_FS_FUSE_RENAME2_REQ_F_NOREPLACE;
114 Rename2Req.Padding = 0;
115
116 //
117 // Submit the request.
118 //
119 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
120 if (EFI_ERROR (Status)) {
121 return Status;
122 }
123
124 //
125 // Verify the response (all response buffers are fixed size).
126 //
127 Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);
128 if (Status == EFI_DEVICE_ERROR) {
129 DEBUG ((
130 DEBUG_ERROR,
131 "%a: Label=\"%s\" OldParentNodeId=%Lu OldName=\"%a\" "
132 "NewParentNodeId=%Lu NewName=\"%a\" Errno=%d\n",
133 __FUNCTION__,
134 VirtioFs->Label,
135 OldParentNodeId,
136 OldName,
137 NewParentNodeId,
138 NewName,
139 CommonResp.Error
140 ));
141 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
142 }
143
144 return Status;
145 }