xfstests: Do not allow step to be zero
[xfstests-dev.git] / 256
1 #! /bin/bash
2 # FS QA Test No. 256
3 #
4 # Test Full File System Hole Punching
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2011 IBM Corporation.  All Rights Reserved.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #
22 #-----------------------------------------------------------------------
23 #
24 # creator
25 owner=achender@linux.vnet.ibm.com
26
27 seq=`basename $0`
28 echo "QA output created by $seq"
29
30 here=`pwd`
31 tmp=/tmp/$$
32 status=1        # failure is the default!
33
34 _cleanup()
35 {
36     rm -f $tmp.*
37 }
38
39 trap "_cleanup ; exit \$status" 0 1 2 3 15
40
41 # get standard environment, filters and checks
42 . ./common.rc
43 . ./common.filter
44 . ./common.punch
45
46 # real QA test starts here
47 _supported_fs generic
48 _supported_os Linux
49
50 _require_xfs_io_falloc_punch
51 _require_scratch
52 _require_user
53
54 testfile=$TEST_DIR/256.$$
55
56
57 # _fill_fs()
58 #
59 # Fills a file system by repeatedly creating files in the given folder
60 # starting with the given file size.  Files are reduced in size when
61 # they can no longer fit untill no more files can be created.
62 #
63 # This routine is used by _test_full_fs_punch to test that a hole may
64 # still be punched when the disk is full by borrowing reserved blocks.
65 # All files are created as a non root user to prevent reserved blocks
66 # from being consumed.
67 #
68 _fill_fs() {
69         local file_size=$1
70         local dir=$2
71         local block_size=$3
72         local file_count=1
73         local bytes_written=0
74
75         if [ $# -ne 3 ]
76         then
77                 echo "USAGE: _fill_fs filesize dir block size"
78                 exit 1
79         fi
80
81         # Creation of files or folders
82         # must not be done as root or
83         # reserved blocks will be consumed
84         _user_do "mkdir -p $dir &> /dev/null"
85         if [ $? -ne 0 ] ; then
86                 return 0
87         fi
88
89         if [ $file_size -lt $block_size ]
90         then
91                 $file_size = $block_size
92         fi
93
94         while [ $file_size -ge $block_size ]
95         do
96                 bytes_written=0
97                 _user_do "$XFS_IO_PROG -F -f -c \"pwrite 0 $file_size\" $dir/$file_count.bin &> /dev/null"
98
99                 if [ -f $dir/$file_count.bin ]
100                 then
101                         bytes_written=`$XFS_IO_PROG -F -c "stat"  $dir/$file_count.bin | grep stat.size | cut -d ' ' -f3`
102                 fi
103
104                 # If there was no room to make the file,
105                 # then divide it in half, and keep going
106                 if [ $bytes_written -lt $file_size ]
107                 then
108                         file_size=$(( $file_size / 2 ))
109                 fi
110                 file_count=$(( $file_count + 1 ))
111
112         done
113 }
114
115 # _test_full_fs_punch()
116 #
117 # This function will test that a hole may be punched
118 # even when the file system is full.  Reserved blocks
119 # should be used to allow a punch hole to proceed even
120 # when there is not enough blocks to further fragment the
121 # file. To test this, this function will fragment the file
122 # system by punching holes in regular intervals and filling
123 # the file system between punches.
124 #
125 _test_full_fs_punch()
126 {
127         local hole_len=$1      # The length of the holes to punch
128         local hole_interval=$2 # The interval between the holes
129         local iterations=$3    # The number of holes to punch
130         local file_name=$4     # File to punch holes in
131         local block_size=$5    # File system block size
132         local file_len=$(( $(( $hole_len + $hole_interval )) * $iterations ))
133         local path=`dirname $file_name`
134         local hole_offset=0
135
136         if [ $# -ne 5 ]
137         then
138                 echo "USAGE: _test_full_fs_punch hole_len hole_interval iterations file_name block_size"
139                 exit 1
140         fi
141
142         rm -f $file_name &> /dev/null
143
144         $XFS_IO_PROG -F -f -c "pwrite 0 $file_len" \
145                 -c "fsync" $file_name &> /dev/null
146         chmod 666 $file_name
147
148         _fill_fs $(( 1024 * 1024 * 1024 )) $path/fill $block_size
149
150         for (( i=0; i<$iterations; i++ ))
151         do
152                 # This part must not be done as root in order to
153                 # test that reserved blocks are used when needed
154                 _user_do "$XFS_IO_PROG -F -f -c \"fpunch $hole_offset $hole_len\" $file_name"
155                 rc=$?
156                 if [ $? -ne 0 ] ; then
157                         echo Punch hole failed
158                         break
159                 fi
160
161                 hole_offset=$(( $hole_offset + $hole_len + $hole_interval ))
162
163                 _fill_fs $hole_len $path/fill.$i $block_size
164
165         done
166 }
167
168 # Make a small file system to fill
169 umount $SCRATCH_DEV &> /dev/null
170 _scratch_mkfs_sized $(( 1024 * 1024 * 1024 )) &> /dev/null
171 _scratch_mount
172 # Test must be able to write files with non-root permissions
173 chmod 777 $SCRATCH_MNT
174
175 block_size=`stat -f $SCRATCH_DEV | grep "Block size" | cut -d " " -f3`
176 _test_full_fs_punch $(( $block_size * 2 )) $block_size 500 $SCRATCH_MNT/252.$$ $block_size
177
178 status=0 ; exit