From 0621b24819e71b07ad4d84d9e8de3b3f585ce692 Mon Sep 17 00:00:00 2001 From: Huang Yang Date: Thu, 28 Jun 2018 02:55:39 -0400 Subject: [PATCH] DM: Add write sync in fwrite An immediate reset or power down will cause a loss of write content. The cause is the data write to disk is at cache within a short time window before it's synced to storage media. An explicit fsync() forces to sync the data to storage to prevent the data loss of such immediate reset. Signed-off-by: Huang Yang Signed-off-by: duminx Acked-by: Eddie Dong --- devicemodel/hw/platform/rpmb/rpmb_sim.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/devicemodel/hw/platform/rpmb/rpmb_sim.c b/devicemodel/hw/platform/rpmb/rpmb_sim.c index 5c4f3e589..f8e0e4ae4 100644 --- a/devicemodel/hw/platform/rpmb/rpmb_sim.c +++ b/devicemodel/hw/platform/rpmb/rpmb_sim.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -168,10 +169,17 @@ static int file_write(FILE *fp, const void *buf, size_t size, off_t offset) return -1; } + /* The flow of file writing sync should be: + C lib caches--->fflush--->disk caches--->fsync--->disk */ if (fflush(fp) < 0) { return -1; } + if (fsync(fileno(fp)) < 0) { + DPRINTF(("%s: fsync failed\n", __func__)); + return -1; + } + return rc; }