2 Type and macro definitions specific to the Virtio Filesystem device.
4 At the time of this writing, the latest released Virtio specification (v1.1)
5 does not include the virtio-fs device. The development version of the
6 specification defines it however; see the latest version at
7 <https://github.com/oasis-tcs/virtio-spec/blob/87fa6b5d8155/virtio-fs.tex>.
9 This header file is minimal, and only defines the types and macros that are
10 necessary for the OvmfPkg implementation.
12 Copyright (C) 2020, Red Hat, Inc.
14 SPDX-License-Identifier: BSD-2-Clause-Patent
20 #include <IndustryStandard/Virtio.h>
23 // Lowest numbered queue for sending normal priority requests.
25 #define VIRTIO_FS_REQUEST_QUEUE 1
28 // Number of bytes in the "VIRTIO_FS_CONFIG.Tag" field.
30 #define VIRTIO_FS_TAG_BYTES 36
33 // Device configuration layout.
38 // The Tag field can be considered the filesystem label, or a mount point
39 // hint. It is UTF-8 encoded, and padded to full size with NUL bytes. If the
40 // encoded bytes take up the entire Tag field, then there is no NUL
43 UINT8 Tag
[VIRTIO_FS_TAG_BYTES
];
45 // The total number of request virtqueues exposed by the device (i.e.,
46 // excluding the "hiprio" queue).
53 // FUSE-related definitions follow.
55 // From virtio-v1.1-cs01-87fa6b5d8155, 5.11 File System Device: "[...] The
56 // driver acts as the FUSE client mounting the file system. The virtio file
57 // system device provides the mechanism for transporting FUSE requests [...]"
59 // Unfortunately, the documentation of the FUSE wire protocol is lacking. The
60 // Virtio spec (as of this writing) simply defers to
61 // "include/uapi/linux/fuse.h" in the Linux kernel source -- see the reference
62 // in virtio spec file "introduction.tex", at commit 87fa6b5d8155.
64 // Of course, "include/uapi/linux/fuse.h" is a moving target (the virtio spec
65 // does not specify a particular FUSE interface version). The OvmfPkg code
66 // targets version 7.31, because that's the lowest version that the QEMU
67 // virtio-fs daemon supports at this time -- see QEMU commit 72c42e2d6551
68 // ("virtiofsd: Trim out compatibility code", 2020-01-23).
70 // Correspondingly, Linux's "include/uapi/linux/fuse.h" is consulted as checked
71 // out at commit (c6ff213fe5b8^) = d78092e4937d ("fuse: fix page dereference
72 // after free", 2020-09-18); that is, right before commit c6ff213fe5b8 ("fuse:
73 // add submount support to <uapi/linux/fuse.h>", 2020-09-18) introduces FUSE
74 // interface version 7.32.
76 #define VIRTIO_FS_FUSE_MAJOR 7
77 #define VIRTIO_FS_FUSE_MINOR 31
80 // The inode number of the root directory.
82 #define VIRTIO_FS_FUSE_ROOT_DIR_NODE_ID 1
85 // Distinguished errno values.
87 #define VIRTIO_FS_FUSE_ERRNO_ENOENT (-2)
90 // File mode bitmasks.
92 #define VIRTIO_FS_FUSE_MODE_TYPE_MASK 0170000u
93 #define VIRTIO_FS_FUSE_MODE_TYPE_REG 0100000u
94 #define VIRTIO_FS_FUSE_MODE_TYPE_DIR 0040000u
95 #define VIRTIO_FS_FUSE_MODE_PERM_RWXU 0000700u
96 #define VIRTIO_FS_FUSE_MODE_PERM_RUSR 0000400u
97 #define VIRTIO_FS_FUSE_MODE_PERM_WUSR 0000200u
98 #define VIRTIO_FS_FUSE_MODE_PERM_RWXG 0000070u
99 #define VIRTIO_FS_FUSE_MODE_PERM_RGRP 0000040u
100 #define VIRTIO_FS_FUSE_MODE_PERM_WGRP 0000020u
101 #define VIRTIO_FS_FUSE_MODE_PERM_RWXO 0000007u
102 #define VIRTIO_FS_FUSE_MODE_PERM_ROTH 0000004u
103 #define VIRTIO_FS_FUSE_MODE_PERM_WOTH 0000002u
106 // Flags for VirtioFsFuseOpSetAttr, in the VIRTIO_FS_FUSE_SETATTR_REQUEST.Valid
109 #define VIRTIO_FS_FUSE_SETATTR_REQ_F_MODE BIT0
110 #define VIRTIO_FS_FUSE_SETATTR_REQ_F_SIZE BIT3
111 #define VIRTIO_FS_FUSE_SETATTR_REQ_F_ATIME BIT4
112 #define VIRTIO_FS_FUSE_SETATTR_REQ_F_MTIME BIT5
115 // Flags for VirtioFsFuseOpOpen.
117 #define VIRTIO_FS_FUSE_OPEN_REQ_F_RDONLY 0
118 #define VIRTIO_FS_FUSE_OPEN_REQ_F_RDWR 2
121 // Flags for VirtioFsFuseOpInit.
123 #define VIRTIO_FS_FUSE_INIT_REQ_F_DO_READDIRPLUS BIT13
126 Macro for calculating the size of a directory stream entry.
128 The macro may evaluate Namelen multiple times.
130 The macro evaluates to a UINTN value that is safe to cast to UINT32.
132 @param[in] Namelen The size of the filename byte array that follows
133 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE in the directory
134 stream, as reported by
135 VIRTIO_FS_FUSE_STATFS_RESPONSE.Namelen or
136 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE.Namelen. The filename
137 byte array is not NUL-terminated.
139 @retval 0 Namelen was zero or greater than SIZE_4KB.
141 @return The number of bytes in the directory entry, including the
142 VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE header.
144 #define VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE_SIZE(Namelen) \
145 ((Namelen) == 0 || (Namelen) > SIZE_4KB ? \
148 sizeof (VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE) + (UINTN)(Namelen), \
154 // Flags for VirtioFsFuseOpRename2.
156 #define VIRTIO_FS_FUSE_RENAME2_REQ_F_NOREPLACE BIT0
159 // FUSE operation codes.
162 VirtioFsFuseOpLookup
= 1,
163 VirtioFsFuseOpForget
= 2,
164 VirtioFsFuseOpGetAttr
= 3,
165 VirtioFsFuseOpSetAttr
= 4,
166 VirtioFsFuseOpMkDir
= 9,
167 VirtioFsFuseOpUnlink
= 10,
168 VirtioFsFuseOpRmDir
= 11,
169 VirtioFsFuseOpOpen
= 14,
170 VirtioFsFuseOpRead
= 15,
171 VirtioFsFuseOpWrite
= 16,
172 VirtioFsFuseOpStatFs
= 17,
173 VirtioFsFuseOpRelease
= 18,
174 VirtioFsFuseOpFsync
= 20,
175 VirtioFsFuseOpFlush
= 25,
176 VirtioFsFuseOpInit
= 26,
177 VirtioFsFuseOpOpenDir
= 27,
178 VirtioFsFuseOpReleaseDir
= 29,
179 VirtioFsFuseOpFsyncDir
= 30,
180 VirtioFsFuseOpCreate
= 35,
181 VirtioFsFuseOpReadDirPlus
= 44,
182 VirtioFsFuseOpRename2
= 45,
183 } VIRTIO_FS_FUSE_OPCODE
;
187 // Request-response headers common to all request types.
198 } VIRTIO_FS_FUSE_REQUEST
;
204 } VIRTIO_FS_FUSE_RESPONSE
;
207 // Structure with which the Virtio Filesystem device reports a NodeId to the
208 // FUSE client (i.e., to the Virtio Filesystem driver). This structure is a
209 // part of the response headers for operations that inform the FUSE client of
217 UINT32 EntryValidNsec
;
218 UINT32 AttrValidNsec
;
219 } VIRTIO_FS_FUSE_NODE_RESPONSE
;
222 // Structure describing the host-side attributes of an inode. This structure is
223 // a part of the response headers for operations that inform the FUSE client of
243 } VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE
;
246 // Header for VirtioFsFuseOpForget.
249 UINT64 NumberOfLookups
;
250 } VIRTIO_FS_FUSE_FORGET_REQUEST
;
253 // Headers for VirtioFsFuseOpGetAttr (VIRTIO_FS_FUSE_GETATTR_RESPONSE is also
254 // for VirtioFsFuseOpSetAttr).
260 } VIRTIO_FS_FUSE_GETATTR_REQUEST
;
264 UINT32 AttrValidNsec
;
266 } VIRTIO_FS_FUSE_GETATTR_RESPONSE
;
269 // Header for VirtioFsFuseOpSetAttr.
288 } VIRTIO_FS_FUSE_SETATTR_REQUEST
;
291 // Header for VirtioFsFuseOpMkDir.
296 } VIRTIO_FS_FUSE_MKDIR_REQUEST
;
299 // Headers for VirtioFsFuseOpOpen and VirtioFsFuseOpOpenDir.
304 } VIRTIO_FS_FUSE_OPEN_REQUEST
;
310 } VIRTIO_FS_FUSE_OPEN_RESPONSE
;
313 // Header for VirtioFsFuseOpRead and VirtioFsFuseOpReadDirPlus.
323 } VIRTIO_FS_FUSE_READ_REQUEST
;
326 // Headers for VirtioFsFuseOpWrite.
336 } VIRTIO_FS_FUSE_WRITE_REQUEST
;
341 } VIRTIO_FS_FUSE_WRITE_RESPONSE
;
344 // Header for VirtioFsFuseOpStatFs.
357 } VIRTIO_FS_FUSE_STATFS_RESPONSE
;
360 // Header for VirtioFsFuseOpRelease and VirtioFsFuseOpReleaseDir.
367 } VIRTIO_FS_FUSE_RELEASE_REQUEST
;
370 // Header for VirtioFsFuseOpFsync and VirtioFsFuseOpFsyncDir.
376 } VIRTIO_FS_FUSE_FSYNC_REQUEST
;
379 // Header for VirtioFsFuseOpFlush.
386 } VIRTIO_FS_FUSE_FLUSH_REQUEST
;
389 // Headers for VirtioFsFuseOpInit.
396 } VIRTIO_FS_FUSE_INIT_REQUEST
;
403 UINT16 MaxBackground
;
404 UINT16 CongestionThreshold
;
410 } VIRTIO_FS_FUSE_INIT_RESPONSE
;
413 // Header for VirtioFsFuseOpCreate.
420 } VIRTIO_FS_FUSE_CREATE_REQUEST
;
423 // Header for VirtioFsFuseOpReadDirPlus.
425 // Diverging from the rest of the headers, this structure embeds other
426 // structures. The reason is that a scatter list cannot be used to receive
427 // NodeResp and AttrResp separately; the record below is followed by a variable
428 // size filename byte array, and then such pairs are repeated a number of
429 // times. Thus, later header start offsets depend on earlier filename array
433 VIRTIO_FS_FUSE_NODE_RESPONSE NodeResp
;
434 VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE AttrResp
;
436 UINT64 CookieForNextEntry
;
439 } VIRTIO_FS_FUSE_DIRENTPLUS_RESPONSE
;
442 // Header for VirtioFsFuseOpRename2.
448 } VIRTIO_FS_FUSE_RENAME2_REQUEST
;
451 #endif // VIRTIO_FS_H_