]> git.openfabrics.org - ~shefty/rdma-dev.git/commitdiff
loopdev: ignore negative offset when calculate loop device size
authorGuo Chao <yan@linux.vnet.ibm.com>
Thu, 21 Feb 2013 23:16:50 +0000 (15:16 -0800)
committerJens Axboe <axboe@kernel.dk>
Fri, 22 Feb 2013 09:43:22 +0000 (10:43 +0100)
Negative offset may cause loop device size larger than backing file
size.

 $ fallocate -l 1M a
 $ losetup --offset 0xffffffffffff0000 /dev/loop0 a
 $ blockdev --getsize64 /dev/loop0
 1114112
 $ ls -l a
 -rw-r--r-- 1 root root 1048576 Jan 23 12:46 a
 $ cat /dev/loop0
 cat: /dev/loop0: Input/output error

It makes no sense to do that. Only apply offset when it's positive.

Fix a typo in the comment by the way.

Signed-off-by: Guo Chao <yan@linux.vnet.ibm.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Guo Chao <yan@linux.vnet.ibm.com>
Cc: M. Hindess <hindessm@uk.ibm.com>
Cc: Nikanth Karthikesan <knikanth@suse.de>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c

index 1b1b0c981a85e2d9f296d027e3078bc31edfb0ee..bef9d63b0c838d7f0da1d6b8c64a534026d4b18e 100644 (file)
@@ -162,12 +162,13 @@ static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
 
 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
 {
-       loff_t size, loopsize;
+       loff_t loopsize;
 
        /* Compute loopsize in bytes */
-       size = i_size_read(file->f_mapping->host);
-       loopsize = size - offset;
-       /* offset is beyond i_size, wierd but possible */
+       loopsize = i_size_read(file->f_mapping->host);
+       if (offset > 0)
+               loopsize -= offset;
+       /* offset is beyond i_size, weird but possible */
        if (loopsize < 0)
                return 0;