common: convert to SPDX license tags
[xfstests-dev.git] / common / quota
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # Functions useful for quota tests
6
7 # checks that the generic quota support in the kernel is enabled
8 # and that we have valid quota user tools installed.
9 #
10 _require_quota()
11 {
12     [ -n "$QUOTA_PROG" ] || _notrun "Quota user tools not installed"
13
14     case $FSTYP in
15     ext2|ext3|ext4|ext4dev|reiserfs)
16         if [ ! -d /proc/sys/fs/quota ]; then
17             _notrun "Installed kernel does not support quotas"
18         fi
19         ;;
20     gfs2|ocfs2)
21         ;;
22     xfs)
23         if [ ! -f /proc/fs/xfs/xqmstat ]; then
24             _notrun "Installed kernel does not support XFS quotas"
25         fi
26         if [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ]; then
27             _notrun "Quotas not supported on realtime test device"
28         fi
29         if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
30             _notrun "Quotas not supported on realtime scratch device"
31         fi
32         ;;
33     *)
34         _notrun "disk quotas not supported by this filesystem type: $FSTYP"
35         ;;
36     esac
37 }
38
39 #
40 # checks that the XFS quota support in the kernel is enabled
41 # and that we have valid quota user tools installed.
42 #
43 _require_xfs_quota()
44 {
45     src/feature -q $TEST_DEV
46     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
47     if [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ]; then
48         _notrun "Quotas not supported on realtime test device"
49     fi
50     if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
51         _notrun "Quotas not supported on realtime scratch device"
52     fi
53     [ -n "$XFS_QUOTA_PROG" ] || _notrun "XFS quota user tools not installed"
54 }
55
56 #
57 # checks that xfs_quota can operate on foreign (non-xfs) filesystems
58 # Skips check on xfs filesystems, old xfs_quota is fine there.
59 # Appends "-f" to enable foreign behavior on non-xfs filesystems if available.
60 #
61 _require_xfs_quota_foreign()
62 {
63         if [ "$FSTYP" != "xfs" ]; then
64                 $XFS_QUOTA_PROG -f -V &>/dev/null || \
65                  _notrun "xfs_quota binary does not support foreign filesystems"
66                 XFS_QUOTA_PROG="$XFS_QUOTA_PROG -f"
67         fi
68 }
69
70 #
71 # checks that the project quota support in the kernel is enabled.
72 #
73 _require_prjquota()
74 {
75     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
76     if [[ "$FSTYP" == ext[234] ]]; then
77         dumpe2fs -h $_dev 2>&1 | grep -qw project || \
78                 _notrun "Project quota not available on this $FSTYP"
79     fi
80     src/feature -P $_dev
81     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
82     if [ "$USE_EXTERNAL" = yes -a ! -z "$_dev" ]; then
83         _notrun "Project quotas not supported on realtime filesystem"
84     fi
85 }
86
87 #
88 # Do we have GETNEXTQUOTA?  Querying ID 0 should work.
89 #
90 _require_getnextquota()
91 {
92         _require_test_program "test-nextquota"
93         $here/src/test-nextquota -i 0 -u -d $SCRATCH_DEV &> $seqres.full || \
94                 _notrun "No GETNEXTQUOTA support"
95 }
96
97 #
98 # ext4 (for now) is unique in that we must enable the project quota feature
99 # prior to mount.  This is a relatively new feature ...
100 _scratch_enable_pquota()
101 {
102         [[ "$FSTYP" != ext[234] ]] && return
103
104         tune2fs -O quota,project $SCRATCH_DEV >>$seqres.full 2>&1
105         _try_scratch_mount >/dev/null 2>&1 \
106                 || _notrun "kernel doesn't support project feature on $FSTYP"
107         _scratch_unmount
108 }
109
110 #
111 # checks for user nobody in /etc/passwd and /etc/group.
112 #
113 _require_nobody()
114 {
115     _cat_passwd | grep -q '^nobody'
116     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
117
118     _cat_group | egrep -q '^no(body|group)'
119     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
120 }
121
122 # create a file as a specific user (uid)
123 # takes filename, id, type (u/g/p), blocksize, blockcount
124 #
125 _file_as_id()
126 {
127     [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
128
129     parent=`dirname $1`
130     if [ $3 = p ]; then
131         echo PARENT: $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
132         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
133         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
134     elif [ $3 = u ]; then
135         magik='$>'      # perlspeak for effective uid
136     elif [ $3 = g ]; then
137         magik='$)'      # perlspeak for effective gid
138     else
139         _notrun "broken type in call to _file_as_id in test $seq"
140     fi
141
142     perl <<EOF >>$seqres.full 2>&1
143         \$| = 1;
144         $magik = $2;
145         if ($5 == 0) {
146             print "touch $1";
147             exec "touch $1";
148         } else {
149             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
150             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
151         }
152 EOF
153 # for debugging the above euid change, try... [need write in cwd]
154 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
155
156     if [ $3 = p ]; then
157         echo PARENT: $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
158         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
159     fi
160 }
161
162 _choose_uid()
163 {
164     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
165 }
166
167 _choose_gid()
168 {
169     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
170 }
171
172 _choose_prid()
173 {
174     if [ "X$projid_file" == "X" ]; then
175         projid_file=/etc/projid
176     fi
177     if [ ! -f $projid_file ]; then
178         echo 0
179         return
180     fi
181     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
182         $projid_file
183 }
184
185 _qmount()
186 {
187     _scratch_unmount >/dev/null 2>&1
188     _try_scratch_mount || _fail "qmount failed"
189     # xfs doesn't need these setups and quotacheck even fails on xfs
190     # redirect the output to $seqres.full for debug purpose and ignore results
191     if [ "$FSTYP" != "xfs" ]; then
192         quotacheck -ug $SCRATCH_MNT >>$seqres.full 2>&1
193         quotaon -ug $SCRATCH_MNT >>$seqres.full 2>&1
194     fi
195     chmod ugo+rwx $SCRATCH_MNT
196 }
197
198 #
199 # Ensures only the given quota mount option is used
200 #
201 _qmount_option()
202 {
203         OPTS=$1
204
205         # Replace any user defined quota options
206         # with the quota option that we want.
207         # Simplest to do this rather than delete existing ones first because
208         # of the variety of commas and spaces and multiple -o's
209         # that we'd have to cater for. Doesn't matter if we have duplicates.
210         # Use "QUOTA" string so that we don't have any substring confusion
211         # thanks to "quota" which will match with "uquota" and "gquota" etc.
212         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
213         | sed   -e 's/uquota/QUOTA/g'      \
214                 -e 's/usrquota/QUOTA/g'    \
215                 -e 's/usrjquota=[^, ]/QUOTA/g' \
216                 -e 's/gquota/QUOTA/g'      \
217                 -e 's/grpquota/QUOTA/g'    \
218                 -e 's/grpjquota=[^, ]/QUOTA/g' \
219                 -e 's/\bpquota/QUOTA/g'    \
220                 -e 's/prjquota/QUOTA/g'    \
221                 -e 's/quota/QUOTA/g'       \
222                 -e 's/uqnoenforce/QUOTA/g' \
223                 -e 's/gqnoenforce/QUOTA/g' \
224                 -e 's/pqnoenforce/QUOTA/g' \
225                 -e 's/qnoenforce/QUOTA/g'  \
226                 -e "s/QUOTA/$OPTS/g"`
227
228         # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
229         # Switch it to "quota" to enable mkfs-time pquota
230         if [[ "$FSTYP" == ext[234] ]]; then
231                 OPTS=`echo $OPTS \
232                 | sed   -e 's/\bpquota/quota/g' \
233                         -e 's/prjquota/quota/g'`
234         fi
235         # Ensure we have the given quota option - duplicates are fine
236         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $OPTS"
237         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
238 }
239
240 _check_quota_usage()
241 {
242         # Sync to get delalloc to disk
243         sync
244
245         # kill caches to guarantee removal speculative delalloc
246         # XXX: really need an ioctl instead of this big hammer
247         echo 3 > /proc/sys/vm/drop_caches
248
249         VFS_QUOTA=0
250         case $FSTYP in
251         ext2|ext3|ext4|ext4dev|reiserfs|gfs2)
252                 VFS_QUOTA=1
253                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
254                 ;;
255         xfs)
256                 # Clear out speculative preallocations to eliminate them
257                 # as a source of intermittent orig/checked differences.
258                 test -x "$XFS_SPACEMAN_PROG" && \
259                         "$XFS_SPACEMAN_PROG" -c 'prealloc -s' $SCRATCH_MNT
260                 ;;
261         *)
262                 ;;
263         esac
264         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
265                 sort >$tmp.user.orig
266         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
267                 sort >$tmp.group.orig
268         if [ $VFS_QUOTA -eq 1 ]; then
269                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
270         else
271                 # use XFS method to force quotacheck
272                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
273                 _scratch_unmount
274                 _scratch_mount "-o usrquota,grpquota"
275         fi
276         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
277                 sort >$tmp.user.checked
278         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
279                 sort >$tmp.group.checked
280         if [ $VFS_QUOTA -eq 1 ]; then
281                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
282         fi
283         {
284                 echo "Comparing user usage"
285                 diff $tmp.user.orig $tmp.user.checked
286         } && {
287                 echo "Comparing group usage"
288                 diff $tmp.group.orig $tmp.group.checked
289         }
290 }
291
292 # Report the block usage of root, $qa_user, and nobody
293 _report_quota_blocks() {
294         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $3, $4, $5}' | sort -r
295 }
296
297 # Report the inode usage of root, $qa_user, and nobody
298 _report_quota_inodes() {
299         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $6, $7, $8}' | sort -r
300 }
301
302 # make sure this script returns success
303 /bin/true