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 <konishi.ryusuke@gmail.com>
This commit is contained in:
Ryusuke Konishi 2026-01-21 20:53:35 +09:00
parent fb146a5345
commit fec73a61ce
3 changed files with 15 additions and 1 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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);