Reputation: 522
As I understand it, more or less all systems do write to disk as an atomic write of the sector (usually 512 or 4096 bytes). But how about reading?
I.e. if I have a process which reads a file sequentially using read(2) command, is there a chance that the sector is overwritten before I have finished reading the whole sector, or is the whole sector copied to memory in an atomic operation?
Lets take a toy example; I have a producer which has mmap
:ed a file. The task of this producer is to write the letter "A" somewhere in the first sector, and then move it to another location, still in the first sector. Since the producer does not know at which time the memory is written to disk it makes sure to always write the "A" in the new address before it removes it from the old address. Thus when looking at the file on disk there might exist 2 "A"'s, but never less than 1. But consider the case where a read of one sector is not atomic (e.g. when reading using another mmap
with MAP_SHARED
?), then it might happen that when the consumer process starts to read the file, the "A" is in the second half of the sector, but when the consumer process gets to the second part of the sector a new (atomic) write of the sector happens where the "A" moves to the first half of the sector. If the consumer continues reading the second half of the sector it will not see any "A" anywhere in the sector. Is this still a risk when using read(2)
?
Upvotes: 1
Views: 229
Reputation: 123690
Realistically, NAND flash, hard disks and CD-ROMs will be sector read atomic because the hardware is unable to read and write from the same block at the same time. Additionally, they store checksums per sector, so any torn write would be discarded as a read error.
However, none of this matters in your case because read/write/mmap
operate on the VFS and not directly on the device. When you do a read
from a mmap
'd page, you get a simple non-atomic copy of that page, and it may indeed show no "A"s.
Upvotes: 3