• Filipe Manana's avatar
    btrfs: fix lost file sync on direct IO write with nowait and dsync iocb · 8184620a
    Filipe Manana authored
    When doing a direct IO write using a iocb with nowait and dsync set, we
    end up not syncing the file once the write completes.
    
    This is because we tell iomap to not call generic_write_sync(), which
    would result in calling btrfs_sync_file(), in order to avoid a deadlock
    since iomap can call it while we are holding the inode's lock and
    btrfs_sync_file() needs to acquire the inode's lock. The deadlock happens
    only if the write happens synchronously, when iomap_dio_rw() calls
    iomap_dio_complete() before it returns. Instead we do the sync ourselves
    at btrfs_do_write_iter().
    
    For a nowait write however we can end up not doing the sync ourselves at
    at btrfs_do_write_iter() because the write could have been queued, and
    therefore we get -EIOCBQUEUED returned from iomap in such case. That makes
    us skip the sync call at btrfs_do_write_iter(), as we don't do it for
    any error returned from btrfs_direct_write(). We can't simply do the call
    even if -EIOCBQUEUED is returned, since that would block the task waiting
    for IO, both for the data since there are bios still in progress as well
    as potentially blocking when joining a log transaction and when syncing
    the log (writing log trees, super blocks, etc).
    
    So let iomap do the sync call itself and in order to avoid deadlocks for
    the case of synchronous writes (without nowait), use __iomap_dio_rw() and
    have ourselves call iomap_dio_complete() after unlocking the inode.
    
    A test case will later be sent for fstests, after this is fixed in Linus'
    tree.
    
    Fixes: 51bd9563 ("btrfs: fix deadlock due to page faults during direct IO reads and writes")
    Reported-by: default avatarМарк Коренберг <socketpair@gmail.com>
    Link: https://lore.kernel.org/linux-btrfs/CAEmTpZGRKbzc16fWPvxbr6AfFsQoLmz-Lcg-7OgJOZDboJ+SGQ@mail.gmail.com/
    CC: stable@vger.kernel.org # 6.0+
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    8184620a
file.c 114 KB