This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Synchronous I/O on sysfs files

Hi,

I'd like to guarantee that what I write to a sysfs file (specifically the /sys/class/gpio/export file) is sync'd to the actual file. The code I had initially opened the file with the `O_SYNC` flag, which I assumed did this. However, in another piece of code, I tried using `fsync()`, but it failed with EINVAL, and `man fsync` tells me:

EROFS, EINVAL
fd is bound to a special file which does not support synchronization

I've checked the code for possible operations on a sysfs file, and did not find any sort of `do_sync_write` or `do_fsync` functions.

So, does the `O_SYNC` flag have any effect when opening a sysfs file? Shouldn't open return an error code when trying to open a file that does not support sync read/write with O_SYNC?

Regards,

Guilherme

  • Hi Guilherme,

    According to the manual the 'O_SYNC' flag provides synchronized I/O file integrity completion, meaning write operations will flush data and all associated metadata to the underlying hardware. But 'O_SYNC' guarantees that the call will not return before all data has been transferred to the disk (as far as the OS can tell). This still does not guarantee that the data isn't somewhere in the harddisk write cache, but it is as much as the OS can guarantee.
    On the other hand the 'O_DIRECT' alone only promises that the kernel will avoid copying data from user space to kernel space, and will instead write it directly via DMA (Direct memory access; if possible). Data does not go into caches. There is no strict guarantee that the function will return only after all data has been transferred.
    Therefore I suggest you to use both flags together (O_DIRECT | O_SYNC). This combination makes "DMA + guarantee".

    BR
    Tsvetolin Shulev
  • Cvetolin,

    I've checked the sysfs documentation and found this:

    - On write(2), sysfs expects the entire buffer to be passed during the
    first write. Sysfs then passes the entire buffer to the store() method.
    A terminating null is added after the data on stores.

    With the lack of specialized sync functions, I believe that the O_SYNC flag has no effect on sysfs files, and all operations will follow the rule shown above. Is that so?

    Regards,
    Guilherme