Skip to content

Commit 2cbd51f

Browse files
johnpgarryaxboe
authored andcommitted
block: Don't allow an atomic write be truncated in blkdev_write_iter()
A write which goes past the end of the bdev in blkdev_write_iter() will be truncated. Truncating cannot tolerated for an atomic write, so error that condition. Fixes: caf336f ("block: Add fops atomic write support") Signed-off-by: John Garry <john.g.garry@oracle.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20241127092318.632790-1-john.g.garry@oracle.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 1b0cab3 commit 2cbd51f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

block/fops.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
677677
struct file *file = iocb->ki_filp;
678678
struct inode *bd_inode = bdev_file_inode(file);
679679
struct block_device *bdev = I_BDEV(bd_inode);
680+
bool atomic = iocb->ki_flags & IOCB_ATOMIC;
680681
loff_t size = bdev_nr_bytes(bdev);
681682
size_t shorted = 0;
682683
ssize_t ret;
@@ -696,14 +697,16 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
696697
if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
697698
return -EOPNOTSUPP;
698699

699-
if (iocb->ki_flags & IOCB_ATOMIC) {
700+
if (atomic) {
700701
ret = generic_atomic_write_valid(iocb, from);
701702
if (ret)
702703
return ret;
703704
}
704705

705706
size -= iocb->ki_pos;
706707
if (iov_iter_count(from) > size) {
708+
if (atomic)
709+
return -EINVAL;
707710
shorted = iov_iter_count(from) - size;
708711
iov_iter_truncate(from, size);
709712
}

0 commit comments

Comments
 (0)