public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] loopdev: sync capacity after setting it
@ 2013-03-15 21:04 Jeff Mahoney
  2013-03-17 14:18 ` Jeff Mahoney
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff Mahoney @ 2013-03-15 21:04 UTC (permalink / raw)
  To: util-linux

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:
# losetup -r -o 32k --sizelimit=102400000 <loopdev> <imagefile>
# mount -r -t hfsplus <loopdev> <mountpoint>

But failed with this:
# mount -r -oloop,offset=32k,sizelimit=102400000 <imagefile> <mountpoint>

# losetup -a
/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:
# blockdev --getsize64 /dev/loop[01]
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.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
---
 lib/loopdev.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -1105,6 +1105,18 @@ int loopcxt_setup_device(struct loopdev_
 		goto err;
 	}
 
+	/*
+	 * mount passes the file descriptor without closing it, so
+	 * the capacity isn't updated automatically. File systems
+	 * won't see the new size if it's not the full size of
+	 * the backing file.
+	 */
+	if ((lc->info.lo_offset || lc->info.lo_sizelimit) &&
+	    ioctl(dev_fd, LOOP_SET_CAPACITY, 0)) {
+		DBG(lc, loopdev_debug("LOOP_SET_CAPACITY failed: %m"));
+		goto err;
+	}
+
 	DBG(lc, loopdev_debug("setup: LOOP_SET_STATUS64: OK"));
 
 	memset(&lc->info, 0, sizeof(lc->info));

-- 
Jeff Mahoney
SUSE Labs

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-04-09 12:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-15 21:04 [PATCH] loopdev: sync capacity after setting it Jeff Mahoney
2013-03-17 14:18 ` Jeff Mahoney
2013-03-18  9:27   ` Karel Zak
2013-03-18 13:05     ` Jeff Mahoney
2013-03-18 13:18       ` Karel Zak
2013-03-27 20:45         ` [PATCH v2] " Jeff Mahoney
2013-04-09 12:39           ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox