generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 154
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 154
6 #
7 # Ensure that CoW on all copies of a file reflinked N times increases block count
8 #   - Record fs block usage (0)
9 #   - Create a file and some reflink copies
10 #   - Record fs block usage (1)
11 #   - CoW some blocks of the copies
12 #   - Record fs block usage (2)
13 #   - CoW all the rest of the blocks of the copies
14 #   - Compare fs block usage to (2), (1), and (0)
15 #
16 . ./common/preamble
17 _begin_fstest auto quick clone
18
19 # Override the default cleanup function.
20 _cleanup()
21 {
22     cd /
23     rm -rf $tmp.* $testdir
24 }
25
26 # Import common functions.
27 . ./common/filter
28 . ./common/reflink
29
30 # real QA test starts here
31 _require_test_reflink
32 _require_cp_reflink
33
34 testdir=$TEST_DIR/test-$seq
35 rm -rf $testdir
36 mkdir $testdir
37
38 echo "Create the original file blocks"
39 blksz="$(_get_block_size $testdir)"
40 blks=2000
41 margin='15%'
42 free_blocks0=$(stat -f $testdir -c '%f')
43 nr=4
44 filesize=$((blksz * nr))
45 sz=$((blks * blksz))
46 _pwrite_byte 0x61 0 $sz $testdir/file1 >> $seqres.full
47 _test_cycle_mount
48
49 echo "Create the reflink copies"
50 for i in `seq 2 $nr`; do
51         _cp_reflink $testdir/file1 $testdir/file$i
52 done
53 _test_cycle_mount
54 free_blocks1=$(stat -f $testdir -c '%f')
55
56 echo "Rewrite some of the blocks"
57 _pwrite_byte 0x62 0 $sz $testdir/file2 >> $seqres.full
58 _pwrite_byte 0x63 0 $((sz / 2)) $testdir/file3 >> $seqres.full
59 _pwrite_byte 0x64 $((sz / 2)) $((sz / 2)) $testdir/file4 >> $seqres.full
60 _test_cycle_mount
61 free_blocks2=$(stat -f $testdir -c '%f')
62
63 echo "Rewrite all the files"
64 _pwrite_byte 0x62 0 $sz $testdir/file2 >> $seqres.full
65 _pwrite_byte 0x63 0 $sz $testdir/file3 >> $seqres.full
66 _pwrite_byte 0x64 0 $sz $testdir/file4 >> $seqres.full
67 _test_cycle_mount
68 free_blocks3=$(stat -f $testdir -c '%f')
69
70 echo "Rewrite the original file"
71 _pwrite_byte 0x65 0 $sz $testdir/file1 >> $seqres.full
72 _test_cycle_mount
73 free_blocks4=$(stat -f $testdir -c '%f')
74 #echo $free_blocks0 $free_blocks1 $free_blocks2 $free_blocks3 $free_blocks4
75
76 _within_tolerance "free blocks after reflinking" $free_blocks1 $((free_blocks0 - blks)) $margin -v
77
78 _within_tolerance "free blocks after partially CoWing some copies" $free_blocks2 $((free_blocks1 - (2 * blks))) $margin -v
79
80 _within_tolerance "free blocks after CoWing all copies" $free_blocks3 $((free_blocks2 - blks)) $margin -v
81
82 _within_tolerance "free blocks after overwriting original" $free_blocks4 $free_blocks3 $margin -v
83
84 _within_tolerance "free blocks after all tests" $free_blocks4 $((free_blocks0 - (4 * blks))) $margin -v
85
86 # success, all done
87 status=0
88 exit