]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/FuseRename.c
fc9b27ccf6d4c13571c817eb9df884bf7d06b303
[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 (VirtioFs, &CommonReq, ReqSgList.TotalSize,
99 VirtioFsFuseOpRename2, OldParentNodeId);
100 if (EFI_ERROR (Status)) {
101 return Status;
102 }
103
104 //
105 // Populate the FUSE_RENAME2-specific fields.
106 //
107 Rename2Req.NewDir = NewParentNodeId;
108 Rename2Req.Flags = VIRTIO_FS_FUSE_RENAME2_REQ_F_NOREPLACE;
109 Rename2Req.Padding = 0;
110
111 //
112 // Submit the request.
113 //
114 Status = VirtioFsSgListsSubmit (VirtioFs, &ReqSgList, &RespSgList);
115 if (EFI_ERROR (Status)) {
116 return Status;
117 }
118
119 //
120 // Verify the response (all response buffers are fixed size).
121 //
122 Status = VirtioFsFuseCheckResponse (&RespSgList, CommonReq.Unique, NULL);
123 if (Status == EFI_DEVICE_ERROR) {
124 DEBUG ((DEBUG_ERROR, "%a: Label=\"%s\" OldParentNodeId=%Lu OldName=\"%a\" "
125 "NewParentNodeId=%Lu NewName=\"%a\" Errno=%d\n", __FUNCTION__,
126 VirtioFs->Label, OldParentNodeId, OldName, NewParentNodeId, NewName,
127 CommonResp.Error));
128 Status = VirtioFsErrnoToEfiStatus (CommonResp.Error);
129 }
130 return Status;
131 }