btrfs-progs: introduce TEST_TOP and INTERNAL_BIN for tests
[platform/upstream/btrfs-progs.git] / tests / fsck-tests / 012-leaf-corruption / test.sh
1 #!/bin/bash
2
3 source "$TEST_TOP/common"
4
5 check_prereq btrfs-image
6
7 # Check file list for leaf corruption, no regular/preallocated
8 # file extent case.
9 # Corrupted leaf is 20832256, which contains inode 1862~1872
10 #
11 # 1862, ref from leaf 20828160 key 24(DIR_ITEM)
12 # 1863, ref from leaf 605388 item key 11(DIR_ITEM)
13 # 1864, no ref to rebuild, no need to rebuild
14 # 1865, ref from leaf 19767296 key 23(DIR_ITEM)
15 # 1866-1868 no ref to rebuild, all refs in corrupted leaf
16 # 1869, ref from leaf 4976640 key 22(DIR_ITEM)
17 # 1870 no ref to rebuild, all refs in corrupted leaf
18 # 1871, ref from leaf 19746816 key 38(DIR_ITEM)
19 # 1872, ref from leaf 19767296 key 14(DIR_ITEM)
20 # The list format is:
21 # INO SIZE MODE NAME
22 # INO: inode number
23 # SIZE: file size, only checked for regular file
24 # MODE: raw file mode, get from stat
25 # NAME: file name
26 leaf_no_data_ext_list=(
27         1862 0 40700 "install.d"
28         1862 0 40700 "install.d"
29         1863 0 40700 "gdb"
30         1865 0 40700 "iptables"
31         1869 0 40700 "snmp"
32         1871 0 100700 "machine-id"
33         1872 0 100700 "adjtime"
34         1877 0 40700 "del"
35 )
36
37 generate_leaf_corrupt_no_data_ext()
38 {
39         dest=$1
40         echo "generating leaf_corrupt_no_data_ext.btrfs-image" >> "$RESULTS"
41         tar --no-same-owner -xJf ./no_data_extent.tar.xz || \
42                 _fail "failed to extract leaf_corrupt_no_data_ext.btrfs-image"
43         "$TOP/btrfs-image" -r test.img.btrfs-image "$dest" || \
44                 _fail "failed to extract leaf_corrupt_no_data_ext.btrfs-image"
45
46         # leaf at 4206592 and 20905984 contains no regular data
47         # extent, clear its csum to corrupt the leaf.
48         for x in 4206592 20905984; do
49                 dd if=/dev/zero of="$dest" bs=1 count=32 conv=notrunc seek="$x" \
50                         1>/dev/null 2>&1
51         done
52 }
53
54 check_inode()
55 {
56         path=$1
57         ino=$2
58         size=$3
59         mode=$4
60         name=$5
61
62         # Check whether the inode exists
63         exists=$($SUDO_HELPER find "$path" -inum "$ino")
64         if [ -z "$exists" ]; then
65                 _fail "inode $ino not recovered correctly"
66         fi
67
68         # Check inode type
69         found_mode=$(printf "%o" 0x$($SUDO_HELPER stat "$exists" -c %f))
70         if [ "$found_mode" -ne "$mode" ]; then
71                 echo "$found_mode"
72                 _fail "inode $ino modes not recovered"
73         fi
74
75         # Check inode size
76         found_size=$($SUDO_HELPER stat "$exists" -c %s)
77         if [ $mode -ne 41700 -a "$found_size" -ne "$size" ]; then
78                 _fail "inode $ino size not recovered correctly"
79         fi
80
81         # Check inode name
82         if [ "$(basename $exists)" != "$name" ]; then
83                 _fail "inode $ino name not recovered correctly"
84         else
85                 return 0
86         fi
87 }
88
89 # Check salvaged data in the recovered image
90 check_leaf_corrupt_no_data_ext()
91 {
92         image=$1
93         $SUDO_HELPER mount -o loop -t btrfs "$image" -o ro "$TEST_MNT"
94
95         i=0
96         while [ $i -lt ${#leaf_no_data_ext_list[@]} ]; do
97                 check_inode "$TEST_MNT/lost+found" \
98                             ${leaf_no_data_ext_list[i]} \
99                             ${leaf_no_data_ext_list[i + 1]} \
100                             ${leaf_no_data_ext_list[i + 2]} \
101                             ${leaf_no_data_ext_list[i + 3]} \
102                             ${leaf_no_data_ext_list[i + 4]}
103                             ((i+=4))
104         done
105         $SUDO_HELPER umount "$TEST_MNT"
106 }
107
108 setup_root_helper
109
110 generate_leaf_corrupt_no_data_ext test.img
111 check_image test.img
112 check_leaf_corrupt_no_data_ext test.img
113
114 rm test.img
115 rm test.img.btrfs-image
116 # Not used, its function is the same as generate_leaf_corrupt_no_data_ext()
117 rm generate_image.sh