diff --git a/block/fops.c b/block/fops.c index 07f4d7336a76..a1da6d428ca3 100644 --- a/block/fops.c +++ b/block/fops.c @@ -714,12 +714,22 @@ blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from) { + struct inode *inode = bdev_file_inode(iocb->ki_filp); + ssize_t ret; + /* Buffered fallback waits for writeback, which NOWAIT cannot allow. */ if (iocb->ki_flags & IOCB_NOWAIT) return -EAGAIN; - return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops, NULL, - NULL); + /* + * Take i_rwsem to avoid racing with set_blocksize() changing + * i_blkbits/folio order and truncating the page cache. + */ + inode_lock_shared(inode); + ret = iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops, NULL, + NULL); + inode_unlock_shared(inode); + return ret; } /* @@ -808,14 +818,7 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) need_sync = true; } } else { - /* - * Take i_rwsem and invalidate_lock to avoid racing with - * set_blocksize changing i_blkbits/folio order and punching - * out the pagecache. - */ - inode_lock_shared(bd_inode); ret = blkdev_buffered_write(iocb, from); - inode_unlock_shared(bd_inode); need_sync = true; }