xfstests: update README file to document some recent changes
[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     [ -x /usr/bin/quota ] || _notrun "Quota user tools not installed"
30     if [ $FSTYP = "xfs" ]; then
31         [ -f /proc/fs/xfs/xqmstat ] || _notrun "Installed kernel does not support XFS quota"
32     elif [ $FSTYP != "gfs2" ]; then
33         [ -d /proc/sys/fs/quota ] || _notrun "Installed kernel does not support quota"
34     fi
35 }
36
37 #
38 # checks that the XFS quota support in the kernel is enabled
39 # and that we have valid quota user tools installed.
40 #
41 _require_xfs_quota()
42 {
43     src/feature -q $TEST_DEV
44     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
45     [ -x /usr/sbin/xfs_quota ] || _notrun "XFS quota user tools not installed"
46 }
47
48 #
49 # checks that the XFS project quota support in the kernel is enabled.
50 #
51 _require_prjquota()
52 {
53     src/feature -p $TEST_DEV
54     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
55 }
56
57 #
58 # checks for user nobody in /etc/passwd and /etc/group.
59 #
60 _require_nobody()
61 {
62     _cat_passwd | grep -q '^nobody'
63     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
64
65     _cat_group | egrep -q '^no(body|group)'
66     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
67 }
68
69 # create a file as a specific user (uid)
70 # takes filename, id, type (u/g/p), blocksize, blockcount
71 #
72 _file_as_id()
73 {
74     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
75
76     parent=`dirname $1`
77     if [ $3 = p ]; then
78         echo PARENT: xfs_io -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full
79         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full 2>&1
80         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
81     elif [ $3 = u ]; then
82         magik='$>'      # perlspeak for effective uid
83     elif [ $3 = g ]; then
84         magik='$)'      # perlspeak for effective gid
85     else
86         _notrun "broken type in call to _file_as_id in test $seq"
87     fi
88
89     perl <<EOF >>$seq.full 2>&1
90         \$| = 1;
91         $magik = $2;
92         if ($5 == 0) {
93             print "touch $1";
94             exec "touch $1";
95         } else {
96             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
97             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
98         }
99 EOF
100 # for debugging the above euid change, try... [need write in cwd]
101 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
102
103     if [ $3 = p ]; then
104         echo PARENT: xfs_io -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full
105         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full 2>&1
106     fi
107 }
108
109 _choose_uid()
110 {
111     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
112 }
113
114 _choose_gid()
115 {
116     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
117 }
118
119 _choose_prid()
120 {
121     if [ "X$projid_file" == "X" ]; then
122         projid_file=/etc/projid
123     fi
124     if [ ! -f $projid_file ]; then
125         echo 0
126         return
127     fi
128     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
129         $projid_file
130 }
131
132 _qmount()
133 {
134     umount $SCRATCH_DEV >/dev/null 2>&1
135     _scratch_mount || _fail "qmount failed"
136     chmod ugo+rwx $SCRATCH_MNT
137 }
138
139 _qsetup()
140 {
141     # setup exactly what it is we'll be testing
142     enforce=1
143     if src/feature -u $SCRATCH_DEV
144     then
145         type=u ;
146         eval `_choose_uid`
147         [ ! -f $seq.out ] && ln -s $seq.usrquota $seq.out
148     elif src/feature -g $SCRATCH_DEV
149     then
150         type=g
151         eval `_choose_gid`
152         [ ! -f $seq.out ] && ln -s $seq.grpquota $seq.out
153     elif src/feature -p $SCRATCH_DEV
154     then
155         type=p
156         eval `_choose_prid`
157         [ ! -f $seq.out ] && ln -s $seq.prjquota $seq.out
158     elif src/feature -U $SCRATCH_DEV
159     then
160         type=u
161         eval `_choose_uid`
162         [ ! -f $seq.out ] && ln -s $seq.uqnoenforce $seq.out
163         enforce=0
164     elif src/feature -G $SCRATCH_DEV
165     then
166         type=g
167         eval `_choose_gid`
168         [ ! -f $seq.out ] && ln -s $seq.gqnoenforce $seq.out
169         enforce=0
170     elif src/feature -P $SCRATCH_DEV
171     then
172         type=p
173         eval `_choose_prid`
174         [ ! -f $seq.out ] && ln -s $seq.pqnoenforce $seq.out
175         enforce=0
176     else
177         _notrun "No quota support at mount time"
178     fi
179
180     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
181     echo "and using type=$type id=$id" >>$seq.full
182 }
183
184 #
185 # Ensures only the given quota mount option is used
186 #
187 _qmount_option()
188 {
189         # Replace any user defined quota options
190         # with the quota option that we want.
191         # Simplest to do this rather than delete existing ones first because
192         # of the variety of commas and spaces and multiple -o's
193         # that we'd have to cater for. Doesn't matter if we have duplicates.
194         # Use "QUOTA" string so that we don't have any substring confusion
195         # thanks to "quota" which will match with "uquota" and "gquota" etc.
196         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
197         | sed   -e 's/uquota/QUOTA/g'      \
198                 -e 's/usrquota/QUOTA/g'    \
199                 -e 's/gquota/QUOTA/g'      \
200                 -e 's/grpquota/QUOTA/g'    \
201                 -e 's/pquota/QUOTA/g'      \
202                 -e 's/quota/QUOTA/g'       \
203                 -e 's/uqnoenforce/QUOTA/g' \
204                 -e 's/gqnoenforce/QUOTA/g' \
205                 -e 's/pqnoenforce/QUOTA/g' \
206                 -e 's/qnoenforce/QUOTA/g'  \
207                 -e "s/QUOTA/$1/g"`
208
209         # Ensure we have the given quota option - duplicates are fine
210         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $1"
211         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seq.full
212 }
213
214 _check_quota_usage()
215 {
216         # Sync to get delalloc to disk
217         sync
218         VFS_QUOTA=0
219         if [ $FSTYP = "ext2" -o $FSTYP = "ext3" -o $FSTYP = "ext4" -o $FSTYP = "reiserfs" ]; then
220                 VFS_QUOTA=1
221                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
222         fi
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                 mount -o remount,noquota $SCRATCH_DEV
232                 mount -o remount,usrquota,grpquota $SCRATCH_DEV
233         fi
234         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
235                 sort >$tmp.user.checked
236         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
237                 sort >$tmp.group.checked
238         if [ $VFS_QUOTA -eq 1 ]; then
239                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
240         fi
241         {
242                 echo "Comparing user usage"
243                 diff $tmp.user.orig $tmp.user.checked
244         } && {
245                 echo "Comparing group usage"
246                 diff $tmp.group.orig $tmp.group.checked
247         }
248 }
249
250 # make sure this script returns success
251 /bin/true