From fec73a61ce86cd0ee12b5596f66ac2f060a9936e Mon Sep 17 00:00:00 2001 From: Ryusuke Konishi Date: Wed, 21 Jan 2026 20:53:35 +0900 Subject: [PATCH] libnilfs: allow opening nilfs by resolving backing device Introduce a new open flag NILFS_OPEN_SRCHDEV. If this flag is set and the 'dev' argument is a file or directory on a NILFS filesystem, nilfs_open() automatically locates the backing block device. The logic calls the internal helper function nilfs_lookup_device() to retrieve the device ID from the given path and resolve the canonical device path (e.g. "/dev/sda1") via sysfs. To support this, the source file "lookup_device.c" is added to the build list in lib/Makefile.am. This feature eliminates the need for users or tools to explicitly specify the block device path when operating on a mounted NILFS filesystem. Signed-off-by: Ryusuke Konishi --- include/nilfs.h | 1 + lib/Makefile.am | 2 +- lib/nilfs.c | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/nilfs.h b/include/nilfs.h index 111a7e6..893fb2f 100644 --- a/include/nilfs.h +++ b/include/nilfs.h @@ -76,6 +76,7 @@ struct nilfs_layout { #define NILFS_OPEN_WRONLY 0x0004 /* Open NILFS API in write only mode */ #define NILFS_OPEN_RDWR 0x0008 /* Open NILFS API in read/write mode */ #define NILFS_OPEN_GCLK 0x1000 /* Open GC lock primitive */ +#define NILFS_OPEN_SRCHDEV 0x2000 /* Search device bound to the node */ struct nilfs *nilfs_open(const char *dev, const char *dir, int flags); diff --git a/lib/Makefile.am b/lib/Makefile.am index ca6c5db..dcfb495 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -27,7 +27,7 @@ libnilfs_REVISION = 0 libnilfs_AGE = 0 libnilfs_VERSIONINFO = $(libnilfs_CURRENT):$(libnilfs_REVISION):$(libnilfs_AGE) -libnilfs_la_SOURCES = nilfs.c sb.c +libnilfs_la_SOURCES = nilfs.c sb.c lookup_device.c libnilfs_la_LDFLAGS = -version-info $(libnilfs_VERSIONINFO) libnilfs_la_LIBADD = librealpath.la libcrc32.la $(LIB_POSIX_SEM) diff --git a/lib/nilfs.c b/lib/nilfs.c index 25bc95d..90821e4 100644 --- a/lib/nilfs.c +++ b/lib/nilfs.c @@ -82,6 +82,7 @@ #include "util.h" #include "pathnames.h" #include "realpath.h" +#include "lookup_device.h" /* nilfs_lookup_device() */ /** * struct nilfs - nilfs object @@ -408,6 +409,7 @@ static int nilfs_open_sem(struct nilfs *nilfs) struct nilfs *nilfs_open(const char *dev, const char *dir, int flags) { struct nilfs *nilfs; + char *backdev; uint64_t features; int ret; @@ -429,6 +431,15 @@ struct nilfs *nilfs_open(const char *dev, const char *dir, int flags) nilfs->n_opts = 0; nilfs->n_mincno = NILFS_CNO_MIN; memset(nilfs->n_sems, 0, sizeof(nilfs->n_sems)); + backdev = NULL; + + if ((flags & NILFS_OPEN_SRCHDEV) && dev) { + ret = nilfs_lookup_device(dev, &backdev); + if (unlikely(ret < 0)) + goto out_fd; + else if (ret > 0) + dev = backdev; /* replace dev in this function */ + } if (flags & NILFS_OPEN_RAW) { if (dev == NULL) { @@ -499,6 +510,7 @@ struct nilfs *nilfs_open(const char *dev, const char *dir, int flags) } /* success */ + free(backdev); return nilfs; /* error */ @@ -508,6 +520,7 @@ out_fd: if (nilfs->n_iocfd >= 0) close(nilfs->n_iocfd); + free(backdev); free(nilfs->n_dev); free(nilfs->n_ioc); free(nilfs->n_sb);