generic: test for non-zero used blocks while writing into a file
[xfstests-dev.git] / tests / generic / 483
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. 483
6 #
7 # Test that fsync operations preserve extents allocated with fallocate(2) that
8 # are placed beyond a file's size.
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13 tmp=/tmp/$$
14 status=1        # failure is the default!
15 trap "_cleanup; exit \$status" 0 1 2 3 15
16
17 _cleanup()
18 {
19         _cleanup_flakey
20         cd /
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27 . ./common/dmflakey
28 . ./common/punch
29
30 # real QA test starts here
31 _supported_fs generic
32 _require_scratch
33 _require_dm_target flakey
34 _require_xfs_io_command "falloc" "-k"
35 _require_xfs_io_command "fiemap"
36
37 rm -f $seqres.full
38
39 _scratch_mkfs >>$seqres.full 2>&1
40 _require_metadata_journaling $SCRATCH_DEV
41 _init_flakey
42 _mount_flakey
43
44 # Create our test files.
45 $XFS_IO_PROG -f -c "pwrite -S 0xea 0 256K" $SCRATCH_MNT/foo >/dev/null
46
47 # Create a file with many extents. We later want to shrink truncate it and
48 # add a prealloc extent beyond its new size.
49 for ((i = 1; i <= 500; i++)); do
50         offset=$(((i - 1) * 4 * 1024))
51         $XFS_IO_PROG -f -s -c "pwrite -S 0xcf $offset 4K" \
52                 $SCRATCH_MNT/bar >/dev/null
53 done
54
55 # A file which already has a prealloc extent beyond its size.
56 # The fsync done on it is motivated by differences in the btrfs implementation
57 # of fsync (first fsync has different logic from subsequent fsyncs).
58 $XFS_IO_PROG -f -c "pwrite -S 0xf1 0 256K" \
59              -c "falloc -k 256K 768K" \
60              -c "fsync" \
61              $SCRATCH_MNT/baz >/dev/null
62
63 # Make sure everything done so far is durably persisted.
64 sync
65
66 # Allocate an extent beyond the size of the first test file and fsync it.
67 $XFS_IO_PROG -c "falloc -k 256K 1M"\
68              -c "fsync" \
69              $SCRATCH_MNT/foo
70
71 # Do a shrinking truncate of our test file, add a prealloc extent to it after
72 # its new size and fsync it.
73 $XFS_IO_PROG -c "truncate 256K" \
74              -c "falloc -k 256K 1M"\
75              -c "fsync" \
76              $SCRATCH_MNT/bar
77
78 # Allocate another extent beyond the size of file baz.
79 $XFS_IO_PROG -c "falloc -k 1M 2M"\
80              -c "fsync" \
81              $SCRATCH_MNT/baz
82
83 # Simulate a power failure and mount the filesystem to check that the extents
84 # previously allocated were not lost and the file sizes are correct.
85 _flakey_drop_and_remount
86
87 echo "File foo fiemap:"
88 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/foo | _filter_hole_fiemap
89 echo "File foo size:"
90 stat --format %s $SCRATCH_MNT/foo
91
92 echo "File bar fiemap:"
93 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/bar | _filter_hole_fiemap
94 echo "File bar size:"
95 stat --format %s $SCRATCH_MNT/bar
96
97 echo "File baz fiemap:"
98 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/baz | _filter_hole_fiemap
99 echo "File baz size:"
100 stat --format %s $SCRATCH_MNT/baz
101
102 _unmount_flakey
103 _cleanup_flakey
104
105 status=0
106 exit