reflink: ensure that we can handle reflinking a lot of extents
[xfstests-dev.git] / tests / generic / 057
1 #! /bin/bash
2 # FS QA Test No. 057
3 #
4 # This test is motivated by an fsync issue discovered in btrfs.
5 # The issue was that we could lose file data, that was previously fsync'ed
6 # successfully, if we end up adding a hard link to our inode and then persist
7 # the fsync log later via an fsync of other inode for example.
8 #
9 # The btrfs issue was fixed by the following linux kernel patch:
10 #
11 #  Btrfs: fix fsync data loss after adding hard link to inode
12 #
13 #-----------------------------------------------------------------------
14 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
15 # Author: Filipe Manana <fdmanana@suse.com>
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35
36 here=`pwd`
37 tmp=/tmp/$$
38 status=1        # failure is the default!
39
40 _cleanup()
41 {
42         _cleanup_flakey
43         rm -f $tmp.*
44 }
45 trap "_cleanup; exit \$status" 0 1 2 3 15
46
47 # get standard environment, filters and checks
48 . ./common/rc
49 . ./common/filter
50 . ./common/dmflakey
51
52 # real QA test starts here
53 _supported_fs generic
54 _supported_os Linux
55 _need_to_be_root
56 _require_scratch
57 _require_dm_target flakey
58 _require_metadata_journaling $SCRATCH_DEV
59
60 rm -f $seqres.full
61
62 _scratch_mkfs >> $seqres.full 2>&1
63 _init_flakey
64 _mount_flakey
65
66 # Create our test file with some data.
67 $XFS_IO_PROG -f -c "pwrite -S 0xaa -b 8K 0 8K" \
68         $SCRATCH_MNT/foo | _filter_xfs_io
69
70 # Make sure the file is durably persisted.
71 sync
72
73 # Append some data to our file, to increase its size.
74 $XFS_IO_PROG -f -c "pwrite -S 0xcc -b 4K 8K 4K" \
75         $SCRATCH_MNT/foo | _filter_xfs_io
76
77 # Fsync the file, so from this point on if a crash/power failure happens, our
78 # new data is guaranteed to be there next time the fs is mounted.
79 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo
80
81 # Add one hard link to our file. This made btrfs write into the in memory fsync
82 # log a special inode with generation 0 and an i_size of 0 too. Note that this
83 # didn't update the inode in the fsync log on disk.
84 ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link
85
86 # Now make sure the in memory fsync log is durably persisted.
87 # Creating and fsync'ing another file will do it.
88 touch $SCRATCH_MNT/bar
89 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
90
91 # As expected, before the crash/power failure, we should be able to read the
92 # 12Kb of file data.
93 echo "File content before:"
94 od -t x1 $SCRATCH_MNT/foo
95
96 _flakey_drop_and_remount
97
98 # After mounting the fs again, the fsync log was replayed.
99 # The btrfs fsync log replay code didn't update the i_size of the persisted
100 # inode because the inode item in the log had a special generation with a
101 # value of 0 (and it couldn't know the correct i_size, since that inode item
102 # had a 0 i_size too). This made the last 4Kb of file data inaccessible and
103 # effectively lost.
104 echo "File content after:"
105 od -t x1 $SCRATCH_MNT/foo
106
107 status=0
108 exit