Merge branch 'master' of git://git.kernel.org/pub/scm/fs/xfs/xfstests-dev
[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         ;;
44     *)
45         _notrun "disk quotas not supported by this filesystem type: $FSTYP"
46         ;;
47     esac
48
49     # SELinux adds extra xattrs which can mess up our expected output.
50     # So, mount with a context, and they won't be created
51     # nfs_t is a "liberal" context so we can use it.
52     if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
53         export SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
54     fi
55 }
56
57 #
58 # checks that the XFS quota support in the kernel is enabled
59 # and that we have valid quota user tools installed.
60 #
61 _require_xfs_quota()
62 {
63     src/feature -q $TEST_DEV
64     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
65     [ -n $XFS_QUOTA_PROG ] || _notrun "XFS quota user tools not installed"
66 }
67
68 #
69 # checks that the XFS project quota support in the kernel is enabled.
70 #
71 _require_prjquota()
72 {
73     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
74     src/feature -p $_dev
75     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
76 }
77
78 #
79 # checks for user nobody in /etc/passwd and /etc/group.
80 #
81 _require_nobody()
82 {
83     _cat_passwd | grep -q '^nobody'
84     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
85
86     _cat_group | egrep -q '^no(body|group)'
87     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
88 }
89
90 # create a file as a specific user (uid)
91 # takes filename, id, type (u/g/p), blocksize, blockcount
92 #
93 _file_as_id()
94 {
95     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
96
97     parent=`dirname $1`
98     if [ $3 = p ]; then
99         echo PARENT: xfs_io -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full
100         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full 2>&1
101         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
102     elif [ $3 = u ]; then
103         magik='$>'      # perlspeak for effective uid
104     elif [ $3 = g ]; then
105         magik='$)'      # perlspeak for effective gid
106     else
107         _notrun "broken type in call to _file_as_id in test $seq"
108     fi
109
110     perl <<EOF >>$seq.full 2>&1
111         \$| = 1;
112         $magik = $2;
113         if ($5 == 0) {
114             print "touch $1";
115             exec "touch $1";
116         } else {
117             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
118             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
119         }
120 EOF
121 # for debugging the above euid change, try... [need write in cwd]
122 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
123
124     if [ $3 = p ]; then
125         echo PARENT: xfs_io -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full
126         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full 2>&1
127     fi
128 }
129
130 _choose_uid()
131 {
132     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
133 }
134
135 _choose_gid()
136 {
137     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
138 }
139
140 _choose_prid()
141 {
142     if [ "X$projid_file" == "X" ]; then
143         projid_file=/etc/projid
144     fi
145     if [ ! -f $projid_file ]; then
146         echo 0
147         return
148     fi
149     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
150         $projid_file
151 }
152
153 _qmount()
154 {
155     umount $SCRATCH_DEV >/dev/null 2>&1
156     _scratch_mount || _fail "qmount failed"
157     chmod ugo+rwx $SCRATCH_MNT
158 }
159
160 _qsetup()
161 {
162     # setup exactly what it is we'll be testing
163     enforce=1
164     if src/feature -u $SCRATCH_DEV
165     then
166         type=u ;
167         eval `_choose_uid`
168         [ ! -f $seq.out ] && ln -s $seq.usrquota $seq.out
169     elif src/feature -g $SCRATCH_DEV
170     then
171         type=g
172         eval `_choose_gid`
173         [ ! -f $seq.out ] && ln -s $seq.grpquota $seq.out
174     elif src/feature -p $SCRATCH_DEV
175     then
176         type=p
177         eval `_choose_prid`
178         [ ! -f $seq.out ] && ln -s $seq.prjquota $seq.out
179     elif src/feature -U $SCRATCH_DEV
180     then
181         type=u
182         eval `_choose_uid`
183         [ ! -f $seq.out ] && ln -s $seq.uqnoenforce $seq.out
184         enforce=0
185     elif src/feature -G $SCRATCH_DEV
186     then
187         type=g
188         eval `_choose_gid`
189         [ ! -f $seq.out ] && ln -s $seq.gqnoenforce $seq.out
190         enforce=0
191     elif src/feature -P $SCRATCH_DEV
192     then
193         type=p
194         eval `_choose_prid`
195         [ ! -f $seq.out ] && ln -s $seq.pqnoenforce $seq.out
196         enforce=0
197     else
198         _notrun "No quota support at mount time"
199     fi
200
201     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
202     echo "and using type=$type id=$id" >>$seq.full
203 }
204
205 #
206 # Ensures only the given quota mount option is used
207 #
208 _qmount_option()
209 {
210         # Replace any user defined quota options
211         # with the quota option that we want.
212         # Simplest to do this rather than delete existing ones first because
213         # of the variety of commas and spaces and multiple -o's
214         # that we'd have to cater for. Doesn't matter if we have duplicates.
215         # Use "QUOTA" string so that we don't have any substring confusion
216         # thanks to "quota" which will match with "uquota" and "gquota" etc.
217         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
218         | sed   -e 's/uquota/QUOTA/g'      \
219                 -e 's/usrquota/QUOTA/g'    \
220                 -e 's/gquota/QUOTA/g'      \
221                 -e 's/grpquota/QUOTA/g'    \
222                 -e 's/pquota/QUOTA/g'      \
223                 -e 's/quota/QUOTA/g'       \
224                 -e 's/uqnoenforce/QUOTA/g' \
225                 -e 's/gqnoenforce/QUOTA/g' \
226                 -e 's/pqnoenforce/QUOTA/g' \
227                 -e 's/qnoenforce/QUOTA/g'  \
228                 -e "s/QUOTA/$1/g"`
229
230         # Ensure we have the given quota option - duplicates are fine
231         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $1"
232         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seq.full
233 }
234
235 _check_quota_usage()
236 {
237         # Sync to get delalloc to disk
238         sync
239
240         # kill caches to guarantee removal speculative delalloc
241         # XXX: really need an ioctl instead of this big hammer
242         echo 3 > /proc/sys/vm/drop_caches
243
244         VFS_QUOTA=0
245         case $FSTYP in
246         ext2|ext3|ext4|ext4dev|reiserfs)
247                 VFS_QUOTA=1
248                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
249                 ;;
250         *)
251                 ;;
252         esac
253         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
254                 sort >$tmp.user.orig
255         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
256                 sort >$tmp.group.orig
257         if [ $VFS_QUOTA -eq 1 ]; then
258                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
259         else
260                 # use XFS method to force quotacheck
261                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
262                 _scratch_unmount
263                 _scratch_mount "-o usrquota,grpquota"
264         fi
265         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
266                 sort >$tmp.user.checked
267         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
268                 sort >$tmp.group.checked
269         if [ $VFS_QUOTA -eq 1 ]; then
270                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
271         fi
272         {
273                 echo "Comparing user usage"
274                 diff $tmp.user.orig $tmp.user.checked
275         } && {
276                 echo "Comparing group usage"
277                 diff $tmp.group.orig $tmp.group.checked
278         }
279 }
280
281 # make sure this script returns success
282 /bin/true