Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fsync(fd) work on a file created by external program?

I have a SATA hard disk with write cache disabled:

hdparm -W0 /dev/foo

I am operating on an ext4 partition with these mount options (amongst others):

data=ordered
auto_da_alloc

Linux kernel version is 2.6.32-5-686.

Now, I have an external program that I cannot modify, but that I know creates a file in the following way:

int fd = open(path);
write(fd, data, data_size);
close(fd);

I.e. it does not fsync before closing. So at this point, the data may probably be in RAM, somewhere in kernel/fs caches.

Note: the metadata is not a concern yet: the final metadata will be written and fsynced after I've made sure the data has reached the disk platters. The data itself is the concern.

So the question is, how can I help the data reach the actual disk platters?

I have thought of running this separate program afterwards:

int fd = open(path);
fsync(fd);
close(fd);

Will that help flush the data, or should I use a different approach?

like image 923
STenyaK Avatar asked Nov 10 '22 17:11

STenyaK


1 Answers

Will that help flush the data,

Yes it will, it doesn't matter who does the fsync.

Note that you likely want to fsync the directory that the file resides in too, in order to sync the metadata of the file.

like image 159
nos Avatar answered Nov 15 '22 06:11

nos