common/rc: Add _require_{chown,chmod}()
[xfstests-dev.git] / tests / generic / 230
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2010 Jan Kara.  All Rights Reserved.
4 # Copyright (c) 2005 Silicon Graphics, Inc.  All Rights Reserved.
5 #
6 # FS QA Test No. 230
7 #
8 # Simple quota enforcement test.
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13
14 here=`pwd`
15 tmp=/tmp/$$
16 status=1        # failure is the default!
17 trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
18
19 # get standard environment, filters and checks
20 . ./common/rc
21 . ./common/filter
22 . ./common/quota
23
24 # real QA test starts here
25 _supported_fs generic
26 _require_scratch
27 _require_quota
28 _require_user
29
30 test_files()
31 {
32         echo; echo "### create files, setting up ownership (type=$type)"
33         touch $SCRATCH_MNT/file{1,2} 2>/dev/null
34         chown $qa_user $SCRATCH_MNT/file{1,2} 2>/dev/null
35         chgrp $qa_user $SCRATCH_MNT/file{1,2} 2>/dev/null
36         chmod 777 $SCRATCH_MNT 2>/dev/null
37 }
38
39 test_enforcement()
40 {
41         echo "### some buffered IO (type=$type)"
42         echo "--- initiating IO..." >>$seqres.full
43         # Firstly fit below block soft limit
44         echo "Write 225 blocks..."
45         su $qa_user -c "$XFS_IO_PROG -c 'pwrite 0 $((225 * $BLOCK_SIZE))' -c fsync \
46                 $SCRATCH_MNT/file1" 2>&1 >>$seqres.full | \
47                 _filter_xfs_io_error | tee -a $seqres.full
48         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
49         # Secondly overcome block soft limit
50         echo "Rewrite 250 blocks plus 1 byte..."
51         su $qa_user -c "$XFS_IO_PROG -c 'pwrite 0 $((250 * $BLOCK_SIZE + 1))' -c fsync \
52                 $SCRATCH_MNT/file1" 2>&1 >>$seqres.full | \
53                 _filter_xfs_io_error | tee -a $seqres.full
54         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
55         # Now try to overcome block hardlimit
56         # Reset grace time here due to the repquota will invoke Q_SYNC on
57         # old quota which may be time consuming on the low speed or busy
58         # scratch device that may leed to grace time exceed.
59         setquota -$type $qa_user -T $grace $grace $SCRATCH_MNT 2>/dev/null
60         echo "Write 250 blocks..."
61         su $qa_user -c "$XFS_IO_PROG -c 'pwrite 0 $((250 * $BLOCK_SIZE))' -c fsync \
62                 $SCRATCH_MNT/file2" 2>&1 >>$seqres.full | \
63                 _filter_xfs_io_error | tee -a $seqres.full
64         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
65         # Now sleep for grace time and check that softlimit got enforced
66         sleep $((grace+1))
67         echo "Write 1 block..."
68         su $qa_user -c "$XFS_IO_PROG -c 'truncate 0' -c 'pwrite 0 $BLOCK_SIZE' \
69                 $SCRATCH_MNT/file2" 2>&1 >>$seqres.full | \
70                 _filter_xfs_io_error | tee -a $seqres.full
71         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
72         # And now the softlimit test for inodes
73         # First reset space limits so that we don't have problems with
74         # space reservations on XFS
75         setquota -$type $qa_user 0 0 3 5 $SCRATCH_MNT
76         echo "Touch 3+4"
77         su $qa_user -c "touch $SCRATCH_MNT/file3 $SCRATCH_MNT/file4" \
78                 2>&1 >>$seqres.full | _filter_scratch | tee -a $seqres.full
79         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
80         # Try to exceed inode hardlimit
81         # Reset grace time here due to the repquota will invoke Q_SYNC on
82         # old quota which may be time consuming on the low speed or busy
83         # scratch device that may leed to grace time exceed.
84         setquota -$type $qa_user -T $grace $grace $SCRATCH_MNT 2>/dev/null
85         echo "Touch 5+6"
86         su $qa_user -c "touch $SCRATCH_MNT/file5 $SCRATCH_MNT/file6" \
87                 2>&1 >>$seqres.full | _filter_scratch | tee -a $seqres.full
88         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
89         # Wait and check grace time enforcement
90         rm -f $SCRATCH_MNT/file5 >>$seqres.full 2>&1
91         sleep $((grace+1))
92         echo "Touch 5"
93         su $qa_user -c "touch $SCRATCH_MNT/file5" 2>&1 >>$seqres.full |
94                 _filter_scratch | tee -a $seqres.full
95         repquota -$type $SCRATCH_MNT  | grep -v "^root" >>$seqres.full 2>&1
96         echo "--- completed IO ($type)" >>$seqres.full
97 }
98
99 cleanup_files()
100 {
101         rm -f $SCRATCH_MNT/file{1,2,3,4,5,6}
102 }
103
104 # real QA test starts here
105 rm -f $seqres.full
106
107 grace=2
108
109 # Test only the quota type specified in each _scratch_mount clause below.
110 _qmount_option 'defaults'
111
112 _scratch_mkfs >> $seqres.full 2>&1
113 _scratch_mount "-o usrquota,grpquota"
114 BLOCK_SIZE=$(_get_file_block_size $SCRATCH_MNT)
115 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
116 quotaon $SCRATCH_MNT 2>/dev/null
117 setquota -u $qa_user $((250 * $BLOCK_SIZE / 1024)) \
118          $((500 * $BLOCK_SIZE / 1024)) 3 5 $SCRATCH_MNT
119 setquota -u -t $grace $grace $SCRATCH_MNT
120 setquota -g $qa_user $((250 * $BLOCK_SIZE / 1024)) \
121          $((500 * $BLOCK_SIZE / 1024)) 3 5 $SCRATCH_MNT
122 setquota -g -t $grace $grace $SCRATCH_MNT
123 _scratch_unmount
124
125 echo; echo "### test user limit enforcement"
126 _scratch_mount "-o usrquota"
127 quotaon $SCRATCH_MNT 2>/dev/null
128 type=u
129 test_files
130 test_enforcement
131 cleanup_files
132 _scratch_unmount 2>/dev/null
133
134 echo; echo "### test group limit enforcement"
135 _scratch_mount "-o grpquota"
136 quotaon $SCRATCH_MNT 2>/dev/null
137 type=g
138 test_files
139 test_enforcement
140 cleanup_files
141 _scratch_unmount 2>/dev/null
142
143 status=0
144 exit