[Devel] [PATCH rh9 v2 1/4] dm-qcow2: check if files are opened with O_DIRECT

Konstantin Khorenko khorenko at virtuozzo.com
Wed Oct 12 18:18:55 MSK 2022


From: Alexander Atanasov <alexander.atanasov at virtuozzo.com>

Check that passed file descriptors are using O_DIRECT.
Required to avoid data integrity problems caused by
the page cache.

+ add error messages

https://jira.sw.ru/browse/PSBM-142050
Signed-off-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
---
 drivers/md/dm-qcow2-target.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index 9a6f970343ec..f646992a6040 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -672,16 +672,28 @@ static int qcow2_attach_file(struct dm_target *ti, struct qcow2_target *tgt,
 	fmode_t mode;
 
 	file = qcow2->file = fget(fd);
-	if (!file) /* In case of further errors, cleanup is made by caller */
+	/* In case of further errors, cleanup is made by caller */
+	if (!file) {
+		ti->error = "No image file set";
 		return -ENOENT;
+	}
+
+	if (!S_ISREG(file_inode(file)->i_mode)) {
+		ti->error = "Image file is not a regular file";
+		return -EINVAL;
+	}
 
-	if (!S_ISREG(file_inode(file)->i_mode))
+	if (!(file->f_flags & O_DIRECT)) {
+		ti->error = "Image file is opened in cached mode";
 		return -EINVAL;
+	}
 
 	mode = tgt->top != qcow2 ? FMODE_READ : dm_table_get_mode(ti->table);
 	mode &= (FMODE_READ|FMODE_WRITE);
-	if (mode & ~(file->f_mode & (FMODE_READ|FMODE_WRITE)))
+	if (mode & ~(file->f_mode & (FMODE_READ|FMODE_WRITE))) {
+		ti->error = "Image file is opened in a wrong mode";
 		return -EACCES;
+	}
 
 	return 0;
 }
@@ -808,7 +820,8 @@ static int qcow2_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
 		ret = qcow2_attach_file(ti, tgt, qcow2, fd);
 		if (ret) {
-			ti->error = "Error attaching file";
+			if (!ti->error)
+				ti->error = "Error attaching file";
 			goto err;
 		}
 
-- 
2.31.1



More information about the Devel mailing list