When a disk read or disk write occurs, where does the data go?
I understand that there is such a thing as a disk controller which contains a buffer, but was wondering if the CPU reads directly from this buffer, or whether the data must first go to a specific location in memory, and then allow itself to be read by the CPU?
Does anyone also know what the buffer is called? And how does DMA fit into all this?
Solution 1:
For a disk read the data flow is essentially:
- after the read/write assembly is at the requested cylinder, the requested r/w head is selected.
- at each sector, the sector ID is read. If the sector number that was read matches the requested sector number, then the sector data is read.
- the sector data is read as a serial bit stream, and converted to bytes.
- the bytes of the sector are stored in a sector buffer (usually SRAM in the controller); this sector buffer is distinct from the "disk cache".
- once the entire sector has been read, the data is validated using ECC, and perhaps corrected.
- once validated, the sector data is transferred from the controller to the host PC. Note: existence of this sector buffer is not well known, and there is widespread misinformation that the transfer speed on the host (e.g.ATA) interface is tied or limited by the bit rate at the R/W head. That is completely false, since these two data transfers are independent and sequential operations and not concurrent.
- as the PC receives the data from the disk controller through the ATA interface, the PC can use either programmed I/O (the CPU repeatedly reads the ATA port's data register and copies the value to the destination memory) or DMA (the DMA controller is setup to copy N bytes from the ATA port's data register to a memory buffer w/o further CPU intervention).
- the PC's memory "buffer" that receives the data could be the application's buffer (when using block I/O system call), or could be an internal system buffer under filesystem control, or even program or data memory if the data came from the swap area (or page file).
A write to disk is similar, except that the data is transferred from PC to disk controller, the ECC is calculated, the requested sector is located, and then the data is written from the sector buffer to the platter.
For extra credit:
Read up on "scatter-gather" transfers, which uses DMA chaining and a scatter-gather list of memory addresses and buffer lengths. Instead of one large, contiguous memory buffer, a "scatter-gather" transfer allows the use of non-contiguous memory buffers to be aggregated for the disk I/O request.
Solution 2:
It goes into a specific memory area that the kernel has allocated the disk's driver. I don't think it is specifically named. This memory area is usually configured as a ring buffer. Depending on the OS, this memory may be copied a second time into user space for the application to use.
DMA is "direct memory access" which means that the disk drive's controller can directly write to the RAM without having the CPU repeatedly ask it if more data is available (known as polling - the way things used to be done). Instead, when the entire buffer has been transferred, the controller then interrupts the CPU to let it know data is now available.
If you are curious about an even lower level, the driver makes ioctl calls.