Skip to content

libmount: support explicit loop direct I/O requests (ML) - #4646

Open
karelzak wants to merge 1 commit into
util-linux:masterfrom
karelzak:PR/libmount-loopdev-directio
Open

karelzak wants to merge 1 commit into
util-linux:masterfrom
karelzak:PR/libmount-loopdev-directio

Conversation

@karelzak

Copy link
Copy Markdown
Collaborator

Allow X-loopdev.direct-io=on|off to select the direct I/O mode when mounting a file through a loop device. Pass the flag to the existing loop setup path and require a matching mode when reusing a device. Omitting the option preserves the current behavior.

Keep reused devices attached if the mount fails; only release the reference acquired by this mount. Add tests for option parsing, new devices, reuse, mode conflicts and failure cleanup.

Fixes: #3464
Assisted-by: LLM

@karelzak karelzak added the PR-FROM-MAILING-LIST This pull request is based on a patch from the mailing list. label Sep 21, 2026
Allow X-loopdev.direct-io=on|off to select the direct I/O mode when
mounting a file through a loop device. Pass the flag to the existing
loop setup path and require a matching mode when reusing a device.
Omitting the option preserves the current behavior.

Keep reused devices attached if the mount fails; only release the
reference acquired by this mount. Add tests for option parsing, new
devices, reuse, mode conflicts and failure cleanup.

Fixes: util-linux#3464
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
@karelzak
karelzak force-pushed the PR/libmount-loopdev-directio branch from fee112d to 42979c7 Compare September 22, 2026 08:41
@karelzak

karelzak commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the patch. A few things before this can go in:

1. Misleading error for the direct-I/O mismatch

rc = -MNT_ERR_LOOPOVERLAP;

MNT_ERR_LOOPOVERLAP makes mount(8) print "overlapping loop device exists for <file>" (see mnt_context_get_excode() in context_mount.c), which tells the user nothing about direct I/O. Please set a specific message with mnt_context_sprintf_mesg() before returning, the same way the "device node %s is lost" case a few lines below does.

2. Please split the commit

The hd->reused change — not detaching a device that this mount did not create — is an independent bug fix. Today a failed mount tears down a loop device the user set up with losetup beforehand. That deserves its own commit (it is also backportable on its own); the X-loopdev.direct-io feature then goes on top.

3. Document that direct-io=on is not guaranteed

The kernel clears LO_FLAGS_DIRECT_IO when the backing file offset is unaligned or the block size is too small — see loop_update_dio() and the comment above it in drivers/block/loop.c. So an explicit X-loopdev.direct-io=on can succeed with buffered I/O. Please mention this in mount.8.adoc: direct I/O is a request, not a guarantee, and the effective mode can be checked with losetup --list -O NAME,DIO.

4. Use ul_strtobool() for the value

Instead of open-coding strcmp(value, "on") / strcmp(value, "off"), use ul_strtobool() from include/strutils.h (already included here). It is library-safe (returns -EINVAL, does not errx()) and accepts the same spellings as losetup --direct-io. The man page refers the user to losetup(8) --direct-io, so the accepted values should not be narrower.

5. Test should use the test-suite helpers

tests/ts/mount/loop-direct-io installs its own trap cleanup EXIT instead of using ts_register_loop_device / ts_cleanup_on_exit. Please follow the framework convention. Also, $DEVICE is assigned and reset all over the test but cleanup() never reads it — it re-derives the device from losetup -j — so all that bookkeeping is dead code.

Minor / style:

  • Coding style: if one branch of an if/else chain needs braces, all branches get them.
  • The new option block has no comment header; the neighbouring loop=, offset= and sizelimit= blocks all have one. It also returns directly rather than using the surrounding rc = ...; goto done_no_deinit pattern.
  • The early return 0 in delete_loopdev() skips the DBG_OBJ(LOOP, cxt, ul_debug("deleted [rc=%d]")) trace — please add a debug message for the "keeping reused device" path so it is visible with LIBMOUNT_DEBUG=all.
  • Please drop Assisted-by: LLM from the commit message; see the attribution rules in AGENTS.md.

— assisted by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR-FROM-MAILING-LIST This pull request is based on a patch from the mailing list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mount: Add a way to setup direct-io with -o loop

2 participants