overlay: correct scratch dirs check
[xfstests-dev.git] / tests / overlay / 036
1 #! /bin/bash
2 # FS QA Test 036
3 #
4 # Test mount error cases with exclusive directories
5 #
6 # Overlayfs is often used to mount several mounts that share a single
7 # lower dir, but every overlayfs mount should have its own private
8 # upperdir and private workdir.
9 #
10 # Overlayfs mount on kernel <= v4.12 does not check if upper/work dirs
11 # are currently in-use by another overlayfs mount on the system and bad
12 # things can happen with such configuration.
13 #
14 # Commit 2cac0c00a6cd ("ovl: get exclusive ownership on upper/work dirs")
15 # in kernel v4.13 enforces upper/work dir in-use for any overlayfs mount.
16 # Later stable fix commit 85fdee1eef1a ("ovl: fix regression caused by
17 # exclusive upper/work dir protection") relaxes mount failure to a warning
18 # for index=off mount and fails mount with EBUSY for index=on mount.
19 #
20 # This test expects success with index=off and EBUSY with index=on
21 # when trying to mount overlay with:
22 # - Upper dir is in-use by another overlay mount
23 # - Work dir is in-use by another overlay mount
24 #
25 #-----------------------------------------------------------------------
26 # Copyright (C) 2017 CTERA Networks. All Rights Reserved.
27 # Author: Amir Goldstein <amir73il@gmail.com>
28 #
29 # This program is free software; you can redistribute it and/or
30 # modify it under the terms of the GNU General Public License as
31 # published by the Free Software Foundation.
32 #
33 # This program is distributed in the hope that it would be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 # GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write the Free Software Foundation,
40 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
41 #-----------------------------------------------------------------------
42 #
43
44 seq=`basename $0`
45 seqres=$RESULT_DIR/$seq
46 echo "QA output created by $seq"
47
48 here=`pwd`
49 tmp=/tmp/$$
50 status=1        # failure is the default!
51 trap "_cleanup; exit \$status" 0 1 2 3 15
52
53 _cleanup()
54 {
55         cd /
56         rm -f $tmp.*
57         # unmount the two extra mounts in case they did not fail
58         $UMOUNT_PROG $SCRATCH_MNT 2>/dev/null
59         $UMOUNT_PROG $SCRATCH_MNT 2>/dev/null
60 }
61
62 # get standard environment, filters and checks
63 . ./common/rc
64 . ./common/filter
65
66 # remove previous $seqres.full before test
67 rm -f $seqres.full
68
69 # real QA test starts here
70 _supported_fs overlay
71 _supported_os Linux
72 # Use non-default scratch underlying overlay dirs, we need to check
73 # them explicity after test.
74 _require_scratch_nocheck
75 _require_scratch_feature index
76
77 # Remove all files from previous tests
78 _scratch_mkfs
79
80 # Create multiple lowerdirs, upperdirs and workdirs
81 lowerdir=$OVL_BASE_SCRATCH_MNT/lower
82 lowerdir2=$OVL_BASE_SCRATCH_MNT/lower2
83 upperdir=$OVL_BASE_SCRATCH_MNT/upper
84 upperdir2=$OVL_BASE_SCRATCH_MNT/upper2
85 workdir=$OVL_BASE_SCRATCH_MNT/workdir
86 workdir2=$OVL_BASE_SCRATCH_MNT/workdir2
87 mkdir -p $lowerdir $lowerdir2 $upperdir $upperdir2 $workdir $workdir2
88
89 # Mount overlay with lowerdir, upperdir, workdir
90 _overlay_mount_dirs $lowerdir $upperdir $workdir \
91                     overlay $SCRATCH_MNT
92
93 # Try to mount another overlay with the same upperdir
94 # with index=off - expect success
95 _overlay_mount_dirs $lowerdir $upperdir $workdir2 \
96                     overlay0 $SCRATCH_MNT -oindex=off && \
97                     $UMOUNT_PROG $SCRATCH_MNT
98
99 # Try to mount another overlay with the same workdir
100 # with index=off - expect success
101 _overlay_mount_dirs $lowerdir2 $upperdir2 $workdir \
102                     overlay1 $SCRATCH_MNT -oindex=off && \
103                     $UMOUNT_PROG $SCRATCH_MNT
104
105 # Try to mount another overlay with the same upperdir
106 # with index=on - expect EBUSY
107 _overlay_mount_dirs $lowerdir $upperdir $workdir2 \
108             overlay2 $SCRATCH_MNT -oindex=on 2>&1 | _filter_busy_mount
109
110 # Try to mount another overlay with the same workdir
111 # with index=on - expect EBUSY
112 _overlay_mount_dirs $lowerdir2 $upperdir2 $workdir \
113             overlay3 $SCRATCH_MNT -oindex=on 2>&1 | _filter_busy_mount
114
115 # check overlayfs
116 _overlay_check_scratch_dirs $lowerdir $upperdir $workdir
117
118 # success, all done
119 status=0
120 exit