c48cc70b8dfec473007a3de074c5b7bcc9408d30
[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|ocfs2)
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 # Do we have GETNEXTQUOTA?  Querying ID 0 should work.
113 #
114 _require_getnextquota()
115 {
116         _require_test_program "test-nextquota"
117         $here/src/test-nextquota -i 0 -u -d $SCRATCH_DEV &> $seqres.full || \
118                 _notrun "No GETNEXTQUOTA support"
119 }
120
121 #
122 # ext4 (for now) is unique in that we must enable the project quota feature
123 # prior to mount.  This is a relatively new feature ...
124 _scratch_enable_pquota()
125 {
126         [ "$FSTYP" != "ext4" ] && return
127
128         tune2fs -O quota,project $SCRATCH_DEV >>$seqres.full 2>&1
129 }
130
131 #
132 # checks for user nobody in /etc/passwd and /etc/group.
133 #
134 _require_nobody()
135 {
136     _cat_passwd | grep -q '^nobody'
137     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
138
139     _cat_group | egrep -q '^no(body|group)'
140     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
141 }
142
143 # create a file as a specific user (uid)
144 # takes filename, id, type (u/g/p), blocksize, blockcount
145 #
146 _file_as_id()
147 {
148     [ $# != 5 ] && _fail "broken call to _file_as_id in test $seq"
149
150     parent=`dirname $1`
151     if [ $3 = p ]; then
152         echo PARENT: $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full
153         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seqres.full 2>&1
154         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
155     elif [ $3 = u ]; then
156         magik='$>'      # perlspeak for effective uid
157     elif [ $3 = g ]; then
158         magik='$)'      # perlspeak for effective gid
159     else
160         _notrun "broken type in call to _file_as_id in test $seq"
161     fi
162
163     perl <<EOF >>$seqres.full 2>&1
164         \$| = 1;
165         $magik = $2;
166         if ($5 == 0) {
167             print "touch $1";
168             exec "touch $1";
169         } else {
170             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
171             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
172         }
173 EOF
174 # for debugging the above euid change, try... [need write in cwd]
175 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seqres.full 2>&1";
176
177     if [ $3 = p ]; then
178         echo PARENT: $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full
179         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seqres.full 2>&1
180     fi
181 }
182
183 _choose_uid()
184 {
185     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
186 }
187
188 _choose_gid()
189 {
190     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
191 }
192
193 _choose_prid()
194 {
195     if [ "X$projid_file" == "X" ]; then
196         projid_file=/etc/projid
197     fi
198     if [ ! -f $projid_file ]; then
199         echo 0
200         return
201     fi
202     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
203         $projid_file
204 }
205
206 _qmount()
207 {
208     _scratch_unmount >/dev/null 2>&1
209     _scratch_mount || _fail "qmount failed"
210     # xfs doesn't need these setups and quotacheck even fails on xfs
211     # redirect the output to $seqres.full for debug purpose and ignore results
212     if [ "$FSTYP" != "xfs" ]; then
213         quotacheck -ug $SCRATCH_MNT >>$seqres.full 2>&1
214         quotaon -ug $SCRATCH_MNT >>$seqres.full 2>&1
215     fi
216     chmod ugo+rwx $SCRATCH_MNT
217 }
218
219 #
220 # Ensures only the given quota mount option is used
221 #
222 _qmount_option()
223 {
224         OPTS=$1
225
226         # Replace any user defined quota options
227         # with the quota option that we want.
228         # Simplest to do this rather than delete existing ones first because
229         # of the variety of commas and spaces and multiple -o's
230         # that we'd have to cater for. Doesn't matter if we have duplicates.
231         # Use "QUOTA" string so that we don't have any substring confusion
232         # thanks to "quota" which will match with "uquota" and "gquota" etc.
233         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
234         | sed   -e 's/uquota/QUOTA/g'      \
235                 -e 's/usrquota/QUOTA/g'    \
236                 -e 's/gquota/QUOTA/g'      \
237                 -e 's/grpquota/QUOTA/g'    \
238                 -e 's/\bpquota/QUOTA/g'    \
239                 -e 's/prjquota/QUOTA/g'    \
240                 -e 's/quota/QUOTA/g'       \
241                 -e 's/uqnoenforce/QUOTA/g' \
242                 -e 's/gqnoenforce/QUOTA/g' \
243                 -e 's/pqnoenforce/QUOTA/g' \
244                 -e 's/qnoenforce/QUOTA/g'  \
245                 -e "s/QUOTA/$OPTS/g"`
246
247         # ext4 doesn't _do_ "-o pquota/prjquota" because reasons
248         # Switch it to "quota" to enable mkfs-time pquota
249         if [ "$FSTYP" == "ext4" ]; then
250                 OPTS=`echo $OPTS \
251                 | sed   -e 's/\bpquota/quota/g' \
252                         -e 's/prjquota/quota/g'`
253         fi
254         # Ensure we have the given quota option - duplicates are fine
255         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $OPTS"
256         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
257 }
258
259 _check_quota_usage()
260 {
261         # Sync to get delalloc to disk
262         sync
263
264         # kill caches to guarantee removal speculative delalloc
265         # XXX: really need an ioctl instead of this big hammer
266         echo 3 > /proc/sys/vm/drop_caches
267
268         VFS_QUOTA=0
269         case $FSTYP in
270         ext2|ext3|ext4|ext4dev|reiserfs|gfs2)
271                 VFS_QUOTA=1
272                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
273                 ;;
274         *)
275                 ;;
276         esac
277         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
278                 sort >$tmp.user.orig
279         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
280                 sort >$tmp.group.orig
281         if [ $VFS_QUOTA -eq 1 ]; then
282                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
283         else
284                 # use XFS method to force quotacheck
285                 xfs_quota -x -c "off -ug" $SCRATCH_MNT
286                 _scratch_unmount
287                 _scratch_mount "-o usrquota,grpquota"
288         fi
289         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
290                 sort >$tmp.user.checked
291         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | _filter_scratch |
292                 sort >$tmp.group.checked
293         if [ $VFS_QUOTA -eq 1 ]; then
294                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
295         fi
296         {
297                 echo "Comparing user usage"
298                 diff $tmp.user.orig $tmp.user.checked
299         } && {
300                 echo "Comparing group usage"
301                 diff $tmp.group.orig $tmp.group.checked
302         }
303 }
304
305 # Report the block usage of root, $qa_user, and nobody
306 _report_quota_blocks() {
307         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $3, $4, $5}'
308 }
309
310 # Report the inode usage of root, $qa_user, and nobody
311 _report_quota_inodes() {
312         repquota $1 | egrep "^($qa_user|root|nobody)" | awk '{print $1, $6, $7, $8}'
313 }
314
315 # make sure this script returns success
316 /bin/true