An easy way to increase performance of file writes, is for the OS to just cache the data, tell (lie to) the application the write went through, and then actually do the write later. This is especially useful if there's other disk activity going on at the same time: the OS can prioritize reads and do the writes later. It can also remove the need for an actual write completely, e.g., in the case ofwhere a temporary file that is removed quickly afterwards.
I said, "is supposed to", since the drive itself might maketell the same lies to the OS, and tellsay that the write is complete, while it'sthe file really only exists in a volatile write cache within the drive. Depending on the drive, there might be no way around that.
In addition to fsync(), there are also the sync() and syncfs() system calls that ask the system to make sure all writes system-wide, writes or all writes on a particular filesystem have hit the disk. The utility sync can be used toto call those.
How a filesystem deals with metadata changes and the ordering between metadata and data writes varies a lot. With eE.g., with ext4, if you set the mount flag data=journal, then all writes, – even data writes – go through the journal and should be rather safe. That also means they get written twice, so performance goes down. The default options try to order the writes so that the data is on the disk before the metadata is updated. Other options or other filesystem may be better or worse,worse; I won't even try a comprehensive study.