[PATCH RFC 1/4] block: Make bdev_can_atomic_write() robust against mis-aligned bdev size

John Garry
2024-09-03T15:08:44Z

For bdev file operations, a write will be truncated when trying to write
past the end of the device. This could not be tolerated for an atomic
write.

Ensure that the size of the bdev matches max atomic write unit so that this
truncation would never occur.

Fixes: 9da3d1e912f3 ("block: Add core atomic write support")
Signed-off-by: John Garry <john.g.garry@oracle.com>
---
include/linux/blkdev.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index b7664d593486..af8434e391fa 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1662,6 +1662,9 @@ static inline bool bdev_can_atomic_write(struct block_device *bdev)
if (!limits->atomic_write_unit_min)
return false;

+ if (!IS_ALIGNED(bdev_nr_bytes(bdev), limits->atomic_write_unit_max))
+ return false;
+
if (bdev_is_partition(bdev)) {
sector_t bd_start_sect = bdev->bd_start_sect;
unsigned int alignment =
--
2.31.1