writeback_cache_control.txt 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. Explicit volatile write back cache control
  2. =====================================
  3. Introduction
  4. ------------
  5. Many storage devices, especially in the consumer market, come with volatile
  6. write back caches. That means the devices signal I/O completion to the
  7. operating system before data actually has hit the non-volatile storage. This
  8. behavior obviously speeds up various workloads, but it means the operating
  9. system needs to force data out to the non-volatile storage when it performs
  10. a data integrity operation like fsync, sync or an unmount.
  11. The Linux block layer provides two simple mechanisms that let filesystems
  12. control the caching behavior of the storage device. These mechanisms are
  13. a forced cache flush, and the Force Unit Access (FUA) flag for requests.
  14. Explicit cache flushes
  15. ----------------------
  16. The REQ_FLUSH flag can be OR ed into the r/w flags of a bio submitted from
  17. the filesystem and will make sure the volatile cache of the storage device
  18. has been flushed before the actual I/O operation is started. This explicitly
  19. guarantees that previously completed write requests are on non-volatile
  20. storage before the flagged bio starts. In addition the REQ_FLUSH flag can be
  21. set on an otherwise empty bio structure, which causes only an explicit cache
  22. flush without any dependent I/O. It is recommend to use
  23. the blkdev_issue_flush() helper for a pure cache flush.
  24. Forced Unit Access
  25. -----------------
  26. The REQ_FUA flag can be OR ed into the r/w flags of a bio submitted from the
  27. filesystem and will make sure that I/O completion for this request is only
  28. signaled after the data has been committed to non-volatile storage.
  29. Implementation details for filesystems
  30. --------------------------------------
  31. Filesystems can simply set the REQ_FLUSH and REQ_FUA bits and do not have to
  32. worry if the underlying devices need any explicit cache flushing and how
  33. the Forced Unit Access is implemented. The REQ_FLUSH and REQ_FUA flags
  34. may both be set on a single bio.
  35. Implementation details for make_request_fn based block drivers
  36. --------------------------------------------------------------
  37. These drivers will always see the REQ_FLUSH and REQ_FUA bits as they sit
  38. directly below the submit_bio interface. For remapping drivers the REQ_FUA
  39. bits need to be propagated to underlying devices, and a global flush needs
  40. to be implemented for bios with the REQ_FLUSH bit set. For real device
  41. drivers that do not have a volatile cache the REQ_FLUSH and REQ_FUA bits
  42. on non-empty bios can simply be ignored, and REQ_FLUSH requests without
  43. data can be completed successfully without doing any work. Drivers for
  44. devices with volatile caches need to implement the support for these
  45. flags themselves without any help from the block layer.
  46. Implementation details for request_fn based block drivers
  47. --------------------------------------------------------------
  48. For devices that do not support volatile write caches there is no driver
  49. support required, the block layer completes empty REQ_FLUSH requests before
  50. entering the driver and strips off the REQ_FLUSH and REQ_FUA bits from
  51. requests that have a payload. For devices with volatile write caches the
  52. driver needs to tell the block layer that it supports flushing caches by
  53. doing:
  54. blk_queue_flush(sdkp->disk->queue, REQ_FLUSH);
  55. and handle empty REQ_FLUSH requests in its prep_fn/request_fn. Note that
  56. REQ_FLUSH requests with a payload are automatically turned into a sequence
  57. of an empty REQ_FLUSH request followed by the actual write by the block
  58. layer. For devices that also support the FUA bit the block layer needs
  59. to be told to pass through the REQ_FUA bit using:
  60. blk_queue_flush(sdkp->disk->queue, REQ_FLUSH | REQ_FUA);
  61. and the driver must handle write requests that have the REQ_FUA bit set
  62. in prep_fn/request_fn. If the FUA bit is not natively supported the block
  63. layer turns it into an empty REQ_FLUSH request after the actual write.