de85f48c6abdd88272fe43ebf9e654b0bfbc343e
[xfstests-dev.git] / tests / xfs / 152
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 #
4 # Copyright (c) 2021 Christian Brauner <christian.brauner@ubuntu.com>
5 # All Rights Reserved.
6 #
7 # FS QA Test No. 152
8 #
9 # Exercise basic xfs_quota functionality (user/group/project quota)
10 # Use of "sync" mount option here is an attempt to get deterministic
11 # allocator behaviour.
12 #
13 . ./common/preamble
14 _begin_fstest auto quick quota idmapped
15
16 wipe_mounts()
17 {
18         umount "${SCRATCH_MNT}/idmapped" >/dev/null 2>&1
19         _scratch_unmount >/dev/null 2>&1
20 }
21
22 # Override the default cleanup function.
23 _cleanup()
24 {
25         cd /
26         wipe_mounts
27         rm -f $tmp.*
28 }
29
30 # Import common functions.
31 . ./common/filter
32 . ./common/quota
33
34 # real QA test starts here
35 _supported_fs xfs
36 _require_idmapped_mounts
37 _require_test_program "idmapped-mounts/mount-idmapped"
38 _require_scratch
39 _require_xfs_quota
40 _require_user fsgqa
41 _require_user fsgqa2
42 _require_group fsgqa
43 _require_group fsgqa2
44
45 _scratch_mkfs_xfs >>$seqres.full 2>&1
46
47 uqid=`id -u fsgqa`
48 gqid=`id -g fsgqa`
49
50 uqid2=`id -u fsgqa2`
51 gqid2=`id -g fsgqa2`
52
53 pqid=10
54 cat >$tmp.projects <<EOF
55 $pqid:$SCRATCH_MNT
56 EOF
57
58 cat >$tmp.projid <<EOF
59 root:0
60 fsgqa:$pqid
61 EOF
62
63 create_files_unmapped()
64 {
65         local bs=$1
66         local inum=$2
67
68         echo "Using type=$type id=$id" >> $seqres.full
69
70         for ((i=0; i<$((inum-1)); i++)); do
71                 _file_as_id $SCRATCH_MNT/unmapped/inode$i $id $type 1024 0
72         done
73
74         _file_as_id $SCRATCH_MNT/unmapped/block $id $type $bs 1
75 }
76
77 create_files_idmapped()
78 {
79         local bs=$1
80         local inum=$2
81
82         echo "Using type=$type id=$id2" >> $seqres.full
83
84         for ((i=0; i<$((inum-1)); i++)); do
85                 _file_as_id $SCRATCH_MNT/idmapped/inode$i $id2 $type 1024 0
86         done
87
88         _file_as_id $SCRATCH_MNT/idmapped/block $id2 $type $bs 1
89 }
90
91 clean_files()
92 {
93         rm -rf $SCRATCH_MNT/unmapped 2>/dev/null
94         rm -rf $SCRATCH_MNT/idmapped 2>/dev/null
95         rm -rf $tmp.quot 2>/dev/null
96         rm -rf $tmp.quota 2>/dev/null
97 }
98
99 filter_quot()
100 {
101         _filter_quota | grep -v "root \|\#0 " \
102                 | sed -e '/#[0-9]*/s/#[0-9]*/#ID/g'
103 }
104
105 filter_report()
106 {
107         _filter_quota | grep -v "^root \|^\#0 " \
108                 | sed -e '/^#[0-9]*/s/^#[0-9]*/#ID/g'
109 }
110
111 filter_quota()
112 {
113         _filter_quota | sed -e "/Disk quotas for/s/([0-9]*)/(ID)/g" \
114                             -e "/Disk quotas for/s/#[0-9]*/#ID/g"
115 }
116
117 filter_state()
118 {
119         _filter_quota | sed -e "s/Inode: #[0-9]* (0 blocks, 0 extents)/Inode: #[INO] (0 blocks, 0 extents)/g" \
120                             -e "s/Inode: #[0-9]* ([0-9]* blocks, [0-9]* extents)/Inode: #[INO] (X blocks, Y extents)/g" \
121                             -e "/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]//g" \
122                             -e '/max warnings:/d'
123 }
124
125 test_quot()
126 {
127         local opt="$*"
128
129         echo "checking quot command (type=$type)"
130         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
131                         -c "quot -$type $opt -bi" $SCRATCH_MNT | filter_quot
132 }
133
134 test_report()
135 {
136         local opt="$*"
137
138         echo "checking report command (type=$type)"
139         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
140                         -c "report -$type $opt -bi" \
141                         $SCRATCH_MNT | filter_report
142 }
143
144 test_quota()
145 {
146         local opt="$*"
147
148         echo "checking quota command (type=$type)"
149         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
150                         -c "quota -$type $opt -bi $id" \
151                         $SCRATCH_MNT | filter_quota
152 }
153
154 test_limit()
155 {
156         local bs=$1
157         local bh=$2
158         local is=$3
159         local ih=$4
160
161         echo "checking limit command (type=$type, bsoft=$bs, bhard=$bh, isoft=$is, ihard=$ih)"
162         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
163                         -c "limit -$type bsoft=$bs bhard=$bh fsgqa" \
164                         -c "limit -$type isoft=$is ihard=$ih fsgqa" \
165                         $SCRATCH_MNT
166
167         # let the timer day transition happen
168         sleep 2
169 }
170
171 test_timer()
172 {
173         echo "checking timer command (type=$type)"
174         # set 3days+1h for time won't become 2days soon
175         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
176                         -c "timer -$type -bi 73h" \
177                         $SCRATCH_MNT | _filter_scratch
178 }
179
180 test_disable()
181 {
182         echo "checking disable command (type=$type)"
183         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
184                         -c "disable -$type -v" \
185                         $SCRATCH_MNT | filter_state
186 }
187
188 test_enable()
189 {
190         echo "checking enable command (type=$type)"
191         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
192                         -c "enable -$type -v" $SCRATCH_MNT | filter_state
193 }
194
195 test_off()
196 {
197         echo "checking off command (type=$type)"
198         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
199                         -c "off -$type -v" $SCRATCH_MNT | _filter_scratch
200 }
201
202 test_remove()
203 {
204         echo "checking remove command (type=$type)"
205         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
206                         -c "remove -$type -v" \
207                         $SCRATCH_MNT | _filter_scratch
208 }
209
210 test_state()
211 {
212         echo "checking state command (type=$type)"
213         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
214                         -c "state -$type" $SCRATCH_MNT | filter_state
215 }
216
217 test_dump()
218 {
219         echo "checking dump command (type=$type)"
220         rm -f $tmp.backup 2>>/dev/null
221         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
222                         -c "dump -$type -f $tmp.backup" \
223                         $SCRATCH_MNT | _filter_scratch
224 }
225
226 test_restore()
227 {
228         echo "checking restore command (type=$type)"
229         $XFS_QUOTA_PROG -D $tmp.projects -P $tmp.projid -x \
230                         -c "restore -$type -f $tmp.backup" \
231                         $SCRATCH_MNT | _filter_scratch
232 }
233
234 wipe_scratch()
235 {
236         wipe_mounts
237         _scratch_mkfs_xfs >>$seqres.full 2>&1
238 }
239
240 qmount_idmapped()
241 {
242         wipe_mounts
243         _try_scratch_mount || _fail "qmount failed"
244
245         mkdir -p "${SCRATCH_MNT}/unmapped"
246         mkdir -p "${SCRATCH_MNT}/idmapped"
247
248         $here/src/idmapped-mounts/mount-idmapped \
249                 --map-mount b:$id:$id2:1 \
250                 --map-mount b:0:0:1 \
251                 "$SCRATCH_MNT/unmapped" "$SCRATCH_MNT/idmapped" || _fail "mount-idmapped failed"
252
253         chmod ugo+rwx $SCRATCH_MNT
254         chmod ugo+rwx $SCRATCH_MNT/unmapped
255         chmod ugo+rwx $SCRATCH_MNT/idmapped
256 }
257
258 test_xfs_quota()
259 {
260         # init quota
261         echo "init quota limit and timer, and dump it"
262         if [ "$idmapped" -eq 1 ]; then
263                 echo "create_files_idmapped 1024k 15"; create_files_idmapped 1024k 15
264         else
265                 echo "create_files_unmapped 1024k 15"; create_files_unmapped 1024k 15
266         fi
267         echo "quota remount"; qmount_idmapped
268         echo ; test_quot
269         echo ; test_timer
270         echo ; test_limit 512k 2048k 10 20
271         echo ; test_dump
272
273         # report options test
274         echo "report options test"
275         echo ; test_report
276         echo "-N option"; test_report -N
277         echo "-L -U options"; test_report -L $id -U $id
278         echo "-t option"; test_report -t
279         echo "-n option"; test_report -n
280         echo "-h option"; test_report -h
281
282         # quot options test
283         echo "quot options test"
284         echo ; test_quot
285         echo "-f option"; test_quot -f $tmp.quot
286         cat $tmp.quot | filter_quot
287         echo "-n option"; test_quot -n
288
289         # quota options test
290         echo ; test_quota
291         echo "-f option"; test_quota -f $tmp.quota
292         cat $tmp.quota | filter_quota
293         echo "-N option"; test_quota -N
294         echo "-n option"; test_quota -n
295         echo "-h option"; test_quota -h
296
297         # disable/enable test
298         echo "disable quota"
299         echo ; test_disable
300         echo ; test_report -N
301         echo "expect a remove error at here"; test_remove
302         echo ; test_enable
303         echo ; test_report -N
304
305         # off and remove test
306         echo "off and remove test"
307         echo ; test_limit 100m 100m 100 100
308         echo ; test_quota -N
309         echo ; test_off
310         echo ; test_state
311         echo ; test_remove
312         echo ; test_report -N
313         echo "quota remount"; qmount_idmapped
314         echo ; test_report -N
315
316         # restore test
317         echo "restore quota"
318         echo ; test_restore
319         echo ; test_report -N
320         echo ; test_state
321         echo "cleanup files"; clean_files
322 }
323
324 echo "----------------------- uquota,sync,unmapped ---------------------------"
325 wipe_scratch
326 _qmount_option "uquota,sync"
327 type=u
328 id=$uqid
329 id2=$uqid2
330 idmapped=0
331 qmount_idmapped
332 test_xfs_quota
333
334 echo "----------------------- uquota,sync,idmapped ---------------------------"
335 wipe_scratch
336 _qmount_option "uquota,sync"
337 type=u
338 id=$uqid
339 id2=$uqid2
340 idmapped=1
341 qmount_idmapped
342 test_xfs_quota
343
344 echo "----------------------- gquota,sync,unmapped ---------------------------"
345 wipe_scratch
346 _qmount_option "gquota,sync"
347 type=g
348 id=$gqid
349 id2=$gqid2
350 idmapped=0
351 qmount_idmapped
352 test_xfs_quota
353
354 echo "----------------------- gquota,sync,idmapped ---------------------------"
355 wipe_scratch
356 _qmount_option "gquota,sync"
357 type=g
358 id=$gqid
359 id2=$gqid2
360 idmapped=1
361 qmount_idmapped
362 test_xfs_quota
363
364 wipe_mounts
365
366 # success, all done
367 status=0
368 exit