1 Upstream-Status: Backport
2 Signed-off-by: Jonathan Liu <net147@gmail.com>
4 From e3b6cb87e0ba1304fa07ec316784de1c6243b28e Mon Sep 17 00:00:00 2001
5 From: Karel Zak <kzak@redhat.com>
6 Date: Mon, 13 May 2013 10:54:41 +0200
7 Subject: [PATCH] lib/loopdev: fix loopcxt_check_size() to work with blkdevs
9 The loopcxt_check_size() is workaround for kernels < v3.9, kernel has
10 been fixed by commit 541c742a7559eb65f0e36d3e2338c2ca532a3e61.
12 The function sets loopdev size according to backing file size. The
13 problem is that the backing file could be a block device where
14 stat.st_size is zero, so we have to use blkdev_get_size() for block
17 Addresses: https://bugs.archlinux.org/task/35193
18 Reported-by: Dave Reisner <d@falconindy.com>
19 Signed-off-by: Karel Zak <kzak@redhat.com>
21 lib/loopdev.c | 16 +++++++++++++++-
22 1 file changed, 15 insertions(+), 1 deletion(-)
24 diff --git a/lib/loopdev.c b/lib/loopdev.c
25 index c35e306..3b65b5d 100644
28 @@ -1097,7 +1097,17 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
29 if (fstat(file_fd, &st))
32 - expected_size = st.st_size;
33 + if (S_ISBLK(st.st_mode)) {
34 + if (blkdev_get_size(file_fd,
35 + (unsigned long long *) &expected_size))
38 + expected_size = st.st_size;
40 + if (expected_size == 0 || expected_size <= lc->info.lo_offset) {
41 + DBG(lc, loopdev_debug("failed to determine expected size"));
42 + return 0; /* ignore this error */
45 if (lc->info.lo_offset > 0)
46 expected_size -= lc->info.lo_offset;
47 @@ -1113,6 +1123,10 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
50 if (expected_size != size) {
51 + DBG(lc, loopdev_debug("warning: loopdev and expected "
52 + "size dismatch (%ju/%ju)",
53 + size, expected_size));
55 if (loopcxt_set_capacity(lc)) {
56 /* ioctl not available */
57 if (errno == ENOTTY || errno == EINVAL)