btrfs/048: add validation of compression options
[xfstests-dev.git] / common / dmlogwrites
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015 Facebook, Inc.  All Rights Reserved.
4 #
5 # common functions for setting up and tearing down a dm log-writes device
6
7 _require_log_writes()
8 {
9         [ -z "$LOGWRITES_DEV" -o ! -b "$LOGWRITES_DEV" ] && \
10                 _notrun "This test requires a valid \$LOGWRITES_DEV"
11
12         _exclude_scratch_mount_option dax
13         _require_dm_target log-writes
14         _require_test_program "log-writes/replay-log"
15 }
16
17 # Starting from v4.15-rc1, DAX support was added to dm-log-writes, but note
18 # that it doesn't track the data that we write via the mmap(), so we can't do
19 # any data integrity checking. We can only verify that the metadata writes for
20 # the page faults happened, e.g. when mmap(2) with MAP_SYNC flag.
21 #
22 # Introduce a new helper to check if dm-log-writes target supports DAX
23 # explicitly. But this is considered as a temporary workaround, we want to move
24 # all the DAX check back to _require_log_writes when dm-log-writes gains full
25 # DAX support and remove this helper.
26 _require_log_writes_dax()
27 {
28         [ -z "$LOGWRITES_DEV" -o ! -b "$LOGWRITES_DEV" ] && \
29                 _notrun "This test requires a valid \$LOGWRITES_DEV"
30
31         _require_dm_target log-writes
32         _require_test_program "log-writes/replay-log"
33
34         local ret=0
35         _log_writes_init $SCRATCH_DEV
36         _log_writes_mkfs > /dev/null 2>&1
37         _log_writes_mount -o dax > /dev/null 2>&1
38         # Check options to be sure. XFS ignores dax option
39         # and goes on if dev underneath does not support dax.
40         _fs_options $LOGWRITES_DMDEV | grep -qw "dax"
41         ret=$?
42         _log_writes_cleanup
43         if [ $ret -ne 0 ]; then
44                 _notrun "$LOGWRITES_DMDEV $FSTYP does not support -o dax"
45         fi
46 }
47
48 _log_writes_init()
49 {
50         blkdev=$1
51
52         [ -z "$blkdev" ] && _fail \
53         "block dev must be specified for _log_writes_init"
54
55         local BLK_DEV_SIZE=`blockdev --getsz $blkdev`
56         LOGWRITES_NAME=logwrites-test
57         LOGWRITES_DMDEV=/dev/mapper/$LOGWRITES_NAME
58         LOGWRITES_TABLE="0 $BLK_DEV_SIZE log-writes $blkdev $LOGWRITES_DEV"
59         _dmsetup_create $LOGWRITES_NAME --table "$LOGWRITES_TABLE" || \
60                 _fail "failed to create log-writes device"
61 }
62
63 _log_writes_mark()
64 {
65         [ $# -ne 1 ] && _fail "_log_writes_mark takes one argument"
66         $DMSETUP_PROG message $LOGWRITES_NAME 0 mark $1
67 }
68
69 _log_writes_mkfs()
70 {
71         _scratch_options mkfs
72         _mkfs_dev $SCRATCH_OPTIONS $LOGWRITES_DMDEV
73         _log_writes_mark mkfs
74 }
75
76 _log_writes_mount()
77 {
78         _scratch_options mount
79         $MOUNT_PROG -t $FSTYP `_common_dev_mount_options $*` $SCRATCH_OPTIONS \
80                 $LOGWRITES_DMDEV $SCRATCH_MNT
81 }
82
83 _log_writes_unmount()
84 {
85         $UMOUNT_PROG $SCRATCH_MNT
86 }
87
88 # _log_writes_replay_log <mark>
89 #
90 # This replays the log contained on $LOGWRITES_DEV onto blkdev upto the
91 # mark passed in.
92 _log_writes_replay_log()
93 {
94         _mark=$1
95         _blkdev=$2
96
97         [ -z "$_blkdev" ] && _fail \
98         "block dev must be specified for _log_writes_replay_log"
99
100         $here/src/log-writes/replay-log --log $LOGWRITES_DEV --find \
101                 --end-mark $_mark >> $seqres.full 2>&1
102         [ $? -ne 0 ] && _fail "mark '$_mark' does not exist"
103
104         $here/src/log-writes/replay-log --log $LOGWRITES_DEV --replay $_blkdev \
105                 --end-mark $_mark >> $seqres.full 2>&1
106         [ $? -ne 0 ] && _fail "replay failed"
107 }
108
109 _log_writes_remove()
110 {
111         _dmsetup_remove $LOGWRITES_NAME
112 }
113
114 _log_writes_cleanup()
115 {
116         $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
117         _log_writes_remove
118 }
119
120 # Convert log writes mark to entry number
121 # Result entry number is output to stdout, could be empty if not found
122 _log_writes_mark_to_entry_number()
123 {
124         local mark=$1
125         local ret
126
127         [ -z "$mark" ] && _fatal \
128                 "mark must be given for _log_writes_mark_to_entry_number"
129
130         ret=$($here/src/log-writes/replay-log --find --log $LOGWRITES_DEV \
131                 --end-mark $mark 2> /dev/null)
132         [ -z "$ret" ] && return
133         ret=$(echo "$ret" | cut -f1 -d\@)
134         echo "mark $mark has entry number $ret" >> $seqres.full
135         echo "$ret"
136 }
137
138 # Find next fua write entry number
139 # Result entry number is output to stdout, could be empty if not found
140 _log_writes_find_next_fua()
141 {
142         local start_entry=$1
143         local ret
144
145         [ -z "$start_entry" ] && start_entry=0
146         ret=$($here/src/log-writes/replay-log --find --log $LOGWRITES_DEV \
147               --next-fua --start-entry $start_entry 2> /dev/null)
148         [ -z "$ret" ] && return
149
150         # Result should be something like "1024@offset" where 1024 is the
151         # entry number we need
152         ret=$(echo "$ret" | cut -f1 -d\@)
153         echo "next fua is entry number $ret" >> $seqres.full
154         echo "$ret"
155 }
156
157 # Replay log range to specified entry
158 # $1:   End entry. The entry with this number *WILL* be replayed
159 _log_writes_replay_log_range()
160 {
161         local end=$1
162         local blkdev=$2
163
164         [ -z "$end" ] && _fail \
165         "end entry must be specified for _log_writes_replay_log_range"
166         [ -z "$blkdev" ] && _fail \
167         "block dev must be specified for _log_writes_replay_log_range"
168
169         # To ensure we replay the last entry,
170         # we need to manually increase the end entry number to ensure
171         # it's played
172         echo "=== replay to $end ===" >> $seqres.full
173         $here/src/log-writes/replay-log --log $LOGWRITES_DEV \
174                 --replay $blkdev --limit $(($end + 1)) \
175                 >> $seqres.full 2>&1
176         [ $? -ne 0 ] && _fail "replay failed"
177 }