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