generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 156
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. 156
6 #
7 # Ensure that fallocate on reflinked files actually CoWs the shared blocks.
8 #   - Record fs block usage (0)
9 #   - Create a file and some reflink copies
10 #   - Record fs block usage (1)
11 #   - funshare half of one of the copies
12 #   - Record fs block usage (2)
13 #   - funshare all of the copies
14 #   - Record fs block usage (3)
15 #   - rewrite the original file
16 #   - Record fs block usage (4)
17 #   - Compare fs block usage of 0-4 to ensure that block usage behaves as
18 #     we expect.
19 #
20 # "funshare" refers to fallocate copy-on-writing the shared blocks
21 #
22 . ./common/preamble
23 _begin_fstest auto quick clone
24
25 # Override the default cleanup function.
26 _cleanup()
27 {
28     cd /
29     rm -rf $tmp.* $testdir
30 }
31
32 # Import common functions.
33 . ./common/filter
34 . ./common/attr
35 . ./common/reflink
36
37 # real QA test starts here
38 _require_test_reflink
39 _require_cp_reflink
40 _require_xfs_io_command "funshare"
41
42 testdir=$TEST_DIR/test-$seq
43 rm -rf $testdir
44 mkdir $testdir
45
46 echo "Create the original file blocks"
47 blksz="$(_get_block_size $testdir)"
48 blks=2000
49 margin='15%'
50 sz=$((blksz * blks))
51 free_blocks0=$(stat -f $testdir -c '%f')
52 nr=4
53 filesize=$((blksz * nr))
54 _pwrite_byte 0x61 0 $sz $testdir/file1 >> $seqres.full
55 _test_cycle_mount
56
57 echo "Create the reflink copies"
58 for i in `seq 2 $nr`; do
59         _cp_reflink $testdir/file1 $testdir/file$i
60 done
61 _test_cycle_mount
62 free_blocks1=$(stat -f $testdir -c '%f')
63
64 echo "funshare part of a file"
65 $XFS_IO_PROG -f -c "funshare 0 $((sz / 2))" $testdir/file2
66 _test_cycle_mount
67
68 echo "funshare some of the copies"
69 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file2
70 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file3
71 _test_cycle_mount
72 free_blocks2=$(stat -f $testdir -c '%f')
73
74 echo "funshare the rest of the files"
75 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file4
76 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file1
77 _test_cycle_mount
78 free_blocks3=$(stat -f $testdir -c '%f')
79
80 echo "Rewrite the original file"
81 _pwrite_byte 0x65 0 $sz $testdir/file1 >> $seqres.full
82 _test_cycle_mount
83 free_blocks4=$(stat -f $testdir -c '%f')
84 #echo $free_blocks0 $free_blocks1 $free_blocks2 $free_blocks3 $free_blocks4
85
86 _within_tolerance "free blocks after reflinking" $free_blocks1 $((free_blocks0 - blks)) $margin -v
87
88 _within_tolerance "free blocks after nocow'ing some copies" $free_blocks2 $((free_blocks1 - (2 * blks))) $margin -v
89
90 _within_tolerance "free blocks after nocow'ing all copies" $free_blocks3 $((free_blocks2 - blks)) $margin -v
91
92 _within_tolerance "free blocks after overwriting original" $free_blocks4 $free_blocks3 $margin -v
93
94 _within_tolerance "free blocks after all tests" $free_blocks4 $((free_blocks0 - (4 * blks))) $margin -v
95
96 # success, all done
97 status=0
98 exit