generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / generic / 677
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2022 SUSE Linux Products GmbH.  All Rights Reserved.
4 #
5 # FS QA Test 677
6 #
7 # Test that after a full fsync of a file with preallocated extents beyond the
8 # file's size, if a power failure happens, the preallocated extents still exist
9 # after we mount the filesystem.
10 #
11 . ./common/preamble
12 _begin_fstest auto quick log prealloc
13
14 _cleanup()
15 {
16         _cleanup_flakey
17         cd /
18         rm -r -f $tmp.*
19 }
20
21 . ./common/filter
22 . ./common/dmflakey
23 . ./common/punch
24
25 # real QA test starts here
26
27 _supported_fs generic
28 _require_scratch
29 _require_dm_target flakey
30 _require_xfs_io_command "falloc" "-k"
31 _require_xfs_io_command "fiemap"
32 _require_odirect
33
34 rm -f $seqres.full
35
36 _scratch_mkfs >>$seqres.full 2>&1
37 _require_metadata_journaling $SCRATCH_DEV
38 _init_flakey
39 _mount_flakey
40
41 # Create our test file with many extents.
42 # On btrfs this results in having multiple leaves of metadata full of file
43 # extent items, a condition necessary to trigger the original bug.
44 #
45 # We use direct IO here because:
46 #
47 # 1) It's faster then doing fsync after each buffered write;
48 #
49 # 2) For btrfs, the first fsync would clear the inode's full sync runtime flag,
50 #    and we want the fsync below to trigger the full fsync code path of btrfs.
51 $XFS_IO_PROG -f -d -c "pwrite -b 4K 0 16M" $SCRATCH_MNT/foo | _filter_xfs_io
52
53 # Now add two preallocated extents to our file without extending the file's size.
54 # One right at i_size, and another further beyond, leaving a gap between the two
55 # prealloc extents.
56 $XFS_IO_PROG -c "falloc -k 16M 1M" $SCRATCH_MNT/foo
57 $XFS_IO_PROG -c "falloc -k 20M 1M" $SCRATCH_MNT/foo
58
59 # Make sure everything is durably persisted.
60 # On btrfs this commits the current transaction and it makes all the created
61 # extents to have a generation lower than the generation of the transaction used
62 # by the next write and fsync.
63 sync
64
65 # Now overwrite only the first extent.
66 # On btrfs, due to COW (both data and metadata), that results in modifying only
67 # the first leaf of metadata for our inode (we replace a file extent item and
68 # update the inode item). Then fsync it. On btrfs this fsync will use the slow
69 # code path because it's the first fsync since the inode was created/loaded.
70 $XFS_IO_PROG -c "pwrite 0 4K" -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io
71
72 # Simulate a power failure and then mount again the filesystem to replay the log
73 # tree.
74 _flakey_drop_and_remount
75
76 # After the power failure we expect that the preallocated extents, beyond the
77 # inode's i_size, still exist.
78 echo "List of extents after power failure:"
79 $XFS_IO_PROG -c "fiemap -v" $SCRATCH_MNT/foo | _filter_fiemap
80
81 _unmount_flakey
82
83 # success, all done
84 status=0
85 exit