common/xfs: refactor commands to select a particular xfs backing device
[xfstests-dev.git] / tests / xfs / 042
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 042
6 #
7 # xfs_fsr QA tests
8 # create a large fragmented file and check that xfs_fsr doesn't corrupt
9 # it or the other contents of the filesystem
10 #
11 set +x
12
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1        # failure is the default!
20
21 _cleanup()
22 {
23     _scratch_unmount
24     rm -f $tmp.*
25 }
26 trap "_cleanup ; exit \$status" 0 1 2 3 15
27
28 # get standard environment, filters and checks
29 . ./common/rc
30 . ./common/filter
31
32 # real QA test starts here
33 _supported_fs xfs
34 _require_xfs_io_command "falloc"
35
36 _require_scratch
37
38 [ "$XFS_FSR_PROG" = "" ] && _notrun "xfs_fsr not found"
39
40 # Test performs several operations to produce a badly fragmented file, then
41 # create enough contiguous free space for xfs_fsr to defragment the fragmented
42 # file:
43 #
44 # - create fs with 3 minimum sized (16Mb) allocation groups
45 # - create 16x1MB contiguous files which will become large free space extents
46 #   when deleted
47 # - put a small "space" between each of the 16 contiuguous files to ensure we
48 #   have separated free space extents
49 # - fill the remaining free space with a "fill file"
50 # - mount/unmount/fill remaining free space with a pad file
51 # - punch alternate single block holes in the the "fill file" to create
52 #   fragmented free space.
53 # - use fill2 to generate a very large fragmented file
54 # - delete the 16 large contiguous files created initially
55 # - run xfs_fsr on the filesystem
56 # - check checksums for remaining files
57
58 rm -f $seqres.full
59 _do_die_on_error=message_only
60
61 echo -n "Make a 48 megabyte filesystem on SCRATCH_DEV and mount... "
62 _scratch_mkfs_xfs -dsize=48m,agcount=3 2>&1 >/dev/null || _fail "mkfs failed"
63 _scratch_mount
64
65 echo "done"
66
67 echo -n "Reserve 16 1Mb unfragmented regions... "
68 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
69 do
70         _do "$XFS_IO_PROG -f -c \"resvsp 0 1m\" $SCRATCH_MNT/hole$i"
71         _do "$XFS_IO_PROG -f -c \"resvsp 0 4k\" $SCRATCH_MNT/space$i"
72         _do "xfs_bmap -vp $SCRATCH_MNT/hole$i"
73 done
74 echo "done" 
75
76 # set up filesystem
77 echo -n "Fill filesystem with fill file... "
78 for i in `seq 0 1 31`; do
79         _do "$XFS_IO_PROG -f -c \"falloc ${i}m 1m\" $SCRATCH_MNT/fill"
80 done
81 _do "xfs_bmap -vp $SCRATCH_MNT/fill"
82 echo "done"
83 # flush the filesystem - make sure there is no space "lost" to pre-allocation
84 _do "_scratch_unmount"
85 _do "_try_scratch_mount"
86 echo -n "Use up any further available space... "
87 _do "$XFS_IO_PROG -f -c \"falloc 0 1m\" $SCRATCH_MNT/pad"
88 echo "done"
89
90 # create fragmented file
91 #_do "Delete every second file" "_cull_files"
92 echo -n "Punch every second 4k block... "
93 for i in `seq 0 8 32768`; do
94         # This generates excessive output that significantly slows down the
95         # test. It's not necessary for debug, so just bin it.
96         $XFS_IO_PROG -f -c "unresvsp ${i}k 4k" $SCRATCH_MNT/fill \
97                                                                 > /dev/null 2>&1
98 done
99 _do "xfs_bmap -vp $SCRATCH_MNT/fill"
100 _do "sum $SCRATCH_MNT/fill >$tmp.fillsum1"
101 echo "done"
102
103 echo -n "Create one very large file... "
104 _do "$here/src/fill2 -d nbytes=16000000,file=$SCRATCH_MNT/fragmented"
105 echo "done"
106 _do "xfs_bmap -v $SCRATCH_MNT/fragmented"
107 _do "sum $SCRATCH_MNT/fragmented >$tmp.sum1"
108 _do "Remove other files" "rm -rf $SCRATCH_MNT/{pad,hole*}"
109
110 # defragment
111 _do "Run xfs_fsr on filesystem" "$XFS_FSR_PROG -v $SCRATCH_MNT/fragmented"
112 _do "xfs_bmap -v $SCRATCH_MNT/fragmented"
113
114 echo -n "Check fill file... "
115 _do "sum $SCRATCH_MNT/fill >$tmp.fillsum2"
116 if ! _do "diff $tmp.fillsum1 $tmp.fillsum2"; then
117     echo "fail"
118     echo "Fill file is corrupt/missing after fsr. Test failed see $seqres.full"
119     status=1; exit
120 fi
121 echo "done"
122
123 # check
124 echo -n "Check large file... "
125 _do "sum $SCRATCH_MNT/fragmented >$tmp.sum2"
126 if ! _do "diff $tmp.sum1 $tmp.sum2"; then
127     echo "fail"
128     echo "File is corrupt/missing after fsr. Test failed see $seqres.full"
129     status=1; exit
130 fi
131 echo "done"
132
133 # success, all done
134 echo "xfs_fsr tests passed."
135 status=0 ; exit