generic: test for non-zero used blocks while writing into a file
[xfstests-dev.git] / tests / btrfs / 159
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2018 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 159
6 #
7 # Test that when we have the no-holes mode enabled and a specific metadata
8 # layout, if we punch a hole and fsync the file, at replay time the whole
9 # hole was preserved.
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         _cleanup_flakey
21         cd /
22         rm -f $tmp.*
23 }
24
25 # get standard environment, filters and checks
26 . ./common/rc
27 . ./common/filter
28 . ./common/dmflakey
29
30 # real QA test starts here
31 _supported_fs btrfs
32 _require_scratch
33 _require_dm_target flakey
34 _require_xfs_io_command "fpunch"
35 _require_odirect
36
37 rm -f $seqres.full
38
39 run_test()
40 {
41         local punch_offset=$1
42
43         # We create the filesystem with a node size of 64Kb because we need to
44         # create a specific metadata layout in order to trigger the bug we are
45         # testing. At the moment the node size can not be smaller then the
46         # system's page size, so given that the largest possible page size is
47         # 64Kb and by default the node size is set to the system's page size
48         # value, we explicitly create a filesystem with a 64Kb node size.
49         _scratch_mkfs -O no-holes -n $((64 * 1024)) >>$seqres.full 2>&1
50         _require_metadata_journaling $SCRATCH_DEV
51         _init_flakey
52         _mount_flakey
53
54         # Create our test file with 832 extents of 256Kb each. Before each
55         # extent, there is a 256Kb hole (except for the first extent, which
56         # starts at offset 0). This creates two leafs in the filesystem tree.
57         # We use direct IO to ensure we get exactly 256K extents (with buffered
58         # IO we can get writeback triggered at any time and therefore get
59         # extents smaller than 256K).
60         for ((i = 0; i <= 831; i++)); do
61                 local offset=$((i * 2 * 256 * 1024))
62                 $XFS_IO_PROG -f -d -c "pwrite -S 0xab -b 256K $offset 256K" \
63                         $SCRATCH_MNT/foobar >/dev/null
64         done
65
66         # Make sure everything done so far is durably persisted.
67         sync
68
69         # Now punch a hole that covers part of the extent at offset
70         # "$punch_offset".
71         # We want to punch a hole that starts in the middle of the last extent
72         # item in the first leaf. On a system without selinux enabled that is
73         # the extent that starts at offset 216530944, while on a system with it
74         # enabled it is the extent that starts at offset 216006656 (because
75         # selinux causes a xattr item to be added to our test file).
76         $XFS_IO_PROG -c "fpunch $((punch_offset + 128 * 1024 - 4000)) 256K" \
77                      -c "fsync" \
78                      $SCRATCH_MNT/foobar
79
80         echo "File digest before power failure:"
81         md5sum $SCRATCH_MNT/foobar | _filter_scratch
82         # Simulate a power failure and mount the filesystem to check that
83         # replaying the fsync log/journal succeeds and our test file has the
84         # expected content.
85         _flakey_drop_and_remount
86         echo "File digest after power failure and log replay:"
87         md5sum $SCRATCH_MNT/foobar | _filter_scratch
88
89         _unmount_flakey
90         _cleanup_flakey
91 }
92
93 run_test 216006656
94 run_test 216530944
95
96 status=0
97 exit