Btrfs-progs: check, ability to detect and fix outdated snapshot root items
[platform/upstream/btrfs-progs.git] / tests / fsck-tests.sh
1 #!/bin/bash
2 #
3 # loop through all of our bad images and make sure fsck repairs them properly
4 #
5 # It's GPL, same as everything else in this tree.
6 #
7
8 here=`pwd`
9 TEST_DEV=
10 TEST_MNT=
11 RESULT="fsck-tests-results.txt"
12
13 _fail()
14 {
15         echo "$*" | tee -a $RESULT
16         exit 1
17 }
18
19 run_check()
20 {
21         echo "############### $@" >> $RESULT 2>&1
22         "$@" >> $RESULT 2>&1 || _fail "failed: $@"
23 }
24
25 rm -f $RESULT
26
27 # test rely on corrupting blocks tool
28 run_check make btrfs-corrupt-block
29
30 # Some broken filesystem images are kept as .img files, created by the tool
31 # btrfs-image, and others are kept as .tar.xz files that contain raw filesystem
32 # image (the backing file of a loop device, as a sparse file). The reason for
33 # keeping some as tarballs of raw images is that for these cases btrfs-image
34 # isn't able to preserve all the (bad) filesystem structure for some reason.
35 for i in $(find $here/tests/fsck-tests -name '*.img' -o -name '*.tar.xz')
36 do
37         echo "     [TEST]    $(basename $i)"
38         echo "testing image $i" >> $RESULT
39
40         extension=${i#*.}
41
42         if [ $extension == "img" ]; then
43                 run_check $here/btrfs-image -r $i test.img
44         else
45                 run_check tar xJf $i
46         fi
47
48         $here/btrfsck test.img >> $RESULT 2>&1
49         [ $? -eq 0 ] && _fail "btrfsck should have detected corruption"
50
51         run_check $here/btrfsck --repair test.img
52         run_check $here/btrfsck test.img
53 done
54
55 if [ -z $TEST_DEV ] || [ -z $TEST_MNT ];then
56         echo "     [NOTRUN] extent tree rebuild"
57         exit 0
58 fi
59
60 # test whether fsck can rebuild a corrupted extent tree
61 test_extent_tree_rebuild()
62 {
63         echo "     [TEST]    extent tree rebuild"
64         $here/mkfs.btrfs -f $TEST_DEV >> /dev/null 2>&1 || _fail "fail to mkfs"
65
66         run_check mount $TEST_DEV $TEST_MNT
67         cp -aR /lib/modules/`uname -r`/ $TEST_MNT 2>&1
68
69         for i in `seq 1 100`;do
70                 $here/btrfs sub snapshot $TEST_MNT \
71                         $TEST_MNT/snapaaaaaaa_$i >& /dev/null
72         done
73         run_check umount $TEST_DEV
74
75         # get extent root bytenr
76         extent_root_bytenr=`$here/btrfs-debug-tree -r $TEST_DEV | grep extent | awk '{print $7}'`
77         if [ -z $extent_root_bytenr ];then
78                 _fail "fail to get extent root bytenr"
79         fi
80
81         # corrupt extent root node block
82         run_check $here/btrfs-corrupt-block -l $extent_root_bytenr \
83                 -b 4096 $TEST_DEV
84
85         $here/btrfs check $TEST_DEV >& /dev/null && \
86                         _fail "fsck should detect failure"
87         run_check $here/btrfs check --init-extent-tree $TEST_DEV
88         run_check $here/btrfs check $TEST_DEV
89 }
90
91 test_extent_tree_rebuild