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