common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / tests / xfs / 013
1 #!/bin/bash
2 # FS QA Test No. xfs/013
3 #
4 # Exercise the free inode btree (finobt). XFS allocates physical inodes in
5 # chunks of 64. Inode records with at least one free inode are stored in the
6 # finobt to optimize free inode lookup. This test runs a workload that creates
7 # and modifies a sparsely allocated set of inodes in combination with an
8 # fsstress workload.
9 #
10 #-----------------------------------------------------------------------
11 # Copyright (c) 2014 Red Hat, Inc.  All Rights Reserved.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License as
15 # published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it would be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write the Free Software Foundation,
24 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25 #
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32
33 here=`pwd`
34 tmp=/tmp/$$
35 status=1        # failure is the default!
36
37 # get standard environment, filters and checks
38 . ./common/rc
39 . ./common/filter
40
41 _cleanup()
42 {
43         $KILLALL_PROG -9 fsstress 2>/dev/null
44         wait
45         cd /
46         _scratch_unmount 2>/dev/null
47         rm -f $tmp.*
48 }
49 trap "_cleanup; exit \$status" 0 1 2 3 15
50
51 filter_enospc() {
52         sed -e '/^.*No space left on device.*/d'
53 }
54
55 _create()
56 {
57         dir=$1
58         count=$2
59
60         mkdir -p $dir
61         for i in $(seq 0 $count)
62         do
63                 touch $dir/$i 2>&1 | filter_enospc
64         done
65 }
66
67 _rand_replace()
68 {
69         dir=$1
70         count=$2
71
72         # replace 5% of the dataset
73         for i in $(seq 0 $((count / 20)))
74         do
75                 file=$((RANDOM % count))
76                 rm -f $dir/$file
77                 touch $dir/$file 2>&1 | filter_enospc
78         done
79 }
80
81 _cleaner()
82 {
83         dir=$1
84         iters=$2
85         mindirs=$3
86
87         iters=$((iters - mindirs))
88
89         for i in $(seq 1 $iters)
90         do
91                 need=$dir/dir$((i + mindirs))
92                 while [ ! -e $need ]
93                 do
94                         sleep 3
95                         if ! pgrep fsstress > /dev/null 2>&1; then
96                                 echo "fsstress died?"
97                                 return
98                         fi
99                 done
100
101                 rm -rf $dir/dir$i
102         done
103 }
104
105 # real QA test starts here
106 _supported_fs xfs
107 _supported_os Linux
108
109 _require_scratch
110 _require_xfs_mkfs_finobt
111 _require_xfs_finobt
112 _require_command "$KILLALL_PROG" killall
113
114 rm -f $seqres.full
115
116 _scratch_mkfs_xfs "-m crc=1,finobt=1 -d agcount=2" | \
117         _filter_mkfs 2>> $seqres.full
118 _scratch_mount
119
120 COUNT=20000     # number of files per directory
121 LOOPS=15        # last loop iteration
122 MINDIRS=2       # number of dirs for the cleaner to leave trailing behind the
123                 # most recent (no less than 2 to prevent an rm from trampling a
124                 # clone)
125
126 # create initial directory
127 _create $SCRATCH_MNT/dir1 $COUNT
128
129 # start background cleaner to remove old directories as new ones are created
130 _cleaner $SCRATCH_MNT $LOOPS $MINDIRS &
131
132 # start a background stress workload on the fs
133 $FSSTRESS_PROG -d $SCRATCH_MNT/fsstress -n 9999999 -p 2 -S t \
134         >> $seqres.full 2>&1 &
135
136 # Each cycle clones the current directory and makes a random file replacement
137 # pass on the new directory. The directory is copied to the next using hard
138 # links. The replacement pass then randomly removes and replaces ~5% of the
139 # content in the directory. Files replaced as such are effectively marked to be
140 # freed by the background cleaner as it moves forward and removes all of the
141 # previous hard links to the inode. Over several iterations, this workload
142 # creates a sparsely located set of a free inodes across the set and uses the
143 # finobt to allocate new inodes for replacement.
144
145 for i in $(seq 1 $LOOPS)
146 do
147         # hard link the content of the current directory to the next
148         while ! test -d $SCRATCH_MNT/dir$((i+1)); do
149                 cp -Rl $SCRATCH_MNT/dir$i $SCRATCH_MNT/dir$((i+1)) 2>&1 | \
150                         filter_enospc
151         done
152
153         # do a random replacement of files in the new directory
154         _rand_replace $SCRATCH_MNT/dir$((i+1)) $COUNT
155 done
156
157 $KILLALL_PROG fsstress
158 wait
159
160 # clean out the competing fsstress allocations, then everything else
161 rm -rf $SCRATCH_MNT/fsstress
162 rm -rf $SCRATCH_MNT/dir*
163
164 _scratch_unmount
165
166 status=0
167 exit