lkml.org 
[lkml]   [2015]   [Nov]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Date
    Subject[PATCH 3.2 11/52] Btrfs: fix file corruption and data loss after cloning inline extents
    3.2.74-rc1 review patch.  If anyone has any objections, please let me know.

    ------------------

    From: Filipe Manana <fdmanana@suse.com>

    commit 8039d87d9e473aeb740d4fdbd59b9d2f89b2ced9 upstream.

    Currently the clone ioctl allows to clone an inline extent from one file
    to another that already has other (non-inlined) extents. This is a problem
    because btrfs is not designed to deal with files having inline and regular
    extents, if a file has an inline extent then it must be the only extent
    in the file and must start at file offset 0. Having a file with an inline
    extent followed by regular extents results in EIO errors when doing reads
    or writes against the first 4K of the file.

    Also, the clone ioctl allows one to lose data if the source file consists
    of a single inline extent, with a size of N bytes, and the destination
    file consists of a single inline extent with a size of M bytes, where we
    have M > N. In this case the clone operation removes the inline extent
    from the destination file and then copies the inline extent from the
    source file into the destination file - we lose the M - N bytes from the
    destination file, a read operation will get the value 0x00 for any bytes
    in the the range [N, M] (the destination inode's i_size remained as M,
    that's why we can read past N bytes).

    So fix this by not allowing such destructive operations to happen and
    return errno EOPNOTSUPP to user space.

    Currently the fstest btrfs/035 tests the data loss case but it totally
    ignores this - i.e. expects the operation to succeed and does not check
    the we got data loss.

    The following test case for fstests exercises all these cases that result
    in file corruption and data loss:

    seq=`basename $0`
    seqres=$RESULT_DIR/$seq
    echo "QA output created by $seq"
    tmp=/tmp/$$
    status=1 # failure is the default!
    trap "_cleanup; exit \$status" 0 1 2 3 15

    _cleanup()
    {
    rm -f $tmp.*
    }

    # get standard environment, filters and checks
    . ./common/rc
    . ./common/filter

    # real QA test starts here
    _need_to_be_root
    _supported_fs btrfs
    _supported_os Linux
    _require_scratch
    _require_cloner
    _require_btrfs_fs_feature "no_holes"
    _require_btrfs_mkfs_feature "no-holes"

    rm -f $seqres.full

    test_cloning_inline_extents()
    {
    local mkfs_opts=$1
    local mount_opts=$2

    _scratch_mkfs $mkfs_opts >>$seqres.full 2>&1
    _scratch_mount $mount_opts

    # File bar, the source for all the following clone operations, consists
    # of a single inline extent (50 bytes).
    $XFS_IO_PROG -f -c "pwrite -S 0xbb 0 50" $SCRATCH_MNT/bar \
    | _filter_xfs_io

    # Test cloning into a file with an extent (non-inlined) where the
    # destination offset overlaps that extent. It should not be possible to
    # clone the inline extent from file bar into this file.
    $XFS_IO_PROG -f -c "pwrite -S 0xaa 0K 16K" $SCRATCH_MNT/foo \
    | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo

    # Doing IO against any range in the first 4K of the file should work.
    # Due to a past clone ioctl bug which allowed cloning the inline extent,
    # these operations resulted in EIO errors.
    echo "File foo data after clone operation:"
    # All bytes should have the value 0xaa (clone operation failed and did
    # not modify our file).
    od -t x1 $SCRATCH_MNT/foo
    $XFS_IO_PROG -c "pwrite -S 0xcc 0 100" $SCRATCH_MNT/foo | _filter_xfs_io

    # Test cloning the inline extent against a file which has a hole in its
    # first 4K followed by a non-inlined extent. It should not be possible
    # as well to clone the inline extent from file bar into this file.
    $XFS_IO_PROG -f -c "pwrite -S 0xdd 4K 12K" $SCRATCH_MNT/foo2 \
    | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo2

    # Doing IO against any range in the first 4K of the file should work.
    # Due to a past clone ioctl bug which allowed cloning the inline extent,
    # these operations resulted in EIO errors.
    echo "File foo2 data after clone operation:"
    # All bytes should have the value 0x00 (clone operation failed and did
    # not modify our file).
    od -t x1 $SCRATCH_MNT/foo2
    $XFS_IO_PROG -c "pwrite -S 0xee 0 90" $SCRATCH_MNT/foo2 | _filter_xfs_io

    # Test cloning the inline extent against a file which has a size of zero
    # but has a prealloc extent. It should not be possible as well to clone
    # the inline extent from file bar into this file.
    $XFS_IO_PROG -f -c "falloc -k 0 1M" $SCRATCH_MNT/foo3 | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo3

    # Doing IO against any range in the first 4K of the file should work.
    # Due to a past clone ioctl bug which allowed cloning the inline extent,
    # these operations resulted in EIO errors.
    echo "First 50 bytes of foo3 after clone operation:"
    # Should not be able to read any bytes, file has 0 bytes i_size (the
    # clone operation failed and did not modify our file).
    od -t x1 $SCRATCH_MNT/foo3
    $XFS_IO_PROG -c "pwrite -S 0xff 0 90" $SCRATCH_MNT/foo3 | _filter_xfs_io

    # Test cloning the inline extent against a file which consists of a
    # single inline extent that has a size not greater than the size of
    # bar's inline extent (40 < 50).
    # It should be possible to do the extent cloning from bar to this file.
    $XFS_IO_PROG -f -c "pwrite -S 0x01 0 40" $SCRATCH_MNT/foo4 \
    | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo4

    # Doing IO against any range in the first 4K of the file should work.
    echo "File foo4 data after clone operation:"
    # Must match file bar's content.
    od -t x1 $SCRATCH_MNT/foo4
    $XFS_IO_PROG -c "pwrite -S 0x02 0 90" $SCRATCH_MNT/foo4 | _filter_xfs_io

    # Test cloning the inline extent against a file which consists of a
    # single inline extent that has a size greater than the size of bar's
    # inline extent (60 > 50).
    # It should not be possible to clone the inline extent from file bar
    # into this file.
    $XFS_IO_PROG -f -c "pwrite -S 0x03 0 60" $SCRATCH_MNT/foo5 \
    | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo5

    # Reading the file should not fail.
    echo "File foo5 data after clone operation:"
    # Must have a size of 60 bytes, with all bytes having a value of 0x03
    # (the clone operation failed and did not modify our file).
    od -t x1 $SCRATCH_MNT/foo5

    # Test cloning the inline extent against a file which has no extents but
    # has a size greater than bar's inline extent (16K > 50).
    # It should not be possible to clone the inline extent from file bar
    # into this file.
    $XFS_IO_PROG -f -c "truncate 16K" $SCRATCH_MNT/foo6 | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo6

    # Reading the file should not fail.
    echo "File foo6 data after clone operation:"
    # Must have a size of 16K, with all bytes having a value of 0x00 (the
    # clone operation failed and did not modify our file).
    od -t x1 $SCRATCH_MNT/foo6

    # Test cloning the inline extent against a file which has no extents but
    # has a size not greater than bar's inline extent (30 < 50).
    # It should be possible to clone the inline extent from file bar into
    # this file.
    $XFS_IO_PROG -f -c "truncate 30" $SCRATCH_MNT/foo7 | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo7

    # Reading the file should not fail.
    echo "File foo7 data after clone operation:"
    # Must have a size of 50 bytes, with all bytes having a value of 0xbb.
    od -t x1 $SCRATCH_MNT/foo7

    # Test cloning the inline extent against a file which has a size not
    # greater than the size of bar's inline extent (20 < 50) but has
    # a prealloc extent that goes beyond the file's size. It should not be
    # possible to clone the inline extent from bar into this file.
    $XFS_IO_PROG -f -c "falloc -k 0 1M" \
    -c "pwrite -S 0x88 0 20" \
    $SCRATCH_MNT/foo8 | _filter_xfs_io
    $CLONER_PROG -s 0 -d 0 -l 0 $SCRATCH_MNT/bar $SCRATCH_MNT/foo8

    echo "File foo8 data after clone operation:"
    # Must have a size of 20 bytes, with all bytes having a value of 0x88
    # (the clone operation did not modify our file).
    od -t x1 $SCRATCH_MNT/foo8

    _scratch_unmount
    }

    echo -e "\nTesting without compression and without the no-holes feature...\n"
    test_cloning_inline_extents

    echo -e "\nTesting with compression and without the no-holes feature...\n"
    test_cloning_inline_extents "" "-o compress"

    echo -e "\nTesting without compression and with the no-holes feature...\n"
    test_cloning_inline_extents "-O no-holes" ""

    echo -e "\nTesting with compression and with the no-holes feature...\n"
    test_cloning_inline_extents "-O no-holes" "-o compress"

    status=0
    exit

    Signed-off-by: Filipe Manana <fdmanana@suse.com>
    [bwh: Backported to 3.2:
    - Adjust parameters to btrfs_drop_extents()
    - Drop use of ASSERT()
    - Keep using BUG_ON() for other error cases, as there is no
    btrfs_abort_transaction()
    - Adjust context]
    Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
    ---
    fs/btrfs/ioctl.c | 195 +++++++++++++++++++++++++++++++++++++++++++------------
    1 file changed, 152 insertions(+), 43 deletions(-)

    --- a/fs/btrfs/ioctl.c
    +++ b/fs/btrfs/ioctl.c
    @@ -2164,6 +2164,151 @@ out:
    return ret;
    }

    +/*
    + * Make sure we do not end up inserting an inline extent into a file that has
    + * already other (non-inline) extents. If a file has an inline extent it can
    + * not have any other extents and the (single) inline extent must start at the
    + * file offset 0. Failing to respect these rules will lead to file corruption,
    + * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
    + *
    + * We can have extents that have been already written to disk or we can have
    + * dirty ranges still in delalloc, in which case the extent maps and items are
    + * created only when we run delalloc, and the delalloc ranges might fall outside
    + * the range we are currently locking in the inode's io tree. So we check the
    + * inode's i_size because of that (i_size updates are done while holding the
    + * i_mutex, which we are holding here).
    + * We also check to see if the inode has a size not greater than "datal" but has
    + * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
    + * protected against such concurrent fallocate calls by the i_mutex).
    + *
    + * If the file has no extents but a size greater than datal, do not allow the
    + * copy because we would need turn the inline extent into a non-inline one (even
    + * with NO_HOLES enabled). If we find our destination inode only has one inline
    + * extent, just overwrite it with the source inline extent if its size is less
    + * than the source extent's size, or we could copy the source inline extent's
    + * data into the destination inode's inline extent if the later is greater then
    + * the former.
    + */
    +static int clone_copy_inline_extent(struct inode *src,
    + struct inode *dst,
    + struct btrfs_trans_handle *trans,
    + struct btrfs_path *path,
    + struct btrfs_key *new_key,
    + const u64 drop_start,
    + const u64 datal,
    + const u64 skip,
    + const u64 size,
    + char *inline_data)
    +{
    + struct btrfs_root *root = BTRFS_I(dst)->root;
    + const u64 aligned_end = ALIGN(new_key->offset + datal,
    + root->sectorsize);
    + int ret;
    + struct btrfs_key key;
    + u64 hint_byte;
    +
    + if (new_key->offset > 0)
    + return -EOPNOTSUPP;
    +
    + key.objectid = btrfs_ino(dst);
    + key.type = BTRFS_EXTENT_DATA_KEY;
    + key.offset = 0;
    + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
    + if (ret < 0) {
    + return ret;
    + } else if (ret > 0) {
    + if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
    + ret = btrfs_next_leaf(root, path);
    + if (ret < 0)
    + return ret;
    + else if (ret > 0)
    + goto copy_inline_extent;
    + }
    + btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
    + if (key.objectid == btrfs_ino(dst) &&
    + key.type == BTRFS_EXTENT_DATA_KEY) {
    + return -EOPNOTSUPP;
    + }
    + } else if (i_size_read(dst) <= datal) {
    + struct btrfs_file_extent_item *ei;
    + u64 ext_len;
    +
    + /*
    + * If the file size is <= datal, make sure there are no other
    + * extents following (can happen do to an fallocate call with
    + * the flag FALLOC_FL_KEEP_SIZE).
    + */
    + ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
    + struct btrfs_file_extent_item);
    + /*
    + * If it's an inline extent, it can not have other extents
    + * following it.
    + */
    + if (btrfs_file_extent_type(path->nodes[0], ei) ==
    + BTRFS_FILE_EXTENT_INLINE)
    + goto copy_inline_extent;
    +
    + ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
    + if (ext_len > aligned_end)
    + return -EOPNOTSUPP;
    +
    + ret = btrfs_next_item(root, path);
    + if (ret < 0) {
    + return ret;
    + } else if (ret == 0) {
    + btrfs_item_key_to_cpu(path->nodes[0], &key,
    + path->slots[0]);
    + if (key.objectid == btrfs_ino(dst) &&
    + key.type == BTRFS_EXTENT_DATA_KEY)
    + return -EOPNOTSUPP;
    + }
    + }
    +
    +copy_inline_extent:
    + /*
    + * We have no extent items, or we have an extent at offset 0 which may
    + * or may not be inlined. All these cases are dealt the same way.
    + */
    + if (i_size_read(dst) > datal) {
    + /*
    + * If the destination inode has an inline extent...
    + * This would require copying the data from the source inline
    + * extent into the beginning of the destination's inline extent.
    + * But this is really complex, both extents can be compressed
    + * or just one of them, which would require decompressing and
    + * re-compressing data (which could increase the new compressed
    + * size, not allowing the compressed data to fit anymore in an
    + * inline extent).
    + * So just don't support this case for now (it should be rare,
    + * we are not really saving space when cloning inline extents).
    + */
    + return -EOPNOTSUPP;
    + }
    +
    + btrfs_release_path(path);
    + ret = btrfs_drop_extents(trans, dst, drop_start, aligned_end,
    + &hint_byte, 1);
    + if (ret)
    + return ret;
    + ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
    + if (ret)
    + return ret;
    +
    + if (skip) {
    + const u32 start = btrfs_file_extent_calc_inline_size(0);
    +
    + memmove(inline_data + start, inline_data + start + skip, datal);
    + }
    +
    + write_extent_buffer(path->nodes[0], inline_data,
    + btrfs_item_ptr_offset(path->nodes[0],
    + path->slots[0]),
    + size);
    + inode_add_bytes(dst, datal);
    +
    + return 0;
    +}
    +
    static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
    u64 off, u64 olen, u64 destoff)
    {
    @@ -2448,20 +2593,6 @@ static noinline long btrfs_ioctl_clone(s
    new_key.offset += skip;
    }

    - /*
    - * Don't copy an inline extent into an offset
    - * greater than zero. Having an inline extent
    - * at such an offset results in chaos as btrfs
    - * isn't prepared for such cases. Just skip
    - * this case for the same reasons as commented
    - * at btrfs_ioctl_clone().
    - */
    - if (new_key.offset > 0) {
    - ret = -EOPNOTSUPP;
    - btrfs_end_transaction(trans, root);
    - goto out;
    - }
    -
    if (key.offset + datal > off+len)
    trim = key.offset + datal - (off+len);

    @@ -2473,29 +2604,20 @@ static noinline long btrfs_ioctl_clone(s
    size -= skip + trim;
    datal -= skip + trim;

    - ret = btrfs_drop_extents(trans, inode,
    - new_key.offset,
    - new_key.offset + datal,
    - &hint_byte, 1);
    - BUG_ON(ret);
    -
    - ret = btrfs_insert_empty_item(trans, root, path,
    - &new_key, size);
    - BUG_ON(ret);
    -
    - if (skip) {
    - u32 start =
    - btrfs_file_extent_calc_inline_size(0);
    - memmove(buf+start, buf+start+skip,
    - datal);
    + ret = clone_copy_inline_extent(src, inode,
    + trans, path,
    + &new_key,
    + new_key.offset,
    + datal,
    + skip, size, buf);
    + if (ret) {
    + BUG_ON(ret != -EOPNOTSUPP);
    + btrfs_end_transaction(trans, root);
    + goto out;
    }

    leaf = path->nodes[0];
    slot = path->slots[0];
    - write_extent_buffer(leaf, buf,
    - btrfs_item_ptr_offset(leaf, slot),
    - size);
    - inode_add_bytes(inode, datal);
    }

    btrfs_mark_buffer_dirty(leaf);


    \
     
     \ /
      Last update: 2015-11-25 00:01    [W:2.311 / U:1.056 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site