← Back to context

Comment by supermatt

4 years ago

I have said a few times already - F_BARRIERFSYNC. This is likely equivalent to what linux is doing.

edit: sorry - not 'standards compliant' (whatever that is - does linux declare support for SIO?), but probably what you are looking for.

It isn't. I already replied to you above. A barrier does not guarantee data durability and we already know Linux fsync() == macOS F_FULLFSYNC because they have the same (lack of) performance on the same hardware.

  • I just looked into this, since what you say and what Apple’s documentation says are two different things.

    Here is Apple’s documentation:

    https://devstreaming-cdn.apple.com/videos/wwdc/2019/419ef9ip...

    F_BARRIERFSYNC: fsync() with a barrier

    F_FULLFSYNC: Drive flush its cache to disk

    This sounds like the Linux fsync() and Linux syncfs() respectively. What you say is that F_FULLFSYNC is the same as Linux fsync() and your performance numbers back that up. Unfortunately, you would only see a difference between Linux fsync() and Linux syncfs() if you have files being asynchronously written at the same time as the files that are subject to fsync()/syncfs(). fsync() would only touch the chosen files while syncfs() would touch both. If you did not have heavy background file writes and F_FULLSYNC really is equivalent to syncfs(), you would not be able to tell the difference in your tests.

    That said, let’s look at how this actually works on Mac OS. Unfortunately, the apfs driver does not appear to be open source, but the HFS+ driver is. Here are the relevant pieces of code in HFS+:

    https://github.com/apple-oss-distributions/hfs/blob/hfs-556....

    https://github.com/apple-oss-distributions/hfs/blob/5e3008b6...

    First, let me start with saying this merits a faceplam. The fsync() operation is operating at the level of the mount point, not the individual file. F_FULLSYNC and F_BARRIERFSYNC are different, but they both might as well be variants of the Linux syncfs().

    For good measure, let us look at how this is done on the MacOS ZFS driver:

    https://github.com/openzfsonosx/zfs/blob/master/module/zfs/z...

    The file is properly synced independently of the mountpoint, such that other files being modified in the file are not immediately required to be written out to disk. That said, both F_FULLSYNC and F_BARRIERFSYNC on MacOS are mapped by the ZFS driver to the same function that implements fsync() on Linux:

    https://github.com/openzfs/zfs/blob/master/module/os/linux/z...

    For good measure, let us look at how syncfs() is implemented by ZFS on Linux:

    https://github.com/openzfs/zfs/blob/master/module/os/linux/z...

    It operates on the superblock, which is what MacOS’ HFS+ driver does.

    From this, I can conclude:

    Linux syncfs() == macOS F_FULLFSYNC on HFS+

    Linux fsync() == macOS fsync()/F_FULLFSYNC/F_BARRIERFSYNC on ZFS

    Also, MacOS F_BARRIERSYNC is a weakened Linux syncfs() and Apple’s documentation is very misleading (although maybe not technically wrong). POSIX does allow fsync to be implemented via syncfs (sync in POSIX, but I am saying syncfs from Linux to be less confusing). However, not issuing and waiting for the completion of an IO barrier on fsync is broken behavior like you claim.

    I am not sure how MacOS APFS behaves. I imagine that additional testing that takes into account the nuances in semantics would be able to clarify that. If it behaves like HFS+, it is broken.

    Edit: Upon further examination and comparing notes with the MacOS ZFS driver team lead, it seems that HFS+ is syncing more than requested when F_FULLFSYNC is used, but less than the entire filesystem. You are fine treating it as a Linux fsync. It is close enough.