generic/223: make sure all files get created on the data device
[xfstests-dev.git] / tests / generic / 603
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 603
6 #
7 # Test per-type(user, group and project) filesystem quota timers, make sure
8 # enforcement
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13
14 here=`pwd`
15 tmp=/tmp/$$
16 status=1        # failure is the default!
17 trap "_cleanup; exit \$status" 0 1 2 3 15
18
19 _cleanup()
20 {
21         restore_project
22         cd /
23         rm -f $tmp.*
24 }
25
26 # get standard environment, filters and checks
27 . ./common/rc
28 . ./common/filter
29 . ./common/quota
30
31 # remove previous $seqres.full before test
32 rm -f $seqres.full
33
34 require_project()
35 {
36         rm -f $tmp.projects $tmp.projid
37         if [ -f /etc/projects ];then
38                 cat /etc/projects > $tmp.projects
39         fi
40         if [ -f /etc/projid ];then
41                 cat /etc/projid > $tmp.projid
42         fi
43
44         cat >/etc/projects <<EOF
45 100:$SCRATCH_MNT/t
46 EOF
47         cat >/etc/projid <<EOF
48 $qa_user:100
49 EOF
50         PROJECT_CHANGED=1
51 }
52
53 restore_project()
54 {
55         if [ "$PROJECT_CHANGED" = "1" ];then
56                 rm -f /etc/projects /etc/projid
57                 if [ -f $tmp.projects ];then
58                         cat $tmp.projects > /etc/projects
59                 fi
60                 if [ -f $tmp.projid ];then
61                         cat $tmp.projid > /etc/projid
62                 fi
63         fi
64 }
65
66 init_files()
67 {
68         local dir=$1
69
70         echo "### Initialize files, and their mode and ownership"
71         touch $dir/file{1,2} 2>/dev/null
72         chown $qa_user $dir/file{1,2} 2>/dev/null
73         chgrp $qa_user $dir/file{1,2} 2>/dev/null
74         chmod 777 $dir 2>/dev/null
75 }
76
77 cleanup_files()
78 {
79         echo "### Remove all files"
80         rm -f ${1}/file{1,2,3,4,5,6}
81 }
82
83 # When project quota is exceeded, some filesystems return ENOSPC (e.g. XFS),
84 # some filsystems return EDQUOT(e.g. ext4). The behavior isn't definitized.
85 # So filter the ENOSPC and EDQUOT output.
86 filter_enospc_edquot()
87 {
88         # The filter is only for project quota
89         if [ "$1" = "P" ];then
90                 sed -e "s,Disk quota exceeded,EDQUOT|ENOSPC,g" \
91                     -e "s,No space left on device,EDQUOT|ENOSPC,g"
92         else
93                 cat -
94         fi
95 }
96
97 test_grace()
98 {
99         local type=$1
100         local dir=$2
101         local bgrace=$3
102         local igrace=$4
103
104         init_files $dir
105         echo "--- Test block quota ---"
106         # Firstly fit below block soft limit
107         echo "Write 225 blocks..."
108         su $qa_user -c "$XFS_IO_PROG -c 'pwrite 0 $((225 * $BLOCK_SIZE))' \
109                 -c fsync $dir/file1" 2>&1 >>$seqres.full | \
110                 _filter_xfs_io_error | tee -a $seqres.full
111         repquota -v -$type $SCRATCH_MNT | grep -v "^root" >>$seqres.full 2>&1
112         # Secondly overcome block soft limit
113         echo "Rewrite 250 blocks plus 1 byte, over the block softlimit..."
114         su $qa_user -c "$XFS_IO_PROG -c 'pwrite 0 $((250 * $BLOCK_SIZE + 1))' \
115                 -c fsync $dir/file1" 2>&1 >>$seqres.full | \
116                 _filter_xfs_io_error | tee -a $seqres.full
117         repquota -v -$type $SCRATCH_MNT | grep -v "^root" >>$seqres.full 2>&1
118         # Reset grace time here, make below grace time test more accurate
119         setquota -$type $qa_user -T $bgrace $igrace $SCRATCH_MNT 2>/dev/null
120         # Now sleep enough grace time and check that softlimit got enforced
121         sleep $((bgrace + 1))
122         echo "Try to write 1 one more block after grace..."
123         su $qa_user -c "$XFS_IO_PROG -c 'truncate 0' -c 'pwrite 0 $BLOCK_SIZE' \
124                 $dir/file2" 2>&1 >>$seqres.full | _filter_xfs_io_error | \
125                 filter_enospc_edquot $type | tee -a $seqres.full
126         repquota -v -$type $SCRATCH_MNT | grep -v "^root" >>$seqres.full 2>&1
127         echo "--- Test inode quota ---"
128         # And now the softlimit test for inodes
129         # First reset space limits so that we don't have problems with
130         # space reservations on XFS
131         setquota -$type $qa_user 0 0 3 100 $SCRATCH_MNT
132         echo "Create 2 more files, over the inode softlimit..."
133         su $qa_user -c "touch $dir/file3 $dir/file4" 2>&1 >>$seqres.full | \
134                 _filter_scratch | tee -a $seqres.full
135         repquota -v -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
136         # Reset grace time here, make below grace time test more accurate
137         setquota -$type $qa_user -T $bgrace $igrace $SCRATCH_MNT 2>/dev/null
138         # Wait and check grace time enforcement
139         sleep $((igrace+1))
140         echo "Try to create one more inode after grace..."
141         su $qa_user -c "touch $dir/file5" 2>&1 >>$seqres.full | \
142                 filter_enospc_edquot $type | _filter_scratch | \
143                 tee -a $seqres.full
144         repquota -v -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
145         cleanup_files $dir
146 }
147
148 # real QA test starts here
149 _supported_fs generic
150 _require_scratch
151 # xfs requires v5 format to support all three quota types at the same time
152 if [ "$FSTYP" = "xfs" ]; then
153         _require_scratch_xfs_crc
154 fi
155 _require_setquota_project
156 _require_quota
157 _require_user
158 _require_group
159
160 _scratch_mkfs >$seqres.full 2>&1
161 _scratch_enable_pquota
162 _qmount_option "usrquota,grpquota,prjquota"
163 _qmount
164 _require_prjquota $SCRATCH_DEV
165 BLOCK_SIZE=$(_get_file_block_size $SCRATCH_MNT)
166 rm -rf $SCRATCH_MNT/t
167 mkdir $SCRATCH_MNT/t
168 $XFS_IO_PROG -r -c "chproj 100" -c "chattr +P" $SCRATCH_MNT/t
169 require_project
170
171 echo "### Set up different grace timers to each type of quota"
172 UBGRACE=12
173 UIGRACE=10
174 GBGRACE=4
175 GIGRACE=2
176 PBGRACE=8
177 PIGRACE=6
178
179 setquota -u $qa_user $((250 * $BLOCK_SIZE / 1024)) \
180         $((1000 * $BLOCK_SIZE / 1024)) 3 100 $SCRATCH_MNT
181 setquota -u -t $UBGRACE $UIGRACE $SCRATCH_MNT
182 echo; echo "### Test user quota softlimit and grace time"
183 test_grace u $SCRATCH_MNT $UBGRACE $UIGRACE
184 # Reset the user quota space & inode limits, avoid it affect later test
185 setquota -u $qa_user 0 0 0 0 $SCRATCH_MNT
186
187 setquota -g $qa_user $((250 * $BLOCK_SIZE / 1024)) \
188         $((1000 * $BLOCK_SIZE / 1024)) 3 100 $SCRATCH_MNT
189 setquota -g -t $GBGRACE $GIGRACE $SCRATCH_MNT
190 echo; echo "### Test group quota softlimit and grace time"
191 test_grace g $SCRATCH_MNT $GBGRACE $GIGRACE
192 # Reset the group quota space & inode limits, avoid it affect later test
193 setquota -g $qa_user 0 0 0 0 $SCRATCH_MNT
194
195 setquota -P $qa_user $((250 * $BLOCK_SIZE / 1024)) \
196         $((1000 * $BLOCK_SIZE / 1024)) 3 100 $SCRATCH_MNT
197 setquota -P -t $PBGRACE $PIGRACE $SCRATCH_MNT
198 echo; echo "### Test project quota softlimit and grace time"
199 test_grace P $SCRATCH_MNT/t $PBGRACE $PIGRACE
200 # Reset the project quota space & inode limits
201 setquota -P $qa_user 0 0 0 0 $SCRATCH_MNT
202
203 # success, all done
204 status=0
205 exit