generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 098
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FSQA Test No. 098
6 #
7 # Test that after truncating a file into the middle of a hole causes the new
8 # size of the file to be persisted after a clean unmount of the filesystem (or
9 # after the inode is evicted). This is for the case where all the data following
10 # the hole is not yet durably persisted, that is, that data is only present in
11 # the page cache.
12 #
13 # This test is motivated by an issue found in btrfs.
14 #
15 . ./common/preamble
16 _begin_fstest auto quick metadata
17
18 # Import common functions.
19 . ./common/filter
20
21 # real QA test starts here
22 _supported_fs generic
23 _require_scratch
24
25 # This test was motivated by an issue found in btrfs when the btrfs no-holes
26 # feature is enabled (introduced in kernel 3.14). So enable the feature if the
27 # fs being tested is btrfs.
28 if [ $FSTYP == "btrfs" ]; then
29         _require_btrfs_fs_feature "no_holes"
30         _require_btrfs_mkfs_feature "no-holes"
31         MKFS_OPTIONS="$MKFS_OPTIONS -O no-holes"
32 fi
33
34 _scratch_mkfs >>$seqres.full 2>&1
35 _scratch_mount
36
37 workout()
38 {
39         local need_sync=$1
40
41         # Create our test file with some data and durably persist it.
42         $XFS_IO_PROG -t -f -c "pwrite -S 0xaa 0 128K" $SCRATCH_MNT/foo | _filter_xfs_io
43         sync
44
45         # Append some data to the file, increasing its size, and leave a hole between
46         # the old size and the start offset if the following write. So our file gets
47         # a hole in the range [128Kb, 256Kb[.
48         $XFS_IO_PROG -c "pwrite -S 0xbb 256K 32K" $SCRATCH_MNT/foo | _filter_xfs_io
49
50         # This 'sync' is to flush file extent on disk and update on-disk inode size.
51         # This is required to trigger a bug in btrfs truncate where it updates on-disk
52         # inode size incorrectly.
53         if [ $need_sync -eq 1 ]; then
54                 sync
55         fi
56
57         # Now truncate our file to a smaller size that is in the middle of the hole we
58         # previously created.
59         # If we don't flush dirty page cache above, on most truncate
60         # implementations the data we appended before gets discarded from
61         # memory (with truncate_setsize()) and never ends up being written to
62         # disk.
63         $XFS_IO_PROG -c "truncate 160K" $SCRATCH_MNT/foo
64
65         _scratch_cycle_mount
66
67         # We expect to see a file with a size of 160Kb, with the first 128Kb of data all
68         # having the value 0xaa and the remaining 32Kb of data all having the value 0x00
69         echo "File content after remount:"
70         od -t x1 $SCRATCH_MNT/foo
71 }
72
73 workout 0
74 # flush after each write
75 workout 1
76
77 status=0
78 exit