misc: move exit status into trap handler
[xfstests-dev.git] / tests / btrfs / 231
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2021 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 231
6 #
7 # Test that when using the NO_HOLES feature, if we truncate down a file, clone a
8 # file range covering only a hole into an offset beyond the current file size,
9 # and then fsync the file, after a power failure we get the expected file content
10 # and we do not get stale data corresponding to file extents that existed before
11 # truncating the file.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16 tmp=/tmp/$$
17 status=1        # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         _cleanup_flakey
23         cd /
24         rm -f $tmp.*
25 }
26
27 # get standard environment, filters and checks
28 . ./common/rc
29 . ./common/filter
30 . ./common/dmflakey
31
32 # real QA test starts here
33 _supported_fs btrfs
34 _require_scratch
35 _require_btrfs_fs_feature "no_holes"
36 _require_btrfs_mkfs_feature "no-holes"
37 _require_dm_target flakey
38
39 rm -f $seqres.full
40
41 _scratch_mkfs -O no-holes >>$seqres.full 2>&1
42 _require_metadata_journaling $SCRATCH_DEV
43 _init_flakey
44 _mount_flakey
45
46 # Create our test file with 3 extents of 256K and a 256K hole at offset 256K.
47 # The file has a size of 1280K.
48 $XFS_IO_PROG -f -s \
49              -c "pwrite -S 0xab -b 256K 0 256K" \
50              -c "pwrite -S 0xcd -b 256K 512K 256K" \
51              -c "pwrite -S 0xef -b 256K 768K 256K" \
52              -c "pwrite -S 0x73 -b 256K 1024K 256K" \
53              $SCRATCH_MNT/foobar | _filter_xfs_io
54
55 # Make sure it's durably persisted. We want the last committed super block to
56 # point to this particular file extent layout.
57 sync
58
59 # Now truncate our file to a smaller size, falling within a position of the
60 # second extent. This sets the full sync runtime flag on the inode.
61 # Then fsync the file to log it and clear the full sync flag from the inode.
62 # The third extent is no longer part of the file and therefore it is not logged.
63 $XFS_IO_PROG -c "truncate 800K" -c "fsync" $SCRATCH_MNT/foobar
64
65 # Now do a clone operation that only clones the hole and sets back the file size
66 # to match the size it had before the truncate operation (1280K).
67 $XFS_IO_PROG \
68         -c "reflink $SCRATCH_MNT/foobar 256K 1024K 256K" \
69         -c "fsync" \
70         $SCRATCH_MNT/foobar | _filter_xfs_io
71
72 echo "File data before power failure:"
73 od -A d -t x1 $SCRATCH_MNT/foobar
74
75 # Simulate a power failure and then mount again the filesystem to replay the log
76 # tree.
77 _flakey_drop_and_remount
78
79 # This should match what we got before the power failure. The range from 1024K
80 # to 1280K should be a hole and not point to an extent full of bytes with a
81 # value of 0x73.
82 echo "File data after power failure:"
83 od -A d -t x1 $SCRATCH_MNT/foobar
84
85 _unmount_flakey
86 status=0
87 exit