dm-bow.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. dm_bow (backup on write)
  2. ========================
  3. dm_bow is a device mapper driver that uses the free space on a device to back up
  4. data that is overwritten. The changes can then be committed by a simple state
  5. change, or rolled back by removing the dm_bow device and running a command line
  6. utility over the underlying device.
  7. dm_bow has three states, set by writing ‘1’ or ‘2’ to /sys/block/dm-?/bow/state.
  8. It is only possible to go from state 0 (initial state) to state 1, and then from
  9. state 1 to state 2.
  10. State 0: dm_bow collects all trims to the device and assumes that these mark
  11. free space on the overlying file system that can be safely used. Typically the
  12. mount code would create the dm_bow device, mount the file system, call the
  13. FITRIM ioctl on the file system then switch to state 1. These trims are not
  14. propagated to the underlying device.
  15. TODO: There are some race conditions if there are writes in state 0. Test
  16. mounting the drive ro and see if trims are still allowed. If that fails, test
  17. holding all writes in a queue until we switch to state 1. If that fails,
  18. consider implementing a ram disk type affair, which will be complex and risky.
  19. State 1: All writes to the device cause the underlying data to be backed up to
  20. the free (trimmed) area as needed in such a way as they can be restored.
  21. However, the writes, with one exception, then happen exactly as they would
  22. without dm_bow, so the device is always in a good final state. The exception is
  23. that sector 0 is used to keep a log of the latest changes, both to indicate that
  24. we are in this state and to allow rollback. See below for all details.
  25. State 2: The transition to state 2 triggers replacing the special sector 0 with
  26. the normal sector 0, and the freeing of all state information. dm_bow then
  27. becomes a pass-through driver, allowing the device to continue to be used with
  28. minimal performance impact.
  29. Usage
  30. =====
  31. dm-bow takes one command line parameter, the name of the underlying device.
  32. dm-bow will typically be used in the following way. dm-bow will be loaded with a
  33. suitable underlying device and the resultant device will be mounted. A file
  34. system trim will be issued via the FITRIM ioctl, then the device will be
  35. switched to state 1. The file system will now be used as normal. At some point,
  36. the changes can either be committed by switching to state 2, or rolled back by
  37. unmounting the file system, removing the dm-bow device and running the command
  38. line utility. Note that rebooting the device will be equivalent to unmounting
  39. and removing, but the command line utility must still be run
  40. Details of operation in state 1
  41. ===============================
  42. dm_bow maintains a type for all sectors. A sector can be any of:
  43. SECTOR0
  44. SECTOR0_CURRENT
  45. UNCHANGED
  46. FREE
  47. CHANGED
  48. BACKUP
  49. SECTOR0 is the first sector on the device, and is used to hold the log of
  50. changes. This is the one exception.
  51. SECTOR0_CURRENT is a sector picked from the FREE sectors, and is where reads and
  52. writes from the true sector zero are redirected to. Note that like any backup
  53. sector, if the sector is written to directly, it must be moved again.
  54. UNCHANGED means that the sector has not been changed since we entered state 1.
  55. Thus if it is written to or trimmed, the contents must first be backed up.
  56. FREE means that the sector was trimmed in state 0 and has not yet been written
  57. to or used for backup. On being written to, a FREE sector is changed to CHANGED.
  58. CHANGED means that the sector has been modified, and can be further modified
  59. without further backup.
  60. BACKUP means that this is a free sector being used as a backup. On being written
  61. to, the contents must first be backed up again.
  62. All backup operations are logged to the first sector. The log sector has the
  63. format:
  64. --------------------------------------------------------
  65. | Magic | Count | Sequence | Log entry | Log entry | …
  66. --------------------------------------------------------
  67. Magic is a magic number. Count is the number of log entries. Sequence is 0
  68. initially. A log entry is
  69. -----------------------------------
  70. | Source | Dest | Size | Checksum |
  71. -----------------------------------
  72. When SECTOR0 is full, the log sector is backup up and another empty log sector
  73. created with sequence number one higher. The first entry in any log entry with
  74. sequence > 0 therefore must be the log of the backing up of the previous log
  75. sector. Note that sequence is not strictly needed, but is a useful sanity check
  76. and potentially limits the time spent trying to restore a corrupted snapshot.
  77. On entering state 1, dm_bow has a list of free sectors. All other sectors are
  78. unchanged. Sector0_current is selected from the free sectors and the contents of
  79. sector 0 are copied there. The sector 0 is backed up, which triggers the first
  80. log entry to be written.