2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2016-2017 CTERA Networks. All Rights Reserved.
7 # Test hardlink breakage
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
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
19 status=1 # failure is the default!
20 trap "_cleanup; exit \$status" 0 1 2 3 15
27 # get standard environment, filters and checks
31 # real QA test starts here
34 _require_scratch_feature index
35 _require_test_program "t_dir_type"
39 _scratch_mkfs >>$seqres.full 2>&1
41 # Create 2 hardlinked files in lower
42 lowerdir=$OVL_BASE_SCRATCH_MNT/$OVL_LOWER
44 echo "zero" >> $lowerdir/foo
45 ln $lowerdir/foo $lowerdir/bar
48 # Record inode numbers in format <ino> <nlink>
49 function record_ino_nlink()
51 ls -li $FILES | awk '{ print $1, $3, $10}' > $1
54 # Check inode numbers match recorded inode numbers
55 function check_ino_nlink()
61 record_ino_nlink $after
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
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)"
76 # Enable overlay index feature to prevent breaking hardlinks on copy up
77 _scratch_mount -o index=on
87 echo "== Before copy up =="
89 record_ino_nlink $tmp.before
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
96 echo "== After write one =="
98 check_ino_nlink $SCRATCH_MNT $tmp.before $tmp.after_one
100 # Verify that the hardlinks survive a mount cycle
101 _scratch_cycle_mount index=on
103 echo "== After mount cycle =="
105 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_cycle
107 # Drop caches to get the copied up hardlink out of cache
108 echo 3 > /proc/sys/vm/drop_caches
110 # Modify content of the other hardlink
113 echo "== After write two =="
115 check_ino_nlink $SCRATCH_MNT $tmp.after_one $tmp.after_two