generic: test for file loss after mix of rename, fsync and inode eviction
[xfstests-dev.git] / tests / generic / 256
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2011 IBM Corporation.  All Rights Reserved.
4 #
5 # FS QA Test No. 256
6 #
7 # Test Full File System Hole Punching
8 #
9 . ./common/preamble
10 _begin_fstest auto quick punch
11
12 # Import common functions.
13 . ./common/populate
14 . ./common/filter
15 . ./common/punch
16
17 # real QA test starts here
18 _supported_fs generic
19
20 _require_xfs_io_command "fpunch"
21 _require_scratch
22 _require_user
23 _require_test
24
25 testfile=$TEST_DIR/256.$$
26
27 # _test_full_fs_punch()
28 #
29 # This function will test that a hole may be punched
30 # even when the file system is full.  Reserved blocks
31 # should be used to allow a punch hole to proceed even
32 # when there is not enough blocks to further fragment the
33 # file. To test this, this function will fragment the file
34 # system by punching holes in regular intervals and filling
35 # the file system between punches.
36 #
37 _test_full_fs_punch()
38 {
39         local hole_len=$1      # The length of the holes to punch
40         local hole_interval=$2 # The interval between the holes
41         local iterations=$3    # The number of holes to punch
42         local file_name=$4     # File to punch holes in
43         local block_size=$5    # File system block size
44         local file_len=$(( $(( $hole_len + $hole_interval )) * $iterations ))
45         local path=`dirname $file_name`
46         local hole_offset=0
47
48         if [ $# -ne 5 ]
49         then
50                 echo "USAGE: _test_full_fs_punch hole_len hole_interval iterations file_name block_size"
51                 exit 1
52         fi
53
54         rm -f $file_name &> /dev/null
55
56         $XFS_IO_PROG -f -c "pwrite 0 $file_len" \
57                 -c "fsync" $file_name &> /dev/null
58         chmod 666 $file_name
59
60         # All files are created as a non root user to prevent reserved blocks
61         # from being consumed.
62         _fill_fs $(( 1024 * 1024 * 1024 )) $path/fill $block_size 1 \
63                 > /dev/null 2>&1
64
65         for (( i=0; i<$iterations; i++ ))
66         do
67                 # This part must not be done as root in order to
68                 # test that reserved blocks are used when needed
69                 _user_do "$XFS_IO_PROG -f -c \"fpunch $hole_offset $hole_len\" $file_name"
70                 rc=$?
71                 if [ $? -ne 0 ] ; then
72                         echo Punch hole failed
73                         break
74                 fi
75
76                 hole_offset=$(( $hole_offset + $hole_len + $hole_interval ))
77
78                 _fill_fs $hole_len $path/fill.$i $block_size 1 > /dev/null 2>&1
79
80         done
81 }
82
83 # Make a small file system to fill
84 _scratch_unmount &> /dev/null
85 _scratch_mkfs_sized $(( 1536 * 1024 * 1024 )) &> /dev/null
86 _scratch_mount
87 # Test must be able to write files with non-root permissions
88 chmod 777 $SCRATCH_MNT
89
90 block_size=`_get_block_size $SCRATCH_MNT`
91 _test_full_fs_punch $(( $block_size * 2 )) $block_size 500 $SCRATCH_MNT/252.$$ $block_size
92
93 status=0 ; exit