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