overlay: consider index dir with whiteouts clean
[xfstests-dev.git] / tests / overlay / 048
1 #! /bin/bash
2 # FS QA Test 048
3 #
4 # Test nlink accounting of overlay hardlinks with offline modifications.
5 #
6 # nlink of overlay inode should account for the union of lower and upper
7 # hardlinks. Orphan index inodes with union nlink 0 should be cleaned on
8 # mount.
9 #
10 #-----------------------------------------------------------------------
11 # Copyright (C) 2017 CTERA Networks. All Rights Reserved.
12 # Author: Amir Goldstein <amir73il@gmail.com>
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it would be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write the Free Software Foundation,
25 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32
33 tmp=/tmp/$$
34 status=1        # failure is the default!
35 trap "_cleanup; exit \$status" 0 1 2 3 15
36
37 _cleanup()
38 {
39         cd /
40         rm -f $tmp.*
41 }
42
43 # get standard environment, filters and checks
44 . ./common/rc
45 . ./common/filter
46
47 # remove previous $seqres.full before test
48 rm -f $seqres.full
49
50 # real QA test starts here
51 _supported_fs overlay
52 _supported_os Linux
53 _require_scratch
54 _require_scratch_feature index
55
56 report_nlink()
57 {
58         when=$1
59
60         # mount and check nlink after overlay offline modification
61         _scratch_mount -o index=on
62
63         # List <nlink> <name>
64         echo "== $when offline =="
65         for f in $HARDLINKS; do
66                 _ls_l $SCRATCH_MNT/$f | awk '{ print $2, $9 }' | _filter_scratch
67         done
68
69         $UMOUNT_PROG $SCRATCH_MNT
70 }
71
72 # Create lower hardlinks
73 create_hardlinks()
74 {
75         mkdir -p $lowerdir
76         touch $lowerdir/0
77         ln $lowerdir/0 $lowerdir/1
78         ln $lowerdir/0 $lowerdir/2
79 }
80
81 test_hardlinks_offline()
82 {
83         HARDLINKS=`seq 0 2`
84         report_nlink "all upper"
85
86         # Unlink copied up hardlink
87         rm $upperdir/0
88         HARDLINKS=`seq 1 2`
89         report_nlink "unlink upper"
90
91         # Link to copied up hardlink
92         ln $upperdir/2 $upperdir/3
93         HARDLINKS=`seq 1 3`
94         report_nlink "link upper"
95
96         # Rename over copied up hardlink
97         touch $upperdir/new
98         mv $upperdir/new $upperdir/1
99         HARDLINKS=`seq 2 3`
100         report_nlink "rename over upper"
101
102         # Unlink new upper hardlink
103         rm $upperdir/3
104         HARDLINKS=2
105         report_nlink "unlink new upper"
106
107         # Unlink last upper and drop union nlink to zero
108         rm $upperdir/2
109
110         HARDLINKS=
111         report_nlink "unlink last lower"
112
113         # Verify that orphan index is cleaned when dropping nlink to zero
114         # With nfs_export=on index will contain a whiteout index entry, so allow
115         # chardev entries in index dir.
116         find $workdir/index -mindepth 1 -type c -o -print
117 }
118
119 lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
120 upperdir=$OVL_BASE_SCRATCH_MNT/$OVL_UPPER
121 workdir=$OVL_BASE_SCRATCH_MNT/$OVL_WORK
122
123 # Remove all files from previous tests
124 _scratch_mkfs
125
126 # Create lower hardlinks
127 create_hardlinks
128
129 # Enable overlay index feature to prevent breaking hardlinks on copy up
130 _scratch_mount -o index=on
131
132 # Copy up and index hardlinks
133 touch $SCRATCH_MNT/0
134 touch $SCRATCH_MNT/1
135 touch $SCRATCH_MNT/2
136
137 # Perform the rest of the changes offline
138 $UMOUNT_PROG $SCRATCH_MNT
139
140 test_hardlinks_offline
141
142 status=0
143 exit