erofs-utils: lib: nbd: add support for the netlink interface
PATCH 1/4 erofs-utils: lib: nbd: add support for the netlink interface
Meta (formerly Facebook) developed a new netlink‑based interface [1] since Linux 4.12 to replace the old ioctl‑based interface for crash recovery and daemon hot upgrade.
[1] https://lore.kernel.org/r/[email protected]
--- configure.ac | 29 +++++++ lib/Makefile.am | 1 + lib/backends/nbd.c | 191 +++++++++++++++++++++++++++++++++++++++++++++ lib/liberofs_nbd.h | 2 + mount/Makefile.am | 2 +- mount/main.c | 79 ++++++++++++++++--- 6 files changed, 290 insertions(+), 14 deletions(-)
configure.ac +29 -0
@@ -181,6 +181,10 @@ AC_ARG_WITH(json_c, [AS_HELP_STRING([--with-json-c], [Enable and build with json-c support @<:@default=auto@:>@])]) +AC_ARG_WITH(libnl3, + [AS_HELP_STRING([--with-libnl3], + [Enable and build with libnl3 support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"])
@@ -718,6 +722,31 @@ AS_IF([test "x$with_libxml2" != "xno"], [ ]) ]) +# Configure libnl3 +have_libnl3="no" +AS_IF([test "x$with_libnl3" != "xno"], [ + PKG_CHECK_MODULES([libnl3], [libnl-genl-3.0 >= 3.1], [ + # Paranoia: don't trust the result reported by pkgconfig before trying out + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${libnl3_CFLAGS} ${CPPFLAGS}" + LIBS="${libnl3_LIBS} $LIBS" + AC_CHECK_HEADERS([netlink/genl/genl.h],[ + AC_CHECK_LIB(nl-genl-3, genl_connect, [], [ + AC_MSG_ERROR([libnl3 doesn't work properly])]) + AC_CHECK_DECL(genl_connect, [have_libnl3="yes"], + [AC_MSG_ERROR([libnl3 doesn't work properly])], [[ +#include <netlink/genl/genl.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}"], [ + AS_IF([test "x$with_libnl3" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper libnl3]) + ]) + ]) +]) + AS_IF([test "x$enable_s3" != "xno"], [ AS_IF( [test "x$have_libcurl" = "xyes" && \
lib/Makefile.am +1 -0
@@ -79,6 +79,7 @@ liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif if OS_LINUX +liberofs_la_CFLAGS += ${libnl3_CFLAGS} liberofs_la_SOURCES += backends/nbd.c endif if ENABLE_OCI
lib/backends/nbd.c +191 -0
203 lines not on this page · raw message
lib/liberofs_nbd.h +2 -0
@@ -39,4 +39,6 @@ int erofs_nbd_get_request(int skfd, struct erofs_nbd_request *rq);int erofs_nbd_send_reply_header (int skfd,__le64 cookie,int err);int erofs_nbd_disconnect (int nbdfd); +int erofs_nbd_nl_connect (int *index,int blkbits,u64 blocks, +const char *identifier);#endif
mount/Makefile.am +1 -1
@@ -9,5 +9,5 @@ mount_erofs_SOURCES = main.c mount_erofs_CFLAGS = -Wall -I$(top_srcdir)/include mount_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \ ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} ${libdeflate_LIBS} \ - ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} + ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} ${libnl3_LIBS} endif
mount/main.c +66 -13
91 lines not on this page · raw message
Signature · 1 line
2.43.5
PATCH 2/4 erofs-utils: lib: nbd: add support for the netlink reconnection
In order to support NBD failover.
--- lib/backends/nbd.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ lib/liberofs_nbd.h | 4 ++ 2 files changed, 97 insertions(+)
lib/backends/nbd.c +93 -0
@@ -174,6 +174,26 @@ err_out:return err; } +char *erofs_nbd_get_identifier (int nbdnum) +{ +char s[32 ], *line =NULL ; +size_t n; +FILE *f; +int err; + + (void )snprintf (s,sizeof (s),"/sys/block/nbd%d/backend" , nbdnum); + f =fopen (s,"r" ); +if (!f) +return ERR_PTR (-errno); + +if (getline (&line, &n, f) <0 ) + err = -errno; +else + err =0 ; +fclose (f); +return err ?ERR_PTR (err) : line; +} +#if defined(HAVE_NETLINK_GENL_GENL_H ) && defined(HAVE_LIBNL_GENL_3 )enum { NBD_ATTR_UNSPEC,
@@ -209,6 +229,7 @@ enum {NBD_CMD_UNSPEC ,NBD_CMD_CONNECT ,NBD_CMD_DISCONNECT , +NBD_CMD_RECONFIGURE , __NBD_CMD_MAX, };
@@ -312,6 +333,7 @@ int erofs_nbd_nl_connect(int *index, int blkbits, u64 blocks,NLA_PUT_U64 (msg,NBD_ATTR_SIZE_BYTES , blocks << blkbits);NLA_PUT_U64 (msg,NBD_ATTR_SERVER_FLAGS ,NBD_FLAG_READ_ONLY );NLA_PUT_U64 (msg,NBD_ATTR_TIMEOUT ,0 ); +NLA_PUT_U64 (msg,NBD_ATTR_DEAD_CONN_TIMEOUT ,EROFS_NBD_DEAD_CONN_TIMEOUT );if (identifier)NLA_PUT_STRING (msg,NBD_ATTR_BACKEND_IDENTIFIER , identifier);
@@ -339,6 +361,72 @@ int erofs_nbd_nl_connect(int *index, int blkbits, u64 blocks,return cbctx.errcode;return sv[0 ]; +nla_put_failure: +nla_nest_cancel (msg, sock_opt); +nla_nest_cancel (msg, sock_attr); +err_nlm_free: +nlmsg_free (msg); +err_nls_free: +nl_socket_free (socket); +err_out: +close (sv[0 ]); +close (sv[1 ]); +return err; +} + +int erofs_nbd_nl_reconnect (int index,const char *identifier) +{ +struct nlattr *sock_attr, *sock_opt; +struct nl_sock *socket; +struct nl_msg *msg; +int sv[2 ], err; +int driver_id; + + err =socketpair (AF_UNIX ,SOCK_STREAM ,0 , sv); +if (err <0 ) +return -errno; + + socket =erofs_nbd_get_nl_sock (&driver_id); +if (IS_ERR (socket)) { + err =PTR_ERR (socket); + goto err_out; + } + + msg =nlmsg_alloc (); +if (!msg) { +erofs_err ("Couldn't allocate netlink message" ); + err = -ENOMEM ; + goto err_nls_free; + } + +genlmsg_put (msg,NL_AUTO_PORT ,NL_AUTO_SEQ , driver_id,0 ,0 , +NBD_CMD_RECONFIGURE ,0 ); +NLA_PUT_U32 (msg,NBD_ATTR_INDEX , index); +if (identifier) +NLA_PUT_STRING (msg,NBD_ATTR_BACKEND_IDENTIFIER , identifier); + + err = -EINVAL ; + sock_attr =nla_nest_start (msg,NBD_ATTR_SOCKETS ); +if (!sock_attr) { +erofs_err ("Couldn't nest the sockets for our connection" ); + goto err_nlm_free; + } + + sock_opt =nla_nest_start (msg,NBD_SOCK_ITEM ); +if (!sock_attr) { +nla_nest_cancel (msg, sock_attr); + goto err_nlm_free; + } +NLA_PUT_U32 (msg,NBD_SOCK_FD , sv[1 ]); +nla_nest_end (msg, sock_opt); +nla_nest_end (msg, sock_attr); + + err =nl_send_sync (socket, msg); +if (err) + goto err_out; +nl_socket_free (socket); +return sv[0 ]; + nla_put_failure:nla_nest_cancel (msg, sock_opt);nla_nest_cancel (msg, sock_attr);
@@ -357,6 +445,11 @@ int erofs_nbd_nl_connect(int *index, int blkbits, u64 blocks, {return -EOPNOTSUPP ; } + +int erofs_nbd_nl_reconnect (int index,const char *identifier) +{ +return -EOPNOTSUPP ; +}#endif int erofs_nbd_do_it (int nbdfd)
lib/liberofs_nbd.h +4 -0
@@ -31,6 +31,9 @@ struct erofs_nbd_request {u32 len; } __packed; +/* 30-day timeout for NBD recovery */ +#define EROFS_NBD_DEAD_CONN_TIMEOUT (3600 * 24 * 30) +long erofs_nbd_in_service (int nbdnum);int erofs_nbd_devscan (void );int erofs_nbd_connect (int nbdfd,int blkbits,u64 blocks);
@@ -41,4 +44,5 @@ int erofs_nbd_disconnect(int nbdfd);int erofs_nbd_nl_connect (int *index,int blkbits,u64 blocks,const char *identifier); +int erofs_nbd_nl_reconnect (int index,const char *identifier);#endif
Signature · 1 line
2.43.5
PATCH 3/4 erofs-utils: mount: record recovery files for NBD failover
In reply to Gao XiangIn order to re-attach, it's necessary to know the exact image source for local images.
For later remote images, more information is expected to be recorded.
--- mount/main.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 7 deletions(-)
mount/main.c +79 -7
@@ -5,6 +5,7 @@#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <signal.h> #include <sys/mount.h> #include <sys/types.h> #include <pthread.h>
@@ -299,6 +300,61 @@ out_closefd:return err; } +static char *erofsmount_write_recovery_info (const char *source) +{ +char recp[] ="/var/run/erofs/mountnbd_XXXXXX" ; +char *realp; +int fd, err; +FILE *f; + + fd =mkstemp (recp); +if (fd <0 && errno ==ENOENT ) { + err =mkdir ("/var/run/erofs" ,0700 ); +if (err) +return ERR_PTR (-errno); + fd =mkstemp (recp); + } +if (fd <0 ) +return ERR_PTR (-errno); + + f =fdopen (fd,"w+" ); +if (!f) { +close (fd); +return ERR_PTR (-errno); + } + + realp =realpath (source,NULL ); +if (!realp) { +fclose (f); +return ERR_PTR (-errno); + } +/* TYPE<LOCAL> <SOURCE PATH>\n(more..) */ + err =fprintf (f,"LOCAL %s\n" , source) <0 ; +fclose (f); +free (realp); +if (err) +return ERR_PTR (-ENOMEM ); +return strdup (recp) ?:ERR_PTR (-ENOMEM ); +} + +static int erofsmount_nbd_fix_backend_linkage (int num,char **recp) +{ +char *newrecp; +int err; + +if (asprintf (&newrecp,"/var/run/erofs/mountnbd_nbd%d" , num) <=0 ) +return -ENOMEM ; + +if (rename (*recp, newrecp) <0 ) { + err = -errno; +free (newrecp); +return err; + } +free (*recp); + *recp = newrecp; +return 0 ; +} +static int erofsmount_startnbd_nl (pid_t *pid,const char *source) {struct erofsmount_nbd_ctx ctx = {};
@@ -317,26 +373,42 @@ static int erofsmount_startnbd_nl(pid_t *pid, const char *source)return err; }if ((*pid =fork ()) ==0 ) { +char *recp; +/* Otherwise, NBD disconnect sends SIGPIPE, skipping cleanup */ if (signal (SIGPIPE ,SIG_IGN ) ==SIG_ERR ) {close (ctx.vd.fd);exit (EXIT_FAILURE ); } - + recp =erofsmount_write_recovery_info (source); +if (IS_ERR (recp)) { +close (ctx.vd.fd); +exit (EXIT_FAILURE ); + } num =-1 ; - err =erofs_nbd_nl_connect (&num,9 ,INT64_MAX >>9 ,NULL ); + err =erofs_nbd_nl_connect (&num,9 ,INT64_MAX >>9 , recp);if (err >=0 ) { ctx.sk.fd = err; - err =write (pipefd[1 ], &num,sizeof (int )); -if (err >=sizeof (int )) { + err =erofsmount_nbd_fix_backend_linkage (num, &recp); +if (err) { +close (ctx.sk.fd); + }else { + err =write (pipefd[1 ], &num,sizeof (int )); +if (err <0 ) + err = -errno;close (pipefd[1 ]);close (pipefd[0 ]); - err = (int )(uintptr_t )erofsmount_nbd_loopfn (&ctx); -exit (err ?EXIT_FAILURE :EXIT_SUCCESS ); +if (err >=sizeof (int )) { + err = (int )(uintptr_t )erofsmount_nbd_loopfn (&ctx); + goto out_fork; + } } }close (ctx.vd.fd); -exit (EXIT_FAILURE ); +out_fork: + (void )unlink (recp); +free (recp); +exit (err ?EXIT_FAILURE :EXIT_SUCCESS ); }close (pipefd[1 ]); err =read (pipefd[0 ], &num,sizeof (int ));
Signature · 1 line
2.43.5
PATCH 4/4 erofs-utils: mount: recover from detached NBD devices
In reply to Gao XiangAdd support for reconnecting to devices using recovery files, e.g.
$ sudo killall -9 mount.erofs $ sudo mount.erofs --reattach /dev/nbdX
--- lib/liberofs_nbd.h | 1 + mount/main.c | 140 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 126 insertions(+), 15 deletions(-)
lib/liberofs_nbd.h +1 -0
@@ -37,6 +37,7 @@ struct erofs_nbd_request {long erofs_nbd_in_service (int nbdnum);int erofs_nbd_devscan (void );int erofs_nbd_connect (int nbdfd,int blkbits,u64 blocks); +char *erofs_nbd_get_identifier (int nbdnum);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);
mount/main.c +125 -15
220 lines not on this page · raw message
Signature · 1 line
2.43.5