146 lines · plain
1=============2dm-log-writes3=============4 5This target takes 2 devices, one to pass all IO to normally, and one to log all6of the write operations to. This is intended for file system developers wishing7to verify the integrity of metadata or data as the file system is written to.8There is a log_write_entry written for every WRITE request and the target is9able to take arbitrary data from userspace to insert into the log. The data10that is in the WRITE requests is copied into the log to make the replay happen11exactly as it happened originally.12 13Log Ordering14============15 16We log things in order of completion once we are sure the write is no longer in17cache. This means that normal WRITE requests are not actually logged until the18next REQ_PREFLUSH request. This is to make it easier for userspace to replay19the log in a way that correlates to what is on disk and not what is in cache,20to make it easier to detect improper waiting/flushing.21 22This works by attaching all WRITE requests to a list once the write completes.23Once we see a REQ_PREFLUSH request we splice this list onto the request and once24the FLUSH request completes we log all of the WRITEs and then the FLUSH. Only25completed WRITEs, at the time the REQ_PREFLUSH is issued, are added in order to26simulate the worst case scenario with regard to power failures. Consider the27following example (W means write, C means complete):28 29 W1,W2,W3,C3,C2,Wflush,C1,Cflush30 31The log would show the following:32 33 W3,W2,flush,W1....34 35Again this is to simulate what is actually on disk, this allows us to detect36cases where a power failure at a particular point in time would create an37inconsistent file system.38 39Any REQ_FUA requests bypass this flushing mechanism and are logged as soon as40they complete as those requests will obviously bypass the device cache.41 42Any REQ_OP_DISCARD requests are treated like WRITE requests. Otherwise we would43have all the DISCARD requests, and then the WRITE requests and then the FLUSH44request. Consider the following example:45 46 WRITE block 1, DISCARD block 1, FLUSH47 48If we logged DISCARD when it completed, the replay would look like this:49 50 DISCARD 1, WRITE 1, FLUSH51 52which isn't quite what happened and wouldn't be caught during the log replay.53 54Target interface55================56 57i) Constructor58 59 log-writes <dev_path> <log_dev_path>60 61 ============= ==============================================62 dev_path Device that all of the IO will go to normally.63 log_dev_path Device where the log entries are written to.64 ============= ==============================================65 66ii) Status67 68 <#logged entries> <highest allocated sector>69 70 =========================== ========================71 #logged entries Number of logged entries72 highest allocated sector Highest allocated sector73 =========================== ========================74 75iii) Messages76 77 mark <description>78 79 You can use a dmsetup message to set an arbitrary mark in a log.80 For example say you want to fsck a file system after every81 write, but first you need to replay up to the mkfs to make sure82 we're fsck'ing something reasonable, you would do something like83 this::84 85 mkfs.btrfs -f /dev/mapper/log86 dmsetup message log 0 mark mkfs87 <run test>88 89 This would allow you to replay the log up to the mkfs mark and90 then replay from that point on doing the fsck check in the91 interval that you want.92 93 Every log has a mark at the end labeled "dm-log-writes-end".94 95Userspace component96===================97 98There is a userspace tool that will replay the log for you in various ways.99It can be found here: https://github.com/josefbacik/log-writes100 101Example usage102=============103 104Say you want to test fsync on your file system. You would do something like105this::106 107 TABLE="0 $(blockdev --getsz /dev/sdb) log-writes /dev/sdb /dev/sdc"108 dmsetup create log --table "$TABLE"109 mkfs.btrfs -f /dev/mapper/log110 dmsetup message log 0 mark mkfs111 112 mount /dev/mapper/log /mnt/btrfs-test113 <some test that does fsync at the end>114 dmsetup message log 0 mark fsync115 md5sum /mnt/btrfs-test/foo116 umount /mnt/btrfs-test117 118 dmsetup remove log119 replay-log --log /dev/sdc --replay /dev/sdb --end-mark fsync120 mount /dev/sdb /mnt/btrfs-test121 md5sum /mnt/btrfs-test/foo122 <verify md5sum's are correct>123 124 Another option is to do a complicated file system operation and verify the file125 system is consistent during the entire operation. You could do this with:126 127 TABLE="0 $(blockdev --getsz /dev/sdb) log-writes /dev/sdb /dev/sdc"128 dmsetup create log --table "$TABLE"129 mkfs.btrfs -f /dev/mapper/log130 dmsetup message log 0 mark mkfs131 132 mount /dev/mapper/log /mnt/btrfs-test133 <fsstress to dirty the fs>134 btrfs filesystem balance /mnt/btrfs-test135 umount /mnt/btrfs-test136 dmsetup remove log137 138 replay-log --log /dev/sdc --replay /dev/sdb --end-mark mkfs139 btrfsck /dev/sdb140 replay-log --log /dev/sdc --replay /dev/sdb --start-mark mkfs \141 --fsck "btrfsck /dev/sdb" --check fua142 143And that will replay the log until it sees a FUA request, run the fsck command144and if the fsck passes it will replay to the next FUA, until it is completed or145the fsck command exists abnormally.146