writeback_cache_control.rst 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ==========================================
  2. Explicit volatile write back cache control
  3. ==========================================
  4. Introduction
  5. ------------
  6. Many storage devices, especially in the consumer market, come with volatile
  7. write back caches. That means the devices signal I/O completion to the
  8. operating system before data actually has hit the non-volatile storage. This
  9. behavior obviously speeds up various workloads, but it means the operating
  10. system needs to force data out to the non-volatile storage when it performs
  11. a data integrity operation like fsync, sync or an unmount.
  12. The Linux block layer provides two simple mechanisms that let filesystems
  13. control the caching behavior of the storage device. These mechanisms are
  14. a forced cache flush, and the Force Unit Access (FUA) flag for requests.
  15. Explicit cache flushes
  16. ----------------------
  17. The REQ_PREFLUSH flag can be OR ed into the r/w flags of a bio submitted from
  18. the filesystem and will make sure the volatile cache of the storage device
  19. has been flushed before the actual I/O operation is started. This explicitly
  20. guarantees that previously completed write requests are on non-volatile
  21. storage before the flagged bio starts. In addition the REQ_PREFLUSH flag can be
  22. set on an otherwise empty bio structure, which causes only an explicit cache
  23. flush without any dependent I/O. It is recommend to use
  24. the blkdev_issue_flush() helper for a pure cache flush.
  25. Forced Unit Access
  26. ------------------
  27. The REQ_FUA flag can be OR ed into the r/w flags of a bio submitted from the
  28. filesystem and will make sure that I/O completion for this request is only
  29. signaled after the data has been committed to non-volatile storage.
  30. Implementation details for filesystems
  31. --------------------------------------
  32. Filesystems can simply set the REQ_PREFLUSH and REQ_FUA bits and do not have to
  33. worry if the underlying devices need any explicit cache flushing and how
  34. the Forced Unit Access is implemented. The REQ_PREFLUSH and REQ_FUA flags
  35. may both be set on a single bio.
  36. Feature settings for block drivers
  37. ----------------------------------
  38. For devices that do not support volatile write caches there is no driver
  39. support required, the block layer completes empty REQ_PREFLUSH requests before
  40. entering the driver and strips off the REQ_PREFLUSH and REQ_FUA bits from
  41. requests that have a payload.
  42. For devices with volatile write caches the driver needs to tell the block layer
  43. that it supports flushing caches by setting the
  44. BLK_FEAT_WRITE_CACHE
  45. flag in the queue_limits feature field. For devices that also support the FUA
  46. bit the block layer needs to be told to pass on the REQ_FUA bit by also setting
  47. the
  48. BLK_FEAT_FUA
  49. flag in the features field of the queue_limits structure.
  50. Implementation details for bio based block drivers
  51. --------------------------------------------------
  52. For bio based drivers the REQ_PREFLUSH and REQ_FUA bit are simply passed on to
  53. the driver if the driver sets the BLK_FEAT_WRITE_CACHE flag and the driver
  54. needs to handle them.
  55. *NOTE*: The REQ_FUA bit also gets passed on when the BLK_FEAT_FUA flags is
  56. _not_ set. Any bio based driver that sets BLK_FEAT_WRITE_CACHE also needs to
  57. handle REQ_FUA.
  58. For remapping drivers the REQ_FUA bits need to be propagated to underlying
  59. devices, and a global flush needs to be implemented for bios with the
  60. REQ_PREFLUSH bit set.
  61. Implementation details for blk-mq drivers
  62. -----------------------------------------
  63. When the BLK_FEAT_WRITE_CACHE flag is set, REQ_OP_WRITE | REQ_PREFLUSH requests
  64. with a payload are automatically turned into a sequence of a REQ_OP_FLUSH
  65. request followed by the actual write by the block layer.
  66. When the BLK_FEAT_FUA flags is set, the REQ_FUA bit is simply passed on for the
  67. REQ_OP_WRITE request, else a REQ_OP_FLUSH request is sent by the block layer
  68. after the completion of the write request for bio submissions with the REQ_FUA
  69. bit set.