btrfs: Use word mathcing for _btrfs_get_subvolid()
[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|f2fs|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     $here/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 # The device must be mounted for detection to work properly.
73 #
74 _require_prjquota()
75 {
76     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
77     if [[ "$FSTYP" == ext[234] ]]; then
78         dumpe2fs -h $_dev 2>&1 | grep -qw project || \
79                 _notrun "Project quota not available on this $FSTYP"
80     fi
81     if [ "$FSTYP" == "f2fs" ]; then
82         dump.f2fs $_dev 2>&1 | grep -qw project_quota
83         [ $? -ne 0 ] && _notrun "Project quota not enabled in this device $_dev"
84         dump.f2fs $_dev 2>&1 | grep -qw quota_ino
85         [ $? -ne 0 ] && _notrun "quota sysfile not enabled in this device $_dev"
86         cat /sys/fs/f2fs/features/project_quota | grep -qw supported
87         [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
88         return
89     fi
90     $here/src/feature -P $_dev
91     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
92     if [ "$USE_EXTERNAL" = yes ]; then
93         if [ -n "$TEST_RTDEV" -o -n "$SCRATCH_RTDEV" ]; then
94             _notrun "Project quotas not supported on realtime filesystem"
95         fi
96     fi
97 }
98
99 #
100 # Do we have GETNEXTQUOTA?  Querying ID 0 should work.
101 #
102 _require_getnextquota()
103 {
104         _require_test_program "test-nextquota"
105         $here/src/test-nextquota -i 0 -u -d $SCRATCH_DEV &> $seqres.full || \
106                 _notrun "No GETNEXTQUOTA support"
107 }
108
109 #
110 # ext4 (for now) is unique in that we must enable the project quota feature
111 # prior to mount.  This is a relatively new feature ...
112 _scratch_enable_pquota()
113 {
114         case $FSTYP in
115         ext2|ext3|ext4)
116                 tune2fs -O quota,project $SCRATCH_DEV >>$seqres.full 2>&1
117                 _try_scratch_mount >/dev/null 2>&1 \
118                         || _notrun "kernel doesn't support project feature on $FSTYP"
119                 _scratch_unmount
120                 ;;
121         f2fs)
122                 _scratch_mkfs "-O extra_attr -O quota -O project_quota" >> $seqres.full 2>&1
123                 ;;
124         esac
125 }
126
127 #
128 # checks for user nobody in /etc/passwd and /etc/group.
129 #
130 _require_nobody()
131 {
132     _cat_passwd | grep -q '^nobody'
133     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
134
135     _cat_group | egrep -q '^no(body|group)'
136     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
137 }
138
139 # create a file as a specific user (uid)
140 # takes filename, id, type (u/g/p), blocksize, blockcount
141 #
142 _file_as_id()
143 {
144     [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
145
146     parent=`dirname $1`
147     if [ $3 = p ]; then
148         echo PARENT: $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
149         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
150         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
151     elif [ $3 = u ]; then
152         magik='$>'      # perlspeak for effective uid
153     elif [ $3 = g ]; then
154         magik='$)'      # perlspeak for effective gid
155     else
156         _notrun "broken type in call to _file_as_id in test $seq"
157     fi
158
159     perl <<EOF >>$seqres.full 2>&1
160         \$| = 1;
161         $magik = $2;
162         if ($5 == 0) {
163             print "touch $1";
164             exec "touch $1";
165         } else {
166             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
167             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
168         }
169 EOF
170 # for debugging the above euid change, try... [need write in cwd]
171 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
172
173     if [ $3 = p ]; then
174         echo PARENT: $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
175         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
176     fi
177 }
178
179 _choose_uid()
180 {
181     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
182 }
183
184 _choose_gid()
185 {
186     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
187 }
188
189 _choose_prid()
190 {
191     if [ "X$projid_file" == "X" ]; then
192         projid_file=/etc/projid
193     fi
194     if [ ! -f $projid_file ]; then
195         echo 0
196         return
197     fi
198     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
199         $projid_file
200 }
201
202 _qmount()
203 {
204     _scratch_unmount >/dev/null 2>&1
205     _try_scratch_mount || _fail "qmount failed"
206     # xfs doesn't need these setups and quotacheck even fails on xfs
207     # redirect the output to $seqres.full for debug purpose and ignore results
208     if [ "$FSTYP" != "xfs" ]; then
209         quotacheck -ug $SCRATCH_MNT >>$seqres.full 2>&1
210         quotaon -ug $SCRATCH_MNT >>$seqres.full 2>&1
211     fi
212     chmod ugo+rwx $SCRATCH_MNT
213 }
214
215 #
216 # Ensures only the given quota mount option is used
217 #
218 _qmount_option()
219 {
220         OPTS=$1
221
222         # Replace any user defined quota options
223         # with the quota option that we want.
224         # Simplest to do this rather than delete existing ones first because
225         # of the variety of commas and spaces and multiple -o's
226         # that we'd have to cater for. Doesn't matter if we have duplicates.
227         # Use "QUOTA" string so that we don't have any substring confusion
228         # thanks to "quota" which will match with "uquota" and "gquota" etc.
229         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
230         | sed   -e 's/uquota/QUOTA/g'      \
231                 -e 's/usrquota/QUOTA/g'    \
232                 -e 's/usrjquota=[^, ]*/QUOTA/g' \
233                 -e 's/gquota/QUOTA/g'      \
234                 -e 's/grpquota/QUOTA/g'    \
235                 -e 's/grpjquota=[^, ]*/QUOTA/g' \
236                 -e 's/\bpquota/QUOTA/g'    \
237                 -e 's/prjquota/QUOTA/g'    \
238                 -e 's/quota/QUOTA/g'       \
239                 -e 's/uqnoenforce/QUOTA/g' \
240                 -e 's/gqnoenforce/QUOTA/g' \
241                 -e 's/pqnoenforce/QUOTA/g' \
242                 -e 's/qnoenforce/QUOTA/g'  \
243                 -e "s/QUOTA/$OPTS/g"`
244
245         # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
246         # Switch it to "quota" to enable mkfs-time pquota
247         if [[ "$FSTYP" == ext[234] ]]; then
248                 OPTS=`echo $OPTS \
249                 | sed   -e 's/\bpquota/quota/g' \
250                         -e 's/prjquota/quota/g'`
251         fi
252         # Ensure we have the given quota option - duplicates are fine
253         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $OPTS"
254         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
255 }
256
257 _check_quota_usage()
258 {
259         # Sync to get delalloc to disk
260         sync
261
262         # kill caches to guarantee removal speculative delalloc
263         # XXX: really need an ioctl instead of this big hammer
264         echo 3 > /proc/sys/vm/drop_caches
265
266         VFS_QUOTA=0
267         case $FSTYP in
268         ext2|ext3|ext4|ext4dev|f2fs|reiserfs|gfs2)
269                 VFS_QUOTA=1
270                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
271                 ;;
272         xfs)
273                 # Only way to make this reliable with cow/delalloc/speculative
274                 # preallocations is to unmount and remount the whole mess...
275                 _scratch_unmount
276                 _scratch_mount "-o usrquota,grpquota"
277                 ;;
278         *)
279                 ;;
280         esac
281         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
282                 sort >$tmp.user.orig
283         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
284                 sort >$tmp.group.orig
285         if [ $VFS_QUOTA -eq 1 ]; then
286                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
287         else
288                 # use XFS method to force quotacheck
289                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
290                 _scratch_unmount
291                 _scratch_mount "-o usrquota,grpquota"
292         fi
293         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
294                 sort >$tmp.user.checked
295         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
296                 sort >$tmp.group.checked
297         if [ $VFS_QUOTA -eq 1 ]; then
298                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
299         fi
300         {
301                 echo "Comparing user usage"
302                 diff $tmp.user.orig $tmp.user.checked
303         } && {
304                 echo "Comparing group usage"
305                 diff $tmp.group.orig $tmp.group.checked
306         }
307 }
308
309 # Report the block usage of root, $qa_user, and nobody
310 _report_quota_blocks() {
311         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $3, $4, $5}' | sort -r
312 }
313
314 # Report the inode usage of root, $qa_user, and nobody
315 _report_quota_inodes() {
316         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $6, $7, $8}' | sort -r
317 }
318
319 # make sure this script returns success
320 /bin/true