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 <yang.huang@intel.com>
Signed-off-by: duminx <minx.du@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Huang Yang 2018-06-28 02:55:39 -04:00 committed by lijinxia
parent 96372ed09c
commit 0621b24819

View File

@ -29,6 +29,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <openssl/hmac.h>
#include <openssl/opensslv.h>
@ -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;
}