There is no common agreement over whether single sector sized writes are atomic or not with respect to disk power loss - see https://stackoverflow.com/a/2015068/4513656 for discussion on this point. At least from a SCSI perspective, single LBA single sector atomicity is the expected behaviour (see http://www.t10.org/pipermail/t10/2011-November/016011.html ) but be aware that the Linux block layer can merge/split requests and deals with more than SCSI devices.
It's also worth noting that for SCSI devices there are explicit T10 commands (e.g. WRITE ATOMIC
) that explicitly guarantee atomicity but devices don't have to implement them and Linux doesn't expose them through regular block device operations. The patches for the O_ATOMIC
flag referenced in another answer were not in a mainline kernel at the time of writing (February 2017 kernel 4.10) and the flag isn't mentioned in the open(2) man page.
If the system/disk never crashes then yes, a single LBA write of a single sector should appear atomic with regard to a single LBA read of that same single sector.
Note that using O_DIRECT
does not imply the write made it to non-volatile storage so again after crash you may find acknowledged data is not there. You would need to use fsync
or O_SYNC
for further stability guarantees even though you're using a block device.