Btrfs: fix wrong lock range and write size in check_can_nocow()
authorMiao Xie <miaox@cn.fujitsu.com>
Thu, 27 Feb 2014 05:58:04 +0000 (13:58 +0800)
committerJosef Bacik <jbacik@fb.com>
Mon, 10 Mar 2014 19:17:00 +0000 (15:17 -0400)
The write range may not be sector-aligned, for example:

       |--------|--------| <- write range, sector-unaligned, size: 2blocks
  |--------|--------|--------|  <- correct lock range, size: 3blocks

But according to the old code, we used the size of write range to calculate
the lock range directly, not considered the offset, we would get a wrong lock
range:

       |--------|--------| <- write range, sector-unaligned, size: 2blocks
  |--------|--------| <- wrong lock range, size: 2blocks

And besides that, the old code also had the same problem when calculating
the real write size. Correct them.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Josef Bacik <jbacik@fb.com>
fs/btrfs/file.c

index 31e48b9470607a834818eb9fbaa83e4ed013ac14..fc2d21b0a02259e1e41270a26b409f563cd1a79d 100644 (file)
@@ -1411,7 +1411,7 @@ static noinline int check_can_nocow(struct inode *inode, loff_t pos,
        int ret;
 
        lockstart = round_down(pos, root->sectorsize);
-       lockend = lockstart + round_up(*write_bytes, root->sectorsize) - 1;
+       lockend = round_up(pos + *write_bytes, root->sectorsize) - 1;
 
        while (1) {
                lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
@@ -1434,7 +1434,8 @@ static noinline int check_can_nocow(struct inode *inode, loff_t pos,
                                 EXTENT_DIRTY | EXTENT_DELALLOC |
                                 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
                                 NULL, GFP_NOFS);
-               *write_bytes = min_t(size_t, *write_bytes, num_bytes);
+               *write_bytes = min_t(size_t, *write_bytes ,
+                                    num_bytes - pos + lockstart);
        }
 
        unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);