de4e7ce079a744d8c69670aa3ae85c9601383ad4
[xfstests-dev.git] / tests / generic / 059
1 #! /bin/bash
2 # FS QA Test No. 059
3 #
4 # This test is motivated by an fsync issue discovered in btrfs.
5 # The issue was that after punching a hole for a small range, which affected
6 # only a partial page, an fsync operation would have no effect at all. This was
7 # because for this particular case the btrfs hole punching implementation did
8 # not update some btrfs specific inode metadata that is required to determine
9 # if an fsync operation needs to update the fsync log. For this to happen, it
10 # was also necessary that in the transaction where the hole punching was
11 # performed, and before the fsync operation, no other operation that modified
12 # the file (or its metadata) was performed.
13 #
14 # The btrfs issue was fixed by the following linux kernel patch:
15 #
16 #  Btrfs: add missing inode update when punching hole
17 #
18 #-----------------------------------------------------------------------
19 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
20 # Author: Filipe Manana <fdmanana@suse.com>
21 #
22 # This program is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU General Public License as
24 # published by the Free Software Foundation.
25 #
26 # This program is distributed in the hope that it would be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write the Free Software Foundation,
33 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
34 #-----------------------------------------------------------------------
35 #
36
37 seq=`basename $0`
38 seqres=$RESULT_DIR/$seq
39 echo "QA output created by $seq"
40
41 here=`pwd`
42 tmp=/tmp/$$
43 status=1        # failure is the default!
44
45 _cleanup()
46 {
47         _cleanup_flakey
48         rm -f $tmp.*
49 }
50 trap "_cleanup; exit \$status" 0 1 2 3 15
51
52 # get standard environment, filters and checks
53 . ./common/rc
54 . ./common/filter
55 . ./common/dmflakey
56
57 # real QA test starts here
58 _supported_fs generic
59 _supported_os Linux
60 _need_to_be_root
61 _require_scratch
62 _require_dm_target flakey
63 _require_metadata_journaling $SCRATCH_DEV
64 _require_xfs_io_command "fpunch"
65
66 rm -f $seqres.full
67
68 _scratch_mkfs >> $seqres.full 2>&1
69 _init_flakey
70 _mount_flakey
71
72 # Create our test file.
73 $XFS_IO_PROG -f -c "pwrite -S 0x22 -b 16K 0 16K" \
74         $SCRATCH_MNT/foo | _filter_xfs_io
75
76 # Fsync the file, this makes btrfs update some btrfs inode specific fields
77 # that are used to track if the inode needs to be written/updated to the fsync
78 # log or not. After this fsync, the new values for those fields indicate that
79 # a subsequent fsync does not need to touch the fsync log.
80 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
81
82 # Force a commit of the current transaction. After this point, any operation
83 # that modifies the data or metadata of our file, should update those fields in
84 # the btrfs inode with values that make the next fsync operation write to the
85 # fsync log.
86 sync
87
88 # Punch a hole in our file. This small range affects only 1 page.
89 # This made the btrfs hole punching implementation write only some zeroes in
90 # one page, but it did not update the btrfs inode fields used to determine if
91 # the next fsync needs to write to the fsync log.
92 $XFS_IO_PROG -c "fpunch 8000 4K" $SCRATCH_MNT/foo
93
94 # Another variation of the previously mentioned case.
95 $XFS_IO_PROG -c "fpunch 15000 100" $SCRATCH_MNT/foo
96
97 # Now fsync the file. This was a no-operation because the previous hole punch
98 # operation didn't update the inode's fields mentioned before, so they remained
99 # with the values they had after the first fsync - that is, they indicate that
100 # it is not needed to write to fsync log.
101 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
102
103 echo "File content before:"
104 od -t x1 $SCRATCH_MNT/foo
105
106 _flakey_drop_and_remount
107
108 # Because the last fsync didn't do anything, here the file content matched what
109 # it was after the first fsync, before the holes were punched, and not what it
110 # was after the holes were punched.
111 echo "File content after:"
112 od -t x1 $SCRATCH_MNT/foo
113
114 status=0
115 exit