check: Remount file system if MOUNT_OPTIONS changed
[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 the XFS project quota support in the kernel is enabled.
82 #
83 _require_prjquota()
84 {
85     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
86     src/feature -p $_dev
87     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
88     if [ "$USE_EXTERNAL" = yes -a ! -z "$_dev" ]; then
89         _notrun "Project quotas not supported on realtime filesystem"
90     fi
91 }
92
93 #
94 # checks for user nobody in /etc/passwd and /etc/group.
95 #
96 _require_nobody()
97 {
98     _cat_passwd | grep -q '^nobody'
99     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
100
101     _cat_group | egrep -q '^no(body|group)'
102     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
103 }
104
105 # create a file as a specific user (uid)
106 # takes filename, id, type (u/g/p), blocksize, blockcount
107 #
108 _file_as_id()
109 {
110     [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
111
112     parent=`dirname $1`
113     if [ $3 = p ]; then
114         echo PARENT: xfs_io -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
115         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
116         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
117     elif [ $3 = u ]; then
118         magik='$>'      # perlspeak for effective uid
119     elif [ $3 = g ]; then
120         magik='$)'      # perlspeak for effective gid
121     else
122         _notrun "broken type in call to _file_as_id in test $seq"
123     fi
124
125     perl <<EOF >>$seqres.full 2>&1
126         \$| = 1;
127         $magik = $2;
128         if ($5 == 0) {
129             print "touch $1";
130             exec "touch $1";
131         } else {
132             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
133             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
134         }
135 EOF
136 # for debugging the above euid change, try... [need write in cwd]
137 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
138
139     if [ $3 = p ]; then
140         echo PARENT: xfs_io -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
141         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
142     fi
143 }
144
145 _choose_uid()
146 {
147     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
148 }
149
150 _choose_gid()
151 {
152     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
153 }
154
155 _choose_prid()
156 {
157     if [ "X$projid_file" == "X" ]; then
158         projid_file=/etc/projid
159     fi
160     if [ ! -f $projid_file ]; then
161         echo 0
162         return
163     fi
164     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
165         $projid_file
166 }
167
168 _qmount()
169 {
170     umount $SCRATCH_DEV >/dev/null 2>&1
171     _scratch_mount || _fail "qmount failed"
172     chmod ugo+rwx $SCRATCH_MNT
173 }
174
175 #
176 # Ensures only the given quota mount option is used
177 #
178 _qmount_option()
179 {
180         # Replace any user defined quota options
181         # with the quota option that we want.
182         # Simplest to do this rather than delete existing ones first because
183         # of the variety of commas and spaces and multiple -o's
184         # that we'd have to cater for. Doesn't matter if we have duplicates.
185         # Use "QUOTA" string so that we don't have any substring confusion
186         # thanks to "quota" which will match with "uquota" and "gquota" etc.
187         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
188         | sed   -e 's/uquota/QUOTA/g'      \
189                 -e 's/usrquota/QUOTA/g'    \
190                 -e 's/gquota/QUOTA/g'      \
191                 -e 's/grpquota/QUOTA/g'    \
192                 -e 's/pquota/QUOTA/g'      \
193                 -e 's/quota/QUOTA/g'       \
194                 -e 's/uqnoenforce/QUOTA/g' \
195                 -e 's/gqnoenforce/QUOTA/g' \
196                 -e 's/pqnoenforce/QUOTA/g' \
197                 -e 's/qnoenforce/QUOTA/g'  \
198                 -e "s/QUOTA/$1/g"`
199
200         # Ensure we have the given quota option - duplicates are fine
201         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $1"
202         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
203 }
204
205 _check_quota_usage()
206 {
207         # Sync to get delalloc to disk
208         sync
209
210         # kill caches to guarantee removal speculative delalloc
211         # XXX: really need an ioctl instead of this big hammer
212         echo 3 > /proc/sys/vm/drop_caches
213
214         VFS_QUOTA=0
215         case $FSTYP in
216         ext2|ext3|ext4|ext4dev|reiserfs)
217                 VFS_QUOTA=1
218                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
219                 ;;
220         *)
221                 ;;
222         esac
223         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
224                 sort >$tmp.user.orig
225         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
226                 sort >$tmp.group.orig
227         if [ $VFS_QUOTA -eq 1 ]; then
228                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
229         else
230                 # use XFS method to force quotacheck
231                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
232                 _scratch_unmount
233                 _scratch_mount "-o usrquota,grpquota"
234         fi
235         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
236                 sort >$tmp.user.checked
237         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
238                 sort >$tmp.group.checked
239         if [ $VFS_QUOTA -eq 1 ]; then
240                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
241         fi
242         {
243                 echo "Comparing user usage"
244                 diff $tmp.user.orig $tmp.user.checked
245         } && {
246                 echo "Comparing group usage"
247                 diff $tmp.group.orig $tmp.group.checked
248         }
249 }
250
251 # make sure this script returns success
252 /bin/true