misc: move exit status into trap handler
[xfstests-dev.git] / tests / btrfs / 041
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana.  All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/041
6 #
7 # Test that btrfs-progs' restore command is able to correctly recover files
8 # that have compressed extents, specially when the respective file extent
9 # items have a non-zero data offset field.
10 #
11 # This issue is fixed by the following btrfs-progs patch:
12 #
13 #    Btrfs-progs: fix restore of files with compressed extents
14 #
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18
19 here=`pwd`
20 tmp=/tmp/$$
21 status=1        # failure is the default!
22 trap "_cleanup; exit \$status" 0 1 2 3 15
23
24 restore_dir=$TEST_DIR/btrfs-test-$seq
25
26 _cleanup()
27 {
28         rm -fr $tmp
29         rm -fr $restore_dir
30 }
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35
36 # real QA test starts here
37 _supported_fs btrfs
38 _require_test
39 _require_scratch
40
41 rm -f $seqres.full
42 mkdir $restore_dir
43
44 test_btrfs_restore()
45 {
46         if [ -z $1 ]
47         then
48                 OPTIONS=""
49         else
50                 OPTIONS="-o compress-force=$1"
51         fi
52         _scratch_mkfs >/dev/null 2>&1
53         _scratch_mount $OPTIONS
54
55         # Create first file extent item, then fsync to make sure the next write
56         # won't end up in the same file extent item, so that we have 2 distinct
57         # file extent items.
58         $XFS_IO_PROG -f -c "pwrite -S 0xff -b 100000 0 100000" -c "fsync" \
59                 $SCRATCH_MNT/foo | _filter_xfs_io
60
61         # This creates a second file extent item.
62         $XFS_IO_PROG -c "pwrite -S 0xaa -b 100000 100000 100000" -c "fsync" \
63                 $SCRATCH_MNT/foo | _filter_xfs_io
64
65         # Now do a few writes that will cause the first extent item to be split,
66         # with some of the new smaller file extent items getting a data offset
67         # field different from 0.
68         $XFS_IO_PROG -c "pwrite -S 0x1e -b 2 10000 2" $SCRATCH_MNT/foo \
69                 | _filter_xfs_io
70         $XFS_IO_PROG -c "pwrite -S 0xd0 -b 11 33000 11" $SCRATCH_MNT/foo \
71                 | _filter_xfs_io
72         $XFS_IO_PROG -c "pwrite -S 0xbc -b 100 99000 100" $SCRATCH_MNT/foo \
73                 | _filter_xfs_io
74
75         md5sum $SCRATCH_MNT/foo | _filter_scratch
76
77         _scratch_unmount
78
79         rm -f $restore_dir/foo
80         # Now that the fs is unmounted, call btrfs restore to read the file
81         # from disk and save it in the test directory. It used to incorrectly
82         # read compressed file extents that have a non-zero data offset field,
83         # resulting either in decompression failure or reading a wrong section
84         # of the extent.
85         _run_btrfs_util_prog restore $SCRATCH_DEV $restore_dir
86         md5sum $restore_dir/foo | cut -d ' ' -f 1
87 }
88
89 echo "Testing restore of file compressed with lzo"
90 test_btrfs_restore "lzo"
91 echo "Testing restore of file compressed with zlib"
92 test_btrfs_restore "zlib"
93 echo "Testing restore of file without any compression"
94 test_btrfs_restore
95
96 status=0
97 exit