]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
loopdev: sync capacity after setting it
authorJeff Mahoney <jeffm@suse.com>
Tue, 9 Apr 2013 12:32:50 +0000 (14:32 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 9 Apr 2013 12:32:50 +0000 (14:32 +0200)
I recently tried to mount an hfsplus file system from an image file with
a partition table by using the loop offset and sizelimit options to specify
the location of the file system.

hfsplus stores some metadata at a set offset from the end of the partition,
so it's sensitive to the device size reported by the kernel.

It worked with this:

But failed with this:

/dev/loop0: [0089]:2 (<imagefile>), offset 32768, sizelimit 102400000
/dev/loop1: [0089]:2 (<imagefile>), offset 32768, sizelimit 102400000

/proc/partitions shows the correct number of blocks to match the sizelimit.

But if I set a breakpoint in mount before the mount syscall, I could see:
102400000
102432768

The kernel loop driver will set the gendisk capacity of the device at
LOOP_SET_STATUS64 but won't sync it to the block device until one of two
conditions are met: All open file descriptors referring to the device are
closed (and it will sync when re-opened) or if the LOOP_SET_CAPACITY ioctl
is called to sync it. Since mount opens the device and passes it directly
to the mount syscall after LOOP_SET_STATUS64 without closing and reopening
it, the sizelimit argument is effectively ignroed. The capacity needs to
be synced immediately for it to work as expected.

This patch adds the LOOP_SET_CAPACITY call to loopctx_setup_device since
the device isn't yet released to the user, so it's safe to sync the capacity
immediately.

[kzak@redhat.com: - port to the current git HEAD,
                  - use uint64_t]

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/loopdev.h
lib/Makemodule.am
lib/blkdev.c
lib/loopdev.c
sys-utils/losetup.c
sys-utils/mount.8

index 6efa0c78f777885028a665bdd92d5eb6da31b27f..9f199a086ebe4af5309d8c9aef851a4d92d35c61 100644 (file)
@@ -163,6 +163,7 @@ extern int loopcxt_next(struct loopdev_cxt *lc);
 
 extern int loopcxt_setup_device(struct loopdev_cxt *lc);
 extern int loopcxt_delete_device(struct loopdev_cxt *lc);
+extern int loopcxt_set_capacity(struct loopdev_cxt *lc);
 
 int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset);
 int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit);
index a118958c5aaee3c2b5e27fba7b1be6bbc64c6ea0..afc2156c72c9bd0f229a4c3e20bff9d7159e0ba9 100644 (file)
@@ -70,7 +70,7 @@ test_ttyutils_CFLAGS = -DTEST_PROGRAM
 test_ttyutils_LDADD = libcommon.la
 
 test_blkdev_SOURCES = lib/blkdev.c
-test_blkdev_CFLAGS = -DTEST_PROGRAM
+test_blkdev_CFLAGS = -DTEST_PROGRAM_BLKDEV
 test_blkdev_LDADD = libcommon.la
 
 test_ismounted_SOURCES = lib/ismounted.c
index 09c1a2ff3d78d7c46d1080b98aa68fb1cb0d5c9a..f8182c0b3ba4ae927ea6bf4993f325846746cf1f 100644 (file)
@@ -28,7 +28,6 @@
 #include "blkdev.h"
 #include "c.h"
 #include "linux_version.h"
-#include "xalloc.h"
 
 static long
 blkdev_valid_offset (int fd, off_t offset) {
@@ -336,7 +335,7 @@ const char *blkdev_scsi_type_to_name(int type)
        return NULL;
 }
 
-#ifdef TEST_PROGRAM
+#ifdef TEST_PROGRAM_BLKDEV
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
index 2a696ab4d6e2791724e8c2fb9544780f9157ced3..c35e306f94a3c96cfd9cf8d7c9cd3b67c62c57eb 100644 (file)
@@ -41,6 +41,7 @@
 #include "loopdev.h"
 #include "canonicalize.h"
 #include "at.h"
+#include "blkdev.h"
 
 #define CONFIG_LOOPDEV_DEBUG
 
@@ -1071,6 +1072,64 @@ int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
        return 0;
 }
 
