Skip to content

Commit

Permalink
DM: Add write sync in fwrite
Browse files Browse the repository at this point in the history
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 <yang.huang@intel.com>
Signed-off-by: duminx <minx.du@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
  • Loading branch information
yang8621 authored and lijinxia committed Jul 5, 2018
1 parent 96372ed commit 0621b24
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions devicemodel/hw/platform/rpmb/rpmb_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <openssl/hmac.h>
#include <openssl/opensslv.h>

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 0621b24

Please sign in to comment.