log-writes.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. =============
  2. dm-log-writes
  3. =============
  4. This target takes 2 devices, one to pass all IO to normally, and one to log all
  5. of the write operations to. This is intended for file system developers wishing
  6. to verify the integrity of metadata or data as the file system is written to.
  7. There is a log_write_entry written for every WRITE request and the target is
  8. able to take arbitrary data from userspace to insert into the log. The data
  9. that is in the WRITE requests is copied into the log to make the replay happen
  10. exactly as it happened originally.
  11. Log Ordering
  12. ============
  13. We log things in order of completion once we are sure the write is no longer in
  14. cache. This means that normal WRITE requests are not actually logged until the
  15. next REQ_PREFLUSH request. This is to make it easier for userspace to replay
  16. the log in a way that correlates to what is on disk and not what is in cache,
  17. to make it easier to detect improper waiting/flushing.
  18. This works by attaching all WRITE requests to a list once the write completes.
  19. Once we see a REQ_PREFLUSH request we splice this list onto the request and once
  20. the FLUSH request completes we log all of the WRITEs and then the FLUSH. Only
  21. completed WRITEs, at the time the REQ_PREFLUSH is issued, are added in order to
  22. simulate the worst case scenario with regard to power failures. Consider the
  23. following example (W means write, C means complete):
  24. W1,W2,W3,C3,C2,Wflush,C1,Cflush
  25. The log would show the following:
  26. W3,W2,flush,W1....
  27. Again this is to simulate what is actually on disk, this allows us to detect
  28. cases where a power failure at a particular point in time would create an
  29. inconsistent file system.
  30. Any REQ_FUA requests bypass this flushing mechanism and are logged as soon as
  31. they complete as those requests will obviously bypass the device cache.
  32. Any REQ_OP_DISCARD requests are treated like WRITE requests. Otherwise we would
  33. have all the DISCARD requests, and then the WRITE requests and then the FLUSH
  34. request. Consider the following example:
  35. WRITE block 1, DISCARD block 1, FLUSH
  36. If we logged DISCARD when it completed, the replay would look like this:
  37. DISCARD 1, WRITE 1, FLUSH
  38. which isn't quite what happened and wouldn't be caught during the log replay.
  39. Target interface
  40. ================
  41. i) Constructor
  42. log-writes <dev_path> <log_dev_path>
  43. ============= ==============================================
  44. dev_path Device that all of the IO will go to normally.
  45. log_dev_path Device where the log entries are written to.
  46. ============= ==============================================
  47. ii) Status
  48. <#logged entries> <highest allocated sector>
  49. =========================== ========================
  50. #logged entries Number of logged entries
  51. highest allocated sector Highest allocated sector
  52. =========================== ========================
  53. iii) Messages
  54. mark <description>
  55. You can use a dmsetup message to set an arbitrary mark in a log.
  56. For example say you want to fsck a file system after every
  57. write, but first you need to replay up to the mkfs to make sure
  58. we're fsck'ing something reasonable, you would do something like
  59. this::
  60. mkfs.btrfs -f /dev/mapper/log
  61. dmsetup message log 0 mark mkfs
  62. <run test>
  63. This would allow you to replay the log up to the mkfs mark and
  64. then replay from that point on doing the fsck check in the
  65. interval that you want.
  66. Every log has a mark at the end labeled "dm-log-writes-end".
  67. Userspace component
  68. ===================
  69. There is a userspace tool that will replay the log for you in various ways.
  70. It can be found here: https://github.com/josefbacik/log-writes
  71. Example usage
  72. =============
  73. Say you want to test fsync on your file system. You would do something like
  74. this::
  75. TABLE="0 $(blockdev --getsz /dev/sdb) log-writes /dev/sdb /dev/sdc"
  76. dmsetup create log --table "$TABLE"
  77. mkfs.btrfs -f /dev/mapper/log
  78. dmsetup message log 0 mark mkfs
  79. mount /dev/mapper/log /mnt/btrfs-test
  80. <some test that does fsync at the end>
  81. dmsetup message log 0 mark fsync
  82. md5sum /mnt/btrfs-test/foo
  83. umount /mnt/btrfs-test
  84. dmsetup remove log
  85. replay-log --log /dev/sdc --replay /dev/sdb --end-mark fsync
  86. mount /dev/sdb /mnt/btrfs-test
  87. md5sum /mnt/btrfs-test/foo
  88. <verify md5sum's are correct>
  89. Another option is to do a complicated file system operation and verify the file
  90. system is consistent during the entire operation. You could do this with:
  91. TABLE="0 $(blockdev --getsz /dev/sdb) log-writes /dev/sdb /dev/sdc"
  92. dmsetup create log --table "$TABLE"
  93. mkfs.btrfs -f /dev/mapper/log
  94. dmsetup message log 0 mark mkfs
  95. mount /dev/mapper/log /mnt/btrfs-test
  96. <fsstress to dirty the fs>
  97. btrfs filesystem balance /mnt/btrfs-test
  98. umount /mnt/btrfs-test
  99. dmsetup remove log
  100. replay-log --log /dev/sdc --replay /dev/sdb --end-mark mkfs
  101. btrfsck /dev/sdb
  102. replay-log --log /dev/sdc --replay /dev/sdb --start-mark mkfs \
  103. --fsck "btrfsck /dev/sdb" --check fua
  104. And that will replay the log until it sees a FUA request, run the fsck command
  105. and if the fsck passes it will replay to the next FUA, until it is completed or
  106. the fsck command exists abnormally.