• Filipe Manana's avatar
    btrfs: fix RWF_NOWAIT write not failling when we need to cow · 260a6339
    Filipe Manana authored
    If we attempt to do a RWF_NOWAIT write against a file range for which we
    can only do NOCOW for a part of it, due to the existence of holes or
    shared extents for example, we proceed with the write as if it were
    possible to NOCOW the whole range.
    
    Example:
    
      $ mkfs.btrfs -f /dev/sdb
      $ mount /dev/sdb /mnt
    
      $ touch /mnt/sdj/bar
      $ chattr +C /mnt/sdj/bar
    
      $ xfs_io -d -c "pwrite -S 0xab -b 256K 0 256K" /mnt/bar
      wrote 262144/262144 bytes at offset 0
      256 KiB, 1 ops; 0.0003 sec (694.444 MiB/sec and 2777.7778 ops/sec)
    
      $ xfs_io -c "fpunch 64K 64K" /mnt/bar
      $ sync
    
      $ xfs_io -d -c "pwrite -N -V 1 -b 128K -S 0xfe 0 128K" /mnt/bar
      wrote 131072/131072 bytes at offset 0
      128 KiB, 1 ops; 0.0007 sec (160.051 MiB/sec and 1280.4097 ops/sec)
    
    This last write should fail with -EAGAIN since the file range from 64K to
    128K is a hole. On xfs it fails, as expected, but on ext4 it currently
    succeeds because apparently it is expensive to check if there are extents
    allocated for the whole range, but I'll check with the ext4 people.
    
    Fix the issue by checking if check_can_nocow() returns a number of
    NOCOW'able bytes smaller then the requested number of bytes, and if it
    does return -EAGAIN.
    
    Fixes: edf064e7 ("btrfs: nowait aio support")
    CC: stable@vger.kernel.org # 4.14+
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    260a6339
file.c 93.7 KB