Skip to contentSkip to conversation

erofs-utils: mount: add NBD backend support

PATCH New Gao Xiang 1 participant 1 message Aug 27, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: mount: add NBD backend support

Currently, only local images are supported (so using the current NBD backend is mostly for functional testing), e.g.:

However, it already uses the vfile interface, so extending it to remote sources should be straightforward.

NBD failover is not supported yet and should be implemented via the new netlink interface.

---
 include/erofs/defs.h |   9 ++
 include/erofs/io.h   |   6 ++
 lib/Makefile.am      |   4 +
 lib/backends/nbd.c   | 223 +++++++++++++++++++++++++++++++++++++++++++
 lib/io.c             |  69 ++++++++++++-
 lib/liberofs_nbd.h   |  39 ++++++++
 mount/main.c         | 156 +++++++++++++++++++++++++++++-
 7 files changed, 499 insertions(+), 7 deletions(-)
 create mode 100644 lib/backends/nbd.c
 create mode 100644 lib/liberofs_nbd.h
include/erofs/defs.h +9 -0
index 0f3e754..8af99ae 100644
@@ -88,6 +88,10 @@ typedef int64_t         s64;
 #define le32_to_cpu(x) ((__u32)(x))
 #define le64_to_cpu(x) ((__u64)(x))
 
+#define cpu_to_be32(x) ((__be32)__builtin_bswap32(x))
+#define cpu_to_be64(x) ((__be64)__builtin_bswap64(x))
+#define be32_to_cpu(x) (__builtin_bswap32(x))
+#define be64_to_cpu(x) (__builtin_bswap64(x))
 #else
 #if __BYTE_ORDER == __BIG_ENDIAN
 #define cpu_to_le16(x) (__builtin_bswap16(x))
@@ -96,6 +100,11 @@ typedef int64_t         s64;
 #define le16_to_cpu(x) (__builtin_bswap16(x))
 #define le32_to_cpu(x) (__builtin_bswap32(x))
 #define le64_to_cpu(x) (__builtin_bswap64(x))
+
+#define cpu_to_be32(x) ((__be32)(x))
+#define cpu_to_be64(x) ((__be64)(x))
+#define be32_to_cpu(x) ((__u32)(x))
+#define be64_to_cpu(x) ((__u64)(x))
 #else
 #pragma error
 #endif
include/erofs/io.h +6 -0
index cc7a3cd..370765f 100644
@@ -16,6 +16,7 @@ extern "C"
 #define _GNU_SOURCE
 #endif
 #include <unistd.h>
+#include <sys/stat.h>
 #include <sys/uio.h>
 #include "defs.h"
 
@@ -36,6 +37,8 @@ struct erofs_vfops {
 	ssize_t (*read)(struct erofs_vfile *vf, void *buf, size_t len);
 	off_t (*lseek)(struct erofs_vfile *vf, u64 offset, int whence);
 	int (*fstat)(struct erofs_vfile *vf, struct stat *buf);
+	ssize_t (*sendfile)(struct erofs_vfile *vout, struct erofs_vfile *vin,
+			    off_t *pos, size_t count);
 	int (*xcopy)(struct erofs_vfile *vout, off_t pos,
 		     struct erofs_vfile *vin, unsigned int len, bool noseek);
 };
@@ -53,6 +56,7 @@ struct erofs_vfile {
 };
 
 ssize_t __erofs_io_write(int fd, const void *buf, size_t len);
+int __erofs_0write(int fd, size_t len);
 
 int erofs_io_fstat(struct erofs_vfile *vf, struct stat *buf);
 ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, u64 pos, size_t len);
@@ -67,6 +71,8 @@ off_t erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence);
 
 ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out,
 			      size_t length);
+ssize_t erofs_io_sendfile(struct erofs_vfile *vout, struct erofs_vfile *vin,
+			  off_t *pos, size_t count);
 int erofs_io_xcopy(struct erofs_vfile *vout, off_t pos,
 		   struct erofs_vfile *vin, unsigned int len, bool noseek);
 
lib/Makefile.am +4 -0
index 955495d..4f8e767 100644
@@ -31,6 +31,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
       $(top_srcdir)/lib/liberofs_private.h \
       $(top_srcdir)/lib/liberofs_xxhash.h \
       $(top_srcdir)/lib/liberofs_metabox.h \
+      $(top_srcdir)/lib/liberofs_nbd.h \
       $(top_srcdir)/lib/liberofs_s3.h
 
 noinst_HEADERS += compressor.h
@@ -76,3 +77,6 @@ if ENABLE_EROFS_MT
 liberofs_la_LDFLAGS = -lpthread
 liberofs_la_SOURCES += workqueue.c
 endif
+if OS_LINUX
+liberofs_la_SOURCES += backends/nbd.c
+endif
lib/backends/nbd.c new file +223 -0

223 lines not on this page · raw message

lib/io.c +65 -4

95 lines not on this page · raw message

lib/liberofs_nbd.h new file +39 -0
new file mode 100644
index 0000000..6660df1
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+/*
+ * Copyright (C) 2025 Alibaba Cloud
+ */
+#ifndef __EROFS_LIB_LIBEROFS_NBD_H
+#define __EROFS_LIB_LIBEROFS_NBD_H
+
+#include "erofs/defs.h"
+
+/* Supported request types */
+enum {
+	EROFS_NBD_CMD_READ		= 0,
+	EROFS_NBD_CMD_WRITE		= 1,
+	EROFS_NBD_CMD_DISC		= 2,
+	EROFS_NBD_CMD_FLUSH		= 3,
+	EROFS_NBD_CMD_TRIM		= 4,
+	/* userspace defines additional extension commands */
+	EROFS_NBD_CMD_WRITE_ZEROES	= 6,
+};
+
+struct erofs_nbd_request {
+	__be32 magic;			/* NBD_REQUEST_MAGIC */
+	u32 type;			/* See NBD_CMD_* */
+	union {
+		__be64 cookie;		/* Opaque identifier for request */
+		char   handle[8];	/* older spelling of cookie */
+	};
+	u64 from;
+        u32 len;
+} __packed;
+
+long erofs_nbd_in_service(int nbdnum);
+int erofs_nbd_devscan(void);
+int erofs_nbd_connect(int nbdfd, int blkbits, u64 blocks);
+int erofs_nbd_do_it(int nbdfd);
+int erofs_nbd_get_request(int skfd, struct erofs_nbd_request *rq);
+int erofs_nbd_send_reply_header(int skfd, __le64 cookie, int err);
+
+#endif
mount/main.c +153 -3

192 lines not on this page · raw message

Signature · 1 line
2.43.0