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