• Filipe Manana's avatar
    btrfs: remove racy and unnecessary inode transaction update when using no-holes · cceaa89f
    Filipe Manana authored
    When using the NO_HOLES feature and expanding the size of an inode, we
    update the inode's last_trans, last_sub_trans and last_log_commit fields
    at maybe_insert_hole() so that a fsync does know that the inode needs to
    be logged (by making sure that btrfs_inode_in_log() returns false). This
    happens for expanding truncate operations, buffered writes, direct IO
    writes and when cloning extents to an offset greater than the inode's
    i_size.
    
    However the way we do it is racy, because in between setting the inode's
    last_sub_trans and last_log_commit fields, the log transaction ID that was
    assigned to last_sub_trans might be committed before we read the root's
    last_log_commit and assign that value to last_log_commit. If that happens
    it would make a future call to btrfs_inode_in_log() return true. This is
    a race that should be extremely unlikely to be hit in practice, and it is
    the same that was described by commit bc0939fc ("btrfs: fix race
    between marking inode needs to be logged and log syncing").
    
    The fix would simply be to set last_log_commit to the value we assigned
    to last_sub_trans minus 1, like it was done in that commit. However
    updating these two fields plus the last_trans field is pointless here
    because all the callers of btrfs_cont_expand() (which is the only
    caller of maybe_insert_hole()) always call btrfs_set_inode_last_trans()
    or btrfs_update_inode() after calling btrfs_cont_expand(). Calling either
    btrfs_set_inode_last_trans() or btrfs_update_inode() guarantees that the
    next fsync will log the inode, as it makes btrfs_inode_in_log() return
    false.
    
    So just remove the code that explicitly sets the inode's last_trans,
    last_sub_trans and last_log_commit fields.
    Reviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    cceaa89f
inode.c 303 KB