]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/SimpleFsWrite.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / SimpleFsWrite.c
1 /** @file
2 EFI_FILE_PROTOCOL.Write() member function for the Virtio Filesystem driver.
3
4 Copyright (C) 2020, Red Hat, Inc.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
9 #include "VirtioFsDxe.h"
10
11 EFI_STATUS
12 EFIAPI
13 VirtioFsSimpleFileWrite (
14 IN EFI_FILE_PROTOCOL *This,
15 IN OUT UINTN *BufferSize,
16 IN VOID *Buffer
17 )
18 {
19 VIRTIO_FS_FILE *VirtioFsFile;
20 VIRTIO_FS *VirtioFs;
21 EFI_STATUS Status;
22 UINTN Transferred;
23 UINTN Left;
24
25 VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
26 VirtioFs = VirtioFsFile->OwnerFs;
27
28 if (VirtioFsFile->IsDirectory) {
29 return EFI_UNSUPPORTED;
30 }
31
32 if (!VirtioFsFile->IsOpenForWriting) {
33 return EFI_ACCESS_DENIED;
34 }
35
36 Status = EFI_SUCCESS;
37 Transferred = 0;
38 Left = *BufferSize;
39 while (Left > 0) {
40 UINT32 WriteSize;
41
42 //
43 // Honor the write buffer size limit.
44 //
45 WriteSize = (UINT32)MIN ((UINTN)VirtioFs->MaxWrite, Left);
46 Status = VirtioFsFuseWrite (
47 VirtioFs,
48 VirtioFsFile->NodeId,
49 VirtioFsFile->FuseHandle,
50 VirtioFsFile->FilePosition + Transferred,
51 &WriteSize,
52 (UINT8 *)Buffer + Transferred
53 );
54 if (!EFI_ERROR (Status) && (WriteSize == 0)) {
55 //
56 // Progress should have been made.
57 //
58 Status = EFI_DEVICE_ERROR;
59 }
60
61 if (EFI_ERROR (Status)) {
62 break;
63 }
64
65 Transferred += WriteSize;
66 Left -= WriteSize;
67 }
68
69 *BufferSize = Transferred;
70 VirtioFsFile->FilePosition += Transferred;
71 //
72 // According to the UEFI spec,
73 //
74 // - 'Partial writes only occur when there has been a data error during the
75 // write attempt (such as "file space full")', and
76 //
77 // - (as an example) EFI_VOLUME_FULL is returned when 'The volume is full'.
78 //
79 // These together imply that after a partial write, we have to return an
80 // error. In other words, (Transferred > 0) is inconsequential for the return
81 // value.
82 //
83 return Status;
84 }