9pfs: Add FreeBSD support
PATCH v2 1/1 9pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for *at() system calls so doesn't require workarounds for a missing mknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin to XATTR_CREATE or XATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." on ZFS, so the prefix is trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user namespace, so this seems innocent enough.
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
---
Changes since v1:
- Handle extended attributes in the system.* namespace as well.
- Fix a typo in a comment.
- Clarify a comment.
fsdev/file-op-9p.h | 6 +- fsdev/meson.build | 2 +- hw/9pfs/9p-synth.c | 2 +- hw/9pfs/9p-util-freebsd.c | 132 ++++++++++++++++++++++++++++++++++++++ hw/9pfs/9p-util.h | 20 ++++-- hw/9pfs/9p.c | 16 ++++- hw/9pfs/meson.build | 2 + include/qemu/xattr.h | 6 +- meson.build | 8 +-- 9 files changed, 179 insertions(+), 15 deletions(-) create mode 100644 hw/9pfs/9p-util-freebsd.c
fsdev/file-op-9p.h +4 -2
@@ -21,9 +21,11 @@#ifdef CONFIG_LINUX # include <sys/vfs.h> -#endif -#ifdef CONFIG_DARWIN +#elif defined(CONFIG_DARWIN ) || defined(CONFIG_FREEBSD )# include <sys/param.h> +# ifdef CONFIG_FREEBSD +# undef MACHINE/* work around some unfortunate namespace pollution */ +# endif # include <sys/mount.h> #endif
fsdev/meson.build +1 -1
@@ -5,6 +5,6 @@ fsdev_ss.add(when: ['CONFIG_FSDEV_9P'], if_true: files( '9p-marshal.c', 'qemu-fsdev.c', ), if_false: files('qemu-fsdev-dummy.c')) -if host_os in ['linux', 'darwin'] +if host_os in ['linux', 'darwin', 'freebsd'] system_ss.add_all(fsdev_ss) endif
hw/9pfs/9p-synth.c +1 -1
@@ -451,7 +451,7 @@ static int synth_statfs(FsContext *s, V9fsPath *fs_path, stbuf->f_bsize =512 ; stbuf->f_blocks =0 ; stbuf->f_files = synth_node_count; -#ifndef CONFIG_DARWIN +#if !defined(CONFIG_DARWIN ) && !defined(CONFIG_FREEBSD ) stbuf->f_namelen =NAME_MAX ;#endif return 0 ;
hw/9pfs/9p-util-freebsd.c new file +132 -0
132 lines not on this page · raw message
hw/9pfs/9p-util.h +16 -4
@@ -21,6 +21,15 @@#define O_PATH_9P_UTIL 0#endif +#ifdef CONFIG_FREEBSD +/* +* FreeBSD does not have these flags, so we can only emulate their intended +* behaviour (racily). +*/ +#define XATTR_CREATE 0x1 +#define XATTR_REPLACE 0x2 +#endif +#if !defined(CONFIG_LINUX )/*
@@ -64,9 +73,9 @@ static inline uint64_t host_dev_to_dotl_dev(dev_t dev)static inline int errno_to_dotl (int err) {#if defined(CONFIG_LINUX )/* nothing to translate (Linux -> Linux) */ -#elif defined(CONFIG_DARWIN ) +#elif defined(CONFIG_DARWIN ) || defined(CONFIG_FREEBSD )/* -* translation mandatory for macOS hosts +* translation mandatory for non-Linux hosts * * FIXME: Only most important errnos translated here yet, this should be * extended to as many errnos being translated as possible in future.
@@ -155,13 +164,13 @@ static inline int openat_file(int dirfd, const char *name, int flags, { int fd, serrno, ret; -#ifndef CONFIG_DARWIN +#if !defined(CONFIG_DARWIN) && !defined(CONFIG_FREEBSD) again: #endif fd = qemu_openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK, mode); if (fd == -1) { -#ifndef CONFIG_DARWIN +#if !defined(CONFIG_DARWIN) && !defined(CONFIG_FREEBSD) if (errno == EPERM && (flags & O_NOATIME)) { /* * The client passed O_NOATIME but we lack permissions to honor it.
@@ -202,6 +211,9 @@ again:return fd; } +#ifdef CONFIG_FREEBSD +ssize_t fgetxattr (int dirfd,const char *name,void *value,size_t size); +#endif ssize_t fgetxattrat_nofollow (int dirfd,const char *path,const char *name,void *value,size_t size); int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
hw/9pfs/9p.c +14 -2
@@ -136,8 +136,10 @@ static int dotl_to_open_flags(int flags) { P9_DOTL_NONBLOCK, O_NONBLOCK } , { P9_DOTL_DSYNC, O_DSYNC }, { P9_DOTL_FASYNC, FASYNC }, -#ifndef CONFIG_DARWIN +#if !defined(CONFIG_DARWIN) && !defined(CONFIG_FREEBSD) { P9_DOTL_NOATIME, O_NOATIME }, +#endif +#ifndef CONFIG_DARWIN/* * On Darwin, we could map to F_NOCACHE, which is * similar, but doesn't quite have the same
@@ -3658,7 +3660,7 @@ static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) f_bavail = stbuf->f_bavail / bsize_factor; f_files = stbuf->f_files; f_ffree = stbuf->f_ffree; -#ifdef CONFIG_DARWIN +#if defined(CONFIG_DARWIN ) || defined(CONFIG_FREEBSD ) fsid_val = (unsigned int )stbuf->f_fsid.val[0 ] | (unsigned long long )stbuf->f_fsid.val[1 ] <<32 ; f_namelen =NAME_MAX ;
@@ -4050,6 +4052,16 @@ out_nofid:* Linux guests. */ #define P9_XATTR_SIZE_MAX 65536 +#elif defined(CONFIG_FREEBSD ) +/* +* FreeBSD similarly doesn't define a maximum xattr size, the limit is +* filesystem dependent. On UFS filesystems it's 2 times the filesystem block +* size, typically 32KB. On ZFS it depends on the value of the xattr property; +* with the default value there is no limit, and with xattr=sa it is 64KB. +* +* So, a limit of 64k seems reasonable here too. +*/ +#define P9_XATTR_SIZE_MAX 65536#else #error Missing definition for P9_XATTR_SIZE_MAX for this host system#endif
hw/9pfs/meson.build +2 -0
@@ -15,6 +15,8 @@ fs_ss.add(files( )) if host_os == 'darwin' fs_ss.add(files('9p-util-darwin.c')) +elif host_os == 'freebsd' + fs_ss.add(files('9p-util-freebsd.c')) elif host_os == 'linux' fs_ss.add(files('9p-util-linux.c')) endif
include/qemu/xattr.h +5 -1
@@ -26,7 +26,11 @@# define ENOATTR ENODATA# endif # ifndef CONFIG_WIN32 -# include <sys/xattr.h> +# ifdef CONFIG_FREEBSD +# include <sys/extattr.h> +# else +# include <sys/xattr.h> +# endif # endif #endif
meson.build +4 -4
@@ -2382,11 +2382,11 @@ dbus_display = get_option('dbus_display') \ .allowed() have_virtfs = get_option('virtfs') \ - .require(host_os == 'linux' or host_os == 'darwin', - error_message: 'virtio-9p (virtfs) requires Linux or macOS') \ - .require(host_os == 'linux' or cc.has_function('pthread_fchdir_np'), + .require(host_os == 'linux' or host_os == 'darwin' or host_os == 'freebsd', + error_message: 'virtio-9p (virtfs) requires Linux or macOS or FreeBSD') \ + .require(host_os != 'darwin' or cc.has_function('pthread_fchdir_np'), error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \ - .require(host_os == 'darwin' or libattr.found(), + .require(host_os != 'linux' or libattr.found(), error_message: 'virtio-9p (virtfs) on Linux requires libattr-devel') \ .disable_auto_if(not have_tools and not have_system) \ .allowed()
Signature · 1 line
2.49.0
Quoted 14 lines · CEST Mark Johnston
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." on ZFS, so the prefix is trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user namespace, so this seems innocent enough.The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Quoted 6 lines
Signed-off-by Mark Johnston <[email protected]>---
Changes since v1:
- Handle extended attributes in the system.* namespace as well.
- Fix a typo in a comment.
- Clarify a comment.
Not forgotten. I just hoped there were other reviewers or testers in the meantime, but be it.
Like I said, I don't have FreeBSD system here to test this, so I am taking your word for now that you tested this and plan to bring this into QEMU when master re-opens for new features soon.
If you have some time to adjust the commit log message above, that would be great, otherwise I can also handle this on my end later on. Looks like that comment is not adjusted for v2 yet (i.e. "user." and not mentioning "system.").
/Christian
Quoted 22 lines · Christian Schoenebeck
Quoted 14 lines · CEST Mark Johnston
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." on ZFS, so the prefix is trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user namespace, so this seems innocent enough.The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Quoted 6 lines
Signed-off-by Mark Johnston <[email protected]>---
Changes since v1:
- Handle extended attributes in the system.* namespace as well.
- Fix a typo in a comment.
- Clarify a comment.
Quoted 6 lines
Not forgotten. I just hoped there were other reviewers or testers in the meantime, but be it.
Like I said, I don't have FreeBSD system here to test this, so I am taking your word for now that you tested this and plan to bring this into QEMU when master re-opens for new features soon.
Thank you very much!
In case I missed somewhat, what testing would you typically do otherwise? So far I had run the QEMU test suite (which indeed found some bugs in the initial version) and tried mounting a 9pfs share from Linux and FreeBSD guests and doing a bit of manual testing.
Quoted 4 lines
If you have some time to adjust the commit log message above, that would be great, otherwise I can also handle this on my end later on. Looks like that comment is not adjusted for v2 yet (i.e. "user." and not mentioning "system.").
Here's an amended commit log message. Please let me know if this is better submitted as a v3.
commit b79bf1b7d42025e3e14da86a7c08d269038cd3ed
Author: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +0000
9pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for *at() system calls so doesn't require workarounds for a missing mknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin to XATTR_CREATE or XATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS, so these prefixes are trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user and system namespaces, so this seems innocent enough.
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Quoted 2 lines · CEST Mark Johnston
Quoted 1 line · Christian Schoenebeck
On Wednesday, August 6, 2025 7:53:08 PM CEST Mark Johnston wrote:
[...]
Quoted 7 lines
Quoted 6 lines
Not forgotten. I just hoped there were other reviewers or testers in the meantime, but be it.
Like I said, I don't have FreeBSD system here to test this, so I am taking your word for now that you tested this and plan to bring this into QEMU when master re-opens for new features soon.
Quoted 6 lines
Thank you very much!
In case I missed somewhat, what testing would you typically do otherwise? So far I had run the QEMU test suite (which indeed found some bugs in the initial version) and tried mounting a 9pfs share from Linux and FreeBSD guests and doing a bit of manual testing.
Apart from QEMU's test cases, I also use guest systems running 9p as root file system [1], run software compilations there among some things. That proofed to be quite a useful test environment to spot edge cases, concurrency and performance issues and such.
[1] https://wiki.qemu.org/Documentation/9p_root_fs
Greg was running some general purpose file system stress test suite in the past, but I currently can't recall what that was.
Quoted 28 lines
Quoted 6 lines
If you have some time to adjust the commit log message above, that would be
great, otherwise I can also handle this on my end later on. Looks like that
comment is not adjusted for v2 yet (i.e. "user." and not mentioning "system.").Here's an amended commit log message. Please let me know if this is better submitted as a v3.
commit
b79bf1b7d42025e3e14da86a7c08d269038cd3edAuthor: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +00009pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of
warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS, so these prefixes are trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user and system namespaces, so this seems innocent enough.
Quoted 4 lines
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Signed-off-by Mark Johnston <[email protected]>
Almost. Maybe something like this to make it a bit more clear?
- Attribute names cannot begin with "user." or "system." on ZFS. However FreeBSD's extattr(2) system supports two dedicated namespaces for these two. So "user." or "system." prefixes are trimmed off from attribute names and instead EXTATTR_NAMESPACE_USER or EXTATTR_NAMESPACE_SYSTEM are picked and passed to extattr system calls instead accordingly.
/Christian
Quoted 18 lines · Christian Schoenebeck
Quoted 2 lines · CEST Mark Johnston
Quoted 1 line · Christian Schoenebeck
On Wednesday, August 6, 2025 7:53:08 PM CEST Mark Johnston wrote:
[...]
Quoted 7 lines
Quoted 6 lines
Not forgotten. I just hoped there were other reviewers or testers in the meantime, but be it.
Like I said, I don't have FreeBSD system here to test this, so I am taking your word for now that you tested this and plan to bring this into QEMU when master re-opens for new features soon.
Quoted 6 lines
Thank you very much!
In case I missed somewhat, what testing would you typically do otherwise? So far I had run the QEMU test suite (which indeed found some bugs in the initial version) and tried mounting a 9pfs share from Linux and FreeBSD guests and doing a bit of manual testing.
Quoted 6 lines
Apart from QEMU's test cases, I also use guest systems running 9p as root file system [1], run software compilations there among some things. That proofed to be quite a useful test environment to spot edge cases, concurrency and performance issues and such.
Thanks, I'll give this a try.
Quoted 36 lines
Greg was running some general purpose file system stress test suite in the past, but I currently can't recall what that was.
Quoted 28 lines
Quoted 6 lines
If you have some time to adjust the commit log message above, that would be
great, otherwise I can also handle this on my end later on. Looks like that
comment is not adjusted for v2 yet (i.e. "user." and not mentioning "system.").Here's an amended commit log message. Please let me know if this is better submitted as a v3.
commit
b79bf1b7d42025e3e14da86a7c08d269038cd3edAuthor: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +00009pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of
warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS, so these prefixes are trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user and system namespaces, so this seems innocent enough.Quoted 4 lines
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Signed-off-by Mark Johnston <[email protected]>
Quoted 7 lines
Almost. Maybe something like this to make it a bit more clear?
- Attribute names cannot begin with "user." or "system." on ZFS. However FreeBSD's extattr(2) system supports two dedicated namespaces for these two. So "user." or "system." prefixes are trimmed off from attribute names and instead
EXTATTR_NAMESPACE_USERorEXTATTR_NAMESPACE_SYSTEMare picked and passed to extattr system calls instead accordingly.
I folded your suggestion in with a couple of minor tweaks:
commit 61de78986912b03f08354a177caf603857b531b5
Author: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +0000
9pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for *at() system calls so doesn't require workarounds for a missing mknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin to XATTR_CREATE or XATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS. However FreeBSD's extattr(2) system calls support two dedicated namespaces for these two. So "user." or "system." prefixes are trimmed off from attribute names and instead EXTATTR_NAMESPACE_USER or EXTATTR_NAMESPACE_SYSTEM are picked and passed to extattr system calls accordingly.
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Quoted 1 line · CEST Mark Johnston
On Tue, Aug 26, 2025 at 02:34:45PM +0200, Christian Schoenebeck wrote:
[...]
Quoted 21 lines
I folded your suggestion in with a couple of minor tweaks:
commit
61de78986912b03f08354a177caf603857b531b5Author: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +00009pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of
warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS. However FreeBSD's extattr(2) system calls support two dedicated namespaces for these two. So "user." or "system." prefixes are trimmed off from attribute names and insteadEXTATTR_NAMESPACE_USERorEXTATTR_NAMESPACE_SYSTEMare picked and passed to extattr system calls accordingly.
Quoted 4 lines
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Signed-off-by Mark Johnston <[email protected]>
LGTM, thanks!
/Christian
Quoted 23 lines · On Tue, 26 Aug 2025 14:34:45 +0200 Christian Schoenebeck…
Quoted 2 lines · CEST Mark Johnston
Quoted 1 line · Christian Schoenebeck
On Wednesday, August 6, 2025 7:53:08 PM CEST Mark Johnston wrote:
[...]
Quoted 7 lines
Quoted 6 lines
Not forgotten. I just hoped there were other reviewers or testers in the meantime, but be it.
Like I said, I don't have FreeBSD system here to test this, so I am taking your word for now that you tested this and plan to bring this into QEMU when master re-opens for new features soon.
Quoted 6 lines
Thank you very much!
In case I missed somewhat, what testing would you typically do otherwise? So far I had run the QEMU test suite (which indeed found some bugs in the initial version) and tried mounting a 9pfs share from Linux and FreeBSD guests and doing a bit of manual testing.
Apart from QEMU's test cases, I also use guest systems running 9p as root file system [1], run software compilations there among some things. That proofed to be quite a useful test environment to spot edge cases, concurrency and performance issues and such.
Quoted 5 lines
[1] https://wiki.qemu.org/Documentation/9p_root_fs
Greg was running some general purpose file system stress test suite in the past, but I currently can't recall what that was.
Hi Christian and Mark,
I was running this in the guest :
https://github.com/pjd/pjdfstest
Cheers,
--
Greg
Quoted 44 lines
Quoted 28 lines
Quoted 6 lines
If you have some time to adjust the commit log message above, that would be
great, otherwise I can also handle this on my end later on. Looks like that
comment is not adjusted for v2 yet (i.e. "user." and not mentioning "system.").Here's an amended commit log message. Please let me know if this is better submitted as a v3.
commit
b79bf1b7d42025e3e14da86a7c08d269038cd3edAuthor: Mark Johnston <[email protected]> Date: Wed Jul 16 20:32:05 2025 +00009pfs: Add FreeBSD support
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of
warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." or "system." on ZFS, so these prefixes are trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user and system namespaces, so this seems innocent enough.Quoted 4 lines
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Signed-off-by Mark Johnston <[email protected]>Almost. Maybe something like this to make it a bit more clear?
- Attribute names cannot begin with "user." or "system." on ZFS. However FreeBSD's extattr(2) system supports two dedicated namespaces for these two. So "user." or "system." prefixes are trimmed off from attribute names and instead
EXTATTR_NAMESPACE_USERorEXTATTR_NAMESPACE_SYSTEMare picked and passed to extattr system calls instead accordingly./Christian
Signature · 1 line
Greg
Quoted 11 lines · CEST Mark Johnston
This is largely derived from existing Darwin support. FreeBSD apparently has better support for
*at()system calls so doesn't require workarounds for a missingmknodat(). The implementation has a couple of warts however:
- The extattr(2) system calls don't support anything akin toXATTR_CREATEorXATTR_REPLACE, so a racy workaround is implemented.
- Attribute names cannot begin with "user." on ZFS, so the prefix is trimmed off. FreeBSD's extattr system calls sport an extra "namespace" identifier, and attributes created by the 9pfs backend live in the universal user namespace, so this seems innocent enough.
Quoted 5 lines
The 9pfs tests were verified to pass on the UFS, ZFS and tmpfs filesystems.
Signed-off-by Mark Johnston <[email protected]>---
Queued on 9p.next:
https://github.com/cschoenebeck/qemu/commits/9p.next
Thanks!
/Christian