+/*
+ * In kernels prior to v3.9, if the offset or sizelimit options
+ * are used, the block device's size won't be synced automatically.
+ * blockdev --getsize64 and filesystems will use the backing
+ * file size until the block device has been re-opened or the
+ * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
+ *
+ * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
+ * the open file descriptor to the mount system call, we need to use
+ * the ioctl. Calling losetup directly doesn't have this problem since
+ * it closes the device when it exits and whatever consumes the device
+ * next will re-open it, causing the resync.
+ */
+static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
+{
+       uint64_t size, expected_size;
+       int dev_fd;
+       struct stat st;
+
+       if (!lc->info.lo_offset && !lc->info.lo_sizelimit)
+               return 0;
+
+       if (fstat(file_fd, &st))
+               return -errno;
+
+       expected_size = st.st_size;
+
+       if (lc->info.lo_offset > 0)
+               expected_size -= lc->info.lo_offset;
+
+       if (lc->info.lo_sizelimit > 0 && lc->info.lo_sizelimit < expected_size)
+               expected_size = lc->info.lo_sizelimit;
+
+       dev_fd = loopcxt_get_fd(lc);
+       if (dev_fd < 0)
+               return -errno;
+
+       if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
+               return -errno;
+
+       if (expected_size != size) {
+               if (loopcxt_set_capacity(lc)) {
+                       /* ioctl not available */
+                       if (errno == ENOTTY || errno == EINVAL)
+                               errno = ERANGE;
+                       return -errno;
+               }
+
+               if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
+                       return -errno;
+
+               if (expected_size != size)
+                       return -ERANGE;
+       }
+
+       return 0;
+}
+
 /*
  * @cl: context
  *
@@ -1150,9 +1209,6 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
 
        DBG(lc, loopdev_debug("setup: LOOP_SET_FD: OK"));
 
-       close(file_fd);
-       file_fd = -1;
-
        if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
                DBG(lc, loopdev_debug("LOOP_SET_STATUS64 failed: %m"));
                goto err;
@@ -1160,6 +1216,12 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
 
        DBG(lc, loopdev_debug("setup: LOOP_SET_STATUS64: OK"));
 
+       if ((rc = loopcxt_check_size(lc, file_fd)))
+               goto err;
+
+       close(file_fd);
+       file_fd = -1;
+
        memset(&lc->info, 0, sizeof(lc->info));
        lc->has_info = 0;
        lc->info_failed = 0;
@@ -1176,6 +1238,24 @@ err:
        return rc;
 }
 
+int loopcxt_set_capacity(struct loopdev_cxt *lc)
+{
+       int fd = loopcxt_get_fd(lc);
+
+       if (fd < 0)
+               return -EINVAL;
+
+       /* Kernels prior to v2.6.30 don't support this ioctl */
+       if (ioctl(fd, LOOP_SET_CAPACITY, 0) < 0) {
+               int rc = -errno;
+               DBG(lc, loopdev_debug("LOOP_SET_CAPACITY failed: %m"));
+               return rc;
+       }
+
+       DBG(lc, loopdev_debug("capacity set"));
+       return 0;
+}
+
 int loopcxt_delete_device(struct loopdev_cxt *lc)
 {
        int fd = loopcxt_get_fd(lc);
index 8f3614e1f5fd2e71e86c2ebc6d0a396026a8952a..ccf120e9f4230504f5f658156005af5b42d6a043 100644 (file)
@@ -183,20 +183,6 @@ static int show_all_loops(struct loopdev_cxt *lc, const char *file,
        return 0;
 }
 
-static int set_capacity(struct loopdev_cxt *lc)
-{
-       int fd = loopcxt_get_fd(lc);
-
-       if (fd < 0)
-               warn(_("cannot open %s"), loopcxt_get_device(lc));
-       else if (ioctl(fd, LOOP_SET_CAPACITY) != 0)
-               warnx(_("%s: set capacity failed"), loopcxt_get_device(lc));
-       else
-               return 0;
-
-       return -1;
-}
-
 static int delete_loop(struct loopdev_cxt *lc)
 {
        if (loopcxt_delete_device(lc))
@@ -685,7 +671,10 @@ int main(int argc, char **argv)
                        warn(_("%s"), loopcxt_get_device(&lc));
                break;
        case A_SET_CAPACITY:
-               res = set_capacity(&lc);
+               res = loopcxt_set_capacity(&lc);
+               if (res)
+                       warn(_("%s: set capacity failed"),
+                               loopcxt_get_device(&lc));
                break;
        default:
                usage(stderr);
index 72f0f0b45652c9c83b463e9a720526042c437f12..626d86169161bfe341e804d5669b38f55e6d4108 100644 (file)
@@ -2976,6 +2976,22 @@ and
 .BR ioctl
 families of functions) may lead to inconsistent result due to the lack of
 consistency check in kernel even if noac is used.
+.PP
+The
+.B loop
+option with the
+.B offset
+or
+.B sizelimit
+options used may fail when using older kernels if the
+.B mount
+command can't confirm that the size of the block device has been configured
+as requested. This situation can be worked around by using
+the
+.B losetup
+command manually before calling
+.B mount
+with the configured loop device.
 .SH HISTORY
 A
 .B mount