]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
losetup: set errno for misaligned offsets
authorKarel Zak <kzak@redhat.com>
Fri, 19 Jul 2013 15:05:31 +0000 (17:05 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 19 Jul 2013 15:15:44 +0000 (17:15 +0200)
References: https://bugs.archlinux.org/task/36189
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/loopdev.c
sys-utils/losetup.c

index 001a9ad4bc30e3bf533cdf557276c63c13eee7b1..9789feb88aa5a6d61a7a5bb3490973f56cfe3fa9 100644 (file)
@@ -1094,13 +1094,16 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
        if (!lc->info.lo_offset && !lc->info.lo_sizelimit)
                return 0;
 
-       if (fstat(file_fd, &st))
+       if (fstat(file_fd, &st)) {
+               DBG(lc, loopdev_debug("failed to fstat backing file"));
                return -errno;
-
+       }
        if (S_ISBLK(st.st_mode)) {
                if (blkdev_get_size(file_fd,
-                               (unsigned long long *) &expected_size))
+                               (unsigned long long *) &expected_size)) {
+                       DBG(lc, loopdev_debug("failed to determine device size"));
                        return -errno;
+               }
        } else
                expected_size = st.st_size;
 
@@ -1116,11 +1119,15 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
                expected_size = lc->info.lo_sizelimit;
 
        dev_fd = loopcxt_get_fd(lc);
-       if (dev_fd < 0)
+       if (dev_fd < 0) {
+               DBG(lc, loopdev_debug("failed to get loop FD"));
                return -errno;
+       }
 
-       if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
+       if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) {
+               DBG(lc, loopdev_debug("failed to determine loopdev size"));
                return -errno;
+       }
 
        if (expected_size != size) {
                DBG(lc, loopdev_debug("warning: loopdev and expected "
@@ -1137,8 +1144,13 @@ static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
                if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
                        return -errno;
 
-               if (expected_size != size)
-                       return -ERANGE;
+               if (expected_size != size) {
+                       errno = ERANGE;
+                       DBG(lc, loopdev_debug("failed to set loopdev size, "
+                                       "size: %ju, expected: %ju",
+                                       size, expected_size));
+                       return -errno;
+               }
        }
 
        return 0;
index 5dd3c6c3eb8e6ece3f9c085050b2a5eb8aef831d..1bd1f41a44008775912bf7be9e078a2469ce41ba 100644 (file)
@@ -620,6 +620,8 @@ int main(int argc, char **argv)
                int hasdev = loopcxt_has_device(&lc);
 
                do {
+                       const char *errpre;
+
                        /* Note that loopcxt_{find_unused,set_device}() resets
                         * loopcxt struct.
                         */
@@ -641,12 +643,18 @@ int main(int argc, char **argv)
                        res = loopcxt_setup_device(&lc);
                        if (res == 0)
                                break;                  /* success */
-                       if (errno != EBUSY) {
-                               warn(_("%s: failed to set up loop device"),
-                                       hasdev && loopcxt_get_fd(&lc) < 0 ?
-                                           loopcxt_get_device(&lc) : file);
-                               break;
-                       }
+                       if (errno == EBUSY)
+                               continue;
+
+                       /* errors */
+                       errpre = hasdev && loopcxt_get_fd(&lc) < 0 ?
+                                        loopcxt_get_device(&lc) : file;
+                       if (errno == ERANGE && offset && offset % 512)
+                               warnx(_("%s: failed to set up loop device, "
+                                       "offset is not 512-byte aligned."), errpre);
+                       else
+                               warn(_("%s: failed to set up loop device"), errpre);
+                       break;
                } while (hasdev == 0);
 
                if (res == 0) {