xfs: Check for extent overflow when trivally adding a new extent
[xfstests-dev.git] / tests / xfs / 186
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 186
6 #
7 # Test out:
8 # pv#979606: xfs bug in going from attr2 back to attr1
9 #
10 # Test bug in going from attr2 back to attr1 where xfs
11 # (due to xfs_attr_shortform_bytesfit)
12 # would reset the di_forkoff to the m_offset instead of
13 # leaving the di_forkoff alone as was intended.
14 #
15 # We create enough dirents to push us past m_attroffset,
16 # and create an EA so we have a fork offset
17 # and then turn on attr1 and add one more EA which
18 # will write over the shortform dirents.
19 #
20 seq=`basename $0`
21 seqres=$RESULT_DIR/$seq
22 echo "QA output created by $seq"
23
24 here=`pwd`
25 tmp=/tmp/$$
26 status=1        # failure is the default!
27 trap "_cleanup; exit \$status" 0 1 2 3 15
28
29 _cleanup()
30 {
31     cd /
32     rm -f $tmp.*
33 }
34
35 _create_dirents()
36 {
37         start_num=$1
38         end_num=$2
39         cd $fork_dir
40         for i in `seq $start_num $end_num`; do
41                 touch file.$i
42         done
43 }
44
45 _create_eas()
46 {
47         start_num=$1
48         end_num=$2
49         for i in `seq $start_num $end_num`; do
50                 $SETFATTR_PROG -n user.$i -v 0xbabe $fork_dir
51         done
52 }
53
54 _rmv_eas()
55 {
56         start_num=$1
57         end_num=$2
58         for i in `seq $start_num $end_num`; do
59                 $SETFATTR_PROG -x user.$i $fork_dir
60         done
61 }
62
63 # If filetype is available (-n ftype=1) will get v3 dirs;
64 # just filter/replace to make this look the same as if we
65 # had v2 dirs, as we are not interested in this info.
66 _filter_inode()
67 {
68         tee -a $seqres.full | \
69                 sed -e "s/core.forkoff/forkoff/g" | \
70                 egrep '^u.sfdir2|^u.sfdir3|^a.sfattr|forkoff' | \
71                 egrep -v 'inumber|parent' | \
72                 sed -e s/sfdir3/sfdir2/g | \
73                 grep -v filetype
74 }
75
76 _filter_version()
77 {
78         tee -a $seqres.full | tr ',' '\n' | grep ATTR
79 }
80
81 _print_inode()
82 {
83         echo ""
84         echo "================================="
85         _scratch_xfs_db -c "version" 2>&1 | _filter_version
86         _scratch_xfs_db -c "inode $inum" -c p 2>&1 | _filter_inode
87         echo "================================="
88 }
89
90 _do_eas()
91 {
92         echo ""
93         _scratch_mount
94         if [ $1 = "-r" ]; then 
95                 echo "*** remove EAs start $2 end $3 ***"
96                 _rmv_eas $2 $3
97         else
98                 echo "*** create EAs start $2 end $3 ***"
99                 _create_eas $2 $3
100         fi
101         echo ""
102         cd /; $UMOUNT_PROG $SCRATCH_MNT
103         _print_inode
104 }
105
106 _do_dirents()
107 {
108         num=`expr $2 - $1 + 1`
109         echo ""
110         echo "*** create $num dirents ***"
111         echo ""
112         _scratch_mount
113         _create_dirents $1 $2
114         cd /; $UMOUNT_PROG $SCRATCH_MNT
115         _print_inode
116 }
117
118 _changeto_attr1()
119 {
120         echo ""
121         echo "Try setting attr1 by db"
122         echo ""
123         _scratch_xfs_db -x -c "version attr1" | _filter_version
124 }
125
126 # get standard environment, filters and checks
127 . ./common/rc
128 . ./common/filter
129 . ./common/attr
130
131 # real QA test starts here
132
133 # Modify as appropriate.
134 _supported_fs xfs
135
136 _require_scratch
137 _require_attrs
138 _require_attr_v1
139
140 rm -f $seqres.full
141
142 _scratch_mkfs -i attr=2,size=512 -l lazy-count=1 | _filter_mkfs \
143         >>$seqres.full 2>$tmp.mkfs
144 # import crc status and attr version
145 . $tmp.mkfs
146
147 # _scratch_mkfs may ignore $MKFS_OPTIONS if mkfs fails due to conflicts between
148 # $MKFS_OPTIONS and the provided mkfs options, which could result in creating
149 # an XFS we don't want. Check crc status and attr version to be sure.
150 if [ $_fs_has_crcs -eq 1 ]; then
151         _notrun "attr v1 not supported on $SCRATCH_DEV"
152 fi
153 if [ $attr -ne 2 ]; then
154         _notrun "need attr v2 on $SCRATCH_DEV"
155 fi
156
157 # set inum to root dir ino
158 # we'll add in dirents and EAs into the root directory
159 inum=`_scratch_xfs_get_sb_field rootino`
160 fork_dir=$SCRATCH_MNT
161 _print_inode
162
163 # add enough dirents to be inline but more
164 # than will fit for m_attroffset for 512b inodes
165 # for attr2 this is not a problem
166 _do_dirents 1 25 
167
168 # add 1 ea so we get our forkoff happening 
169 _do_eas -c 1 1
170
171 # now change back to attr1 where forkoff is constant now
172 _changeto_attr1
173
174 # now add another EA
175 # for a bug in xfs_add_shortform_bytesfit
176 # where it resets the forkoff to m_attroffset>>3 instead of 
177 # leaving as di_forkoff
178 # If it resets to m_attroffset which is in the middle of
179 # the dirents then they will get corrupted
180 _do_eas -c 2 2
181
182 # success, all done
183 status=0
184 exit