misc: move exit status into trap handler
[xfstests-dev.git] / tests / xfs / 013
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. xfs/013
6 #
7 # Exercise the free inode btree (finobt). XFS allocates physical inodes in
8 # chunks of 64. Inode records with at least one free inode are stored in the
9 # finobt to optimize free inode lookup. This test runs a workload that creates
10 # and modifies a sparsely allocated set of inodes in combination with an
11 # fsstress workload.
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 # get standard environment, filters and checks
22 . ./common/rc
23 . ./common/filter
24
25 _cleanup()
26 {
27         $KILLALL_PROG -9 fsstress 2>/dev/null
28         wait
29         cd /
30         _scratch_unmount 2>/dev/null
31         rm -f $tmp.*
32 }
33 trap "_cleanup; exit \$status" 0 1 2 3 15
34
35 filter_enospc() {
36         sed -e '/^.*No space left on device.*/d'
37 }
38
39 _create()
40 {
41         dir=$1
42         count=$2
43
44         mkdir -p $dir
45         for i in $(seq 0 $count)
46         do
47                 touch $dir/$i 2>&1 | filter_enospc
48         done
49 }
50
51 _rand_replace()
52 {
53         dir=$1
54         count=$2
55
56         # replace 5% of the dataset
57         for i in $(seq 0 $((count / 20)))
58         do
59                 file=$((RANDOM % count))
60                 rm -f $dir/$file
61                 touch $dir/$file 2>&1 | filter_enospc
62         done
63 }
64
65 _cleaner()
66 {
67         dir=$1
68         iters=$2
69         mindirs=$3
70
71         iters=$((iters - mindirs))
72
73         for i in $(seq 1 $iters)
74         do
75                 need=$dir/dir$((i + mindirs))
76                 while [ ! -e $need ]
77                 do
78                         sleep 3
79                         if ! pgrep fsstress > /dev/null 2>&1; then
80                                 echo "fsstress died?"
81                                 return
82                         fi
83                 done
84
85                 rm -rf $dir/dir$i
86         done
87 }
88
89 # real QA test starts here
90 _supported_fs xfs
91
92 _require_scratch
93 _require_xfs_mkfs_finobt
94 _require_xfs_finobt
95 _require_command "$KILLALL_PROG" killall
96
97 rm -f $seqres.full
98
99 _scratch_mkfs_xfs "-m crc=1,finobt=1 -d agcount=2" | \
100         _filter_mkfs 2>> $seqres.full
101 _scratch_mount
102
103 COUNT=20000     # number of files per directory
104 LOOPS=15        # last loop iteration
105 MINDIRS=2       # number of dirs for the cleaner to leave trailing behind the
106                 # most recent (no less than 2 to prevent an rm from trampling a
107                 # clone)
108
109 # create initial directory
110 _create $SCRATCH_MNT/dir1 $COUNT
111
112 # start background cleaner to remove old directories as new ones are created
113 _cleaner $SCRATCH_MNT $LOOPS $MINDIRS &
114
115 # start a background stress workload on the fs
116 $FSSTRESS_PROG -d $SCRATCH_MNT/fsstress -n 9999999 -p 2 -S t \
117         >> $seqres.full 2>&1 &
118
119 # Each cycle clones the current directory and makes a random file replacement
120 # pass on the new directory. The directory is copied to the next using hard
121 # links. The replacement pass then randomly removes and replaces ~5% of the
122 # content in the directory. Files replaced as such are effectively marked to be
123 # freed by the background cleaner as it moves forward and removes all of the
124 # previous hard links to the inode. Over several iterations, this workload
125 # creates a sparsely located set of a free inodes across the set and uses the
126 # finobt to allocate new inodes for replacement.
127
128 for i in $(seq 1 $LOOPS)
129 do
130         # hard link the content of the current directory to the next
131         while ! test -d $SCRATCH_MNT/dir$((i+1)); do
132                 cp -Rl $SCRATCH_MNT/dir$i $SCRATCH_MNT/dir$((i+1)) 2>&1 | \
133                         filter_enospc
134         done
135
136         # do a random replacement of files in the new directory
137         _rand_replace $SCRATCH_MNT/dir$((i+1)) $COUNT
138 done
139
140 $KILLALL_PROG fsstress
141 wait
142
143 # clean out the competing fsstress allocations, then everything else
144 rm -rf $SCRATCH_MNT/fsstress
145 rm -rf $SCRATCH_MNT/dir*
146
147 _scratch_unmount
148
149 status=0
150 exit