xfstests: remove stale machine configs
[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
25 seq=`basename $0`
26 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=/tmp/$$
30 status=1        # failure is the default!
31
32 _cleanup()
33 {
34     rm -f $tmp.*
35 }
36
37 trap "_cleanup ; exit \$status" 0 1 2 3 15
38
39 # get standard environment, filters and checks
40 . ./common.rc
41 . ./common.filter
42 . ./common.punch
43
44 # real QA test starts here
45 _supported_fs generic
46 _supported_os Linux
47
48 _require_xfs_io_falloc_punch
49 _require_scratch
50 _require_user
51
52 testfile=$TEST_DIR/256.$$
53
54
55 # _fill_fs()
56 #
57 # Fills a file system by repeatedly creating files in the given folder
58 # starting with the given file size.  Files are reduced in size when
59 # they can no longer fit untill no more files can be created.
60 #
61 # This routine is used by _test_full_fs_punch to test that a hole may
62 # still be punched when the disk is full by borrowing reserved blocks.
63 # All files are created as a non root user to prevent reserved blocks
64 # from being consumed.
65 #
66 _fill_fs() {
67         local file_size=$1
68         local dir=$2
69         local block_size=$3
70         local file_count=1
71         local bytes_written=0
72
73         if [ $# -ne 3 ]
74         then
75                 echo "USAGE: _fill_fs filesize dir block size"
76                 exit 1
77         fi
78
79         # Creation of files or folders
80         # must not be done as root or
81         # reserved blocks will be consumed
82         _user_do "mkdir -p $dir &> /dev/null"
83         if [ $? -ne 0 ] ; then
84                 return 0
85         fi
86
87         if [ $file_size -lt $block_size ]
88         then
89                 $file_size = $block_size
90         fi
91
92         while [ $file_size -ge $block_size ]
93         do
94                 bytes_written=0
95                 _user_do "$XFS_IO_PROG -F -f -c \"pwrite 0 $file_size\" $dir/$file_count.bin &> /dev/null"
96
97                 if [ -f $dir/$file_count.bin ]
98                 then
99                         bytes_written=`$XFS_IO_PROG -F -c "stat"  $dir/$file_count.bin | grep stat.size | cut -d ' ' -f3`
100                 fi
101
102                 # If there was no room to make the file,
103                 # then divide it in half, and keep going
104                 if [ $bytes_written -lt $file_size ]
105                 then
106                         file_size=$(( $file_size / 2 ))
107                 fi
108                 file_count=$(( $file_count + 1 ))
109
110         done
111 }
112
113 # _test_full_fs_punch()
114 #
115 # This function will test that a hole may be punched
116 # even when the file system is full.  Reserved blocks
117 # should be used to allow a punch hole to proceed even
118 # when there is not enough blocks to further fragment the
119 # file. To test this, this function will fragment the file
120 # system by punching holes in regular intervals and filling
121 # the file system between punches.
122 #
123 _test_full_fs_punch()
124 {
125         local hole_len=$1      # The length of the holes to punch
126         local hole_interval=$2 # The interval between the holes
127         local iterations=$3    # The number of holes to punch
128         local file_name=$4     # File to punch holes in
129         local block_size=$5    # File system block size
130         local file_len=$(( $(( $hole_len + $hole_interval )) * $iterations ))
131         local path=`dirname $file_name`
132         local hole_offset=0
133
134         if [ $# -ne 5 ]
135         then
136                 echo "USAGE: _test_full_fs_punch hole_len hole_interval iterations file_name block_size"
137                 exit 1
138         fi
139
140         rm -f $file_name &> /dev/null
141
142         $XFS_IO_PROG -F -f -c "pwrite 0 $file_len" \
143                 -c "fsync" $file_name &> /dev/null
144         chmod 666 $file_name
145
146         _fill_fs $(( 1024 * 1024 * 1024 )) $path/fill $block_size
147
148         for (( i=0; i<$iterations; i++ ))
149         do
150                 # This part must not be done as root in order to
151                 # test that reserved blocks are used when needed
152                 _user_do "$XFS_IO_PROG -F -f -c \"fpunch $hole_offset $hole_len\" $file_name"
153                 rc=$?
154                 if [ $? -ne 0 ] ; then
155                         echo Punch hole failed
156                         break
157                 fi
158
159                 hole_offset=$(( $hole_offset + $hole_len + $hole_interval ))
160
161                 _fill_fs $hole_len $path/fill.$i $block_size
162
163         done
164 }
165
166 # Make a small file system to fill
167 umount $SCRATCH_DEV &> /dev/null
168 _scratch_mkfs_sized $(( 1024 * 1024 * 1024 )) &> /dev/null
169 _scratch_mount
170 # Test must be able to write files with non-root permissions
171 chmod 777 $SCRATCH_MNT
172
173 block_size=`stat -f $SCRATCH_DEV | grep "Block size" | cut -d " " -f3`
174 _test_full_fs_punch $(( $block_size * 2 )) $block_size 500 $SCRATCH_MNT/252.$$ $block_size
175
176 status=0 ; exit