2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. All Rights Reserved.
5 # Functions useful for quota tests
7 # checks that the generic quota support in the kernel is enabled
8 # and that we have valid quota user tools installed.
12 [ -n "$QUOTA_PROG" ] || _notrun "Quota user tools not installed"
15 ext2|ext3|ext4|ext4dev|f2fs|reiserfs)
16 if [ ! -d /proc/sys/fs/quota ]; then
17 _notrun "Installed kernel does not support quotas"
23 if [ ! -f /proc/fs/xfs/xqmstat ]; then
24 _notrun "Installed kernel does not support XFS quotas"
26 if [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ]; then
27 _notrun "Quotas not supported on realtime test device"
29 if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
30 _notrun "Quotas not supported on realtime scratch device"
34 _notrun "disk quotas not supported by this filesystem type: $FSTYP"
40 # checks that the XFS quota support in the kernel is enabled
41 # and that we have valid quota user tools installed.
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"
50 if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
51 _notrun "Quotas not supported on realtime scratch device"
53 [ -n "$XFS_QUOTA_PROG" ] || _notrun "XFS quota user tools not installed"
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.
61 _require_xfs_quota_foreign()
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"
71 # checks that the project quota support in the kernel is enabled.
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"
80 if [ "$FSTYP" == "f2fs" ]; then
81 dump.f2fs $_dev 2>&1 | grep -qw project_quota
82 [ $? -ne 0 ] && _notrun "Project quota not enabled in this device $_dev"
83 cat /sys/fs/f2fs/features/project_quota | grep -qw supported
84 [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
88 [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
89 if [ "$USE_EXTERNAL" = yes ]; then
90 if [ -n "$TEST_RTDEV" -o -n "$SCRATCH_RTDEV" ]; then
91 _notrun "Project quotas not supported on realtime filesystem"
97 # Do we have GETNEXTQUOTA? Querying ID 0 should work.
99 _require_getnextquota()
101 _require_test_program "test-nextquota"
102 $here/src/test-nextquota -i 0 -u -d $SCRATCH_DEV &> $seqres.full || \
103 _notrun "No GETNEXTQUOTA support"
107 # ext4 (for now) is unique in that we must enable the project quota feature
108 # prior to mount. This is a relatively new feature ...
109 _scratch_enable_pquota()
111 [[ "$FSTYP" != ext[234] ]] && return
113 tune2fs -O quota,project $SCRATCH_DEV >>$seqres.full 2>&1
114 _try_scratch_mount >/dev/null 2>&1 \
115 || _notrun "kernel doesn't support project feature on $FSTYP"
120 # checks for user nobody in /etc/passwd and /etc/group.
124 _cat_passwd | grep -q '^nobody'
125 [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
127 _cat_group | egrep -q '^no(body|group)'
128 [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
131 # create a file as a specific user (uid)
132 # takes filename, id, type (u/g/p), blocksize, blockcount
136 [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
140 echo PARENT: $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
141 $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
142 magik='$>' # (irrelevent, above set projid-inherit-on-parent)
143 elif [ $3 = u ]; then
144 magik='$>' # perlspeak for effective uid
145 elif [ $3 = g ]; then
146 magik='$)' # perlspeak for effective gid
148 _notrun "broken type in call to _file_as_id in test $seq"
151 perl <<EOF >>$seqres.full 2>&1
158 print "dd if=/dev/zero of=$1 bs=$4 count=$5";
159 exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
162 # for debugging the above euid change, try... [need write in cwd]
163 # exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
166 echo PARENT: $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
167 $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
173 _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
178 _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
183 if [ "X$projid_file" == "X" ]; then
184 projid_file=/etc/projid
186 if [ ! -f $projid_file ]; then
190 perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
196 _scratch_unmount >/dev/null 2>&1
197 _try_scratch_mount || _fail "qmount failed"
198 # xfs doesn't need these setups and quotacheck even fails on xfs
199 # redirect the output to $seqres.full for debug purpose and ignore results
200 if [ "$FSTYP" != "xfs" ]; then
201 quotacheck -ug $SCRATCH_MNT >>$seqres.full 2>&1
202 quotaon -ug $SCRATCH_MNT >>$seqres.full 2>&1
204 chmod ugo+rwx $SCRATCH_MNT
208 # Ensures only the given quota mount option is used
214 # Replace any user defined quota options
215 # with the quota option that we want.
216 # Simplest to do this rather than delete existing ones first because
217 # of the variety of commas and spaces and multiple -o's
218 # that we'd have to cater for. Doesn't matter if we have duplicates.
219 # Use "QUOTA" string so that we don't have any substring confusion
220 # thanks to "quota" which will match with "uquota" and "gquota" etc.
221 export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
222 | sed -e 's/uquota/QUOTA/g' \
223 -e 's/usrquota/QUOTA/g' \
224 -e 's/usrjquota=[^, ]/QUOTA/g' \
225 -e 's/gquota/QUOTA/g' \
226 -e 's/grpquota/QUOTA/g' \
227 -e 's/grpjquota=[^, ]/QUOTA/g' \
228 -e 's/\bpquota/QUOTA/g' \
229 -e 's/prjquota/QUOTA/g' \
230 -e 's/quota/QUOTA/g' \
231 -e 's/uqnoenforce/QUOTA/g' \
232 -e 's/gqnoenforce/QUOTA/g' \
233 -e 's/pqnoenforce/QUOTA/g' \
234 -e 's/qnoenforce/QUOTA/g' \
235 -e "s/QUOTA/$OPTS/g"`
237 # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
238 # Switch it to "quota" to enable mkfs-time pquota
239 if [[ "$FSTYP" == ext[234] ]]; then
241 | sed -e 's/\bpquota/quota/g' \
242 -e 's/prjquota/quota/g'`
244 # Ensure we have the given quota option - duplicates are fine
245 export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $OPTS"
246 echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
251 # Sync to get delalloc to disk
254 # kill caches to guarantee removal speculative delalloc
255 # XXX: really need an ioctl instead of this big hammer
256 echo 3 > /proc/sys/vm/drop_caches
260 ext2|ext3|ext4|ext4dev|f2fs|reiserfs|gfs2)
262 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
265 # Only way to make this reliable with cow/delalloc/speculative
266 # preallocations is to unmount and remount the whole mess...
268 _scratch_mount "-o usrquota,grpquota"
273 repquota -u -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
275 repquota -g -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
276 sort >$tmp.group.orig
277 if [ $VFS_QUOTA -eq 1 ]; then
278 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
280 # use XFS method to force quotacheck
281 xfs_quota -x -c "off -ug" $SCRATCH_MNT
283 _scratch_mount "-o usrquota,grpquota"
285 repquota -u -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
286 sort >$tmp.user.checked
287 repquota -g -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
288 sort >$tmp.group.checked
289 if [ $VFS_QUOTA -eq 1 ]; then
290 quotaon -u -g $SCRATCH_MNT 2>/dev/null
293 echo "Comparing user usage"
294 diff $tmp.user.orig $tmp.user.checked
296 echo "Comparing group usage"
297 diff $tmp.group.orig $tmp.group.checked
301 # Report the block usage of root, $qa_user, and nobody
302 _report_quota_blocks() {
303 repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $3, $4, $5}' | sort -r
306 # Report the inode usage of root, $qa_user, and nobody
307 _report_quota_inodes() {
308 repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $6, $7, $8}' | sort -r
311 # make sure this script returns success