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 $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"
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.
72 # The device must be mounted for detection to work properly.
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"
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"
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"
100 # Do we have GETNEXTQUOTA? Querying ID 0 should work.
102 _require_getnextquota()
104 _require_test_program "test-nextquota"
105 $here/src/test-nextquota -i 0 -u -d $SCRATCH_DEV &> $seqres.full || \
106 _notrun "No GETNEXTQUOTA support"
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()
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"
122 _scratch_mkfs "-O extra_attr -O quota -O project_quota" >> $seqres.full 2>&1
127 _require_setquota_project()
129 setquota --help 2>&1 | \
130 grep -q "\-P, \-\-project[[:space:]]*set limits for project"
131 if [ "$?" -ne 0 ];then
132 _notrun "setquota doesn't support project quota (-P)"
137 # checks for user nobody in /etc/passwd and /etc/group.
141 _cat_passwd | grep -q '^nobody'
142 [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
144 _cat_group | egrep -q '^no(body|group)'
145 [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
148 # create a file as a specific user (uid)
149 # takes filename, id, type (u/g/p), blocksize, blockcount
153 [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
157 echo PARENT: $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
158 $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
159 magik='$>' # (irrelevent, above set projid-inherit-on-parent)
160 elif [ $3 = u ]; then
161 magik='$>' # perlspeak for effective uid
162 elif [ $3 = g ]; then
163 magik='$)' # perlspeak for effective gid
165 _notrun "broken type in call to _file_as_id in test $seq"
168 perl <<EOF >>$seqres.full 2>&1
175 print "dd if=/dev/zero of=$1 bs=$4 count=$5";
176 exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
179 # for debugging the above euid change, try... [need write in cwd]
180 # exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
183 echo PARENT: $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
184 $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
190 _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
195 _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
200 if [ "X$projid_file" == "X" ]; then
201 projid_file=/etc/projid
203 if [ ! -f $projid_file ]; then
207 perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
213 _scratch_unmount >/dev/null 2>&1
214 _try_scratch_mount || _fail "qmount failed"
215 # xfs doesn't need these setups and quotacheck even fails on xfs
216 # redirect the output to $seqres.full for debug purpose and ignore results
217 if [ "$FSTYP" != "xfs" ]; then
218 quotacheck -ug $SCRATCH_MNT >>$seqres.full 2>&1
219 quotaon -ug $SCRATCH_MNT >>$seqres.full 2>&1
220 # try to turn on project quota if it's supported
221 if quotaon --help 2>&1 | grep -q '\-\-project'; then
222 quotaon --project $SCRATCH_MNT >>$seqres.full 2>&1
225 chmod ugo+rwx $SCRATCH_MNT
229 # Ensures only the given quota mount option is used
235 # Replace any user defined quota options
236 # with the quota option that we want.
237 # Simplest to do this rather than delete existing ones first because
238 # of the variety of commas and spaces and multiple -o's
239 # that we'd have to cater for. Doesn't matter if we have duplicates.
240 # Use "QUOTA" string so that we don't have any substring confusion
241 # thanks to "quota" which will match with "uquota" and "gquota" etc.
242 export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
243 | sed -e 's/uquota/QUOTA/g' \
244 -e 's/usrquota/QUOTA/g' \
245 -e 's/usrjquota=[^, ]*/QUOTA/g' \
246 -e 's/gquota/QUOTA/g' \
247 -e 's/grpquota/QUOTA/g' \
248 -e 's/grpjquota=[^, ]*/QUOTA/g' \
249 -e 's/\bpquota/QUOTA/g' \
250 -e 's/prjquota/QUOTA/g' \
251 -e 's/quota/QUOTA/g' \
252 -e 's/uqnoenforce/QUOTA/g' \
253 -e 's/gqnoenforce/QUOTA/g' \
254 -e 's/pqnoenforce/QUOTA/g' \
255 -e 's/qnoenforce/QUOTA/g' \
256 -e "s/QUOTA/$OPTS/g"`
258 # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
259 # Switch it to "quota" to enable mkfs-time pquota
260 if [[ "$FSTYP" == ext[234] ]]; then
262 | sed -e 's/\bpquota/quota/g' \
263 -e 's/prjquota/quota/g'`
265 # Ensure we have the given quota option - duplicates are fine
266 export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $OPTS"
267 echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
272 # Sync to get delalloc to disk
275 # kill caches to guarantee removal speculative delalloc
276 # XXX: really need an ioctl instead of this big hammer
277 echo 3 > /proc/sys/vm/drop_caches
281 ext2|ext3|ext4|ext4dev|f2fs|reiserfs|gfs2|bcachefs)
283 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
286 # Only way to make this reliable with cow/delalloc/speculative
287 # preallocations is to unmount and remount the whole mess...
289 _scratch_mount "-o usrquota,grpquota"
294 repquota -u -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
296 repquota -g -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
297 sort >$tmp.group.orig
298 if [ $VFS_QUOTA -eq 1 ]; then
299 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
301 # use XFS method to force quotacheck
302 xfs_quota -x -c "off -ug" $SCRATCH_MNT
304 _scratch_mount "-o usrquota,grpquota"
306 repquota -u -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
307 sort >$tmp.user.checked
308 repquota -g -n $SCRATCH_MNT | grep -v "^#0" | _filter_scratch |
309 sort >$tmp.group.checked
310 if [ $VFS_QUOTA -eq 1 ]; then
311 quotaon -u -g $SCRATCH_MNT 2>/dev/null
314 echo "Comparing user usage"
315 diff $tmp.user.orig $tmp.user.checked
317 echo "Comparing group usage"
318 diff $tmp.group.orig $tmp.group.checked
322 # Report the block usage of root, $qa_user, and nobody
323 _report_quota_blocks() {
324 repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $3, $4, $5}' | sort -r
327 # Report the inode usage of root, $qa_user, and nobody
328 _report_quota_inodes() {
329 repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $6, $7, $8}' | sort -r
332 # Determine which type of quota we're using
337 if [ $opt = "u" -o $opt = "uno" ]; then
340 elif [ $opt = "g" -o $opt = "gno" ]; then
343 elif [ $opt = "p" -o $opt = "pno" ]; then
347 [ $opt = "u" -o $opt = "g" -o $opt = "p" ] && enforce=1
349 echo "Using type=$type id=$id" >> $seqres.full
352 # make sure this script returns success