generic: test for non-zero used blocks while writing into a file
[xfstests-dev.git] / tests / overlay / 033
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2017 CTERA Networks. All Rights Reserved.
4 #
5 # FS QA Test 033
6 #
7 # Test nlink accounting of overlay hardlinks.
8 #
9 # nlink of overlay inode should account for the union of lower and upper
10 # hardlinks.
11 #
12 seq=`basename $0`
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
15
16 tmp=/tmp/$$
17 status=1        # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
19
20 _cleanup()
21 {
22         cd /
23         rm -f $tmp.*
24 }
25
26 # get standard environment, filters and checks
27 . ./common/rc
28 . ./common/filter
29
30 # remove previous $seqres.full before test
31 rm -f $seqres.full
32
33 # real QA test starts here
34 _supported_fs overlay
35 _require_scratch
36 _require_scratch_feature index
37
38 report_nlink()
39 {
40         when=$1
41
42         [ $DCACHETEMP != cold ] || echo 2 > /proc/sys/vm/drop_caches
43
44         # check nlink with warm dcache after overlay modification
45         # List <nlink> <name>
46         echo "== $when - $DCACHETEMP dcache =="
47         for f in $HARDLINKS; do
48                 _ls_l $SCRATCH_MNT/$f | awk '{ print $2, $9 }' | _filter_scratch
49         done
50 }
51
52 # Create lower hardlinks
53 create_hardlinks()
54 {
55         mkdir -p $lowerdir
56         touch $lowerdir/0
57         ln $lowerdir/0 $lowerdir/1
58         ln $lowerdir/0 $lowerdir/2
59         ln $lowerdir/0 $lowerdir/3
60 }
61
62 test_hardlinks()
63 {
64         HARDLINKS=`seq 0 3`
65         report_nlink "all lower"
66
67         # Unlink lower hardlink
68         rm $SCRATCH_MNT/0
69         HARDLINKS=`seq 1 3`
70         report_nlink "unlink lower"
71
72         # Link to lower hardlink
73         ln $SCRATCH_MNT/3 $SCRATCH_MNT/4
74         HARDLINKS=`seq 1 4`
75         report_nlink "link lower"
76
77         # Link to upper hardlink
78         ln $SCRATCH_MNT/4 $SCRATCH_MNT/5
79         HARDLINKS=`seq 1 5`
80         report_nlink "link upper"
81
82         # Rename over lower hardlink
83         touch $SCRATCH_MNT/new
84         mv $SCRATCH_MNT/new $SCRATCH_MNT/1
85         HARDLINKS=`seq 2 5`
86         report_nlink "cover lower"
87
88         # Unlink upper hardlink
89         rm $SCRATCH_MNT/5
90         HARDLINKS=`seq 2 4`
91         report_nlink "unlink upper"
92
93         # Rename over upper hardlink
94         touch $SCRATCH_MNT/new
95         mv $SCRATCH_MNT/new $SCRATCH_MNT/4
96         HARDLINKS=`seq 2 3`
97         report_nlink "cover upper"
98
99         # Unlink last upper (union still has one lower)
100         rm $SCRATCH_MNT/3
101         HARDLINKS=2
102         report_nlink "unlink last upper"
103
104         # Unlink last lower and drop union nlink to zero (and hopefully not below)
105         rm $SCRATCH_MNT/2
106
107         # Verify that orphan index is cleaned when dropping nlink to zero
108         # With nfs_export=on index will contain a whiteout index entry, so allow
109         # chardev entries in index dir.
110         find $workdir/index -mindepth 1 -type c -o -print
111 }
112
113 lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
114 workdir=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
115
116 # Remove all files from previous tests
117 _scratch_mkfs
118
119 # Create lower hardlinks
120 create_hardlinks
121
122 # Enable overlay index feature to prevent breaking hardlinks on copy up
123 _scratch_mount -o index=on
124 # Test hardlinks with warm dcache
125 DCACHETEMP=warm
126 test_hardlinks
127
128 # Reset to lower hardlinks
129 _scratch_unmount
130 _scratch_mkfs
131 create_hardlinks
132 _scratch_mount -o index=on
133
134 # Test hardlinks with cold dcache
135 DCACHETEMP=cold
136 test_hardlinks
137
138 status=0
139 exit