Skip to contentSkip to conversation

libmount: support explicit loop direct I/O requests

PATCH In review Tianyi Chen 2 participants 2 messages Sep 21 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmount: support explicit loop direct I/O requests

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.

---
Following the RFC discussion, this uses loopcxt_setup_device() without adding effective-mode readback for new devices:
https://lore.kernel.org/r/zxpnm3dbgy6vv64gockhcl5egfpe2qpld7jlkgg3enr5jbfha2@q27vjqxnocmb

Built with Meson. The new loop-direct-io test and existing regfile and fstab-loop tests pass in an x86-64 VM with ext4 backing storage.

 libmount/src/hook_loopdev.c         | 27 ++++++++-
 sys-utils/mount.8.adoc              | 10 +++-
 tests/expected/mount/loop-direct-io |  6 ++
 tests/ts/mount/loop-direct-io       | 91 +++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+), 2 deletions(-)
 create mode 100644 tests/expected/mount/loop-direct-io
 create mode 100755 tests/ts/mount/loop-direct-io
libmount/src/hook_loopdev.c +26 -1
index b2291340..cfd07fb1 100644
@@ -21,6 +21,7 @@
 
 struct hook_data {
 	int loopdev_fd;
+	bool reused;
 };
 
 static int delete_loopdev(struct libmnt_context *cxt, struct hook_data *hd);
@@ -138,7 +139,7 @@ static int setup_loopdev(struct libmnt_context *cxt,
 {
 	const char *backing_file, *loopdev = NULL;
 	struct loopdev_cxt lc;
-	int rc = 0, lo_flags = 0;
+	int rc = 0, lo_flags = 0, direct_io = -1;
 	uint64_t offset = 0, sizelimit = 0;
 	bool reuse = FALSE;
 	struct libmnt_opt *opt, *loopopt = NULL;
@@ -154,6 +155,19 @@ static int setup_loopdev(struct libmnt_context *cxt,
 		lo_flags |= LO_FLAGS_READ_ONLY;
 	}
 
+	opt = mnt_optlist_get_named(ol, "X-loopdev.direct-io", cxt->map_userspace);
+	if (opt) {
+		const char *value = mnt_opt_get_value(opt);
+
+		if (value && strcmp(value, "on") == 0) {
+			direct_io = 1;
+			lo_flags |= LO_FLAGS_DIRECT_IO;
+		} else if (value && strcmp(value, "off") == 0)
+			direct_io = 0;
+		else
+			return -MNT_ERR_MOUNTOPT;
+	}
+
 	/*
 	 * loop=
 	 */
@@ -257,6 +271,13 @@ static int setup_loopdev(struct libmnt_context *cxt,
 				goto done;
 			}
 
+			if (direct_io != -1 && !!loopcxt_is_dio(&lc) != direct_io) {
+				DBG_OBJ(LOOP, cxt, ul_debug("%s has incompatible direct I/O mode",
+						loopcxt_get_device(&lc)));
+				rc = -MNT_ERR_LOOPOVERLAP;
+				goto done;
+			}
+
 			/* This is no more supported, but check to be safe. */
 			if (loopcxt_get_encrypt_type(&lc, &lc_encrypt_type) == 0
 			    && lc_encrypt_type != LO_CRYPT_NONE) {
@@ -350,6 +371,7 @@ static int setup_loopdev(struct libmnt_context *cxt,
 	} while (1);
 
 success:
+	hd->reused = reuse;
 	if (!rc)
 		rc = mnt_fs_set_source(cxt->fs, loopcxt_get_device(&lc));
 
@@ -408,6 +430,9 @@ static int delete_loopdev(struct libmnt_context *cxt, struct hook_data *hd)
 		hd->loopdev_fd = -1;
 	}
 
+	if (hd && hd->reused)
+		return 0;
+
 	rc = loopdev_detach(src);	/* see lib/loopdev.c */
 
 	DBG_OBJ(LOOP, cxt, ul_debug("deleted [rc=%d]", rc));
sys-utils/mount.8.adoc +9 -1
index 922ecda7..208a1a16 100644
@@ -796,6 +796,14 @@ ____
 mount -t auto -o'X-mount.auto-fstypes="noext2,ext3"'  /dev/sdc1 /mnt/test
 ____
 
+*X-loopdev.direct-io*=*on*|*off*::
+Enable or disable direct I/O when setting up a loop device. If a loop device
+already exists for the same file, offset and size limit, its direct I/O mode
+must match; otherwise the mount fails without changing the existing device.
+Without this option, new loop devices use buffered I/O and existing devices
+can be reused in either mode. This option does not change an existing mount
+or apply when no loop device is needed. See also *losetup*(8) *--direct-io*.
+
 *X-mount.mkdir*[**=**_mode_]::
 Allow to make a target directory (mountpoint) if it does not exist yet. The optional argument _mode_ specifies the filesystem access mode used for *mkdir*(2) in octal notation. The default mode is 0755. This functionality is supported only for root users or when *mount* is executed without suid permissions. The option is also supported as *x-mount.mkdir*, but this notation is deprecated since v2.30. See also *--mkdir* command line option.
 
@@ -1708,7 +1716,7 @@ ____
 *mount -t ext4 /tmp/disk.img /mnt*
 ____
 
-This type of mount knows about three options, namely *loop*, *offset* and *sizelimit*, that are really options to *losetup*(8). (These options can be used in addition to those specific to the filesystem type.)
+Loop device setup accepts *loop*, *offset*, *sizelimit* and *X-loopdev.direct-io*, corresponding to options of *losetup*(8). (These options can be used in addition to those specific to the filesystem type.)
 
 Since Linux 2.6.25 auto-destruction of loop devices is supported, meaning that any loop device allocated by *mount* will be freed by *umount* independently of _/etc/mtab_.
 
tests/expected/mount/loop-direct-io new file +6 -0
new file mode 100644
index 00000000..990eda84
@@ -0,0 +1,6 @@
+invalid options: OK
+new device (on): OK
+new device (off): OK
+new device (default): OK
+reuse and failed mount (on): OK
+reuse and failed mount (off): OK
tests/ts/mount/loop-direct-io new file +91 -0

91 lines not on this page · raw message

base-commit: 55e3c4dba6101a47ecaae71c91edbe10c59b8d56

Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 7 lines · Tianyi Chen
 libmount/src/hook_loopdev.c         | 27 ++++++++-
 sys-utils/mount.8.adoc              | 10 +++-
 tests/expected/mount/loop-direct-io |  6 ++
 tests/ts/mount/loop-direct-io       | 91 +++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+), 2 deletions(-)
 create mode 100644 tests/expected/mount/loop-direct-io
 create mode 100755 tests/ts/mount/loop-direct-io

Created https://github.com/util-linux/util-linux/pull/4646 to test it by CI and for review. Thanks.

libmount/src/hook_loopdev.c static int delete_loopdev(struct libmnt_context *cxt, struct hook_data *hd) · line 434

Quoted 2 lines
libmount/src/hook_loopdev.c +2 -0
+	if (hd && hd->reused)
+		return 0;

This is a good catch!

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com