fstests: fix call sites that used xfs_io directly
[xfstests-dev.git] / common / quota
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 # All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write the Free Software Foundation,
17 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 #
19 #
20 # Functions useful for quota tests
21 #
22
23 #
24 # checks that the generic quota support in the kernel is enabled
25 # and that we have valid quota user tools installed.
26 #
27 _require_quota()
28 {
29     [ -n "$QUOTA_PROG" ] || _notrun "Quota user tools not installed"
30
31     case $FSTYP in
32     ext2|ext3|ext4|ext4dev|reiserfs)
33         if [ ! -d /proc/sys/fs/quota ]; then
34             _notrun "Installed kernel does not support quotas"
35         fi
36         ;;
37     gfs2)
38         ;;
39     xfs)
40         if [ ! -f /proc/fs/xfs/xqmstat ]; then
41             _notrun "Installed kernel does not support XFS quotas"
42         fi
43         if [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ]; then
44             _notrun "Quotas not supported on realtime test device"
45         fi
46         if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
47             _notrun "Quotas not supported on realtime scratch device"
48         fi
49         ;;
50     *)
51         _notrun "disk quotas not supported by this filesystem type: $FSTYP"
52         ;;
53     esac
54
55     # SELinux adds extra xattrs which can mess up our expected output.
56     # So, mount with a context, and they won't be created
57     # nfs_t is a "liberal" context so we can use it.
58     if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
59         export SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
60     fi
61 }
62
63 #
64 # checks that the XFS quota support in the kernel is enabled
65 # and that we have valid quota user tools installed.
66 #
67 _require_xfs_quota()
68 {
69     src/feature -q $TEST_DEV
70     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
71     if [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ]; then
72         _notrun "Quotas not supported on realtime test device"
73     fi
74     if [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ]; then
75         _notrun "Quotas not supported on realtime scratch device"
76     fi
77     [ -n "$XFS_QUOTA_PROG" ] || _notrun "XFS quota user tools not installed"
78 }
79
80 #
81 # checks that xfs_quota can operate on foreign (non-xfs) filesystems
82 # Skips check on xfs filesystems, old xfs_quota is fine there.
83 # Appends "-f" to enable foreign behavior on non-xfs filesystems if available.
84 #
85 _require_xfs_quota_foreign()
86 {
87         if [ "$FSTYP" != "xfs" ]; then
88                 $XFS_QUOTA_PROG -f -V &>/dev/null || \
89                  _notrun "xfs_quota binary does not support foreign filesystems"
90                 XFS_QUOTA_PROG="$XFS_QUOTA_PROG -f"
91         fi
92 }
93
94 #
95 # checks that the project quota support in the kernel is enabled.
96 #
97 _require_prjquota()
98 {
99     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
100     if [ "$FSTYP" == "ext4" ]; then
101         dumpe2fs -h $_dev 2>&1 | grep -qw project || \
102                 _notrun "Project quota not available on this ext4"
103     fi
104     src/feature -P $_dev
105     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
106     if [ "$USE_EXTERNAL" = yes -a ! -z "$_dev" ]; then
107         _notrun "Project quotas not supported on realtime filesystem"
108     fi
109 }
110
111 #
112 # ext4 (for now) is unique in that we must enable the project quota feature
113 # prior to mount.  This is a relatively new feature ...
114 _scratch_enable_pquota()
115 {
116         [ "$FSTYP" != "ext4" ] && return
117
118         tune2fs -O quota,project $SCRATCH_DEV >>$seqres.full 2>&1
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     _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/gquota/QUOTA/g'      \
227                 -e 's/grpquota/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"`
236
237         # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
238         # Switch it to "quota" to enable mkfs-time pquota
239         if [ "$FSTYP" == "ext4" ]; then
240                 OPTS=`echo $OPTS \
241                 | sed   -e 's/\bpquota/quota/g' \
242                         -e 's/prjquota/quota/g'`
243         fi
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
247 }
248
249 _check_quota_usage()
250 {
251         # Sync to get delalloc to disk
252         sync
253
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
257
258         VFS_QUOTA=0
259         case $FSTYP in
260         ext2|ext3|ext4|ext4dev|reiserfs|gfs2)
261                 VFS_QUOTA=1
262                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
263                 ;;
264         *)
265                 ;;
266         esac
267         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
268                 sort >$tmp.user.orig
269         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
270                 sort >$tmp.group.orig
271         if [ $VFS_QUOTA -eq 1 ]; then
272                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
273         else
274                 # use XFS method to force quotacheck
275                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
276                 _scratch_unmount
277                 _scratch_mount "-o usrquota,grpquota"
278         fi
279         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
280                 sort >$tmp.user.checked
281         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
282                 sort >$tmp.group.checked
283         if [ $VFS_QUOTA -eq 1 ]; then
284                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
285         fi
286         {
287                 echo "Comparing user usage"
288                 diff $tmp.user.orig $tmp.user.checked
289         } && {
290                 echo "Comparing group usage"
291                 diff $tmp.group.orig $tmp.group.checked
292         }
293 }
294
295 # make sure this script returns success
296 /bin/true