alistair23-linux/fs/ext4
Roman Pen 2a9b8cba62 ext4: Include forgotten start block on fallocate insert range
While doing 'insert range' start block should be also shifted right.
The bug can be easily reproduced by the following test:

    ptr = malloc(4096);
    assert(ptr);

    fd = open("./ext4.file", O_CREAT | O_TRUNC | O_RDWR, 0600);
    assert(fd >= 0);

    rc = fallocate(fd, 0, 0, 8192);
    assert(rc == 0);
    for (i = 0; i < 2048; i++)
            *((unsigned short *)ptr + i) = 0xbeef;
    rc = pwrite(fd, ptr, 4096, 0);
    assert(rc == 4096);
    rc = pwrite(fd, ptr, 4096, 4096);
    assert(rc == 4096);

    for (block = 2; block < 1000; block++) {
            rc = fallocate(fd, FALLOC_FL_INSERT_RANGE, 4096, 4096);
            assert(rc == 0);

            for (i = 0; i < 2048; i++)
                    *((unsigned short *)ptr + i) = block;

            rc = pwrite(fd, ptr, 4096, 4096);
            assert(rc == 4096);
    }

Because start block is not included in the range the hole appears at
the wrong offset (just after the desired offset) and the following
pwrite() overwrites already existent block, keeping hole untouched.

Simple way to verify wrong behaviour is to check zeroed blocks after
the test:

   $ hexdump ./ext4.file | grep '0000 0000'

The root cause of the bug is a wrong range (start, stop], where start
should be inclusive, i.e. [start, stop].

This patch fixes the problem by including start into the range.  But
not to break left shift (range collapse) stop points to the beginning
of the a block, not to the end.

The other not obvious change is an iterator check on validness in a
main loop.  Because iterator is unsigned the following corner case
should be considered with care: insert a block at 0 offset, when stop
variables overflows and never becomes less than start, which is 0.
To handle this special case iterator is set to NULL to indicate that
end of the loop is reached.

Fixes: 331573febb
Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: Namjae Jeon <namjae.jeon@samsung.com>
Cc: Andreas Dilger <adilger.kernel@dilger.ca>
Cc: stable@vger.kernel.org
2017-01-08 20:59:35 -05:00
..
acl.c ext4: use current_time() for inode timestamps 2016-11-14 21:40:10 -05:00
acl.h
balloc.c
bitmap.c
block_validity.c ext4: add missing KERN_CONT to a few more debugging uses 2016-10-15 09:57:31 -04:00
dir.c ext4: remove unused variable 2016-09-30 02:14:56 -04:00
ext4.h fscrypt: make fscrypt_operations.key_prefix a string 2017-01-08 01:03:41 -05:00
ext4_extents.h
ext4_jbd2.c
ext4_jbd2.h ext4: do not perform data journaling when data is encrypted 2016-12-10 17:54:58 -05:00
extents.c ext4: Include forgotten start block on fallocate insert range 2017-01-08 20:59:35 -05:00
extents_status.c
extents_status.h
file.c ext4: Simplify DAX fault path 2016-12-26 20:29:25 -08:00
fsync.c
hash.c
ialloc.c fscrypt: use ENOKEY when file cannot be created w/o key 2016-12-31 16:26:20 -05:00
indirect.c
inline.c ext4: return -ENOMEM instead of success 2016-12-10 09:56:01 -05:00
inode.c Merge branch 'for-4.10/fs-unmap' of git://git.kernel.dk/linux-block 2016-12-14 17:09:00 -08:00
ioctl.c Replace <asm/uaccess.h> with <linux/uaccess.h> globally 2016-12-24 11:46:01 -08:00
Kconfig ext4: Add select for CONFIG_FS_IOMAP 2016-11-22 23:21:58 -05:00
Makefile
mballoc.c ext4: fix stack memory corruption with 64k block size 2016-11-14 21:26:26 -05:00
mballoc.h ext4: add missing KERN_CONT to a few more debugging uses 2016-10-15 09:57:31 -04:00
migrate.c
mmp.c block,fs: use REQ_* flags directly 2016-11-01 09:43:26 -06:00
move_extent.c
namei.c ext4: don't allow encrypted operations without keys 2017-01-08 00:58:23 -05:00
page-io.c Merge branch 'for-4.10/fs-unmap' of git://git.kernel.dk/linux-block 2016-12-14 17:09:00 -08:00
readpage.c
resize.c
super.c fscrypt: make fscrypt_operations.key_prefix a string 2017-01-08 01:03:41 -05:00
symlink.c vfs: remove ".readlink = generic_readlink" assignments 2016-12-09 16:45:04 +01:00
sysfs.c ext4: do not advertise encryption support when disabled 2016-10-12 23:24:51 -04:00
truncate.h
xattr.c ext4: correctly detect when an xattr value has an invalid size 2016-12-01 14:57:29 -05:00
xattr.h
xattr_security.c
xattr_trusted.c
xattr_user.c