generic: test for creating duplicate filenames in encrypted dir
[xfstests-dev.git] / tests / overlay / 018
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2016-2017 CTERA Networks. All Rights Reserved.
4 #
5 # FSQA Test No. 018
6 #
7 # Test hardlink breakage
8 #
9 # This simple test demonstrates a known issue with overlayfs:
10 # - file A and B are hardlinked in lower
11 # - modify A to trigger copy up
12 # - file A is no longer a hardlink of file B
13 #
14 seq=`basename $0`
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         rm -f $tmp.*
25 }
26
27 # get standard environment, filters and checks
28 . ./common/rc
29 . ./common/filter
30
31 # real QA test starts here
32 _supported_fs overlay
33 _require_scratch
34 _require_scratch_feature index
35 _require_test_program "t_dir_type"
36
37 rm -f $seqres.full
38
39 _scratch_mkfs >>$seqres.full 2>&1
40
41 # Create 2 hardlinked files in lower
42 lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
43 mkdir -p $lowerdir
44 echo "zero" >> $lowerdir/foo
45 ln $lowerdir/foo $lowerdir/bar
46
47
48 # Record inode numbers in format <ino> <nlink>
49 function record_ino_nlink()
50 {
51         ls -li $FILES | awk '{ print $1, $3, $10}' > $1
52 }
53
54 # Check inode numbers match recorded inode numbers
55 function check_ino_nlink()
56 {
57         dir=$1
58         before=$2
59         after=$3
60
61         record_ino_nlink $after
62
63         # Test constant stat(2) st_ino/st_nlink -
64         #   Compare before..after - expect silence
65         # We use diff -u so out.bad will tell us which stage failed
66         diff -u $before $after
67
68         # Test constant readdir(3)/getdents(2) d_ino -
69         #   Expect to find file by inode number
70         cat $before | while read ino nlink f; do
71                 $here/src/t_dir_type $dir $ino | grep -q $(basename $f) || \
72                         echo "$(basename $f) not found by ino $ino (from $before)"
73         done
74 }
75
76 # Enable overlay index feature to prevent breaking hardlinks on copy up
77 _scratch_mount -o index=on
78
79
80 rm -f $tmp.*
81
82 foo=$SCRATCH_MNT/foo
83 bar=$SCRATCH_MNT/bar
84
85 FILES="$foo $bar"
86
87 echo "== Before copy up =="
88 cat $FILES
89 record_ino_nlink $tmp.before
90
91 # Modify content of one of the hardlinks
92 # Intentionally modify the last hardlink in $FILES, so after mount cycle
93 # when reading the first file in $FILES, last file won't be in inode/dcache
94 echo "one" >> $bar
95
96 echo "== After write one =="
97 cat $FILES
98 check_ino_nlink $SCRATCH_MNT $tmp.before $tmp.after_one
99
100 # Verify that the hardlinks survive a mount cycle
101 _scratch_cycle_mount index=on
102
103 echo "== After mount cycle =="
104 cat $FILES
105 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_cycle
106
107 # Drop caches to get the copied up hardlink out of cache
108 echo 3 > /proc/sys/vm/drop_caches
109
110 # Modify content of the other hardlink
111 echo "two" >> $foo
112
113 echo "== After write two =="
114 cat $FILES
115 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_two
116
117 status=0
118 exit