generic: test for file fsync after moving it to a new parent directory
[xfstests-dev.git] / tests / generic / 468
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Huawei.  All Rights Reserved.
4 #
5 # FS QA Test 468
6 #
7 # This testcase is a fallocate variant of generic/392, it expands to test
8 # block preallocation functionality of fallocate.
9 # In this case, we are trying to execute:
10 # 1. fallocate {,-k}
11 # 2. f{data,}sync
12 # 3. power-cuts
13 # 4. recovery filesystem during mount
14 # 5. check inode's metadata
15 #
16 # In the case of fsync, filesystem should recover all the inode metadata, while
17 # recovering i_blocks and i_size at least for fdatasync, so this testcase excepts
18 # that inode metadata will be unchanged after recovery.
19 #
20 seq=`basename $0`
21 seqres=$RESULT_DIR/$seq
22 echo "QA output created by $seq"
23
24 here=`pwd`
25 tmp=/tmp/$$
26 status=1        # failure is the default!
27 trap "_cleanup; exit \$status" 0 1 2 3 15
28
29 _cleanup()
30 {
31         cd /
32         rm -f $tmp.*
33 }
34
35 # get standard environment, filters and checks
36 . ./common/rc
37 . ./common/filter
38
39 # remove previous $seqres.full before test
40 rm -f $seqres.full
41
42 # real QA test starts here
43 _supported_fs generic
44 _supported_os Linux
45
46 _require_scratch
47 _require_scratch_shutdown
48 _require_xfs_io_command "falloc" "-k"
49
50 _scratch_mkfs >/dev/null 2>&1
51 _require_metadata_journaling $SCRATCH_DEV
52 _scratch_mount
53
54 testfile=$SCRATCH_MNT/testfile
55
56 # check inode metadata after shutdown
57 check_inode_metadata()
58 {
59         sync_mode=$1
60
61         # fsync or fdatasync
62         if [ "$sync_mode" = "fsync" ]; then
63                 stat_opt='-c "b: %b s: %s a: %x m: %y c: %z"'
64         else
65                 stat_opt='-c "b: %b s: %s"'
66         fi
67
68         before=`stat "$stat_opt" $testfile`
69
70         $XFS_IO_PROG -c "$sync_mode" $testfile | _filter_xfs_io
71         _scratch_shutdown | tee -a $seqres.full
72         _scratch_cycle_mount
73
74         after=`stat "$stat_opt" $testfile`
75
76         if [ "$before" != "$after" ]; then
77                 echo "Before: $before"
78                 echo "After : $after"
79         fi
80         echo "Before: $before" >> $seqres.full
81         echo "After : $after" >> $seqres.full
82         rm $testfile
83 }
84
85 # fallocate XX KB with f{data}sync, followed by power-cut
86 test_falloc()
87 {
88         # 4202496 = 4m + 8k
89         echo "==== falloc $2$3 test with $1 ====" | tee -a $seqres.full
90         $XFS_IO_PROG -f -c "truncate 4202496"   \
91                         -c "pwrite 0 4202496"   \
92                         -c "fsync"              \
93                         -c "falloc $2 4202496 $3"\
94                         $testfile >/dev/null
95         check_inode_metadata $1
96 }
97
98 for i in fsync fdatasync; do
99         test_falloc $i "" 1024
100         test_falloc $i "" 4096
101         test_falloc $i "" 104857600
102         test_falloc $i "-k " 1024
103         test_falloc $i "-k " 4096
104         test_falloc $i "-k " 104857600
105 done
106
107 status=0
108 exit