9b17b6a4f5be8b1abe06bad6cd971e10397b3922
[xfstests-dev.git] / tests / xfs / 328
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 328
6 #
7 # See how well xfs_fsr handles "defragging" a file with a hojillion extents.
8 #
9 seq=`basename "$0"`
10 seqres="$RESULT_DIR/$seq"
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1    # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         cd /
21         rm -rf "$tmp".*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27 . ./common/attr
28 . ./common/reflink
29
30 # real QA test starts here
31 _supported_fs xfs
32 _require_scratch_reflink
33 _require_cp_reflink
34 _require_test_program "punch-alternating"
35 _require_xfs_io_command "falloc" # used in FSR
36 _require_command "$XFS_FSR_PROG" "xfs_fsr"
37
38 rm -f "$seqres.full"
39
40 echo "Format and mount"
41 _scratch_mkfs > "$seqres.full" 2>&1
42 _scratch_mount >> "$seqres.full" 2>&1
43
44 testdir="$SCRATCH_MNT/test-$seq"
45 mkdir "$testdir"
46
47 # Setup for 16000 blocks, but we'll accept stress testing down to
48 # 2^10 blocks... that should be plenty for anyone.
49 fnr=$((12 + LOAD_FACTOR))
50 free_blocks=$(stat -f -c '%a' "$testdir")
51 blksz=$(_get_block_size $testdir)
52 space_avail=$((free_blocks * blksz))
53 calc_space()
54 {
55         blocks_needed=$(( 2 ** (fnr + 1) ))
56         space_needed=$((blocks_needed * blksz * 5 / 4))
57 }
58 calc_space
59 while test $space_needed -gt $space_avail; do
60         fnr=$((fnr - 1))
61         calc_space
62 done
63 test $fnr -lt 10 && _notrun "Insufficient space for stress test; would only create $blocks_needed extents."
64 bytes=$((blocks_needed * blksz))
65
66 echo "Create a many-block file"
67 echo "creating $blocks_needed blocks..." >> "$seqres.full"
68 _pwrite_byte 0x62 0 $blksz $testdir/file0 >> $seqres.full
69 $XFS_IO_PROG -f -c "pwrite -S 0x61 -b 4194304 0 $bytes" "$testdir/file1" >> "$seqres.full"
70 echo "punching..." >> "$seqres.full"
71 "$here/src/punch-alternating" "$testdir/file1" >> "$seqres.full"
72 seq 0 2 $((2 ** (fnr + 1) )) | while read lblk; do
73         _reflink_range $testdir/file0 0 $testdir/file1 $((lblk * blksz)) $blksz >> $seqres.full
74 done
75 echo "...done" >> "$seqres.full"
76 _scratch_cycle_mount
77
78 echo "Reflink the big file"
79 echo "reflinking $((blocks_needed / 2)) blocks, $((bytes / 2)) bytes" >> "$seqres.full"
80 _reflink_range "$testdir/file1" 0 "$testdir/file2" 0 $bytes >> "$seqres.full"
81
82 echo "Defrag the big file"
83 old_nextents=$(_count_extents $testdir/file1)
84 $XFS_FSR_PROG -v -d $testdir/file1 >> $seqres.full
85 new_nextents=$(_count_extents $testdir/file1)
86
87 echo "Check extent count"
88 $XFS_IO_PROG -c 'stat -v' $testdir/file1 >> $seqres.full
89 $XFS_IO_PROG -c 'stat -v' $testdir/file2 >> $seqres.full
90 echo "extents: $old_nextents -> $new_nextents" >> $seqres.full
91 test $old_nextents -gt $new_nextents || echo "FAIL: $old_nextents -> $new_nextents"
92
93 # success, all done
94 status=0
95 exit