dea3d081cdbe70f60ad356add264d39cf9f3b8d1
[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 -9 fsstress 2>/dev/null
44         wait
45         cd /
46         umount $SCRATCH_MNT 2>/dev/null
47         rm -f $tmp.*
48 }
49 trap "_cleanup; exit \$status" 0 1 2 3 15
50
51 _create()
52 {
53         dir=$1
54         count=$2
55
56         mkdir -p $dir
57         for i in $(seq 0 $count)
58         do
59                 touch $dir/$i
60         done
61 }
62
63 _rand_replace()
64 {
65         dir=$1
66         count=$2
67
68         # replace 5% of the dataset
69         for i in $(seq 0 $((count / 20)))
70         do
71                 file=$((RANDOM % count))
72                 rm -f $dir/$file
73                 touch $dir/$file
74         done
75 }
76
77 _cleaner()
78 {
79         dir=$1
80         iters=$2
81         mindirs=$3
82
83         iters=$((iters - mindirs))
84
85         for i in $(seq 1 $iters)
86         do
87                 need=$dir/dir$((i + mindirs))
88                 while [ ! -e $need ]
89                 do
90                         sleep 3
91                 done
92
93                 rm -rf $dir/dir$i
94         done
95 }
96
97 # real QA test starts here
98 _supported_fs xfs
99 _supported_os Linux
100
101 _require_scratch
102 _require_xfs_mkfs_finobt
103 _require_xfs_finobt
104
105 rm -f $seqres.full
106
107 _scratch_mkfs_xfs "-m crc=1,finobt=1 -d agcount=2" | \
108         _filter_mkfs 2>> $seqres.full
109 _scratch_mount
110
111 COUNT=20000     # number of files per directory
112 LOOPS=15        # last loop iteration
113 MINDIRS=2       # number of dirs for the cleaner to leave trailing behind the
114                 # most recent (no less than 2 to prevent an rm from trampling a
115                 # clone)
116
117 # create initial directory
118 _create $SCRATCH_MNT/dir1 $COUNT
119
120 # start background cleaner to remove old directories as new ones are created
121 _cleaner $SCRATCH_MNT $LOOPS $MINDIRS &
122
123 # start a background stress workload on the fs
124 $FSSTRESS_PROG -d $SCRATCH_MNT/fsstress -n 9999999 -p 2 -S t \
125         >> $seqres.full 2>&1 &
126
127 # Each cycle clones the current directory and makes a random file replacement
128 # pass on the new directory. The directory is copied to the next using hard
129 # links. The replacement pass then randomly removes and replaces ~5% of the
130 # content in the directory. Files replaced as such are effectively marked to be
131 # freed by the background cleaner as it moves forward and removes all of the
132 # previous hard links to the inode. Over several iterations, this workload
133 # creates a sparsely located set of a free inodes across the set and uses the
134 # finobt to allocate new inodes for replacement.
135
136 for i in $(seq 1 $LOOPS)
137 do
138         # hard link the content of the current directory to the next
139         cp -Rl $SCRATCH_MNT/dir$i $SCRATCH_MNT/dir$((i+1))
140
141         # do a random replacement of files in the new directory
142         _rand_replace $SCRATCH_MNT/dir$((i+1)) $COUNT
143 done
144
145 killall fsstress
146 wait
147
148 # clean out the competing fsstress allocations, then everything else
149 rm -rf $SCRATCH_MNT/fsstress
150 rm -rf $SCRATCH_MNT/dir*
151
152 umount $SCRATCH_MNT
153
154 status=0
155 exit