Path: blob/master/Documentation/block/writeback_cache_control.txt
10821 views
1Explicit volatile write back cache control2=====================================34Introduction5------------67Many storage devices, especially in the consumer market, come with volatile8write back caches. That means the devices signal I/O completion to the9operating system before data actually has hit the non-volatile storage. This10behavior obviously speeds up various workloads, but it means the operating11system needs to force data out to the non-volatile storage when it performs12a data integrity operation like fsync, sync or an unmount.1314The Linux block layer provides two simple mechanisms that let filesystems15control the caching behavior of the storage device. These mechanisms are16a forced cache flush, and the Force Unit Access (FUA) flag for requests.171819Explicit cache flushes20----------------------2122The REQ_FLUSH flag can be OR ed into the r/w flags of a bio submitted from23the filesystem and will make sure the volatile cache of the storage device24has been flushed before the actual I/O operation is started. This explicitly25guarantees that previously completed write requests are on non-volatile26storage before the flagged bio starts. In addition the REQ_FLUSH flag can be27set on an otherwise empty bio structure, which causes only an explicit cache28flush without any dependent I/O. It is recommend to use29the blkdev_issue_flush() helper for a pure cache flush.303132Forced Unit Access33-----------------3435The REQ_FUA flag can be OR ed into the r/w flags of a bio submitted from the36filesystem and will make sure that I/O completion for this request is only37signaled after the data has been committed to non-volatile storage.383940Implementation details for filesystems41--------------------------------------4243Filesystems can simply set the REQ_FLUSH and REQ_FUA bits and do not have to44worry if the underlying devices need any explicit cache flushing and how45the Forced Unit Access is implemented. The REQ_FLUSH and REQ_FUA flags46may both be set on a single bio.474849Implementation details for make_request_fn based block drivers50--------------------------------------------------------------5152These drivers will always see the REQ_FLUSH and REQ_FUA bits as they sit53directly below the submit_bio interface. For remapping drivers the REQ_FUA54bits need to be propagated to underlying devices, and a global flush needs55to be implemented for bios with the REQ_FLUSH bit set. For real device56drivers that do not have a volatile cache the REQ_FLUSH and REQ_FUA bits57on non-empty bios can simply be ignored, and REQ_FLUSH requests without58data can be completed successfully without doing any work. Drivers for59devices with volatile caches need to implement the support for these60flags themselves without any help from the block layer.616263Implementation details for request_fn based block drivers64--------------------------------------------------------------6566For devices that do not support volatile write caches there is no driver67support required, the block layer completes empty REQ_FLUSH requests before68entering the driver and strips off the REQ_FLUSH and REQ_FUA bits from69requests that have a payload. For devices with volatile write caches the70driver needs to tell the block layer that it supports flushing caches by71doing:7273blk_queue_flush(sdkp->disk->queue, REQ_FLUSH);7475and handle empty REQ_FLUSH requests in its prep_fn/request_fn. Note that76REQ_FLUSH requests with a payload are automatically turned into a sequence77of an empty REQ_FLUSH request followed by the actual write by the block78layer. For devices that also support the FUA bit the block layer needs79to be told to pass through the REQ_FUA bit using:8081blk_queue_flush(sdkp->disk->queue, REQ_FLUSH | REQ_FUA);8283and the driver must handle write requests that have the REQ_FUA bit set84in prep_fn/request_fn. If the FUA bit is not natively supported the block85layer turns it into an empty REQ_FLUSH request after the actual write.868788