0

The block size for a SSD Persistent Disk appears to be 4096 bytes.

If I write aligned blocks of 4096 bytes directly to the block device (/dev/sdb with O_DIRECT and posix_memalign), are these writes going to be atomic?

What I want is for the write call to either successfully write the whole 4K block or not write anything.

Can I end up with 100 bytes from the new write and the rest being old data?

1

Check out O_ATOMIC which works in conjuction with O_DIRECT to accomplish that. See https://lwn.net/Articles/573092/.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.