xfs/014: try a few times to create speculative preallocations
[xfstests-dev.git] / tests / xfs / 014
index 875ab400ca38bb86372d1c581a7f6b4c0b17c525..1f0ebac3447ce5facb49053f6ffcaba04e9df729 100755 (executable)
@@ -1,4 +1,7 @@
 #!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
+#
 # FS QA Test No. xfs/014
 #
 # Test the behavior of XFS dynamic speculative preallocation at ENOSPC and
 # new writers reclaim said preallocations rather than prematurely fail with
 # ENOSPC/EDQUOT.
 #
-#-----------------------------------------------------------------------
-# Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation.
-#
-# This program is distributed in the hope that it would be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write the Free Software Foundation,
-# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#
-#-----------------------------------------------------------------------
-#
-
-seq=`basename $0`
-seqres=$RESULT_DIR/$seq
-echo "QA output created by $seq"
+. ./common/preamble
+_begin_fstest auto enospc quick quota
 
-here=`pwd`
-tmp=/tmp/$$
-status=1       # failure is the default!
-
-# get standard environment, filters and checks
-. ./common/rc
+# Import common functions.
 . ./common/filter
 . ./common/quota
 
+# Override the default cleanup function.
 _cleanup()
 {
        cd /
@@ -47,7 +26,6 @@ _cleanup()
        _scratch_unmount 2>/dev/null
        rm -f $tmp.*
 }
-trap "_cleanup; exit \$status" 0 1 2 3 15
 
 # Create a file using a repeated open, extending write and close pattern. This
 # causes the preallocation to persist after the file is closed. Preallocation
@@ -55,27 +33,36 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 # failure.
 _spec_prealloc_file()
 {
-       file=$1
-
-       rm -f $file
-
-       # a few file extending open-write-close cycles should be enough to
-       # trigger the fs to retain preallocation. write 256k in 32k intervals to
-       # be sure
-       for i in $(seq 0 32768 262144); do
-               $XFS_IO_PROG -f -c "pwrite $i 32k" $file >> $seqres.full
+       local file=$1
+       local prealloc_size=0
+       local i=0
+
+       # Now that we have background garbage collection processes that can be
+       # triggered by low space/quota conditions, it's possible that we won't
+       # succeed in creating a speculative preallocation on the first try.
+       for ((tries = 0; tries < 5 && prealloc_size == 0; tries++)); do
+               rm -f $file
+
+               # a few file extending open-write-close cycles should be enough
+               # to trigger the fs to retain preallocation. write 256k in 32k
+               # intervals to be sure
+               for i in $(seq 0 32768 262144); do
+                       $XFS_IO_PROG -f -c "pwrite $i 32k" $file >> $seqres.full
+               done
+
+               # write a 4k aligned amount of data to keep the calculations
+               # simple
+               $XFS_IO_PROG -c "pwrite 0 128m" $file >> $seqres.full
+
+               size=`_get_filesize $file`
+               blocks=`stat -c "%b" $file`
+               blocksize=`stat -c "%B" $file`
+
+               prealloc_size=$((blocks * blocksize - size))
        done
 
-       # write a 4k aligned amount of data to keep the calculations simple
-       $XFS_IO_PROG -c "pwrite 0 128m" $file >> $seqres.full
-
-       size=`stat -c "%s" $file`
-       blocks=`stat -c "%b" $file`
-       blocksize=`stat -c "%B" $file`
-
-       prealloc_size=$((blocks * blocksize - size))
        if [ $prealloc_size -eq 0 ]; then
-               echo "Warning: No speculative preallocation for $file." \
+               echo "Warning: No speculative preallocation for $file after $tries iterations." \
                        "Check use of the allocsize= mount option."
        fi
 
@@ -111,6 +98,9 @@ _test_enospc()
 
        # consume 1/2 of the current preallocation across the set of 4 writers
        write_size=$((TOTAL_PREALLOC / 2 / 4))
+       for i in $(seq 0 3); do
+               touch $dir/file.$i
+       done
        for i in $(seq 0 3); do
                $XFS_IO_PROG -f -c "pwrite 0 $write_size" $dir/file.$i \
                        >> $seqres.full &
@@ -157,7 +147,6 @@ _test_edquot()
 
 # real QA test starts here
 _supported_fs xfs
-_supported_os Linux
 
 _require_scratch
 _require_xfs_io_command "falloc"
@@ -166,8 +155,6 @@ _require_quota
 _require_user
 _require_group
 
-rm -f $seqres.full
-
 echo "Silence is golden."
 
 _scratch_mkfs_xfs >> $seqres.full 2>&1