fs: Add 9P filesystem support over virtio
Subsystems (4)
cover fs: Add 9P filesystem support over virtio
Add 9P network filesystem support to U-Boot.
It allows U-Boot to directly mount host directories in virtualization environments via virtio.
The functionality has been verified on qemu arm64 by successfully loading a Linux kernel image and an initramfs image via 9P, and booting to the Linux shell.
To test with QEMU:
-fsdev local,id=fsdev0,path=/path/to/host/dir,security_model=none \
-device virtio-9p-device,fsdev=fsdev0,mount_tag=rootfs
U-Boot usage:
=> virtio scan
=> ls 9p - /
=> load 9p - $kernel_addr_r /Image
=> load 9p - $ramdisk_addr_r /initramfs.cpio
=> booti $kernel_addr_r $ramdisk_addr_r:$filesize $fdtcontroladdr
Kuan-Wei Chiu (4):
net: 9p: Add 9P2000 protocol support
fs: 9p: Add 9P filesystem support
virtio: 9p: Add 9P transport driver
MAINTAINERS: Add entry for 9PFS
MAINTAINERS | 8 + drivers/virtio/Kconfig | 7 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_9p.c | 75 +++++++++ fs/9p/9p.c | 137 +++++++++++++++++ fs/9p/Kconfig | 8 + fs/9p/Makefile | 5 + fs/Kconfig | 2 + fs/Makefile | 1 + fs/fs.c | 35 +++++ include/9p.h | 82 ++++++++++ include/fs.h | 1 + include/virtio.h | 4 +- net/9p/Kconfig | 5 + net/9p/Makefile | 5 + net/9p/client.c | 272 +++++++++++++++++++++++++++++++++ net/Kconfig | 2 + net/Makefile | 1 + 19 files changed, 651 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_9p.c create mode 100644 fs/9p/9p.c create mode 100644 fs/9p/Kconfig create mode 100644 fs/9p/Makefile create mode 100644 include/9p.h create mode 100644 net/9p/Kconfig create mode 100644 net/9p/Makefile create mode 100644 net/9p/client.c
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
PATCH 1/4 net: 9p: Add 9P2000 protocol support
Introduce the core 9P network protocol client implementation.
9P is a network filesystem protocol originally developed for the Plan 9 operating system. Add the baseline 9P2000.L protocol handling, including message serialization, client registration, and basic transport operations.
--- include/9p.h | 82 +++++++++++++++ net/9p/Kconfig | 5 + net/9p/Makefile | 5 + net/9p/client.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++ net/Kconfig | 2 + net/Makefile | 1 + 6 files changed, 367 insertions(+) create mode 100644 include/9p.h create mode 100644 net/9p/Kconfig create mode 100644 net/9p/Makefile create mode 100644 net/9p/client.c
include/9p.h new file +82 -0
@@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2026, Kuan-Wei Chiu <[email protected]> +*/ + +#ifndef __9P_H__ +#define __9P_H__ + +#include <linux/types.h> + +#define P9_MSIZE 65536 +#define P9_NOFID (~0U) +#define P9_NOTAG 0xFFFF +#define P9_REQ_TAG 1 + +#define P9_RLERROR 7 +#define P9_TLOPEN 12 +#define P9_RLOPEN 13 +#define P9_TGETATTR 24 +#define P9_RGETATTR 25 +#define P9_TREADDIR 40 +#define P9_RREADDIR 41 +#define P9_TVERSION 100 +#define P9_RVERSION 101 +#define P9_TATTACH 104 +#define P9_RATTACH 105 +#define P9_TWALK 110 +#define P9_RWALK 111 +#define P9_TREAD 116 +#define P9_RREAD 117 +#define P9_TCLUNK 120 +#define P9_RCLUNK 121 + +#define P9_O_RDONLY 00000000 +#define P9_GETATTR_SIZE 0x00000010ULL +#define P9_GETATTR_SIZE_OFFSET 41 +#define P9_DIRENT_OFFSET 21 +#define P9_DT_DIR 4 + +struct p9_header { +u32 size; +u8 id; +u16 tag; +} __packed; + +struct p9_client ; + +struct p9_trans_ops { +int (*request)(struct p9_client *c,void *tx,int tx_len,void *rx,int rx_len); +}; + +struct p9_client { +const struct p9_trans_ops *ops; +void *priv; +u8 *tx; +u8 *rx; +u32 next_fid; +}; + +void p9_register_client (struct p9_client *c); +struct p9_client *p9_get_client (void ); + +u32 p9_client_alloc_fid (struct p9_client *c); +int p9_client_version (struct p9_client *c); +int p9_client_attach (struct p9_client *c,u32 fid,const char *uname,const char *aname); +int p9_client_walk (struct p9_client *c,u32 base_fid,u32 new_fid,const char *path); +int p9_client_lopen (struct p9_client *c,u32 fid,u32 flags); +int p9_client_clunk (struct p9_client *c,u32 fid); +int p9_client_stat (struct p9_client *c,u32 fid,loff_t *size); +int p9_client_read (struct p9_client *c,u32 fid,u64 offset,u32 count,void *buf,u32 *actread); +int p9_client_readdir (struct p9_client *c,u32 fid,u64 offset,u32 count,void *buf,u32 *actread); + +struct blk_desc ; +struct disk_partition ; + +int p9_fs_probe (struct blk_desc *fs_dev_desc,struct disk_partition *fs_partition); +int p9_fs_ls (const char *dirname); +int p9_fs_exists (const char *filename); +int p9_fs_size (const char *filename,loff_t *size); +int p9_fs_read (const char *filename,void *buf,loff_t offset,loff_t len,loff_t *actread); + +#endif /* __9P_H__ */
net/9p/Kconfig new file +5 -0
@@ -0,0 +1,5 @@ +config NET_9P + bool "9P protocol support" + help + This enables support for the 9P network protocol (9P2000). + It implements the core 9P client and protocol handling.
net/9p/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright (C) 2026, Kuan-Wei Chiu <[email protected]> + +obj-y += client.o
net/9p/client.c new file +272 -0
272 lines not on this page · raw message
net/Kconfig +2 -0
@@ -236,6 +236,8 @@ endif # if NET_LEGACY source "net/lwip/Kconfig" +source "net/9p/Kconfig" + config BOOTDEV_ETH bool "Enable bootdev for ethernet" depends on BOOTSTD
net/Makefile +1 -0
@@ -49,3 +49,4 @@ obj-y += net-common.o endif obj-$(CONFIG_NET_LWIP) += lwip/ +obj-$(CONFIG_NET_9P) += 9p/
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
Re: [PATCH 1/4] net: 9p: Add 9P2000 protocol support
Hi Kuan-Wei,
Quoted 12 lines · Kuan-Wei Chiu
net: 9p: Add 9P2000 protocol support
Introduce the core 9P network protocol client implementation.
9P is a network filesystem protocol originally developed for the Plan 9 operating system. Add the baseline 9P2000.L protocol handling, including message serialization, client registration, and basic transport operations.
Signed-off-by Kuan-Wei Chiu <[email protected]>
include/9p.h| 82 +++++++++++++++++
Quoted 8 lines
net/9p/Kconfig | 5 ++ net/9p/Makefile | 5 ++ net/9p/client.c | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net/Kconfig | 2 + net/Makefile | 1 + 6 files changed, 367 insertions(+)net: 9p: Add 9P2000 protocol support
The subject says 9P2000 but the client only negotiates and implements 9P2000.L, which is a different dialect. Please can you say 9P2000.L consistently throughout the series?
include/9p.h line 7
Quoted 9 lines
include/9p.h +7
-0@@ -0,0 +1,82 @@ +struct p9_client { +const struct p9_trans_ops *ops; +void *priv; +u8 *tx; +u8 *rx; +u32 next_fid; +};
Please can you add comments to the structs and exported functions in this header, in the normal U-Boot style (see include/fs.h)? Each function should document its parameters and return value, and each struct member should have a comment.
include/9p.h line 4
Quoted 6 lines
include/9p.h +4
-0@@ -0,0 +1,82 @@ +struct blk_desc ; +struct disk_partition ; + +int p9_fs_probe (struct blk_desc *fs_dev_desc,struct disk_partition *fs_partition);
These p9_fs_*() functions are only implemented in fs/9p in the next patch, so please move the declarations there, so each patch is self-contained.
net/9p/client.c line 3
Quoted 5 lines
net/9p/client.c +3
-0@@ -0,0 +1,272 @@ +#include <9p.h> +#include <config.h> +#include <log.h>
Please define LOG_CATEGORY at the top of the file (LOGC_NET seems right) so the log calls can be filtered. The <config.h> include is not needed, since the build system includes it automatically.
net/9p/client.c line 4
Quoted 6 lines
net/9p/client.c +4
-0@@ -0,0 +1,272 @@ +if (rx_hdr->id ==P9_RLERROR ) { + ecode =get_unaligned_le32 (c->rx +7 ); +log_err ("9P Error: %s failed (ecode=%u)\n" , step, ecode); +return -EIO ;
In 9P2000.L the Rlerror ecode is a Linux errno value, so please return -ecode here rather than -EIO, so callers can distinguish ENOENT from real I/O errors (as fs/9p does in the next patch). Also please use sizeof(struct p9_header) instead of the magic 7
net/9p/client.c line 4
Quoted 6 lines
net/9p/client.c +4
-0@@ -0,0 +1,272 @@ +if (!c->tx) + c->tx =memalign (ARCH_DMA_MINALIGN ,P9_MSIZE ); +if (!c->rx) + c->rx =memalign (ARCH_DMA_MINALIGN ,P9_MSIZE );
These 64KB allocations can fail on boards with a small malloc() pool, so please check for NULL and return -ENOMEM
net/9p/client.c line 4
Quoted 6 lines
net/9p/client.c +4
-0@@ -0,0 +1,272 @@ +if (c->ops->request (c, c->tx, hdr->size, c->rx,P9_MSIZE ) <0 ) +return -EIO ; + +return p9_check_error (c,P9_RVERSION ,'Tversion' );
The Rversion reply carries the server's negotiated msize and version string, but both are ignored here. A server may reply with a smaller msize than requested, in which case the P9_MSIZE - 24 reads in p9_client_read() can exceed what it accepts, or with the version string 'unknown' if it does not support 9P2000.L. Please parse the reply, store the negotiated msize in struct p9_client for the read paths to use, and fail if the version string does not match.
net/9p/client.c line 2
Quoted 4 lines
net/9p/client.c +2
-0@@ -0,0 +1,272 @@ +char path_buf[128 ]; +char *wnames[16 ];
strlcpy() silently truncates paths longer than 127 characters, so the walk would quietly go to the wrong file. Please check its return value against sizeof(path_buf) and return -ENAMETOOLONG on truncation.
net/9p/client.c line 1
Quoted 3 lines
net/9p/client.c +1
-0@@ -0,0 +1,272 @@ +return p9_check_error (c,P9_RWALK ,'Twalk' );
Not quite complete: per the 9P spec, only a failure on the first path element produces Rlerror. If a later element fails, the server returns Rwalk with nwqid < nwname and new_fid is not created, yet this returns
0. Please check that nwqid equals nwname and return -ENOENT otherwise, so callers such as p9_fs_exists() get the right answer.
net/9p/client.c line 5
Quoted 7 lines
net/9p/client.c +5
-0@@ -0,0 +1,272 @@ +if (p9_check_error (c,P9_RREAD ,'Tread' ) ==0 ) { + ptr = c->rx +sizeof (*hdr); + *actread =get_unaligned_le32 (ptr); + ptr +=4 ; +memcpy (buf, ptr, *actread);
The count returned by the server is trusted here - a buggy or malicious server could return a value larger than read_len (or the rx buffer) and overflow the caller's buffer. Please check it against read_len before the memcpy(). The same applies in p9_client_readdir()
Regards,
Simon
PATCH 2/4 fs: 9p: Add 9P filesystem support
In reply to Kuan-Wei ChiuImplement the VFS interface for the 9P filesystem.
Map the U-Boot filesystem operations to the underlying 9P protocol client. It also registers the 9P filesystem type within the U-Boot generic fs framework, allowing it to bypass standard block device partition checks since 9P is a network/host-backed filesystem.
--- fs/9p/9p.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/9p/Kconfig | 8 +++ fs/9p/Makefile | 5 ++ fs/Kconfig | 2 + fs/Makefile | 1 + fs/fs.c | 35 +++++++++++++ include/fs.h | 1 + 7 files changed, 189 insertions(+) create mode 100644 fs/9p/9p.c create mode 100644 fs/9p/Kconfig create mode 100644 fs/9p/Makefile
fs/9p/9p.c new file +137 -0
@@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2026, Kuan-Wei Chiu <[email protected]> +*/ + +#include <9p.h> +#include <config.h> +#include <log.h> +#include <asm/unaligned.h> +#include <linux/errno.h> +#include <linux/string.h> +#include <linux/types.h> + +static u32 p9_root_fid =1 ; + +int p9_fs_probe (struct blk_desc *fs_dev_desc,struct disk_partition *fs_partition) +{ +struct p9_client *c =p9_get_client (); + +if (!c) { +log_err ("9P Error: No transport client registered\n" ); +return -ENODEV ; + } + +if (p9_client_version (c)) +return -EIO ; +if (p9_client_attach (c, p9_root_fid,"root" ,"" )) +return -EIO ; + +return 0 ; +} + +int p9_fs_ls (const char *dirname) +{ +struct p9_client *c =p9_get_client (); +u32 fid =p9_client_alloc_fid (c); +u8 buf[4096 ]; +u32 actread; +u8 *ptr, *end; +u16 name_len; +u8 d_type; + +if (p9_client_walk (c, p9_root_fid, fid, dirname)) +return -ENOENT ; +if (p9_client_lopen (c, fid,P9_O_RDONLY )) +return -EIO ; + +if (p9_client_readdir (c, fid,0 ,sizeof (buf), buf, &actread)) { +p9_client_clunk (c, fid); +return -EIO ; + } + +if (actread ==0 ) { +printf (" (empty directory)\n" ); + }else { + ptr = buf; + end = buf + actread; +while (ptr < end) { + ptr +=P9_DIRENT_OFFSET ; + d_type = *ptr++; + name_len =get_unaligned_le16 (ptr); + ptr +=2 ; +printf (" %s %.*s\n" , (d_type ==P9_DT_DIR ) ?"<DIR> " :" " , + name_len, ptr); + ptr += name_len; + } + } + +p9_client_clunk (c, fid); +return 0 ; +} + +int p9_fs_size (const char *filename,loff_t *size) +{ +struct p9_client *c =p9_get_client (); +u32 fid =p9_client_alloc_fid (c); +int ret; + +if (p9_client_walk (c, p9_root_fid, fid, filename)) +return -ENOENT ; + ret =p9_client_stat (c, fid, size); +p9_client_clunk (c, fid); + +return ret; +} + +int p9_fs_exists (const char *filename) +{ +loff_t size; + +return (p9_fs_size (filename, &size) ==0 ) ?1 :0 ; +} + +int p9_fs_read (const char *filename,void *buf,loff_t offset,loff_t len,loff_t *actread) +{ +struct p9_client *c =p9_get_client (); +u32 fid =p9_client_alloc_fid (c); +u32 read_bytes =0 ; +loff_t total_read =0 ; +loff_t remaining; +u32 req_len; +u8 *dst = (u8 *)buf; +int ret =0 ; + +if (p9_client_walk (c, p9_root_fid, fid, filename)) +return -ENOENT ; +if (p9_client_lopen (c, fid,P9_O_RDONLY )) { +p9_client_clunk (c, fid); +return -EIO ; + } + +while (1 ) { +if (len ==0 ) { + req_len =0 ; + }else { + remaining = len - total_read; +if (remaining ==0 ) +break ; + + req_len = (remaining >0xFFFFFFFF ) ?0xFFFFFFFF : (u32 )remaining; + } + + ret =p9_client_read (c, fid, offset, req_len, dst, &read_bytes); + +if (ret !=0 || read_bytes ==0 ) +break ; + + total_read += read_bytes; + offset += read_bytes; + dst += read_bytes; + } + + *actread = total_read; +p9_client_clunk (c, fid); + +return (total_read >0 || ret ==0 ) ?0 : ret; +}
fs/9p/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +config FS_9P + bool "9P filesystem support" + depends on NET_9P + help + This provides support for the 9P2000.L filesystem. + + To use this filesystem, you also need to enable a 9P + transport driver.
fs/9p/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright (C) 2026, Kuan-Wei Chiu <[email protected]> + +obj-y += 9p.o
fs/Kconfig +2 -0
@@ -26,4 +26,6 @@ source "fs/squashfs/Kconfig" source "fs/erofs/Kconfig" +source "fs/9p/Kconfig" + endmenu
fs/Makefile +1 -0
@@ -26,5 +26,6 @@ obj-$(CONFIG_CMD_UBIFS) += ubifs/ obj-$(CONFIG_CMD_ZFS) += zfs/ obj-$(CONFIG_FS_SQUASHFS) += squashfs/ obj-$(CONFIG_FS_EROFS) += erofs/ +obj-$(CONFIG_FS_9P) += 9p/ endif obj-y += fs_internal.o
fs/fs.c +35 -0
@@ -32,6 +32,7 @@#include <squashfs.h> #include <erofs.h> #include <exfat.h> +#include <9p.h> static struct blk_desc *fs_dev_desc;static int fs_dev_part;
@@ -398,6 +399,26 @@ static struct fstype_info fstypes[] = { .mkdir = exfat_fs_mkdir, .rename = exfat_fs_rename, }, +#endif +#if CONFIG_IS_ENABLED (FS_9P ) + { + .fstype = FS_TYPE_9P, + .name = "9p", + .null_dev_desc_ok = true, + .probe = p9_fs_probe, + .close = fs_close_unsupported, + .ls = p9_fs_ls, + .exists = p9_fs_exists, + .size = p9_fs_size, + .read = p9_fs_read, + .write = fs_write_unsupported, + .uuid = fs_uuid_unsupported, + .opendir = fs_opendir_unsupported, + .unlink = fs_unlink_unsupported, + .mkdir = fs_mkdir_unsupported, + .ln = fs_ln_unsupported, + .rename = fs_rename_unsupported, + }, #endif { .fstype = FS_TYPE_ANY,
@@ -464,6 +485,20 @@ int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)struct fstype_info *info;int part, i; +/* Intercept 9p to bypass block device partition checks */ +if (CONFIG_IS_ENABLED (FS_9P ) && !strncmp (ifname,"9p" ,2 )) { +for (i =0 , info = fstypes; i <ARRAY_SIZE (fstypes); i++, info++) { +if (info->fstype ==FS_TYPE_9P ) { + fs_dev_desc =NULL ; + fs_type =FS_TYPE_9P ; +if (info->probe (fs_dev_desc,NULL )) +return -1 ; +return 0 ; + } + } +return -1 ; + } + part =part_get_info_by_dev_and_name_or_num (ifname, dev_part_str, &fs_dev_desc, &fs_partition,1 );if (part <0 )
include/fs.h +1 -0
@@ -19,6 +19,7 @@ struct cmd_tbl;#define FS_TYPE_EROFS 7#define FS_TYPE_SEMIHOSTING 8#define FS_TYPE_EXFAT 9 +#define FS_TYPE_9P 10struct blk_desc ;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
Re: [PATCH 2/4] fs: 9p: Add 9P filesystem support
Hi Kuan-Wei,
Quoted 25 lines · Kuan-Wei Chiu
fs: 9p: Add 9P filesystem support
Implement the VFS interface for the 9P filesystem.
Map the U-Boot filesystem operations to the underlying 9P protocol client. It also registers the 9P filesystem type within the U-Boot generic fs framework, allowing it to bypass standard block device partition checks since 9P is a network/host-backed filesystem.
Signed-off-by Kuan-Wei Chiu <[email protected]>
fs/9p/9p.c| 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++fs/9p/Kconfig | 8 ++++ fs/9p/Makefile | 5 +++ fs/Kconfig | 2 + fs/Makefile | 1 + fs/fs.c | 35 +++++++++++++++ include/fs.h | 1 + 7 files changed, 189 insertions(+)fs/fs.c +3
-0@@ -466,6 +487,20 @@ int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) +/* Intercept 9p to bypass block device partition checks */ +if (CONFIG_IS_ENABLED (FS_9P ) && !strncmp (ifname,'9p' ,2 )) { +for (i =0 , info = fstypes; i <ARRAY_SIZE (fstypes); i++, info++) {
fs/fs.c int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) · line 499
Quoted 10 lines
fs/fs.c +10
-0+if (info->fstype ==FS_TYPE_9P ) { + fs_dev_desc =NULL ; + fs_type =FS_TYPE_9P ; +if (info->probe (fs_dev_desc,NULL )) +return -1 ; +return 0 ; + } + } +return -1 ; + }
There is already a mechanism for pseudo block devices: the hostfs and ubi special cases in blk_get_device_part_str() in disk/part.c return success with a NULL desc, and the normal loop below then probes any filesystem with null_dev_desc_ok set, which you have already done for the 9p entry. Please can you add a 9p case there instead of duplicating the probe loop here? That also fixes two problems with this version: strncmp() matches any ifname starting with 9p (strcmp() is what you want), and fs_type is set before probe() succeeds, so a failed probe leaves stale state behind (the generic loop only sets it on success).
fs/9p/9p.c line 4
Quoted 6 lines
fs/9p/9p.c +4
-0@@ -0,0 +1,137 @@ +if (p9_client_walk (c, p9_root_fid, fid, dirname)) +return -ENOENT ; +if (p9_client_lopen (c, fid,P9_O_RDONLY )) +return -EIO ;
If the walk succeeds but the open fails, the fid is left allocated on the server. Please clunk it before returning, as in the readdir-failure path just below and in p9_fs_read()
fs/9p/9p.c line 4
Quoted 6 lines
fs/9p/9p.c +4
-0@@ -0,0 +1,137 @@ +if (p9_client_readdir (c, fid,0 ,sizeof (buf), buf, &actread)) { +p9_client_clunk (c, fid); +return -EIO ; + }
This issues a single Treaddir at offset 0, so any directory whose entries do not fit in 4KB is silently truncated. Each dirent carries an offset field (the 8 bytes skipped within P9_DIRENT_OFFSET), so please loop, passing the last entry's offset back in, until the server returns zero bytes. BTW a 4KB stack buffer is quite large for some boards - since it would be reused across iterations anyway, consider malloc() / free() here.
fs/9p/9p.c line 4
Quoted 6 lines
fs/9p/9p.c +4
-0@@ -0,0 +1,137 @@ + *actread = total_read; +p9_client_clunk (c, fid); + +return (total_read >0 || ret ==0 ) ?0 : ret;
If a read fails part-way through, this returns 0 with a short actread, so the caller (e.g. the load command) reports success on a partially loaded file, which could go unnoticed until the image fails to boot. Other filesystems propagate the error here, so please return ret whenever it is non-zero.
Regards,
Simon
PATCH 3/4 virtio: 9p: Add 9P transport driver
In reply to Kuan-Wei ChiuAdd virtio transport driver for the 9P filesystem.
This driver binds to the virtio-9p device exposed by the hypervisor and registers the transport operations with the 9P client framework. It implements the transmit and receive routines.
--- drivers/virtio/Kconfig | 7 ++++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_9p.c | 75 ++++++++++++++++++++++++++++++++++ include/virtio.h | 4 +- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_9p.c
drivers/virtio/Kconfig +7 -0
@@ -77,4 +77,11 @@ config VIRTIO_RNG help This is the virtual random number generator driver. It can be used with QEMU based targets. + +config VIRTIO_9P + bool "virtio 9P transport driver" + depends on VIRTIO && NET_9P + help + This enables the 9P transport driver over virtio. + It can be used with QEMU based targets. endmenu
drivers/virtio/Makefile +1 -0
@@ -11,3 +11,4 @@ obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o +obj-$(CONFIG_VIRTIO_9P) += virtio_9p.o
drivers/virtio/virtio-uclass.c +1 -0
@@ -30,6 +30,7 @@ static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME, + [VIRTIO_ID_9P] = VIRTIO_9P_DRV_NAME, }; int virtio_get_config(struct udevice *vdev, unsigned int offset,
drivers/virtio/virtio_9p.c new file +75 -0
75 lines not on this page · raw message
include/virtio.h +3 -1
@@ -31,11 +31,13 @@#define VIRTIO_ID_NET 1/* virtio net */ #define VIRTIO_ID_BLOCK 2/* virtio block */ #define VIRTIO_ID_RNG 4/* virtio rng */ -#define VIRTIO_ID_MAX_NUM 5 +#define VIRTIO_ID_9P 9/* virtio 9p */ +#define VIRTIO_ID_MAX_NUM 10#define VIRTIO_NET_DRV_NAME "virtio-net"#define VIRTIO_BLK_DRV_NAME "virtio-blk"#define VIRTIO_RNG_DRV_NAME "virtio-rng" +#define VIRTIO_9P_DRV_NAME "virtio-9p"/* Status byte for guest to report progress, and synchronize features */
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
Re: [PATCH 3/4] virtio: 9p: Add 9P transport driver
Hi Kuan-Wei,
Quoted 19 lines · Kuan-Wei Chiu
virtio: 9p: Add 9P transport driver
Add virtio transport driver for the 9P filesystem.
This driver binds to the virtio-9p device exposed by the hypervisor and registers the transport operations with the 9P client framework. It implements the transmit and receive routines.
Signed-off-by Kuan-Wei Chiu <[email protected]>
drivers/virtio/Kconfig| 7 ++++drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_9p.c | 75 ++++++++++++++++++++++++++++++++++++++++++ include/virtio.h | 4 ++- 5 files changed, 87 insertions(+), 1 deletion(-)
drivers/virtio/virtio_9p.c line 2
Quoted 2 lines
drivers/virtio/virtio_9p.c +2
-0@@ -0,0 +1,75 @@ +flush_dcache_range ((ulong )tx,ALIGN ((ulong )tx + tx_len,ARCH_DMA_MINALIGN )); +flush_dcache_range ((ulong )rx,ALIGN ((ulong )rx + rx_len,ARCH_DMA_MINALIGN ));
Just to check, why are these needed? No other virtio driver (nor virtio_ring.c itself) does any cache maintenance, so on a non-coherent system the vring descriptors would still be stale and flushing the data buffers alone would not help. If virtio on QEMU is effectively coherent, these are dead code and should be dropped; if you have hit a real cache problem, it needs fixing in virtio_ring.c for all drivers rather than here. What do you think?
drivers/virtio/virtio_9p.c line 7
Quoted 9 lines
drivers/virtio/virtio_9p.c +7
-0@@ -0,0 +1,75 @@ +if (virtqueue_add (priv->vq, sgs,1 ,1 )) +return -EIO ; + +virtqueue_kick (priv->vq); + +while (!virtqueue_get_buf (priv->vq, &len)) + ;
Please can you propagate the error code from virtqueue_add() rather than replacing it with -EIO
Also, this loop spins forever if the device never responds, hanging U-Boot with no way out. I realise virtio_rng does the same, but a filesystem request is a good deal larger, so please add a timeout (e.g. using get_timer()) and return -ETIMEDOUT
BTW 'len' is the number of bytes the device actually wrote, but it is discarded here. It would be worth passing it back to the client so it can validate the size field in the reply header against what was really received.
drivers/virtio/virtio_9p.c line 3
Quoted 5 lines
drivers/virtio/virtio_9p.c +3
-0@@ -0,0 +1,75 @@ + priv->client.ops = &virtio_9p_ops; + priv->client.priv = priv; +p9_register_client (&priv->client);
Since you are adding a new virtio driver you may need a bind() method which calls virtio_driver_features_init(), as virtio_net, virtio_blk and virtio_rng all do. That would also let you negotiate VIRTIO_9P_MOUNT_TAG and read the mount tag from the config space, which is needed for the device selection mentioned in my reply to the cover letter. It would be good to at least log the tag now, so that selection can be added later.
drivers/virtio/virtio_9p.c line 6
Quoted 8 lines
drivers/virtio/virtio_9p.c +6
-0@@ -0,0 +1,75 @@ +U_BOOT_DRIVER (virtio_9p) = { + .name =VIRTIO_9P_DRV_NAME , + .id =UCLASS_MISC , + .probe = virtio_9p_probe, + .priv_auto =sizeof (struct virtio_9p_priv ), +};
This is missing .remove = virtio_reset and
.flags = DM_FLAG_ACTIVE_DMA, which the other virtio drivers have. Without them the device is left with DMA active when U-Boot boots an OS. The remove path also needs to unregister the client: priv is freed on removal, so the pointer held by p9_register_client() in patch 1 would be left dangling and a later 'ls 9p - /' would use freed memory.
Regards,
Simon
PATCH 4/4 MAINTAINERS: Add entry for 9PFS
In reply to Kuan-Wei ChiuAdd maintainer entry for the newly introduced 9P filesystem subsystem, including the protocol core, vfs integration, and virtio transport driver.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -50,6 +50,14 @@ so much easier [Ed] Maintainers List (try to look for most precise areas first) ----------------------------------- +9PFS +M: Kuan-Wei Chiu <[email protected]> +S: Maintained +F: drivers/virtio/virtio_9p.c +F: fs/9p/ +F: include/9p.h +F: net/9p/ + ACPI M: Simon Glass <[email protected]> S: Maintained
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
Re: [PATCH 4/4] MAINTAINERS: Add entry for 9PFS
Quoted 10 lines · Kuan-Wei Chiu
MAINTAINERS: Add entry for 9PFSAdd maintainer entry for the newly introduced 9P filesystem subsystem, including the protocol core, vfs integration, and virtio transport driver.
Signed-off-by Kuan-Wei Chiu <[email protected]>
MAINTAINERS| 8 ++++++++1 file changed, 8 insertions(+)
Quoted 13 lines · Kuan-Wei Chiu
Add 9P network filesystem support to U-Boot.
It allows U-Boot to directly mount host directories in virtualization environments via virtio.
The functionality has been verified on qemu arm64 by successfully loading a Linux kernel image and an initramfs image via 9P, and booting to the Linux shell.
To test with QEMU:
-fsdev local,id=fsdev0,path=/path/to/host/dir,security_model=none \
-device virtio-9p-device,fsdev=fsdev0,mount_tag=rootfs
Quoted 6 lines
U-Boot usage:
=> virtio scan
=> ls 9p - /
=> load 9p - $kernel_addr_r /Image
=> load 9p - $ramdisk_addr_r /initramfs.cpio
=> booti $kernel_addr_r $ramdisk_addr_r:$filesize $fdtcontroladdr
Neat. Can you please update this to include docs and tests (and likely updating u-boot-test-hooks so QEMU is started with the required options? We already have a bit of python so that we can pass a build directory over to QEMU for TFTP tests, iirc). Thanks!
Signature · 1 line
Tom
Hi Kuan-Wei,
Quoted 2 lines · Kuan-Wei Chiu
It allows U-Boot to directly mount host directories in virtualization environments via virtio.
Thanks for working on this - a nice feature which has been missing for a while.
My main concern is the overall design. The transport driver registers a single global client which the filesystem layer picks up, so there can only ever be one 9P device and the QEMU mount tag is ignored. Since the transport is already a driver-model device, please can you attach the client state to the udevice and have the filesystem layer look up the device, rather than using a global? That would let the user select a particular device (or mount tag) in the ls and load commands instead of passing a dash, and would fit better with the rest of U-Boot.
Related to this, intercepting the 9p interface name in the generic filesystem code feels like a hack. It would be better to integrate with the existing device/partition handling, perhaps along the lines of semihosting.
Quoted 3 lines
The functionality has been verified on qemu arm64 by successfully loading a Linux kernel image and an initramfs image via 9P, and booting to the Linux shell.
For the tests Tom asked for, one option would be a sandbox transport driver (see the existing sandbox virtio support) so the protocol and filesystem code can be exercised in CI without QEMU, plus a pytest for the QEMU side. A doc/usage page showing the QEMU flags and U-Boot commands from this cover letter would be a good start for the docs.
Regards,
Simon