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